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 { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 35 | class ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | } // namespace mirror |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 37 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 39 | enum ProfilerClockSource { |
| 40 | kProfilerClockSourceThreadCpu, |
| 41 | kProfilerClockSourceWall, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 42 | kProfilerClockSourceDual, // Both wall and thread CPU clocks. |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 45 | class Trace : public instrumentation::InstrumentationListener { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 46 | public: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 47 | enum TraceFlag { |
| 48 | kTraceCountAllocs = 1, |
| 49 | }; |
| 50 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 51 | static void SetDefaultClockSource(ProfilerClockSource clock_source); |
| 52 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 53 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, |
| 54 | bool direct_to_ddms) |
| 55 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 56 | Locks::thread_list_lock_, |
| 57 | Locks::thread_suspend_count_lock_, |
| 58 | Locks::trace_lock_); |
| 59 | static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_); |
| 60 | static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_); |
| 61 | static bool IsMethodTracingActive() LOCKS_EXCLUDED(Locks::trace_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 62 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 63 | bool UseWallClock(); |
| 64 | bool UseThreadCpuClock(); |
| 65 | |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 66 | void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace) |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 67 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 68 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 69 | virtual void MethodEntered(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 70 | const mirror::ArtMethod* method, uint32_t dex_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 72 | virtual void MethodExited(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 73 | const mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 74 | const JValue& return_value) |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 76 | virtual void MethodUnwind(Thread* thread, const mirror::ArtMethod* method, uint32_t dex_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 77 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 78 | virtual void DexPcMoved(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 79 | const mirror::ArtMethod* method, uint32_t new_dex_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 81 | virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 82 | mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 83 | mirror::Throwable* exception_object) |
| 84 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 85 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 86 | ~Trace(); |
| 87 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 88 | private: |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 89 | explicit Trace(File* trace_file, int buffer_size, int flags); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 90 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 91 | static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_); |
| 92 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 93 | void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 94 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 95 | void LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 96 | instrumentation::Instrumentation::InstrumentationEvent event); |
| 97 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 98 | // Methods to output traced methods and threads. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 99 | void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods); |
| 100 | void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 102 | void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 103 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 104 | // 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] | 105 | static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 106 | |
| 107 | // The default profiler clock source. |
| 108 | static ProfilerClockSource default_clock_source_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 109 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 110 | // True if traceview should sample instead of instrumenting method entry/exit. |
| 111 | static bool sampling_enabled_; |
| 112 | |
| 113 | // Sampling interval in microseconds. |
| 114 | static uint32_t sampling_interval_us_; |
| 115 | |
| 116 | // Sampling thread, non-zero when sampling. |
| 117 | static pthread_t sampling_pthread_; |
| 118 | |
| 119 | // Maps a thread to its most recent stack trace sample. |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 120 | SafeMap<Thread*, std::vector<mirror::ArtMethod*>*> thread_stack_trace_map_; |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 121 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 122 | // Maps a thread to its clock base. |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 123 | SafeMap<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 124 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 125 | // File to write trace data out to, NULL if direct to ddms. |
| 126 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 127 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 128 | // Buffer to store trace data. |
| 129 | UniquePtr<uint8_t> buf_; |
| 130 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 131 | // Flags enabling extra tracing of things such as alloc counts. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 132 | const int flags_; |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 133 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 134 | const ProfilerClockSource clock_source_; |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 135 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 136 | // Size of buf_. |
| 137 | const int buffer_size_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 138 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 139 | // Time trace was created. |
| 140 | const uint64_t start_time_; |
| 141 | |
| 142 | // Offset into buf_. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 143 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 144 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 145 | // Did we overflow the buffer recording traces? |
| 146 | bool overflow_; |
| 147 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 149 | }; |
| 150 | |
| 151 | } // namespace art |
| 152 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 153 | #endif // ART_RUNTIME_TRACE_H_ |