blob: 753b80faec83c0a6ffe94520b3f04087f994dae5 [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
jeffhaoe343b762011-12-05 16:36:44 -080021#include "class_linker.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080022#include "debugger.h"
jeffhaoe343b762011-12-05 16:36:44 -080023#include "dex_cache.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070024#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "oat/runtime/oat_support_entrypoints.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070026#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080027#include "object_utils.h"
28#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "scoped_thread_state_change.h"
jeffhaoe343b762011-12-05 16:36:44 -080030#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070031#include "thread_list.h"
jeffhao2692b572011-12-16 15:42:28 -080032
33namespace art {
34
Elliott Hughese119a362012-05-22 17:37:06 -070035// File format:
36// header
37// record 0
38// record 1
39// ...
40//
41// Header format:
42// u4 magic ('SLOW')
43// u2 version
44// u2 offset to data
45// u8 start date/time in usec
46// u2 record size in bytes (version >= 2 only)
47// ... padding to 32 bytes
48//
49// Record format v1:
50// u1 thread ID
51// u4 method ID | method action
52// u4 time delta since start, in usec
53//
54// Record format v2:
55// u2 thread ID
56// u4 method ID | method action
57// u4 time delta since start, in usec
58//
59// Record format v3:
60// u2 thread ID
61// u4 method ID | method action
62// u4 time delta since start, in usec
63// u4 wall time since start, in usec (when clock == "dual" only)
64//
65// 32 bits of microseconds is 70 minutes.
66//
67// All values are stored in little-endian order.
68
jeffhaoa9ef3fd2011-12-13 18:33:43 -080069static const uint32_t kTraceMethodActionMask = 0x03; // two bits
70static const char kTraceTokenChar = '*';
71static const uint16_t kTraceHeaderLength = 32;
72static const uint32_t kTraceMagicValue = 0x574f4c53;
73static const uint16_t kTraceVersionSingleClock = 2;
74static const uint16_t kTraceVersionDualClock = 3;
75static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
76static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
77
Elliott Hughese119a362012-05-22 17:37:06 -070078static ProfilerClockSource gDefaultTraceClockSource = kProfilerClockSourceDual;
79
jeffhaoa9ef3fd2011-12-13 18:33:43 -080080static inline uint32_t TraceMethodId(uint32_t methodValue) {
81 return (methodValue & ~kTraceMethodActionMask);
82}
Elliott Hughese119a362012-05-22 17:37:06 -070083
jeffhaoa9ef3fd2011-12-13 18:33:43 -080084static inline uint32_t TraceMethodCombine(uint32_t method, uint8_t traceEvent) {
85 return (method | traceEvent);
86}
87
Elliott Hughese119a362012-05-22 17:37:06 -070088void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
89 gDefaultTraceClockSource = clock_source;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080090}
91
Elliott Hughese119a362012-05-22 17:37:06 -070092bool Trace::UseThreadCpuClock() {
93#if defined(HAVE_POSIX_CLOCKS)
94 return clock_source_ != kProfilerClockSourceWall;
95#else
96 return false;
97#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080098}
99
Elliott Hughese119a362012-05-22 17:37:06 -0700100bool Trace::UseWallClock() {
101#if defined(HAVE_POSIX_CLOCKS)
102 return clock_source_ != kProfilerClockSourceThreadCpu;
103#else
104 return true;
105#endif
106}
107
108static void MeasureClockOverhead(Trace* trace) {
109 if (trace->UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800110 ThreadCpuMicroTime();
111 }
Elliott Hughese119a362012-05-22 17:37:06 -0700112 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800113 MicroTime();
114 }
115}
116
Elliott Hughese119a362012-05-22 17:37:06 -0700117static uint32_t GetClockOverhead(Trace* trace) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800118 uint64_t start = ThreadCpuMicroTime();
119
120 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700121 MeasureClockOverhead(trace);
122 MeasureClockOverhead(trace);
123 MeasureClockOverhead(trace);
124 MeasureClockOverhead(trace);
125 MeasureClockOverhead(trace);
126 MeasureClockOverhead(trace);
127 MeasureClockOverhead(trace);
128 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800129 }
130
131 uint64_t elapsed = ThreadCpuMicroTime() - start;
132 return uint32_t (elapsed / 32);
133}
134
Elliott Hughesffb465f2012-03-01 18:46:05 -0800135// TODO: put this somewhere with the big-endian equivalent used by JDWP.
136static void Append2LE(uint8_t* buf, uint16_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800137 *buf++ = (uint8_t) val;
138 *buf++ = (uint8_t) (val >> 8);
139}
140
Elliott Hughesffb465f2012-03-01 18:46:05 -0800141// TODO: put this somewhere with the big-endian equivalent used by JDWP.
142static void Append4LE(uint8_t* buf, uint32_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800143 *buf++ = (uint8_t) val;
144 *buf++ = (uint8_t) (val >> 8);
145 *buf++ = (uint8_t) (val >> 16);
146 *buf++ = (uint8_t) (val >> 24);
147}
148
Elliott Hughesffb465f2012-03-01 18:46:05 -0800149// TODO: put this somewhere with the big-endian equivalent used by JDWP.
150static void Append8LE(uint8_t* buf, uint64_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800151 *buf++ = (uint8_t) val;
152 *buf++ = (uint8_t) (val >> 8);
153 *buf++ = (uint8_t) (val >> 16);
154 *buf++ = (uint8_t) (val >> 24);
155 *buf++ = (uint8_t) (val >> 32);
156 *buf++ = (uint8_t) (val >> 40);
157 *buf++ = (uint8_t) (val >> 48);
158 *buf++ = (uint8_t) (val >> 56);
159}
160
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161static bool InstallStubsClassVisitor(Class* klass, void*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao2692b572011-12-16 15:42:28 -0800163 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800164 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700165 AbstractMethod* method = klass->GetDirectMethod(i);
jeffhaob5e81852012-03-12 11:15:45 -0700166 if (tracer->GetSavedCodeFromMap(method) == NULL) {
167 tracer->SaveAndUpdateCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800168 }
169 }
170
171 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 AbstractMethod* method = klass->GetVirtualMethod(i);
jeffhaob5e81852012-03-12 11:15:45 -0700173 if (tracer->GetSavedCodeFromMap(method) == NULL) {
174 tracer->SaveAndUpdateCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800175 }
176 }
jeffhaoe343b762011-12-05 16:36:44 -0800177 return true;
178}
179
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180static bool UninstallStubsClassVisitor(Class* klass, void*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao2692b572011-12-16 15:42:28 -0800182 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800183 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700184 AbstractMethod* method = klass->GetDirectMethod(i);
jeffhao2692b572011-12-16 15:42:28 -0800185 if (tracer->GetSavedCodeFromMap(method) != NULL) {
186 tracer->ResetSavedCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800187 }
188 }
189
190 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700191 AbstractMethod* method = klass->GetVirtualMethod(i);
jeffhao2692b572011-12-16 15:42:28 -0800192 if (tracer->GetSavedCodeFromMap(method) != NULL) {
193 tracer->ResetSavedCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800194 }
195 }
jeffhaoe343b762011-12-05 16:36:44 -0800196 return true;
197}
198
Ian Rogers0399dde2012-06-06 17:09:28 -0700199static void TraceRestoreStack(Thread* self, void*) {
200 struct RestoreStackVisitor : public StackVisitor {
Ian Rogersca190662012-06-26 15:45:57 -0700201 RestoreStackVisitor(Thread* self)
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700202 : StackVisitor(self->GetManagedStack(), self->GetTraceStack(), NULL), self_(self) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700203
204 virtual bool VisitFrame() {
205 if (self_->IsTraceStackEmpty()) {
206 return false; // Stop.
jeffhaoe343b762011-12-05 16:36:44 -0800207 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700208 uintptr_t pc = GetReturnPc();
Ian Rogers57b86d42012-03-27 16:05:41 -0700209 if (IsTraceExitPc(pc)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700210 TraceStackFrame trace_frame = self_->PopTraceStackFrame();
211 SetReturnPc(trace_frame.return_pc_);
212 CHECK(GetMethod() == trace_frame.method_);
jeffhaoe343b762011-12-05 16:36:44 -0800213 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700214 return true; // Continue.
jeffhaoe343b762011-12-05 16:36:44 -0800215 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700216
217 Thread* self_;
218 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 ScopedObjectAccess soa(self);
Ian Rogers0399dde2012-06-06 17:09:28 -0700220 RestoreStackVisitor visitor(self);
221 visitor.WalkStack();
jeffhaob5e81852012-03-12 11:15:45 -0700222}
jeffhaoe343b762011-12-05 16:36:44 -0800223
Mathieu Chartier66f19252012-09-18 08:57:04 -0700224void Trace::AddSavedCodeToMap(const AbstractMethod* method, const void* code) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700225 saved_code_map_.Put(method, code);
jeffhaoe343b762011-12-05 16:36:44 -0800226}
227
Mathieu Chartier66f19252012-09-18 08:57:04 -0700228void Trace::RemoveSavedCodeFromMap(const AbstractMethod* method) {
jeffhaoe343b762011-12-05 16:36:44 -0800229 saved_code_map_.erase(method);
230}
231
Mathieu Chartier66f19252012-09-18 08:57:04 -0700232const void* Trace::GetSavedCodeFromMap(const AbstractMethod* method) {
233 typedef SafeMap<const AbstractMethod*, const void*>::const_iterator It; // TODO: C++0x auto
jeffhao2692b572011-12-16 15:42:28 -0800234 It it = saved_code_map_.find(method);
235 if (it == saved_code_map_.end()) {
236 return NULL;
237 } else {
238 return it->second;
239 }
jeffhaoe343b762011-12-05 16:36:44 -0800240}
241
Mathieu Chartier66f19252012-09-18 08:57:04 -0700242void Trace::SaveAndUpdateCode(AbstractMethod* method) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700243#if defined(ART_USE_LLVM_COMPILER)
buzbeec531cef2012-10-18 07:09:20 -0700244 UNUSED(method);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700245 UNIMPLEMENTED(FATAL);
246#else
Ian Rogers57b86d42012-03-27 16:05:41 -0700247 void* trace_stub = GetLogTraceEntryPoint();
jeffhaoe343b762011-12-05 16:36:44 -0800248 CHECK(GetSavedCodeFromMap(method) == NULL);
249 AddSavedCodeToMap(method, method->GetCode());
jeffhaob5e81852012-03-12 11:15:45 -0700250 method->SetCode(trace_stub);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700251#endif
jeffhaoe343b762011-12-05 16:36:44 -0800252}
253
Mathieu Chartier66f19252012-09-18 08:57:04 -0700254void Trace::ResetSavedCode(AbstractMethod* method) {
jeffhaoe343b762011-12-05 16:36:44 -0800255 CHECK(GetSavedCodeFromMap(method) != NULL);
256 method->SetCode(GetSavedCodeFromMap(method));
257 RemoveSavedCodeFromMap(method);
258}
259
Elliott Hughese119a362012-05-22 17:37:06 -0700260Trace::Trace(File* trace_file, int buffer_size, int flags)
261 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
262 clock_source_(gDefaultTraceClockSource), overflow_(false),
263 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
264}
265
jeffhaoe343b762011-12-05 16:36:44 -0800266void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800267 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800268 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800269 return;
270 }
271
Elliott Hughes34e06962012-04-09 13:55:55 -0700272 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800274
jeffhao2692b572011-12-16 15:42:28 -0800275 // Open trace file if not going directly to ddms.
276 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800277 if (!direct_to_ddms) {
278 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800279 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800280 } else {
jeffhao2692b572011-12-16 15:42:28 -0800281 trace_file = OS::FileFromFd("tracefile", trace_fd);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800282 }
jeffhao2692b572011-12-16 15:42:28 -0800283 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700284 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800285 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
286 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800288 return;
289 }
290 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800291
jeffhao2692b572011-12-16 15:42:28 -0800292 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700293 Trace* tracer(new Trace(trace_file, buffer_size, flags));
294
295 // Enable count of allocs if specified in the flags.
296 if ((flags && kTraceCountAllocs) != 0) {
297 Runtime::Current()->SetStatsEnabled(true);
298 }
299
jeffhao2692b572011-12-16 15:42:28 -0800300 Runtime::Current()->EnableMethodTracing(tracer);
301 tracer->BeginTracing();
302
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800304}
305
306void Trace::Stop() {
307 if (!Runtime::Current()->IsMethodTracingActive()) {
308 LOG(INFO) << "Trace stop requested, but no trace currently running";
309 return;
310 }
311
Elliott Hughes34e06962012-04-09 13:55:55 -0700312 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhao2692b572011-12-16 15:42:28 -0800314
315 Runtime::Current()->GetTracer()->FinishTracing();
316 Runtime::Current()->DisableMethodTracing();
317
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800319}
320
jeffhaob5e81852012-03-12 11:15:45 -0700321void Trace::Shutdown() {
322 if (!Runtime::Current()->IsMethodTracingActive()) {
323 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
324 return;
325 }
326 Runtime::Current()->GetTracer()->FinishTracing();
327 Runtime::Current()->DisableMethodTracing();
328}
329
jeffhao2692b572011-12-16 15:42:28 -0800330void Trace::BeginTracing() {
331 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800332 start_time_ = MicroTime();
333
jeffhao2692b572011-12-16 15:42:28 -0800334 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800335 if (UseThreadCpuClock() && UseWallClock()) {
336 trace_version_ = kTraceVersionDualClock;
337 record_size_ = kTraceRecordSizeDualClock;
338 } else {
339 trace_version_ = kTraceVersionSingleClock;
340 record_size_ = kTraceRecordSizeSingleClock;
341 }
342
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800343 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800344 memset(buf_.get(), 0, kTraceHeaderLength);
345 Append4LE(buf_.get(), kTraceMagicValue);
346 Append2LE(buf_.get() + 4, trace_version_);
347 Append2LE(buf_.get() + 6, kTraceHeaderLength);
348 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800349 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800350 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800352
jeffhao2692b572011-12-16 15:42:28 -0800353 // Update current offset.
354 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800355
356 // Install all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800357 InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800358}
359
jeffhao2692b572011-12-16 15:42:28 -0800360void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800361 // Uninstall all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800362 UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800363
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800364 // Compute elapsed time.
365 uint64_t elapsed = MicroTime() - start_time_;
366
367 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700368 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800369
jeffhao0791adc2012-04-04 11:14:32 -0700370 if ((flags_ & kTraceCountAllocs) != 0) {
371 Runtime::Current()->SetStatsEnabled(false);
372 }
373
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800374 GetVisitedMethods(final_offset);
375
376 std::ostringstream os;
377
378 os << StringPrintf("%cversion\n", kTraceTokenChar);
379 os << StringPrintf("%d\n", trace_version_);
380 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
381 if (UseThreadCpuClock()) {
382 if (UseWallClock()) {
383 os << StringPrintf("clock=dual\n");
384 } else {
385 os << StringPrintf("clock=thread-cpu\n");
386 }
387 } else {
388 os << StringPrintf("clock=wall\n");
389 }
390 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800391 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800392 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
393 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700394 if ((flags_ & kTraceCountAllocs) != 0) {
395 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
396 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
397 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
398 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800399 os << StringPrintf("%cthreads\n", kTraceTokenChar);
400 DumpThreadList(os);
401 os << StringPrintf("%cmethods\n", kTraceTokenChar);
402 DumpMethodList(os);
403 os << StringPrintf("%cend\n", kTraceTokenChar);
404
405 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800406 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700407 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800408 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
409 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800410 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800411 iov[1].iov_len = final_offset;
412 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
413 } else {
414 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800415 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700416 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
417 PLOG(ERROR) << detail;
418 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800419 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800420 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800421}
422
Mathieu Chartier66f19252012-09-18 08:57:04 -0700423void Trace::LogMethodTraceEvent(Thread* self, const AbstractMethod* method, Trace::TraceEvent event) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800424 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
425 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700426 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800427 }
428
429 // Advance cur_offset_ atomically.
430 int32_t new_offset;
431 int32_t old_offset;
432 do {
433 old_offset = cur_offset_;
434 new_offset = old_offset + record_size_;
435 if (new_offset > buffer_size_) {
436 overflow_ = true;
437 return;
438 }
439 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
440
441 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
442
443 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800444 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800445 Append2LE(ptr, self->GetTid());
446 Append4LE(ptr + 2, method_value);
447 ptr += 6;
448
449 if (UseThreadCpuClock()) {
450 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
451 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
452 Append4LE(ptr, thread_clock_diff);
453 ptr += 4;
454 }
455
456 if (UseWallClock()) {
457 uint32_t wall_clock_diff = MicroTime() - start_time_;
458 Append4LE(ptr, wall_clock_diff);
459 }
460}
461
462void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800463 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
464 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800465
466 while (ptr < end) {
467 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468 AbstractMethod* method = reinterpret_cast<AbstractMethod*>(TraceMethodId(method_value));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800469 visited_methods_.insert(method);
470 ptr += record_size_;
471 }
472}
473
474void Trace::DumpMethodList(std::ostream& os) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700475 typedef std::set<const AbstractMethod*>::const_iterator It; // TODO: C++0x auto
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800476 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700477 const AbstractMethod* method = *it;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478 MethodHelper mh(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700479 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800480 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700481 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800482 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800483}
484
485static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800486 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
487 std::string name;
488 t->GetThreadName(name);
489 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800490}
491
492void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700493 Thread* self = Thread::Current();
494 Locks::thread_list_lock_->AssertNotHeld(self);
495 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800496 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800497}
498
499void Trace::InstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700500 Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800501}
502
503void Trace::UninstallStubs() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700504 Thread* self = Thread::Current();
505 Locks::thread_list_lock_->AssertNotHeld(self);
jeffhaob5e81852012-03-12 11:15:45 -0700506 Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL);
Ian Rogers81d425b2012-09-27 16:03:43 -0700507 MutexLock mu(self, *Locks::thread_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700508 Runtime::Current()->GetThreadList()->ForEach(TraceRestoreStack, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800509}
510
Ian Rogers57b86d42012-03-27 16:05:41 -0700511uint32_t TraceMethodUnwindFromCode(Thread* self) {
512 Trace* tracer = Runtime::Current()->GetTracer();
513 TraceStackFrame trace_frame = self->PopTraceStackFrame();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700514 AbstractMethod* method = trace_frame.method_;
Ian Rogers57b86d42012-03-27 16:05:41 -0700515 uint32_t lr = trace_frame.return_pc_;
516
517 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
518
519 return lr;
520}
521
jeffhaoe343b762011-12-05 16:36:44 -0800522} // namespace art