blob: 7bc495a2376d3c0dd48b1a5699e3d3fe51bfed78 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
Andreas Gampe40da2862015-02-27 12:49:04 -080039 class DexCache;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040} // namespace mirror
Ian Rogerse63db272014-07-15 15:36:11 -070041
Mathieu Chartierc7853442015-03-27 14:35:38 -070042class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070043class ArtMethod;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080044class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080045
Andreas Gampe40da2862015-02-27 12:49:04 -080046using DexIndexBitSet = std::bitset<65536>;
47using ThreadIDBitSet = std::bitset<65536>;
48
Jeff Hao64caa7d2013-08-29 11:18:01 -070049enum TracingMode {
50 kTracingInactive,
51 kMethodTracingActive,
52 kSampleProfilingActive,
53};
54
Mathieu Chartier4d64cd42015-06-02 16:38:29 -070055// File format:
56// header
57// record 0
58// record 1
59// ...
60//
61// Header format:
62// u4 magic ('SLOW')
63// u2 version
64// u2 offset to data
65// u8 start date/time in usec
66// u2 record size in bytes (version >= 2 only)
67// ... padding to 32 bytes
68//
69// Record format v1:
70// u1 thread ID
71// u4 method ID | method action
72// u4 time delta since start, in usec
73//
74// Record format v2:
75// u2 thread ID
76// u4 method ID | method action
77// u4 time delta since start, in usec
78//
79// Record format v3:
80// u2 thread ID
81// u4 method ID | method action
82// u4 time delta since start, in usec
83// u4 wall time since start, in usec (when clock == "dual" only)
84//
85// 32 bits of microseconds is 70 minutes.
86//
87// All values are stored in little-endian order.
88
89enum TraceAction {
90 kTraceMethodEnter = 0x00, // method entry
91 kTraceMethodExit = 0x01, // method exit
92 kTraceUnroll = 0x02, // method exited by exception unrolling
93 // 0x03 currently unused
94 kTraceMethodActionMask = 0x03, // two bits
95};
96
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020097class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080098 public:
jeffhao0791adc2012-04-04 11:14:32 -070099 enum TraceFlag {
100 kTraceCountAllocs = 1,
101 };
102
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700103 enum class TraceOutputMode {
104 kFile,
Andreas Gampe40da2862015-02-27 12:49:04 -0800105 kDDMS,
106 kStreaming
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700107 };
108
109 enum class TraceMode {
110 kMethodTracing,
111 kSampling
112 };
113
Andreas Gampe40da2862015-02-27 12:49:04 -0800114 ~Trace();
115
Ian Rogerse63db272014-07-15 15:36:11 -0700116 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700117
Andreas Gampee34a42c2015-04-25 14:44:29 -0700118 static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags,
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700119 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us)
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100120 LOCKS_EXCLUDED(Locks::mutator_lock_,
121 Locks::thread_list_lock_,
122 Locks::thread_suspend_count_lock_,
123 Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800124 static void Pause() LOCKS_EXCLUDED(Locks::trace_lock_, Locks::thread_list_lock_);
125 static void Resume() LOCKS_EXCLUDED(Locks::trace_lock_);
126
127 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100128 static void Stop()
Andreas Gampe40da2862015-02-27 12:49:04 -0800129 LOCKS_EXCLUDED(Locks::mutator_lock_,
130 Locks::thread_list_lock_,
131 Locks::trace_lock_);
132 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
133 static void Abort()
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100134 LOCKS_EXCLUDED(Locks::mutator_lock_,
135 Locks::thread_list_lock_,
136 Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -0700138 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -0800139
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700140 bool UseWallClock();
141 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -0700142 void MeasureClockOverhead();
143 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700144
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200148 // InstrumentationListener implementation.
149 void MethodEntered(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
152 void MethodExited(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 ArtMethod* method, uint32_t dex_pc,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200154 const JValue& return_value)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
156 void MethodUnwind(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
159 void DexPcMoved(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160 ArtMethod* method, uint32_t new_dex_pc)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
162 void FieldRead(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 ArtMethod* method, uint32_t dex_pc, ArtField* field)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
165 void FieldWritten(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 ArtMethod* method, uint32_t dex_pc, ArtField* field,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200167 const JValue& field_value)
168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000169 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 void BackwardBranch(Thread* thread, ArtMethod* method, int32_t dex_pc_offset)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700173 // Reuse an old stack trace if it exists, otherwise allocate a new one.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 static std::vector<ArtMethod*>* AllocStackTrace();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700175 // Clear and store an old stack trace for later use.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700177 // Save id and name of a thread before it exits.
178 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700179
Andreas Gampe40da2862015-02-27 12:49:04 -0800180 static TraceOutputMode GetOutputMode() LOCKS_EXCLUDED(Locks::trace_lock_);
181 static TraceMode GetMode() LOCKS_EXCLUDED(Locks::trace_lock_);
Andreas Gampee34a42c2015-04-25 14:44:29 -0700182 static size_t GetBufferSize() LOCKS_EXCLUDED(Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800183
jeffhaoe343b762011-12-05 16:36:44 -0800184 private:
Andreas Gampee34a42c2015-04-25 14:44:29 -0700185 Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags,
Andreas Gampe40da2862015-02-27 12:49:04 -0800186 TraceOutputMode output_mode, TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800187
Jeff Hao23009dc2013-08-22 15:36:42 -0700188 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700189 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
190
Andreas Gampe40da2862015-02-27 12:49:04 -0800191 static void StopTracing(bool finish_tracing, bool flush_file);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800193
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700194 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
195
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700197 instrumentation::Instrumentation::InstrumentationEvent event,
Andreas Gampe40da2862015-02-27 12:49:04 -0800198 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800200
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800201 // Methods to output traced methods and threads.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods);
203 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800206
Andreas Gampe40da2862015-02-27 12:49:04 -0800207 // Methods to register seen entitites in streaming mode. The methods return true if the entity
208 // is newly discovered.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 bool RegisterMethod(ArtMethod* method)
Andreas Gampe40da2862015-02-27 12:49:04 -0800210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
211 bool RegisterThread(Thread* thread)
212 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
213
214 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
215 // annotation.
216 void WriteToBuf(const uint8_t* src, size_t src_size)
217 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
218
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700219 uint32_t EncodeTraceMethod(ArtMethod* method) LOCKS_EXCLUDED(unique_methods_lock_);
220 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
221 LOCKS_EXCLUDED(unique_methods_lock_);
222 ArtMethod* DecodeTraceMethod(uint32_t tmid) LOCKS_EXCLUDED(unique_methods_lock_);
223 std::string GetMethodLine(ArtMethod* method) LOCKS_EXCLUDED(unique_methods_lock_)
224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
225
226 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700229 // Singleton instance of the Trace or null when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700230 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800231
232 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700233 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800234
Jeff Hao0abc72e2013-08-13 13:45:14 -0700235 // Sampling thread, non-zero when sampling.
236 static pthread_t sampling_pthread_;
237
Jeff Hao5ce4b172013-08-16 16:27:18 -0700238 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800240
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700241 // File to write trace data out to, null if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700242 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800243
jeffhao2692b572011-12-16 15:42:28 -0800244 // Buffer to store trace data.
Christopher Ferris241a9582015-04-27 15:19:41 -0700245 std::unique_ptr<uint8_t[]> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800246
jeffhao0791adc2012-04-04 11:14:32 -0700247 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700249
Andreas Gampe40da2862015-02-27 12:49:04 -0800250 // The kind of output for this tracing.
251 const TraceOutputMode trace_output_mode_;
252
253 // The tracing method.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700254 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700255
Ian Rogerse63db272014-07-15 15:36:11 -0700256 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700257
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 // Size of buf_.
Andreas Gampee34a42c2015-04-25 14:44:29 -0700259 const size_t buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800260
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 // Time trace was created.
262 const uint64_t start_time_;
263
Jeff Haoc5d824a2014-07-28 18:35:38 -0700264 // Clock overhead.
265 const uint32_t clock_overhead_ns_;
266
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 // Offset into buf_.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700268 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800269
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 // Did we overflow the buffer recording traces?
271 bool overflow_;
272
Jeff Haoe094b872014-10-14 13:12:01 -0700273 // Map of thread ids and names that have already exited.
274 SafeMap<pid_t, std::string> exited_threads_;
275
Andreas Gampe40da2862015-02-27 12:49:04 -0800276 // Sampling profiler sampling interval.
277 int interval_us_;
278
279 // Streaming mode data.
280 std::string streaming_file_name_;
281 Mutex* streaming_lock_;
282 std::map<mirror::DexCache*, DexIndexBitSet*> seen_methods_;
283 std::unique_ptr<ThreadIDBitSet> seen_threads_;
284
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700285 // Bijective map from ArtMethod* to index.
286 // Map from ArtMethod* to index in unique_methods_;
287 Mutex* unique_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
288 std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_);
289 std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_);
290
jeffhaoe343b762011-12-05 16:36:44 -0800291 DISALLOW_COPY_AND_ASSIGN(Trace);
292};
293
294} // namespace art
295
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700296#endif // ART_RUNTIME_TRACE_H_