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