blob: d2e6fb162196dbceb799063b5a44ba31ccb16723 [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 {
Mathieu Chartier66f19252012-09-18 08:57:04 -070035class AbstractMethod;
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
Jeff Hao0abc72e2013-08-13 13:45:14 -070066 void CompareAndUpdateStackTrace(Thread* thread, std::vector<mirror::AbstractMethod*>* stack_trace)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68
Ian Rogers62d6c772013-02-27 08:32:07 -080069 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
70 const mirror::AbstractMethod* method, uint32_t dex_pc)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
73 const mirror::AbstractMethod* method, uint32_t dex_pc,
74 const JValue& return_value)
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
76 virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method, uint32_t dex_pc)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
79 const mirror::AbstractMethod* method, uint32_t new_dex_pc)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
82 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
83 mirror::Throwable* exception_object)
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070085
Jeff Hao0abc72e2013-08-13 13:45:14 -070086 ~Trace();
87
jeffhaoe343b762011-12-05 16:36:44 -080088 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070089 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080090
Jeff Hao0abc72e2013-08-13 13:45:14 -070091 static void* RunSamplingThread(void* arg) LOCKS_EXCLUDED(Locks::trace_lock_);
92
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080094
Ian Rogers62d6c772013-02-27 08:32:07 -080095 void LogMethodTraceEvent(Thread* thread, const mirror::AbstractMethod* method,
96 instrumentation::Instrumentation::InstrumentationEvent event);
97
jeffhaoa9ef3fd2011-12-13 18:33:43 -080098 // Methods to output traced methods and threads.
Ian Rogers62d6c772013-02-27 08:32:07 -080099 void GetVisitedMethods(size_t end_offset, std::set<mirror::AbstractMethod*>* visited_methods);
100 void DumpMethodList(std::ostream& os, const std::set<mirror::AbstractMethod*>& visited_methods)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800103
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 // Singleton instance of the Trace or NULL when no method tracing is active.
Jeff Hao0abc72e2013-08-13 13:45:14 -0700105 static Trace* volatile the_trace_ GUARDED_BY(Locks::trace_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800106
107 // The default profiler clock source.
108 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800109
Jeff Hao0abc72e2013-08-13 13:45:14 -0700110 // True if traceview should sample instead of instrumenting method entry/exit.
111 static bool sampling_enabled_;
112
113 // Sampling interval in microseconds.
114 static uint32_t sampling_interval_us_;
115
116 // Sampling thread, non-zero when sampling.
117 static pthread_t sampling_pthread_;
118
119 // Maps a thread to its most recent stack trace sample.
120 SafeMap<Thread*, std::vector<mirror::AbstractMethod*>*> thread_stack_trace_map_;
121
jeffhao2692b572011-12-16 15:42:28 -0800122 // Maps a thread to its clock base.
Elliott Hughesa0e18062012-04-13 15:59:59 -0700123 SafeMap<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800124
jeffhao2692b572011-12-16 15:42:28 -0800125 // File to write trace data out to, NULL if direct to ddms.
126 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800127
jeffhao2692b572011-12-16 15:42:28 -0800128 // Buffer to store trace data.
129 UniquePtr<uint8_t> buf_;
130
jeffhao0791adc2012-04-04 11:14:32 -0700131 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700133
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700135
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 // Size of buf_.
137 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800138
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 // Time trace was created.
140 const uint64_t start_time_;
141
142 // Offset into buf_.
jeffhao2692b572011-12-16 15:42:28 -0800143 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800144
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 // Did we overflow the buffer recording traces?
146 bool overflow_;
147
jeffhaoe343b762011-12-05 16:36:44 -0800148 DISALLOW_COPY_AND_ASSIGN(Trace);
149};
150
151} // namespace art
152
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700153#endif // ART_RUNTIME_TRACE_H_