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 | |
| 17 | #ifndef ART_SRC_TRACE_H_ |
| 18 | #define ART_SRC_TRACE_H_ |
| 19 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | #include <set> |
| 22 | #include <string> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 25 | #include "globals.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 26 | #include "instrumentation.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 28 | #include "safe_map.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 29 | #include "UniquePtr.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | namespace mirror { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 34 | class AbstractMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | } // namespace mirror |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 36 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 38 | enum ProfilerClockSource { |
| 39 | kProfilerClockSourceThreadCpu, |
| 40 | kProfilerClockSourceWall, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 41 | kProfilerClockSourceDual, // Both wall and thread CPU clocks. |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 44 | class Trace : public instrumentation::InstrumentationListener { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 45 | public: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 46 | enum TraceFlag { |
| 47 | kTraceCountAllocs = 1, |
| 48 | }; |
| 49 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 50 | static void SetDefaultClockSource(ProfilerClockSource clock_source); |
| 51 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 52 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, |
| 53 | bool direct_to_ddms) |
| 54 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 55 | Locks::thread_list_lock_, |
| 56 | Locks::thread_suspend_count_lock_, |
| 57 | Locks::trace_lock_); |
| 58 | static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_); |
| 59 | static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_); |
| 60 | static bool IsMethodTracingActive() LOCKS_EXCLUDED(Locks::trace_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 61 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 62 | bool UseWallClock(); |
| 63 | bool UseThreadCpuClock(); |
| 64 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 65 | virtual void MethodEntered(Thread* thread, mirror::Object* this_object, |
| 66 | const mirror::AbstractMethod* method, uint32_t dex_pc) |
| 67 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 68 | virtual void MethodExited(Thread* thread, mirror::Object* this_object, |
| 69 | const mirror::AbstractMethod* method, uint32_t dex_pc, |
| 70 | const JValue& return_value) |
| 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 72 | virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method, uint32_t dex_pc) |
| 73 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 74 | virtual void DexPcMoved(Thread* thread, mirror::Object* this_object, |
| 75 | const mirror::AbstractMethod* method, uint32_t new_dex_pc) |
| 76 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 77 | virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
| 78 | mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc, |
| 79 | mirror::Throwable* exception_object) |
| 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 81 | private: |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 82 | explicit Trace(File* trace_file, int buffer_size, int flags); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 83 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 84 | void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 85 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 86 | void LogMethodTraceEvent(Thread* thread, const mirror::AbstractMethod* method, |
| 87 | instrumentation::Instrumentation::InstrumentationEvent event); |
| 88 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 89 | // Methods to output traced methods and threads. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 90 | void GetVisitedMethods(size_t end_offset, std::set<mirror::AbstractMethod*>* visited_methods); |
| 91 | void DumpMethodList(std::ostream& os, const std::set<mirror::AbstractMethod*>& visited_methods) |
| 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 93 | void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 94 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 95 | // Singleton instance of the Trace or NULL when no method tracing is active. |
| 96 | static Trace* the_trace_ GUARDED_BY(Locks::trace_lock_); |
| 97 | |
| 98 | // The default profiler clock source. |
| 99 | static ProfilerClockSource default_clock_source_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 100 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 101 | // Maps a thread to its clock base. |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 102 | SafeMap<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 103 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 104 | // File to write trace data out to, NULL if direct to ddms. |
| 105 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 106 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 107 | // Buffer to store trace data. |
| 108 | UniquePtr<uint8_t> buf_; |
| 109 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 110 | // Flags enabling extra tracing of things such as alloc counts. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 111 | const int flags_; |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 112 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 113 | const ProfilerClockSource clock_source_; |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 115 | // Size of buf_. |
| 116 | const int buffer_size_; |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 117 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 118 | // Time trace was created. |
| 119 | const uint64_t start_time_; |
| 120 | |
| 121 | // Offset into buf_. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 122 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 123 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 124 | // Did we overflow the buffer recording traces? |
| 125 | bool overflow_; |
| 126 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 128 | }; |
| 129 | |
| 130 | } // namespace art |
| 131 | |
| 132 | #endif // ART_SRC_TRACE_H_ |