blob: 9c8d35bb935d9897d8ef87d634c3e1ee91c48e45 [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 {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020036 class ArtField;
Brian Carlstromea46f952013-07-30 01:26:50 -070037 class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038} // namespace mirror
jeffhaoa9ef3fd2011-12-13 18:33:43 -080039class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080040
Elliott Hughescfbe73d2012-05-22 17:37:06 -070041enum ProfilerClockSource {
42 kProfilerClockSourceThreadCpu,
43 kProfilerClockSourceWall,
Ian Rogers62d6c772013-02-27 08:32:07 -080044 kProfilerClockSourceDual, // Both wall and thread CPU clocks.
Elliott Hughescfbe73d2012-05-22 17:37:06 -070045};
46
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080047#if defined(HAVE_POSIX_CLOCKS)
48const ProfilerClockSource kDefaultProfilerClockSource = kProfilerClockSourceDual;
49#else
50const ProfilerClockSource kDefaultProfilerClockSource = kProfilerClockSourceWall;
51#endif
52
Jeff Hao64caa7d2013-08-29 11:18:01 -070053enum TracingMode {
54 kTracingInactive,
55 kMethodTracingActive,
56 kSampleProfilingActive,
57};
58
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020059class Trace FINAL : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080060 public:
jeffhao0791adc2012-04-04 11:14:32 -070061 enum TraceFlag {
62 kTraceCountAllocs = 1,
63 };
64
Elliott Hughescfbe73d2012-05-22 17:37:06 -070065 static void SetDefaultClockSource(ProfilerClockSource clock_source);
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -070068 bool direct_to_ddms, bool sampling_enabled, int interval_us)
Sebastien Hertzbae182c2013-12-17 10:42:03 +010069 LOCKS_EXCLUDED(Locks::mutator_lock_,
70 Locks::thread_list_lock_,
71 Locks::thread_suspend_count_lock_,
72 Locks::trace_lock_);
73 static void Stop()
74 LOCKS_EXCLUDED(Locks::mutator_lock_,
75 Locks::thread_list_lock_,
76 Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -080077 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -070078 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080079
Elliott Hughescfbe73d2012-05-22 17:37:06 -070080 bool UseWallClock();
81 bool UseThreadCpuClock();
82
Ian Rogerse2f77e72013-08-13 19:19:40 -070083 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -070084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020086 // InstrumentationListener implementation.
87 void MethodEntered(Thread* thread, mirror::Object* this_object,
88 mirror::ArtMethod* method, uint32_t dex_pc)
89 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
90 void MethodExited(Thread* thread, mirror::Object* this_object,
91 mirror::ArtMethod* method, uint32_t dex_pc,
92 const JValue& return_value)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
94 void MethodUnwind(Thread* thread, mirror::Object* this_object,
95 mirror::ArtMethod* method, uint32_t dex_pc)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
97 void DexPcMoved(Thread* thread, mirror::Object* this_object,
98 mirror::ArtMethod* method, uint32_t new_dex_pc)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
100 void FieldRead(Thread* thread, mirror::Object* this_object,
101 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
103 void FieldWritten(Thread* thread, mirror::Object* this_object,
104 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
105 const JValue& field_value)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
107 void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
108 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
109 mirror::Throwable* exception_object)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700111
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);
116
jeffhaoe343b762011-12-05 16:36:44 -0800117 private:
Jeff Hao23009dc2013-08-22 15:36:42 -0700118 explicit Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled);
jeffhao2692b572011-12-16 15:42:28 -0800119
Jeff Hao23009dc2013-08-22 15:36:42 -0700120 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700121 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
122
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800124
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700125 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
126
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700128 instrumentation::Instrumentation::InstrumentationEvent event,
129 uint32_t thread_clock_diff, uint32_t wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800130
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800131 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
133 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800136
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700138 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800139
140 // The default profiler clock source.
141 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800142
Jeff Hao0abc72e2013-08-13 13:45:14 -0700143 // Sampling thread, non-zero when sampling.
144 static pthread_t sampling_pthread_;
145
Jeff Hao5ce4b172013-08-16 16:27:18 -0700146 // Used to remember an unused stack trace to avoid re-allocation during sampling.
Ian Rogers700a4022014-05-19 16:49:03 -0700147 static std::unique_ptr<std::vector<mirror::ArtMethod*>> temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800148
jeffhao2692b572011-12-16 15:42:28 -0800149 // File to write trace data out to, NULL if direct to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700150 std::unique_ptr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800151
jeffhao2692b572011-12-16 15:42:28 -0800152 // Buffer to store trace data.
Ian Rogers700a4022014-05-19 16:49:03 -0700153 std::unique_ptr<uint8_t> buf_;
jeffhao2692b572011-12-16 15:42:28 -0800154
jeffhao0791adc2012-04-04 11:14:32 -0700155 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700157
Jeff Hao23009dc2013-08-22 15:36:42 -0700158 // True if traceview should sample instead of instrumenting method entry/exit.
159 const bool sampling_enabled_;
160
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700162
Ian Rogers62d6c772013-02-27 08:32:07 -0800163 // Size of buf_.
164 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800165
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 // Time trace was created.
167 const uint64_t start_time_;
168
169 // Offset into buf_.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700170 AtomicInteger cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800171
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 // Did we overflow the buffer recording traces?
173 bool overflow_;
174
jeffhaoe343b762011-12-05 16:36:44 -0800175 DISALLOW_COPY_AND_ASSIGN(Trace);
176};
177
178} // namespace art
179
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700180#endif // ART_RUNTIME_TRACE_H_