trace.cc revision 5ce4b178d2483df679e7f718e379305e5d42a300
1/* 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 */ 16 17#include "trace.h" 18 19#include <sys/uio.h> 20 21#include "base/stl_util.h" 22#include "base/unix_file/fd_file.h" 23#include "class_linker.h" 24#include "common_throws.h" 25#include "debugger.h" 26#include "dex_file-inl.h" 27#include "instrumentation.h" 28#include "mirror/art_method-inl.h" 29#include "mirror/class-inl.h" 30#include "mirror/dex_cache.h" 31#include "mirror/object_array-inl.h" 32#include "mirror/object-inl.h" 33#include "object_utils.h" 34#include "os.h" 35#include "scoped_thread_state_change.h" 36#include "ScopedLocalRef.h" 37#include "thread.h" 38#include "thread_list.h" 39#if !defined(ART_USE_PORTABLE_COMPILER) 40#include "entrypoints/quick/quick_entrypoints.h" 41#endif 42 43namespace art { 44 45// File format: 46// header 47// record 0 48// record 1 49// ... 50// 51// Header format: 52// u4 magic ('SLOW') 53// u2 version 54// u2 offset to data 55// u8 start date/time in usec 56// u2 record size in bytes (version >= 2 only) 57// ... padding to 32 bytes 58// 59// Record format v1: 60// u1 thread ID 61// u4 method ID | method action 62// u4 time delta since start, in usec 63// 64// Record format v2: 65// u2 thread ID 66// u4 method ID | method action 67// u4 time delta since start, in usec 68// 69// Record format v3: 70// u2 thread ID 71// u4 method ID | method action 72// u4 time delta since start, in usec 73// u4 wall time since start, in usec (when clock == "dual" only) 74// 75// 32 bits of microseconds is 70 minutes. 76// 77// All values are stored in little-endian order. 78 79enum TraceAction { 80 kTraceMethodEnter = 0x00, // method entry 81 kTraceMethodExit = 0x01, // method exit 82 kTraceUnroll = 0x02, // method exited by exception unrolling 83 // 0x03 currently unused 84 kTraceMethodActionMask = 0x03, // two bits 85}; 86 87class BuildStackTraceVisitor : public StackVisitor { 88 public: 89 explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL), 90 method_trace_(Trace::AllocStackTrace()) {} 91 92 bool VisitFrame() { 93 mirror::ArtMethod* m = GetMethod(); 94 // Ignore runtime frames (in particular callee save). 95 if (!m->IsRuntimeMethod()) { 96 method_trace_->push_back(m); 97 } 98 return true; 99 } 100 101 // Returns a stack trace where the topmost frame corresponds with the first element of the vector. 102 std::vector<mirror::ArtMethod*>* GetStackTrace() const { 103 return method_trace_; 104 } 105 106 private: 107 std::vector<mirror::ArtMethod*>* const method_trace_; 108}; 109 110static const char kTraceTokenChar = '*'; 111static const uint16_t kTraceHeaderLength = 32; 112static const uint32_t kTraceMagicValue = 0x574f4c53; 113static const uint16_t kTraceVersionSingleClock = 2; 114static const uint16_t kTraceVersionDualClock = 3; 115static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2 116static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps 117 118#if defined(HAVE_POSIX_CLOCKS) 119ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceDual; 120#else 121ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceWall; 122#endif 123 124Trace* volatile Trace::the_trace_ = NULL; 125// TODO: Add way to enable sampling and set interval through gui. 126bool Trace::sampling_enabled_ = true; 127uint32_t Trace::sampling_interval_us_ = 10000; 128pthread_t Trace::sampling_pthread_ = 0U; 129UniquePtr<std::vector<mirror::ArtMethod*> > Trace::temp_stack_trace_; 130 131static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) { 132 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask); 133} 134 135static TraceAction DecodeTraceAction(uint32_t tmid) { 136 return static_cast<TraceAction>(tmid & kTraceMethodActionMask); 137} 138 139static uint32_t EncodeTraceMethodAndAction(const mirror::ArtMethod* method, 140 TraceAction action) { 141 uint32_t tmid = reinterpret_cast<uint32_t>(method) | action; 142 DCHECK_EQ(method, DecodeTraceMethodId(tmid)); 143 return tmid; 144} 145 146std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() { 147 if (temp_stack_trace_.get() != NULL) { 148 return temp_stack_trace_.release(); 149 } else { 150 return new std::vector<mirror::ArtMethod*>(); 151 } 152} 153 154void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) { 155 stack_trace->clear(); 156 temp_stack_trace_.reset(stack_trace); 157} 158 159void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) { 160#if defined(HAVE_POSIX_CLOCKS) 161 default_clock_source_ = clock_source; 162#else 163 if (clock_source != kProfilerClockSourceWall) { 164 LOG(WARNING) << "Ignoring tracing request to use CPU time."; 165 } 166#endif 167} 168 169static uint16_t GetTraceVersion(ProfilerClockSource clock_source) { 170 return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock 171 : kTraceVersionSingleClock; 172} 173 174static uint16_t GetRecordSize(ProfilerClockSource clock_source) { 175 return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock 176 : kTraceRecordSizeSingleClock; 177} 178 179bool Trace::UseThreadCpuClock() { 180 return (clock_source_ == kProfilerClockSourceThreadCpu) || 181 (clock_source_ == kProfilerClockSourceDual); 182} 183 184bool Trace::UseWallClock() { 185 return (clock_source_ == kProfilerClockSourceWall) || 186 (clock_source_ == kProfilerClockSourceDual); 187} 188 189static void MeasureClockOverhead(Trace* trace) { 190 if (trace->UseThreadCpuClock()) { 191 Thread::Current()->GetCpuMicroTime(); 192 } 193 if (trace->UseWallClock()) { 194 MicroTime(); 195 } 196} 197 198// Compute an average time taken to measure clocks. 199static uint32_t GetClockOverheadNanoSeconds(Trace* trace) { 200 Thread* self = Thread::Current(); 201 uint64_t start = self->GetCpuMicroTime(); 202 203 for (int i = 4000; i > 0; i--) { 204 MeasureClockOverhead(trace); 205 MeasureClockOverhead(trace); 206 MeasureClockOverhead(trace); 207 MeasureClockOverhead(trace); 208 MeasureClockOverhead(trace); 209 MeasureClockOverhead(trace); 210 MeasureClockOverhead(trace); 211 MeasureClockOverhead(trace); 212 } 213 214 uint64_t elapsed_us = self->GetCpuMicroTime() - start; 215 return static_cast<uint32_t>(elapsed_us / 32); 216} 217 218// TODO: put this somewhere with the big-endian equivalent used by JDWP. 219static void Append2LE(uint8_t* buf, uint16_t val) { 220 *buf++ = static_cast<uint8_t>(val); 221 *buf++ = static_cast<uint8_t>(val >> 8); 222} 223 224// TODO: put this somewhere with the big-endian equivalent used by JDWP. 225static void Append4LE(uint8_t* buf, uint32_t val) { 226 *buf++ = static_cast<uint8_t>(val); 227 *buf++ = static_cast<uint8_t>(val >> 8); 228 *buf++ = static_cast<uint8_t>(val >> 16); 229 *buf++ = static_cast<uint8_t>(val >> 24); 230} 231 232// TODO: put this somewhere with the big-endian equivalent used by JDWP. 233static void Append8LE(uint8_t* buf, uint64_t val) { 234 *buf++ = static_cast<uint8_t>(val); 235 *buf++ = static_cast<uint8_t>(val >> 8); 236 *buf++ = static_cast<uint8_t>(val >> 16); 237 *buf++ = static_cast<uint8_t>(val >> 24); 238 *buf++ = static_cast<uint8_t>(val >> 32); 239 *buf++ = static_cast<uint8_t>(val >> 40); 240 *buf++ = static_cast<uint8_t>(val >> 48); 241 *buf++ = static_cast<uint8_t>(val >> 56); 242} 243 244static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 245 BuildStackTraceVisitor build_trace_visitor(thread); 246 build_trace_visitor.WalkStack(); 247 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace(); 248 Trace* the_trace = reinterpret_cast<Trace*>(arg); 249 the_trace->CompareAndUpdateStackTrace(thread, stack_trace); 250} 251 252static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg) { 253 thread->SetTraceClockBase(0); 254 std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample(); 255 thread->SetStackTraceSample(NULL); 256 delete stack_trace; 257} 258 259void Trace::CompareAndUpdateStackTrace(Thread* thread, 260 std::vector<mirror::ArtMethod*>* stack_trace) { 261 CHECK_EQ(pthread_self(), sampling_pthread_); 262 std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample(); 263 // Update the thread's stack trace sample. 264 thread->SetStackTraceSample(stack_trace); 265 if (old_stack_trace == NULL) { 266 // If there's no previous stack trace sample for this thread, log an entry event for all 267 // methods in the trace. 268 for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin(); 269 rit != stack_trace->rend(); ++rit) { 270 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered); 271 } 272 } else { 273 // If there's a previous stack trace for this thread, diff the traces and emit entry and exit 274 // events accordingly. 275 std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin(); 276 std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin(); 277 // Iterate bottom-up over both traces until there's a difference between them. 278 while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) { 279 old_rit++; 280 rit++; 281 } 282 // Iterate top-down over the old trace until the point where they differ, emitting exit events. 283 for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin(); 284 old_it != old_rit.base(); ++old_it) { 285 LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited); 286 } 287 // Iterate bottom-up over the new trace from the point where they differ, emitting entry events. 288 for (; rit != stack_trace->rend(); ++rit) { 289 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered); 290 } 291 FreeStackTrace(old_stack_trace); 292 } 293} 294 295void* Trace::RunSamplingThread(void* arg) { 296 Runtime* runtime = Runtime::Current(); 297 CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(), 298 !runtime->IsCompiler())); 299 300 while (true) { 301 usleep(sampling_interval_us_); 302 ATRACE_BEGIN("Profile sampling"); 303 Thread* self = Thread::Current(); 304 Trace* the_trace; 305 { 306 MutexLock mu(self, *Locks::trace_lock_); 307 the_trace = the_trace_; 308 if (the_trace == NULL) { 309 break; 310 } 311 } 312 313 runtime->GetThreadList()->SuspendAll(); 314 { 315 MutexLock mu(self, *Locks::thread_list_lock_); 316 runtime->GetThreadList()->ForEach(GetSample, the_trace); 317 } 318 runtime->GetThreadList()->ResumeAll(); 319 ATRACE_END(); 320 } 321 322 runtime->DetachCurrentThread(); 323 return NULL; 324} 325 326void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, 327 bool direct_to_ddms) { 328 Thread* self = Thread::Current(); 329 { 330 MutexLock mu(self, *Locks::trace_lock_); 331 if (the_trace_ != NULL) { 332 LOG(ERROR) << "Trace already in progress, ignoring this request"; 333 return; 334 } 335 } 336 Runtime* runtime = Runtime::Current(); 337 runtime->GetThreadList()->SuspendAll(); 338 339 // Open trace file if not going directly to ddms. 340 UniquePtr<File> trace_file; 341 if (!direct_to_ddms) { 342 if (trace_fd < 0) { 343 trace_file.reset(OS::CreateEmptyFile(trace_filename)); 344 } else { 345 trace_file.reset(new File(trace_fd, "tracefile")); 346 trace_file->DisableAutoClose(); 347 } 348 if (trace_file.get() == NULL) { 349 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'"; 350 runtime->GetThreadList()->ResumeAll(); 351 ScopedObjectAccess soa(self); 352 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename); 353 return; 354 } 355 } 356 357 // Create Trace object. 358 { 359 MutexLock mu(self, *Locks::trace_lock_); 360 if (the_trace_ != NULL) { 361 LOG(ERROR) << "Trace already in progress, ignoring this request"; 362 } else { 363 the_trace_ = new Trace(trace_file.release(), buffer_size, flags); 364 365 // Enable count of allocs if specified in the flags. 366 if ((flags && kTraceCountAllocs) != 0) { 367 runtime->SetStatsEnabled(true); 368 } 369 370 if (sampling_enabled_) { 371 CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread, NULL), 372 "Sampling profiler thread"); 373 } else { 374 runtime->GetInstrumentation()->AddListener(the_trace_, 375 instrumentation::Instrumentation::kMethodEntered | 376 instrumentation::Instrumentation::kMethodExited | 377 instrumentation::Instrumentation::kMethodUnwind); 378 } 379 } 380 } 381 runtime->GetThreadList()->ResumeAll(); 382} 383 384void Trace::Stop() { 385 Runtime* runtime = Runtime::Current(); 386 runtime->GetThreadList()->SuspendAll(); 387 Trace* the_trace = NULL; 388 pthread_t sampling_pthread = 0U; 389 { 390 MutexLock mu(Thread::Current(), *Locks::trace_lock_); 391 if (the_trace_ == NULL) { 392 LOG(ERROR) << "Trace stop requested, but no trace currently running"; 393 } else { 394 the_trace = the_trace_; 395 the_trace_ = NULL; 396 sampling_pthread = sampling_pthread_; 397 sampling_pthread_ = 0U; 398 } 399 } 400 if (the_trace != NULL) { 401 the_trace->FinishTracing(); 402 403 if (sampling_enabled_) { 404 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); 405 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL); 406 } else { 407 runtime->GetInstrumentation()->RemoveListener(the_trace, 408 instrumentation::Instrumentation::kMethodEntered | 409 instrumentation::Instrumentation::kMethodExited | 410 instrumentation::Instrumentation::kMethodUnwind); 411 } 412 delete the_trace; 413 } 414 runtime->GetThreadList()->ResumeAll(); 415 416 if (sampling_enabled_ && sampling_pthread != 0U) { 417 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown"); 418 } 419} 420 421void Trace::Shutdown() { 422 if (IsMethodTracingActive()) { 423 Stop(); 424 } 425} 426 427bool Trace::IsMethodTracingActive() { 428 MutexLock mu(Thread::Current(), *Locks::trace_lock_); 429 return the_trace_ != NULL; 430} 431 432Trace::Trace(File* trace_file, int buffer_size, int flags) 433 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags), 434 clock_source_(default_clock_source_), buffer_size_(buffer_size), start_time_(MicroTime()), 435 cur_offset_(0), overflow_(false) { 436 // Set up the beginning of the trace. 437 uint16_t trace_version = GetTraceVersion(clock_source_); 438 memset(buf_.get(), 0, kTraceHeaderLength); 439 Append4LE(buf_.get(), kTraceMagicValue); 440 Append2LE(buf_.get() + 4, trace_version); 441 Append2LE(buf_.get() + 6, kTraceHeaderLength); 442 Append8LE(buf_.get() + 8, start_time_); 443 if (trace_version >= kTraceVersionDualClock) { 444 uint16_t record_size = GetRecordSize(clock_source_); 445 Append2LE(buf_.get() + 16, record_size); 446 } 447 448 // Update current offset. 449 cur_offset_ = kTraceHeaderLength; 450} 451 452Trace::~Trace() { 453 CHECK_EQ(sampling_pthread_, static_cast<pthread_t>(0U)); 454} 455 456static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source) 457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 458 uint8_t* ptr = buf + kTraceHeaderLength; 459 uint8_t* end = buf + buf_size; 460 461 while (ptr < end) { 462 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); 463 mirror::ArtMethod* method = DecodeTraceMethodId(tmid); 464 TraceAction action = DecodeTraceAction(tmid); 465 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action); 466 ptr += GetRecordSize(clock_source); 467 } 468} 469 470void Trace::FinishTracing() { 471 // Compute elapsed time. 472 uint64_t elapsed = MicroTime() - start_time_; 473 474 size_t final_offset = cur_offset_; 475 uint32_t clock_overhead_ns = GetClockOverheadNanoSeconds(this); 476 477 if ((flags_ & kTraceCountAllocs) != 0) { 478 Runtime::Current()->SetStatsEnabled(false); 479 } 480 481 std::set<mirror::ArtMethod*> visited_methods; 482 GetVisitedMethods(final_offset, &visited_methods); 483 484 std::ostringstream os; 485 486 os << StringPrintf("%cversion\n", kTraceTokenChar); 487 os << StringPrintf("%d\n", GetTraceVersion(clock_source_)); 488 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false"); 489 if (UseThreadCpuClock()) { 490 if (UseWallClock()) { 491 os << StringPrintf("clock=dual\n"); 492 } else { 493 os << StringPrintf("clock=thread-cpu\n"); 494 } 495 } else { 496 os << StringPrintf("clock=wall\n"); 497 } 498 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed); 499 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_); 500 os << StringPrintf("num-method-calls=%zd\n", num_records); 501 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns); 502 os << StringPrintf("vm=art\n"); 503 if ((flags_ & kTraceCountAllocs) != 0) { 504 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS)); 505 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES)); 506 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS)); 507 } 508 os << StringPrintf("%cthreads\n", kTraceTokenChar); 509 DumpThreadList(os); 510 os << StringPrintf("%cmethods\n", kTraceTokenChar); 511 DumpMethodList(os, visited_methods); 512 os << StringPrintf("%cend\n", kTraceTokenChar); 513 514 std::string header(os.str()); 515 if (trace_file_.get() == NULL) { 516 iovec iov[2]; 517 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str())); 518 iov[0].iov_len = header.length(); 519 iov[1].iov_base = buf_.get(); 520 iov[1].iov_len = final_offset; 521 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2); 522 const bool kDumpTraceInfo = false; 523 if (kDumpTraceInfo) { 524 LOG(INFO) << "Trace sent:\n" << header; 525 DumpBuf(buf_.get(), final_offset, clock_source_); 526 } 527 } else { 528 if (!trace_file_->WriteFully(header.c_str(), header.length()) || 529 !trace_file_->WriteFully(buf_.get(), final_offset)) { 530 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno))); 531 PLOG(ERROR) << detail; 532 ThrowRuntimeException("%s", detail.c_str()); 533 } 534 } 535} 536 537void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object, 538 const mirror::ArtMethod* method, uint32_t new_dex_pc) { 539 // We're not recorded to listen to this kind of event, so complain. 540 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc; 541}; 542 543void Trace::MethodEntered(Thread* thread, mirror::Object* this_object, 544 const mirror::ArtMethod* method, uint32_t dex_pc) { 545 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered); 546} 547 548void Trace::MethodExited(Thread* thread, mirror::Object* this_object, 549 const mirror::ArtMethod* method, uint32_t dex_pc, 550 const JValue& return_value) { 551 UNUSED(return_value); 552 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited); 553} 554 555void Trace::MethodUnwind(Thread* thread, const mirror::ArtMethod* method, uint32_t dex_pc) { 556 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind); 557} 558 559void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, 560 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, 561 mirror::Throwable* exception_object) 562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 563 LOG(ERROR) << "Unexpected exception caught event in tracing"; 564} 565 566void Trace::LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method, 567 instrumentation::Instrumentation::InstrumentationEvent event) { 568 // Advance cur_offset_ atomically. 569 int32_t new_offset; 570 int32_t old_offset; 571 do { 572 old_offset = cur_offset_; 573 new_offset = old_offset + GetRecordSize(clock_source_); 574 if (new_offset > buffer_size_) { 575 overflow_ = true; 576 return; 577 } 578 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0); 579 580 TraceAction action = kTraceMethodEnter; 581 switch (event) { 582 case instrumentation::Instrumentation::kMethodEntered: 583 action = kTraceMethodEnter; 584 break; 585 case instrumentation::Instrumentation::kMethodExited: 586 action = kTraceMethodExit; 587 break; 588 case instrumentation::Instrumentation::kMethodUnwind: 589 action = kTraceUnroll; 590 break; 591 default: 592 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event; 593 } 594 595 uint32_t method_value = EncodeTraceMethodAndAction(method, action); 596 597 // Write data 598 uint8_t* ptr = buf_.get() + old_offset; 599 Append2LE(ptr, thread->GetTid()); 600 Append4LE(ptr + 2, method_value); 601 ptr += 6; 602 603 if (UseThreadCpuClock()) { 604 uint64_t clock_base = thread->GetTraceClockBase(); 605 uint32_t thread_clock_diff = 0; 606 if (UNLIKELY(clock_base == 0)) { 607 // First event, record the base time in the map. 608 uint64_t time = thread->GetCpuMicroTime(); 609 thread->SetTraceClockBase(time); 610 } else { 611 thread_clock_diff = thread->GetCpuMicroTime() - clock_base; 612 } 613 Append4LE(ptr, thread_clock_diff); 614 ptr += 4; 615 } 616 if (UseWallClock()) { 617 uint32_t wall_clock_diff = MicroTime() - start_time_; 618 Append4LE(ptr, wall_clock_diff); 619 } 620} 621 622void Trace::GetVisitedMethods(size_t buf_size, 623 std::set<mirror::ArtMethod*>* visited_methods) { 624 uint8_t* ptr = buf_.get() + kTraceHeaderLength; 625 uint8_t* end = buf_.get() + buf_size; 626 627 while (ptr < end) { 628 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); 629 mirror::ArtMethod* method = DecodeTraceMethodId(tmid); 630 visited_methods->insert(method); 631 ptr += GetRecordSize(clock_source_); 632 } 633} 634 635void Trace::DumpMethodList(std::ostream& os, 636 const std::set<mirror::ArtMethod*>& visited_methods) { 637 typedef std::set<mirror::ArtMethod*>::const_iterator It; // TODO: C++0x auto 638 MethodHelper mh; 639 for (It it = visited_methods.begin(); it != visited_methods.end(); ++it) { 640 mirror::ArtMethod* method = *it; 641 mh.ChangeMethod(method); 642 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method, 643 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(), 644 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile()); 645 } 646} 647 648static void DumpThread(Thread* t, void* arg) { 649 std::ostream& os = *reinterpret_cast<std::ostream*>(arg); 650 std::string name; 651 t->GetThreadName(name); 652 os << t->GetTid() << "\t" << name << "\n"; 653} 654 655void Trace::DumpThreadList(std::ostream& os) { 656 Thread* self = Thread::Current(); 657 Locks::thread_list_lock_->AssertNotHeld(self); 658 MutexLock mu(self, *Locks::thread_list_lock_); 659 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); 660} 661 662} // namespace art 663