blob: 5bd6a8d5cafa3ade15fc5b8f9949e17285ee0262 [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>
jeffhaoe343b762011-12-05 16:36:44 -080023
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "globals.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "instrumentation.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070028#include "safe_map.h"
jeffhao2692b572011-12-16 15:42:28 -080029#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080030
31namespace art {
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Mathieu Chartier66f19252012-09-18 08:57:04 -070034class AbstractMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035} // namespace mirror
jeffhaoa9ef3fd2011-12-13 18:33:43 -080036class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080037
Elliott Hughescfbe73d2012-05-22 17:37:06 -070038enum ProfilerClockSource {
39 kProfilerClockSourceThreadCpu,
40 kProfilerClockSourceWall,
Ian Rogers62d6c772013-02-27 08:32:07 -080041 kProfilerClockSourceDual, // Both wall and thread CPU clocks.
Elliott Hughescfbe73d2012-05-22 17:37:06 -070042};
43
Ian Rogers62d6c772013-02-27 08:32:07 -080044class Trace : public instrumentation::InstrumentationListener {
jeffhaoe343b762011-12-05 16:36:44 -080045 public:
jeffhao0791adc2012-04-04 11:14:32 -070046 enum TraceFlag {
47 kTraceCountAllocs = 1,
48 };
49
Elliott Hughescfbe73d2012-05-22 17:37:06 -070050 static void SetDefaultClockSource(ProfilerClockSource clock_source);
51
Ian Rogers62d6c772013-02-27 08:32:07 -080052 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
53 bool direct_to_ddms)
54 LOCKS_EXCLUDED(Locks::mutator_lock_,
55 Locks::thread_list_lock_,
56 Locks::thread_suspend_count_lock_,
57 Locks::trace_lock_);
58 static void Stop() LOCKS_EXCLUDED(Locks::trace_lock_);
59 static void Shutdown() LOCKS_EXCLUDED(Locks::trace_lock_);
60 static bool IsMethodTracingActive() LOCKS_EXCLUDED(Locks::trace_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080061
Elliott Hughescfbe73d2012-05-22 17:37:06 -070062 bool UseWallClock();
63 bool UseThreadCpuClock();
64
Ian Rogers62d6c772013-02-27 08:32:07 -080065 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
66 const mirror::AbstractMethod* method, uint32_t dex_pc)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
69 const mirror::AbstractMethod* method, uint32_t dex_pc,
70 const JValue& return_value)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72 virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method, uint32_t dex_pc)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
75 const mirror::AbstractMethod* method, uint32_t new_dex_pc)
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
77 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
78 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
79 mirror::Throwable* exception_object)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080081 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070082 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080083
Ian Rogersb726dcb2012-09-05 08:57:23 -070084 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080085
Ian Rogers62d6c772013-02-27 08:32:07 -080086 void LogMethodTraceEvent(Thread* thread, const mirror::AbstractMethod* method,
87 instrumentation::Instrumentation::InstrumentationEvent event);
88
jeffhaoa9ef3fd2011-12-13 18:33:43 -080089 // Methods to output traced methods and threads.
Ian Rogers62d6c772013-02-27 08:32:07 -080090 void GetVisitedMethods(size_t end_offset, std::set<mirror::AbstractMethod*>* visited_methods);
91 void DumpMethodList(std::ostream& os, const std::set<mirror::AbstractMethod*>& visited_methods)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080094
Ian Rogers62d6c772013-02-27 08:32:07 -080095 // Singleton instance of the Trace or NULL when no method tracing is active.
96 static Trace* the_trace_ GUARDED_BY(Locks::trace_lock_);
97
98 // The default profiler clock source.
99 static ProfilerClockSource default_clock_source_;
jeffhaoe343b762011-12-05 16:36:44 -0800100
jeffhao2692b572011-12-16 15:42:28 -0800101 // Maps a thread to its clock base.
Elliott Hughesa0e18062012-04-13 15:59:59 -0700102 SafeMap<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800103
jeffhao2692b572011-12-16 15:42:28 -0800104 // File to write trace data out to, NULL if direct to ddms.
105 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800106
jeffhao2692b572011-12-16 15:42:28 -0800107 // Buffer to store trace data.
108 UniquePtr<uint8_t> buf_;
109
jeffhao0791adc2012-04-04 11:14:32 -0700110 // Flags enabling extra tracing of things such as alloc counts.
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 const int flags_;
jeffhao0791adc2012-04-04 11:14:32 -0700112
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 const ProfilerClockSource clock_source_;
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700114
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 // Size of buf_.
116 const int buffer_size_;
jeffhao2692b572011-12-16 15:42:28 -0800117
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 // Time trace was created.
119 const uint64_t start_time_;
120
121 // Offset into buf_.
jeffhao2692b572011-12-16 15:42:28 -0800122 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800123
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 // Did we overflow the buffer recording traces?
125 bool overflow_;
126
jeffhaoe343b762011-12-05 16:36:44 -0800127 DISALLOW_COPY_AND_ASSIGN(Trace);
128};
129
130} // namespace art
131
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700132#endif // ART_RUNTIME_TRACE_H_