Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "monitor.h" |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Mark Salyzyn | 280a162 | 2016-10-17 14:11:20 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 24 | #include <log/log_event_list.h> |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 26 | #include "art_method.h" |
| 27 | #include "thread.h" |
| 28 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 29 | #define EVENT_LOG_TAG_dvm_lock_sample 20003 |
| 30 | |
| 31 | namespace art { |
| 32 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 33 | void Monitor::LogContentionEvent(Thread* self, |
| 34 | uint32_t wait_ms, |
| 35 | uint32_t sample_percent, |
| 36 | ArtMethod* owner_method, |
| 37 | uint32_t owner_dex_pc) { |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 38 | android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample); |
| 39 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 40 | const char* owner_filename; |
| 41 | int32_t owner_line_number; |
| 42 | TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number); |
| 43 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 44 | // Emit the process name, <= 37 bytes. |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 45 | { |
| 46 | int fd = open("/proc/self/cmdline", O_RDONLY); |
| 47 | char procName[33]; |
| 48 | memset(procName, 0, sizeof(procName)); |
| 49 | read(fd, procName, sizeof(procName) - 1); |
| 50 | close(fd); |
| 51 | ctx << procName; |
| 52 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 53 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 54 | // Emit the sensitive thread ("main thread") status. We follow tradition that this corresponds |
| 55 | // to a C++ bool's value, but be explicit. |
| 56 | constexpr uint32_t kIsSensitive = 1u; |
| 57 | constexpr uint32_t kIsNotSensitive = 0u; |
| 58 | ctx << (Thread::IsSensitiveThread() ? kIsSensitive : kIsNotSensitive); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 59 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 60 | // Emit self thread name string. |
| 61 | { |
| 62 | std::string thread_name; |
| 63 | self->GetThreadName(thread_name); |
| 64 | ctx << thread_name; |
| 65 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 66 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 67 | // Emit the wait time. |
| 68 | ctx << wait_ms; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 69 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 70 | const char* filename = nullptr; |
| 71 | { |
| 72 | uint32_t pc; |
| 73 | ArtMethod* m = self->GetCurrentMethod(&pc); |
| 74 | int32_t line_number; |
| 75 | TranslateLocation(m, pc, &filename, &line_number); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 77 | // Emit the source code file name. |
| 78 | ctx << filename; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 79 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 80 | // Emit the source code line number. |
| 81 | ctx << line_number; |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 82 | |
| 83 | // Emit the method name. |
| 84 | ctx << ArtMethod::PrettyMethod(m); |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Emit the lock owner source code file name. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 88 | if (owner_filename == nullptr) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 89 | owner_filename = ""; |
| 90 | } else if (strcmp(filename, owner_filename) == 0) { |
| 91 | // Common case, so save on log space. |
| 92 | owner_filename = "-"; |
| 93 | } |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 94 | ctx << owner_filename; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 95 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 96 | // Emit the source code line number. |
| 97 | ctx << owner_line_number; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 99 | // Emit the owner method name. |
| 100 | ctx << ArtMethod::PrettyMethod(owner_method); |
| 101 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 102 | // Emit the sample percentage. |
| 103 | ctx << sample_percent; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 104 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 105 | ctx << LOG_ID_EVENTS; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace art |