blob: 7b3cea8e3dbc3568c7815b3ab9bfe99163289ff6 [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
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080022#include "class_linker.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080023#include "debugger.h"
jeffhaoe343b762011-12-05 16:36:44 -080024#include "dex_cache.h"
jeffhao725a9572012-11-13 18:20:12 -080025#include "instrumentation.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070026#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers57b86d42012-03-27 16:05:41 -070027#include "oat/runtime/oat_support_entrypoints.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070028#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080029#include "object_utils.h"
30#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
jeffhaoe343b762011-12-05 16:36:44 -080032#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070033#include "thread_list.h"
jeffhao2692b572011-12-16 15:42:28 -080034
35namespace art {
36
Elliott Hughese119a362012-05-22 17:37:06 -070037// File format:
38// header
39// record 0
40// record 1
41// ...
42//
43// Header format:
44// u4 magic ('SLOW')
45// u2 version
46// u2 offset to data
47// u8 start date/time in usec
48// u2 record size in bytes (version >= 2 only)
49// ... padding to 32 bytes
50//
51// Record format v1:
52// u1 thread ID
53// u4 method ID | method action
54// u4 time delta since start, in usec
55//
56// Record format v2:
57// u2 thread ID
58// u4 method ID | method action
59// u4 time delta since start, in usec
60//
61// Record format v3:
62// u2 thread ID
63// u4 method ID | method action
64// u4 time delta since start, in usec
65// u4 wall time since start, in usec (when clock == "dual" only)
66//
67// 32 bits of microseconds is 70 minutes.
68//
69// All values are stored in little-endian order.
70
jeffhaoa9ef3fd2011-12-13 18:33:43 -080071static const uint32_t kTraceMethodActionMask = 0x03; // two bits
72static const char kTraceTokenChar = '*';
73static const uint16_t kTraceHeaderLength = 32;
74static const uint32_t kTraceMagicValue = 0x574f4c53;
75static const uint16_t kTraceVersionSingleClock = 2;
76static const uint16_t kTraceVersionDualClock = 3;
77static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
78static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
79
Elliott Hughese119a362012-05-22 17:37:06 -070080static ProfilerClockSource gDefaultTraceClockSource = kProfilerClockSourceDual;
81
jeffhaoa9ef3fd2011-12-13 18:33:43 -080082static inline uint32_t TraceMethodId(uint32_t methodValue) {
83 return (methodValue & ~kTraceMethodActionMask);
84}
Elliott Hughese119a362012-05-22 17:37:06 -070085
jeffhaoa9ef3fd2011-12-13 18:33:43 -080086static inline uint32_t TraceMethodCombine(uint32_t method, uint8_t traceEvent) {
87 return (method | traceEvent);
88}
89
Elliott Hughese119a362012-05-22 17:37:06 -070090void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
91 gDefaultTraceClockSource = clock_source;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080092}
93
Elliott Hughese119a362012-05-22 17:37:06 -070094bool Trace::UseThreadCpuClock() {
95#if defined(HAVE_POSIX_CLOCKS)
96 return clock_source_ != kProfilerClockSourceWall;
97#else
98 return false;
99#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800100}
101
Elliott Hughese119a362012-05-22 17:37:06 -0700102bool Trace::UseWallClock() {
103#if defined(HAVE_POSIX_CLOCKS)
104 return clock_source_ != kProfilerClockSourceThreadCpu;
105#else
106 return true;
107#endif
108}
109
110static void MeasureClockOverhead(Trace* trace) {
111 if (trace->UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800112 ThreadCpuMicroTime();
113 }
Elliott Hughese119a362012-05-22 17:37:06 -0700114 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800115 MicroTime();
116 }
117}
118
Elliott Hughese119a362012-05-22 17:37:06 -0700119static uint32_t GetClockOverhead(Trace* trace) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800120 uint64_t start = ThreadCpuMicroTime();
121
122 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700123 MeasureClockOverhead(trace);
124 MeasureClockOverhead(trace);
125 MeasureClockOverhead(trace);
126 MeasureClockOverhead(trace);
127 MeasureClockOverhead(trace);
128 MeasureClockOverhead(trace);
129 MeasureClockOverhead(trace);
130 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800131 }
132
133 uint64_t elapsed = ThreadCpuMicroTime() - start;
134 return uint32_t (elapsed / 32);
135}
136
Elliott Hughesffb465f2012-03-01 18:46:05 -0800137// TODO: put this somewhere with the big-endian equivalent used by JDWP.
138static void Append2LE(uint8_t* buf, uint16_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800139 *buf++ = (uint8_t) val;
140 *buf++ = (uint8_t) (val >> 8);
141}
142
Elliott Hughesffb465f2012-03-01 18:46:05 -0800143// TODO: put this somewhere with the big-endian equivalent used by JDWP.
144static void Append4LE(uint8_t* buf, uint32_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800145 *buf++ = (uint8_t) val;
146 *buf++ = (uint8_t) (val >> 8);
147 *buf++ = (uint8_t) (val >> 16);
148 *buf++ = (uint8_t) (val >> 24);
149}
150
Elliott Hughesffb465f2012-03-01 18:46:05 -0800151// TODO: put this somewhere with the big-endian equivalent used by JDWP.
152static void Append8LE(uint8_t* buf, uint64_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800153 *buf++ = (uint8_t) val;
154 *buf++ = (uint8_t) (val >> 8);
155 *buf++ = (uint8_t) (val >> 16);
156 *buf++ = (uint8_t) (val >> 24);
157 *buf++ = (uint8_t) (val >> 32);
158 *buf++ = (uint8_t) (val >> 40);
159 *buf++ = (uint8_t) (val >> 48);
160 *buf++ = (uint8_t) (val >> 56);
161}
162
Elliott Hughese119a362012-05-22 17:37:06 -0700163Trace::Trace(File* trace_file, int buffer_size, int flags)
164 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
165 clock_source_(gDefaultTraceClockSource), overflow_(false),
166 buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
167}
168
jeffhaoe343b762011-12-05 16:36:44 -0800169void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) {
jeffhao2692b572011-12-16 15:42:28 -0800170 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800171 LOG(INFO) << "Trace already in progress, ignoring this request";
jeffhaoe343b762011-12-05 16:36:44 -0800172 return;
173 }
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800176
jeffhao2692b572011-12-16 15:42:28 -0800177 // Open trace file if not going directly to ddms.
178 File* trace_file = NULL;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800179 if (!direct_to_ddms) {
180 if (trace_fd < 0) {
jeffhao2692b572011-12-16 15:42:28 -0800181 trace_file = OS::OpenFile(trace_filename, true);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800182 } else {
Elliott Hughes76160052012-12-12 16:31:20 -0800183 trace_file = new File(trace_fd, "tracefile");
184 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800185 }
jeffhao2692b572011-12-16 15:42:28 -0800186 if (trace_file == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700187 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800188 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
189 StringPrintf("Unable to open trace file '%s'", trace_filename).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800191 return;
192 }
193 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800194
jeffhao2692b572011-12-16 15:42:28 -0800195 // Create Trace object.
jeffhao0791adc2012-04-04 11:14:32 -0700196 Trace* tracer(new Trace(trace_file, buffer_size, flags));
197
198 // Enable count of allocs if specified in the flags.
199 if ((flags && kTraceCountAllocs) != 0) {
200 Runtime::Current()->SetStatsEnabled(true);
201 }
202
jeffhao2692b572011-12-16 15:42:28 -0800203 Runtime::Current()->EnableMethodTracing(tracer);
204 tracer->BeginTracing();
205
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800207}
208
209void Trace::Stop() {
210 if (!Runtime::Current()->IsMethodTracingActive()) {
211 LOG(INFO) << "Trace stop requested, but no trace currently running";
212 return;
213 }
214
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 Runtime::Current()->GetThreadList()->SuspendAll();
jeffhao2692b572011-12-16 15:42:28 -0800216
jeffhao725a9572012-11-13 18:20:12 -0800217 Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing();
jeffhao2692b572011-12-16 15:42:28 -0800218 Runtime::Current()->DisableMethodTracing();
219
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 Runtime::Current()->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800221}
222
jeffhaob5e81852012-03-12 11:15:45 -0700223void Trace::Shutdown() {
224 if (!Runtime::Current()->IsMethodTracingActive()) {
225 LOG(INFO) << "Trace shutdown requested, but no trace currently running";
226 return;
227 }
jeffhao725a9572012-11-13 18:20:12 -0800228 Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing();
jeffhaob5e81852012-03-12 11:15:45 -0700229 Runtime::Current()->DisableMethodTracing();
230}
231
jeffhao2692b572011-12-16 15:42:28 -0800232void Trace::BeginTracing() {
233 // Set the start time of tracing.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800234 start_time_ = MicroTime();
235
jeffhao2692b572011-12-16 15:42:28 -0800236 // Set trace version and record size.
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800237 if (UseThreadCpuClock() && UseWallClock()) {
238 trace_version_ = kTraceVersionDualClock;
239 record_size_ = kTraceRecordSizeDualClock;
240 } else {
241 trace_version_ = kTraceVersionSingleClock;
242 record_size_ = kTraceRecordSizeSingleClock;
243 }
244
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800245 // Set up the beginning of the trace.
jeffhao2692b572011-12-16 15:42:28 -0800246 memset(buf_.get(), 0, kTraceHeaderLength);
247 Append4LE(buf_.get(), kTraceMagicValue);
248 Append2LE(buf_.get() + 4, trace_version_);
249 Append2LE(buf_.get() + 6, kTraceHeaderLength);
250 Append8LE(buf_.get() + 8, start_time_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800251 if (trace_version_ >= kTraceVersionDualClock) {
jeffhao2692b572011-12-16 15:42:28 -0800252 Append2LE(buf_.get() + 16, record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800253 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800254
jeffhao2692b572011-12-16 15:42:28 -0800255 // Update current offset.
256 cur_offset_ = kTraceHeaderLength;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800257
258 // Install all method tracing stubs.
jeffhao725a9572012-11-13 18:20:12 -0800259 Runtime::Current()->GetInstrumentation()->InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -0800260}
261
jeffhao2692b572011-12-16 15:42:28 -0800262void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800263 // Uninstall all method tracing stubs.
jeffhao725a9572012-11-13 18:20:12 -0800264 Runtime::Current()->GetInstrumentation()->UninstallStubs();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800265
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800266 // Compute elapsed time.
267 uint64_t elapsed = MicroTime() - start_time_;
268
269 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700270 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800271
jeffhao0791adc2012-04-04 11:14:32 -0700272 if ((flags_ & kTraceCountAllocs) != 0) {
273 Runtime::Current()->SetStatsEnabled(false);
274 }
275
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276 GetVisitedMethods(final_offset);
277
278 std::ostringstream os;
279
280 os << StringPrintf("%cversion\n", kTraceTokenChar);
281 os << StringPrintf("%d\n", trace_version_);
282 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
283 if (UseThreadCpuClock()) {
284 if (UseWallClock()) {
285 os << StringPrintf("clock=dual\n");
286 } else {
287 os << StringPrintf("clock=thread-cpu\n");
288 }
289 } else {
290 os << StringPrintf("clock=wall\n");
291 }
292 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800293 os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800294 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
295 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700296 if ((flags_ & kTraceCountAllocs) != 0) {
297 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
298 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
299 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
300 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800301 os << StringPrintf("%cthreads\n", kTraceTokenChar);
302 DumpThreadList(os);
303 os << StringPrintf("%cmethods\n", kTraceTokenChar);
304 DumpMethodList(os);
305 os << StringPrintf("%cend\n", kTraceTokenChar);
306
307 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800308 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700309 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800310 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
311 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800312 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800313 iov[1].iov_len = final_offset;
314 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
315 } else {
316 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800317 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700318 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
319 PLOG(ERROR) << detail;
320 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800321 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800322 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800323}
324
Mathieu Chartier66f19252012-09-18 08:57:04 -0700325void Trace::LogMethodTraceEvent(Thread* self, const AbstractMethod* method, Trace::TraceEvent event) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800326 if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) {
327 uint64_t time = ThreadCpuMicroTime();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700328 thread_clock_base_map_.Put(self, time);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800329 }
330
331 // Advance cur_offset_ atomically.
332 int32_t new_offset;
333 int32_t old_offset;
334 do {
335 old_offset = cur_offset_;
336 new_offset = old_offset + record_size_;
337 if (new_offset > buffer_size_) {
338 overflow_ = true;
339 return;
340 }
341 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
342
343 uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event);
344
345 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800346 uint8_t* ptr = buf_.get() + old_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800347 Append2LE(ptr, self->GetTid());
348 Append4LE(ptr + 2, method_value);
349 ptr += 6;
350
351 if (UseThreadCpuClock()) {
352 uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second;
353 uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
354 Append4LE(ptr, thread_clock_diff);
355 ptr += 4;
356 }
357
358 if (UseWallClock()) {
359 uint32_t wall_clock_diff = MicroTime() - start_time_;
360 Append4LE(ptr, wall_clock_diff);
361 }
362}
363
364void Trace::GetVisitedMethods(size_t end_offset) {
jeffhao2692b572011-12-16 15:42:28 -0800365 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
366 uint8_t* end = buf_.get() + end_offset;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800367
368 while (ptr < end) {
369 uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700370 AbstractMethod* method = reinterpret_cast<AbstractMethod*>(TraceMethodId(method_value));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800371 visited_methods_.insert(method);
372 ptr += record_size_;
373 }
374}
375
376void Trace::DumpMethodList(std::ostream& os) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700377 typedef std::set<const AbstractMethod*>::const_iterator It; // TODO: C++0x auto
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800378 for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700379 const AbstractMethod* method = *it;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800380 MethodHelper mh(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700381 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800382 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700383 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800384 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800385}
386
387static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800388 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
389 std::string name;
390 t->GetThreadName(name);
391 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800392}
393
394void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700395 Thread* self = Thread::Current();
396 Locks::thread_list_lock_->AssertNotHeld(self);
397 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800398 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800399}
400
jeffhaoe343b762011-12-05 16:36:44 -0800401} // namespace art