trace.cc revision 8e5bd18fc665d7ec5461ea068e98740a65da754c
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#include <unistd.h>
21
22#define ATRACE_TAG ATRACE_TAG_DALVIK
23#include "cutils/trace.h"
24
25#include "base/casts.h"
26#include "base/stl_util.h"
27#include "base/unix_file/fd_file.h"
28#include "class_linker.h"
29#include "common_throws.h"
30#include "debugger.h"
31#include "dex_file-inl.h"
32#include "instrumentation.h"
33#include "mirror/art_method-inl.h"
34#include "mirror/class-inl.h"
35#include "mirror/dex_cache-inl.h"
36#include "mirror/object_array-inl.h"
37#include "mirror/object-inl.h"
38#include "os.h"
39#include "scoped_thread_state_change.h"
40#include "ScopedLocalRef.h"
41#include "thread.h"
42#include "thread_list.h"
43#include "entrypoints/quick/quick_entrypoints.h"
44
45namespace art {
46
47// File format:
48//     header
49//     record 0
50//     record 1
51//     ...
52//
53// Header format:
54//     u4  magic ('SLOW')
55//     u2  version
56//     u2  offset to data
57//     u8  start date/time in usec
58//     u2  record size in bytes (version >= 2 only)
59//     ... padding to 32 bytes
60//
61// Record format v1:
62//     u1  thread ID
63//     u4  method ID | method action
64//     u4  time delta since start, in usec
65//
66// Record format v2:
67//     u2  thread ID
68//     u4  method ID | method action
69//     u4  time delta since start, in usec
70//
71// Record format v3:
72//     u2  thread ID
73//     u4  method ID | method action
74//     u4  time delta since start, in usec
75//     u4  wall time since start, in usec (when clock == "dual" only)
76//
77// 32 bits of microseconds is 70 minutes.
78//
79// All values are stored in little-endian order.
80
81enum TraceAction {
82    kTraceMethodEnter = 0x00,       // method entry
83    kTraceMethodExit = 0x01,        // method exit
84    kTraceUnroll = 0x02,            // method exited by exception unrolling
85    // 0x03 currently unused
86    kTraceMethodActionMask = 0x03,  // two bits
87};
88
89static constexpr uint8_t kOpNewMethod = 1U;
90static constexpr uint8_t kOpNewThread = 2U;
91
92class BuildStackTraceVisitor : public StackVisitor {
93 public:
94  explicit BuildStackTraceVisitor(Thread* thread)
95      : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
96        method_trace_(Trace::AllocStackTrace()) {}
97
98  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99    mirror::ArtMethod* m = GetMethod();
100    // Ignore runtime frames (in particular callee save).
101    if (!m->IsRuntimeMethod()) {
102      method_trace_->push_back(m);
103    }
104    return true;
105  }
106
107  // Returns a stack trace where the topmost frame corresponds with the first element of the vector.
108  std::vector<mirror::ArtMethod*>* GetStackTrace() const {
109    return method_trace_;
110  }
111
112 private:
113  std::vector<mirror::ArtMethod*>* const method_trace_;
114};
115
116static const char     kTraceTokenChar             = '*';
117static const uint16_t kTraceHeaderLength          = 32;
118static const uint32_t kTraceMagicValue            = 0x574f4c53;
119static const uint16_t kTraceVersionSingleClock    = 2;
120static const uint16_t kTraceVersionDualClock      = 3;
121static const uint16_t kTraceRecordSizeSingleClock = 10;  // using v2
122static const uint16_t kTraceRecordSizeDualClock   = 14;  // using v3 with two timestamps
123
124TraceClockSource Trace::default_clock_source_ = kDefaultTraceClockSource;
125
126Trace* volatile Trace::the_trace_ = nullptr;
127pthread_t Trace::sampling_pthread_ = 0U;
128std::unique_ptr<std::vector<mirror::ArtMethod*>> Trace::temp_stack_trace_;
129
130// The key identifying the tracer to update instrumentation.
131static constexpr const char* kTracerInstrumentationKey = "Tracer";
132
133static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
134  return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
135}
136
137static TraceAction DecodeTraceAction(uint32_t tmid) {
138  return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
139}
140
141static uint32_t EncodeTraceMethodAndAction(mirror::ArtMethod* method,
142                                           TraceAction action) {
143  uint32_t tmid = PointerToLowMemUInt32(method) | action;
144  DCHECK_EQ(method, DecodeTraceMethodId(tmid));
145  return tmid;
146}
147
148std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
149  if (temp_stack_trace_.get() != nullptr) {
150    return temp_stack_trace_.release();
151  } else {
152    return new std::vector<mirror::ArtMethod*>();
153  }
154}
155
156void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
157  stack_trace->clear();
158  temp_stack_trace_.reset(stack_trace);
159}
160
161void Trace::SetDefaultClockSource(TraceClockSource clock_source) {
162#if defined(__linux__)
163  default_clock_source_ = clock_source;
164#else
165  if (clock_source != TraceClockSource::kWall) {
166    LOG(WARNING) << "Ignoring tracing request to use CPU time.";
167  }
168#endif
169}
170
171static uint16_t GetTraceVersion(TraceClockSource clock_source) {
172  return (clock_source == TraceClockSource::kDual) ? kTraceVersionDualClock
173                                                    : kTraceVersionSingleClock;
174}
175
176static uint16_t GetRecordSize(TraceClockSource clock_source) {
177  return (clock_source == TraceClockSource::kDual) ? kTraceRecordSizeDualClock
178                                                    : kTraceRecordSizeSingleClock;
179}
180
181bool Trace::UseThreadCpuClock() {
182  return (clock_source_ == TraceClockSource::kThreadCpu) ||
183      (clock_source_ == TraceClockSource::kDual);
184}
185
186bool Trace::UseWallClock() {
187  return (clock_source_ == TraceClockSource::kWall) ||
188      (clock_source_ == TraceClockSource::kDual);
189}
190
191void Trace::MeasureClockOverhead() {
192  if (UseThreadCpuClock()) {
193    Thread::Current()->GetCpuMicroTime();
194  }
195  if (UseWallClock()) {
196    MicroTime();
197  }
198}
199
200// Compute an average time taken to measure clocks.
201uint32_t Trace::GetClockOverheadNanoSeconds() {
202  Thread* self = Thread::Current();
203  uint64_t start = self->GetCpuMicroTime();
204
205  for (int i = 4000; i > 0; i--) {
206    MeasureClockOverhead();
207    MeasureClockOverhead();
208    MeasureClockOverhead();
209    MeasureClockOverhead();
210    MeasureClockOverhead();
211    MeasureClockOverhead();
212    MeasureClockOverhead();
213    MeasureClockOverhead();
214  }
215
216  uint64_t elapsed_us = self->GetCpuMicroTime() - start;
217  return static_cast<uint32_t>(elapsed_us / 32);
218}
219
220// TODO: put this somewhere with the big-endian equivalent used by JDWP.
221static void Append2LE(uint8_t* buf, uint16_t val) {
222  *buf++ = static_cast<uint8_t>(val);
223  *buf++ = static_cast<uint8_t>(val >> 8);
224}
225
226// TODO: put this somewhere with the big-endian equivalent used by JDWP.
227static void Append4LE(uint8_t* buf, uint32_t val) {
228  *buf++ = static_cast<uint8_t>(val);
229  *buf++ = static_cast<uint8_t>(val >> 8);
230  *buf++ = static_cast<uint8_t>(val >> 16);
231  *buf++ = static_cast<uint8_t>(val >> 24);
232}
233
234// TODO: put this somewhere with the big-endian equivalent used by JDWP.
235static void Append8LE(uint8_t* buf, uint64_t val) {
236  *buf++ = static_cast<uint8_t>(val);
237  *buf++ = static_cast<uint8_t>(val >> 8);
238  *buf++ = static_cast<uint8_t>(val >> 16);
239  *buf++ = static_cast<uint8_t>(val >> 24);
240  *buf++ = static_cast<uint8_t>(val >> 32);
241  *buf++ = static_cast<uint8_t>(val >> 40);
242  *buf++ = static_cast<uint8_t>(val >> 48);
243  *buf++ = static_cast<uint8_t>(val >> 56);
244}
245
246static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
247  BuildStackTraceVisitor build_trace_visitor(thread);
248  build_trace_visitor.WalkStack();
249  std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
250  Trace* the_trace = reinterpret_cast<Trace*>(arg);
251  the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
252}
253
254static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
255  thread->SetTraceClockBase(0);
256  std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
257  thread->SetStackTraceSample(nullptr);
258  delete stack_trace;
259}
260
261void Trace::CompareAndUpdateStackTrace(Thread* thread,
262                                       std::vector<mirror::ArtMethod*>* stack_trace) {
263  CHECK_EQ(pthread_self(), sampling_pthread_);
264  std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
265  // Update the thread's stack trace sample.
266  thread->SetStackTraceSample(stack_trace);
267  // Read timer clocks to use for all events in this trace.
268  uint32_t thread_clock_diff = 0;
269  uint32_t wall_clock_diff = 0;
270  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
271  if (old_stack_trace == nullptr) {
272    // If there's no previous stack trace sample for this thread, log an entry event for all
273    // methods in the trace.
274    for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
275         rit != stack_trace->rend(); ++rit) {
276      LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
277                          thread_clock_diff, wall_clock_diff);
278    }
279  } else {
280    // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
281    // events accordingly.
282    std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
283    std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
284    // Iterate bottom-up over both traces until there's a difference between them.
285    while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
286      old_rit++;
287      rit++;
288    }
289    // Iterate top-down over the old trace until the point where they differ, emitting exit events.
290    for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
291         old_it != old_rit.base(); ++old_it) {
292      LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
293                          thread_clock_diff, wall_clock_diff);
294    }
295    // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
296    for (; rit != stack_trace->rend(); ++rit) {
297      LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
298                          thread_clock_diff, wall_clock_diff);
299    }
300    FreeStackTrace(old_stack_trace);
301  }
302}
303
304void* Trace::RunSamplingThread(void* arg) {
305  Runtime* runtime = Runtime::Current();
306  intptr_t interval_us = reinterpret_cast<intptr_t>(arg);
307  CHECK_GE(interval_us, 0);
308  CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
309                                     !runtime->IsAotCompiler()));
310
311  while (true) {
312    usleep(interval_us);
313    ATRACE_BEGIN("Profile sampling");
314    Thread* self = Thread::Current();
315    Trace* the_trace;
316    {
317      MutexLock mu(self, *Locks::trace_lock_);
318      the_trace = the_trace_;
319      if (the_trace == nullptr) {
320        break;
321      }
322    }
323
324    runtime->GetThreadList()->SuspendAll(__FUNCTION__);
325    {
326      MutexLock mu(self, *Locks::thread_list_lock_);
327      runtime->GetThreadList()->ForEach(GetSample, the_trace);
328    }
329    runtime->GetThreadList()->ResumeAll();
330    ATRACE_END();
331  }
332
333  runtime->DetachCurrentThread();
334  return nullptr;
335}
336
337void Trace::Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags,
338                  TraceOutputMode output_mode, TraceMode trace_mode, int interval_us) {
339  Thread* self = Thread::Current();
340  {
341    MutexLock mu(self, *Locks::trace_lock_);
342    if (the_trace_ != nullptr) {
343      LOG(ERROR) << "Trace already in progress, ignoring this request";
344      return;
345    }
346  }
347
348  // Check interval if sampling is enabled
349  if (trace_mode == TraceMode::kSampling && interval_us <= 0) {
350    LOG(ERROR) << "Invalid sampling interval: " << interval_us;
351    ScopedObjectAccess soa(self);
352    ThrowRuntimeException("Invalid sampling interval: %d", interval_us);
353    return;
354  }
355
356  // Open trace file if not going directly to ddms.
357  std::unique_ptr<File> trace_file;
358  if (output_mode != TraceOutputMode::kDDMS) {
359    if (trace_fd < 0) {
360      trace_file.reset(OS::CreateEmptyFile(trace_filename));
361    } else {
362      trace_file.reset(new File(trace_fd, "tracefile"));
363      trace_file->DisableAutoClose();
364    }
365    if (trace_file.get() == nullptr) {
366      PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
367      ScopedObjectAccess soa(self);
368      ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
369      return;
370    }
371  }
372
373  Runtime* runtime = Runtime::Current();
374
375  // Enable count of allocs if specified in the flags.
376  bool enable_stats = false;
377
378  runtime->GetThreadList()->SuspendAll(__FUNCTION__);
379
380  // Create Trace object.
381  {
382    MutexLock mu(self, *Locks::trace_lock_);
383    if (the_trace_ != nullptr) {
384      LOG(ERROR) << "Trace already in progress, ignoring this request";
385    } else {
386      enable_stats = (flags && kTraceCountAllocs) != 0;
387      the_trace_ = new Trace(trace_file.release(), trace_filename, buffer_size, flags, output_mode,
388                             trace_mode);
389      if (trace_mode == TraceMode::kSampling) {
390        CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, nullptr, &RunSamplingThread,
391                                            reinterpret_cast<void*>(interval_us)),
392                                            "Sampling profiler thread");
393        the_trace_->interval_us_ = interval_us;
394      } else {
395        runtime->GetInstrumentation()->AddListener(the_trace_,
396                                                   instrumentation::Instrumentation::kMethodEntered |
397                                                   instrumentation::Instrumentation::kMethodExited |
398                                                   instrumentation::Instrumentation::kMethodUnwind);
399        // TODO: In full-PIC mode, we don't need to fully deopt.
400        runtime->GetInstrumentation()->EnableMethodTracing(kTracerInstrumentationKey);
401      }
402    }
403  }
404
405  runtime->GetThreadList()->ResumeAll();
406
407  // Can't call this when holding the mutator lock.
408  if (enable_stats) {
409    runtime->SetStatsEnabled(true);
410  }
411}
412
413void Trace::StopTracing(bool finish_tracing, bool flush_file) {
414  bool stop_alloc_counting = false;
415  Runtime* const runtime = Runtime::Current();
416  Trace* the_trace = nullptr;
417  pthread_t sampling_pthread = 0U;
418  {
419    MutexLock mu(Thread::Current(), *Locks::trace_lock_);
420    if (the_trace_ == nullptr) {
421      LOG(ERROR) << "Trace stop requested, but no trace currently running";
422    } else {
423      the_trace = the_trace_;
424      the_trace_ = nullptr;
425      sampling_pthread = sampling_pthread_;
426    }
427  }
428  // Make sure that we join before we delete the trace since we don't want to have
429  // the sampling thread access a stale pointer. This finishes since the sampling thread exits when
430  // the_trace_ is null.
431  if (sampling_pthread != 0U) {
432    CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, nullptr), "sampling thread shutdown");
433    sampling_pthread_ = 0U;
434  }
435  runtime->GetThreadList()->SuspendAll(__FUNCTION__);
436
437  if (the_trace != nullptr) {
438    stop_alloc_counting = (the_trace->flags_ & Trace::kTraceCountAllocs) != 0;
439    if (finish_tracing) {
440      the_trace->FinishTracing();
441    }
442
443    if (the_trace->trace_mode_ == TraceMode::kSampling) {
444      MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
445      runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, nullptr);
446    } else {
447      runtime->GetInstrumentation()->DisableMethodTracing(kTracerInstrumentationKey);
448      runtime->GetInstrumentation()->RemoveListener(
449          the_trace, instrumentation::Instrumentation::kMethodEntered |
450          instrumentation::Instrumentation::kMethodExited |
451          instrumentation::Instrumentation::kMethodUnwind);
452    }
453    if (the_trace->trace_file_.get() != nullptr) {
454      // Do not try to erase, so flush and close explicitly.
455      if (flush_file) {
456        if (the_trace->trace_file_->Flush() != 0) {
457          PLOG(ERROR) << "Could not flush trace file.";
458        }
459      } else {
460        the_trace->trace_file_->MarkUnchecked();  // Do not trigger guard.
461      }
462      if (the_trace->trace_file_->Close() != 0) {
463        PLOG(ERROR) << "Could not close trace file.";
464      }
465    }
466    delete the_trace;
467  }
468  runtime->GetThreadList()->ResumeAll();
469  if (stop_alloc_counting) {
470    // Can be racy since SetStatsEnabled is not guarded by any locks.
471    runtime->SetStatsEnabled(false);
472  }
473}
474
475void Trace::Abort() {
476  // Do not write anything anymore.
477  StopTracing(false, false);
478}
479
480void Trace::Stop() {
481  // Finish writing.
482  StopTracing(true, true);
483}
484
485void Trace::Shutdown() {
486  if (GetMethodTracingMode() != kTracingInactive) {
487    Stop();
488  }
489}
490
491void Trace::Pause() {
492  bool stop_alloc_counting = false;
493  Runtime* runtime = Runtime::Current();
494  Trace* the_trace = nullptr;
495
496  pthread_t sampling_pthread = 0U;
497  {
498    MutexLock mu(Thread::Current(), *Locks::trace_lock_);
499    if (the_trace_ == nullptr) {
500      LOG(ERROR) << "Trace pause requested, but no trace currently running";
501      return;
502    } else {
503      the_trace = the_trace_;
504      sampling_pthread = sampling_pthread_;
505    }
506  }
507
508  if (sampling_pthread != 0U) {
509    {
510      MutexLock mu(Thread::Current(), *Locks::trace_lock_);
511      the_trace_ = nullptr;
512    }
513    CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, nullptr), "sampling thread shutdown");
514    sampling_pthread_ = 0U;
515    {
516      MutexLock mu(Thread::Current(), *Locks::trace_lock_);
517      the_trace_ = the_trace;
518    }
519  }
520
521  if (the_trace != nullptr) {
522    runtime->GetThreadList()->SuspendAll(__FUNCTION__);
523    stop_alloc_counting = (the_trace->flags_ & Trace::kTraceCountAllocs) != 0;
524
525    if (the_trace->trace_mode_ == TraceMode::kSampling) {
526      MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
527      runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, nullptr);
528    } else {
529      runtime->GetInstrumentation()->DisableMethodTracing(kTracerInstrumentationKey);
530      runtime->GetInstrumentation()->RemoveListener(the_trace,
531                                                    instrumentation::Instrumentation::kMethodEntered |
532                                                    instrumentation::Instrumentation::kMethodExited |
533                                                    instrumentation::Instrumentation::kMethodUnwind);
534    }
535    runtime->GetThreadList()->ResumeAll();
536  }
537
538  if (stop_alloc_counting) {
539    // Can be racy since SetStatsEnabled is not guarded by any locks.
540    Runtime::Current()->SetStatsEnabled(false);
541  }
542}
543
544void Trace::Resume() {
545  Thread* self = Thread::Current();
546  Trace* the_trace;
547  {
548    MutexLock mu(self, *Locks::trace_lock_);
549    if (the_trace_ == nullptr) {
550      LOG(ERROR) << "No trace to resume (or sampling mode), ignoring this request";
551      return;
552    }
553    the_trace = the_trace_;
554  }
555
556  Runtime* runtime = Runtime::Current();
557
558  // Enable count of allocs if specified in the flags.
559  bool enable_stats = (the_trace->flags_ && kTraceCountAllocs) != 0;
560
561  runtime->GetThreadList()->SuspendAll(__FUNCTION__);
562
563  // Reenable.
564  if (the_trace->trace_mode_ == TraceMode::kSampling) {
565    CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, nullptr, &RunSamplingThread,
566        reinterpret_cast<void*>(the_trace->interval_us_)), "Sampling profiler thread");
567  } else {
568    runtime->GetInstrumentation()->AddListener(the_trace,
569                                               instrumentation::Instrumentation::kMethodEntered |
570                                               instrumentation::Instrumentation::kMethodExited |
571                                               instrumentation::Instrumentation::kMethodUnwind);
572    // TODO: In full-PIC mode, we don't need to fully deopt.
573    runtime->GetInstrumentation()->EnableMethodTracing(kTracerInstrumentationKey);
574  }
575
576  runtime->GetThreadList()->ResumeAll();
577
578  // Can't call this when holding the mutator lock.
579  if (enable_stats) {
580    runtime->SetStatsEnabled(true);
581  }
582}
583
584TracingMode Trace::GetMethodTracingMode() {
585  MutexLock mu(Thread::Current(), *Locks::trace_lock_);
586  if (the_trace_ == nullptr) {
587    return kTracingInactive;
588  } else {
589    switch (the_trace_->trace_mode_) {
590      case TraceMode::kSampling:
591        return kSampleProfilingActive;
592      case TraceMode::kMethodTracing:
593        return kMethodTracingActive;
594    }
595    LOG(FATAL) << "Unreachable";
596    UNREACHABLE();
597  }
598}
599
600static constexpr size_t kMinBufSize = 18U;  // Trace header is up to 18B.
601
602Trace::Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags,
603             TraceOutputMode output_mode, TraceMode trace_mode)
604    : trace_file_(trace_file),
605      buf_(new uint8_t[std::max(kMinBufSize, buffer_size)]()),
606      flags_(flags), trace_output_mode_(output_mode), trace_mode_(trace_mode),
607      clock_source_(default_clock_source_),
608      buffer_size_(std::max(kMinBufSize, buffer_size)),
609      start_time_(MicroTime()), clock_overhead_ns_(GetClockOverheadNanoSeconds()), cur_offset_(0),
610      overflow_(false), interval_us_(0), streaming_lock_(nullptr) {
611  uint16_t trace_version = GetTraceVersion(clock_source_);
612  if (output_mode == TraceOutputMode::kStreaming) {
613    trace_version |= 0xF0U;
614  }
615  // Set up the beginning of the trace.
616  memset(buf_.get(), 0, kTraceHeaderLength);
617  Append4LE(buf_.get(), kTraceMagicValue);
618  Append2LE(buf_.get() + 4, trace_version);
619  Append2LE(buf_.get() + 6, kTraceHeaderLength);
620  Append8LE(buf_.get() + 8, start_time_);
621  if (trace_version >= kTraceVersionDualClock) {
622    uint16_t record_size = GetRecordSize(clock_source_);
623    Append2LE(buf_.get() + 16, record_size);
624  }
625  static_assert(18 <= kMinBufSize, "Minimum buffer size not large enough for trace header");
626
627  // Update current offset.
628  cur_offset_.StoreRelaxed(kTraceHeaderLength);
629
630  if (output_mode == TraceOutputMode::kStreaming) {
631    streaming_file_name_ = trace_name;
632    streaming_lock_ = new Mutex("tracing lock");
633    seen_threads_.reset(new ThreadIDBitSet());
634  }
635}
636
637Trace::~Trace() {
638  delete streaming_lock_;
639}
640
641static void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
642    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
643  uint8_t* ptr = buf + kTraceHeaderLength;
644  uint8_t* end = buf + buf_size;
645
646  while (ptr < end) {
647    uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
648    mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
649    TraceAction action = DecodeTraceAction(tmid);
650    LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
651    ptr += GetRecordSize(clock_source);
652  }
653}
654
655static void GetVisitedMethodsFromBitSets(
656    const std::map<mirror::DexCache*, DexIndexBitSet*>& seen_methods,
657    std::set<mirror::ArtMethod*>* visited_methods) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
658  for (auto& e : seen_methods) {
659    DexIndexBitSet* bit_set = e.second;
660    for (uint32_t i = 0; i < bit_set->size(); ++i) {
661      if ((*bit_set)[i]) {
662        visited_methods->insert(e.first->GetResolvedMethod(i));
663      }
664    }
665  }
666}
667
668void Trace::FinishTracing() {
669  size_t final_offset = 0;
670
671  std::set<mirror::ArtMethod*> visited_methods;
672  if (trace_output_mode_ == TraceOutputMode::kStreaming) {
673    // Write the secondary file with all the method names.
674    GetVisitedMethodsFromBitSets(seen_methods_, &visited_methods);
675
676    // Clean up.
677    for (auto& e : seen_methods_) {
678      delete e.second;
679    }
680  } else {
681    final_offset = cur_offset_.LoadRelaxed();
682    GetVisitedMethods(final_offset, &visited_methods);
683  }
684
685  // Compute elapsed time.
686  uint64_t elapsed = MicroTime() - start_time_;
687
688  std::ostringstream os;
689
690  os << StringPrintf("%cversion\n", kTraceTokenChar);
691  os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
692  os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
693  if (UseThreadCpuClock()) {
694    if (UseWallClock()) {
695      os << StringPrintf("clock=dual\n");
696    } else {
697      os << StringPrintf("clock=thread-cpu\n");
698    }
699  } else {
700    os << StringPrintf("clock=wall\n");
701  }
702  os << StringPrintf("elapsed-time-usec=%" PRIu64 "\n", elapsed);
703  if (trace_output_mode_ != TraceOutputMode::kStreaming) {
704    size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
705    os << StringPrintf("num-method-calls=%zd\n", num_records);
706  }
707  os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns_);
708  os << StringPrintf("vm=art\n");
709  os << StringPrintf("pid=%d\n", getpid());
710  if ((flags_ & kTraceCountAllocs) != 0) {
711    os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
712    os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
713    os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
714  }
715  os << StringPrintf("%cthreads\n", kTraceTokenChar);
716  DumpThreadList(os);
717  os << StringPrintf("%cmethods\n", kTraceTokenChar);
718  DumpMethodList(os, visited_methods);
719  os << StringPrintf("%cend\n", kTraceTokenChar);
720  std::string header(os.str());
721
722  if (trace_output_mode_ == TraceOutputMode::kStreaming) {
723    File file;
724    if (!file.Open(streaming_file_name_ + ".sec", O_CREAT | O_WRONLY)) {
725      LOG(WARNING) << "Could not open secondary trace file!";
726      return;
727    }
728    if (!file.WriteFully(header.c_str(), header.length())) {
729      file.Erase();
730      std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
731      PLOG(ERROR) << detail;
732      ThrowRuntimeException("%s", detail.c_str());
733    }
734    if (file.FlushCloseOrErase() != 0) {
735      PLOG(ERROR) << "Could not write secondary file";
736    }
737  } else {
738    if (trace_file_.get() == nullptr) {
739      iovec iov[2];
740      iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
741      iov[0].iov_len = header.length();
742      iov[1].iov_base = buf_.get();
743      iov[1].iov_len = final_offset;
744      Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
745      const bool kDumpTraceInfo = false;
746      if (kDumpTraceInfo) {
747        LOG(INFO) << "Trace sent:\n" << header;
748        DumpBuf(buf_.get(), final_offset, clock_source_);
749      }
750    } else {
751      if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
752          !trace_file_->WriteFully(buf_.get(), final_offset)) {
753        std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
754        PLOG(ERROR) << detail;
755        ThrowRuntimeException("%s", detail.c_str());
756      }
757    }
758  }
759}
760
761void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
762                       mirror::ArtMethod* method, uint32_t new_dex_pc) {
763  UNUSED(thread, this_object, method, new_dex_pc);
764  // We're not recorded to listen to this kind of event, so complain.
765  LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
766}
767
768void Trace::FieldRead(Thread* thread, mirror::Object* this_object,
769                       mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field)
770    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
771  UNUSED(thread, this_object, method, dex_pc, field);
772  // We're not recorded to listen to this kind of event, so complain.
773  LOG(ERROR) << "Unexpected field read event in tracing " << PrettyMethod(method) << " " << dex_pc;
774}
775
776void Trace::FieldWritten(Thread* thread, mirror::Object* this_object,
777                          mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field,
778                          const JValue& field_value)
779    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
780  UNUSED(thread, this_object, method, dex_pc, field, field_value);
781  // We're not recorded to listen to this kind of event, so complain.
782  LOG(ERROR) << "Unexpected field write event in tracing " << PrettyMethod(method) << " " << dex_pc;
783}
784
785void Trace::MethodEntered(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
786                          mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
787  uint32_t thread_clock_diff = 0;
788  uint32_t wall_clock_diff = 0;
789  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
790  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
791                      thread_clock_diff, wall_clock_diff);
792}
793
794void Trace::MethodExited(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
795                         mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED,
796                         const JValue& return_value ATTRIBUTE_UNUSED) {
797  uint32_t thread_clock_diff = 0;
798  uint32_t wall_clock_diff = 0;
799  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
800  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
801                      thread_clock_diff, wall_clock_diff);
802}
803
804void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
805                         mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
806  uint32_t thread_clock_diff = 0;
807  uint32_t wall_clock_diff = 0;
808  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
809  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
810                      thread_clock_diff, wall_clock_diff);
811}
812
813void Trace::ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
814    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
815  UNUSED(thread, exception_object);
816  LOG(ERROR) << "Unexpected exception caught event in tracing";
817}
818
819void Trace::BackwardBranch(Thread* /*thread*/, mirror::ArtMethod* method,
820                           int32_t /*dex_pc_offset*/)
821      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
822  LOG(ERROR) << "Unexpected backward branch event in tracing" << PrettyMethod(method);
823}
824
825void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
826  if (UseThreadCpuClock()) {
827    uint64_t clock_base = thread->GetTraceClockBase();
828    if (UNLIKELY(clock_base == 0)) {
829      // First event, record the base time in the map.
830      uint64_t time = thread->GetCpuMicroTime();
831      thread->SetTraceClockBase(time);
832    } else {
833      *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
834    }
835  }
836  if (UseWallClock()) {
837    *wall_clock_diff = MicroTime() - start_time_;
838  }
839}
840
841bool Trace::RegisterMethod(mirror::ArtMethod* method) {
842  mirror::DexCache* dex_cache = method->GetDexCache();
843  if (dex_cache->GetResolvedMethod(method->GetDexMethodIndex()) != method) {
844    DCHECK(dex_cache->GetResolvedMethod(method->GetDexMethodIndex()) == nullptr);
845    dex_cache->SetResolvedMethod(method->GetDexMethodIndex(), method);
846  }
847  if (seen_methods_.find(dex_cache) == seen_methods_.end()) {
848    seen_methods_.insert(std::make_pair(dex_cache, new DexIndexBitSet()));
849  }
850  DexIndexBitSet* bit_set = seen_methods_.find(dex_cache)->second;
851  if (!(*bit_set)[method->GetDexMethodIndex()]) {
852    bit_set->set(method->GetDexMethodIndex());
853    return true;
854  }
855  return false;
856}
857
858bool Trace::RegisterThread(Thread* thread) {
859  pid_t tid = thread->GetTid();
860  CHECK_LT(0U, static_cast<uint32_t>(tid));
861  CHECK_LT(static_cast<uint32_t>(tid), 65536U);
862
863  if (!(*seen_threads_)[tid]) {
864    seen_threads_->set(tid);
865    return true;
866  }
867  return false;
868}
869
870static std::string GetMethodLine(mirror::ArtMethod* method)
871    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
872  return StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
873      PrettyDescriptor(method->GetDeclaringClassDescriptor()).c_str(), method->GetName(),
874      method->GetSignature().ToString().c_str(), method->GetDeclaringClassSourceFile());
875}
876
877void Trace::WriteToBuf(const uint8_t* src, size_t src_size) {
878  int32_t old_offset = cur_offset_.LoadRelaxed();
879  int32_t new_offset = old_offset + static_cast<int32_t>(src_size);
880  if (dchecked_integral_cast<size_t>(new_offset) > buffer_size_) {
881    // Flush buffer.
882    if (!trace_file_->WriteFully(buf_.get(), old_offset)) {
883      PLOG(WARNING) << "Failed streaming a tracing event.";
884    }
885
886    // Check whether the data is too large for the buffer, then write immediately.
887    if (src_size >= buffer_size_) {
888      if (!trace_file_->WriteFully(src, src_size)) {
889        PLOG(WARNING) << "Failed streaming a tracing event.";
890      }
891      cur_offset_.StoreRelease(0);  // Buffer is empty now.
892      return;
893    }
894
895    old_offset = 0;
896    new_offset = static_cast<int32_t>(src_size);
897  }
898  cur_offset_.StoreRelease(new_offset);
899  // Fill in data.
900  memcpy(buf_.get() + old_offset, src, src_size);
901}
902
903void Trace::LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
904                                instrumentation::Instrumentation::InstrumentationEvent event,
905                                uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
906  // Advance cur_offset_ atomically.
907  int32_t new_offset;
908  int32_t old_offset = 0;
909
910  // We do a busy loop here trying to acquire the next offset.
911  if (trace_output_mode_ != TraceOutputMode::kStreaming) {
912    do {
913      old_offset = cur_offset_.LoadRelaxed();
914      new_offset = old_offset + GetRecordSize(clock_source_);
915      if (static_cast<size_t>(new_offset) > buffer_size_) {
916        overflow_ = true;
917        return;
918      }
919    } while (!cur_offset_.CompareExchangeWeakSequentiallyConsistent(old_offset, new_offset));
920  }
921
922  TraceAction action = kTraceMethodEnter;
923  switch (event) {
924    case instrumentation::Instrumentation::kMethodEntered:
925      action = kTraceMethodEnter;
926      break;
927    case instrumentation::Instrumentation::kMethodExited:
928      action = kTraceMethodExit;
929      break;
930    case instrumentation::Instrumentation::kMethodUnwind:
931      action = kTraceUnroll;
932      break;
933    default:
934      UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
935  }
936
937  uint32_t method_value = EncodeTraceMethodAndAction(method, action);
938
939  // Write data
940  uint8_t* ptr;
941  static constexpr size_t kPacketSize = 14U;  // The maximum size of data in a packet.
942  uint8_t stack_buf[kPacketSize];             // Space to store a packet when in streaming mode.
943  if (trace_output_mode_ == TraceOutputMode::kStreaming) {
944    ptr = stack_buf;
945  } else {
946    ptr = buf_.get() + old_offset;
947  }
948
949  Append2LE(ptr, thread->GetTid());
950  Append4LE(ptr + 2, method_value);
951  ptr += 6;
952
953  if (UseThreadCpuClock()) {
954    Append4LE(ptr, thread_clock_diff);
955    ptr += 4;
956  }
957  if (UseWallClock()) {
958    Append4LE(ptr, wall_clock_diff);
959  }
960  static_assert(kPacketSize == 2 + 4 + 4 + 4, "Packet size incorrect.");
961
962  if (trace_output_mode_ == TraceOutputMode::kStreaming) {
963    MutexLock mu(Thread::Current(), *streaming_lock_);  // To serialize writing.
964    if (RegisterMethod(method)) {
965      // Write a special block with the name.
966      std::string method_line(GetMethodLine(method));
967      uint8_t buf2[5];
968      Append2LE(buf2, 0);
969      buf2[2] = kOpNewMethod;
970      Append2LE(buf2 + 3, static_cast<uint16_t>(method_line.length()));
971      WriteToBuf(buf2, sizeof(buf2));
972      WriteToBuf(reinterpret_cast<const uint8_t*>(method_line.c_str()), method_line.length());
973    }
974    if (RegisterThread(thread)) {
975      // It might be better to postpone this. Threads might not have received names...
976      std::string thread_name;
977      thread->GetThreadName(thread_name);
978      uint8_t buf2[7];
979      Append2LE(buf2, 0);
980      buf2[2] = kOpNewThread;
981      Append2LE(buf2 + 3, static_cast<uint16_t>(thread->GetTid()));
982      Append2LE(buf2 + 5, static_cast<uint16_t>(thread_name.length()));
983      WriteToBuf(buf2, sizeof(buf2));
984      WriteToBuf(reinterpret_cast<const uint8_t*>(thread_name.c_str()), thread_name.length());
985    }
986    WriteToBuf(stack_buf, sizeof(stack_buf));
987  }
988}
989
990void Trace::GetVisitedMethods(size_t buf_size,
991                              std::set<mirror::ArtMethod*>* visited_methods) {
992  uint8_t* ptr = buf_.get() + kTraceHeaderLength;
993  uint8_t* end = buf_.get() + buf_size;
994
995  while (ptr < end) {
996    uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
997    mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
998    visited_methods->insert(method);
999    ptr += GetRecordSize(clock_source_);
1000  }
1001}
1002
1003void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
1004  for (const auto& method : visited_methods) {
1005    os << GetMethodLine(method);
1006  }
1007}
1008
1009static void DumpThread(Thread* t, void* arg) {
1010  std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
1011  std::string name;
1012  t->GetThreadName(name);
1013  os << t->GetTid() << "\t" << name << "\n";
1014}
1015
1016void Trace::DumpThreadList(std::ostream& os) {
1017  Thread* self = Thread::Current();
1018  for (auto it : exited_threads_) {
1019    os << it.first << "\t" << it.second << "\n";
1020  }
1021  Locks::thread_list_lock_->AssertNotHeld(self);
1022  MutexLock mu(self, *Locks::thread_list_lock_);
1023  Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
1024}
1025
1026void Trace::StoreExitingThreadInfo(Thread* thread) {
1027  MutexLock mu(thread, *Locks::trace_lock_);
1028  if (the_trace_ != nullptr) {
1029    std::string name;
1030    thread->GetThreadName(name);
1031    // The same thread/tid may be used multiple times. As SafeMap::Put does not allow to override
1032    // a previous mapping, use SafeMap::Overwrite.
1033    the_trace_->exited_threads_.Overwrite(thread->GetTid(), name);
1034  }
1035}
1036
1037Trace::TraceOutputMode Trace::GetOutputMode() {
1038  MutexLock mu(Thread::Current(), *Locks::trace_lock_);
1039  CHECK(the_trace_ != nullptr) << "Trace output mode requested, but no trace currently running";
1040  return the_trace_->trace_output_mode_;
1041}
1042
1043Trace::TraceMode Trace::GetMode() {
1044  MutexLock mu(Thread::Current(), *Locks::trace_lock_);
1045  CHECK(the_trace_ != nullptr) << "Trace mode requested, but no trace currently running";
1046  return the_trace_->trace_mode_;
1047}
1048
1049size_t Trace::GetBufferSize() {
1050  MutexLock mu(Thread::Current(), *Locks::trace_lock_);
1051  CHECK(the_trace_ != nullptr) << "Trace mode requested, but no trace currently running";
1052  return the_trace_->buffer_size_;
1053}
1054
1055}  // namespace art
1056