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 | #include "thread.h" |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | 280a162 | 2016-10-17 14:11:20 -0700 | [diff] [blame] | 24 | #include <log/log.h> |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 25 | #include <log/log_event_list.h> |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 26 | |
| 27 | #define EVENT_LOG_TAG_dvm_lock_sample 20003 |
| 28 | |
| 29 | namespace art { |
| 30 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 31 | void Monitor::LogContentionEvent(Thread* self, |
| 32 | uint32_t wait_ms, |
| 33 | uint32_t sample_percent, |
| 34 | ArtMethod* owner_method, |
| 35 | uint32_t owner_dex_pc) { |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 36 | android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample); |
| 37 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame] | 38 | const char* owner_filename; |
| 39 | int32_t owner_line_number; |
| 40 | TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number); |
| 41 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 42 | // Emit the process name, <= 37 bytes. |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 43 | { |
| 44 | int fd = open("/proc/self/cmdline", O_RDONLY); |
| 45 | char procName[33]; |
| 46 | memset(procName, 0, sizeof(procName)); |
| 47 | read(fd, procName, sizeof(procName) - 1); |
| 48 | close(fd); |
| 49 | ctx << procName; |
| 50 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 51 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 52 | // Emit the sensitive thread ("main thread") status. We follow tradition that this corresponds |
| 53 | // to a C++ bool's value, but be explicit. |
| 54 | constexpr uint32_t kIsSensitive = 1u; |
| 55 | constexpr uint32_t kIsNotSensitive = 0u; |
| 56 | ctx << (Thread::IsSensitiveThread() ? kIsSensitive : kIsNotSensitive); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 57 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 58 | // Emit self thread name string. |
| 59 | { |
| 60 | std::string thread_name; |
| 61 | self->GetThreadName(thread_name); |
| 62 | ctx << thread_name; |
| 63 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 64 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 65 | // Emit the wait time. |
| 66 | ctx << wait_ms; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 68 | const char* filename = nullptr; |
| 69 | { |
| 70 | uint32_t pc; |
| 71 | ArtMethod* m = self->GetCurrentMethod(&pc); |
| 72 | int32_t line_number; |
| 73 | TranslateLocation(m, pc, &filename, &line_number); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 74 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 75 | // Emit the source code file name. |
| 76 | ctx << filename; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 78 | // Emit the source code line number. |
| 79 | ctx << line_number; |
| 80 | } |
| 81 | |
| 82 | // Emit the lock owner source code file name. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 83 | if (owner_filename == nullptr) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 84 | owner_filename = ""; |
| 85 | } else if (strcmp(filename, owner_filename) == 0) { |
| 86 | // Common case, so save on log space. |
| 87 | owner_filename = "-"; |
| 88 | } |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 89 | ctx << owner_filename; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 90 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 91 | // Emit the source code line number. |
| 92 | ctx << owner_line_number; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 93 | |
Andreas Gampe | e59cb81 | 2017-06-02 14:26:06 -0700 | [diff] [blame^] | 94 | // Emit the sample percentage. |
| 95 | ctx << sample_percent; |
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 | ctx << LOG_ID_EVENTS; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace art |