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 <unordered_map> 27 #include <vector> 28 29 #include "base/atomic.h" 30 #include "base/locks.h" 31 #include "base/macros.h" 32 #include "base/os.h" 33 #include "base/safe_map.h" 34 #include "instrumentation.h" 35 #include "runtime_globals.h" 36 37 namespace unix_file { 38 class FdFile; 39 } // namespace unix_file 40 41 namespace art { 42 43 class ArtField; 44 class ArtMethod; 45 class DexFile; 46 class LOCKABLE Mutex; 47 class ShadowFrame; 48 class Thread; 49 50 using DexIndexBitSet = std::bitset<65536>; 51 52 constexpr size_t kMaxThreadIdNumber = kIsTargetBuild ? 65536U : 1048576U; 53 using ThreadIDBitSet = std::bitset<kMaxThreadIdNumber>; 54 55 enum TracingMode { 56 kTracingInactive, 57 kMethodTracingActive, // Trace activity synchronous with method progress. 58 kSampleProfilingActive, // Trace activity captured by sampling thread. 59 }; 60 std::ostream& operator<<(std::ostream& os, TracingMode rhs); 61 62 // File format: 63 // header 64 // record 0 65 // record 1 66 // ... 67 // 68 // Header format: 69 // u4 magic ('SLOW') 70 // u2 version 71 // u2 offset to data 72 // u8 start date/time in usec 73 // u2 record size in bytes (version >= 2 only) 74 // ... padding to 32 bytes 75 // 76 // Record format v1: 77 // u1 thread ID 78 // u4 method ID | method action 79 // u4 time delta since start, in usec 80 // 81 // Record format v2: 82 // u2 thread ID 83 // u4 method ID | method action 84 // u4 time delta since start, in usec 85 // 86 // Record format v3: 87 // u2 thread ID 88 // u4 method ID | method action 89 // u4 time delta since start, in usec 90 // u4 wall time since start, in usec (when clock == "dual" only) 91 // 92 // 32 bits of microseconds is 70 minutes. 93 // 94 // All values are stored in little-endian order. 95 96 enum TraceAction { 97 kTraceMethodEnter = 0x00, // method entry 98 kTraceMethodExit = 0x01, // method exit 99 kTraceUnroll = 0x02, // method exited by exception unrolling 100 // 0x03 currently unused 101 kTraceMethodActionMask = 0x03, // two bits 102 }; 103 104 // Class for recording event traces. Trace data is either collected 105 // synchronously during execution (TracingMode::kMethodTracingActive), 106 // or by a separate sampling thread (TracingMode::kSampleProfilingActive). 107 class Trace final : public instrumentation::InstrumentationListener { 108 public: 109 enum TraceFlag { 110 kTraceCountAllocs = 1, 111 }; 112 113 enum class TraceOutputMode { 114 kFile, 115 kDDMS, 116 kStreaming 117 }; 118 119 enum class TraceMode { 120 kMethodTracing, 121 kSampling 122 }; 123 124 ~Trace(); 125 126 static void SetDefaultClockSource(TraceClockSource clock_source); 127 128 static void Start(const char* trace_filename, 129 size_t buffer_size, 130 int flags, 131 TraceOutputMode output_mode, 132 TraceMode trace_mode, 133 int interval_us) 134 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 135 !Locks::trace_lock_); 136 static void Start(int trace_fd, 137 size_t buffer_size, 138 int flags, 139 TraceOutputMode output_mode, 140 TraceMode trace_mode, 141 int interval_us) 142 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 143 !Locks::trace_lock_); 144 static void Start(std::unique_ptr<unix_file::FdFile>&& file, 145 size_t buffer_size, 146 int flags, 147 TraceOutputMode output_mode, 148 TraceMode trace_mode, 149 int interval_us) 150 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 151 !Locks::trace_lock_); 152 static void StartDDMS(size_t buffer_size, 153 int flags, 154 TraceMode trace_mode, 155 int interval_us) 156 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 157 !Locks::trace_lock_); 158 159 // Stop tracing. This will finish the trace and write it to file/send it via DDMS. 160 static void Stop() 161 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 162 // Abort tracing. This will just stop tracing and *not* write/send the collected data. 163 static void Abort() 164 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 165 static void Shutdown() 166 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 167 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_); 168 169 bool UseWallClock(); 170 bool UseThreadCpuClock(); 171 void MeasureClockOverhead(); 172 uint32_t GetClockOverheadNanoSeconds(); 173 174 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace) 175 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); 176 177 // InstrumentationListener implementation. 178 void MethodEntered(Thread* thread, ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) 179 REQUIRES(!unique_methods_lock_, !streaming_lock_) override; 180 void MethodExited(Thread* thread, 181 ArtMethod* method, 182 instrumentation::OptionalFrame frame, 183 JValue& return_value) 184 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) 185 override; 186 void MethodUnwind(Thread* thread, 187 Handle<mirror::Object> this_object, 188 ArtMethod* method, 189 uint32_t dex_pc) 190 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) 191 override; 192 void DexPcMoved(Thread* thread, 193 Handle<mirror::Object> this_object, 194 ArtMethod* method, 195 uint32_t new_dex_pc) 196 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) 197 override; 198 void FieldRead(Thread* thread, 199 Handle<mirror::Object> this_object, 200 ArtMethod* method, 201 uint32_t dex_pc, 202 ArtField* field) 203 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; 204 void FieldWritten(Thread* thread, 205 Handle<mirror::Object> this_object, 206 ArtMethod* method, 207 uint32_t dex_pc, 208 ArtField* field, 209 const JValue& field_value) 210 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; 211 void ExceptionThrown(Thread* thread, 212 Handle<mirror::Throwable> exception_object) 213 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; 214 void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object) 215 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; 216 void Branch(Thread* thread, 217 ArtMethod* method, 218 uint32_t dex_pc, 219 int32_t dex_pc_offset) 220 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; 221 void WatchedFramePop(Thread* thread, const ShadowFrame& frame) 222 REQUIRES_SHARED(Locks::mutator_lock_) override; 223 // Reuse an old stack trace if it exists, otherwise allocate a new one. 224 static std::vector<ArtMethod*>* AllocStackTrace(); 225 // Clear and store an old stack trace for later use. 226 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace); 227 // Save id and name of a thread before it exits. 228 static void StoreExitingThreadInfo(Thread* thread); 229 230 static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_); 231 static TraceMode GetMode() REQUIRES(!Locks::trace_lock_); 232 static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_); 233 234 // Used by class linker to prevent class unloading. 235 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_); 236 237 private: 238 Trace(File* trace_file, 239 size_t buffer_size, 240 int flags, 241 TraceOutputMode output_mode, 242 TraceMode trace_mode); 243 244 // The sampling interval in microseconds is passed as an argument. 245 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_); 246 247 static void StopTracing(bool finish_tracing, bool flush_file) 248 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_) 249 // There is an annoying issue with static functions that create a new object and call into 250 // that object that causes them to not be able to tell that we don't currently hold the lock. 251 // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out 252 // how to annotate this. 253 NO_THREAD_SAFETY_ANALYSIS; 254 void FinishTracing() 255 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); 256 257 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff); 258 259 void LogMethodTraceEvent(Thread* thread, ArtMethod* method, 260 instrumentation::Instrumentation::InstrumentationEvent event, 261 uint32_t thread_clock_diff, uint32_t wall_clock_diff) 262 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); 263 264 // Methods to output traced methods and threads. 265 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods) 266 REQUIRES(!unique_methods_lock_); 267 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods) 268 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_); 269 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_); 270 271 // Methods to register seen entitites in streaming mode. The methods return true if the entity 272 // is newly discovered. 273 bool RegisterMethod(ArtMethod* method) 274 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_); 275 bool RegisterThread(Thread* thread) 276 REQUIRES(streaming_lock_); 277 278 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock 279 // annotation. 280 void WriteToBuf(const uint8_t* src, size_t src_size) 281 REQUIRES(streaming_lock_); 282 // Flush the main buffer to file. Used for streaming. Exposed here for lock annotation. 283 void FlushBuf() 284 REQUIRES(streaming_lock_); 285 286 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!unique_methods_lock_); 287 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action) 288 REQUIRES(!unique_methods_lock_); 289 ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!unique_methods_lock_); 290 std::string GetMethodLine(ArtMethod* method) REQUIRES(!unique_methods_lock_) 291 REQUIRES_SHARED(Locks::mutator_lock_); 292 293 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source) 294 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_); 295 296 // Singleton instance of the Trace or null when no method tracing is active. 297 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); 298 299 // The default profiler clock source. 300 static TraceClockSource default_clock_source_; 301 302 // Sampling thread, non-zero when sampling. 303 static pthread_t sampling_pthread_; 304 305 // Used to remember an unused stack trace to avoid re-allocation during sampling. 306 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_; 307 308 // File to write trace data out to, null if direct to ddms. 309 std::unique_ptr<File> trace_file_; 310 311 // Buffer to store trace data. In streaming mode, this is protected 312 // by the streaming_lock_. In non-streaming mode, reserved regions 313 // are atomically allocated (using cur_offset_) for log entries to 314 // be written. 315 std::unique_ptr<uint8_t[]> buf_; 316 317 // Flags enabling extra tracing of things such as alloc counts. 318 const int flags_; 319 320 // The kind of output for this tracing. 321 const TraceOutputMode trace_output_mode_; 322 323 // The tracing method. 324 const TraceMode trace_mode_; 325 326 const TraceClockSource clock_source_; 327 328 // Size of buf_. 329 const size_t buffer_size_; 330 331 // Time trace was created. 332 const uint64_t start_time_; 333 334 // Clock overhead. 335 const uint32_t clock_overhead_ns_; 336 337 // Offset into buf_. The field is atomic to allow multiple writers 338 // to concurrently reserve space in the buffer. The newly written 339 // buffer contents are not read without some other form of thread 340 // synchronization, such as suspending all potential writers or 341 // acquiring *streaming_lock_. Reading cur_offset_ is thus never 342 // used to ensure visibility of any other objects, and all accesses 343 // are memory_order_relaxed. 344 // 345 // All accesses to buf_ in streaming mode occur whilst holding the 346 // streaming lock. In streaming mode, the buffer may be written out 347 // so cur_offset_ can move forwards and backwards. 348 // 349 // When not in streaming mode, the buf_ writes can come from 350 // multiple threads when the trace mode is kMethodTracing. When 351 // trace mode is kSampling, writes only come from the sampling 352 // thread. 353 // 354 // Reads to the buffer happen after the event sources writing to the 355 // buffer have been shutdown and all stores have completed. The 356 // stores are made visible in StopTracing() when execution leaves 357 // the ScopedSuspendAll block. 358 AtomicInteger cur_offset_; 359 360 // Did we overflow the buffer recording traces? 361 bool overflow_; 362 363 // Map of thread ids and names that have already exited. 364 SafeMap<pid_t, std::string> exited_threads_; 365 366 // Sampling profiler sampling interval. 367 int interval_us_; 368 369 // Streaming mode data. 370 Mutex* streaming_lock_; 371 std::map<const DexFile*, DexIndexBitSet*> seen_methods_ GUARDED_BY(streaming_lock_); 372 std::unique_ptr<ThreadIDBitSet> seen_threads_ GUARDED_BY(streaming_lock_); 373 374 // Bijective map from ArtMethod* to index. 375 // Map from ArtMethod* to index in unique_methods_; 376 Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_); 377 std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_); 378 std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_); 379 380 DISALLOW_COPY_AND_ASSIGN(Trace); 381 }; 382 383 } // namespace art 384 385 #endif // ART_RUNTIME_TRACE_H_ 386