blob: 1af12831e7adb3104c40815e7ba426dc716635af [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
jeffhaoa9ef3fd2011-12-13 18:33:43 -080020#include <ostream>
21#include <set>
22#include <string>
Jeff Hao0abc72e2013-08-13 13:45:14 -070023#include <vector>
jeffhaoe343b762011-12-05 16:36:44 -080024
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080026#include "globals.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080027#include "instrumentation.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070029#include "safe_map.h"
jeffhao2692b572011-12-16 15:42:28 -080030#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080031
32namespace art {
33
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070035 class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036} // namespace mirror
jeffhaoa9ef3fd2011-12-13 18:33:43 -080037class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080038
Elliott Hughescfbe73d2012-05-22 17:37:06 -070039enum ProfilerClockSource {
40 kProfilerClockSourceThreadCpu,
41 kProfilerClockSourceWall,
Ian Rogers62d6c772013-02-27 08:32:07 -080042 kProfilerClockSourceDual, // Both wall and thread CPU clocks.
Elliott Hughescfbe73d2012-05-22 17:37:06 -070043};
44
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080045#if defined(HAVE_POSIX_CLOCKS)
46const ProfilerClockSource kDefaultProfilerClockSource = kProfilerClockSourceDual;
47#else
48const ProfilerClockSource kDefaultProfilerClockSource = kProfilerClockSourceWall;
49#endif
50
Jeff Hao64caa7d2013-08-29 11:18:01 -070051enum TracingMode {
52 kTracingInactive,
53 kMethodTracingActive,
54 kSampleProfilingActive,
55};
56
Ian Rogers62d6c772013-02-27 08:32:07 -080057class Trace : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080058 public:
jeffhao0791adc2012-04-04 11:14:32 -070059 enum TraceFlag {
60 kTraceCountAllocs = 1,
61 };
62
Elliott Hughescfbe73d2012-05-22 17:37:06 -070063 static void SetDefaultClockSource(ProfilerClockSource clock_source);
64
Ian Rogers62d6c772013-02-27 08:32:07 -080065 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -070066 bool direct_to_ddms, bool sampling_enabled, int interval_us)
Ian Rogers62d6c772013-02-27 08:32:07 -080067 LOCKS_EXCLUDED(Locks::mutator_lock_,
68 Locks::thread_list_lock_,
69 Locks::thread_suspend_count_lock_,
70 Locks::trace_lock_);
71 static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_);
72 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -070073 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080074
Elliott Hughescfbe73d2012-05-22 17:37:06 -070075 bool UseWallClock();
76 bool UseThreadCpuClock();
77
Ian Rogerse2f77e72013-08-13 19:19:40 -070078 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -070079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80
Ian Rogers62d6c772013-02-27 08:32:07 -080081 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 mirror::ArtMethod* method, uint32_t dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080086 const JValue& return_value)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz51db44a2013-11-19 10:00:29 +010088 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 mirror::ArtMethod* method, uint32_t dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070095 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080096 mirror::Throwable* exception_object)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070098
Jeff Hao5ce4b172013-08-16 16:27:18 -070099 // Reuse an old stack trace if it exists, otherwise allocate a new one.
100 static std::vector<mirror::ArtMethod*>* AllocStackTrace();
101 // Clear and store an old stack trace for later use.
102 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace);
103
jeffhaoe343b762011-12-05 16:36:44 -0800104 private:
Jeff Hao23009dc2013-08-22 15:36:42 -0700105 explicit Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled);
jeffhao2692b572011-12-16 15:42:28 -0800106
Jeff Hao23009dc2013-08-22 15:36:42 -0700107 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700108 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
109
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800111
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700112 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
113
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 void LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700115 instrumentation::Instrumentation::InstrumentationEvent event,
116 uint32_t thread_clock_diff, uint32_t wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800117
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800118 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700119 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
120 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800123
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700125 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800126
127 // The default profiler clock source.
128 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800129
Jeff Hao0abc72e2013-08-13 13:45:14 -0700130 // Sampling thread, non-zero when sampling.
131 static pthread_t sampling_pthread_;
132
Jeff Hao5ce4b172013-08-16 16:27:18 -0700133 // Used to remember an unused stack trace to avoid re-allocation during sampling.
134 static UniquePtr<std::vector<mirror::ArtMethod*> > temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800135
jeffhao2692b572011-12-16 15:42:28 -0800136 // File to write trace data out to, NULL if direct to ddms.
137 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800138
jeffhao2692b572011-12-16 15:42:28 -0800139 // Buffer to store trace data.
140 UniquePtr<uint8_t> buf_;
141
jeffhao0791adc2012-04-04 11:14:32 -0700142 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700144
Jeff Hao23009dc2013-08-22 15:36:42 -0700145 // True if traceview should sample instead of instrumenting method entry/exit.
146 const bool sampling_enabled_;
147
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700149
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 // Size of buf_.
151 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800152
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 // Time trace was created.
154 const uint64_t start_time_;
155
156 // Offset into buf_.
jeffhao2692b572011-12-16 15:42:28 -0800157 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800158
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 // Did we overflow the buffer recording traces?
160 bool overflow_;
161
jeffhaoe343b762011-12-05 16:36:44 -0800162 DISALLOW_COPY_AND_ASSIGN(Trace);
163};
164
165} // namespace art
166
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700167#endif // ART_RUNTIME_TRACE_H_