blob: 2dac71ed6072572ffed354abced58521b8328f6c [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
Jeff Hao0abc72e2013-08-13 13:45:14 -070021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080023#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include "common_throws.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080025#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
jeffhao725a9572012-11-13 18:20:12 -080027#include "instrumentation.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/dex_cache.h"
31#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080033#include "object_utils.h"
34#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Jeff Hao0abc72e2013-08-13 13:45:14 -070036#include "ScopedLocalRef.h"
jeffhaoe343b762011-12-05 16:36:44 -080037#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070038#include "thread_list.h"
Ian Rogers166db042013-07-26 12:05:57 -070039#if !defined(ART_USE_PORTABLE_COMPILER)
40#include "entrypoints/quick/quick_entrypoints.h"
41#endif
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
92 bool VisitFrame() {
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 Rogers62d6c772013-02-27 08:32:07 -0800118#if defined(HAVE_POSIX_CLOCKS)
119ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceDual;
120#else
121ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceWall;
122#endif
Elliott Hughese119a362012-05-22 17:37:06 -0700123
Jeff Hao0abc72e2013-08-13 13:45:14 -0700124Trace* volatile Trace::the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700125pthread_t Trace::sampling_pthread_ = 0U;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700126UniquePtr<std::vector<mirror::ArtMethod*> > Trace::temp_stack_trace_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800127
Brian Carlstromea46f952013-07-30 01:26:50 -0700128static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
129 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800130}
Elliott Hughese119a362012-05-22 17:37:06 -0700131
Ian Rogers62d6c772013-02-27 08:32:07 -0800132static TraceAction DecodeTraceAction(uint32_t tmid) {
133 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
134}
135
Brian Carlstromea46f952013-07-30 01:26:50 -0700136static uint32_t EncodeTraceMethodAndAction(const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 TraceAction action) {
138 uint32_t tmid = reinterpret_cast<uint32_t>(method) | action;
139 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
140 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800141}
142
Jeff Hao5ce4b172013-08-16 16:27:18 -0700143std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
144 if (temp_stack_trace_.get() != NULL) {
145 return temp_stack_trace_.release();
146 } else {
147 return new std::vector<mirror::ArtMethod*>();
148 }
149}
150
151void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
152 stack_trace->clear();
153 temp_stack_trace_.reset(stack_trace);
154}
155
Elliott Hughese119a362012-05-22 17:37:06 -0700156void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800157#if defined(HAVE_POSIX_CLOCKS)
158 default_clock_source_ = clock_source;
159#else
160 if (clock_source != kProfilerClockSourceWall) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700161 LOG(WARNING) << "Ignoring tracing request to use CPU time.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 }
163#endif
164}
165
166static uint16_t GetTraceVersion(ProfilerClockSource clock_source) {
167 return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock
168 : kTraceVersionSingleClock;
169}
170
171static uint16_t GetRecordSize(ProfilerClockSource clock_source) {
172 return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock
173 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800174}
175
Elliott Hughese119a362012-05-22 17:37:06 -0700176bool Trace::UseThreadCpuClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 return (clock_source_ == kProfilerClockSourceThreadCpu) ||
178 (clock_source_ == kProfilerClockSourceDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800179}
180
Elliott Hughese119a362012-05-22 17:37:06 -0700181bool Trace::UseWallClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 return (clock_source_ == kProfilerClockSourceWall) ||
183 (clock_source_ == kProfilerClockSourceDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700184}
185
186static void MeasureClockOverhead(Trace* trace) {
187 if (trace->UseThreadCpuClock()) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700188 Thread::Current()->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800189 }
Elliott Hughese119a362012-05-22 17:37:06 -0700190 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800191 MicroTime();
192 }
193}
194
Jeff Hao5ce4b172013-08-16 16:27:18 -0700195// Compute an average time taken to measure clocks.
196static uint32_t GetClockOverheadNanoSeconds(Trace* trace) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700197 Thread* self = Thread::Current();
198 uint64_t start = self->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800199
200 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700201 MeasureClockOverhead(trace);
202 MeasureClockOverhead(trace);
203 MeasureClockOverhead(trace);
204 MeasureClockOverhead(trace);
205 MeasureClockOverhead(trace);
206 MeasureClockOverhead(trace);
207 MeasureClockOverhead(trace);
208 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800209 }
210
Jeff Hao5ce4b172013-08-16 16:27:18 -0700211 uint64_t elapsed_us = self->GetCpuMicroTime() - start;
212 return static_cast<uint32_t>(elapsed_us / 32);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800213}
214
Elliott Hughesffb465f2012-03-01 18:46:05 -0800215// TODO: put this somewhere with the big-endian equivalent used by JDWP.
216static void Append2LE(uint8_t* buf, uint16_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700217 *buf++ = static_cast<uint8_t>(val);
218 *buf++ = static_cast<uint8_t>(val >> 8);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800219}
220
Elliott Hughesffb465f2012-03-01 18:46:05 -0800221// TODO: put this somewhere with the big-endian equivalent used by JDWP.
222static void Append4LE(uint8_t* buf, uint32_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700223 *buf++ = static_cast<uint8_t>(val);
224 *buf++ = static_cast<uint8_t>(val >> 8);
225 *buf++ = static_cast<uint8_t>(val >> 16);
226 *buf++ = static_cast<uint8_t>(val >> 24);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800227}
228
Elliott Hughesffb465f2012-03-01 18:46:05 -0800229// TODO: put this somewhere with the big-endian equivalent used by JDWP.
230static void Append8LE(uint8_t* buf, uint64_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700231 *buf++ = static_cast<uint8_t>(val);
232 *buf++ = static_cast<uint8_t>(val >> 8);
233 *buf++ = static_cast<uint8_t>(val >> 16);
234 *buf++ = static_cast<uint8_t>(val >> 24);
235 *buf++ = static_cast<uint8_t>(val >> 32);
236 *buf++ = static_cast<uint8_t>(val >> 40);
237 *buf++ = static_cast<uint8_t>(val >> 48);
238 *buf++ = static_cast<uint8_t>(val >> 56);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800239}
240
Jeff Hao0abc72e2013-08-13 13:45:14 -0700241static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
242 BuildStackTraceVisitor build_trace_visitor(thread);
243 build_trace_visitor.WalkStack();
Ian Rogerse2f77e72013-08-13 19:19:40 -0700244 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700245 Trace* the_trace = reinterpret_cast<Trace*>(arg);
246 the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
247}
248
Jeff Hao5ce4b172013-08-16 16:27:18 -0700249static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg) {
250 thread->SetTraceClockBase(0);
251 std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
252 thread->SetStackTraceSample(NULL);
253 delete stack_trace;
254}
255
Jeff Hao0abc72e2013-08-13 13:45:14 -0700256void Trace::CompareAndUpdateStackTrace(Thread* thread,
Ian Rogerse2f77e72013-08-13 19:19:40 -0700257 std::vector<mirror::ArtMethod*>* stack_trace) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700258 CHECK_EQ(pthread_self(), sampling_pthread_);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700259 std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
260 // Update the thread's stack trace sample.
261 thread->SetStackTraceSample(stack_trace);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700262 // Read timer clocks to use for all events in this trace.
263 uint32_t thread_clock_diff = 0;
264 uint32_t wall_clock_diff = 0;
265 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700266 if (old_stack_trace == NULL) {
267 // If there's no previous stack trace sample for this thread, log an entry event for all
Jeff Hao0abc72e2013-08-13 13:45:14 -0700268 // methods in the trace.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700269 for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700270 rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700271 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
272 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700273 }
274 } else {
275 // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
276 // events accordingly.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700277 std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
278 std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700279 // Iterate bottom-up over both traces until there's a difference between them.
280 while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
281 old_rit++;
282 rit++;
283 }
284 // Iterate top-down over the old trace until the point where they differ, emitting exit events.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700285 for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700286 old_it != old_rit.base(); ++old_it) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700287 LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
288 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700289 }
290 // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
291 for (; rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700292 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
293 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700294 }
Jeff Hao5ce4b172013-08-16 16:27:18 -0700295 FreeStackTrace(old_stack_trace);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700296 }
297}
298
299void* Trace::RunSamplingThread(void* arg) {
300 Runtime* runtime = Runtime::Current();
Jeff Hao23009dc2013-08-22 15:36:42 -0700301 int interval_us = reinterpret_cast<int>(arg);
Jeff Hao4044bda2014-01-06 15:50:45 -0800302 CHECK_GE(interval_us, 0);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700303 CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
304 !runtime->IsCompiler()));
305
306 while (true) {
Jeff Hao23009dc2013-08-22 15:36:42 -0700307 usleep(interval_us);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700308 ATRACE_BEGIN("Profile sampling");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700309 Thread* self = Thread::Current();
310 Trace* the_trace;
311 {
312 MutexLock mu(self, *Locks::trace_lock_);
313 the_trace = the_trace_;
314 if (the_trace == NULL) {
315 break;
316 }
317 }
318
319 runtime->GetThreadList()->SuspendAll();
320 {
321 MutexLock mu(self, *Locks::thread_list_lock_);
322 runtime->GetThreadList()->ForEach(GetSample, the_trace);
323 }
324 runtime->GetThreadList()->ResumeAll();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700325 ATRACE_END();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700326 }
327
328 runtime->DetachCurrentThread();
329 return NULL;
330}
331
Ian Rogers62d6c772013-02-27 08:32:07 -0800332void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -0700333 bool direct_to_ddms, bool sampling_enabled, int interval_us) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 Thread* self = Thread::Current();
335 {
336 MutexLock mu(self, *Locks::trace_lock_);
337 if (the_trace_ != NULL) {
338 LOG(ERROR) << "Trace already in progress, ignoring this request";
339 return;
340 }
jeffhaoe343b762011-12-05 16:36:44 -0800341 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 Runtime* runtime = Runtime::Current();
343 runtime->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800344
jeffhao2692b572011-12-16 15:42:28 -0800345 // Open trace file if not going directly to ddms.
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 UniquePtr<File> trace_file;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800347 if (!direct_to_ddms) {
348 if (trace_fd < 0) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700349 trace_file.reset(OS::CreateEmptyFile(trace_filename));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800350 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 trace_file.reset(new File(trace_fd, "tracefile"));
Elliott Hughes76160052012-12-12 16:31:20 -0800352 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800353 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 if (trace_file.get() == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700355 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 runtime->GetThreadList()->ResumeAll();
357 ScopedObjectAccess soa(self);
358 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800359 return;
360 }
361 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800362
jeffhao2692b572011-12-16 15:42:28 -0800363 // Create Trace object.
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 {
365 MutexLock mu(self, *Locks::trace_lock_);
Brian Carlstromdf629502013-07-17 22:39:56 -0700366 if (the_trace_ != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 LOG(ERROR) << "Trace already in progress, ignoring this request";
368 } else {
Jeff Hao23009dc2013-08-22 15:36:42 -0700369 the_trace_ = new Trace(trace_file.release(), buffer_size, flags, sampling_enabled);
jeffhao0791adc2012-04-04 11:14:32 -0700370
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 // Enable count of allocs if specified in the flags.
372 if ((flags && kTraceCountAllocs) != 0) {
373 runtime->SetStatsEnabled(true);
374 }
375
Jeff Hao23009dc2013-08-22 15:36:42 -0700376
377
378 if (sampling_enabled) {
379 CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread,
380 reinterpret_cast<void*>(interval_us)),
381 "Sampling profiler thread");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700382 } else {
383 runtime->GetInstrumentation()->AddListener(the_trace_,
384 instrumentation::Instrumentation::kMethodEntered |
385 instrumentation::Instrumentation::kMethodExited |
386 instrumentation::Instrumentation::kMethodUnwind);
387 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 }
jeffhao0791adc2012-04-04 11:14:32 -0700389 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 runtime->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800391}
392
393void Trace::Stop() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 Runtime* runtime = Runtime::Current();
395 runtime->GetThreadList()->SuspendAll();
396 Trace* the_trace = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700397 pthread_t sampling_pthread = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 {
399 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
400 if (the_trace_ == NULL) {
401 LOG(ERROR) << "Trace stop requested, but no trace currently running";
402 } else {
403 the_trace = the_trace_;
404 the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700405 sampling_pthread = sampling_pthread_;
406 sampling_pthread_ = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 }
jeffhao2692b572011-12-16 15:42:28 -0800408 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 if (the_trace != NULL) {
410 the_trace->FinishTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700411
Jeff Hao23009dc2013-08-22 15:36:42 -0700412 if (the_trace->sampling_enabled_) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700413 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
414 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL);
415 } else {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700416 runtime->GetInstrumentation()->RemoveListener(the_trace,
417 instrumentation::Instrumentation::kMethodEntered |
418 instrumentation::Instrumentation::kMethodExited |
419 instrumentation::Instrumentation::kMethodUnwind);
420 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 delete the_trace;
422 }
423 runtime->GetThreadList()->ResumeAll();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700424
Jeff Hao23009dc2013-08-22 15:36:42 -0700425 if (sampling_pthread != 0U) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700426 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
427 }
jeffhao2692b572011-12-16 15:42:28 -0800428}
429
jeffhaob5e81852012-03-12 11:15:45 -0700430void Trace::Shutdown() {
Jeff Hao64caa7d2013-08-29 11:18:01 -0700431 if (GetMethodTracingMode() != kTracingInactive) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800432 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700433 }
jeffhaob5e81852012-03-12 11:15:45 -0700434}
435
Jeff Hao64caa7d2013-08-29 11:18:01 -0700436TracingMode Trace::GetMethodTracingMode() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800437 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -0700438 if (the_trace_ == NULL) {
439 return kTracingInactive;
440 } else if (the_trace_->sampling_enabled_) {
441 return kSampleProfilingActive;
442 } else {
443 return kMethodTracingActive;
444 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800445}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800446
Jeff Hao23009dc2013-08-22 15:36:42 -0700447Trace::Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled)
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
Jeff Hao23009dc2013-08-22 15:36:42 -0700449 sampling_enabled_(sampling_enabled), clock_source_(default_clock_source_),
450 buffer_size_(buffer_size), start_time_(MicroTime()), cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800451 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800453 memset(buf_.get(), 0, kTraceHeaderLength);
454 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800456 Append2LE(buf_.get() + 6, kTraceHeaderLength);
457 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800458 if (trace_version >= kTraceVersionDualClock) {
459 uint16_t record_size = GetRecordSize(clock_source_);
460 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800461 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800462
jeffhao2692b572011-12-16 15:42:28 -0800463 // Update current offset.
464 cur_offset_ = kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800465}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800466
Ian Rogers62d6c772013-02-27 08:32:07 -0800467static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source)
468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
469 uint8_t* ptr = buf + kTraceHeaderLength;
470 uint8_t* end = buf + buf_size;
471
472 while (ptr < end) {
473 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700474 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 TraceAction action = DecodeTraceAction(tmid);
476 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
477 ptr += GetRecordSize(clock_source);
478 }
jeffhaoe343b762011-12-05 16:36:44 -0800479}
480
jeffhao2692b572011-12-16 15:42:28 -0800481void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800482 // Compute elapsed time.
483 uint64_t elapsed = MicroTime() - start_time_;
484
485 size_t final_offset = cur_offset_;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700486 uint32_t clock_overhead_ns = GetClockOverheadNanoSeconds(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800487
jeffhao0791adc2012-04-04 11:14:32 -0700488 if ((flags_ & kTraceCountAllocs) != 0) {
489 Runtime::Current()->SetStatsEnabled(false);
490 }
491
Brian Carlstromea46f952013-07-30 01:26:50 -0700492 std::set<mirror::ArtMethod*> visited_methods;
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800494
495 std::ostringstream os;
496
497 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800498 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800499 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
500 if (UseThreadCpuClock()) {
501 if (UseWallClock()) {
502 os << StringPrintf("clock=dual\n");
503 } else {
504 os << StringPrintf("clock=thread-cpu\n");
505 }
506 } else {
507 os << StringPrintf("clock=wall\n");
508 }
509 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800510 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
511 os << StringPrintf("num-method-calls=%zd\n", num_records);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700512 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800513 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700514 if ((flags_ & kTraceCountAllocs) != 0) {
515 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
516 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
517 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
518 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800519 os << StringPrintf("%cthreads\n", kTraceTokenChar);
520 DumpThreadList(os);
521 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800522 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800523 os << StringPrintf("%cend\n", kTraceTokenChar);
524
525 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800526 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700527 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800528 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
529 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800530 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800531 iov[1].iov_len = final_offset;
532 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 const bool kDumpTraceInfo = false;
534 if (kDumpTraceInfo) {
535 LOG(INFO) << "Trace sent:\n" << header;
536 DumpBuf(buf_.get(), final_offset, clock_source_);
537 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800538 } else {
539 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800540 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700541 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
542 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800543 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800544 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800545 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800546}
547
Ian Rogers62d6c772013-02-27 08:32:07 -0800548void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 const mirror::ArtMethod* method, uint32_t new_dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 // We're not recorded to listen to this kind of event, so complain.
551 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
552};
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800553
Ian Rogers62d6c772013-02-27 08:32:07 -0800554void Trace::MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700555 const mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700556 uint32_t thread_clock_diff = 0;
557 uint32_t wall_clock_diff = 0;
558 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
559 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
560 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800561}
562
563void Trace::MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 const JValue& return_value) {
566 UNUSED(return_value);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700567 uint32_t thread_clock_diff = 0;
568 uint32_t wall_clock_diff = 0;
569 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
570 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
571 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800572}
573
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100574void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object,
575 const mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700576 uint32_t thread_clock_diff = 0;
577 uint32_t wall_clock_diff = 0;
578 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
579 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
580 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800581}
582
583void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800585 mirror::Throwable* exception_object)
586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
587 LOG(ERROR) << "Unexpected exception caught event in tracing";
588}
589
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700590void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
591 if (UseThreadCpuClock()) {
592 uint64_t clock_base = thread->GetTraceClockBase();
593 if (UNLIKELY(clock_base == 0)) {
594 // First event, record the base time in the map.
595 uint64_t time = thread->GetCpuMicroTime();
596 thread->SetTraceClockBase(time);
597 } else {
598 *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
599 }
600 }
601 if (UseWallClock()) {
602 *wall_clock_diff = MicroTime() - start_time_;
603 }
604}
605
Brian Carlstromea46f952013-07-30 01:26:50 -0700606void Trace::LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700607 instrumentation::Instrumentation::InstrumentationEvent event,
608 uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800609 // Advance cur_offset_ atomically.
610 int32_t new_offset;
611 int32_t old_offset;
612 do {
613 old_offset = cur_offset_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800614 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800615 if (new_offset > buffer_size_) {
616 overflow_ = true;
617 return;
618 }
619 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
620
Ian Rogers62d6c772013-02-27 08:32:07 -0800621 TraceAction action = kTraceMethodEnter;
622 switch (event) {
623 case instrumentation::Instrumentation::kMethodEntered:
624 action = kTraceMethodEnter;
625 break;
626 case instrumentation::Instrumentation::kMethodExited:
627 action = kTraceMethodExit;
628 break;
629 case instrumentation::Instrumentation::kMethodUnwind:
630 action = kTraceUnroll;
631 break;
632 default:
633 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
634 }
635
636 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800637
638 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800639 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800641 Append4LE(ptr + 2, method_value);
642 ptr += 6;
643
644 if (UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800645 Append4LE(ptr, thread_clock_diff);
646 ptr += 4;
647 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800648 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800649 Append4LE(ptr, wall_clock_diff);
650 }
651}
652
Ian Rogers62d6c772013-02-27 08:32:07 -0800653void Trace::GetVisitedMethods(size_t buf_size,
Brian Carlstromea46f952013-07-30 01:26:50 -0700654 std::set<mirror::ArtMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800655 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800657
658 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800659 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700660 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800661 visited_methods->insert(method);
662 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800663 }
664}
665
Mathieu Chartier02e25112013-08-14 16:14:24 -0700666void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800667 MethodHelper mh;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700668 for (const auto& method : visited_methods) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800669 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700670 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800671 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogersd91d6d62013-09-25 20:26:14 -0700672 mh.GetSignature().ToString().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800673 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800674}
675
676static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800677 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
678 std::string name;
679 t->GetThreadName(name);
680 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800681}
682
683void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700684 Thread* self = Thread::Current();
685 Locks::thread_list_lock_->AssertNotHeld(self);
686 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800687 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800688}
689
jeffhaoe343b762011-12-05 16:36:44 -0800690} // namespace art