blob: 3a8ba62840a8a9e6e09a9d934cb180677a23b55a [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;
125// TODO: Add way to enable sampling and set interval through gui.
126bool Trace::sampling_enabled_ = true;
127uint32_t Trace::sampling_interval_us_ = 10000;
128pthread_t Trace::sampling_pthread_ = 0U;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700129UniquePtr<std::vector<mirror::ArtMethod*> > Trace::temp_stack_trace_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800130
Brian Carlstromea46f952013-07-30 01:26:50 -0700131static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
132 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800133}
Elliott Hughese119a362012-05-22 17:37:06 -0700134
Ian Rogers62d6c772013-02-27 08:32:07 -0800135static TraceAction DecodeTraceAction(uint32_t tmid) {
136 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
137}
138
Brian Carlstromea46f952013-07-30 01:26:50 -0700139static uint32_t EncodeTraceMethodAndAction(const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800140 TraceAction action) {
141 uint32_t tmid = reinterpret_cast<uint32_t>(method) | action;
142 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
143 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800144}
145
Jeff Hao5ce4b172013-08-16 16:27:18 -0700146std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
147 if (temp_stack_trace_.get() != NULL) {
148 return temp_stack_trace_.release();
149 } else {
150 return new std::vector<mirror::ArtMethod*>();
151 }
152}
153
154void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
155 stack_trace->clear();
156 temp_stack_trace_.reset(stack_trace);
157}
158
Elliott Hughese119a362012-05-22 17:37:06 -0700159void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800160#if defined(HAVE_POSIX_CLOCKS)
161 default_clock_source_ = clock_source;
162#else
163 if (clock_source != kProfilerClockSourceWall) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700164 LOG(WARNING) << "Ignoring tracing request to use CPU time.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 }
166#endif
167}
168
169static uint16_t GetTraceVersion(ProfilerClockSource clock_source) {
170 return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock
171 : kTraceVersionSingleClock;
172}
173
174static uint16_t GetRecordSize(ProfilerClockSource clock_source) {
175 return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock
176 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800177}
178
Elliott Hughese119a362012-05-22 17:37:06 -0700179bool Trace::UseThreadCpuClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 return (clock_source_ == kProfilerClockSourceThreadCpu) ||
181 (clock_source_ == kProfilerClockSourceDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800182}
183
Elliott Hughese119a362012-05-22 17:37:06 -0700184bool Trace::UseWallClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 return (clock_source_ == kProfilerClockSourceWall) ||
186 (clock_source_ == kProfilerClockSourceDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700187}
188
189static void MeasureClockOverhead(Trace* trace) {
190 if (trace->UseThreadCpuClock()) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700191 Thread::Current()->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800192 }
Elliott Hughese119a362012-05-22 17:37:06 -0700193 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800194 MicroTime();
195 }
196}
197
Jeff Hao5ce4b172013-08-16 16:27:18 -0700198// Compute an average time taken to measure clocks.
199static uint32_t GetClockOverheadNanoSeconds(Trace* trace) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700200 Thread* self = Thread::Current();
201 uint64_t start = self->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800202
203 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700204 MeasureClockOverhead(trace);
205 MeasureClockOverhead(trace);
206 MeasureClockOverhead(trace);
207 MeasureClockOverhead(trace);
208 MeasureClockOverhead(trace);
209 MeasureClockOverhead(trace);
210 MeasureClockOverhead(trace);
211 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800212 }
213
Jeff Hao5ce4b172013-08-16 16:27:18 -0700214 uint64_t elapsed_us = self->GetCpuMicroTime() - start;
215 return static_cast<uint32_t>(elapsed_us / 32);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800216}
217
Elliott Hughesffb465f2012-03-01 18:46:05 -0800218// TODO: put this somewhere with the big-endian equivalent used by JDWP.
219static void Append2LE(uint8_t* buf, uint16_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700220 *buf++ = static_cast<uint8_t>(val);
221 *buf++ = static_cast<uint8_t>(val >> 8);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800222}
223
Elliott Hughesffb465f2012-03-01 18:46:05 -0800224// TODO: put this somewhere with the big-endian equivalent used by JDWP.
225static void Append4LE(uint8_t* buf, uint32_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700226 *buf++ = static_cast<uint8_t>(val);
227 *buf++ = static_cast<uint8_t>(val >> 8);
228 *buf++ = static_cast<uint8_t>(val >> 16);
229 *buf++ = static_cast<uint8_t>(val >> 24);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800230}
231
Elliott Hughesffb465f2012-03-01 18:46:05 -0800232// TODO: put this somewhere with the big-endian equivalent used by JDWP.
233static void Append8LE(uint8_t* buf, uint64_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700234 *buf++ = static_cast<uint8_t>(val);
235 *buf++ = static_cast<uint8_t>(val >> 8);
236 *buf++ = static_cast<uint8_t>(val >> 16);
237 *buf++ = static_cast<uint8_t>(val >> 24);
238 *buf++ = static_cast<uint8_t>(val >> 32);
239 *buf++ = static_cast<uint8_t>(val >> 40);
240 *buf++ = static_cast<uint8_t>(val >> 48);
241 *buf++ = static_cast<uint8_t>(val >> 56);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800242}
243
Jeff Hao0abc72e2013-08-13 13:45:14 -0700244static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
245 BuildStackTraceVisitor build_trace_visitor(thread);
246 build_trace_visitor.WalkStack();
Ian Rogerse2f77e72013-08-13 19:19:40 -0700247 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700248 Trace* the_trace = reinterpret_cast<Trace*>(arg);
249 the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
250}
251
Jeff Hao5ce4b172013-08-16 16:27:18 -0700252static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg) {
253 thread->SetTraceClockBase(0);
254 std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
255 thread->SetStackTraceSample(NULL);
256 delete stack_trace;
257}
258
Jeff Hao0abc72e2013-08-13 13:45:14 -0700259void Trace::CompareAndUpdateStackTrace(Thread* thread,
Ian Rogerse2f77e72013-08-13 19:19:40 -0700260 std::vector<mirror::ArtMethod*>* stack_trace) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700261 CHECK_EQ(pthread_self(), sampling_pthread_);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700262 std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
263 // Update the thread's stack trace sample.
264 thread->SetStackTraceSample(stack_trace);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700265 // Read timer clocks to use for all events in this trace.
266 uint32_t thread_clock_diff = 0;
267 uint32_t wall_clock_diff = 0;
268 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700269 if (old_stack_trace == NULL) {
270 // If there's no previous stack trace sample for this thread, log an entry event for all
Jeff Hao0abc72e2013-08-13 13:45:14 -0700271 // methods in the trace.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700272 for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700273 rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700274 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
275 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700276 }
277 } else {
278 // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
279 // events accordingly.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700280 std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
281 std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700282 // Iterate bottom-up over both traces until there's a difference between them.
283 while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
284 old_rit++;
285 rit++;
286 }
287 // Iterate top-down over the old trace until the point where they differ, emitting exit events.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700288 for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700289 old_it != old_rit.base(); ++old_it) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700290 LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
291 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700292 }
293 // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
294 for (; rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700295 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
296 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700297 }
Jeff Hao5ce4b172013-08-16 16:27:18 -0700298 FreeStackTrace(old_stack_trace);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700299 }
300}
301
302void* Trace::RunSamplingThread(void* arg) {
303 Runtime* runtime = Runtime::Current();
304 CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
305 !runtime->IsCompiler()));
306
307 while (true) {
308 usleep(sampling_interval_us_);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700309 ATRACE_BEGIN("Profile sampling");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700310 Thread* self = Thread::Current();
311 Trace* the_trace;
312 {
313 MutexLock mu(self, *Locks::trace_lock_);
314 the_trace = the_trace_;
315 if (the_trace == NULL) {
316 break;
317 }
318 }
319
320 runtime->GetThreadList()->SuspendAll();
321 {
322 MutexLock mu(self, *Locks::thread_list_lock_);
323 runtime->GetThreadList()->ForEach(GetSample, the_trace);
324 }
325 runtime->GetThreadList()->ResumeAll();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700326 ATRACE_END();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700327 }
328
329 runtime->DetachCurrentThread();
330 return NULL;
331}
332
Ian Rogers62d6c772013-02-27 08:32:07 -0800333void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
334 bool direct_to_ddms) {
335 Thread* self = Thread::Current();
336 {
337 MutexLock mu(self, *Locks::trace_lock_);
338 if (the_trace_ != NULL) {
339 LOG(ERROR) << "Trace already in progress, ignoring this request";
340 return;
341 }
jeffhaoe343b762011-12-05 16:36:44 -0800342 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 Runtime* runtime = Runtime::Current();
344 runtime->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800345
jeffhao2692b572011-12-16 15:42:28 -0800346 // Open trace file if not going directly to ddms.
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 UniquePtr<File> trace_file;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800348 if (!direct_to_ddms) {
349 if (trace_fd < 0) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700350 trace_file.reset(OS::CreateEmptyFile(trace_filename));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 trace_file.reset(new File(trace_fd, "tracefile"));
Elliott Hughes76160052012-12-12 16:31:20 -0800353 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800354 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 if (trace_file.get() == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700356 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 runtime->GetThreadList()->ResumeAll();
358 ScopedObjectAccess soa(self);
359 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800360 return;
361 }
362 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800363
jeffhao2692b572011-12-16 15:42:28 -0800364 // Create Trace object.
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 {
366 MutexLock mu(self, *Locks::trace_lock_);
Brian Carlstromdf629502013-07-17 22:39:56 -0700367 if (the_trace_ != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 LOG(ERROR) << "Trace already in progress, ignoring this request";
369 } else {
370 the_trace_ = new Trace(trace_file.release(), buffer_size, flags);
jeffhao0791adc2012-04-04 11:14:32 -0700371
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 // Enable count of allocs if specified in the flags.
373 if ((flags && kTraceCountAllocs) != 0) {
374 runtime->SetStatsEnabled(true);
375 }
376
Jeff Hao0abc72e2013-08-13 13:45:14 -0700377 if (sampling_enabled_) {
378 CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread, NULL),
379 "Sampling profiler thread");
380 } else {
381 runtime->GetInstrumentation()->AddListener(the_trace_,
382 instrumentation::Instrumentation::kMethodEntered |
383 instrumentation::Instrumentation::kMethodExited |
384 instrumentation::Instrumentation::kMethodUnwind);
385 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 }
jeffhao0791adc2012-04-04 11:14:32 -0700387 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 runtime->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800389}
390
391void Trace::Stop() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 Runtime* runtime = Runtime::Current();
393 runtime->GetThreadList()->SuspendAll();
394 Trace* the_trace = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700395 pthread_t sampling_pthread = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 {
397 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
398 if (the_trace_ == NULL) {
399 LOG(ERROR) << "Trace stop requested, but no trace currently running";
400 } else {
401 the_trace = the_trace_;
402 the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700403 sampling_pthread = sampling_pthread_;
404 sampling_pthread_ = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 }
jeffhao2692b572011-12-16 15:42:28 -0800406 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 if (the_trace != NULL) {
408 the_trace->FinishTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700409
Jeff Hao5ce4b172013-08-16 16:27:18 -0700410 if (sampling_enabled_) {
411 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
412 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL);
413 } else {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700414 runtime->GetInstrumentation()->RemoveListener(the_trace,
415 instrumentation::Instrumentation::kMethodEntered |
416 instrumentation::Instrumentation::kMethodExited |
417 instrumentation::Instrumentation::kMethodUnwind);
418 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800419 delete the_trace;
420 }
421 runtime->GetThreadList()->ResumeAll();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700422
423 if (sampling_enabled_ && sampling_pthread != 0U) {
424 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
425 }
jeffhao2692b572011-12-16 15:42:28 -0800426}
427
jeffhaob5e81852012-03-12 11:15:45 -0700428void Trace::Shutdown() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 if (IsMethodTracingActive()) {
430 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700431 }
jeffhaob5e81852012-03-12 11:15:45 -0700432}
433
Ian Rogers62d6c772013-02-27 08:32:07 -0800434bool Trace::IsMethodTracingActive() {
435 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
436 return the_trace_ != NULL;
437}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800438
Ian Rogers62d6c772013-02-27 08:32:07 -0800439Trace::Trace(File* trace_file, int buffer_size, int flags)
440 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
441 clock_source_(default_clock_source_), buffer_size_(buffer_size), start_time_(MicroTime()),
442 cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800443 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800445 memset(buf_.get(), 0, kTraceHeaderLength);
446 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800448 Append2LE(buf_.get() + 6, kTraceHeaderLength);
449 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 if (trace_version >= kTraceVersionDualClock) {
451 uint16_t record_size = GetRecordSize(clock_source_);
452 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800453 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800454
jeffhao2692b572011-12-16 15:42:28 -0800455 // Update current offset.
456 cur_offset_ = kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800457}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800458
Jeff Hao0abc72e2013-08-13 13:45:14 -0700459Trace::~Trace() {
460 CHECK_EQ(sampling_pthread_, static_cast<pthread_t>(0U));
Jeff Hao0abc72e2013-08-13 13:45:14 -0700461}
462
Ian Rogers62d6c772013-02-27 08:32:07 -0800463static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source)
464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
465 uint8_t* ptr = buf + kTraceHeaderLength;
466 uint8_t* end = buf + buf_size;
467
468 while (ptr < end) {
469 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700470 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 TraceAction action = DecodeTraceAction(tmid);
472 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
473 ptr += GetRecordSize(clock_source);
474 }
jeffhaoe343b762011-12-05 16:36:44 -0800475}
476
jeffhao2692b572011-12-16 15:42:28 -0800477void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478 // Compute elapsed time.
479 uint64_t elapsed = MicroTime() - start_time_;
480
481 size_t final_offset = cur_offset_;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700482 uint32_t clock_overhead_ns = GetClockOverheadNanoSeconds(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800483
jeffhao0791adc2012-04-04 11:14:32 -0700484 if ((flags_ & kTraceCountAllocs) != 0) {
485 Runtime::Current()->SetStatsEnabled(false);
486 }
487
Brian Carlstromea46f952013-07-30 01:26:50 -0700488 std::set<mirror::ArtMethod*> visited_methods;
Ian Rogers62d6c772013-02-27 08:32:07 -0800489 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800490
491 std::ostringstream os;
492
493 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800494 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800495 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
496 if (UseThreadCpuClock()) {
497 if (UseWallClock()) {
498 os << StringPrintf("clock=dual\n");
499 } else {
500 os << StringPrintf("clock=thread-cpu\n");
501 }
502 } else {
503 os << StringPrintf("clock=wall\n");
504 }
505 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800506 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
507 os << StringPrintf("num-method-calls=%zd\n", num_records);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700508 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800509 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700510 if ((flags_ & kTraceCountAllocs) != 0) {
511 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
512 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
513 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
514 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800515 os << StringPrintf("%cthreads\n", kTraceTokenChar);
516 DumpThreadList(os);
517 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800518 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800519 os << StringPrintf("%cend\n", kTraceTokenChar);
520
521 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800522 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700523 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800524 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
525 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800526 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800527 iov[1].iov_len = final_offset;
528 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800529 const bool kDumpTraceInfo = false;
530 if (kDumpTraceInfo) {
531 LOG(INFO) << "Trace sent:\n" << header;
532 DumpBuf(buf_.get(), final_offset, clock_source_);
533 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800534 } else {
535 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800536 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700537 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
538 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800539 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800540 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800541 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800542}
543
Ian Rogers62d6c772013-02-27 08:32:07 -0800544void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700545 const mirror::ArtMethod* method, uint32_t new_dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 // We're not recorded to listen to this kind of event, so complain.
547 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
548};
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800549
Ian Rogers62d6c772013-02-27 08:32:07 -0800550void Trace::MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700551 const mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700552 uint32_t thread_clock_diff = 0;
553 uint32_t wall_clock_diff = 0;
554 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
555 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
556 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800557}
558
559void Trace::MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700560 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 const JValue& return_value) {
562 UNUSED(return_value);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700563 uint32_t thread_clock_diff = 0;
564 uint32_t wall_clock_diff = 0;
565 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
566 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
567 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800568}
569
Brian Carlstromea46f952013-07-30 01:26:50 -0700570void Trace::MethodUnwind(Thread* thread, const mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700571 uint32_t thread_clock_diff = 0;
572 uint32_t wall_clock_diff = 0;
573 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
574 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
575 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800576}
577
578void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 mirror::Throwable* exception_object)
581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
582 LOG(ERROR) << "Unexpected exception caught event in tracing";
583}
584
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700585void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
586 if (UseThreadCpuClock()) {
587 uint64_t clock_base = thread->GetTraceClockBase();
588 if (UNLIKELY(clock_base == 0)) {
589 // First event, record the base time in the map.
590 uint64_t time = thread->GetCpuMicroTime();
591 thread->SetTraceClockBase(time);
592 } else {
593 *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
594 }
595 }
596 if (UseWallClock()) {
597 *wall_clock_diff = MicroTime() - start_time_;
598 }
599}
600
Brian Carlstromea46f952013-07-30 01:26:50 -0700601void Trace::LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700602 instrumentation::Instrumentation::InstrumentationEvent event,
603 uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800604 // Advance cur_offset_ atomically.
605 int32_t new_offset;
606 int32_t old_offset;
607 do {
608 old_offset = cur_offset_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800610 if (new_offset > buffer_size_) {
611 overflow_ = true;
612 return;
613 }
614 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
615
Ian Rogers62d6c772013-02-27 08:32:07 -0800616 TraceAction action = kTraceMethodEnter;
617 switch (event) {
618 case instrumentation::Instrumentation::kMethodEntered:
619 action = kTraceMethodEnter;
620 break;
621 case instrumentation::Instrumentation::kMethodExited:
622 action = kTraceMethodExit;
623 break;
624 case instrumentation::Instrumentation::kMethodUnwind:
625 action = kTraceUnroll;
626 break;
627 default:
628 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
629 }
630
631 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800632
633 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800634 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800636 Append4LE(ptr + 2, method_value);
637 ptr += 6;
638
639 if (UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800640 Append4LE(ptr, thread_clock_diff);
641 ptr += 4;
642 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800643 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800644 Append4LE(ptr, wall_clock_diff);
645 }
646}
647
Ian Rogers62d6c772013-02-27 08:32:07 -0800648void Trace::GetVisitedMethods(size_t buf_size,
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 std::set<mirror::ArtMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800650 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800652
653 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700655 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 visited_methods->insert(method);
657 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800658 }
659}
660
Mathieu Chartier02e25112013-08-14 16:14:24 -0700661void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 MethodHelper mh;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700663 for (const auto& method : visited_methods) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700665 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800666 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700667 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800668 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800669}
670
671static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800672 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
673 std::string name;
674 t->GetThreadName(name);
675 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800676}
677
678void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700679 Thread* self = Thread::Current();
680 Locks::thread_list_lock_->AssertNotHeld(self);
681 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800682 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800683}
684
jeffhaoe343b762011-12-05 16:36:44 -0800685} // namespace art