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