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"
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <log/log.h>
24 #include <log/log_event_list.h>
25 
26 #include "art_method.h"
27 #include "jni/jni_env_ext.h"
28 #include "palette/palette.h"
29 #include "thread.h"
30 
31 #define EVENT_LOG_TAG_dvm_lock_sample 20003
32 
33 namespace art {
34 
LogContentionEvent(Thread * self,uint32_t wait_ms,uint32_t sample_percent,ArtMethod * owner_method,uint32_t owner_dex_pc)35 void Monitor::LogContentionEvent(Thread* self,
36                                  uint32_t wait_ms,
37                                  uint32_t sample_percent,
38                                  ArtMethod* owner_method,
39                                  uint32_t owner_dex_pc) {
40   android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample);
41 
42   const char* owner_filename;
43   int32_t owner_line_number;
44   TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number);
45 
46   // Emit the process name, <= 33 bytes.
47   char proc_name[33] = {};
48   {
49     int fd = open("/proc/self/cmdline", O_RDONLY  | O_CLOEXEC);
50     read(fd, proc_name, sizeof(proc_name) - 1);
51     close(fd);
52     ctx << proc_name;
53   }
54 
55   // 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);
60 
61   // Emit self thread name string.
62   std::string thread_name;
63   self->GetThreadName(thread_name);
64   ctx << thread_name;
65 
66   // Emit the wait time.
67   ctx << wait_ms;
68 
69   const char* filename = nullptr;
70   int32_t line_number;
71   std::string method_name;
72   {
73     uint32_t pc;
74     ArtMethod* m = self->GetCurrentMethod(&pc);
75     TranslateLocation(m, pc, &filename, &line_number);
76 
77     // Emit the source code file name.
78     ctx << filename;
79 
80     // Emit the source code line number.
81     ctx << line_number;
82 
83     // Emit the method name.
84     method_name = ArtMethod::PrettyMethod(m);
85     ctx << method_name;
86   }
87 
88   // Emit the lock owner source code file name.
89   if (owner_filename == nullptr) {
90     owner_filename = "";
91   } else if (strcmp(filename, owner_filename) == 0) {
92     // Common case, so save on log space.
93     owner_filename = "-";
94   }
95   ctx << owner_filename;
96 
97   // Emit the source code line number.
98   ctx << owner_line_number;
99 
100   // Emit the owner method name.
101   std::string owner_method_name = ArtMethod::PrettyMethod(owner_method);
102   ctx << owner_method_name;
103 
104   // Emit the sample percentage.
105   ctx << sample_percent;
106 
107   ctx << LOG_ID_EVENTS;
108 
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());
120 }
121 
122 }  // namespace art
123