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" |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 27 | #include "jni/jni_env_ext.h" |
| 28 | #include "palette/palette.h" |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 29 | #include "thread.h" |
| 30 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 31 | #define EVENT_LOG_TAG_dvm_lock_sample 20003 |
| 32 | |
| 33 | namespace art { |
| 34 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 35 | void Monitor::LogContentionEvent(Thread* self, |
| 36 | uint32_t wait_ms, |
| 37 | uint32_t sample_percent, |
| 38 | ArtMethod* owner_method, |
| 39 | uint32_t owner_dex_pc) { |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 40 | android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample); |
| 41 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 42 | const char* owner_filename; |
| 43 | int32_t owner_line_number; |
| 44 | TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number); |
| 45 | |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 46 | // Emit the process name, <= 33 bytes. |
| 47 | char proc_name[33] = {}; |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 48 | { |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 49 | int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC); |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 50 | read(fd, proc_name, sizeof(proc_name) - 1); |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 51 | close(fd); |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 52 | ctx << proc_name; |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 53 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 54 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 55 | // Emit the sensitive thread ("main thread") status. We follow tradition that this corresponds |
| 56 | // to a C++ bool's value, but be explicit. |
| 57 | constexpr uint32_t kIsSensitive = 1u; |
| 58 | constexpr uint32_t kIsNotSensitive = 0u; |
| 59 | ctx << (Thread::IsSensitiveThread() ? kIsSensitive : kIsNotSensitive); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 60 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 61 | // Emit self thread name string. |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 62 | std::string thread_name; |
| 63 | self->GetThreadName(thread_name); |
| 64 | ctx << thread_name; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 65 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 66 | // Emit the wait time. |
| 67 | ctx << wait_ms; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 68 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 69 | const char* filename = nullptr; |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 70 | int32_t line_number; |
| 71 | std::string method_name; |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 72 | { |
| 73 | uint32_t pc; |
| 74 | ArtMethod* m = self->GetCurrentMethod(&pc); |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 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. |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 84 | method_name = ArtMethod::PrettyMethod(m); |
| 85 | ctx << method_name; |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Emit the lock owner source code file name. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 89 | if (owner_filename == nullptr) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 90 | owner_filename = ""; |
| 91 | } else if (strcmp(filename, owner_filename) == 0) { |
| 92 | // Common case, so save on log space. |
| 93 | owner_filename = "-"; |
| 94 | } |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 95 | ctx << owner_filename; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 96 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 97 | // Emit the source code line number. |
| 98 | ctx << owner_line_number; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 99 | |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 100 | // Emit the owner method name. |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 101 | std::string owner_method_name = ArtMethod::PrettyMethod(owner_method); |
| 102 | ctx << owner_method_name; |
Andreas Gampe | d2c03b5 | 2017-06-02 15:34:55 -0700 | [diff] [blame] | 103 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 104 | // Emit the sample percentage. |
| 105 | ctx << sample_percent; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 106 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame] | 107 | ctx << LOG_ID_EVENTS; |
Nicolas Geoffray | e261356 | 2021-08-18 22:46:32 +0100 | [diff] [blame] | 108 | |
| 109 | // Now report to other interested parties. |
| 110 | PaletteReportLockContention(self->GetJniEnv(), |
| 111 | wait_ms, |
| 112 | filename, |
| 113 | line_number, |
| 114 | method_name.c_str(), |
| 115 | owner_filename, |
| 116 | owner_line_number, |
| 117 | owner_method_name.c_str(), |
| 118 | proc_name, |
| 119 | thread_name.c_str()); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace art |