blob: d8bd428bf9f45d263185238bd0e3dd6edb2dbec1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhaoe343b762011-12-05 16:36:44 -080016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_TRACE_H_
18#define ART_RUNTIME_TRACE_H_
jeffhaoe343b762011-12-05 16:36:44 -080019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
jeffhaoa9ef3fd2011-12-13 18:33:43 -080021#include <ostream>
22#include <set>
23#include <string>
Jeff Hao0abc72e2013-08-13 13:45:14 -070024#include <vector>
jeffhaoe343b762011-12-05 16:36:44 -080025
Ian Rogers8ab25ef2014-07-09 18:00:50 -070026#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080028#include "globals.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080029#include "instrumentation.h"
Elliott Hughes76160052012-12-12 16:31:20 -080030#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070031#include "safe_map.h"
jeffhaoe343b762011-12-05 16:36:44 -080032
33namespace art {
34
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070036 class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037} // namespace mirror
Ian Rogerse63db272014-07-15 15:36:11 -070038
Mathieu Chartierc7853442015-03-27 14:35:38 -070039class ArtField;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080040class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080041
Jeff Hao64caa7d2013-08-29 11:18:01 -070042enum TracingMode {
43 kTracingInactive,
44 kMethodTracingActive,
45 kSampleProfilingActive,
46};
47
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020048class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080049 public:
jeffhao0791adc2012-04-04 11:14:32 -070050 enum TraceFlag {
51 kTraceCountAllocs = 1,
52 };
53
Andreas Gampe7e7e0f42015-03-29 15:26:23 -070054 enum class TraceOutputMode {
55 kFile,
56 kDDMS
57 };
58
59 enum class TraceMode {
60 kMethodTracing,
61 kSampling
62 };
63
Ian Rogerse63db272014-07-15 15:36:11 -070064 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -070065
Ian Rogers62d6c772013-02-27 08:32:07 -080066 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Andreas Gampe7e7e0f42015-03-29 15:26:23 -070067 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us)
Sebastien Hertzbae182c2013-12-17 10:42:03 +010068 LOCKS_EXCLUDED(Locks::mutator_lock_,
69 Locks::thread_list_lock_,
70 Locks::thread_suspend_count_lock_,
71 Locks::trace_lock_);
72 static void Stop()
73 LOCKS_EXCLUDED(Locks::mutator_lock_,
74 Locks::thread_list_lock_,
75 Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -080076 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -070077 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080078
Elliott Hughescfbe73d2012-05-22 17:37:06 -070079 bool UseWallClock();
80 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -070081 void MeasureClockOverhead();
82 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -070083
Ian Rogerse2f77e72013-08-13 19:19:40 -070084 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020087 // InstrumentationListener implementation.
88 void MethodEntered(Thread* thread, mirror::Object* this_object,
89 mirror::ArtMethod* method, uint32_t dex_pc)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
91 void MethodExited(Thread* thread, mirror::Object* this_object,
92 mirror::ArtMethod* method, uint32_t dex_pc,
93 const JValue& return_value)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
95 void MethodUnwind(Thread* thread, mirror::Object* this_object,
96 mirror::ArtMethod* method, uint32_t dex_pc)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
98 void DexPcMoved(Thread* thread, mirror::Object* this_object,
99 mirror::ArtMethod* method, uint32_t new_dex_pc)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
101 void FieldRead(Thread* thread, mirror::Object* this_object,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
104 void FieldWritten(Thread* thread, mirror::Object* this_object,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700105 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200106 const JValue& field_value)
107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000108 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700112 // Reuse an old stack trace if it exists, otherwise allocate a new one.
113 static std::vector<mirror::ArtMethod*>* AllocStackTrace();
114 // Clear and store an old stack trace for later use.
115 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700116 // Save id and name of a thread before it exits.
117 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700118
jeffhaoe343b762011-12-05 16:36:44 -0800119 private:
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700120 explicit Trace(File* trace_file, int buffer_size, int flags, TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800121
Jeff Hao23009dc2013-08-22 15:36:42 -0700122 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700123 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
124
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800126
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700127 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
128
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700130 instrumentation::Instrumentation::InstrumentationEvent event,
131 uint32_t thread_clock_diff, uint32_t wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800132
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800133 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700134 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
135 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700137 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800138
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700140 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800141
142 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700143 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800144
Jeff Hao0abc72e2013-08-13 13:45:14 -0700145 // Sampling thread, non-zero when sampling.
146 static pthread_t sampling_pthread_;
147
Jeff Hao5ce4b172013-08-16 16:27:18 -0700148 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Ian Rogers700a4022014-05-19 16:49:03 -0700149 static std::unique_ptr<std::vector<mirror::ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800150
jeffhao2692b572011-12-16 15:42:28 -0800151 // File to write trace data out to, NULL if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700152 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800153
jeffhao2692b572011-12-16 15:42:28 -0800154 // Buffer to store trace data.
Ian Rogers700a4022014-05-19 16:49:03 -0700155 std::unique_ptr<uint8_t> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800156
jeffhao0791adc2012-04-04 11:14:32 -0700157 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700159
Jeff Hao23009dc2013-08-22 15:36:42 -0700160 // True if traceview should sample instead of instrumenting method entry/exit.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700161 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700162
Ian Rogerse63db272014-07-15 15:36:11 -0700163 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700164
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 // Size of buf_.
166 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800167
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 // Time trace was created.
169 const uint64_t start_time_;
170
Jeff Haoc5d824a2014-07-28 18:35:38 -0700171 // Clock overhead.
172 const uint32_t clock_overhead_ns_;
173
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 // Offset into buf_.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700175 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800176
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 // Did we overflow the buffer recording traces?
178 bool overflow_;
179
Jeff Haoe094b872014-10-14 13:12:01 -0700180 // Map of thread ids and names that have already exited.
181 SafeMap<pid_t, std::string> exited_threads_;
182
jeffhaoe343b762011-12-05 16:36:44 -0800183 DISALLOW_COPY_AND_ASSIGN(Trace);
184};
185
186} // namespace art
187
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700188#endif // ART_RUNTIME_TRACE_H_