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