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