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, const 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,
179                      Handle<mirror::Object> this_object,
180                      ArtMethod* method,
181                      uint32_t dex_pc)
182       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
183       override;
184   void MethodExited(Thread* thread,
185                     Handle<mirror::Object> this_object,
186                     ArtMethod* method,
187                     uint32_t dex_pc,
188                     const JValue& return_value)
189       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
190       override;
191   void MethodUnwind(Thread* thread,
192                     Handle<mirror::Object> this_object,
193                     ArtMethod* method,
194                     uint32_t dex_pc)
195       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
196       override;
197   void DexPcMoved(Thread* thread,
198                   Handle<mirror::Object> this_object,
199                   ArtMethod* method,
200                   uint32_t new_dex_pc)
201       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
202       override;
203   void FieldRead(Thread* thread,
204                  Handle<mirror::Object> this_object,
205                  ArtMethod* method,
206                  uint32_t dex_pc,
207                  ArtField* field)
208       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
209   void FieldWritten(Thread* thread,
210                     Handle<mirror::Object> this_object,
211                     ArtMethod* method,
212                     uint32_t dex_pc,
213                     ArtField* field,
214                     const JValue& field_value)
215       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
216   void ExceptionThrown(Thread* thread,
217                        Handle<mirror::Throwable> exception_object)
218       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
219   void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
220       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
221   void Branch(Thread* thread,
222               ArtMethod* method,
223               uint32_t dex_pc,
224               int32_t dex_pc_offset)
225       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
226   void WatchedFramePop(Thread* thread, const ShadowFrame& frame)
227       REQUIRES_SHARED(Locks::mutator_lock_) override;
228   // Reuse an old stack trace if it exists, otherwise allocate a new one.
229   static std::vector<ArtMethod*>* AllocStackTrace();
230   // Clear and store an old stack trace for later use.
231   static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
232   // Save id and name of a thread before it exits.
233   static void StoreExitingThreadInfo(Thread* thread);
234 
235   static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_);
236   static TraceMode GetMode() REQUIRES(!Locks::trace_lock_);
237   static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_);
238 
239   // Used by class linker to prevent class unloading.
240   static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_);
241 
242  private:
243   Trace(File* trace_file,
244         size_t buffer_size,
245         int flags,
246         TraceOutputMode output_mode,
247         TraceMode trace_mode);
248 
249   // The sampling interval in microseconds is passed as an argument.
250   static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_);
251 
252   static void StopTracing(bool finish_tracing, bool flush_file)
253       REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_)
254       // There is an annoying issue with static functions that create a new object and call into
255       // that object that causes them to not be able to tell that we don't currently hold the lock.
256       // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out
257       // how to annotate this.
258       NO_THREAD_SAFETY_ANALYSIS;
259   void FinishTracing()
260       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_);
261 
262   void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
263 
264   void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
265                            instrumentation::Instrumentation::InstrumentationEvent event,
266                            uint32_t thread_clock_diff, uint32_t wall_clock_diff)
267       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_);
268 
269   // Methods to output traced methods and threads.
270   void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods)
271       REQUIRES(!unique_methods_lock_);
272   void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
273       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_);
274   void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_);
275 
276   // Methods to register seen entitites in streaming mode. The methods return true if the entity
277   // is newly discovered.
278   bool RegisterMethod(ArtMethod* method)
279       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_);
280   bool RegisterThread(Thread* thread)
281       REQUIRES(streaming_lock_);
282 
283   // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
284   // annotation.
285   void WriteToBuf(const uint8_t* src, size_t src_size)
286       REQUIRES(streaming_lock_);
287   // Flush the main buffer to file. Used for streaming. Exposed here for lock annotation.
288   void FlushBuf()
289       REQUIRES(streaming_lock_);
290 
291   uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!unique_methods_lock_);
292   uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
293       REQUIRES(!unique_methods_lock_);
294   ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!unique_methods_lock_);
295   std::string GetMethodLine(ArtMethod* method) REQUIRES(!unique_methods_lock_)
296       REQUIRES_SHARED(Locks::mutator_lock_);
297 
298   void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
299       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_);
300 
301   // Singleton instance of the Trace or null when no method tracing is active.
302   static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
303 
304   // The default profiler clock source.
305   static TraceClockSource default_clock_source_;
306 
307   // Sampling thread, non-zero when sampling.
308   static pthread_t sampling_pthread_;
309 
310   // Used to remember an unused stack trace to avoid re-allocation during sampling.
311   static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
312 
313   // File to write trace data out to, null if direct to ddms.
314   std::unique_ptr<File> trace_file_;
315 
316   // Buffer to store trace data. In streaming mode, this is protected
317   // by the streaming_lock_. In non-streaming mode, reserved regions
318   // are atomically allocated (using cur_offset_) for log entries to
319   // be written.
320   std::unique_ptr<uint8_t[]> buf_;
321 
322   // Flags enabling extra tracing of things such as alloc counts.
323   const int flags_;
324 
325   // The kind of output for this tracing.
326   const TraceOutputMode trace_output_mode_;
327 
328   // The tracing method.
329   const TraceMode trace_mode_;
330 
331   const TraceClockSource clock_source_;
332 
333   // Size of buf_.
334   const size_t buffer_size_;
335 
336   // Time trace was created.
337   const uint64_t start_time_;
338 
339   // Clock overhead.
340   const uint32_t clock_overhead_ns_;
341 
342   // Offset into buf_. The field is atomic to allow multiple writers
343   // to concurrently reserve space in the buffer. The newly written
344   // buffer contents are not read without some other form of thread
345   // synchronization, such as suspending all potential writers or
346   // acquiring *streaming_lock_. Reading cur_offset_ is thus never
347   // used to ensure visibility of any other objects, and all accesses
348   // are memory_order_relaxed.
349   //
350   // All accesses to buf_ in streaming mode occur whilst holding the
351   // streaming lock. In streaming mode, the buffer may be written out
352   // so cur_offset_ can move forwards and backwards.
353   //
354   // When not in streaming mode, the buf_ writes can come from
355   // multiple threads when the trace mode is kMethodTracing. When
356   // trace mode is kSampling, writes only come from the sampling
357   // thread.
358   //
359   // Reads to the buffer happen after the event sources writing to the
360   // buffer have been shutdown and all stores have completed. The
361   // stores are made visible in StopTracing() when execution leaves
362   // the ScopedSuspendAll block.
363   AtomicInteger cur_offset_;
364 
365   // Did we overflow the buffer recording traces?
366   bool overflow_;
367 
368   // Map of thread ids and names that have already exited.
369   SafeMap<pid_t, std::string> exited_threads_;
370 
371   // Sampling profiler sampling interval.
372   int interval_us_;
373 
374   // Streaming mode data.
375   Mutex* streaming_lock_;
376   std::map<const DexFile*, DexIndexBitSet*> seen_methods_ GUARDED_BY(streaming_lock_);
377   std::unique_ptr<ThreadIDBitSet> seen_threads_ GUARDED_BY(streaming_lock_);
378 
379   // Bijective map from ArtMethod* to index.
380   // Map from ArtMethod* to index in unique_methods_;
381   Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_);
382   std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_);
383   std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_);
384 
385   DISALLOW_COPY_AND_ASSIGN(Trace);
386 };
387 
388 }  // namespace art
389 
390 #endif  // ART_RUNTIME_TRACE_H_
391