blob: 0950abeb97c0b4da2f5b3f7d2d616008bf69f28a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
jeffhaoe343b762011-12-05 16:36:44 -080016
17#include "trace.h"
18
jeffhaoa9ef3fd2011-12-13 18:33:43 -080019#include <sys/uio.h>
20
Ian Rogerscf7f1912014-10-22 22:06:39 -070021#define ATRACE_TAG ATRACE_TAG_DALVIK
22#include "cutils/trace.h"
23
Jeff Hao0abc72e2013-08-13 13:45:14 -070024#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080026#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080027#include "common_throws.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080028#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
jeffhao725a9572012-11-13 18:20:12 -080030#include "instrumentation.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070031#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/dex_cache.h"
34#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "mirror/object-inl.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080036#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Jeff Hao0abc72e2013-08-13 13:45:14 -070038#include "ScopedLocalRef.h"
jeffhaoe343b762011-12-05 16:36:44 -080039#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070040#include "thread_list.h"
Ian Rogers166db042013-07-26 12:05:57 -070041#include "entrypoints/quick/quick_entrypoints.h"
jeffhao2692b572011-12-16 15:42:28 -080042
43namespace art {
44
Elliott Hughese119a362012-05-22 17:37:06 -070045// File format:
46// header
47// record 0
48// record 1
49// ...
50//
51// Header format:
52// u4 magic ('SLOW')
53// u2 version
54// u2 offset to data
55// u8 start date/time in usec
56// u2 record size in bytes (version >= 2 only)
57// ... padding to 32 bytes
58//
59// Record format v1:
60// u1 thread ID
61// u4 method ID | method action
62// u4 time delta since start, in usec
63//
64// Record format v2:
65// u2 thread ID
66// u4 method ID | method action
67// u4 time delta since start, in usec
68//
69// Record format v3:
70// u2 thread ID
71// u4 method ID | method action
72// u4 time delta since start, in usec
73// u4 wall time since start, in usec (when clock == "dual" only)
74//
75// 32 bits of microseconds is 70 minutes.
76//
77// All values are stored in little-endian order.
78
Ian Rogers62d6c772013-02-27 08:32:07 -080079enum TraceAction {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070080 kTraceMethodEnter = 0x00, // method entry
81 kTraceMethodExit = 0x01, // method exit
82 kTraceUnroll = 0x02, // method exited by exception unrolling
Ian Rogers62d6c772013-02-27 08:32:07 -080083 // 0x03 currently unused
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084 kTraceMethodActionMask = 0x03, // two bits
Ian Rogers62d6c772013-02-27 08:32:07 -080085};
86
Jeff Hao0abc72e2013-08-13 13:45:14 -070087class BuildStackTraceVisitor : public StackVisitor {
88 public:
89 explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL),
Jeff Hao5ce4b172013-08-16 16:27:18 -070090 method_trace_(Trace::AllocStackTrace()) {}
Jeff Hao0abc72e2013-08-13 13:45:14 -070091
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerse2f77e72013-08-13 19:19:40 -070093 mirror::ArtMethod* m = GetMethod();
Jeff Hao0abc72e2013-08-13 13:45:14 -070094 // Ignore runtime frames (in particular callee save).
95 if (!m->IsRuntimeMethod()) {
96 method_trace_->push_back(m);
97 }
98 return true;
99 }
100
101 // Returns a stack trace where the topmost frame corresponds with the first element of the vector.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700102 std::vector<mirror::ArtMethod*>* GetStackTrace() const {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700103 return method_trace_;
104 }
105
106 private:
Ian Rogerse2f77e72013-08-13 19:19:40 -0700107 std::vector<mirror::ArtMethod*>* const method_trace_;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700108};
109
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800110static const char kTraceTokenChar = '*';
111static const uint16_t kTraceHeaderLength = 32;
112static const uint32_t kTraceMagicValue = 0x574f4c53;
113static const uint16_t kTraceVersionSingleClock = 2;
114static const uint16_t kTraceVersionDualClock = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700115static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
116static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800117
Ian Rogerse63db272014-07-15 15:36:11 -0700118TraceClockSource Trace::default_clock_source_ = kDefaultTraceClockSource;
Elliott Hughese119a362012-05-22 17:37:06 -0700119
Jeff Hao0abc72e2013-08-13 13:45:14 -0700120Trace* volatile Trace::the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700121pthread_t Trace::sampling_pthread_ = 0U;
Ian Rogers700a4022014-05-19 16:49:03 -0700122std::unique_ptr<std::vector<mirror::ArtMethod*>> Trace::temp_stack_trace_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800123
Brian Carlstromea46f952013-07-30 01:26:50 -0700124static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
125 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800126}
Elliott Hughese119a362012-05-22 17:37:06 -0700127
Ian Rogers62d6c772013-02-27 08:32:07 -0800128static TraceAction DecodeTraceAction(uint32_t tmid) {
129 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132static uint32_t EncodeTraceMethodAndAction(mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 TraceAction action) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 uint32_t tmid = PointerToLowMemUInt32(method) | action;
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
136 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800137}
138
Jeff Hao5ce4b172013-08-16 16:27:18 -0700139std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
140 if (temp_stack_trace_.get() != NULL) {
141 return temp_stack_trace_.release();
142 } else {
143 return new std::vector<mirror::ArtMethod*>();
144 }
145}
146
147void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
148 stack_trace->clear();
149 temp_stack_trace_.reset(stack_trace);
150}
151
Ian Rogerse63db272014-07-15 15:36:11 -0700152void Trace::SetDefaultClockSource(TraceClockSource clock_source) {
Elliott Hughes0a18df82015-01-09 15:16:16 -0800153#if defined(__linux__)
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 default_clock_source_ = clock_source;
155#else
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800156 if (clock_source != TraceClockSource::kWall) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700157 LOG(WARNING) << "Ignoring tracing request to use CPU time.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 }
159#endif
160}
161
Ian Rogerse63db272014-07-15 15:36:11 -0700162static uint16_t GetTraceVersion(TraceClockSource clock_source) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800163 return (clock_source == TraceClockSource::kDual) ? kTraceVersionDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800164 : kTraceVersionSingleClock;
165}
166
Ian Rogerse63db272014-07-15 15:36:11 -0700167static uint16_t GetRecordSize(TraceClockSource clock_source) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800168 return (clock_source == TraceClockSource::kDual) ? kTraceRecordSizeDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800170}
171
Elliott Hughese119a362012-05-22 17:37:06 -0700172bool Trace::UseThreadCpuClock() {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800173 return (clock_source_ == TraceClockSource::kThreadCpu) ||
174 (clock_source_ == TraceClockSource::kDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800175}
176
Elliott Hughese119a362012-05-22 17:37:06 -0700177bool Trace::UseWallClock() {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800178 return (clock_source_ == TraceClockSource::kWall) ||
179 (clock_source_ == TraceClockSource::kDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700180}
181
Jeff Haoc5d824a2014-07-28 18:35:38 -0700182void Trace::MeasureClockOverhead() {
183 if (UseThreadCpuClock()) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700184 Thread::Current()->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800185 }
Jeff Haoc5d824a2014-07-28 18:35:38 -0700186 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800187 MicroTime();
188 }
189}
190
Jeff Hao5ce4b172013-08-16 16:27:18 -0700191// Compute an average time taken to measure clocks.
Jeff Haoc5d824a2014-07-28 18:35:38 -0700192uint32_t Trace::GetClockOverheadNanoSeconds() {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700193 Thread* self = Thread::Current();
194 uint64_t start = self->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800195
196 for (int i = 4000; i > 0; i--) {
Jeff Haoc5d824a2014-07-28 18:35:38 -0700197 MeasureClockOverhead();
198 MeasureClockOverhead();
199 MeasureClockOverhead();
200 MeasureClockOverhead();
201 MeasureClockOverhead();
202 MeasureClockOverhead();
203 MeasureClockOverhead();
204 MeasureClockOverhead();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800205 }
206
Jeff Hao5ce4b172013-08-16 16:27:18 -0700207 uint64_t elapsed_us = self->GetCpuMicroTime() - start;
208 return static_cast<uint32_t>(elapsed_us / 32);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800209}
210
Elliott Hughesffb465f2012-03-01 18:46:05 -0800211// TODO: put this somewhere with the big-endian equivalent used by JDWP.
212static void Append2LE(uint8_t* buf, uint16_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700213 *buf++ = static_cast<uint8_t>(val);
214 *buf++ = static_cast<uint8_t>(val >> 8);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800215}
216
Elliott Hughesffb465f2012-03-01 18:46:05 -0800217// TODO: put this somewhere with the big-endian equivalent used by JDWP.
218static void Append4LE(uint8_t* buf, uint32_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700219 *buf++ = static_cast<uint8_t>(val);
220 *buf++ = static_cast<uint8_t>(val >> 8);
221 *buf++ = static_cast<uint8_t>(val >> 16);
222 *buf++ = static_cast<uint8_t>(val >> 24);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800223}
224
Elliott Hughesffb465f2012-03-01 18:46:05 -0800225// TODO: put this somewhere with the big-endian equivalent used by JDWP.
226static void Append8LE(uint8_t* buf, uint64_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700227 *buf++ = static_cast<uint8_t>(val);
228 *buf++ = static_cast<uint8_t>(val >> 8);
229 *buf++ = static_cast<uint8_t>(val >> 16);
230 *buf++ = static_cast<uint8_t>(val >> 24);
231 *buf++ = static_cast<uint8_t>(val >> 32);
232 *buf++ = static_cast<uint8_t>(val >> 40);
233 *buf++ = static_cast<uint8_t>(val >> 48);
234 *buf++ = static_cast<uint8_t>(val >> 56);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800235}
236
Jeff Hao0abc72e2013-08-13 13:45:14 -0700237static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
238 BuildStackTraceVisitor build_trace_visitor(thread);
239 build_trace_visitor.WalkStack();
Ian Rogerse2f77e72013-08-13 19:19:40 -0700240 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700241 Trace* the_trace = reinterpret_cast<Trace*>(arg);
242 the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
243}
244
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700245static void ClearThreadStackTraceAndClockBase(Thread* thread ATTRIBUTE_UNUSED,
246 void* arg ATTRIBUTE_UNUSED) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700247 thread->SetTraceClockBase(0);
248 std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
249 thread->SetStackTraceSample(NULL);
250 delete stack_trace;
251}
252
Jeff Hao0abc72e2013-08-13 13:45:14 -0700253void Trace::CompareAndUpdateStackTrace(Thread* thread,
Ian Rogerse2f77e72013-08-13 19:19:40 -0700254 std::vector<mirror::ArtMethod*>* stack_trace) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700255 CHECK_EQ(pthread_self(), sampling_pthread_);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700256 std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
257 // Update the thread's stack trace sample.
258 thread->SetStackTraceSample(stack_trace);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700259 // Read timer clocks to use for all events in this trace.
260 uint32_t thread_clock_diff = 0;
261 uint32_t wall_clock_diff = 0;
262 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700263 if (old_stack_trace == NULL) {
264 // If there's no previous stack trace sample for this thread, log an entry event for all
Jeff Hao0abc72e2013-08-13 13:45:14 -0700265 // methods in the trace.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700266 for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700267 rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700268 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
269 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700270 }
271 } else {
272 // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
273 // events accordingly.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700274 std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
275 std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700276 // Iterate bottom-up over both traces until there's a difference between them.
277 while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
278 old_rit++;
279 rit++;
280 }
281 // Iterate top-down over the old trace until the point where they differ, emitting exit events.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700282 for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700283 old_it != old_rit.base(); ++old_it) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700284 LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
285 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700286 }
287 // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
288 for (; rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700289 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
290 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700291 }
Jeff Hao5ce4b172013-08-16 16:27:18 -0700292 FreeStackTrace(old_stack_trace);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700293 }
294}
295
296void* Trace::RunSamplingThread(void* arg) {
297 Runtime* runtime = Runtime::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 intptr_t interval_us = reinterpret_cast<intptr_t>(arg);
Jeff Hao4044bda2014-01-06 15:50:45 -0800299 CHECK_GE(interval_us, 0);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700300 CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
301 !runtime->IsCompiler()));
302
303 while (true) {
Jeff Hao23009dc2013-08-22 15:36:42 -0700304 usleep(interval_us);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700305 ATRACE_BEGIN("Profile sampling");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700306 Thread* self = Thread::Current();
307 Trace* the_trace;
308 {
309 MutexLock mu(self, *Locks::trace_lock_);
310 the_trace = the_trace_;
311 if (the_trace == NULL) {
312 break;
313 }
314 }
315
316 runtime->GetThreadList()->SuspendAll();
317 {
318 MutexLock mu(self, *Locks::thread_list_lock_);
319 runtime->GetThreadList()->ForEach(GetSample, the_trace);
320 }
321 runtime->GetThreadList()->ResumeAll();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700322 ATRACE_END();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700323 }
324
325 runtime->DetachCurrentThread();
326 return NULL;
327}
328
Ian Rogers62d6c772013-02-27 08:32:07 -0800329void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -0700330 bool direct_to_ddms, bool sampling_enabled, int interval_us) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 Thread* self = Thread::Current();
332 {
333 MutexLock mu(self, *Locks::trace_lock_);
334 if (the_trace_ != NULL) {
335 LOG(ERROR) << "Trace already in progress, ignoring this request";
336 return;
337 }
jeffhaoe343b762011-12-05 16:36:44 -0800338 }
Jeff Haod063d912014-09-08 09:38:18 -0700339
340 // Check interval if sampling is enabled
341 if (sampling_enabled && interval_us <= 0) {
342 LOG(ERROR) << "Invalid sampling interval: " << interval_us;
343 ScopedObjectAccess soa(self);
344 ThrowRuntimeException("Invalid sampling interval: %d", interval_us);
345 return;
346 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800347
jeffhao2692b572011-12-16 15:42:28 -0800348 // Open trace file if not going directly to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700349 std::unique_ptr<File> trace_file;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800350 if (!direct_to_ddms) {
351 if (trace_fd < 0) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700352 trace_file.reset(OS::CreateEmptyFile(trace_filename));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800353 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 trace_file.reset(new File(trace_fd, "tracefile"));
Elliott Hughes76160052012-12-12 16:31:20 -0800355 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800356 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 if (trace_file.get() == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700358 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 ScopedObjectAccess soa(self);
360 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800361 return;
362 }
363 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800364
Jeff Haod063d912014-09-08 09:38:18 -0700365 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700366
367 // Enable count of allocs if specified in the flags.
368 bool enable_stats = false;
369
Jeff Haod063d912014-09-08 09:38:18 -0700370 runtime->GetThreadList()->SuspendAll();
371
jeffhao2692b572011-12-16 15:42:28 -0800372 // Create Trace object.
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 {
374 MutexLock mu(self, *Locks::trace_lock_);
Brian Carlstromdf629502013-07-17 22:39:56 -0700375 if (the_trace_ != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 LOG(ERROR) << "Trace already in progress, ignoring this request";
377 } else {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700378 enable_stats = (flags && kTraceCountAllocs) != 0;
Jeff Hao23009dc2013-08-22 15:36:42 -0700379 the_trace_ = new Trace(trace_file.release(), buffer_size, flags, sampling_enabled);
Jeff Hao23009dc2013-08-22 15:36:42 -0700380 if (sampling_enabled) {
381 CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread,
382 reinterpret_cast<void*>(interval_us)),
383 "Sampling profiler thread");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700384 } else {
385 runtime->GetInstrumentation()->AddListener(the_trace_,
386 instrumentation::Instrumentation::kMethodEntered |
387 instrumentation::Instrumentation::kMethodExited |
388 instrumentation::Instrumentation::kMethodUnwind);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100389 runtime->GetInstrumentation()->EnableMethodTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700390 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 }
jeffhao0791adc2012-04-04 11:14:32 -0700392 }
Jeff Haod063d912014-09-08 09:38:18 -0700393
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 runtime->GetThreadList()->ResumeAll();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700395
396 // Can't call this when holding the mutator lock.
397 if (enable_stats) {
398 runtime->SetStatsEnabled(true);
399 }
jeffhao2692b572011-12-16 15:42:28 -0800400}
401
402void Trace::Stop() {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700403 bool stop_alloc_counting = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 Runtime* runtime = Runtime::Current();
405 runtime->GetThreadList()->SuspendAll();
406 Trace* the_trace = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700407 pthread_t sampling_pthread = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 {
409 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
410 if (the_trace_ == NULL) {
411 LOG(ERROR) << "Trace stop requested, but no trace currently running";
412 } else {
413 the_trace = the_trace_;
414 the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700415 sampling_pthread = sampling_pthread_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 }
jeffhao2692b572011-12-16 15:42:28 -0800417 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 if (the_trace != NULL) {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700419 stop_alloc_counting = (the_trace->flags_ & kTraceCountAllocs) != 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800420 the_trace->FinishTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700421
Jeff Hao23009dc2013-08-22 15:36:42 -0700422 if (the_trace->sampling_enabled_) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700423 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
424 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL);
425 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100426 runtime->GetInstrumentation()->DisableMethodTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700427 runtime->GetInstrumentation()->RemoveListener(the_trace,
428 instrumentation::Instrumentation::kMethodEntered |
429 instrumentation::Instrumentation::kMethodExited |
430 instrumentation::Instrumentation::kMethodUnwind);
431 }
Andreas Gampe4303ba92014-11-06 01:00:46 -0800432 if (the_trace->trace_file_.get() != nullptr) {
433 // Do not try to erase, so flush and close explicitly.
434 if (the_trace->trace_file_->Flush() != 0) {
435 PLOG(ERROR) << "Could not flush trace file.";
436 }
437 if (the_trace->trace_file_->Close() != 0) {
438 PLOG(ERROR) << "Could not close trace file.";
439 }
440 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800441 delete the_trace;
442 }
443 runtime->GetThreadList()->ResumeAll();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700444
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700445 if (stop_alloc_counting) {
446 // Can be racy since SetStatsEnabled is not guarded by any locks.
447 Runtime::Current()->SetStatsEnabled(false);
448 }
449
Jeff Hao23009dc2013-08-22 15:36:42 -0700450 if (sampling_pthread != 0U) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700451 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
Jeff Haod063d912014-09-08 09:38:18 -0700452 sampling_pthread_ = 0U;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700453 }
jeffhao2692b572011-12-16 15:42:28 -0800454}
455
jeffhaob5e81852012-03-12 11:15:45 -0700456void Trace::Shutdown() {
Jeff Hao64caa7d2013-08-29 11:18:01 -0700457 if (GetMethodTracingMode() != kTracingInactive) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800458 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700459 }
jeffhaob5e81852012-03-12 11:15:45 -0700460}
461
Jeff Hao64caa7d2013-08-29 11:18:01 -0700462TracingMode Trace::GetMethodTracingMode() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800463 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -0700464 if (the_trace_ == NULL) {
465 return kTracingInactive;
466 } else if (the_trace_->sampling_enabled_) {
467 return kSampleProfilingActive;
468 } else {
469 return kMethodTracingActive;
470 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800471}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800472
Jeff Hao23009dc2013-08-22 15:36:42 -0700473Trace::Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled)
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
Jeff Hao23009dc2013-08-22 15:36:42 -0700475 sampling_enabled_(sampling_enabled), clock_source_(default_clock_source_),
Jeff Haoc5d824a2014-07-28 18:35:38 -0700476 buffer_size_(buffer_size), start_time_(MicroTime()),
477 clock_overhead_ns_(GetClockOverheadNanoSeconds()), cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800480 memset(buf_.get(), 0, kTraceHeaderLength);
481 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800482 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800483 Append2LE(buf_.get() + 6, kTraceHeaderLength);
484 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 if (trace_version >= kTraceVersionDualClock) {
486 uint16_t record_size = GetRecordSize(clock_source_);
487 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800488 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800489
jeffhao2692b572011-12-16 15:42:28 -0800490 // Update current offset.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700491 cur_offset_.StoreRelaxed(kTraceHeaderLength);
Ian Rogers62d6c772013-02-27 08:32:07 -0800492}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800493
Ian Rogerse63db272014-07-15 15:36:11 -0700494static void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
496 uint8_t* ptr = buf + kTraceHeaderLength;
497 uint8_t* end = buf + buf_size;
498
499 while (ptr < end) {
500 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 TraceAction action = DecodeTraceAction(tmid);
503 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
504 ptr += GetRecordSize(clock_source);
505 }
jeffhaoe343b762011-12-05 16:36:44 -0800506}
507
jeffhao2692b572011-12-16 15:42:28 -0800508void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800509 // Compute elapsed time.
510 uint64_t elapsed = MicroTime() - start_time_;
511
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700512 size_t final_offset = cur_offset_.LoadRelaxed();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800513
Brian Carlstromea46f952013-07-30 01:26:50 -0700514 std::set<mirror::ArtMethod*> visited_methods;
Ian Rogers62d6c772013-02-27 08:32:07 -0800515 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800516
517 std::ostringstream os;
518
519 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800520 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800521 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
522 if (UseThreadCpuClock()) {
523 if (UseWallClock()) {
524 os << StringPrintf("clock=dual\n");
525 } else {
526 os << StringPrintf("clock=thread-cpu\n");
527 }
528 } else {
529 os << StringPrintf("clock=wall\n");
530 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800531 os << StringPrintf("elapsed-time-usec=%" PRIu64 "\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800532 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
533 os << StringPrintf("num-method-calls=%zd\n", num_records);
Jeff Haoc5d824a2014-07-28 18:35:38 -0700534 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800535 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700536 if ((flags_ & kTraceCountAllocs) != 0) {
537 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
538 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
539 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
540 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800541 os << StringPrintf("%cthreads\n", kTraceTokenChar);
542 DumpThreadList(os);
543 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800544 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800545 os << StringPrintf("%cend\n", kTraceTokenChar);
546
547 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800548 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700549 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800550 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
551 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800552 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800553 iov[1].iov_len = final_offset;
554 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800555 const bool kDumpTraceInfo = false;
556 if (kDumpTraceInfo) {
557 LOG(INFO) << "Trace sent:\n" << header;
558 DumpBuf(buf_.get(), final_offset, clock_source_);
559 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800560 } else {
561 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800562 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700563 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
564 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800566 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800567 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800568}
569
Ian Rogers62d6c772013-02-27 08:32:07 -0800570void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800571 mirror::ArtMethod* method, uint32_t new_dex_pc) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700572 UNUSED(thread, this_object, method, new_dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 // We're not recorded to listen to this kind of event, so complain.
574 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700575}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800576
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700577void Trace::FieldRead(Thread* thread, mirror::Object* this_object,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200578 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field)
579 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700580 UNUSED(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200581 // We're not recorded to listen to this kind of event, so complain.
582 LOG(ERROR) << "Unexpected field read event in tracing " << PrettyMethod(method) << " " << dex_pc;
583}
584
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700585void Trace::FieldWritten(Thread* thread, mirror::Object* this_object,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200586 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
587 const JValue& field_value)
588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700589 UNUSED(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200590 // We're not recorded to listen to this kind of event, so complain.
591 LOG(ERROR) << "Unexpected field write event in tracing " << PrettyMethod(method) << " " << dex_pc;
592}
593
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700594void Trace::MethodEntered(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
595 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700596 uint32_t thread_clock_diff = 0;
597 uint32_t wall_clock_diff = 0;
598 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
599 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
600 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800601}
602
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700603void Trace::MethodExited(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
604 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED,
605 const JValue& return_value ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700606 uint32_t thread_clock_diff = 0;
607 uint32_t wall_clock_diff = 0;
608 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
609 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
610 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800611}
612
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700613void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
614 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700615 uint32_t thread_clock_diff = 0;
616 uint32_t wall_clock_diff = 0;
617 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
618 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
619 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800620}
621
622void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700623 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800624 mirror::Throwable* exception_object)
625 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626 UNUSED(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800627 LOG(ERROR) << "Unexpected exception caught event in tracing";
628}
629
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700630void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
631 if (UseThreadCpuClock()) {
632 uint64_t clock_base = thread->GetTraceClockBase();
633 if (UNLIKELY(clock_base == 0)) {
634 // First event, record the base time in the map.
635 uint64_t time = thread->GetCpuMicroTime();
636 thread->SetTraceClockBase(time);
637 } else {
638 *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
639 }
640 }
641 if (UseWallClock()) {
642 *wall_clock_diff = MicroTime() - start_time_;
643 }
644}
645
Ian Rogersef7d42f2014-01-06 12:55:46 -0800646void Trace::LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700647 instrumentation::Instrumentation::InstrumentationEvent event,
648 uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800649 // Advance cur_offset_ atomically.
650 int32_t new_offset;
651 int32_t old_offset;
652 do {
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700653 old_offset = cur_offset_.LoadRelaxed();
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800655 if (new_offset > buffer_size_) {
656 overflow_ = true;
657 return;
658 }
Ian Rogers54fb8fd2014-07-09 23:16:06 -0700659 } while (!cur_offset_.CompareExchangeWeakSequentiallyConsistent(old_offset, new_offset));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800660
Ian Rogers62d6c772013-02-27 08:32:07 -0800661 TraceAction action = kTraceMethodEnter;
662 switch (event) {
663 case instrumentation::Instrumentation::kMethodEntered:
664 action = kTraceMethodEnter;
665 break;
666 case instrumentation::Instrumentation::kMethodExited:
667 action = kTraceMethodExit;
668 break;
669 case instrumentation::Instrumentation::kMethodUnwind:
670 action = kTraceUnroll;
671 break;
672 default:
673 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
674 }
675
676 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800677
678 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800679 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800681 Append4LE(ptr + 2, method_value);
682 ptr += 6;
683
684 if (UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800685 Append4LE(ptr, thread_clock_diff);
686 ptr += 4;
687 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800688 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800689 Append4LE(ptr, wall_clock_diff);
690 }
691}
692
Ian Rogers62d6c772013-02-27 08:32:07 -0800693void Trace::GetVisitedMethods(size_t buf_size,
Brian Carlstromea46f952013-07-30 01:26:50 -0700694 std::set<mirror::ArtMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800695 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800697
698 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800699 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700700 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 visited_methods->insert(method);
702 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800703 }
704}
705
Mathieu Chartier02e25112013-08-14 16:14:24 -0700706void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700707 for (const auto& method : visited_methods) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700708 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700709 PrettyDescriptor(method->GetDeclaringClassDescriptor()).c_str(), method->GetName(),
710 method->GetSignature().ToString().c_str(), method->GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800711 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800712}
713
714static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800715 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
716 std::string name;
717 t->GetThreadName(name);
718 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800719}
720
721void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700722 Thread* self = Thread::Current();
Jeff Haoe094b872014-10-14 13:12:01 -0700723 for (auto it : exited_threads_) {
724 os << it.first << "\t" << it.second << "\n";
725 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700726 Locks::thread_list_lock_->AssertNotHeld(self);
727 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800728 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800729}
730
Jeff Haoe094b872014-10-14 13:12:01 -0700731void Trace::StoreExitingThreadInfo(Thread* thread) {
732 MutexLock mu(thread, *Locks::trace_lock_);
733 if (the_trace_ != nullptr) {
734 std::string name;
735 thread->GetThreadName(name);
Andreas Gampea1785c52014-11-25 20:40:08 -0800736 // The same thread/tid may be used multiple times. As SafeMap::Put does not allow to override
737 // a previous mapping, use SafeMap::Overwrite.
738 the_trace_->exited_threads_.Overwrite(thread->GetTid(), name);
Jeff Haoe094b872014-10-14 13:12:01 -0700739 }
740}
741
jeffhaoe343b762011-12-05 16:36:44 -0800742} // namespace art