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