2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_TRACE_H_
18 #define ART_RUNTIME_TRACE_H_
26 #include <unordered_map>
30 #include "base/macros.h"
32 #include "instrumentation.h"
43 using DexIndexBitSet = std::bitset<65536>;
44 using ThreadIDBitSet = std::bitset<65536>;
49 kSampleProfilingActive,
62 // u8 start date/time in usec
63 // u2 record size in bytes (version >= 2 only)
64 // ... padding to 32 bytes
68 // u4 method ID | method action
69 // u4 time delta since start, in usec
73 // u4 method ID | method action
74 // u4 time delta since start, in usec
78 // u4 method ID | method action
79 // u4 time delta since start, in usec
80 // u4 wall time since start, in usec (when clock == "dual" only)
82 // 32 bits of microseconds is 70 minutes.
84 // All values are stored in little-endian order.
87 kTraceMethodEnter = 0x00, // method entry
88 kTraceMethodExit = 0x01, // method exit
89 kTraceUnroll = 0x02, // method exited by exception unrolling
90 // 0x03 currently unused
91 kTraceMethodActionMask = 0x03, // two bits
94 class Trace FINAL : public instrumentation::InstrumentationListener {
97 kTraceCountAllocs = 1,
100 enum class TraceOutputMode {
106 enum class TraceMode {
113 static void SetDefaultClockSource(TraceClockSource clock_source);
115 static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags,
116 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us)
117 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
118 !Locks::trace_lock_);
119 static void Pause() REQUIRES(!Locks::trace_lock_, !Locks::thread_list_lock_);
120 static void Resume() REQUIRES(!Locks::trace_lock_);
122 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
124 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
125 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
127 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
128 static void Shutdown()
129 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
130 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_);
133 bool UseThreadCpuClock();
134 void MeasureClockOverhead();
135 uint32_t GetClockOverheadNanoSeconds();
137 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace)
138 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
140 // InstrumentationListener implementation.
141 void MethodEntered(Thread* thread, mirror::Object* this_object,
142 ArtMethod* method, uint32_t dex_pc)
143 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
145 void MethodExited(Thread* thread, mirror::Object* this_object,
146 ArtMethod* method, uint32_t dex_pc,
147 const JValue& return_value)
148 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
150 void MethodUnwind(Thread* thread, mirror::Object* this_object,
151 ArtMethod* method, uint32_t dex_pc)
152 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
154 void DexPcMoved(Thread* thread, mirror::Object* this_object,
155 ArtMethod* method, uint32_t new_dex_pc)
156 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
158 void FieldRead(Thread* thread, mirror::Object* this_object,
159 ArtMethod* method, uint32_t dex_pc, ArtField* field)
160 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
161 void FieldWritten(Thread* thread, mirror::Object* this_object,
162 ArtMethod* method, uint32_t dex_pc, ArtField* field,
163 const JValue& field_value)
164 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
165 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
166 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
167 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset)
168 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
169 void InvokeVirtualOrInterface(Thread* thread,
170 mirror::Object* this_object,
174 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
175 // Reuse an old stack trace if it exists, otherwise allocate a new one.
176 static std::vector<ArtMethod*>* AllocStackTrace();
177 // Clear and store an old stack trace for later use.
178 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
179 // Save id and name of a thread before it exits.
180 static void StoreExitingThreadInfo(Thread* thread);
182 static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_);
183 static TraceMode GetMode() REQUIRES(!Locks::trace_lock_);
184 static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_);
186 // Used by class linker to prevent class unloading.
187 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_);
190 Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags,
191 TraceOutputMode output_mode, TraceMode trace_mode);
193 // The sampling interval in microseconds is passed as an argument.
194 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_);
196 static void StopTracing(bool finish_tracing, bool flush_file)
197 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_)
198 // There is an annoying issue with static functions that create a new object and call into
199 // that object that causes them to not be able to tell that we don't currently hold the lock.
200 // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out
201 // how to annotate this.
202 NO_THREAD_SAFETY_ANALYSIS;
203 void FinishTracing() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
205 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
207 void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
208 instrumentation::Instrumentation::InstrumentationEvent event,
209 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
210 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
212 // Methods to output traced methods and threads.
213 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods)
214 REQUIRES(!*unique_methods_lock_);
215 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
216 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
217 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_);
219 // Methods to register seen entitites in streaming mode. The methods return true if the entity
220 // is newly discovered.
221 bool RegisterMethod(ArtMethod* method)
222 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(streaming_lock_);
223 bool RegisterThread(Thread* thread)
224 REQUIRES(streaming_lock_);
226 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
228 void WriteToBuf(const uint8_t* src, size_t src_size)
229 REQUIRES(streaming_lock_);
231 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!*unique_methods_lock_);
232 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
233 REQUIRES(!*unique_methods_lock_);
234 ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!*unique_methods_lock_);
235 std::string GetMethodLine(ArtMethod* method) REQUIRES(!*unique_methods_lock_)
236 SHARED_REQUIRES(Locks::mutator_lock_);
238 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
239 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
241 // Singleton instance of the Trace or null when no method tracing is active.
242 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
244 // The default profiler clock source.
245 static TraceClockSource default_clock_source_;
247 // Sampling thread, non-zero when sampling.
248 static pthread_t sampling_pthread_;
250 // Used to remember an unused stack trace to avoid re-allocation during sampling.
251 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
253 // File to write trace data out to, null if direct to ddms.
254 std::unique_ptr<File> trace_file_;
256 // Buffer to store trace data.
257 std::unique_ptr<uint8_t[]> buf_;
259 // Flags enabling extra tracing of things such as alloc counts.
262 // The kind of output for this tracing.
263 const TraceOutputMode trace_output_mode_;
265 // The tracing method.
266 const TraceMode trace_mode_;
268 const TraceClockSource clock_source_;
271 const size_t buffer_size_;
273 // Time trace was created.
274 const uint64_t start_time_;
277 const uint32_t clock_overhead_ns_;
280 AtomicInteger cur_offset_;
282 // Did we overflow the buffer recording traces?
285 // Map of thread ids and names that have already exited.
286 SafeMap<pid_t, std::string> exited_threads_;
288 // Sampling profiler sampling interval.
291 // Streaming mode data.
292 std::string streaming_file_name_;
293 Mutex* streaming_lock_;
294 std::map<const DexFile*, DexIndexBitSet*> seen_methods_;
295 std::unique_ptr<ThreadIDBitSet> seen_threads_;
297 // Bijective map from ArtMethod* to index.
298 // Map from ArtMethod* to index in unique_methods_;
299 Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_);
300 std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_);
301 std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_);
303 DISALLOW_COPY_AND_ASSIGN(Trace);
308 #endif // ART_RUNTIME_TRACE_H_