blob: 7b7c3eb4d3b6892c9d6a381f4032d8f9d6eb8c11 [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
Ian Rogers0399dde2012-06-06 17:09:28 -0700197static void TraceRestoreStack(Thread* self, void*) {
198 struct RestoreStackVisitor : public StackVisitor {
199 RestoreStackVisitor(Thread* self) : StackVisitor(self->GetManagedStack(),
200 self->GetTraceStack()), self_(self) {}
201
202 virtual bool VisitFrame() {
203 if (self_->IsTraceStackEmpty()) {
204 return false; // Stop.
jeffhaoe343b762011-12-05 16:36:44 -0800205 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700206 uintptr_t pc = GetReturnPc();
Ian Rogers57b86d42012-03-27 16:05:41 -0700207 if (IsTraceExitPc(pc)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700208 TraceStackFrame trace_frame = self_->PopTraceStackFrame();
209 SetReturnPc(trace_frame.return_pc_);
210 CHECK(GetMethod() == trace_frame.method_);
jeffhaoe343b762011-12-05 16:36:44 -0800211 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700212 return true; // Continue.
jeffhaoe343b762011-12-05 16:36:44 -0800213 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700214
215 Thread* self_;
216 };
217 RestoreStackVisitor visitor(self);
218 visitor.WalkStack();
jeffhaob5e81852012-03-12 11:15:45 -0700219}
jeffhaoe343b762011-12-05 16:36:44 -0800220
jeffhaoe343b762011-12-05 16:36:44 -0800221void Trace::AddSavedCodeToMap(const Method* method, const void* code) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700222 saved_code_map_.Put(method, code);
jeffhaoe343b762011-12-05 16:36:44 -0800223}
224
225void Trace::RemoveSavedCodeFromMap(const Method* method) {
226 saved_code_map_.erase(method);
227}
228
229const void* Trace::GetSavedCodeFromMap(const Method* method) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700230 typedef SafeMap<const Method*, const void*>::const_iterator It; // TODO: C++0x auto
jeffhao2692b572011-12-16 15:42:28 -0800231 It it = saved_code_map_.find(method);
232 if (it == saved_code_map_.end()) {
233 return NULL;
234 } else {
235 return it->second;
236 }
jeffhaoe343b762011-12-05 16:36:44 -0800237}
238
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700239void Trace::SaveAndUpdateCode(Method* method) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700240#if defined(ART_USE_LLVM_COMPILER)
241 UNIMPLEMENTED(FATAL);
242#else
Ian Rogers57b86d42012-03-27 16:05:41 -0700243 void* trace_stub = GetLogTraceEntryPoint();
jeffhaoe343b762011-12-05 16:36:44 -0800244 CHECK(GetSavedCodeFromMap(method) == NULL);
245 AddSavedCodeToMap(method, method->GetCode());
jeffhaob5e81852012-03-12 11:15:45 -0700246 method->SetCode(trace_stub);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700247#endif
jeffhaoe343b762011-12-05 16:36:44 -0800248}
249
250void Trace::ResetSavedCode(Method* method) {
251 CHECK(GetSavedCodeFromMap(method) != NULL);
252 method->SetCode(GetSavedCodeFromMap(method));
253 RemoveSavedCodeFromMap(method);
254}
255
Elliott Hughese119a362012-05-22 17:37:06 -0700256Trace::Trace(File* trace_file, int buffer_size, int flags)
257 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
258 clock_source_(gDefaultTraceClockSource), overflow_(false),
259 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
260}
261
jeffhaoe343b762011-12-05 16:36:44 -0800262void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800263 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800264 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800265 return;
266 }
267
Elliott Hughes34e06962012-04-09 13:55:55 -0700268 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800269 Runtime::Current()->GetThreadList()->SuspendAll(false);
270
jeffhao2692b572011-12-16 15:42:28 -0800271 // Open trace file if not going directly to ddms.
272 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800273 if (!direct_to_ddms) {
274 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800275 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276 } else {
jeffhao2692b572011-12-16 15:42:28 -0800277 trace_file = OS::FileFromFd("tracefile", trace_fd);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800278 }
jeffhao2692b572011-12-16 15:42:28 -0800279 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700280 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800281 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
282 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
283 Runtime::Current()->GetThreadList()->ResumeAll(false);
284 return;
285 }
286 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800287
jeffhao2692b572011-12-16 15:42:28 -0800288 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700289 Trace* tracer(new Trace(trace_file, buffer_size, flags));
290
291 // Enable count of allocs if specified in the flags.
292 if ((flags && kTraceCountAllocs) != 0) {
293 Runtime::Current()->SetStatsEnabled(true);
294 }
295
jeffhao2692b572011-12-16 15:42:28 -0800296 Runtime::Current()->EnableMethodTracing(tracer);
297 tracer->BeginTracing();
298
299 Runtime::Current()->GetThreadList()->ResumeAll(false);
300}
301
302void Trace::Stop() {
303 if (!Runtime::Current()->IsMethodTracingActive()) {
304 LOG(INFO) << "Trace stop requested, but no trace currently running";
305 return;
306 }
307
Elliott Hughes34e06962012-04-09 13:55:55 -0700308 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
jeffhao2692b572011-12-16 15:42:28 -0800309 Runtime::Current()->GetThreadList()->SuspendAll(false);
310
311 Runtime::Current()->GetTracer()->FinishTracing();
312 Runtime::Current()->DisableMethodTracing();
313
314 Runtime::Current()->GetThreadList()->ResumeAll(false);
315}
316
jeffhaob5e81852012-03-12 11:15:45 -0700317void Trace::Shutdown() {
318 if (!Runtime::Current()->IsMethodTracingActive()) {
319 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
320 return;
321 }
322 Runtime::Current()->GetTracer()->FinishTracing();
323 Runtime::Current()->DisableMethodTracing();
324}
325
jeffhao2692b572011-12-16 15:42:28 -0800326void Trace::BeginTracing() {
327 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800328 start_time_ = MicroTime();
329
jeffhao2692b572011-12-16 15:42:28 -0800330 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800331 if (UseThreadCpuClock() && UseWallClock()) {
332 trace_version_ = kTraceVersionDualClock;
333 record_size_ = kTraceRecordSizeDualClock;
334 } else {
335 trace_version_ = kTraceVersionSingleClock;
336 record_size_ = kTraceRecordSizeSingleClock;
337 }
338
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800339 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800340 memset(buf_.get(), 0, kTraceHeaderLength);
341 Append4LE(buf_.get(), kTraceMagicValue);
342 Append2LE(buf_.get() + 4, trace_version_);
343 Append2LE(buf_.get() + 6, kTraceHeaderLength);
344 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800345 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800346 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800347 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800348
jeffhao2692b572011-12-16 15:42:28 -0800349 // Update current offset.
350 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351
352 // Install all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800353 InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800354}
355
jeffhao2692b572011-12-16 15:42:28 -0800356void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800357 // Uninstall all method tracing stubs.
jeffhaoe343b762011-12-05 16:36:44 -0800358 UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800359
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800360 // Compute elapsed time.
361 uint64_t elapsed = MicroTime() - start_time_;
362
363 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700364 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800365
jeffhao0791adc2012-04-04 11:14:32 -0700366 if ((flags_ & kTraceCountAllocs) != 0) {
367 Runtime::Current()->SetStatsEnabled(false);
368 }
369
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800370 GetVisitedMethods(final_offset);
371
372 std::ostringstream os;
373
374 os << StringPrintf("%cversion\n", kTraceTokenChar);
375 os << StringPrintf("%d\n", trace_version_);
376 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
377 if (UseThreadCpuClock()) {
378 if (UseWallClock()) {
379 os << StringPrintf("clock=dual\n");
380 } else {
381 os << StringPrintf("clock=thread-cpu\n");
382 }
383 } else {
384 os << StringPrintf("clock=wall\n");
385 }
386 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800387 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800388 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
389 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700390 if ((flags_ & kTraceCountAllocs) != 0) {
391 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
392 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
393 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
394 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800395 os << StringPrintf("%cthreads\n", kTraceTokenChar);
396 DumpThreadList(os);
397 os << StringPrintf("%cmethods\n", kTraceTokenChar);
398 DumpMethodList(os);
399 os << StringPrintf("%cend\n", kTraceTokenChar);
400
401 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800402 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700403 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800404 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
405 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800406 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800407 iov[1].iov_len = final_offset;
408 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
409 } else {
410 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800411 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700412 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
413 PLOG(ERROR) << detail;
414 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800415 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800416 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800417}
418
419void Trace::LogMethodTraceEvent(Thread* self, const Method* method, Trace::TraceEvent event) {
420 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
421 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700422 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800423 }
424
425 // Advance cur_offset_ atomically.
426 int32_t new_offset;
427 int32_t old_offset;
428 do {
429 old_offset = cur_offset_;
430 new_offset = old_offset + record_size_;
431 if (new_offset > buffer_size_) {
432 overflow_ = true;
433 return;
434 }
435 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
436
437 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
438
439 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800440 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800441 Append2LE(ptr, self->GetTid());
442 Append4LE(ptr + 2, method_value);
443 ptr += 6;
444
445 if (UseThreadCpuClock()) {
446 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
447 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
448 Append4LE(ptr, thread_clock_diff);
449 ptr += 4;
450 }
451
452 if (UseWallClock()) {
453 uint32_t wall_clock_diff = MicroTime() - start_time_;
454 Append4LE(ptr, wall_clock_diff);
455 }
456}
457
458void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800459 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
460 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800461
462 while (ptr < end) {
463 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
464 Method* method = reinterpret_cast<Method*>(TraceMethodId(method_value));
465 visited_methods_.insert(method);
466 ptr += record_size_;
467 }
468}
469
470void Trace::DumpMethodList(std::ostream& os) {
471 typedef std::set<const Method*>::const_iterator It; // TODO: C++0x auto
472 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
473 const Method* method = *it;
474 MethodHelper mh(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700475 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800476 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700477 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800479}
480
481static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800482 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
483 std::string name;
484 t->GetThreadName(name);
485 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800486}
487
488void Trace::DumpThreadList(std::ostream& os) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800489 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800490}
491
492void Trace::InstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700493 Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800494}
495
496void Trace::UninstallStubs() {
jeffhaob5e81852012-03-12 11:15:45 -0700497 Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL);
Elliott Hughesf8349362012-06-18 15:00:06 -0700498 Runtime::Current()->GetThreadList()->ForEach(TraceRestoreStack, NULL);
jeffhaoe343b762011-12-05 16:36:44 -0800499}
500
Ian Rogers57b86d42012-03-27 16:05:41 -0700501uint32_t TraceMethodUnwindFromCode(Thread* self) {
502 Trace* tracer = Runtime::Current()->GetTracer();
503 TraceStackFrame trace_frame = self->PopTraceStackFrame();
504 Method* method = trace_frame.method_;
505 uint32_t lr = trace_frame.return_pc_;
506
507 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
508
509 return lr;
510}
511
jeffhaoe343b762011-12-05 16:36:44 -0800512} // namespace art