blob: 7ba36529fc5e5b29814f3dfee4da671f1c8b0e6a [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#include "thread.h"
19
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
Mark Salyzyn280a1622016-10-17 14:11:20 -070024#include <log/log.h>
Andreas Gampee59cb812017-06-02 14:26:06 -070025#include <log/log_event_list.h>
Elliott Hughesfc861622011-10-17 17:57:47 -070026
27#define EVENT_LOG_TAG_dvm_lock_sample 20003
28
29namespace art {
30
Andreas Gampe39b98112017-06-01 16:28:27 -070031void 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 Gampee59cb812017-06-02 14:26:06 -070036 android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample);
37
Andreas Gampe39b98112017-06-01 16:28:27 -070038 const char* owner_filename;
39 int32_t owner_line_number;
40 TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number);
41
Elliott Hughesfc861622011-10-17 17:57:47 -070042 // Emit the process name, <= 37 bytes.
Andreas Gampee59cb812017-06-02 14:26:06 -070043 {
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 Hughesfc861622011-10-17 17:57:47 -070051
Andreas Gampee59cb812017-06-02 14:26:06 -070052 // 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 Hughesfc861622011-10-17 17:57:47 -070057
Andreas Gampee59cb812017-06-02 14:26:06 -070058 // Emit self thread name string.
59 {
60 std::string thread_name;
61 self->GetThreadName(thread_name);
62 ctx << thread_name;
63 }
Elliott Hughesfc861622011-10-17 17:57:47 -070064
Andreas Gampee59cb812017-06-02 14:26:06 -070065 // Emit the wait time.
66 ctx << wait_ms;
Elliott Hughesfc861622011-10-17 17:57:47 -070067
Andreas Gampee59cb812017-06-02 14:26:06 -070068 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 Hughesfc861622011-10-17 17:57:47 -070074
Andreas Gampee59cb812017-06-02 14:26:06 -070075 // Emit the source code file name.
76 ctx << filename;
Elliott Hughesfc861622011-10-17 17:57:47 -070077
Andreas Gampee59cb812017-06-02 14:26:06 -070078 // Emit the source code line number.
79 ctx << line_number;
80 }
81
82 // Emit the lock owner source code file name.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 if (owner_filename == nullptr) {
Elliott Hughesfc861622011-10-17 17:57:47 -070084 owner_filename = "";
85 } else if (strcmp(filename, owner_filename) == 0) {
86 // Common case, so save on log space.
87 owner_filename = "-";
88 }
Andreas Gampee59cb812017-06-02 14:26:06 -070089 ctx << owner_filename;
Elliott Hughesfc861622011-10-17 17:57:47 -070090
Andreas Gampee59cb812017-06-02 14:26:06 -070091 // Emit the source code line number.
92 ctx << owner_line_number;
Elliott Hughesfc861622011-10-17 17:57:47 -070093
Andreas Gampee59cb812017-06-02 14:26:06 -070094 // Emit the sample percentage.
95 ctx << sample_percent;
Elliott Hughesfc861622011-10-17 17:57:47 -070096
Andreas Gampee59cb812017-06-02 14:26:06 -070097 ctx << LOG_ID_EVENTS;
Elliott Hughesfc861622011-10-17 17:57:47 -070098}
99
100} // namespace art