blob: f66163178953443efc76e9f540a641b6d93919fe [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"
Nicolas Geoffraye2613562021-08-18 22:46:32 +010027#include "jni/jni_env_ext.h"
28#include "palette/palette.h"
Andreas Gamped2c03b52017-06-02 15:34:55 -070029#include "thread.h"
30
Elliott Hughesfc861622011-10-17 17:57:47 -070031#define EVENT_LOG_TAG_dvm_lock_sample 20003
32
33namespace art {
34
Andreas Gampe39b98112017-06-01 16:28:27 -070035void Monitor::LogContentionEvent(Thread* self,
36 uint32_t wait_ms,
37 uint32_t sample_percent,
38 ArtMethod* owner_method,
39 uint32_t owner_dex_pc) {
Andreas Gampee59cb812017-06-02 14:26:06 -070040 android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample);
41
Andreas Gampe39b98112017-06-01 16:28:27 -070042 const char* owner_filename;
43 int32_t owner_line_number;
44 TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number);
45
Nicolas Geoffraye2613562021-08-18 22:46:32 +010046 // Emit the process name, <= 33 bytes.
47 char proc_name[33] = {};
Andreas Gampee59cb812017-06-02 14:26:06 -070048 {
Andreas Gampedfcd82c2018-10-16 20:22:37 -070049 int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
Nicolas Geoffraye2613562021-08-18 22:46:32 +010050 read(fd, proc_name, sizeof(proc_name) - 1);
Andreas Gampee59cb812017-06-02 14:26:06 -070051 close(fd);
Nicolas Geoffraye2613562021-08-18 22:46:32 +010052 ctx << proc_name;
Andreas Gampee59cb812017-06-02 14:26:06 -070053 }
Elliott Hughesfc861622011-10-17 17:57:47 -070054
Andreas Gampee59cb812017-06-02 14:26:06 -070055 // Emit the sensitive thread ("main thread") status. We follow tradition that this corresponds
56 // to a C++ bool's value, but be explicit.
57 constexpr uint32_t kIsSensitive = 1u;
58 constexpr uint32_t kIsNotSensitive = 0u;
59 ctx << (Thread::IsSensitiveThread() ? kIsSensitive : kIsNotSensitive);
Elliott Hughesfc861622011-10-17 17:57:47 -070060
Andreas Gampee59cb812017-06-02 14:26:06 -070061 // Emit self thread name string.
Nicolas Geoffraye2613562021-08-18 22:46:32 +010062 std::string thread_name;
63 self->GetThreadName(thread_name);
64 ctx << thread_name;
Elliott Hughesfc861622011-10-17 17:57:47 -070065
Andreas Gampee59cb812017-06-02 14:26:06 -070066 // Emit the wait time.
67 ctx << wait_ms;
Elliott Hughesfc861622011-10-17 17:57:47 -070068
Andreas Gampee59cb812017-06-02 14:26:06 -070069 const char* filename = nullptr;
Nicolas Geoffraye2613562021-08-18 22:46:32 +010070 int32_t line_number;
71 std::string method_name;
Andreas Gampee59cb812017-06-02 14:26:06 -070072 {
73 uint32_t pc;
74 ArtMethod* m = self->GetCurrentMethod(&pc);
Andreas Gampee59cb812017-06-02 14:26:06 -070075 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.
Nicolas Geoffraye2613562021-08-18 22:46:32 +010084 method_name = ArtMethod::PrettyMethod(m);
85 ctx << method_name;
Andreas Gampee59cb812017-06-02 14:26:06 -070086 }
87
88 // Emit the lock owner source code file name.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 if (owner_filename == nullptr) {
Elliott Hughesfc861622011-10-17 17:57:47 -070090 owner_filename = "";
91 } else if (strcmp(filename, owner_filename) == 0) {
92 // Common case, so save on log space.
93 owner_filename = "-";
94 }
Andreas Gampee59cb812017-06-02 14:26:06 -070095 ctx << owner_filename;
Elliott Hughesfc861622011-10-17 17:57:47 -070096
Andreas Gampee59cb812017-06-02 14:26:06 -070097 // Emit the source code line number.
98 ctx << owner_line_number;
Elliott Hughesfc861622011-10-17 17:57:47 -070099
Andreas Gamped2c03b52017-06-02 15:34:55 -0700100 // Emit the owner method name.
Nicolas Geoffraye2613562021-08-18 22:46:32 +0100101 std::string owner_method_name = ArtMethod::PrettyMethod(owner_method);
102 ctx << owner_method_name;
Andreas Gamped2c03b52017-06-02 15:34:55 -0700103
Andreas Gampee59cb812017-06-02 14:26:06 -0700104 // Emit the sample percentage.
105 ctx << sample_percent;
Elliott Hughesfc861622011-10-17 17:57:47 -0700106
Andreas Gampee59cb812017-06-02 14:26:06 -0700107 ctx << LOG_ID_EVENTS;
Nicolas Geoffraye2613562021-08-18 22:46:32 +0100108
109 // Now report to other interested parties.
110 PaletteReportLockContention(self->GetJniEnv(),
111 wait_ms,
112 filename,
113 line_number,
114 method_name.c_str(),
115 owner_filename,
116 owner_line_number,
117 owner_method_name.c_str(),
118 proc_name,
119 thread_name.c_str());
Elliott Hughesfc861622011-10-17 17:57:47 -0700120}
121
122} // namespace art