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