blob: ffcb36d29411385d80cd0cf9faeacd6c7d70e794 [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
Jeff Hao64caa7d2013-08-29 11:18:01 -070045enum TracingMode {
46 kTracingInactive,
47 kMethodTracingActive,
48 kSampleProfilingActive,
49};
50
Ian Rogers62d6c772013-02-27 08:32:07 -080051class Trace : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080052 public:
jeffhao0791adc2012-04-04 11:14:32 -070053 enum TraceFlag {
54 kTraceCountAllocs = 1,
55 };
56
Elliott Hughescfbe73d2012-05-22 17:37:06 -070057 static void SetDefaultClockSource(ProfilerClockSource clock_source);
58
Ian Rogers62d6c772013-02-27 08:32:07 -080059 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -070060 bool direct_to_ddms, bool sampling_enabled, int interval_us)
Ian Rogers62d6c772013-02-27 08:32:07 -080061 LOCKS_EXCLUDED(Locks::mutator_lock_,
62 Locks::thread_list_lock_,
63 Locks::thread_suspend_count_lock_,
64 Locks::trace_lock_);
65 static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_);
66 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -070067 static TracingMode GetMethodTracingMode() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080068
Elliott Hughescfbe73d2012-05-22 17:37:06 -070069 bool UseWallClock();
70 bool UseThreadCpuClock();
71
Ian Rogerse2f77e72013-08-13 19:19:40 -070072 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74
Ian Rogers62d6c772013-02-27 08:32:07 -080075 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070076 const mirror::ArtMethod* method, uint32_t dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070079 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080080 const JValue& return_value)
81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -070082 virtual void MethodUnwind(Thread* thread, const mirror::ArtMethod* method, uint32_t dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070085 const mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
87 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070088 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080089 mirror::Throwable* exception_object)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070091
Jeff Hao5ce4b172013-08-16 16:27:18 -070092 // Reuse an old stack trace if it exists, otherwise allocate a new one.
93 static std::vector<mirror::ArtMethod*>* AllocStackTrace();
94 // Clear and store an old stack trace for later use.
95 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace);
96
jeffhaoe343b762011-12-05 16:36:44 -080097 private:
Jeff Hao23009dc2013-08-22 15:36:42 -070098 explicit Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled);
jeffhao2692b572011-12-16 15:42:28 -080099
Jeff Hao23009dc2013-08-22 15:36:42 -0700100 // The sampling interval in microseconds is passed as an argument.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700101 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
102
Ian Rogersb726dcb2012-09-05 08:57:23 -0700103 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -0800104
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700105 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
106
Brian Carlstromea46f952013-07-30 01:26:50 -0700107 void LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700108 instrumentation::Instrumentation::InstrumentationEvent event,
109 uint32_t thread_clock_diff, uint32_t wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800110
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800111 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700112 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
113 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800116
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700118 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800119
120 // The default profiler clock source.
121 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800122
Jeff Hao0abc72e2013-08-13 13:45:14 -0700123 // Sampling thread, non-zero when sampling.
124 static pthread_t sampling_pthread_;
125
Jeff Hao5ce4b172013-08-16 16:27:18 -0700126 // Used to remember an unused stack trace to avoid re-allocation during sampling.
127 static UniquePtr<std::vector<mirror::ArtMethod*> > temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800128
jeffhao2692b572011-12-16 15:42:28 -0800129 // File to write trace data out to, NULL if direct to ddms.
130 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800131
jeffhao2692b572011-12-16 15:42:28 -0800132 // Buffer to store trace data.
133 UniquePtr<uint8_t> buf_;
134
jeffhao0791adc2012-04-04 11:14:32 -0700135 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700137
Jeff Hao23009dc2013-08-22 15:36:42 -0700138 // True if traceview should sample instead of instrumenting method entry/exit.
139 const bool sampling_enabled_;
140
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700142
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 // Size of buf_.
144 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800145
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 // Time trace was created.
147 const uint64_t start_time_;
148
149 // Offset into buf_.
jeffhao2692b572011-12-16 15:42:28 -0800150 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800151
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 // Did we overflow the buffer recording traces?
153 bool overflow_;
154
jeffhaoe343b762011-12-05 16:36:44 -0800155 DISALLOW_COPY_AND_ASSIGN(Trace);
156};
157
158} // namespace art
159
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700160#endif // ART_RUNTIME_TRACE_H_