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