blob: eccf157d2d8a93931221cef35ad1e1a038c4eac6 [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
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/atomic.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/locks.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080033#include "base/safe_map.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080034#include "instrumentation.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080035#include "runtime_globals.h"
jeffhaoe343b762011-12-05 16:36:44 -080036
Andreas Gampe6ff3b372018-03-15 08:53:16 -070037namespace unix_file {
38class FdFile;
39} // namespace unix_file
40
jeffhaoe343b762011-12-05 16:36:44 -080041namespace art {
42
Mathieu Chartierc7853442015-03-27 14:35:38 -070043class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070044class ArtMethod;
Andreas Gampe7526d782015-06-22 22:53:45 -070045class DexFile;
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080046class LOCKABLE Mutex;
Alex Light05f47742017-09-14 00:34:44 +000047class ShadowFrame;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080048class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080049
Andreas Gampe40da2862015-02-27 12:49:04 -080050using DexIndexBitSet = std::bitset<65536>;
Alex Lighta344f6a2016-07-20 10:43:39 -070051
52constexpr size_t kMaxThreadIdNumber = kIsTargetBuild ? 65536U : 1048576U;
53using ThreadIDBitSet = std::bitset<kMaxThreadIdNumber>;
Andreas Gampe40da2862015-02-27 12:49:04 -080054
Jeff Hao64caa7d2013-08-29 11:18:01 -070055enum TracingMode {
56 kTracingInactive,
Orion Hodson283ad2d2018-03-26 13:37:41 +010057 kMethodTracingActive, // Trace activity synchronous with method progress.
58 kSampleProfilingActive, // Trace activity captured by sampling thread.
Jeff Hao64caa7d2013-08-29 11:18:01 -070059};
Orion Hodson283ad2d2018-03-26 13:37:41 +010060std::ostream& operator<<(std::ostream& os, const TracingMode& rhs);
Jeff Hao64caa7d2013-08-29 11:18:01 -070061
Mathieu Chartier4d64cd42015-06-02 16:38:29 -070062// 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
96enum 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
Orion Hodson283ad2d2018-03-26 13:37:41 +0100104// 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).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100107class Trace final : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -0800108 public:
jeffhao0791adc2012-04-04 11:14:32 -0700109 enum TraceFlag {
110 kTraceCountAllocs = 1,
111 };
112
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700113 enum class TraceOutputMode {
114 kFile,
Andreas Gampe40da2862015-02-27 12:49:04 -0800115 kDDMS,
116 kStreaming
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700117 };
118
119 enum class TraceMode {
120 kMethodTracing,
121 kSampling
122 };
123
Andreas Gampe40da2862015-02-27 12:49:04 -0800124 ~Trace();
125
Ian Rogerse63db272014-07-15 15:36:11 -0700126 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700127
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700128 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
135 !Locks::trace_lock_);
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700136 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
Andreas Gampe40da2862015-02-27 12:49:04 -0800159 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100160 static void Stop()
Mathieu Chartier90443472015-07-16 20:32:27 -0700161 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800162 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
163 static void Abort()
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe7526d782015-06-22 22:53:45 -0700165 static void Shutdown()
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
167 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -0800168
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700169 bool UseWallClock();
170 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -0700171 void MeasureClockOverhead();
172 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700173
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800175 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700176
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200177 // InstrumentationListener implementation.
Alex Lightd7661582017-05-01 13:48:16 -0700178 void MethodEntered(Thread* thread,
179 Handle<mirror::Object> this_object,
180 ArtMethod* method,
181 uint32_t dex_pc)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800182 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100183 override;
Alex Lightd7661582017-05-01 13:48:16 -0700184 void MethodExited(Thread* thread,
185 Handle<mirror::Object> this_object,
186 ArtMethod* method,
187 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700188 instrumentation::OptionalFrame frame,
189 JValue& return_value)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800190 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100191 override;
Alex Lightd7661582017-05-01 13:48:16 -0700192 void MethodUnwind(Thread* thread,
193 Handle<mirror::Object> this_object,
194 ArtMethod* method,
195 uint32_t dex_pc)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800196 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100197 override;
Alex Lightd7661582017-05-01 13:48:16 -0700198 void DexPcMoved(Thread* thread,
199 Handle<mirror::Object> this_object,
200 ArtMethod* method,
201 uint32_t new_dex_pc)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800202 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100203 override;
Alex Lightd7661582017-05-01 13:48:16 -0700204 void FieldRead(Thread* thread,
205 Handle<mirror::Object> this_object,
206 ArtMethod* method,
207 uint32_t dex_pc,
208 ArtField* field)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800209 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
Alex Lightd7661582017-05-01 13:48:16 -0700210 void FieldWritten(Thread* thread,
211 Handle<mirror::Object> this_object,
212 ArtMethod* method,
213 uint32_t dex_pc,
214 ArtField* field,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200215 const JValue& field_value)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800216 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
Alex Light6e1607e2017-08-23 10:06:18 -0700217 void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700218 Handle<mirror::Throwable> exception_object)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800219 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
Alex Light798eab02017-08-23 12:54:53 -0700220 void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800221 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
Alex Lightd7661582017-05-01 13:48:16 -0700222 void Branch(Thread* thread,
223 ArtMethod* method,
224 uint32_t dex_pc,
225 int32_t dex_pc_offset)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800226 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override;
Alex Light05f47742017-09-14 00:34:44 +0000227 void WatchedFramePop(Thread* thread, const ShadowFrame& frame)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100228 REQUIRES_SHARED(Locks::mutator_lock_) override;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700229 // Reuse an old stack trace if it exists, otherwise allocate a new one.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 static std::vector<ArtMethod*>* AllocStackTrace();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700231 // Clear and store an old stack trace for later use.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700233 // Save id and name of a thread before it exits.
234 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700235
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_);
237 static TraceMode GetMode() REQUIRES(!Locks::trace_lock_);
238 static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800239
Mathieu Chartier7778b882015-10-05 16:41:10 -0700240 // Used by class linker to prevent class unloading.
241 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_);
242
jeffhaoe343b762011-12-05 16:36:44 -0800243 private:
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700244 Trace(File* trace_file,
245 size_t buffer_size,
246 int flags,
247 TraceOutputMode output_mode,
248 TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800249
Jeff Hao23009dc2013-08-22 15:36:42 -0700250 // The sampling interval in microseconds is passed as an argument.
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700252
Andreas Gampe7526d782015-06-22 22:53:45 -0700253 static void StopTracing(bool finish_tracing, bool flush_file)
Mathieu Chartier90443472015-07-16 20:32:27 -0700254 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;
Shukang Zhou8a5ab912017-01-20 11:40:16 -0800260 void FinishTracing()
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800261 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800262
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700263 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
264
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700266 instrumentation::Instrumentation::InstrumentationEvent event,
Andreas Gampe40da2862015-02-27 12:49:04 -0800267 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800268 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800269
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800270 // Methods to output traced methods and threads.
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800272 REQUIRES(!unique_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800274 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700275 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276
Andreas Gampe40da2862015-02-27 12:49:04 -0800277 // Methods to register seen entitites in streaming mode. The methods return true if the entity
278 // is newly discovered.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 bool RegisterMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800281 bool RegisterThread(Thread* thread)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800283
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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700287 REQUIRES(streaming_lock_);
Shukang Zhou8a5ab912017-01-20 11:40:16 -0800288 // Flush the main buffer to file. Used for streaming. Exposed here for lock annotation.
289 void FlushBuf()
290 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800291
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800292 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700293 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800294 REQUIRES(!unique_methods_lock_);
295 ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!unique_methods_lock_);
296 std::string GetMethodLine(ArtMethod* method) REQUIRES(!unique_methods_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700297 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700298
299 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Andreas Gampe7fbc4a52018-11-28 08:26:47 -0800300 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700301
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700302 // Singleton instance of the Trace or null when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700303 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800304
305 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700306 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800307
Jeff Hao0abc72e2013-08-13 13:45:14 -0700308 // Sampling thread, non-zero when sampling.
309 static pthread_t sampling_pthread_;
310
Jeff Hao5ce4b172013-08-16 16:27:18 -0700311 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800313
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700314 // File to write trace data out to, null if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700315 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800316
Orion Hodson283ad2d2018-03-26 13:37:41 +0100317 // 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.
Christopher Ferris241a9582015-04-27 15:19:41 -0700321 std::unique_ptr<uint8_t[]> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800322
jeffhao0791adc2012-04-04 11:14:32 -0700323 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700325
Andreas Gampe40da2862015-02-27 12:49:04 -0800326 // The kind of output for this tracing.
327 const TraceOutputMode trace_output_mode_;
328
329 // The tracing method.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700330 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700331
Ian Rogerse63db272014-07-15 15:36:11 -0700332 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700333
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 // Size of buf_.
Andreas Gampee34a42c2015-04-25 14:44:29 -0700335 const size_t buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800336
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 // Time trace was created.
338 const uint64_t start_time_;
339
Jeff Haoc5d824a2014-07-28 18:35:38 -0700340 // Clock overhead.
341 const uint32_t clock_overhead_ns_;
342
Orion Hodson283ad2d2018-03-26 13:37:41 +0100343 // 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.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700364 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800365
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 // Did we overflow the buffer recording traces?
367 bool overflow_;
368
Jeff Haoe094b872014-10-14 13:12:01 -0700369 // Map of thread ids and names that have already exited.
370 SafeMap<pid_t, std::string> exited_threads_;
371
Andreas Gampe40da2862015-02-27 12:49:04 -0800372 // Sampling profiler sampling interval.
373 int interval_us_;
374
375 // Streaming mode data.
Andreas Gampe40da2862015-02-27 12:49:04 -0800376 Mutex* streaming_lock_;
Orion Hodson283ad2d2018-03-26 13:37:41 +0100377 std::map<const DexFile*, DexIndexBitSet*> seen_methods_ GUARDED_BY(streaming_lock_);
378 std::unique_ptr<ThreadIDBitSet> seen_threads_ GUARDED_BY(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800379
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700380 // Bijective map from ArtMethod* to index.
381 // Map from ArtMethod* to index in unique_methods_;
Andreas Gampe7526d782015-06-22 22:53:45 -0700382 Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700383 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
jeffhaoe343b762011-12-05 16:36:44 -0800386 DISALLOW_COPY_AND_ASSIGN(Trace);
387};
388
389} // namespace art
390
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700391#endif // ART_RUNTIME_TRACE_H_