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> |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 25 | |
| 26 | #define EVENT_LOG_TAG_dvm_lock_sample 20003 |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static void Set4LE(uint8_t* buf, uint32_t val) { |
| 31 | *buf++ = (uint8_t)(val); |
| 32 | *buf++ = (uint8_t)(val >> 8); |
| 33 | *buf++ = (uint8_t)(val >> 16); |
| 34 | *buf = (uint8_t)(val >> 24); |
| 35 | } |
| 36 | |
| 37 | static char* EventLogWriteInt(char* dst, int value) { |
| 38 | *dst++ = EVENT_TYPE_INT; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 39 | Set4LE(reinterpret_cast<uint8_t*>(dst), value); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 40 | return dst + 4; |
| 41 | } |
| 42 | |
| 43 | static char* EventLogWriteString(char* dst, const char* value, size_t len) { |
| 44 | *dst++ = EVENT_TYPE_STRING; |
| 45 | len = len < 32 ? len : 32; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 46 | Set4LE(reinterpret_cast<uint8_t*>(dst), len); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 47 | dst += 4; |
| 48 | memcpy(dst, value, len); |
| 49 | return dst + len; |
| 50 | } |
| 51 | |
Andreas Gampe | 39b9811 | 2017-06-01 16:28:27 -0700 | [diff] [blame^] | 52 | void Monitor::LogContentionEvent(Thread* self, |
| 53 | uint32_t wait_ms, |
| 54 | uint32_t sample_percent, |
| 55 | ArtMethod* owner_method, |
| 56 | uint32_t owner_dex_pc) { |
| 57 | const char* owner_filename; |
| 58 | int32_t owner_line_number; |
| 59 | TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number); |
| 60 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 61 | // Emit the event list length, 1 byte. |
| 62 | char eventBuffer[174]; |
| 63 | char* cp = eventBuffer; |
| 64 | *cp++ = 9; |
| 65 | |
| 66 | // Emit the process name, <= 37 bytes. |
| 67 | int fd = open("/proc/self/cmdline", O_RDONLY); |
| 68 | char procName[33]; |
| 69 | memset(procName, 0, sizeof(procName)); |
| 70 | read(fd, procName, sizeof(procName) - 1); |
| 71 | close(fd); |
| 72 | size_t len = strlen(procName); |
| 73 | cp = EventLogWriteString(cp, procName, len); |
| 74 | |
| 75 | // Emit the sensitive thread ("main thread") status, 5 bytes. |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 76 | cp = EventLogWriteInt(cp, Thread::IsSensitiveThread()); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 77 | |
| 78 | // Emit self thread name string, <= 37 bytes. |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 79 | std::string thread_name; |
| 80 | self->GetThreadName(thread_name); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 81 | cp = EventLogWriteString(cp, thread_name.c_str(), thread_name.size()); |
| 82 | |
| 83 | // Emit the wait time, 5 bytes. |
| 84 | cp = EventLogWriteInt(cp, wait_ms); |
| 85 | |
| 86 | // Emit the source code file name, <= 37 bytes. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 87 | uint32_t pc; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 88 | ArtMethod* m = self->GetCurrentMethod(&pc); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 89 | const char* filename; |
Brian Carlstrom | eaa4609 | 2015-10-07 21:29:28 -0700 | [diff] [blame] | 90 | int32_t line_number; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 91 | TranslateLocation(m, pc, &filename, &line_number); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 92 | cp = EventLogWriteString(cp, filename, strlen(filename)); |
| 93 | |
| 94 | // Emit the source code line number, 5 bytes. |
| 95 | cp = EventLogWriteInt(cp, line_number); |
| 96 | |
| 97 | // Emit the lock owner source code file name, <= 37 bytes. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 98 | if (owner_filename == nullptr) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 99 | owner_filename = ""; |
| 100 | } else if (strcmp(filename, owner_filename) == 0) { |
| 101 | // Common case, so save on log space. |
| 102 | owner_filename = "-"; |
| 103 | } |
| 104 | cp = EventLogWriteString(cp, owner_filename, strlen(owner_filename)); |
| 105 | |
| 106 | // Emit the source code line number, 5 bytes. |
| 107 | cp = EventLogWriteInt(cp, owner_line_number); |
| 108 | |
| 109 | // Emit the sample percentage, 5 bytes. |
| 110 | cp = EventLogWriteInt(cp, sample_percent); |
| 111 | |
| 112 | CHECK_LE((size_t)(cp - eventBuffer), sizeof(eventBuffer)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample, EVENT_TYPE_LIST, eventBuffer, |
| 114 | (size_t)(cp - eventBuffer)); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | } // namespace art |