blob: 06824b8be884e548cca9a32acdf174a4189b66f3 [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
Andreas Gampe40da2862015-02-27 12:49:04 -080020#include <bitset>
21#include <map>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
jeffhaoa9ef3fd2011-12-13 18:33:43 -080023#include <ostream>
24#include <set>
25#include <string>
Jeff Hao0abc72e2013-08-13 13:45:14 -070026#include <vector>
jeffhaoe343b762011-12-05 16:36:44 -080027
Ian Rogers8ab25ef2014-07-09 18:00:50 -070028#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080030#include "globals.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080031#include "instrumentation.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
jeffhaoe343b762011-12-05 16:36:44 -080034
35namespace art {
36
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070038 class ArtMethod;
Andreas Gampe40da2862015-02-27 12:49:04 -080039 class DexCache;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040} // namespace mirror
Ian Rogerse63db272014-07-15 15:36:11 -070041
Mathieu Chartierc7853442015-03-27 14:35:38 -070042class ArtField;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080043class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080044
Andreas Gampe40da2862015-02-27 12:49:04 -080045using DexIndexBitSet = std::bitset<65536>;
46using ThreadIDBitSet = std::bitset<65536>;
47
Jeff Hao64caa7d2013-08-29 11:18:01 -070048enum TracingMode {
49 kTracingInactive,
50 kMethodTracingActive,
51 kSampleProfilingActive,
52};
53
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020054class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080055 public:
jeffhao0791adc2012-04-04 11:14:32 -070056 enum TraceFlag {
57 kTraceCountAllocs = 1,
58 };
59
Andreas Gampe7e7e0f42015-03-29 15:26:23 -070060 enum class TraceOutputMode {
61 kFile,
Andreas Gampe40da2862015-02-27 12:49:04 -080062 kDDMS,
63 kStreaming
Andreas Gampe7e7e0f42015-03-29 15:26:23 -070064 };
65
66 enum class TraceMode {
67 kMethodTracing,
68 kSampling
69 };
70
Andreas Gampe40da2862015-02-27 12:49:04 -080071 ~Trace();
72
Ian Rogerse63db272014-07-15 15:36:11 -070073 static void SetDefaultClockSource(TraceClockSource clock_source);
Elliott Hughescfbe73d2012-05-22 17:37:06 -070074
Andreas Gampee34a42c2015-04-25 14:44:29 -070075 static void Start(const char* trace_filename, int trace_fd, size_t buffer_size, int flags,
Andreas Gampe7e7e0f42015-03-29 15:26:23 -070076 TraceOutputMode output_mode, TraceMode trace_mode, int interval_us)
Sebastien Hertzbae182c2013-12-17 10:42:03 +010077 LOCKS_EXCLUDED(Locks::mutator_lock_,
78 Locks::thread_list_lock_,
79 Locks::thread_suspend_count_lock_,
80 Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -080081 static void Pause() LOCKS_EXCLUDED(Locks::trace_lock_, Locks::thread_list_lock_);
82 static void Resume() LOCKS_EXCLUDED(Locks::trace_lock_);
83
84 // Stop tracing. This will finish the trace and write it to file/send it via DDMS.
Sebastien Hertzbae182c2013-12-17 10:42:03 +010085 static void Stop()
Andreas Gampe40da2862015-02-27 12:49:04 -080086 LOCKS_EXCLUDED(Locks::mutator_lock_,
87 Locks::thread_list_lock_,
88 Locks::trace_lock_);
89 // Abort tracing. This will just stop tracing and *not* write/send the collected data.
90 static void Abort()
Sebastien Hertzbae182c2013-12-17 10:42:03 +010091 LOCKS_EXCLUDED(Locks::mutator_lock_,
92 Locks::thread_list_lock_,
93 Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -080094 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -070095 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080096
Elliott Hughescfbe73d2012-05-22 17:37:06 -070097 bool UseWallClock();
98 bool UseThreadCpuClock();
Jeff Haoc5d824a2014-07-28 18:35:38 -070099 void MeasureClockOverhead();
100 uint32_t GetClockOverheadNanoSeconds();
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700101
Ian Rogerse2f77e72013-08-13 19:19:40 -0700102 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -0700103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200105 // InstrumentationListener implementation.
106 void MethodEntered(Thread* thread, mirror::Object* this_object,
107 mirror::ArtMethod* method, uint32_t dex_pc)
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
109 void MethodExited(Thread* thread, mirror::Object* this_object,
110 mirror::ArtMethod* method, uint32_t dex_pc,
111 const JValue& return_value)
112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
113 void MethodUnwind(Thread* thread, mirror::Object* this_object,
114 mirror::ArtMethod* method, uint32_t dex_pc)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
116 void DexPcMoved(Thread* thread, mirror::Object* this_object,
117 mirror::ArtMethod* method, uint32_t new_dex_pc)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
119 void FieldRead(Thread* thread, mirror::Object* this_object,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
122 void FieldWritten(Thread* thread, mirror::Object* this_object,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700123 mirror::ArtMethod* method, uint32_t dex_pc, ArtField* field,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200124 const JValue& field_value)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000126 void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Jeff Hao5ce4b172013-08-16 16:27:18 -0700130 // Reuse an old stack trace if it exists, otherwise allocate a new one.
131 static std::vector<mirror::ArtMethod*>* AllocStackTrace();
132 // Clear and store an old stack trace for later use.
133 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace);
Jeff Haoe094b872014-10-14 13:12:01 -0700134 // Save id and name of a thread before it exits.
135 static void StoreExitingThreadInfo(Thread* thread);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700136
Andreas Gampe40da2862015-02-27 12:49:04 -0800137 static TraceOutputMode GetOutputMode() LOCKS_EXCLUDED(Locks::trace_lock_);
138 static TraceMode GetMode() LOCKS_EXCLUDED(Locks::trace_lock_);
Andreas Gampee34a42c2015-04-25 14:44:29 -0700139 static size_t GetBufferSize() LOCKS_EXCLUDED(Locks::trace_lock_);
Andreas Gampe40da2862015-02-27 12:49:04 -0800140
jeffhaoe343b762011-12-05 16:36:44 -0800141 private:
Andreas Gampee34a42c2015-04-25 14:44:29 -0700142 Trace(File* trace_file, const char* trace_name, size_t buffer_size, int flags,
Andreas Gampe40da2862015-02-27 12:49:04 -0800143 TraceOutputMode output_mode, TraceMode trace_mode);
jeffhao2692b572011-12-16 15:42:28 -0800144
Jeff Hao23009dc2013-08-22 15:36:42 -0700145 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700146 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
147
Andreas Gampe40da2862015-02-27 12:49:04 -0800148 static void StopTracing(bool finish_tracing, bool flush_file);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800150
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700151 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
152
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700154 instrumentation::Instrumentation::InstrumentationEvent event,
Andreas Gampe40da2862015-02-27 12:49:04 -0800155 uint32_t thread_clock_diff, uint32_t wall_clock_diff)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800157
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800158 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700159 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
160 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800163
Andreas Gampe40da2862015-02-27 12:49:04 -0800164 // Methods to register seen entitites in streaming mode. The methods return true if the entity
165 // is newly discovered.
166 bool RegisterMethod(mirror::ArtMethod* method)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
168 bool RegisterThread(Thread* thread)
169 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
170
171 // Copy a temporary buffer to the main buffer. Used for streaming. Exposed here for lock
172 // annotation.
173 void WriteToBuf(const uint8_t* src, size_t src_size)
174 EXCLUSIVE_LOCKS_REQUIRED(streaming_lock_);
175
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 // Singleton instance of the Trace or null when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700177 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800178
179 // The default profiler clock source.
Ian Rogerse63db272014-07-15 15:36:11 -0700180 static TraceClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800181
Jeff Hao0abc72e2013-08-13 13:45:14 -0700182 // Sampling thread, non-zero when sampling.
183 static pthread_t sampling_pthread_;
184
Jeff Hao5ce4b172013-08-16 16:27:18 -0700185 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Ian Rogers700a4022014-05-19 16:49:03 -0700186 static std::unique_ptr<std::vector<mirror::ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800187
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700188 // File to write trace data out to, null if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700189 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800190
jeffhao2692b572011-12-16 15:42:28 -0800191 // Buffer to store trace data.
Ian Rogers700a4022014-05-19 16:49:03 -0700192 std::unique_ptr<uint8_t> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800193
jeffhao0791adc2012-04-04 11:14:32 -0700194 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700196
Andreas Gampe40da2862015-02-27 12:49:04 -0800197 // The kind of output for this tracing.
198 const TraceOutputMode trace_output_mode_;
199
200 // The tracing method.
Andreas Gampe7e7e0f42015-03-29 15:26:23 -0700201 const TraceMode trace_mode_;
Jeff Hao23009dc2013-08-22 15:36:42 -0700202
Ian Rogerse63db272014-07-15 15:36:11 -0700203 const TraceClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700204
Ian Rogers62d6c772013-02-27 08:32:07 -0800205 // Size of buf_.
Andreas Gampee34a42c2015-04-25 14:44:29 -0700206 const size_t buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800207
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 // Time trace was created.
209 const uint64_t start_time_;
210
Jeff Haoc5d824a2014-07-28 18:35:38 -0700211 // Clock overhead.
212 const uint32_t clock_overhead_ns_;
213
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 // Offset into buf_.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700215 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800216
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 // Did we overflow the buffer recording traces?
218 bool overflow_;
219
Jeff Haoe094b872014-10-14 13:12:01 -0700220 // Map of thread ids and names that have already exited.
221 SafeMap<pid_t, std::string> exited_threads_;
222
Andreas Gampe40da2862015-02-27 12:49:04 -0800223 // Sampling profiler sampling interval.
224 int interval_us_;
225
226 // Streaming mode data.
227 std::string streaming_file_name_;
228 Mutex* streaming_lock_;
229 std::map<mirror::DexCache*, DexIndexBitSet*> seen_methods_;
230 std::unique_ptr<ThreadIDBitSet> seen_threads_;
231
jeffhaoe343b762011-12-05 16:36:44 -0800232 DISALLOW_COPY_AND_ASSIGN(Trace);
233};
234
235} // namespace art
236
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700237#endif // ART_RUNTIME_TRACE_H_