Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_TRACE_H_ |
| 18 | #define ART_RUNTIME_TRACE_H_ |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 19 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 20 | #include <bitset> |
| 21 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 23 | #include <ostream> |
| 24 | #include <set> |
| 25 | #include <string> |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 27 | #include <vector> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 28 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/atomic.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 30 | #include "base/locks.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 32 | #include "base/os.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 33 | #include "base/safe_map.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 34 | #include "instrumentation.h" |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 35 | #include "runtime_globals.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 36 | |
Andreas Gampe | 6ff3b37 | 2018-03-15 08:53:16 -0700 | [diff] [blame] | 37 | namespace unix_file { |
| 38 | class FdFile; |
| 39 | } // namespace unix_file |
| 40 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 43 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | class ArtMethod; |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 45 | class DexFile; |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 46 | class LOCKABLE Mutex; |
Alex Light | 05f4774 | 2017-09-14 00:34:44 +0000 | [diff] [blame] | 47 | class ShadowFrame; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 48 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 49 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 50 | using DexIndexBitSet = std::bitset<65536>; |
Alex Light | a344f6a | 2016-07-20 10:43:39 -0700 | [diff] [blame] | 51 | |
| 52 | constexpr size_t kMaxThreadIdNumber = kIsTargetBuild ? 65536U : 1048576U; |
| 53 | using ThreadIDBitSet = std::bitset<kMaxThreadIdNumber>; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 54 | |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 55 | enum TracingMode { |
| 56 | kTracingInactive, |
Orion Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 57 | kMethodTracingActive, // Trace activity synchronous with method progress. |
| 58 | kSampleProfilingActive, // Trace activity captured by sampling thread. |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 59 | }; |
Orion Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 60 | std::ostream& operator<<(std::ostream& os, const TracingMode& rhs); |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 61 | |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 62 | // 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 | |
| 96 | enum 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 Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 104 | // 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 Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 107 | class Trace final : public instrumentation::InstrumentationListener { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 108 | public: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 109 | enum TraceFlag { |
| 110 | kTraceCountAllocs = 1, |
| 111 | }; |
| 112 | |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 113 | enum class TraceOutputMode { |
| 114 | kFile, |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 115 | kDDMS, |
| 116 | kStreaming |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | enum class TraceMode { |
| 120 | kMethodTracing, |
| 121 | kSampling |
| 122 | }; |
| 123 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 124 | ~Trace(); |
| 125 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 126 | static void SetDefaultClockSource(TraceClockSource clock_source); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 127 | |
Andreas Gampe | 6ff3b37 | 2018-03-15 08:53:16 -0700 | [diff] [blame] | 128 | 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 Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 134 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, |
| 135 | !Locks::trace_lock_); |
Andreas Gampe | 6ff3b37 | 2018-03-15 08:53:16 -0700 | [diff] [blame] | 136 | 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 Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 159 | // Stop tracing. This will finish the trace and write it to file/send it via DDMS. |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 160 | static void Stop() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 161 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 162 | // Abort tracing. This will just stop tracing and *not* write/send the collected data. |
| 163 | static void Abort() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 164 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 165 | static void Shutdown() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 166 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
| 167 | static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 168 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 169 | bool UseWallClock(); |
| 170 | bool UseThreadCpuClock(); |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 171 | void MeasureClockOverhead(); |
| 172 | uint32_t GetClockOverheadNanoSeconds(); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 173 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 175 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 176 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 177 | // InstrumentationListener implementation. |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 178 | void MethodEntered(Thread* thread, |
| 179 | Handle<mirror::Object> this_object, |
| 180 | ArtMethod* method, |
| 181 | uint32_t dex_pc) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 182 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 183 | override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 184 | void MethodExited(Thread* thread, |
| 185 | Handle<mirror::Object> this_object, |
| 186 | ArtMethod* method, |
| 187 | uint32_t dex_pc, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 188 | instrumentation::OptionalFrame frame, |
| 189 | JValue& return_value) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 190 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 191 | override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 192 | void MethodUnwind(Thread* thread, |
| 193 | Handle<mirror::Object> this_object, |
| 194 | ArtMethod* method, |
| 195 | uint32_t dex_pc) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 196 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 197 | override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 198 | void DexPcMoved(Thread* thread, |
| 199 | Handle<mirror::Object> this_object, |
| 200 | ArtMethod* method, |
| 201 | uint32_t new_dex_pc) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 202 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 203 | override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 204 | void FieldRead(Thread* thread, |
| 205 | Handle<mirror::Object> this_object, |
| 206 | ArtMethod* method, |
| 207 | uint32_t dex_pc, |
| 208 | ArtField* field) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 209 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 210 | void FieldWritten(Thread* thread, |
| 211 | Handle<mirror::Object> this_object, |
| 212 | ArtMethod* method, |
| 213 | uint32_t dex_pc, |
| 214 | ArtField* field, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 215 | const JValue& field_value) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 216 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 217 | void ExceptionThrown(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 218 | Handle<mirror::Throwable> exception_object) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 219 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; |
Alex Light | 798eab0 | 2017-08-23 12:54:53 -0700 | [diff] [blame] | 220 | void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 221 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 222 | void Branch(Thread* thread, |
| 223 | ArtMethod* method, |
| 224 | uint32_t dex_pc, |
| 225 | int32_t dex_pc_offset) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 226 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_) override; |
Alex Light | 05f4774 | 2017-09-14 00:34:44 +0000 | [diff] [blame] | 227 | void WatchedFramePop(Thread* thread, const ShadowFrame& frame) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 228 | REQUIRES_SHARED(Locks::mutator_lock_) override; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 229 | // Reuse an old stack trace if it exists, otherwise allocate a new one. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 230 | static std::vector<ArtMethod*>* AllocStackTrace(); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 231 | // Clear and store an old stack trace for later use. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 232 | static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace); |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 233 | // Save id and name of a thread before it exits. |
| 234 | static void StoreExitingThreadInfo(Thread* thread); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 235 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 236 | 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 Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 239 | |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 240 | // Used by class linker to prevent class unloading. |
| 241 | static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_); |
| 242 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 243 | private: |
Andreas Gampe | 6ff3b37 | 2018-03-15 08:53:16 -0700 | [diff] [blame] | 244 | Trace(File* trace_file, |
| 245 | size_t buffer_size, |
| 246 | int flags, |
| 247 | TraceOutputMode output_mode, |
| 248 | TraceMode trace_mode); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 249 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 250 | // The sampling interval in microseconds is passed as an argument. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 251 | static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 252 | |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 253 | static void StopTracing(bool finish_tracing, bool flush_file) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 254 | 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 Zhou | 8a5ab91 | 2017-01-20 11:40:16 -0800 | [diff] [blame] | 260 | void FinishTracing() |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 261 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 262 | |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 263 | void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff); |
| 264 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 265 | void LogMethodTraceEvent(Thread* thread, ArtMethod* method, |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 266 | instrumentation::Instrumentation::InstrumentationEvent event, |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 267 | uint32_t thread_clock_diff, uint32_t wall_clock_diff) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 268 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_, !streaming_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 269 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 270 | // Methods to output traced methods and threads. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 271 | void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 272 | REQUIRES(!unique_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 273 | void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 274 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 275 | void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 276 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 277 | // Methods to register seen entitites in streaming mode. The methods return true if the entity |
| 278 | // is newly discovered. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 279 | bool RegisterMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 280 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 281 | bool RegisterThread(Thread* thread) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 282 | REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 283 | |
| 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 Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 287 | REQUIRES(streaming_lock_); |
Shukang Zhou | 8a5ab91 | 2017-01-20 11:40:16 -0800 | [diff] [blame] | 288 | // Flush the main buffer to file. Used for streaming. Exposed here for lock annotation. |
| 289 | void FlushBuf() |
| 290 | REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 291 | |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 292 | uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!unique_methods_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 293 | uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 294 | 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 297 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 298 | |
| 299 | void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source) |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 300 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!unique_methods_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 301 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 302 | // Singleton instance of the Trace or null when no method tracing is active. |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 303 | static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 304 | |
| 305 | // The default profiler clock source. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 306 | static TraceClockSource default_clock_source_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 307 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 308 | // Sampling thread, non-zero when sampling. |
| 309 | static pthread_t sampling_pthread_; |
| 310 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 311 | // Used to remember an unused stack trace to avoid re-allocation during sampling. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 312 | static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 313 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 314 | // File to write trace data out to, null if direct to ddms. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 315 | std::unique_ptr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 316 | |
Orion Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 317 | // 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 Ferris | 241a958 | 2015-04-27 15:19:41 -0700 | [diff] [blame] | 321 | std::unique_ptr<uint8_t[]> buf_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 322 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 323 | // Flags enabling extra tracing of things such as alloc counts. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 324 | const int flags_; |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 325 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 326 | // The kind of output for this tracing. |
| 327 | const TraceOutputMode trace_output_mode_; |
| 328 | |
| 329 | // The tracing method. |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 330 | const TraceMode trace_mode_; |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 331 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 332 | const TraceClockSource clock_source_; |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 333 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 334 | // Size of buf_. |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 335 | const size_t buffer_size_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 336 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 337 | // Time trace was created. |
| 338 | const uint64_t start_time_; |
| 339 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 340 | // Clock overhead. |
| 341 | const uint32_t clock_overhead_ns_; |
| 342 | |
Orion Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 343 | // 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 Rogers | 8ab25ef | 2014-07-09 18:00:50 -0700 | [diff] [blame] | 364 | AtomicInteger cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 365 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 366 | // Did we overflow the buffer recording traces? |
| 367 | bool overflow_; |
| 368 | |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 369 | // Map of thread ids and names that have already exited. |
| 370 | SafeMap<pid_t, std::string> exited_threads_; |
| 371 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 372 | // Sampling profiler sampling interval. |
| 373 | int interval_us_; |
| 374 | |
| 375 | // Streaming mode data. |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 376 | Mutex* streaming_lock_; |
Orion Hodson | 283ad2d | 2018-03-26 13:37:41 +0100 | [diff] [blame] | 377 | std::map<const DexFile*, DexIndexBitSet*> seen_methods_ GUARDED_BY(streaming_lock_); |
| 378 | std::unique_ptr<ThreadIDBitSet> seen_threads_ GUARDED_BY(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 379 | |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 380 | // Bijective map from ArtMethod* to index. |
| 381 | // Map from ArtMethod* to index in unique_methods_; |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 382 | Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 383 | 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 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 386 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 387 | }; |
| 388 | |
| 389 | } // namespace art |
| 390 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 391 | #endif // ART_RUNTIME_TRACE_H_ |