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 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 21 | #include <ostream> |
| 22 | #include <set> |
| 23 | #include <string> |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 24 | #include <vector> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 25 | |
Ian Rogers | 8ab25ef | 2014-07-09 18:00:50 -0700 | [diff] [blame] | 26 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 28 | #include "globals.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 29 | #include "instrumentation.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 31 | #include "safe_map.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | namespace mirror { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 36 | class ArtField; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 37 | class ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | } // namespace mirror |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 39 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 40 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 41 | |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 42 | enum TracingMode { |
| 43 | kTracingInactive, |
| 44 | kMethodTracingActive, |
| 45 | kSampleProfilingActive, |
| 46 | }; |
| 47 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 48 | class Trace FINAL : public instrumentation::InstrumentationListener { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 49 | public: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 50 | enum TraceFlag { |
| 51 | kTraceCountAllocs = 1, |
| 52 | }; |
| 53 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 54 | static void SetDefaultClockSource(TraceClockSource clock_source); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 57 | bool direct_to_ddms, bool sampling_enabled, int interval_us) |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 58 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 59 | Locks::thread_list_lock_, |
| 60 | Locks::thread_suspend_count_lock_, |
| 61 | Locks::trace_lock_); |
| 62 | static void Stop() |
| 63 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 64 | Locks::thread_list_lock_, |
| 65 | Locks::trace_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 66 | static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_); |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 67 | static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 68 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 69 | bool UseWallClock(); |
| 70 | bool UseThreadCpuClock(); |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame^] | 71 | void MeasureClockOverhead(); |
| 72 | uint32_t GetClockOverheadNanoSeconds(); |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 73 | |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 74 | void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace) |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 77 | // InstrumentationListener implementation. |
| 78 | void MethodEntered(Thread* thread, mirror::Object* this_object, |
| 79 | mirror::ArtMethod* method, uint32_t dex_pc) |
| 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 81 | void MethodExited(Thread* thread, mirror::Object* this_object, |
| 82 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 83 | const JValue& return_value) |
| 84 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 85 | void MethodUnwind(Thread* thread, mirror::Object* this_object, |
| 86 | mirror::ArtMethod* method, uint32_t dex_pc) |
| 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 88 | void DexPcMoved(Thread* thread, mirror::Object* this_object, |
| 89 | mirror::ArtMethod* method, uint32_t new_dex_pc) |
| 90 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 91 | void FieldRead(Thread* thread, mirror::Object* this_object, |
| 92 | mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field) |
| 93 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 94 | void FieldWritten(Thread* thread, mirror::Object* this_object, |
| 95 | mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field, |
| 96 | const JValue& field_value) |
| 97 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
| 98 | void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
| 99 | mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, |
| 100 | mirror::Throwable* exception_object) |
| 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 102 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 103 | // Reuse an old stack trace if it exists, otherwise allocate a new one. |
| 104 | static std::vector<mirror::ArtMethod*>* AllocStackTrace(); |
| 105 | // Clear and store an old stack trace for later use. |
| 106 | static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace); |
| 107 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 108 | private: |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 109 | explicit Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 110 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 111 | // The sampling interval in microseconds is passed as an argument. |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 112 | static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_); |
| 113 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 114 | void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 115 | |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 116 | void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff); |
| 117 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 118 | void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method, |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 119 | instrumentation::Instrumentation::InstrumentationEvent event, |
| 120 | uint32_t thread_clock_diff, uint32_t wall_clock_diff); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 122 | // Methods to output traced methods and threads. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 123 | void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods); |
| 124 | void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 125 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 126 | void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 127 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 128 | // 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] | 129 | static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 130 | |
| 131 | // The default profiler clock source. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 132 | static TraceClockSource default_clock_source_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 133 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 134 | // Sampling thread, non-zero when sampling. |
| 135 | static pthread_t sampling_pthread_; |
| 136 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 137 | // Used to remember an unused stack trace to avoid re-allocation during sampling. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 138 | static std::unique_ptr<std::vector<mirror::ArtMethod*>> temp_stack_trace_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 139 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 140 | // File to write trace data out to, NULL if direct to ddms. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 141 | std::unique_ptr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 142 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 143 | // Buffer to store trace data. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 144 | std::unique_ptr<uint8_t> buf_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 145 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 146 | // Flags enabling extra tracing of things such as alloc counts. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 147 | const int flags_; |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 148 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 149 | // True if traceview should sample instead of instrumenting method entry/exit. |
| 150 | const bool sampling_enabled_; |
| 151 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 152 | const TraceClockSource clock_source_; |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 153 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 154 | // Size of buf_. |
| 155 | const int buffer_size_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 156 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | // Time trace was created. |
| 158 | const uint64_t start_time_; |
| 159 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame^] | 160 | // Clock overhead. |
| 161 | const uint32_t clock_overhead_ns_; |
| 162 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 163 | // Offset into buf_. |
Ian Rogers | 8ab25ef | 2014-07-09 18:00:50 -0700 | [diff] [blame] | 164 | AtomicInteger cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 165 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 166 | // Did we overflow the buffer recording traces? |
| 167 | bool overflow_; |
| 168 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 169 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 170 | }; |
| 171 | |
| 172 | } // namespace art |
| 173 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 174 | #endif // ART_RUNTIME_TRACE_H_ |