blob: 6e22237afbb78cf14bd46991636522f700558e04 [file] [log] [blame]
Elliott Hughesfc861622011-10-17 17:57:47 -07001/*
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"
18#include "object.h"
19#include "thread.h"
20
21#include <fcntl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include "cutils/log.h"
26
27#define EVENT_LOG_TAG_dvm_lock_sample 20003
28
29namespace art {
30
31static void Set4LE(uint8_t* buf, uint32_t val) {
32 *buf++ = (uint8_t)(val);
33 *buf++ = (uint8_t)(val >> 8);
34 *buf++ = (uint8_t)(val >> 16);
35 *buf = (uint8_t)(val >> 24);
36}
37
38static char* EventLogWriteInt(char* dst, int value) {
39 *dst++ = EVENT_TYPE_INT;
Elliott Hughes398f64b2012-03-26 18:05:48 -070040 Set4LE(reinterpret_cast<uint8_t*>(dst), value);
Elliott Hughesfc861622011-10-17 17:57:47 -070041 return dst + 4;
42}
43
44static char* EventLogWriteString(char* dst, const char* value, size_t len) {
45 *dst++ = EVENT_TYPE_STRING;
46 len = len < 32 ? len : 32;
Elliott Hughes398f64b2012-03-26 18:05:48 -070047 Set4LE(reinterpret_cast<uint8_t*>(dst), len);
Elliott Hughesfc861622011-10-17 17:57:47 -070048 dst += 4;
49 memcpy(dst, value, len);
50 return dst + len;
51}
52
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053void Monitor::LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent,
54 const char* owner_filename, uint32_t owner_line_number) {
Elliott Hughesfc861622011-10-17 17:57:47 -070055 // Emit the event list length, 1 byte.
56 char eventBuffer[174];
57 char* cp = eventBuffer;
58 *cp++ = 9;
59
60 // Emit the process name, <= 37 bytes.
61 int fd = open("/proc/self/cmdline", O_RDONLY);
62 char procName[33];
63 memset(procName, 0, sizeof(procName));
64 read(fd, procName, sizeof(procName) - 1);
65 close(fd);
66 size_t len = strlen(procName);
67 cp = EventLogWriteString(cp, procName, len);
68
69 // Emit the sensitive thread ("main thread") status, 5 bytes.
70 cp = EventLogWriteInt(cp, Monitor::IsSensitiveThread());
71
72 // Emit self thread name string, <= 37 bytes.
Ian Rogers365c1022012-06-22 15:05:28 -070073 std::string thread_name;
74 self->GetThreadName(thread_name);
Elliott Hughesfc861622011-10-17 17:57:47 -070075 cp = EventLogWriteString(cp, thread_name.c_str(), thread_name.size());
76
77 // Emit the wait time, 5 bytes.
78 cp = EventLogWriteInt(cp, wait_ms);
79
80 // Emit the source code file name, <= 37 bytes.
Elliott Hughesd07986f2011-12-06 18:27:45 -080081 uintptr_t pc;
Mathieu Chartier66f19252012-09-18 08:57:04 -070082 AbstractMethod* m = self->GetCurrentMethod(&pc);
Elliott Hughesfc861622011-10-17 17:57:47 -070083 const char* filename;
84 uint32_t line_number;
Elliott Hughesd07986f2011-12-06 18:27:45 -080085 TranslateLocation(m, pc, filename, line_number);
Elliott Hughesfc861622011-10-17 17:57:47 -070086 cp = EventLogWriteString(cp, filename, strlen(filename));
87
88 // Emit the source code line number, 5 bytes.
89 cp = EventLogWriteInt(cp, line_number);
90
91 // Emit the lock owner source code file name, <= 37 bytes.
92 if (owner_filename == NULL) {
93 owner_filename = "";
94 } else if (strcmp(filename, owner_filename) == 0) {
95 // Common case, so save on log space.
96 owner_filename = "-";
97 }
98 cp = EventLogWriteString(cp, owner_filename, strlen(owner_filename));
99
100 // Emit the source code line number, 5 bytes.
101 cp = EventLogWriteInt(cp, owner_line_number);
102
103 // Emit the sample percentage, 5 bytes.
104 cp = EventLogWriteInt(cp, sample_percent);
105
106 CHECK_LE((size_t)(cp - eventBuffer), sizeof(eventBuffer));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample, EVENT_TYPE_LIST, eventBuffer,
108 (size_t)(cp - eventBuffer));
Elliott Hughesfc861622011-10-17 17:57:47 -0700109}
110
111} // namespace art