blob: f04b1db1231d95a86cde6ef6a6df05b3e0b71201 [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"
jeffhao725a9572012-11-13 18:20:12 -080024#include "instrumentation.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070025#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "oat/runtime/oat_support_entrypoints.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080028#include "object_utils.h"
29#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
jeffhaoe343b762011-12-05 16:36:44 -080031#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070032#include "thread_list.h"
jeffhao2692b572011-12-16 15:42:28 -080033
34namespace art {
35
Elliott Hughese119a362012-05-22 17:37:06 -070036// File format:
37// header
38// record 0
39// record 1
40// ...
41//
42// Header format:
43// u4 magic ('SLOW')
44// u2 version
45// u2 offset to data
46// u8 start date/time in usec
47// u2 record size in bytes (version >= 2 only)
48// ... padding to 32 bytes
49//
50// Record format v1:
51// u1 thread ID
52// u4 method ID | method action
53// u4 time delta since start, in usec
54//
55// Record format v2:
56// u2 thread ID
57// u4 method ID | method action
58// u4 time delta since start, in usec
59//
60// Record format v3:
61// u2 thread ID
62// u4 method ID | method action
63// u4 time delta since start, in usec
64// u4 wall time since start, in usec (when clock == "dual" only)
65//
66// 32 bits of microseconds is 70 minutes.
67//
68// All values are stored in little-endian order.
69
jeffhaoa9ef3fd2011-12-13 18:33:43 -080070static const uint32_t kTraceMethodActionMask = 0x03; // two bits
71static const char kTraceTokenChar = '*';
72static const uint16_t kTraceHeaderLength = 32;
73static const uint32_t kTraceMagicValue = 0x574f4c53;
74static const uint16_t kTraceVersionSingleClock = 2;
75static const uint16_t kTraceVersionDualClock = 3;
76static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
77static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
78
Elliott Hughese119a362012-05-22 17:37:06 -070079static ProfilerClockSource gDefaultTraceClockSource = kProfilerClockSourceDual;
80
jeffhaoa9ef3fd2011-12-13 18:33:43 -080081static inline uint32_t TraceMethodId(uint32_t methodValue) {
82 return (methodValue & ~kTraceMethodActionMask);
83}
Elliott Hughese119a362012-05-22 17:37:06 -070084
jeffhaoa9ef3fd2011-12-13 18:33:43 -080085static inline uint32_t TraceMethodCombine(uint32_t method, uint8_t traceEvent) {
86 return (method | traceEvent);
87}
88
Elliott Hughese119a362012-05-22 17:37:06 -070089void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
90 gDefaultTraceClockSource = clock_source;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080091}
92
Elliott Hughese119a362012-05-22 17:37:06 -070093bool Trace::UseThreadCpuClock() {
94#if defined(HAVE_POSIX_CLOCKS)
95 return clock_source_ != kProfilerClockSourceWall;
96#else
97 return false;
98#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080099}
100
Elliott Hughese119a362012-05-22 17:37:06 -0700101bool Trace::UseWallClock() {
102#if defined(HAVE_POSIX_CLOCKS)
103 return clock_source_ != kProfilerClockSourceThreadCpu;
104#else
105 return true;
106#endif
107}
108
109static void MeasureClockOverhead(Trace* trace) {
110 if (trace->UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800111 ThreadCpuMicroTime();
112 }
Elliott Hughese119a362012-05-22 17:37:06 -0700113 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800114 MicroTime();
115 }
116}
117
Elliott Hughese119a362012-05-22 17:37:06 -0700118static uint32_t GetClockOverhead(Trace* trace) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800119 uint64_t start = ThreadCpuMicroTime();
120
121 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700122 MeasureClockOverhead(trace);
123 MeasureClockOverhead(trace);
124 MeasureClockOverhead(trace);
125 MeasureClockOverhead(trace);
126 MeasureClockOverhead(trace);
127 MeasureClockOverhead(trace);
128 MeasureClockOverhead(trace);
129 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800130 }
131
132 uint64_t elapsed = ThreadCpuMicroTime() - start;
133 return uint32_t (elapsed / 32);
134}
135
Elliott Hughesffb465f2012-03-01 18:46:05 -0800136// TODO: put this somewhere with the big-endian equivalent used by JDWP.
137static void Append2LE(uint8_t* buf, uint16_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800138 *buf++ = (uint8_t) val;
139 *buf++ = (uint8_t) (val >> 8);
140}
141
Elliott Hughesffb465f2012-03-01 18:46:05 -0800142// TODO: put this somewhere with the big-endian equivalent used by JDWP.
143static void Append4LE(uint8_t* buf, uint32_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800144 *buf++ = (uint8_t) val;
145 *buf++ = (uint8_t) (val >> 8);
146 *buf++ = (uint8_t) (val >> 16);
147 *buf++ = (uint8_t) (val >> 24);
148}
149
Elliott Hughesffb465f2012-03-01 18:46:05 -0800150// TODO: put this somewhere with the big-endian equivalent used by JDWP.
151static void Append8LE(uint8_t* buf, uint64_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800152 *buf++ = (uint8_t) val;
153 *buf++ = (uint8_t) (val >> 8);
154 *buf++ = (uint8_t) (val >> 16);
155 *buf++ = (uint8_t) (val >> 24);
156 *buf++ = (uint8_t) (val >> 32);
157 *buf++ = (uint8_t) (val >> 40);
158 *buf++ = (uint8_t) (val >> 48);
159 *buf++ = (uint8_t) (val >> 56);
160}
161
Elliott Hughese119a362012-05-22 17:37:06 -0700162Trace::Trace(File* trace_file, int buffer_size, int flags)
163 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
164 clock_source_(gDefaultTraceClockSource), overflow_(false),
165 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
166}
167
jeffhaoe343b762011-12-05 16:36:44 -0800168void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800169 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800170 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800171 return;
172 }
173
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800175
jeffhao2692b572011-12-16 15:42:28 -0800176 // Open trace file if not going directly to ddms.
177 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800178 if (!direct_to_ddms) {
179 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800180 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800181 } else {
jeffhao2692b572011-12-16 15:42:28 -0800182 trace_file = OS::FileFromFd("tracefile", trace_fd);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800183 }
jeffhao2692b572011-12-16 15:42:28 -0800184 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700185 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800186 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
187 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800189 return;
190 }
191 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800192
jeffhao2692b572011-12-16 15:42:28 -0800193 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700194 Trace* tracer(new Trace(trace_file, buffer_size, flags));
195
196 // Enable count of allocs if specified in the flags.
197 if ((flags && kTraceCountAllocs) != 0) {
198 Runtime::Current()->SetStatsEnabled(true);
199 }
200
jeffhao2692b572011-12-16 15:42:28 -0800201 Runtime::Current()->EnableMethodTracing(tracer);
202 tracer->BeginTracing();
203
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800205}
206
207void Trace::Stop() {
208 if (!Runtime::Current()->IsMethodTracingActive()) {
209 LOG(INFO) << "Trace stop requested, but no trace currently running";
210 return;
211 }
212
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhao2692b572011-12-16 15:42:28 -0800214
jeffhao725a9572012-11-13 18:20:12 -0800215 Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing();
jeffhao2692b572011-12-16 15:42:28 -0800216 Runtime::Current()->DisableMethodTracing();
217
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800219}
220
jeffhaob5e81852012-03-12 11:15:45 -0700221void Trace::Shutdown() {
222 if (!Runtime::Current()->IsMethodTracingActive()) {
223 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
224 return;
225 }
jeffhao725a9572012-11-13 18:20:12 -0800226 Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing();
jeffhaob5e81852012-03-12 11:15:45 -0700227 Runtime::Current()->DisableMethodTracing();
228}
229
jeffhao2692b572011-12-16 15:42:28 -0800230void Trace::BeginTracing() {
231 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800232 start_time_ = MicroTime();
233
jeffhao2692b572011-12-16 15:42:28 -0800234 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800235 if (UseThreadCpuClock() && UseWallClock()) {
236 trace_version_ = kTraceVersionDualClock;
237 record_size_ = kTraceRecordSizeDualClock;
238 } else {
239 trace_version_ = kTraceVersionSingleClock;
240 record_size_ = kTraceRecordSizeSingleClock;
241 }
242
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800243 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800244 memset(buf_.get(), 0, kTraceHeaderLength);
245 Append4LE(buf_.get(), kTraceMagicValue);
246 Append2LE(buf_.get() + 4, trace_version_);
247 Append2LE(buf_.get() + 6, kTraceHeaderLength);
248 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800249 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800250 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800251 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800252
jeffhao2692b572011-12-16 15:42:28 -0800253 // Update current offset.
254 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800255
256 // Install all method tracing stubs.
jeffhao725a9572012-11-13 18:20:12 -0800257 Runtime::Current()->GetInstrumentation()->InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800258}
259
jeffhao2692b572011-12-16 15:42:28 -0800260void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800261 // Uninstall all method tracing stubs.
jeffhao725a9572012-11-13 18:20:12 -0800262 Runtime::Current()->GetInstrumentation()->UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800263
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800264 // Compute elapsed time.
265 uint64_t elapsed = MicroTime() - start_time_;
266
267 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700268 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800269
jeffhao0791adc2012-04-04 11:14:32 -0700270 if ((flags_ & kTraceCountAllocs) != 0) {
271 Runtime::Current()->SetStatsEnabled(false);
272 }
273
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800274 GetVisitedMethods(final_offset);
275
276 std::ostringstream os;
277
278 os << StringPrintf("%cversion\n", kTraceTokenChar);
279 os << StringPrintf("%d\n", trace_version_);
280 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
281 if (UseThreadCpuClock()) {
282 if (UseWallClock()) {
283 os << StringPrintf("clock=dual\n");
284 } else {
285 os << StringPrintf("clock=thread-cpu\n");
286 }
287 } else {
288 os << StringPrintf("clock=wall\n");
289 }
290 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800291 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800292 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
293 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700294 if ((flags_ & kTraceCountAllocs) != 0) {
295 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
296 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
297 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
298 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800299 os << StringPrintf("%cthreads\n", kTraceTokenChar);
300 DumpThreadList(os);
301 os << StringPrintf("%cmethods\n", kTraceTokenChar);
302 DumpMethodList(os);
303 os << StringPrintf("%cend\n", kTraceTokenChar);
304
305 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800306 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700307 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800308 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
309 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800310 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800311 iov[1].iov_len = final_offset;
312 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
313 } else {
314 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800315 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700316 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
317 PLOG(ERROR) << detail;
318 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800319 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800320 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800321}
322
Mathieu Chartier66f19252012-09-18 08:57:04 -0700323void Trace::LogMethodTraceEvent(Thread* self, const AbstractMethod* method, Trace::TraceEvent event) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800324 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
325 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700326 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800327 }
328
329 // Advance cur_offset_ atomically.
330 int32_t new_offset;
331 int32_t old_offset;
332 do {
333 old_offset = cur_offset_;
334 new_offset = old_offset + record_size_;
335 if (new_offset > buffer_size_) {
336 overflow_ = true;
337 return;
338 }
339 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
340
341 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
342
343 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800344 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800345 Append2LE(ptr, self->GetTid());
346 Append4LE(ptr + 2, method_value);
347 ptr += 6;
348
349 if (UseThreadCpuClock()) {
350 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
351 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
352 Append4LE(ptr, thread_clock_diff);
353 ptr += 4;
354 }
355
356 if (UseWallClock()) {
357 uint32_t wall_clock_diff = MicroTime() - start_time_;
358 Append4LE(ptr, wall_clock_diff);
359 }
360}
361
362void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800363 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
364 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800365
366 while (ptr < end) {
367 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700368 AbstractMethod* method = reinterpret_cast<AbstractMethod*>(TraceMethodId(method_value));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800369 visited_methods_.insert(method);
370 ptr += record_size_;
371 }
372}
373
374void Trace::DumpMethodList(std::ostream& os) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700375 typedef std::set<const AbstractMethod*>::const_iterator It; // TODO: C++0x auto
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800376 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700377 const AbstractMethod* method = *it;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800378 MethodHelper mh(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700379 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800380 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700381 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800382 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800383}
384
385static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800386 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
387 std::string name;
388 t->GetThreadName(name);
389 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800390}
391
392void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700393 Thread* self = Thread::Current();
394 Locks::thread_list_lock_->AssertNotHeld(self);
395 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800396 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800397}
398
jeffhaoe343b762011-12-05 16:36:44 -0800399} // namespace art