blob: 6fc3790a23fd282a11bf7318aae651e1bdb8133d [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
Ian Rogers62d6c772013-02-27 08:32:07 -080045class Trace : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080046 public:
jeffhao0791adc2012-04-04 11:14:32 -070047 enum TraceFlag {
48 kTraceCountAllocs = 1,
49 };
50
Elliott Hughescfbe73d2012-05-22 17:37:06 -070051 static void SetDefaultClockSource(ProfilerClockSource clock_source);
52
Ian Rogers62d6c772013-02-27 08:32:07 -080053 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
54 bool direct_to_ddms)
55 LOCKS_EXCLUDED(Locks::mutator_lock_,
56 Locks::thread_list_lock_,
57 Locks::thread_suspend_count_lock_,
58 Locks::trace_lock_);
59 static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_);
60 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
61 static bool IsMethodTracingActive() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080062
Elliott Hughescfbe73d2012-05-22 17:37:06 -070063 bool UseWallClock();
64 bool UseThreadCpuClock();
65
Ian Rogerse2f77e72013-08-13 19:19:40 -070066 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::ArtMethod*>* stack_trace)
Jeff Hao0abc72e2013-08-13 13:45:14 -070067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68
Ian Rogers62d6c772013-02-27 08:32:07 -080069 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070070 const mirror::ArtMethod* method, uint32_t dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070073 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080074 const JValue& return_value)
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -070076 virtual void MethodUnwind(Thread* thread, 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 DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070079 const mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070082 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080083 mirror::Throwable* exception_object)
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070085
Jeff Hao5ce4b172013-08-16 16:27:18 -070086 // Reuse an old stack trace if it exists, otherwise allocate a new one.
87 static std::vector<mirror::ArtMethod*>* AllocStackTrace();
88 // Clear and store an old stack trace for later use.
89 static void FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace);
90
Jeff Hao0abc72e2013-08-13 13:45:14 -070091 ~Trace();
92
jeffhaoe343b762011-12-05 16:36:44 -080093 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070094 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080095
Jeff Hao0abc72e2013-08-13 13:45:14 -070096 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
97
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080099
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700100 void ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff);
101
Brian Carlstromea46f952013-07-30 01:26:50 -0700102 void LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700103 instrumentation::Instrumentation::InstrumentationEvent event,
104 uint32_t thread_clock_diff, uint32_t wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800105
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800106 // Methods to output traced methods and threads.
Brian Carlstromea46f952013-07-30 01:26:50 -0700107 void GetVisitedMethods(size_t end_offset, std::set<mirror::ArtMethod*>* visited_methods);
108 void DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods)
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800111
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700113 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800114
115 // The default profiler clock source.
116 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800117
Jeff Hao0abc72e2013-08-13 13:45:14 -0700118 // True if traceview should sample instead of instrumenting method entry/exit.
119 static bool sampling_enabled_;
120
121 // Sampling interval in microseconds.
122 static uint32_t sampling_interval_us_;
123
124 // Sampling thread, non-zero when sampling.
125 static pthread_t sampling_pthread_;
126
Jeff Hao5ce4b172013-08-16 16:27:18 -0700127 // Used to remember an unused stack trace to avoid re-allocation during sampling.
128 static UniquePtr<std::vector<mirror::ArtMethod*> > temp_stack_trace_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800129
jeffhao2692b572011-12-16 15:42:28 -0800130 // File to write trace data out to, NULL if direct to ddms.
131 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800132
jeffhao2692b572011-12-16 15:42:28 -0800133 // Buffer to store trace data.
134 UniquePtr<uint8_t> buf_;
135
jeffhao0791adc2012-04-04 11:14:32 -0700136 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700138
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700140
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 // Size of buf_.
142 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800143
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 // Time trace was created.
145 const uint64_t start_time_;
146
147 // Offset into buf_.
jeffhao2692b572011-12-16 15:42:28 -0800148 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800149
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 // Did we overflow the buffer recording traces?
151 bool overflow_;
152
jeffhaoe343b762011-12-05 16:36:44 -0800153 DISALLOW_COPY_AND_ASSIGN(Trace);
154};
155
156} // namespace art
157
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700158#endif // ART_RUNTIME_TRACE_H_