blob: 80f1a4c1bcab8ef881c17a2c408ec4285fcaec49 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhaoe343b762011-12-05 16:36:44 -080016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_TRACE_H_
18#define ART_RUNTIME_TRACE_H_
jeffhaoe343b762011-12-05 16:36:44 -080019
Andreas Gampe40da2862015-02-27 12:49:04 -080020#include <bitset>
21#include <map>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
jeffhaoa9ef3fd2011-12-13 18:33:43 -080023#include <ostream>
24#include <set>
25#include <string>
Mathieu Chartier4d64cd42015-06-02 16:38:29 -070026#include <unordered_map>
Jeff Hao0abc72e2013-08-13 13:45:14 -070027#include <vector>
jeffhaoe343b762011-12-05 16:36:44 -080028
Ian Rogers8ab25ef2014-07-09 18:00:50 -070029#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080030#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080031#include "globals.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080032#include "instrumentation.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
jeffhaoe343b762011-12-05 16:36:44 -080035
36namespace art {
37
Mathieu Chartierc7853442015-03-27 14:35:38 -070038class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Andreas Gampe7526d782015-06-22 22:53:45 -070040class DexFile;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080041class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080042
Andreas Gampe40da2862015-02-27 12:49:04 -080043using DexIndexBitSet = std::bitset<65536>;
44using ThreadIDBitSet = std::bitset<65536>;
45
Jeff Hao64caa7d2013-08-29 11:18:01 -070046enum TracingMode {
47 kTracingInactive,
48 kMethodTracingActive,
49 kSampleProfilingActive,
50};
51
Mathieu Chartier4d64cd42015-06-02 16:38:29 -070052// File format:
53// header
54// record 0
55// record 1
56// ...
57//
58// Header format:
59// u4 magic ('SLOW')
60// u2 version
61// u2 offset to data
62// u8 start date/time in usec
63// u2 record size in bytes (version >= 2 only)
64// ... padding to 32 bytes
65//
66// Record format v1:
67// u1 thread ID
68// u4 method ID | method action
69// u4 time delta since start, in usec
70//
71// Record format v2:
72// u2 thread ID
73// u4 method ID | method action
74// u4 time delta since start, in usec
75//
76// Record format v3:
77// u2 thread ID
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)
81//
82// 32 bits of microseconds is 70 minutes.
83//
84// All values are stored in little-endian order.
85
86enum TraceAction {
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
92};
93
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020094class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080095 public:
jeffhao0791adc2012-04-04 11:14:32 -070096 enum TraceFlag {
97 kTraceCountAllocs = 1,
98 };
99
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700100 enum class TraceOutputMode {
101 kFile,
Andreas Gampe40da2862015-02-27 12:49:04 -0800102 kDDMS,
103 kStreaming
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700104 };
105
106 enum class TraceMode {
107 kMethodTracing,
108 kSampling
109 };
110
Andreas Gampe40da2862015-02-27 12:49:04 -0800111 ~Trace();
112
Ian Rogerse63db272014-07-15 15:36:11 -0700113 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700114
Andreas Gampee34a42c2015-04-25 14:44:29 -0700115 static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags,
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700116 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us)
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 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_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800121
122 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100123 static void Stop()
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800125 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
126 static void Abort()
Mathieu Chartier90443472015-07-16 20:32:27 -0700127 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe7526d782015-06-22 22:53:45 -0700128 static void Shutdown()
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
130 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -0800131
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700132 bool UseWallClock();
133 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -0700134 void MeasureClockOverhead();
135 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700136
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace)
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700139
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200140 // InstrumentationListener implementation.
141 void MethodEntered(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142 ArtMethod* method, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
144 OVERRIDE;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200145 void MethodExited(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 ArtMethod* method, uint32_t dex_pc,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200147 const JValue& return_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
149 OVERRIDE;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200150 void MethodUnwind(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 ArtMethod* method, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
153 OVERRIDE;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200154 void DexPcMoved(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 ArtMethod* method, uint32_t new_dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700156 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
157 OVERRIDE;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200158 void FieldRead(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 ArtMethod* method, uint32_t dex_pc, ArtField* field)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200161 void FieldWritten(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162 ArtMethod* method, uint32_t dex_pc, ArtField* field,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200163 const JValue& field_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000165 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000167 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100169 void InvokeVirtualOrInterface(Thread* thread,
170 mirror::Object* this_object,
171 ArtMethod* caller,
172 uint32_t dex_pc,
173 ArtMethod* callee)
174 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700175 // Reuse an old stack trace if it exists, otherwise allocate a new one.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 static std::vector<ArtMethod*>* AllocStackTrace();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700177 // Clear and store an old stack trace for later use.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700179 // Save id and name of a thread before it exits.
180 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700181
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_);
183 static TraceMode GetMode() REQUIRES(!Locks::trace_lock_);
184 static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800185
Mathieu Chartier7778b882015-10-05 16:41:10 -0700186 // Used by class linker to prevent class unloading.
187 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_);
188
jeffhaoe343b762011-12-05 16:36:44 -0800189 private:
Andreas Gampee34a42c2015-04-25 14:44:29 -0700190 Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags,
Andreas Gampe40da2862015-02-27 12:49:04 -0800191 TraceOutputMode output_mode, TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800192
Jeff Hao23009dc2013-08-22 15:36:42 -0700193 // The sampling interval in microseconds is passed as an argument.
Mathieu Chartier90443472015-07-16 20:32:27 -0700194 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700195
Andreas Gampe7526d782015-06-22 22:53:45 -0700196 static void StopTracing(bool finish_tracing, bool flush_file)
Mathieu Chartier90443472015-07-16 20:32:27 -0700197 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_);
jeffhao2692b572011-12-16 15:42:28 -0800204
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700205 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
206
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700208 instrumentation::Instrumentation::InstrumentationEvent event,
Andreas Gampe40da2862015-02-27 12:49:04 -0800209 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
Mathieu Chartier90443472015-07-16 20:32:27 -0700210 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800211
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800212 // Methods to output traced methods and threads.
Mathieu Chartier90443472015-07-16 20:32:27 -0700213 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods)
214 REQUIRES(!*unique_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
Mathieu Chartier90443472015-07-16 20:32:27 -0700216 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
217 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800218
Andreas Gampe40da2862015-02-27 12:49:04 -0800219 // Methods to register seen entitites in streaming mode. The methods return true if the entity
220 // is newly discovered.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 bool RegisterMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800223 bool RegisterThread(Thread* thread)
Mathieu Chartier90443472015-07-16 20:32:27 -0700224 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800225
226 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
227 // annotation.
228 void WriteToBuf(const uint8_t* src, size_t src_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700229 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800230
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!*unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700232 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 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_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700237
238 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700240
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700241 // Singleton instance of the Trace or null when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700242 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800243
244 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700245 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800246
Jeff Hao0abc72e2013-08-13 13:45:14 -0700247 // Sampling thread, non-zero when sampling.
248 static pthread_t sampling_pthread_;
249
Jeff Hao5ce4b172013-08-16 16:27:18 -0700250 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800252
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700253 // File to write trace data out to, null if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700254 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800255
jeffhao2692b572011-12-16 15:42:28 -0800256 // Buffer to store trace data.
Christopher Ferris241a9582015-04-27 15:19:41 -0700257 std::unique_ptr<uint8_t[]> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800258
jeffhao0791adc2012-04-04 11:14:32 -0700259 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700261
Andreas Gampe40da2862015-02-27 12:49:04 -0800262 // The kind of output for this tracing.
263 const TraceOutputMode trace_output_mode_;
264
265 // The tracing method.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700266 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700267
Ian Rogerse63db272014-07-15 15:36:11 -0700268 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700269
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 // Size of buf_.
Andreas Gampee34a42c2015-04-25 14:44:29 -0700271 const size_t buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800272
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 // Time trace was created.
274 const uint64_t start_time_;
275
Jeff Haoc5d824a2014-07-28 18:35:38 -0700276 // Clock overhead.
277 const uint32_t clock_overhead_ns_;
278
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 // Offset into buf_.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700280 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800281
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 // Did we overflow the buffer recording traces?
283 bool overflow_;
284
Jeff Haoe094b872014-10-14 13:12:01 -0700285 // Map of thread ids and names that have already exited.
286 SafeMap<pid_t, std::string> exited_threads_;
287
Andreas Gampe40da2862015-02-27 12:49:04 -0800288 // Sampling profiler sampling interval.
289 int interval_us_;
290
291 // Streaming mode data.
292 std::string streaming_file_name_;
293 Mutex* streaming_lock_;
Andreas Gampe7526d782015-06-22 22:53:45 -0700294 std::map<const DexFile*, DexIndexBitSet*> seen_methods_;
Andreas Gampe40da2862015-02-27 12:49:04 -0800295 std::unique_ptr<ThreadIDBitSet> seen_threads_;
296
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700297 // Bijective map from ArtMethod* to index.
298 // Map from ArtMethod* to index in unique_methods_;
Andreas Gampe7526d782015-06-22 22:53:45 -0700299 Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700300 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_);
302
jeffhaoe343b762011-12-05 16:36:44 -0800303 DISALLOW_COPY_AND_ASSIGN(Trace);
304};
305
306} // namespace art
307
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700308#endif // ART_RUNTIME_TRACE_H_