blob: 5375dc0d3cfbe94c9d4b96496c02086718f70dcc [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>
John Reck0624a272015-03-26 15:47:54 -070020#include <unistd.h>
jeffhaoa9ef3fd2011-12-13 18:33:43 -080021
Ian Rogerscf7f1912014-10-22 22:06:39 -070022#define ATRACE_TAG ATRACE_TAG_DALVIK
23#include "cutils/trace.h"
24
Jeff Hao0abc72e2013-08-13 13:45:14 -070025#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080027#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080028#include "common_throws.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080029#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
jeffhao725a9572012-11-13 18:20:12 -080031#include "instrumentation.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/dex_cache.h"
35#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070036#include "mirror/object-inl.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080037#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038#include "scoped_thread_state_change.h"
Jeff Hao0abc72e2013-08-13 13:45:14 -070039#include "ScopedLocalRef.h"
jeffhaoe343b762011-12-05 16:36:44 -080040#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070041#include "thread_list.h"
Ian Rogers166db042013-07-26 12:05:57 -070042#include "entrypoints/quick/quick_entrypoints.h"
jeffhao2692b572011-12-16 15:42:28 -080043
44namespace art {
45
Elliott Hughese119a362012-05-22 17:37:06 -070046// File format:
47// header
48// record 0
49// record 1
50// ...
51//
52// Header format:
53// u4 magic ('SLOW')
54// u2 version
55// u2 offset to data
56// u8 start date/time in usec
57// u2 record size in bytes (version >= 2 only)
58// ... padding to 32 bytes
59//
60// Record format v1:
61// u1 thread ID
62// u4 method ID | method action
63// u4 time delta since start, in usec
64//
65// Record format v2:
66// u2 thread ID
67// u4 method ID | method action
68// u4 time delta since start, in usec
69//
70// Record format v3:
71// u2 thread ID
72// u4 method ID | method action
73// u4 time delta since start, in usec
74// u4 wall time since start, in usec (when clock == "dual" only)
75//
76// 32 bits of microseconds is 70 minutes.
77//
78// All values are stored in little-endian order.
79
Ian Rogers62d6c772013-02-27 08:32:07 -080080enum TraceAction {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081 kTraceMethodEnter = 0x00, // method entry
82 kTraceMethodExit = 0x01, // method exit
83 kTraceUnroll = 0x02, // method exited by exception unrolling
Ian Rogers62d6c772013-02-27 08:32:07 -080084 // 0x03 currently unused
Brian Carlstrom7934ac22013-07-26 10:54:15 -070085 kTraceMethodActionMask = 0x03, // two bits
Ian Rogers62d6c772013-02-27 08:32:07 -080086};
87
Jeff Hao0abc72e2013-08-13 13:45:14 -070088class BuildStackTraceVisitor : public StackVisitor {
89 public:
90 explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL),
Jeff Hao5ce4b172013-08-16 16:27:18 -070091 method_trace_(Trace::AllocStackTrace()) {}
Jeff Hao0abc72e2013-08-13 13:45:14 -070092
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerse2f77e72013-08-13 19:19:40 -070094 mirror::ArtMethod* m = GetMethod();
Jeff Hao0abc72e2013-08-13 13:45:14 -070095 // Ignore runtime frames (in particular callee save).
96 if (!m->IsRuntimeMethod()) {
97 method_trace_->push_back(m);
98 }
99 return true;
100 }
101
102 // Returns a stack trace where the topmost frame corresponds with the first element of the vector.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700103 std::vector<mirror::ArtMethod*>* GetStackTrace() const {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700104 return method_trace_;
105 }
106
107 private:
Ian Rogerse2f77e72013-08-13 19:19:40 -0700108 std::vector<mirror::ArtMethod*>* const method_trace_;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700109};
110
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800111static const char kTraceTokenChar = '*';
112static const uint16_t kTraceHeaderLength = 32;
113static const uint32_t kTraceMagicValue = 0x574f4c53;
114static const uint16_t kTraceVersionSingleClock = 2;
115static const uint16_t kTraceVersionDualClock = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700116static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
117static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800118
Ian Rogerse63db272014-07-15 15:36:11 -0700119TraceClockSource Trace::default_clock_source_ = kDefaultTraceClockSource;
Elliott Hughese119a362012-05-22 17:37:06 -0700120
Jeff Hao0abc72e2013-08-13 13:45:14 -0700121Trace* volatile Trace::the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700122pthread_t Trace::sampling_pthread_ = 0U;
Ian Rogers700a4022014-05-19 16:49:03 -0700123std::unique_ptr<std::vector<mirror::ArtMethod*>> Trace::temp_stack_trace_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800124
Brian Carlstromea46f952013-07-30 01:26:50 -0700125static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
126 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800127}
Elliott Hughese119a362012-05-22 17:37:06 -0700128
Ian Rogers62d6c772013-02-27 08:32:07 -0800129static TraceAction DecodeTraceAction(uint32_t tmid) {
130 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
131}
132
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133static uint32_t EncodeTraceMethodAndAction(mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 TraceAction action) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 uint32_t tmid = PointerToLowMemUInt32(method) | action;
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
137 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800138}
139
Jeff Hao5ce4b172013-08-16 16:27:18 -0700140std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
141 if (temp_stack_trace_.get() != NULL) {
142 return temp_stack_trace_.release();
143 } else {
144 return new std::vector<mirror::ArtMethod*>();
145 }
146}
147
148void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
149 stack_trace->clear();
150 temp_stack_trace_.reset(stack_trace);
151}
152
Ian Rogerse63db272014-07-15 15:36:11 -0700153void Trace::SetDefaultClockSource(TraceClockSource clock_source) {
Elliott Hughes0a18df82015-01-09 15:16:16 -0800154#if defined(__linux__)
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 default_clock_source_ = clock_source;
156#else
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800157 if (clock_source != TraceClockSource::kWall) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700158 LOG(WARNING) << "Ignoring tracing request to use CPU time.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 }
160#endif
161}
162
Ian Rogerse63db272014-07-15 15:36:11 -0700163static uint16_t GetTraceVersion(TraceClockSource clock_source) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800164 return (clock_source == TraceClockSource::kDual) ? kTraceVersionDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 : kTraceVersionSingleClock;
166}
167
Ian Rogerse63db272014-07-15 15:36:11 -0700168static uint16_t GetRecordSize(TraceClockSource clock_source) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800169 return (clock_source == TraceClockSource::kDual) ? kTraceRecordSizeDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800171}
172
Elliott Hughese119a362012-05-22 17:37:06 -0700173bool Trace::UseThreadCpuClock() {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800174 return (clock_source_ == TraceClockSource::kThreadCpu) ||
175 (clock_source_ == TraceClockSource::kDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800176}
177
Elliott Hughese119a362012-05-22 17:37:06 -0700178bool Trace::UseWallClock() {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800179 return (clock_source_ == TraceClockSource::kWall) ||
180 (clock_source_ == TraceClockSource::kDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700181}
182
Jeff Haoc5d824a2014-07-28 18:35:38 -0700183void Trace::MeasureClockOverhead() {
184 if (UseThreadCpuClock()) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700185 Thread::Current()->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800186 }
Jeff Haoc5d824a2014-07-28 18:35:38 -0700187 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800188 MicroTime();
189 }
190}
191
Jeff Hao5ce4b172013-08-16 16:27:18 -0700192// Compute an average time taken to measure clocks.
Jeff Haoc5d824a2014-07-28 18:35:38 -0700193uint32_t Trace::GetClockOverheadNanoSeconds() {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700194 Thread* self = Thread::Current();
195 uint64_t start = self->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800196
197 for (int i = 4000; i > 0; i--) {
Jeff Haoc5d824a2014-07-28 18:35:38 -0700198 MeasureClockOverhead();
199 MeasureClockOverhead();
200 MeasureClockOverhead();
201 MeasureClockOverhead();
202 MeasureClockOverhead();
203 MeasureClockOverhead();
204 MeasureClockOverhead();
205 MeasureClockOverhead();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800206 }
207
Jeff Hao5ce4b172013-08-16 16:27:18 -0700208 uint64_t elapsed_us = self->GetCpuMicroTime() - start;
209 return static_cast<uint32_t>(elapsed_us / 32);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800210}
211
Elliott Hughesffb465f2012-03-01 18:46:05 -0800212// TODO: put this somewhere with the big-endian equivalent used by JDWP.
213static void Append2LE(uint8_t* buf, uint16_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700214 *buf++ = static_cast<uint8_t>(val);
215 *buf++ = static_cast<uint8_t>(val >> 8);
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 Append4LE(uint8_t* buf, uint32_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700220 *buf++ = static_cast<uint8_t>(val);
221 *buf++ = static_cast<uint8_t>(val >> 8);
222 *buf++ = static_cast<uint8_t>(val >> 16);
223 *buf++ = static_cast<uint8_t>(val >> 24);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800224}
225
Elliott Hughesffb465f2012-03-01 18:46:05 -0800226// TODO: put this somewhere with the big-endian equivalent used by JDWP.
227static void Append8LE(uint8_t* buf, uint64_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700228 *buf++ = static_cast<uint8_t>(val);
229 *buf++ = static_cast<uint8_t>(val >> 8);
230 *buf++ = static_cast<uint8_t>(val >> 16);
231 *buf++ = static_cast<uint8_t>(val >> 24);
232 *buf++ = static_cast<uint8_t>(val >> 32);
233 *buf++ = static_cast<uint8_t>(val >> 40);
234 *buf++ = static_cast<uint8_t>(val >> 48);
235 *buf++ = static_cast<uint8_t>(val >> 56);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800236}
237
Jeff Hao0abc72e2013-08-13 13:45:14 -0700238static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
239 BuildStackTraceVisitor build_trace_visitor(thread);
240 build_trace_visitor.WalkStack();
Ian Rogerse2f77e72013-08-13 19:19:40 -0700241 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700242 Trace* the_trace = reinterpret_cast<Trace*>(arg);
243 the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
244}
245
Andreas Gampeca714582015-04-03 19:41:34 -0700246static void ClearThreadStackTraceAndClockBase(Thread* thread, 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(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800301 !runtime->IsAotCompiler()));
Jeff Hao0abc72e2013-08-13 13:45:14 -0700302
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
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700316 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700317 {
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,
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700330 TraceOutputMode output_mode, TraceMode trace_mode, 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
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700341 if (trace_mode == TraceMode::kSampling && interval_us <= 0) {
Jeff Haod063d912014-09-08 09:38:18 -0700342 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;
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700350 if (output_mode != TraceOutputMode::kDDMS) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351 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
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700370 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Jeff Haod063d912014-09-08 09:38:18 -0700371
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;
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700379 the_trace_ = new Trace(trace_file.release(), buffer_size, flags, trace_mode);
380 if (trace_mode == TraceMode::kSampling) {
Jeff Hao23009dc2013-08-22 15:36:42 -0700381 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;
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700404 Runtime* const runtime = Runtime::Current();
405 Trace* the_trace = nullptr;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700406 pthread_t sampling_pthread = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 {
408 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
409 if (the_trace_ == NULL) {
410 LOG(ERROR) << "Trace stop requested, but no trace currently running";
411 } else {
412 the_trace = the_trace_;
413 the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700414 sampling_pthread = sampling_pthread_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 }
jeffhao2692b572011-12-16 15:42:28 -0800416 }
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700417 // Make sure that we join before we delete the trace since we don't want to have
418 // the sampling thread access a stale pointer. This finishes since the sampling thread exits when
419 // the_trace_ is null.
420 if (sampling_pthread != 0U) {
421 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
422 sampling_pthread_ = 0U;
423 }
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700424 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700425 if (the_trace != nullptr) {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700426 stop_alloc_counting = (the_trace->flags_ & kTraceCountAllocs) != 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 the_trace->FinishTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700428
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700429 if (the_trace->trace_mode_ == TraceMode::kSampling) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700430 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700431 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, nullptr);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700432 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100433 runtime->GetInstrumentation()->DisableMethodTracing();
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700434 runtime->GetInstrumentation()->RemoveListener(
435 the_trace, instrumentation::Instrumentation::kMethodEntered |
436 instrumentation::Instrumentation::kMethodExited |
437 instrumentation::Instrumentation::kMethodUnwind);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700438 }
Andreas Gampe4303ba92014-11-06 01:00:46 -0800439 if (the_trace->trace_file_.get() != nullptr) {
440 // Do not try to erase, so flush and close explicitly.
441 if (the_trace->trace_file_->Flush() != 0) {
442 PLOG(ERROR) << "Could not flush trace file.";
443 }
444 if (the_trace->trace_file_->Close() != 0) {
445 PLOG(ERROR) << "Could not close trace file.";
446 }
447 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 delete the_trace;
449 }
450 runtime->GetThreadList()->ResumeAll();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700451 if (stop_alloc_counting) {
452 // Can be racy since SetStatsEnabled is not guarded by any locks.
Mathieu Chartier02e5f162015-03-11 09:54:22 -0700453 runtime->SetStatsEnabled(false);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700454 }
jeffhao2692b572011-12-16 15:42:28 -0800455}
456
jeffhaob5e81852012-03-12 11:15:45 -0700457void Trace::Shutdown() {
Jeff Hao64caa7d2013-08-29 11:18:01 -0700458 if (GetMethodTracingMode() != kTracingInactive) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700460 }
jeffhaob5e81852012-03-12 11:15:45 -0700461}
462
Jeff Hao64caa7d2013-08-29 11:18:01 -0700463TracingMode Trace::GetMethodTracingMode() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800464 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -0700465 if (the_trace_ == NULL) {
466 return kTracingInactive;
Jeff Hao64caa7d2013-08-29 11:18:01 -0700467 } else {
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700468 switch (the_trace_->trace_mode_) {
469 case TraceMode::kSampling:
470 return kSampleProfilingActive;
471 case TraceMode::kMethodTracing:
472 return kMethodTracingActive;
473 }
474 LOG(FATAL) << "Unreachable";
475 UNREACHABLE();
Jeff Hao64caa7d2013-08-29 11:18:01 -0700476 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800477}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700479Trace::Trace(File* trace_file, int buffer_size, int flags, TraceMode trace_mode)
Ian Rogers62d6c772013-02-27 08:32:07 -0800480 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700481 trace_mode_(trace_mode), clock_source_(default_clock_source_),
Jeff Haoc5d824a2014-07-28 18:35:38 -0700482 buffer_size_(buffer_size), start_time_(MicroTime()),
483 clock_overhead_ns_(GetClockOverheadNanoSeconds()), cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800484 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800486 memset(buf_.get(), 0, kTraceHeaderLength);
487 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800489 Append2LE(buf_.get() + 6, kTraceHeaderLength);
490 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 if (trace_version >= kTraceVersionDualClock) {
492 uint16_t record_size = GetRecordSize(clock_source_);
493 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800494 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800495
jeffhao2692b572011-12-16 15:42:28 -0800496 // Update current offset.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700497 cur_offset_.StoreRelaxed(kTraceHeaderLength);
Ian Rogers62d6c772013-02-27 08:32:07 -0800498}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800499
Ian Rogerse63db272014-07-15 15:36:11 -0700500static void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Ian Rogers62d6c772013-02-27 08:32:07 -0800501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
502 uint8_t* ptr = buf + kTraceHeaderLength;
503 uint8_t* end = buf + buf_size;
504
505 while (ptr < end) {
506 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700507 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800508 TraceAction action = DecodeTraceAction(tmid);
509 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
510 ptr += GetRecordSize(clock_source);
511 }
jeffhaoe343b762011-12-05 16:36:44 -0800512}
513
jeffhao2692b572011-12-16 15:42:28 -0800514void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800515 // Compute elapsed time.
516 uint64_t elapsed = MicroTime() - start_time_;
517
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700518 size_t final_offset = cur_offset_.LoadRelaxed();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800519
Brian Carlstromea46f952013-07-30 01:26:50 -0700520 std::set<mirror::ArtMethod*> visited_methods;
Ian Rogers62d6c772013-02-27 08:32:07 -0800521 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800522
523 std::ostringstream os;
524
525 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800526 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800527 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
528 if (UseThreadCpuClock()) {
529 if (UseWallClock()) {
530 os << StringPrintf("clock=dual\n");
531 } else {
532 os << StringPrintf("clock=thread-cpu\n");
533 }
534 } else {
535 os << StringPrintf("clock=wall\n");
536 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 os << StringPrintf("elapsed-time-usec=%" PRIu64 "\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800538 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
539 os << StringPrintf("num-method-calls=%zd\n", num_records);
Jeff Haoc5d824a2014-07-28 18:35:38 -0700540 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800541 os << StringPrintf("vm=art\n");
John Reck0624a272015-03-26 15:47:54 -0700542 os << StringPrintf("pid=%d\n", getpid());
jeffhao0791adc2012-04-04 11:14:32 -0700543 if ((flags_ & kTraceCountAllocs) != 0) {
544 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
545 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
546 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
547 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800548 os << StringPrintf("%cthreads\n", kTraceTokenChar);
549 DumpThreadList(os);
550 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800551 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800552 os << StringPrintf("%cend\n", kTraceTokenChar);
553
554 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800555 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700556 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800557 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
558 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800559 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800560 iov[1].iov_len = final_offset;
561 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800562 const bool kDumpTraceInfo = false;
563 if (kDumpTraceInfo) {
564 LOG(INFO) << "Trace sent:\n" << header;
565 DumpBuf(buf_.get(), final_offset, clock_source_);
566 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800567 } else {
568 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800569 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700570 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
571 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800572 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800573 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800574 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800575}
576
Ian Rogers62d6c772013-02-27 08:32:07 -0800577void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 mirror::ArtMethod* method, uint32_t new_dex_pc) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700579 UNUSED(thread, this_object, method, new_dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 // We're not recorded to listen to this kind of event, so complain.
581 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700582}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800583
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700584void Trace::FieldRead(Thread* thread, mirror::Object* this_object,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200585 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field)
586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700587 UNUSED(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200588 // We're not recorded to listen to this kind of event, so complain.
589 LOG(ERROR) << "Unexpected field read event in tracing " << PrettyMethod(method) << " " << dex_pc;
590}
591
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700592void Trace::FieldWritten(Thread* thread, mirror::Object* this_object,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200593 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
594 const JValue& field_value)
595 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700596 UNUSED(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200597 // We're not recorded to listen to this kind of event, so complain.
598 LOG(ERROR) << "Unexpected field write event in tracing " << PrettyMethod(method) << " " << dex_pc;
599}
600
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700601void Trace::MethodEntered(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
602 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700603 uint32_t thread_clock_diff = 0;
604 uint32_t wall_clock_diff = 0;
605 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
606 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
607 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800608}
609
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700610void Trace::MethodExited(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
611 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED,
612 const JValue& return_value ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700613 uint32_t thread_clock_diff = 0;
614 uint32_t wall_clock_diff = 0;
615 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
616 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
617 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800618}
619
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700620void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
621 mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700622 uint32_t thread_clock_diff = 0;
623 uint32_t wall_clock_diff = 0;
624 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
625 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
626 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800627}
628
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000629void Trace::ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Ian Rogers62d6c772013-02-27 08:32:07 -0800630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000631 UNUSED(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800632 LOG(ERROR) << "Unexpected exception caught event in tracing";
633}
634
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800635void Trace::BackwardBranch(Thread* /*thread*/, mirror::ArtMethod* method,
636 int32_t /*dex_pc_offset*/)
637 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
638 LOG(ERROR) << "Unexpected backward branch event in tracing" << PrettyMethod(method);
639}
640
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700641void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
642 if (UseThreadCpuClock()) {
643 uint64_t clock_base = thread->GetTraceClockBase();
644 if (UNLIKELY(clock_base == 0)) {
645 // First event, record the base time in the map.
646 uint64_t time = thread->GetCpuMicroTime();
647 thread->SetTraceClockBase(time);
648 } else {
649 *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
650 }
651 }
652 if (UseWallClock()) {
653 *wall_clock_diff = MicroTime() - start_time_;
654 }
655}
656
Ian Rogersef7d42f2014-01-06 12:55:46 -0800657void Trace::LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700658 instrumentation::Instrumentation::InstrumentationEvent event,
659 uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800660 // Advance cur_offset_ atomically.
661 int32_t new_offset;
662 int32_t old_offset;
663 do {
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700664 old_offset = cur_offset_.LoadRelaxed();
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800666 if (new_offset > buffer_size_) {
667 overflow_ = true;
668 return;
669 }
Ian Rogers54fb8fd2014-07-09 23:16:06 -0700670 } while (!cur_offset_.CompareExchangeWeakSequentiallyConsistent(old_offset, new_offset));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800671
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 TraceAction action = kTraceMethodEnter;
673 switch (event) {
674 case instrumentation::Instrumentation::kMethodEntered:
675 action = kTraceMethodEnter;
676 break;
677 case instrumentation::Instrumentation::kMethodExited:
678 action = kTraceMethodExit;
679 break;
680 case instrumentation::Instrumentation::kMethodUnwind:
681 action = kTraceUnroll;
682 break;
683 default:
684 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
685 }
686
687 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800688
689 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800690 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800692 Append4LE(ptr + 2, method_value);
693 ptr += 6;
694
695 if (UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800696 Append4LE(ptr, thread_clock_diff);
697 ptr += 4;
698 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800699 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800700 Append4LE(ptr, wall_clock_diff);
701 }
702}
703
Ian Rogers62d6c772013-02-27 08:32:07 -0800704void Trace::GetVisitedMethods(size_t buf_size,
Brian Carlstromea46f952013-07-30 01:26:50 -0700705 std::set<mirror::ArtMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800706 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800708
709 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700711 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 visited_methods->insert(method);
713 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800714 }
715}
716
Mathieu Chartier02e25112013-08-14 16:14:24 -0700717void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700718 for (const auto& method : visited_methods) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700719 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700720 PrettyDescriptor(method->GetDeclaringClassDescriptor()).c_str(), method->GetName(),
721 method->GetSignature().ToString().c_str(), method->GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800722 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800723}
724
725static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800726 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
727 std::string name;
728 t->GetThreadName(name);
729 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800730}
731
732void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700733 Thread* self = Thread::Current();
Jeff Haoe094b872014-10-14 13:12:01 -0700734 for (auto it : exited_threads_) {
735 os << it.first << "\t" << it.second << "\n";
736 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700737 Locks::thread_list_lock_->AssertNotHeld(self);
738 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800739 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800740}
741
Jeff Haoe094b872014-10-14 13:12:01 -0700742void Trace::StoreExitingThreadInfo(Thread* thread) {
743 MutexLock mu(thread, *Locks::trace_lock_);
744 if (the_trace_ != nullptr) {
745 std::string name;
746 thread->GetThreadName(name);
Andreas Gampea1785c52014-11-25 20:40:08 -0800747 // The same thread/tid may be used multiple times. As SafeMap::Put does not allow to override
748 // a previous mapping, use SafeMap::Overwrite.
749 the_trace_->exited_threads_.Overwrite(thread->GetTid(), name);
Jeff Haoe094b872014-10-14 13:12:01 -0700750 }
751}
752
jeffhaoe343b762011-12-05 16:36:44 -0800753} // namespace art