blob: 74623dab31caef6ce244b35f776d9331cd59f0d3 [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"
Elliott Hughesfc861622011-10-17 17:57:47 -070018
19#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Mark Salyzyn280a1622016-10-17 14:11:20 -070023#include <log/log.h>
Andreas Gampee59cb812017-06-02 14:26:06 -070024#include <log/log_event_list.h>
Elliott Hughesfc861622011-10-17 17:57:47 -070025
Andreas Gamped2c03b52017-06-02 15:34:55 -070026#include "art_method.h"
27#include "thread.h"
28
Elliott Hughesfc861622011-10-17 17:57:47 -070029#define EVENT_LOG_TAG_dvm_lock_sample 20003
30
31namespace art {
32
Andreas Gampe39b98112017-06-01 16:28:27 -070033void 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 Gampee59cb812017-06-02 14:26:06 -070038 android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample);
39
Andreas Gampe39b98112017-06-01 16:28:27 -070040 const char* owner_filename;
41 int32_t owner_line_number;
42 TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number);
43
Elliott Hughesfc861622011-10-17 17:57:47 -070044 // Emit the process name, <= 37 bytes.
Andreas Gampee59cb812017-06-02 14:26:06 -070045 {
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 Hughesfc861622011-10-17 17:57:47 -070053
Andreas Gampee59cb812017-06-02 14:26:06 -070054 // 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 Hughesfc861622011-10-17 17:57:47 -070059
Andreas Gampee59cb812017-06-02 14:26:06 -070060 // Emit self thread name string.
61 {
62 std::string thread_name;
63 self->GetThreadName(thread_name);
64 ctx << thread_name;
65 }
Elliott Hughesfc861622011-10-17 17:57:47 -070066
Andreas Gampee59cb812017-06-02 14:26:06 -070067 // Emit the wait time.
68 ctx << wait_ms;
Elliott Hughesfc861622011-10-17 17:57:47 -070069
Andreas Gampee59cb812017-06-02 14:26:06 -070070 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 Hughesfc861622011-10-17 17:57:47 -070076
Andreas Gampee59cb812017-06-02 14:26:06 -070077 // Emit the source code file name.
78 ctx << filename;
Elliott Hughesfc861622011-10-17 17:57:47 -070079
Andreas Gampee59cb812017-06-02 14:26:06 -070080 // Emit the source code line number.
81 ctx << line_number;
Andreas Gamped2c03b52017-06-02 15:34:55 -070082
83 // Emit the method name.
84 ctx << ArtMethod::PrettyMethod(m);
Andreas Gampee59cb812017-06-02 14:26:06 -070085 }
86
87 // Emit the lock owner source code file name.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070088 if (owner_filename == nullptr) {
Elliott Hughesfc861622011-10-17 17:57:47 -070089 owner_filename = "";
90 } else if (strcmp(filename, owner_filename) == 0) {
91 // Common case, so save on log space.
92 owner_filename = "-";
93 }
Andreas Gampee59cb812017-06-02 14:26:06 -070094 ctx << owner_filename;
Elliott Hughesfc861622011-10-17 17:57:47 -070095
Andreas Gampee59cb812017-06-02 14:26:06 -070096 // Emit the source code line number.
97 ctx << owner_line_number;
Elliott Hughesfc861622011-10-17 17:57:47 -070098
Andreas Gamped2c03b52017-06-02 15:34:55 -070099 // Emit the owner method name.
100 ctx << ArtMethod::PrettyMethod(owner_method);
101
Andreas Gampee59cb812017-06-02 14:26:06 -0700102 // Emit the sample percentage.
103 ctx << sample_percent;
Elliott Hughesfc861622011-10-17 17:57:47 -0700104
Andreas Gampee59cb812017-06-02 14:26:06 -0700105 ctx << LOG_ID_EVENTS;
Elliott Hughesfc861622011-10-17 17:57:47 -0700106}
107
108} // namespace art