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