blob: a453e3790bc09cc94325a3975dc06dd87c5a117a [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"
Elliott Hughes88c5c352012-03-15 18:49:48 -070029#include "scoped_thread_list_lock.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
jeffhaob5e81852012-03-12 11:15:45 -0700161static bool InstallStubsClassVisitor(Class* klass, void*) {
jeffhao2692b572011-12-16 15:42:28 -0800162 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800163 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
164 Method* method = klass->GetDirectMethod(i);
jeffhaob5e81852012-03-12 11:15:45 -0700165 if (tracer->GetSavedCodeFromMap(method) == NULL) {
166 tracer->SaveAndUpdateCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800167 }
168 }
169
170 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
171 Method* method = klass->GetVirtualMethod(i);
jeffhaob5e81852012-03-12 11:15:45 -0700172 if (tracer->GetSavedCodeFromMap(method) == NULL) {
173 tracer->SaveAndUpdateCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800174 }
175 }
jeffhaoe343b762011-12-05 16:36:44 -0800176 return true;
177}
178
jeffhaob5e81852012-03-12 11:15:45 -0700179static bool UninstallStubsClassVisitor(Class* klass, void*) {
jeffhao2692b572011-12-16 15:42:28 -0800180 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800181 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
182 Method* method = klass->GetDirectMethod(i);
jeffhao2692b572011-12-16 15:42:28 -0800183 if (tracer->GetSavedCodeFromMap(method) != NULL) {
184 tracer->ResetSavedCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800185 }
186 }
187
188 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
189 Method* method = klass->GetVirtualMethod(i);
jeffhao2692b572011-12-16 15:42:28 -0800190 if (tracer->GetSavedCodeFromMap(method) != NULL) {
191 tracer->ResetSavedCode(method);
jeffhaoe343b762011-12-05 16:36:44 -0800192 }
193 }
jeffhaoe343b762011-12-05 16:36:44 -0800194 return true;
195}
196
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700197static void TraceRestoreStack(Thread* t, void*) {
jeffhaoe343b762011-12-05 16:36:44 -0800198 Frame frame = t->GetTopOfStack();
199 if (frame.GetSP() != 0) {
200 for ( ; frame.GetMethod() != 0; frame.Next()) {
201 if (t->IsTraceStackEmpty()) {
202 break;
203 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700204#if defined(ART_USE_LLVM_COMPILER)
205 UNIMPLEMENTED(FATAL);
206#else
jeffhaoe343b762011-12-05 16:36:44 -0800207 uintptr_t pc = frame.GetReturnPC();
208 Method* method = frame.GetMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700209 if (IsTraceExitPc(pc)) {
jeffhaoe343b762011-12-05 16:36:44 -0800210 TraceStackFrame trace_frame = t->PopTraceStackFrame();
211 frame.SetReturnPC(trace_frame.return_pc_);
212 CHECK(method == trace_frame.method_);
213 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700214#endif
jeffhaoe343b762011-12-05 16:36:44 -0800215 }
216 }
jeffhaob5e81852012-03-12 11:15:45 -0700217}
jeffhaoe343b762011-12-05 16:36:44 -0800218
jeffhaoe343b762011-12-05 16:36:44 -0800219void Trace::AddSavedCodeToMap(const Method* method, const void* code) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700220 saved_code_map_.Put(method, code);
jeffhaoe343b762011-12-05 16:36:44 -0800221}
222
223void Trace::RemoveSavedCodeFromMap(const Method* method) {
224 saved_code_map_.erase(method);
225}
226
227const void* Trace::GetSavedCodeFromMap(const Method* method) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700228 typedef SafeMap<const Method*, const void*>::const_iterator It; // TODO: C++0x auto
jeffhao2692b572011-12-16 15:42:28 -0800229 It it = saved_code_map_.find(method);
230 if (it == saved_code_map_.end()) {
231 return NULL;
232 } else {
233 return it->second;
234 }
jeffhaoe343b762011-12-05 16:36:44 -0800235}
236
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700237void Trace::SaveAndUpdateCode(Method* method) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700238#if defined(ART_USE_LLVM_COMPILER)
239 UNIMPLEMENTED(FATAL);
240#else
Ian Rogers57b86d42012-03-27 16:05:41 -0700241 void* trace_stub = GetLogTraceEntryPoint();
jeffhaoe343b762011-12-05 16:36:44 -0800242 CHECK(GetSavedCodeFromMap(method) == NULL);
243 AddSavedCodeToMap(method, method->GetCode());
jeffhaob5e81852012-03-12 11:15:45 -0700244 method->SetCode(trace_stub);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700245#endif
jeffhaoe343b762011-12-05 16:36:44 -0800246}
247
248void Trace::ResetSavedCode(Method* method) {
249 CHECK(GetSavedCodeFromMap(method) != NULL);
250 method->SetCode(GetSavedCodeFromMap(method));
251 RemoveSavedCodeFromMap(method);
252}
253
Elliott Hughese119a362012-05-22 17:37:06 -0700254Trace::Trace(File* trace_file, int buffer_size, int flags)
255 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
256 clock_source_(gDefaultTraceClockSource), overflow_(false),
257 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
258}
259
jeffhaoe343b762011-12-05 16:36:44 -0800260void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800261 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800262 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800263 return;
264 }
265
Elliott Hughes34e06962012-04-09 13:55:55 -0700266 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800267 Runtime::Current()->GetThreadList()->SuspendAll(false);
268
jeffhao2692b572011-12-16 15:42:28 -0800269 // Open trace file if not going directly to ddms.
270 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800271 if (!direct_to_ddms) {
272 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800273 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800274 } else {
jeffhao2692b572011-12-16 15:42:28 -0800275 trace_file = OS::FileFromFd("tracefile", trace_fd);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276 }
jeffhao2692b572011-12-16 15:42:28 -0800277 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700278 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800279 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
280 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
281 Runtime::Current()->GetThreadList()->ResumeAll(false);
282 return;
283 }
284 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800285
jeffhao2692b572011-12-16 15:42:28 -0800286 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700287 Trace* tracer(new Trace(trace_file, buffer_size, flags));
288
289 // Enable count of allocs if specified in the flags.
290 if ((flags && kTraceCountAllocs) != 0) {
291 Runtime::Current()->SetStatsEnabled(true);
292 }
293
jeffhao2692b572011-12-16 15:42:28 -0800294 Runtime::Current()->EnableMethodTracing(tracer);
295 tracer->BeginTracing();
296
297 Runtime::Current()->GetThreadList()->ResumeAll(false);
298}
299
300void Trace::Stop() {
301 if (!Runtime::Current()->IsMethodTracingActive()) {
302 LOG(INFO) << "Trace stop requested, but no trace currently running";
303 return;
304 }
305
Elliott Hughes34e06962012-04-09 13:55:55 -0700306 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
jeffhao2692b572011-12-16 15:42:28 -0800307 Runtime::Current()->GetThreadList()->SuspendAll(false);
308
309 Runtime::Current()->GetTracer()->FinishTracing();
310 Runtime::Current()->DisableMethodTracing();
311
312 Runtime::Current()->GetThreadList()->ResumeAll(false);
313}
314
jeffhaob5e81852012-03-12 11:15:45 -0700315void Trace::Shutdown() {
316 if (!Runtime::Current()->IsMethodTracingActive()) {
317 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
318 return;
319 }
320 Runtime::Current()->GetTracer()->FinishTracing();
321 Runtime::Current()->DisableMethodTracing();
322}
323
jeffhao2692b572011-12-16 15:42:28 -0800324void Trace::BeginTracing() {
325 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800326 start_time_ = MicroTime();
327
jeffhao2692b572011-12-16 15:42:28 -0800328 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800329 if (UseThreadCpuClock() && UseWallClock()) {
330 trace_version_ = kTraceVersionDualClock;
331 record_size_ = kTraceRecordSizeDualClock;
332 } else {
333 trace_version_ = kTraceVersionSingleClock;
334 record_size_ = kTraceRecordSizeSingleClock;
335 }
336
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800337 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800338 memset(buf_.get(), 0, kTraceHeaderLength);
339 Append4LE(buf_.get(), kTraceMagicValue);
340 Append2LE(buf_.get() + 4, trace_version_);
341 Append2LE(buf_.get() + 6, kTraceHeaderLength);
342 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800343 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800344 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800345 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800346
jeffhao2692b572011-12-16 15:42:28 -0800347 // Update current offset.
348 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800349
350 // Install all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800351 InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800352}
353
jeffhao2692b572011-12-16 15:42:28 -0800354void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800355 // Uninstall all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800356 UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800357
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800358 // Compute elapsed time.
359 uint64_t elapsed = MicroTime() - start_time_;
360
361 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700362 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800363
jeffhao0791adc2012-04-04 11:14:32 -0700364 if ((flags_ & kTraceCountAllocs) != 0) {
365 Runtime::Current()->SetStatsEnabled(false);
366 }
367
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800368 GetVisitedMethods(final_offset);
369
370 std::ostringstream os;
371
372 os << StringPrintf("%cversion\n", kTraceTokenChar);
373 os << StringPrintf("%d\n", trace_version_);
374 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
375 if (UseThreadCpuClock()) {
376 if (UseWallClock()) {
377 os << StringPrintf("clock=dual\n");
378 } else {
379 os << StringPrintf("clock=thread-cpu\n");
380 }
381 } else {
382 os << StringPrintf("clock=wall\n");
383 }
384 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800385 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800386 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
387 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700388 if ((flags_ & kTraceCountAllocs) != 0) {
389 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
390 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
391 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
392 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800393 os << StringPrintf("%cthreads\n", kTraceTokenChar);
394 DumpThreadList(os);
395 os << StringPrintf("%cmethods\n", kTraceTokenChar);
396 DumpMethodList(os);
397 os << StringPrintf("%cend\n", kTraceTokenChar);
398
399 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800400 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700401 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800402 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
403 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800404 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800405 iov[1].iov_len = final_offset;
406 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
407 } else {
408 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800409 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700410 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
411 PLOG(ERROR) << detail;
412 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800413 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800414 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800415}
416
417void Trace::LogMethodTraceEvent(Thread* self, const Method* method, Trace::TraceEvent event) {
418 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
419 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700420 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800421 }
422
423 // Advance cur_offset_ atomically.
424 int32_t new_offset;
425 int32_t old_offset;
426 do {
427 old_offset = cur_offset_;
428 new_offset = old_offset + record_size_;
429 if (new_offset > buffer_size_) {
430 overflow_ = true;
431 return;
432 }
433 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
434
435 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
436
437 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800438 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800439 Append2LE(ptr, self->GetTid());
440 Append4LE(ptr + 2, method_value);
441 ptr += 6;
442
443 if (UseThreadCpuClock()) {
444 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
445 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
446 Append4LE(ptr, thread_clock_diff);
447 ptr += 4;
448 }
449
450 if (UseWallClock()) {
451 uint32_t wall_clock_diff = MicroTime() - start_time_;
452 Append4LE(ptr, wall_clock_diff);
453 }
454}
455
456void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800457 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
458 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800459
460 while (ptr < end) {
461 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
462 Method* method = reinterpret_cast<Method*>(TraceMethodId(method_value));
463 visited_methods_.insert(method);
464 ptr += record_size_;
465 }
466}
467
468void Trace::DumpMethodList(std::ostream& os) {
469 typedef std::set<const Method*>::const_iterator It; // TODO: C++0x auto
470 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
471 const Method* method = *it;
472 MethodHelper mh(method);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800473 os << StringPrintf("%p\t%s\t%s\t%s\t%s\t%d\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800474 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
475 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile(),
476 mh.GetLineNumFromNativePC(0));
477 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478}
479
480static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800481 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
482 std::string name;
483 t->GetThreadName(name);
484 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800485}
486
487void Trace::DumpThreadList(std::ostream& os) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800488 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800489}
490
491void Trace::InstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700492 Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800493}
494
495void Trace::UninstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700496 Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700497 Runtime::Current()->GetThreadList()->ForEach(TraceRestoreStack, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800498}
499
Ian Rogers57b86d42012-03-27 16:05:41 -0700500uint32_t TraceMethodUnwindFromCode(Thread* self) {
501 Trace* tracer = Runtime::Current()->GetTracer();
502 TraceStackFrame trace_frame = self->PopTraceStackFrame();
503 Method* method = trace_frame.method_;
504 uint32_t lr = trace_frame.return_pc_;
505
506 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
507
508 return lr;
509}
510
jeffhaoe343b762011-12-05 16:36:44 -0800511} // namespace art