blob: 1fae250d77033d3aeb083874041979e1d2234565 [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"
David Sehr1979c642018-04-26 14:41:18 -070030#include "base/globals.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"
jeffhaoe343b762011-12-05 16:36:44 -080035
Andreas Gampe6ff3b372018-03-15 08:53:16 -070036namespace unix_file {
37class FdFile;
38} // namespace unix_file
39
jeffhaoe343b762011-12-05 16:36:44 -080040namespace art {
41
Mathieu Chartierc7853442015-03-27 14:35:38 -070042class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070043class ArtMethod;
Andreas Gampe7526d782015-06-22 22:53:45 -070044class DexFile;
Alex Light05f47742017-09-14 00:34:44 +000045class ShadowFrame;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080046class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080047
Andreas Gampe40da2862015-02-27 12:49:04 -080048using DexIndexBitSet = std::bitset<65536>;
Alex Lighta344f6a2016-07-20 10:43:39 -070049
50constexpr size_t kMaxThreadIdNumber = kIsTargetBuild ? 65536U : 1048576U;
51using ThreadIDBitSet = std::bitset<kMaxThreadIdNumber>;
Andreas Gampe40da2862015-02-27 12:49:04 -080052
Jeff Hao64caa7d2013-08-29 11:18:01 -070053enum TracingMode {
54 kTracingInactive,
Orion Hodson283ad2d2018-03-26 13:37:41 +010055 kMethodTracingActive, // Trace activity synchronous with method progress.
56 kSampleProfilingActive, // Trace activity captured by sampling thread.
Jeff Hao64caa7d2013-08-29 11:18:01 -070057};
Orion Hodson283ad2d2018-03-26 13:37:41 +010058std::ostream& operator<<(std::ostream& os, const TracingMode& rhs);
Jeff Hao64caa7d2013-08-29 11:18:01 -070059
Mathieu Chartier4d64cd42015-06-02 16:38:29 -070060// File format:
61// header
62// record 0
63// record 1
64// ...
65//
66// Header format:
67// u4 magic ('SLOW')
68// u2 version
69// u2 offset to data
70// u8 start date/time in usec
71// u2 record size in bytes (version >= 2 only)
72// ... padding to 32 bytes
73//
74// Record format v1:
75// u1 thread ID
76// u4 method ID | method action
77// u4 time delta since start, in usec
78//
79// Record format v2:
80// u2 thread ID
81// u4 method ID | method action
82// u4 time delta since start, in usec
83//
84// Record format v3:
85// u2 thread ID
86// u4 method ID | method action
87// u4 time delta since start, in usec
88// u4 wall time since start, in usec (when clock == "dual" only)
89//
90// 32 bits of microseconds is 70 minutes.
91//
92// All values are stored in little-endian order.
93
94enum TraceAction {
95 kTraceMethodEnter = 0x00, // method entry
96 kTraceMethodExit = 0x01, // method exit
97 kTraceUnroll = 0x02, // method exited by exception unrolling
98 // 0x03 currently unused
99 kTraceMethodActionMask = 0x03, // two bits
100};
101
Orion Hodson283ad2d2018-03-26 13:37:41 +0100102// Class for recording event traces. Trace data is either collected
103// synchronously during execution (TracingMode::kMethodTracingActive),
104// or by a separate sampling thread (TracingMode::kSampleProfilingActive).
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200105class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -0800106 public:
jeffhao0791adc2012-04-04 11:14:32 -0700107 enum TraceFlag {
108 kTraceCountAllocs = 1,
109 };
110
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700111 enum class TraceOutputMode {
112 kFile,
Andreas Gampe40da2862015-02-27 12:49:04 -0800113 kDDMS,
114 kStreaming
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700115 };
116
117 enum class TraceMode {
118 kMethodTracing,
119 kSampling
120 };
121
Andreas Gampe40da2862015-02-27 12:49:04 -0800122 ~Trace();
123
Ian Rogerse63db272014-07-15 15:36:11 -0700124 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700125
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700126 static void Start(const char* trace_filename,
127 size_t buffer_size,
128 int flags,
129 TraceOutputMode output_mode,
130 TraceMode trace_mode,
131 int interval_us)
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
133 !Locks::trace_lock_);
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700134 static void Start(int trace_fd,
135 size_t buffer_size,
136 int flags,
137 TraceOutputMode output_mode,
138 TraceMode trace_mode,
139 int interval_us)
140 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
141 !Locks::trace_lock_);
142 static void Start(std::unique_ptr<unix_file::FdFile>&& file,
143 size_t buffer_size,
144 int flags,
145 TraceOutputMode output_mode,
146 TraceMode trace_mode,
147 int interval_us)
148 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
149 !Locks::trace_lock_);
150 static void StartDDMS(size_t buffer_size,
151 int flags,
152 TraceMode trace_mode,
153 int interval_us)
154 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
155 !Locks::trace_lock_);
156
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 static void Pause() REQUIRES(!Locks::trace_lock_, !Locks::thread_list_lock_);
158 static void Resume() REQUIRES(!Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800159
160 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100161 static void Stop()
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800163 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
164 static void Abort()
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
Andreas Gampe7526d782015-06-22 22:53:45 -0700166 static void Shutdown()
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_);
168 static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -0800169
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700170 bool UseWallClock();
171 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -0700172 void MeasureClockOverhead();
173 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700174
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700177
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200178 // InstrumentationListener implementation.
Alex Lightd7661582017-05-01 13:48:16 -0700179 void MethodEntered(Thread* thread,
180 Handle<mirror::Object> this_object,
181 ArtMethod* method,
182 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 OVERRIDE;
Alex Lightd7661582017-05-01 13:48:16 -0700185 void MethodExited(Thread* thread,
186 Handle<mirror::Object> this_object,
187 ArtMethod* method,
188 uint32_t dex_pc,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200189 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 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 Gampebdf7f1c2016-08-30 16:38:47 -0700196 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700197 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 Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700203 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 Gampebdf7f1c2016-08-30 16:38:47 -0700209 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 Gampebdf7f1c2016-08-30 16:38:47 -0700216 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 Gampebdf7f1c2016-08-30 16:38:47 -0700219 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)
221 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 Gampebdf7f1c2016-08-30 16:38:47 -0700226 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100227 void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700228 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100229 ArtMethod* caller,
230 uint32_t dex_pc,
231 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE;
Alex Light05f47742017-09-14 00:34:44 +0000233 void WatchedFramePop(Thread* thread, const ShadowFrame& frame)
234 REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700235 // Reuse an old stack trace if it exists, otherwise allocate a new one.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 static std::vector<ArtMethod*>* AllocStackTrace();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700237 // Clear and store an old stack trace for later use.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700239 // Save id and name of a thread before it exits.
240 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700241
Mathieu Chartier90443472015-07-16 20:32:27 -0700242 static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_);
243 static TraceMode GetMode() REQUIRES(!Locks::trace_lock_);
244 static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800245
Mathieu Chartier7778b882015-10-05 16:41:10 -0700246 // Used by class linker to prevent class unloading.
247 static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_);
248
jeffhaoe343b762011-12-05 16:36:44 -0800249 private:
Andreas Gampe6ff3b372018-03-15 08:53:16 -0700250 Trace(File* trace_file,
251 size_t buffer_size,
252 int flags,
253 TraceOutputMode output_mode,
254 TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800255
Jeff Hao23009dc2013-08-22 15:36:42 -0700256 // The sampling interval in microseconds is passed as an argument.
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700258
Andreas Gampe7526d782015-06-22 22:53:45 -0700259 static void StopTracing(bool finish_tracing, bool flush_file)
Mathieu Chartier90443472015-07-16 20:32:27 -0700260 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_)
261 // There is an annoying issue with static functions that create a new object and call into
262 // that object that causes them to not be able to tell that we don't currently hold the lock.
263 // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out
264 // how to annotate this.
265 NO_THREAD_SAFETY_ANALYSIS;
Shukang Zhou8a5ab912017-01-20 11:40:16 -0800266 void FinishTracing()
267 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800268
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700269 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
270
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 void LogMethodTraceEvent(Thread* thread, ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700272 instrumentation::Instrumentation::InstrumentationEvent event,
Andreas Gampe40da2862015-02-27 12:49:04 -0800273 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800275
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800276 // Methods to output traced methods and threads.
Mathieu Chartier90443472015-07-16 20:32:27 -0700277 void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods)
278 REQUIRES(!*unique_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700281 void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800282
Andreas Gampe40da2862015-02-27 12:49:04 -0800283 // Methods to register seen entitites in streaming mode. The methods return true if the entity
284 // is newly discovered.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 bool RegisterMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700286 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800287 bool RegisterThread(Thread* thread)
Mathieu Chartier90443472015-07-16 20:32:27 -0700288 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800289
290 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
291 // annotation.
292 void WriteToBuf(const uint8_t* src, size_t src_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 REQUIRES(streaming_lock_);
Shukang Zhou8a5ab912017-01-20 11:40:16 -0800294 // Flush the main buffer to file. Used for streaming. Exposed here for lock annotation.
295 void FlushBuf()
296 REQUIRES(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800297
Mathieu Chartier90443472015-07-16 20:32:27 -0700298 uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!*unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700299 uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action)
Mathieu Chartier90443472015-07-16 20:32:27 -0700300 REQUIRES(!*unique_methods_lock_);
301 ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!*unique_methods_lock_);
302 std::string GetMethodLine(ArtMethod* method) REQUIRES(!*unique_methods_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700304
305 void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700307
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700308 // Singleton instance of the Trace or null when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700309 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800310
311 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700312 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800313
Jeff Hao0abc72e2013-08-13 13:45:14 -0700314 // Sampling thread, non-zero when sampling.
315 static pthread_t sampling_pthread_;
316
Jeff Hao5ce4b172013-08-16 16:27:18 -0700317 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800319
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700320 // File to write trace data out to, null if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700321 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800322
Orion Hodson283ad2d2018-03-26 13:37:41 +0100323 // Buffer to store trace data. In streaming mode, this is protected
324 // by the streaming_lock_. In non-streaming mode, reserved regions
325 // are atomically allocated (using cur_offset_) for log entries to
326 // be written.
Christopher Ferris241a9582015-04-27 15:19:41 -0700327 std::unique_ptr<uint8_t[]> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800328
jeffhao0791adc2012-04-04 11:14:32 -0700329 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700331
Andreas Gampe40da2862015-02-27 12:49:04 -0800332 // The kind of output for this tracing.
333 const TraceOutputMode trace_output_mode_;
334
335 // The tracing method.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700336 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700337
Ian Rogerse63db272014-07-15 15:36:11 -0700338 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700339
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 // Size of buf_.
Andreas Gampee34a42c2015-04-25 14:44:29 -0700341 const size_t buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800342
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 // Time trace was created.
344 const uint64_t start_time_;
345
Jeff Haoc5d824a2014-07-28 18:35:38 -0700346 // Clock overhead.
347 const uint32_t clock_overhead_ns_;
348
Orion Hodson283ad2d2018-03-26 13:37:41 +0100349 // Offset into buf_. The field is atomic to allow multiple writers
350 // to concurrently reserve space in the buffer. The newly written
351 // buffer contents are not read without some other form of thread
352 // synchronization, such as suspending all potential writers or
353 // acquiring *streaming_lock_. Reading cur_offset_ is thus never
354 // used to ensure visibility of any other objects, and all accesses
355 // are memory_order_relaxed.
356 //
357 // All accesses to buf_ in streaming mode occur whilst holding the
358 // streaming lock. In streaming mode, the buffer may be written out
359 // so cur_offset_ can move forwards and backwards.
360 //
361 // When not in streaming mode, the buf_ writes can come from
362 // multiple threads when the trace mode is kMethodTracing. When
363 // trace mode is kSampling, writes only come from the sampling
364 // thread.
365 //
366 // Reads to the buffer happen after the event sources writing to the
367 // buffer have been shutdown and all stores have completed. The
368 // stores are made visible in StopTracing() when execution leaves
369 // the ScopedSuspendAll block.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700370 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800371
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 // Did we overflow the buffer recording traces?
373 bool overflow_;
374
Jeff Haoe094b872014-10-14 13:12:01 -0700375 // Map of thread ids and names that have already exited.
376 SafeMap<pid_t, std::string> exited_threads_;
377
Andreas Gampe40da2862015-02-27 12:49:04 -0800378 // Sampling profiler sampling interval.
379 int interval_us_;
380
381 // Streaming mode data.
Andreas Gampe40da2862015-02-27 12:49:04 -0800382 Mutex* streaming_lock_;
Orion Hodson283ad2d2018-03-26 13:37:41 +0100383 std::map<const DexFile*, DexIndexBitSet*> seen_methods_ GUARDED_BY(streaming_lock_);
384 std::unique_ptr<ThreadIDBitSet> seen_threads_ GUARDED_BY(streaming_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800385
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700386 // Bijective map from ArtMethod* to index.
387 // Map from ArtMethod* to index in unique_methods_;
Andreas Gampe7526d782015-06-22 22:53:45 -0700388 Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_);
Mathieu Chartier4d64cd42015-06-02 16:38:29 -0700389 std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_);
390 std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_);
391
jeffhaoe343b762011-12-05 16:36:44 -0800392 DISALLOW_COPY_AND_ASSIGN(Trace);
393};
394
395} // namespace art
396
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700397#endif // ART_RUNTIME_TRACE_H_