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 | |
Ian Rogers | 8ab25ef | 2014-07-09 18:00:50 -0700 | [diff] [blame] | 29 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | #include "base/macros.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 31 | #include "globals.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 32 | #include "instrumentation.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 33 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 34 | #include "safe_map.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 38 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | class ArtMethod; |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 40 | class DexFile; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 41 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 42 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 43 | using DexIndexBitSet = std::bitset<65536>; |
Alex Light | a344f6a | 2016-07-20 10:43:39 -0700 | [diff] [blame] | 44 | |
| 45 | constexpr size_t kMaxThreadIdNumber = kIsTargetBuild ? 65536U : 1048576U; |
| 46 | using ThreadIDBitSet = std::bitset<kMaxThreadIdNumber>; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 47 | |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 48 | enum TracingMode { |
| 49 | kTracingInactive, |
| 50 | kMethodTracingActive, |
| 51 | kSampleProfilingActive, |
| 52 | }; |
| 53 | |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 54 | // File format: |
| 55 | // header |
| 56 | // record 0 |
| 57 | // record 1 |
| 58 | // ... |
| 59 | // |
| 60 | // Header format: |
| 61 | // u4 magic ('SLOW') |
| 62 | // u2 version |
| 63 | // u2 offset to data |
| 64 | // u8 start date/time in usec |
| 65 | // u2 record size in bytes (version >= 2 only) |
| 66 | // ... padding to 32 bytes |
| 67 | // |
| 68 | // Record format v1: |
| 69 | // u1 thread ID |
| 70 | // u4 method ID | method action |
| 71 | // u4 time delta since start, in usec |
| 72 | // |
| 73 | // Record format v2: |
| 74 | // u2 thread ID |
| 75 | // u4 method ID | method action |
| 76 | // u4 time delta since start, in usec |
| 77 | // |
| 78 | // Record format v3: |
| 79 | // u2 thread ID |
| 80 | // u4 method ID | method action |
| 81 | // u4 time delta since start, in usec |
| 82 | // u4 wall time since start, in usec (when clock == "dual" only) |
| 83 | // |
| 84 | // 32 bits of microseconds is 70 minutes. |
| 85 | // |
| 86 | // All values are stored in little-endian order. |
| 87 | |
| 88 | enum TraceAction { |
| 89 | kTraceMethodEnter = 0x00, // method entry |
| 90 | kTraceMethodExit = 0x01, // method exit |
| 91 | kTraceUnroll = 0x02, // method exited by exception unrolling |
| 92 | // 0x03 currently unused |
| 93 | kTraceMethodActionMask = 0x03, // two bits |
| 94 | }; |
| 95 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 96 | class Trace FINAL : public instrumentation::InstrumentationListener { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 97 | public: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 98 | enum TraceFlag { |
| 99 | kTraceCountAllocs = 1, |
| 100 | }; |
| 101 | |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 102 | enum class TraceOutputMode { |
| 103 | kFile, |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 104 | kDDMS, |
| 105 | kStreaming |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | enum class TraceMode { |
| 109 | kMethodTracing, |
| 110 | kSampling |
| 111 | }; |
| 112 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 113 | ~Trace(); |
| 114 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 115 | static void SetDefaultClockSource(TraceClockSource clock_source); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 116 | |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 117 | static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags, |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 118 | TraceOutputMode output_mode, TraceMode trace_mode, int interval_us) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, |
| 120 | !Locks::trace_lock_); |
| 121 | static void Pause() REQUIRES(!Locks::trace_lock_, !Locks::thread_list_lock_); |
| 122 | static void Resume() REQUIRES(!Locks::trace_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 123 | |
| 124 | // 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] | 125 | static void Stop() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 126 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 127 | // Abort tracing. This will just stop tracing and *not* write/send the collected data. |
| 128 | static void Abort() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 129 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 130 | static void Shutdown() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 131 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_); |
| 132 | static TracingMode GetMethodTracingMode() REQUIRES(!Locks::trace_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 133 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 134 | bool UseWallClock(); |
| 135 | bool UseThreadCpuClock(); |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 136 | void MeasureClockOverhead(); |
| 137 | uint32_t GetClockOverheadNanoSeconds(); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 138 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 139 | void CompareAndUpdateStackTrace(Thread* thread, std::vector<ArtMethod*>* stack_trace) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 140 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 141 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 142 | // InstrumentationListener implementation. |
| 143 | void MethodEntered(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 144 | ArtMethod* method, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 146 | OVERRIDE; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 147 | void MethodExited(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 148 | ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 149 | const JValue& return_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 150 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 151 | OVERRIDE; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 152 | void MethodUnwind(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 153 | ArtMethod* method, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 155 | OVERRIDE; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 156 | void DexPcMoved(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 157 | ArtMethod* method, uint32_t new_dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 158 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 159 | OVERRIDE; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 160 | void FieldRead(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 161 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 162 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 163 | void FieldWritten(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 164 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 165 | const JValue& field_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 167 | void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 168 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE; |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 169 | void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 170 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 171 | void InvokeVirtualOrInterface(Thread* thread, |
| 172 | mirror::Object* this_object, |
| 173 | ArtMethod* caller, |
| 174 | uint32_t dex_pc, |
| 175 | ArtMethod* callee) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 176 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_) OVERRIDE; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 177 | // 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] | 178 | static std::vector<ArtMethod*>* AllocStackTrace(); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 179 | // Clear and store an old stack trace for later use. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 180 | static void FreeStackTrace(std::vector<ArtMethod*>* stack_trace); |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 181 | // Save id and name of a thread before it exits. |
| 182 | static void StoreExitingThreadInfo(Thread* thread); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 183 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 184 | static TraceOutputMode GetOutputMode() REQUIRES(!Locks::trace_lock_); |
| 185 | static TraceMode GetMode() REQUIRES(!Locks::trace_lock_); |
| 186 | static size_t GetBufferSize() REQUIRES(!Locks::trace_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 187 | |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 188 | // Used by class linker to prevent class unloading. |
| 189 | static bool IsTracingEnabled() REQUIRES(!Locks::trace_lock_); |
| 190 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 191 | private: |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 192 | Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags, |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 193 | TraceOutputMode output_mode, TraceMode trace_mode); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 194 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 195 | // The sampling interval in microseconds is passed as an argument. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 196 | static void* RunSamplingThread(void* arg) REQUIRES(!Locks::trace_lock_); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 197 | |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 198 | static void StopTracing(bool finish_tracing, bool flush_file) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 199 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::trace_lock_) |
| 200 | // There is an annoying issue with static functions that create a new object and call into |
| 201 | // that object that causes them to not be able to tell that we don't currently hold the lock. |
| 202 | // This causes the negative annotations to incorrectly have a false positive. TODO: Figure out |
| 203 | // how to annotate this. |
| 204 | NO_THREAD_SAFETY_ANALYSIS; |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 205 | void FinishTracing() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 206 | |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 207 | void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff); |
| 208 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 209 | void LogMethodTraceEvent(Thread* thread, ArtMethod* method, |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 210 | instrumentation::Instrumentation::InstrumentationEvent event, |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 211 | uint32_t thread_clock_diff, uint32_t wall_clock_diff) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 212 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_, !*streaming_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 213 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 214 | // Methods to output traced methods and threads. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 215 | void GetVisitedMethods(size_t end_offset, std::set<ArtMethod*>* visited_methods) |
| 216 | REQUIRES(!*unique_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 217 | void DumpMethodList(std::ostream& os, const std::set<ArtMethod*>& visited_methods) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 218 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 219 | void DumpThreadList(std::ostream& os) REQUIRES(!Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 220 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 221 | // Methods to register seen entitites in streaming mode. The methods return true if the entity |
| 222 | // is newly discovered. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 223 | bool RegisterMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 224 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 225 | bool RegisterThread(Thread* thread) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 226 | REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 227 | |
| 228 | // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock |
| 229 | // annotation. |
| 230 | void WriteToBuf(const uint8_t* src, size_t src_size) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 231 | REQUIRES(streaming_lock_); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 232 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 233 | uint32_t EncodeTraceMethod(ArtMethod* method) REQUIRES(!*unique_methods_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 234 | uint32_t EncodeTraceMethodAndAction(ArtMethod* method, TraceAction action) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 235 | REQUIRES(!*unique_methods_lock_); |
| 236 | ArtMethod* DecodeTraceMethod(uint32_t tmid) REQUIRES(!*unique_methods_lock_); |
| 237 | std::string GetMethodLine(ArtMethod* method) REQUIRES(!*unique_methods_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 238 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 239 | |
| 240 | void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 241 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*unique_methods_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 242 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 243 | // 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] | 244 | static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 245 | |
| 246 | // The default profiler clock source. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 247 | static TraceClockSource default_clock_source_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 248 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 249 | // Sampling thread, non-zero when sampling. |
| 250 | static pthread_t sampling_pthread_; |
| 251 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 252 | // 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] | 253 | static std::unique_ptr<std::vector<ArtMethod*>> temp_stack_trace_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 254 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 255 | // File to write trace data out to, null if direct to ddms. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 256 | std::unique_ptr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 257 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 258 | // Buffer to store trace data. |
Christopher Ferris | 241a958 | 2015-04-27 15:19:41 -0700 | [diff] [blame] | 259 | std::unique_ptr<uint8_t[]> buf_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 260 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 261 | // Flags enabling extra tracing of things such as alloc counts. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 262 | const int flags_; |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 263 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 264 | // The kind of output for this tracing. |
| 265 | const TraceOutputMode trace_output_mode_; |
| 266 | |
| 267 | // The tracing method. |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame] | 268 | const TraceMode trace_mode_; |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 269 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 270 | const TraceClockSource clock_source_; |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 271 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 272 | // Size of buf_. |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 273 | const size_t buffer_size_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 274 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 275 | // Time trace was created. |
| 276 | const uint64_t start_time_; |
| 277 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 278 | // Clock overhead. |
| 279 | const uint32_t clock_overhead_ns_; |
| 280 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 281 | // Offset into buf_. |
Ian Rogers | 8ab25ef | 2014-07-09 18:00:50 -0700 | [diff] [blame] | 282 | AtomicInteger cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 283 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 284 | // Did we overflow the buffer recording traces? |
| 285 | bool overflow_; |
| 286 | |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 287 | // Map of thread ids and names that have already exited. |
| 288 | SafeMap<pid_t, std::string> exited_threads_; |
| 289 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 290 | // Sampling profiler sampling interval. |
| 291 | int interval_us_; |
| 292 | |
| 293 | // Streaming mode data. |
| 294 | std::string streaming_file_name_; |
| 295 | Mutex* streaming_lock_; |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 296 | std::map<const DexFile*, DexIndexBitSet*> seen_methods_; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 297 | std::unique_ptr<ThreadIDBitSet> seen_threads_; |
| 298 | |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 299 | // Bijective map from ArtMethod* to index. |
| 300 | // Map from ArtMethod* to index in unique_methods_; |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 301 | Mutex* unique_methods_lock_ ACQUIRED_AFTER(streaming_lock_); |
Mathieu Chartier | 4d64cd4 | 2015-06-02 16:38:29 -0700 | [diff] [blame] | 302 | std::unordered_map<ArtMethod*, uint32_t> art_method_id_map_ GUARDED_BY(unique_methods_lock_); |
| 303 | std::vector<ArtMethod*> unique_methods_ GUARDED_BY(unique_methods_lock_); |
| 304 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 305 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 306 | }; |
| 307 | |
| 308 | } // namespace art |
| 309 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 310 | #endif // ART_RUNTIME_TRACE_H_ |