trace.h revision 241a9588c6d7d0fcb4c92da40c7141863930083a
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#ifndef ART_RUNTIME_TRACE_H_ 18#define ART_RUNTIME_TRACE_H_ 19 20#include <bitset> 21#include <map> 22#include <memory> 23#include <ostream> 24#include <set> 25#include <string> 26#include <vector> 27 28#include "atomic.h" 29#include "base/macros.h" 30#include "globals.h" 31#include "instrumentation.h" 32#include "os.h" 33#include "safe_map.h" 34 35namespace art { 36 37namespace mirror { 38 class ArtMethod; 39 class DexCache; 40} // namespace mirror 41 42class ArtField; 43class Thread; 44 45using DexIndexBitSet = std::bitset<65536>; 46using ThreadIDBitSet = std::bitset<65536>; 47 48enum TracingMode { 49 kTracingInactive, 50 kMethodTracingActive, 51 kSampleProfilingActive, 52}; 53 54class Trace FINAL : public instrumentation::InstrumentationListener { 55 public: 56 enum TraceFlag { 57 kTraceCountAllocs = 1, 58 }; 59 60 enum class TraceOutputMode { 61 kFile, 62 kDDMS, 63 kStreaming 64 }; 65 66 enum class TraceMode { 67 kMethodTracing, 68 kSampling 69 }; 70 71 ~Trace(); 72 73 static void SetDefaultClockSource(TraceClockSource clock_source); 74 75 static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags, 76 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us) 77 LOCKS_EXCLUDED(Locks::mutator_lock_, 78 Locks::thread_list_lock_, 79 Locks::thread_suspend_count_lock_, 80 Locks::trace_lock_); 81 static void Pause() LOCKS_EXCLUDED(Locks::trace_lock_, Locks::thread_list_lock_); 82 static void Resume() LOCKS_EXCLUDED(Locks::trace_lock_); 83 84 // Stop tracing. This will finish the trace and write it to file/send it via DDMS. 85 static void Stop() 86 LOCKS_EXCLUDED(Locks::mutator_lock_, 87 Locks::thread_list_lock_, 88 Locks::trace_lock_); 89 // Abort tracing. This will just stop tracing and *not* write/send the collected data. 90 static void Abort() 91 LOCKS_EXCLUDED(Locks::mutator_lock_, 92 Locks::thread_list_lock_, 93 Locks::trace_lock_); 94 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_); 95 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_); 96 97 bool UseWallClock(); 98 bool UseThreadCpuClock(); 99 void MeasureClockOverhead(); 100 uint32_t GetClockOverheadNanoSeconds(); 101 102 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace) 103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 104 105 // InstrumentationListener implementation. 106 void MethodEntered(Thread* thread, mirror::Object* this_object, 107 mirror::ArtMethod* method, uint32_t dex_pc) 108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 109 void MethodExited(Thread* thread, mirror::Object* this_object, 110 mirror::ArtMethod* method, uint32_t dex_pc, 111 const JValue& return_value) 112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 113 void MethodUnwind(Thread* thread, mirror::Object* this_object, 114 mirror::ArtMethod* method, uint32_t dex_pc) 115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 116 void DexPcMoved(Thread* thread, mirror::Object* this_object, 117 mirror::ArtMethod* method, uint32_t new_dex_pc) 118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 119 void FieldRead(Thread* thread, mirror::Object* this_object, 120 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field) 121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 122 void FieldWritten(Thread* thread, mirror::Object* this_object, 123 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field, 124 const JValue& field_value) 125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 126 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object) 127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 128 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset) 129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; 130 // Reuse an old stack trace if it exists, otherwise allocate a new one. 131 static std::vector<mirror::ArtMethod*>* AllocStackTrace(); 132 // Clear and store an old stack trace for later use. 133 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace); 134 // Save id and name of a thread before it exits. 135 static void StoreExitingThreadInfo(Thread* thread); 136 137 static TraceOutputMode GetOutputMode() LOCKS_EXCLUDED(Locks::trace_lock_); 138 static TraceMode GetMode() LOCKS_EXCLUDED(Locks::trace_lock_); 139 static size_t GetBufferSize() LOCKS_EXCLUDED(Locks::trace_lock_); 140 141 private: 142 Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags, 143 TraceOutputMode output_mode, TraceMode trace_mode); 144 145 // The sampling interval in microseconds is passed as an argument. 146 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_); 147 148 static void StopTracing(bool finish_tracing, bool flush_file); 149 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 150 151 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff); 152 153 void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method, 154 instrumentation::Instrumentation::InstrumentationEvent event, 155 uint32_t thread_clock_diff, uint32_t wall_clock_diff) 156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 157 158 // Methods to output traced methods and threads. 159 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods); 160 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) 161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 162 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); 163 164 // Methods to register seen entitites in streaming mode. The methods return true if the entity 165 // is newly discovered. 166 bool RegisterMethod(mirror::ArtMethod* method) 167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_); 168 bool RegisterThread(Thread* thread) 169 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_); 170 171 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock 172 // annotation. 173 void WriteToBuf(const uint8_t* src, size_t src_size) 174 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_); 175 176 // Singleton instance of the Trace or null when no method tracing is active. 177 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); 178 179 // The default profiler clock source. 180 static TraceClockSource default_clock_source_; 181 182 // Sampling thread, non-zero when sampling. 183 static pthread_t sampling_pthread_; 184 185 // Used to remember an unused stack trace to avoid re-allocation during sampling. 186 static std::unique_ptr<std::vector<mirror::ArtMethod*>> temp_stack_trace_; 187 188 // File to write trace data out to, null if direct to ddms. 189 std::unique_ptr<File> trace_file_; 190 191 // Buffer to store trace data. 192 std::unique_ptr<uint8_t[]> buf_; 193 194 // Flags enabling extra tracing of things such as alloc counts. 195 const int flags_; 196 197 // The kind of output for this tracing. 198 const TraceOutputMode trace_output_mode_; 199 200 // The tracing method. 201 const TraceMode trace_mode_; 202 203 const TraceClockSource clock_source_; 204 205 // Size of buf_. 206 const size_t buffer_size_; 207 208 // Time trace was created. 209 const uint64_t start_time_; 210 211 // Clock overhead. 212 const uint32_t clock_overhead_ns_; 213 214 // Offset into buf_. 215 AtomicInteger cur_offset_; 216 217 // Did we overflow the buffer recording traces? 218 bool overflow_; 219 220 // Map of thread ids and names that have already exited. 221 SafeMap<pid_t, std::string> exited_threads_; 222 223 // Sampling profiler sampling interval. 224 int interval_us_; 225 226 // Streaming mode data. 227 std::string streaming_file_name_; 228 Mutex* streaming_lock_; 229 std::map<mirror::DexCache*, DexIndexBitSet*> seen_methods_; 230 std::unique_ptr<ThreadIDBitSet> seen_threads_; 231 232 DISALLOW_COPY_AND_ASSIGN(Trace); 233}; 234 235} // namespace art 236 237#endif // ART_RUNTIME_TRACE_H_ 238