blob: 9ce3b0cfe8b6f43e10e49d09b96262d275fd1057 [file] [log] [blame]
xueliang.zhong383b57d2016-10-04 11:19:17 +01001/*
2 * Copyright 2016 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 "jit_logger.h"
18
19#include "arch/instruction_set.h"
20#include "art_method-inl.h"
21#include "base/time_utils.h"
22#include "base/unix_file/fd_file.h"
23#include "driver/compiler_driver.h"
24#include "jit/jit.h"
25#include "jit/jit_code_cache.h"
26
27namespace art {
28namespace jit {
29
30#ifdef ART_TARGET_ANDROID
31static const char* kLogPrefix = "/data/misc/trace";
32#else
33static const char* kLogPrefix = "/tmp";
34#endif
35
36// File format of perf-PID.map:
37// +---------------------+
38// |ADDR SIZE symbolname1|
39// |ADDR SIZE symbolname2|
40// |... |
41// +---------------------+
42void JitLogger::OpenPerfMapLog() {
43 std::string pid_str = std::to_string(getpid());
44 std::string perf_filename = std::string(kLogPrefix) + "/perf-" + pid_str + ".map";
45 perf_file_.reset(OS::CreateEmptyFileWriteOnly(perf_filename.c_str()));
46 if (perf_file_ == nullptr) {
47 LOG(ERROR) << "Could not create perf file at " << perf_filename <<
48 " Are you on a user build? Perf only works on userdebug/eng builds";
49 }
50}
51
52void JitLogger::WritePerfMapLog(JitCodeCache* code_cache, ArtMethod* method) {
53 if (perf_file_ != nullptr) {
54 const void* ptr = method->GetEntryPointFromQuickCompiledCode();
55 size_t code_size = code_cache->GetMemorySizeOfCodePointer(ptr);
56 std::string method_name = method->PrettyMethod();
57
58 std::ostringstream stream;
59 stream << std::hex
60 << reinterpret_cast<uintptr_t>(ptr)
61 << " "
62 << code_size
63 << " "
64 << method_name
65 << std::endl;
66 std::string str = stream.str();
67 bool res = perf_file_->WriteFully(str.c_str(), str.size());
68 if (!res) {
69 LOG(WARNING) << "Failed to write jitted method info in log: write failure.";
70 }
71 } else {
72 LOG(WARNING) << "Failed to write jitted method info in log: log file doesn't exist.";
73 }
74}
75
76void JitLogger::ClosePerfMapLog() {
77 if (perf_file_ != nullptr) {
78 UNUSED(perf_file_->Flush());
79 UNUSED(perf_file_->Close());
80 }
81}
82
83// File format of jit-PID.jump:
84//
85// +--------------------------------+
86// | PerfJitHeader |
87// +--------------------------------+
88// | PerfJitCodeLoad { | .
89// | struct PerfJitBase; | .
90// | uint32_t process_id_; | .
91// | uint32_t thread_id_; | .
92// | uint64_t vma_; | .
93// | uint64_t code_address_; | .
94// | uint64_t code_size_; | .
95// | uint64_t code_id_; | .
96// | } | .
97// +- -+ .
98// | method_name'\0' | +--> one jitted method
99// +- -+ .
100// | jitted code binary | .
101// | ... | .
102// +--------------------------------+ .
103// | PerfJitCodeDebugInfo { | .
104// | struct PerfJitBase; | .
105// | uint64_t address_; | .
106// | uint64_t entry_count_; | .
107// | struct PerfJitDebugEntry; | .
108// | } | .
109// +--------------------------------+
110// | PerfJitCodeLoad |
111// ...
112//
113struct PerfJitHeader {
114 uint32_t magic_; // Characters "JiTD"
115 uint32_t version_; // Header version
116 uint32_t size_; // Total size of header
117 uint32_t elf_mach_target_; // Elf mach target
118 uint32_t reserved_; // Reserved, currently not used
119 uint32_t process_id_; // Process ID of the JIT compiler
120 uint64_t time_stamp_; // Timestamp when the header is generated
121 uint64_t flags_; // Currently the flags are only used for choosing clock for timestamp,
122 // we set it to 0 to tell perf that we use CLOCK_MONOTONIC clock.
123 static const uint32_t kMagic = 0x4A695444; // "JiTD"
124 static const uint32_t kVersion = 1;
125};
126
127// Each record starts with such basic information: event type, total size, and timestamp.
128struct PerfJitBase {
129 enum PerfJitEvent {
130 // A jitted code load event.
131 // In ART JIT, it is used to log a new method is jit compiled and committed to jit-code-cache.
132 // Note that such kLoad event supports code cache GC in ART JIT.
133 // For every kLoad event recorded in jit-PID.dump and every perf sample recorded in perf.data,
134 // each event/sample has time stamp. In case code cache GC happens in ART JIT, and a new
135 // jitted method is committed to the same address of a previously deleted method,
136 // the time stamp information can help profiler to tell whether this sample belongs to the
137 // era of the first jitted method, or does it belong to the period of the second jitted method.
138 // JitCodeCache doesn't have to record any event on 'code delete'.
139 kLoad = 0,
140
141 // A jitted code move event, i,e. a jitted code moved from one address to another address.
142 // It helps profiler to map samples to the right symbol even when the code is moved.
143 // In ART JIT, this event can help log such behavior:
144 // A jitted method is recorded in previous kLoad event, but due to some reason,
145 // it is moved to another address in jit-code-cache.
146 kMove = 1,
147
148 // Logs debug line/column information.
149 kDebugInfo = 2,
150
151 // Logs JIT VM end of life event.
152 kClose = 3
153 };
154 uint32_t event_; // Must be one of the events defined in PerfJitEvent.
155 uint32_t size_; // Total size of this event record.
156 // For example, for kLoad event, size of the event record is:
157 // sizeof(PerfJitCodeLoad) + method_name.size() + compiled code size.
158 uint64_t time_stamp_; // Timestamp for the event.
159};
160
161// Logs a jitted code load event (kLoad).
162// In ART JIT, it is used to log a new method is jit compiled and commited to jit-code-cache.
163struct PerfJitCodeLoad : PerfJitBase {
164 uint32_t process_id_; // Process ID who performs the jit code load.
165 // In ART JIT, it is the pid of the JIT compiler.
166 uint32_t thread_id_; // Thread ID who performs the jit code load.
167 // In ART JIT, it is the tid of the JIT compiler.
168 uint64_t vma_; // Address of the code section. In ART JIT, because code_address_
169 // uses absolute address, this field is 0.
170 uint64_t code_address_; // Address where is jitted code is loaded.
171 uint64_t code_size_; // Size of the jitted code.
172 uint64_t code_id_; // Unique ID for each jitted code.
173};
174
175// This structure is for source line/column mapping.
176// Currently this feature is not implemented in ART JIT yet.
177struct PerfJitDebugEntry {
178 uint64_t address_; // Code address which maps to the line/column in source.
179 uint32_t line_number_; // Source line number starting at 1.
180 uint32_t column_; // Column discriminator, default 0.
181 const char name_[0]; // Followed by null-terminated name or \0xff\0 if same as previous.
182};
183
184// Logs debug line information (kDebugInfo).
185// This structure is for source line/column mapping.
186// Currently this feature is not implemented in ART JIT yet.
187struct PerfJitCodeDebugInfo : PerfJitBase {
188 uint64_t address_; // Starting code address which the debug info describes.
189 uint64_t entry_count_; // How many instances of PerfJitDebugEntry.
190 PerfJitDebugEntry entries_[0]; // Followed by entry_count_ instances of PerfJitDebugEntry.
191};
192
193static uint32_t GetElfMach() {
194#if defined(__arm__)
195 static const uint32_t kElfMachARM = 0x28;
196 return kElfMachARM;
197#elif defined(__aarch64__)
198 static const uint32_t kElfMachARM64 = 0xB7;
199 return kElfMachARM64;
200#elif defined(__i386__)
201 static const uint32_t kElfMachIA32 = 0x3;
202 return kElfMachIA32;
203#elif defined(__x86_64__)
204 static const uint32_t kElfMachX64 = 0x3E;
205 return kElfMachX64;
206#else
207 UNIMPLEMENTED(WARNING) << "Unsupported architecture in JitLogger";
208 return 0;
209#endif
210}
211
212void JitLogger::OpenMarkerFile() {
213 int fd = jit_dump_file_->Fd();
214 // The 'perf inject' tool requires that the jit-PID.dump file
215 // must have a mmap(PROT_READ|PROT_EXEC) record in perf.data.
216 marker_address_ = mmap(nullptr, kPageSize, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
217 if (marker_address_ == MAP_FAILED) {
218 LOG(WARNING) << "Failed to create record in perf.data. JITed code profiling will not work.";
219 return;
220 }
221}
222
223void JitLogger::CloseMarkerFile() {
224 if (marker_address_ != nullptr) {
225 munmap(marker_address_, kPageSize);
226 }
227}
228
229void JitLogger::WriteJitDumpDebugInfo() {
230 // In the future, we can add java source file line/column mapping here.
231}
232
233void JitLogger::WriteJitDumpHeader() {
234 PerfJitHeader header;
235
236 std::memset(&header, 0, sizeof(header));
237 header.magic_ = PerfJitHeader::kMagic;
238 header.version_ = PerfJitHeader::kVersion;
239 header.size_ = sizeof(header);
240 header.elf_mach_target_ = GetElfMach();
241 header.process_id_ = static_cast<uint32_t>(getpid());
242 header.time_stamp_ = art::NanoTime(); // CLOCK_MONOTONIC clock is required.
243 header.flags_ = 0;
244
245 bool res = jit_dump_file_->WriteFully(reinterpret_cast<const char*>(&header), sizeof(header));
246 if (!res) {
247 LOG(WARNING) << "Failed to write profiling log. The 'perf inject' tool will not work.";
248 }
249}
250
251void JitLogger::OpenJitDumpLog() {
252 std::string pid_str = std::to_string(getpid());
253 std::string jitdump_filename = std::string(kLogPrefix) + "/jit-" + pid_str + ".dump";
254
255 jit_dump_file_.reset(OS::CreateEmptyFile(jitdump_filename.c_str()));
256 if (jit_dump_file_ == nullptr) {
257 LOG(ERROR) << "Could not create jit dump file at " << jitdump_filename <<
258 " Are you on a user build? Perf only works on userdebug/eng builds";
259 return;
260 }
261
262 OpenMarkerFile();
263
264 // Continue to write jit-PID.dump file even above OpenMarkerFile() fails.
265 // Even if that means 'perf inject' tool cannot work, developers can still use other tools
266 // to map the samples in perf.data to the information (symbol,address,code) recorded
267 // in the jit-PID.dump file, and still proceed the jitted code analysis.
268 WriteJitDumpHeader();
269}
270
271void JitLogger::WriteJitDumpLog(JitCodeCache* code_cache, ArtMethod* method) {
272 if (jit_dump_file_ != nullptr) {
273 const void* code = method->GetEntryPointFromQuickCompiledCode();
274 size_t code_size = code_cache->GetMemorySizeOfCodePointer(code);
275 std::string method_name = method->PrettyMethod();
276
277 PerfJitCodeLoad jit_code;
278 std::memset(&jit_code, 0, sizeof(jit_code));
279 jit_code.event_ = PerfJitCodeLoad::kLoad;
280 jit_code.size_ = sizeof(jit_code) + method_name.size() + 1 + code_size;
281 jit_code.time_stamp_ = art::NanoTime(); // CLOCK_MONOTONIC clock is required.
282 jit_code.process_id_ = static_cast<uint32_t>(getpid());
283 jit_code.thread_id_ = static_cast<uint32_t>(art::GetTid());
284 jit_code.vma_ = 0x0;
285 jit_code.code_address_ = reinterpret_cast<uint64_t>(code);
286 jit_code.code_size_ = code_size;
287 jit_code.code_id_ = code_index_++;
288
289 // Write one complete jitted method info, including:
290 // - PerfJitCodeLoad structure
291 // - Method name
292 // - Complete generated code of this method
293 //
294 // Use UNUSED() here to avoid compiler warnings.
295 UNUSED(jit_dump_file_->WriteFully(reinterpret_cast<const char*>(&jit_code), sizeof(jit_code)));
296 UNUSED(jit_dump_file_->WriteFully(method_name.c_str(), method_name.size() + 1));
297 UNUSED(jit_dump_file_->WriteFully(code, code_size));
298
299 WriteJitDumpDebugInfo();
300 }
301}
302
303void JitLogger::CloseJitDumpLog() {
304 if (jit_dump_file_ != nullptr) {
305 CloseMarkerFile();
306 UNUSED(jit_dump_file_->Flush());
307 UNUSED(jit_dump_file_->Close());
308 }
309}
310
311} // namespace jit
312} // namespace art