blob: 5ac7e3d6f86acc75fd29fc772f94ae1e4a05632d [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*)
162 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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++) {
165 Method* 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++) {
172 Method* 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*)
181 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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++) {
184 Method* 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++) {
191 Method* 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
jeffhaoe343b762011-12-05 16:36:44 -0800224void Trace::AddSavedCodeToMap(const Method* 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
228void Trace::RemoveSavedCodeFromMap(const Method* method) {
229 saved_code_map_.erase(method);
230}
231
232const void* Trace::GetSavedCodeFromMap(const Method* method) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700233 typedef SafeMap<const Method*, 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
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700242void Trace::SaveAndUpdateCode(Method* method) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700243#if defined(ART_USE_LLVM_COMPILER)
244 UNIMPLEMENTED(FATAL);
245#else
Ian Rogers57b86d42012-03-27 16:05:41 -0700246 void* trace_stub = GetLogTraceEntryPoint();
jeffhaoe343b762011-12-05 16:36:44 -0800247 CHECK(GetSavedCodeFromMap(method) == NULL);
248 AddSavedCodeToMap(method, method->GetCode());
jeffhaob5e81852012-03-12 11:15:45 -0700249 method->SetCode(trace_stub);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700250#endif
jeffhaoe343b762011-12-05 16:36:44 -0800251}
252
253void Trace::ResetSavedCode(Method* method) {
254 CHECK(GetSavedCodeFromMap(method) != NULL);
255 method->SetCode(GetSavedCodeFromMap(method));
256 RemoveSavedCodeFromMap(method);
257}
258
Elliott Hughese119a362012-05-22 17:37:06 -0700259Trace::Trace(File* trace_file, int buffer_size, int flags)
260 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
261 clock_source_(gDefaultTraceClockSource), overflow_(false),
262 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
263}
264
jeffhaoe343b762011-12-05 16:36:44 -0800265void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800266 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800267 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800268 return;
269 }
270
Elliott Hughes34e06962012-04-09 13:55:55 -0700271 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800273
jeffhao2692b572011-12-16 15:42:28 -0800274 // Open trace file if not going directly to ddms.
275 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276 if (!direct_to_ddms) {
277 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800278 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800279 } else {
jeffhao2692b572011-12-16 15:42:28 -0800280 trace_file = OS::FileFromFd("tracefile", trace_fd);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800281 }
jeffhao2692b572011-12-16 15:42:28 -0800282 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700283 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800284 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
285 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800287 return;
288 }
289 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800290
jeffhao2692b572011-12-16 15:42:28 -0800291 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700292 Trace* tracer(new Trace(trace_file, buffer_size, flags));
293
294 // Enable count of allocs if specified in the flags.
295 if ((flags && kTraceCountAllocs) != 0) {
296 Runtime::Current()->SetStatsEnabled(true);
297 }
298
jeffhao2692b572011-12-16 15:42:28 -0800299 Runtime::Current()->EnableMethodTracing(tracer);
300 tracer->BeginTracing();
301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800303}
304
305void Trace::Stop() {
306 if (!Runtime::Current()->IsMethodTracingActive()) {
307 LOG(INFO) << "Trace stop requested, but no trace currently running";
308 return;
309 }
310
Elliott Hughes34e06962012-04-09 13:55:55 -0700311 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhao2692b572011-12-16 15:42:28 -0800313
314 Runtime::Current()->GetTracer()->FinishTracing();
315 Runtime::Current()->DisableMethodTracing();
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800318}
319
jeffhaob5e81852012-03-12 11:15:45 -0700320void Trace::Shutdown() {
321 if (!Runtime::Current()->IsMethodTracingActive()) {
322 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
323 return;
324 }
325 Runtime::Current()->GetTracer()->FinishTracing();
326 Runtime::Current()->DisableMethodTracing();
327}
328
jeffhao2692b572011-12-16 15:42:28 -0800329void Trace::BeginTracing() {
330 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800331 start_time_ = MicroTime();
332
jeffhao2692b572011-12-16 15:42:28 -0800333 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800334 if (UseThreadCpuClock() && UseWallClock()) {
335 trace_version_ = kTraceVersionDualClock;
336 record_size_ = kTraceRecordSizeDualClock;
337 } else {
338 trace_version_ = kTraceVersionSingleClock;
339 record_size_ = kTraceRecordSizeSingleClock;
340 }
341
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800342 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800343 memset(buf_.get(), 0, kTraceHeaderLength);
344 Append4LE(buf_.get(), kTraceMagicValue);
345 Append2LE(buf_.get() + 4, trace_version_);
346 Append2LE(buf_.get() + 6, kTraceHeaderLength);
347 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800348 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800349 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800350 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351
jeffhao2692b572011-12-16 15:42:28 -0800352 // Update current offset.
353 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800354
355 // Install all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800356 InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800357}
358
jeffhao2692b572011-12-16 15:42:28 -0800359void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800360 // Uninstall all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800361 UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800362
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800363 // Compute elapsed time.
364 uint64_t elapsed = MicroTime() - start_time_;
365
366 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700367 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800368
jeffhao0791adc2012-04-04 11:14:32 -0700369 if ((flags_ & kTraceCountAllocs) != 0) {
370 Runtime::Current()->SetStatsEnabled(false);
371 }
372
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800373 GetVisitedMethods(final_offset);
374
375 std::ostringstream os;
376
377 os << StringPrintf("%cversion\n", kTraceTokenChar);
378 os << StringPrintf("%d\n", trace_version_);
379 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
380 if (UseThreadCpuClock()) {
381 if (UseWallClock()) {
382 os << StringPrintf("clock=dual\n");
383 } else {
384 os << StringPrintf("clock=thread-cpu\n");
385 }
386 } else {
387 os << StringPrintf("clock=wall\n");
388 }
389 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800390 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800391 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
392 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700393 if ((flags_ & kTraceCountAllocs) != 0) {
394 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
395 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
396 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
397 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800398 os << StringPrintf("%cthreads\n", kTraceTokenChar);
399 DumpThreadList(os);
400 os << StringPrintf("%cmethods\n", kTraceTokenChar);
401 DumpMethodList(os);
402 os << StringPrintf("%cend\n", kTraceTokenChar);
403
404 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800405 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700406 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800407 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
408 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800409 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800410 iov[1].iov_len = final_offset;
411 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
412 } else {
413 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800414 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700415 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
416 PLOG(ERROR) << detail;
417 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800418 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800419 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800420}
421
422void Trace::LogMethodTraceEvent(Thread* self, const Method* method, Trace::TraceEvent event) {
423 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
424 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700425 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800426 }
427
428 // Advance cur_offset_ atomically.
429 int32_t new_offset;
430 int32_t old_offset;
431 do {
432 old_offset = cur_offset_;
433 new_offset = old_offset + record_size_;
434 if (new_offset > buffer_size_) {
435 overflow_ = true;
436 return;
437 }
438 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
439
440 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
441
442 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800443 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800444 Append2LE(ptr, self->GetTid());
445 Append4LE(ptr + 2, method_value);
446 ptr += 6;
447
448 if (UseThreadCpuClock()) {
449 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
450 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
451 Append4LE(ptr, thread_clock_diff);
452 ptr += 4;
453 }
454
455 if (UseWallClock()) {
456 uint32_t wall_clock_diff = MicroTime() - start_time_;
457 Append4LE(ptr, wall_clock_diff);
458 }
459}
460
461void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800462 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
463 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800464
465 while (ptr < end) {
466 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
467 Method* method = reinterpret_cast<Method*>(TraceMethodId(method_value));
468 visited_methods_.insert(method);
469 ptr += record_size_;
470 }
471}
472
473void Trace::DumpMethodList(std::ostream& os) {
474 typedef std::set<const Method*>::const_iterator It; // TODO: C++0x auto
475 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
476 const Method* method = *it;
477 MethodHelper mh(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700478 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800479 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700480 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800481 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800482}
483
484static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800485 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
486 std::string name;
487 t->GetThreadName(name);
488 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800489}
490
491void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700492 GlobalSynchronization::thread_list_lock_->AssertNotHeld();
493 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800494 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800495}
496
497void Trace::InstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700498 Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800499}
500
501void Trace::UninstallStubs() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 GlobalSynchronization::thread_list_lock_->AssertNotHeld();
jeffhaob5e81852012-03-12 11:15:45 -0700503 Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700505 Runtime::Current()->GetThreadList()->ForEach(TraceRestoreStack, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800506}
507
Ian Rogers57b86d42012-03-27 16:05:41 -0700508uint32_t TraceMethodUnwindFromCode(Thread* self) {
509 Trace* tracer = Runtime::Current()->GetTracer();
510 TraceStackFrame trace_frame = self->PopTraceStackFrame();
511 Method* method = trace_frame.method_;
512 uint32_t lr = trace_frame.return_pc_;
513
514 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
515
516 return lr;
517}
518
jeffhaoe343b762011-12-05 16:36:44 -0800519} // namespace art