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/mutex.h" 33 #include "base/os.h" 34 #include "base/safe_map.h" 35 #include "instrumentation.h" 36 #include "runtime_globals.h" 37 38 namespace unix_file { 39 class FdFile; 40 } // namespace unix_file 41 42 namespace art { 43 44 class ArtField; 45 class ArtMethod; 46 class DexFile; 47 class ShadowFrame; 48 class Thread; 49 50 using DexIndexBitSet = std::bitset<65536>; 51 52 enum TracingMode { 53 kTracingInactive, 54 kMethodTracingActive, // Trace activity synchronous with method progress. 55 kSampleProfilingActive, // Trace activity captured by sampling thread. 56 }; 57 std::ostream& operator<<(std::ostream& os, TracingMode rhs); 58 59 // File format: 60 // header 61 // record 0 62 // record 1 63 // ... 64 // 65 // Header format: 66 // u4 magic ('SLOW') 67 // u2 version 68 // u2 offset to data 69 // u8 start date/time in usec 70 // u2 record size in bytes (version >= 2 only) 71 // ... padding to 32 bytes 72 // 73 // Record format v1: 74 // u1 thread ID 75 // u4 method ID | method action 76 // u4 time delta since start, in usec 77 // 78 // Record format v2: 79 // u2 thread ID 80 // u4 method ID | method action 81 // u4 time delta since start, in usec 82 // 83 // Record format v3: 84 // u2 thread ID 85 // u4 method ID | method action 86 // u4 time delta since start, in usec 87 // u4 wall time since start, in usec (when clock == "dual" only) 88 // 89 // 32 bits of microseconds is 70 minutes. 90 // 91 // All values are stored in little-endian order. 92 93 enum TraceAction { 94 kTraceMethodEnter = 0x00, // method entry 95 kTraceMethodExit = 0x01, // method exit 96 kTraceUnroll = 0x02, // method exited by exception unrolling 97 // 0x03 currently unused 98 kTraceMethodActionMask = 0x03, // two bits 99 }; 100 101 // Class for recording event traces. Trace data is either collected 102 // synchronously during execution (TracingMode::kMethodTracingActive), 103 // or by a separate sampling thread (TracingMode::kSampleProfilingActive). 104 class Trace final : public instrumentation::InstrumentationListener { 105 public: 106 enum TraceFlag { 107 kTraceCountAllocs = 0x001, 108 kTraceClockSourceWallClock = 0x010, 109 kTraceClockSourceThreadCpu = 0x100, 110 }; 111 112 enum class TraceOutputMode { 113 kFile, 114 kDDMS, 115 kStreaming 116 }; 117 118 enum class TraceMode { 119 kMethodTracing, 120 kSampling 121 }; 122 123 static void SetDefaultClockSource(TraceClockSource clock_source); 124 125 static void Start(const char* trace_filename, 126 size_t buffer_size, 127 int flags, 128 TraceOutputMode output_mode, 129 TraceMode trace_mode, 130 int interval_us) 131 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 132 !Locks::trace_lock_); 133 static void Start(int trace_fd, 134 size_t buffer_size, 135 int flags, 136 TraceOutputMode output_mode, 137 TraceMode trace_mode, 138 int interval_us) 139 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 140 !Locks::trace_lock_); 141 static void Start(std::unique_ptr<unix_file::FdFile>&& file, 142 size_t buffer_size, 143 int flags, 144 TraceOutputMode output_mode, 145 TraceMode trace_mode, 146 int interval_us) 147 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 148 !Locks::trace_lock_); 149 static void StartDDMS(size_t buffer_size, 150 int flags, 151 TraceMode trace_mode, 152 int interval_us) 153 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, 154 !Locks::trace_lock_); 155 156 // Stop tracing. This will finish the trace and write it to file/send it via DDMS. 157 static void Stop() 158 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 159 // Abort tracing. This will just stop tracing and *not* write/send the collected data. 160 static void Abort() 161 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 162 static void Shutdown() 163 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); 164 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_); 165 166 // Flush the per-thread buffer. This is called when the thread is about to detach. 167 static void FlushThreadBuffer(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) 168 REQUIRES(!Locks::trace_lock_) NO_THREAD_SAFETY_ANALYSIS; 169 170 bool UseWallClock(); 171 bool UseThreadCpuClock(); 172 void MeasureClockOverhead(); 173 uint32_t GetClockOverheadNanoSeconds(); 174 175 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace) 176 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_); 177 178 // InstrumentationListener implementation. 179 void MethodEntered(Thread* thread, ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) 180 REQUIRES(!tracing_lock_) override; 181 void MethodExited(Thread* thread, 182 ArtMethod* method, 183 instrumentation::OptionalFrame frame, 184 JValue& return_value) 185 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_) 186 override; 187 void MethodUnwind(Thread* thread, 188 ArtMethod* method, 189 uint32_t dex_pc) 190 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_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(!tracing_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(!tracing_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(!tracing_lock_) override; 211 void ExceptionThrown(Thread* thread, 212 Handle<mirror::Throwable> exception_object) 213 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_) override; 214 void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object) 215 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_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(!tracing_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 static int GetFlags() REQUIRES(!Locks::trace_lock_); 234 static int GetIntervalInMillis() REQUIRES(!Locks::trace_lock_); 235 236 // Used by class linker to prevent class unloading. 237 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_); 238 239 private: 240 Trace(File* trace_file, 241 size_t buffer_size, 242 int flags, 243 TraceOutputMode output_mode, 244 TraceMode trace_mode); 245 246 // The sampling interval in microseconds is passed as an argument. 247 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_); 248 249 static void StopTracing(bool finish_tracing, bool flush_file) 250 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_) 251 // There is an annoying issue with static functions that create a new object and call into 252 // that object that causes them to not be able to tell that we don't currently hold the lock. 253 // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out 254 // how to annotate this. 255 NO_THREAD_SAFETY_ANALYSIS; 256 void FinishTracing() 257 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_); 258 259 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint64_t* timestamp_counter); 260 261 void LogMethodTraceEvent(Thread* thread, 262 ArtMethod* method, 263 TraceAction action, 264 uint32_t thread_clock_diff, 265 uint64_t timestamp_counter) REQUIRES_SHARED(Locks::mutator_lock_) 266 REQUIRES(!tracing_lock_); 267 268 // Methods to output traced methods and threads. 269 void DumpMethodList(std::ostream& os) 270 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_); 271 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_); 272 273 void RecordMethodEvent(Thread* thread, 274 ArtMethod* method, 275 TraceAction action, 276 uint32_t thread_clock_diff, 277 uint64_t timestamp) REQUIRES(!tracing_lock_); 278 279 // Encodes event in non-streaming mode. This assumes that there is enough space reserved to 280 // encode the entry. 281 void EncodeEventEntry(uint8_t* ptr, 282 Thread* thread, 283 uint32_t method_index, 284 TraceAction action, 285 uint32_t thread_clock_diff, 286 uint32_t wall_clock_diff) REQUIRES(tracing_lock_); 287 288 // These methods are used to encode events in streaming mode. 289 290 // This records the method event in the per-thread buffer if there is sufficient space for the 291 // entire record. If the buffer is full then it just flushes the buffer and then records the 292 // entry. 293 void RecordStreamingMethodEvent(Thread* thread, 294 ArtMethod* method, 295 TraceAction action, 296 uint32_t thread_clock_diff, 297 uint64_t timestamp) REQUIRES_SHARED(Locks::mutator_lock_) 298 REQUIRES(!tracing_lock_); 299 // This encodes all the events in the per-thread trace buffer and writes it to the trace file. 300 // This acquires streaming lock to prevent any other threads writing concurrently. It is required 301 // to serialize these since each method is encoded with a unique id which is assigned when the 302 // method is seen for the first time in the recoreded events. So we need to serialize these 303 // flushes across threads. 304 void FlushStreamingBuffer(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) 305 REQUIRES(!tracing_lock_); 306 // Ensures there is sufficient space in the buffer to record the requested_size. If there is not 307 // enough sufficient space the current contents of the buffer are written to the file and 308 // current_index is reset to 0. This doesn't check if buffer_size is big enough to hold the 309 // requested size. 310 void EnsureSpace(uint8_t* buffer, 311 size_t* current_index, 312 size_t buffer_size, 313 size_t required_size); 314 // Writes header followed by data to the buffer at the current_index. This also updates the 315 // current_index to point to the next entry. 316 void WriteToBuf(uint8_t* header, 317 size_t header_size, 318 const std::string& data, 319 size_t* current_index, 320 uint8_t* buffer, 321 size_t buffer_size); 322 323 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(tracing_lock_); 324 ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(tracing_lock_); 325 std::string GetMethodLine(ArtMethod* method, uint32_t method_id) REQUIRES(tracing_lock_) 326 REQUIRES_SHARED(Locks::mutator_lock_); 327 328 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source) 329 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!tracing_lock_); 330 331 void UpdateThreadsList(Thread* thread); 332 333 // Singleton instance of the Trace or null when no method tracing is active. 334 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); 335 336 // The default profiler clock source. 337 static TraceClockSource default_clock_source_; 338 339 // Sampling thread, non-zero when sampling. 340 static pthread_t sampling_pthread_; 341 342 // Used to remember an unused stack trace to avoid re-allocation during sampling. 343 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_; 344 345 // File to write trace data out to, null if direct to ddms. 346 std::unique_ptr<File> trace_file_; 347 348 // Buffer to store trace data. In streaming mode, this is protected 349 // by the tracing_lock_. In non-streaming mode, reserved regions 350 // are atomically allocated (using cur_offset_) for log entries to 351 // be written. 352 std::unique_ptr<uint8_t[]> buf_; 353 354 // Flags enabling extra tracing of things such as alloc counts. 355 const int flags_; 356 357 // The kind of output for this tracing. 358 const TraceOutputMode trace_output_mode_; 359 360 // The tracing method. 361 const TraceMode trace_mode_; 362 363 const TraceClockSource clock_source_; 364 365 // Size of buf_. 366 const size_t buffer_size_; 367 368 // Time trace was created. 369 const uint64_t start_time_; 370 371 // Clock overhead. 372 const uint32_t clock_overhead_ns_; 373 374 // Offset into buf_. The field is atomic to allow multiple writers 375 // to concurrently reserve space in the buffer. The newly written 376 // buffer contents are not read without some other form of thread 377 // synchronization, such as suspending all potential writers or 378 // acquiring *tracing_lock_. Reading cur_offset_ is thus never 379 // used to ensure visibility of any other objects, and all accesses 380 // are memory_order_relaxed. 381 // 382 // All accesses to buf_ in streaming mode occur whilst holding the 383 // streaming lock. In streaming mode, the buffer may be written out 384 // so cur_offset_ can move forwards and backwards. 385 // 386 // When not in streaming mode, the buf_ writes can come from 387 // multiple threads when the trace mode is kMethodTracing. When 388 // trace mode is kSampling, writes only come from the sampling 389 // thread. 390 // 391 // Reads to the buffer happen after the event sources writing to the 392 // buffer have been shutdown and all stores have completed. The 393 // stores are made visible in StopTracing() when execution leaves 394 // the ScopedSuspendAll block. 395 AtomicInteger cur_offset_; 396 397 // Did we overflow the buffer recording traces? 398 bool overflow_; 399 400 // Map of thread ids and names. We record the information when the threads are 401 // exiting and when the tracing has finished. 402 SafeMap<pid_t, std::string> threads_list_; 403 404 // Sampling profiler sampling interval. 405 int interval_us_; 406 407 // A flag to indicate to the sampling thread whether to stop tracing 408 bool stop_tracing_; 409 410 // Streaming mode data. 411 Mutex tracing_lock_; 412 413 // Map from ArtMethod* to index. 414 std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(tracing_lock_); 415 uint32_t current_method_index_ = 0; 416 417 DISALLOW_COPY_AND_ASSIGN(Trace); 418 }; 419 420 } // namespace art 421 422 #endif // ART_RUNTIME_TRACE_H_ 423