blob: 43b0ff791fc3a5d7a190726ce6312e0176494674 [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
17#ifndef ART_SRC_TRACE_H_
18#define ART_SRC_TRACE_H_
19
jeffhaoa9ef3fd2011-12-13 18:33:43 -080020#include <ostream>
21#include <set>
22#include <string>
jeffhaoe343b762011-12-05 16:36:44 -080023
jeffhaoa9ef3fd2011-12-13 18:33:43 -080024#include "file.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "globals.h"
26#include "macros.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070027#include "safe_map.h"
jeffhao2692b572011-12-16 15:42:28 -080028#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080029
30namespace art {
31
Mathieu Chartier66f19252012-09-18 08:57:04 -070032class AbstractMethod;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080033class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080034
Ian Rogers57b86d42012-03-27 16:05:41 -070035uint32_t TraceMethodUnwindFromCode(Thread* self);
36
jeffhaoe343b762011-12-05 16:36:44 -080037struct TraceStackFrame {
Mathieu Chartier66f19252012-09-18 08:57:04 -070038 TraceStackFrame(AbstractMethod* method, uintptr_t return_pc)
jeffhaoe343b762011-12-05 16:36:44 -080039 : method_(method), return_pc_(return_pc) {
40 }
41
Mathieu Chartier66f19252012-09-18 08:57:04 -070042 AbstractMethod* method_;
jeffhaoe343b762011-12-05 16:36:44 -080043 uintptr_t return_pc_;
44};
45
Elliott Hughescfbe73d2012-05-22 17:37:06 -070046enum ProfilerClockSource {
47 kProfilerClockSourceThreadCpu,
48 kProfilerClockSourceWall,
49 kProfilerClockSourceDual,
50};
51
jeffhaoe343b762011-12-05 16:36:44 -080052class Trace {
53 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080054 enum TraceEvent {
55 kMethodTraceEnter = 0,
56 kMethodTraceExit = 1,
57 kMethodTraceUnwind = 2,
58 };
59
jeffhao0791adc2012-04-04 11:14:32 -070060 enum TraceFlag {
61 kTraceCountAllocs = 1,
62 };
63
Elliott Hughescfbe73d2012-05-22 17:37:06 -070064 static void SetDefaultClockSource(ProfilerClockSource clock_source);
65
jeffhaoe343b762011-12-05 16:36:44 -080066 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
67 static void Stop();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 static void Shutdown() NO_THREAD_SAFETY_ANALYSIS; // TODO: implement appropriate locking.
jeffhaoe343b762011-12-05 16:36:44 -080069
Elliott Hughescfbe73d2012-05-22 17:37:06 -070070 bool UseWallClock();
71 bool UseThreadCpuClock();
72
Mathieu Chartier66f19252012-09-18 08:57:04 -070073 void LogMethodTraceEvent(Thread* self, const AbstractMethod* method, TraceEvent event);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080074
Mathieu Chartier66f19252012-09-18 08:57:04 -070075 void AddSavedCodeToMap(const AbstractMethod* method, const void* code);
76 void RemoveSavedCodeFromMap(const AbstractMethod* method);
77 const void* GetSavedCodeFromMap(const AbstractMethod* method);
jeffhaoe343b762011-12-05 16:36:44 -080078
Mathieu Chartier66f19252012-09-18 08:57:04 -070079 void SaveAndUpdateCode(AbstractMethod* method);
80 void ResetSavedCode(AbstractMethod* method);
jeffhaoe343b762011-12-05 16:36:44 -080081
82 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070083 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080084
85 void BeginTracing();
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080087
jeffhaoe343b762011-12-05 16:36:44 -080088 // Replaces code of each method with a pointer to a stub for method tracing.
jeffhao2692b572011-12-16 15:42:28 -080089 void InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -080090
91 // Restores original code for each method and fixes the return values of each thread's stack.
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 void UninstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoe343b762011-12-05 16:36:44 -080093
jeffhaoa9ef3fd2011-12-13 18:33:43 -080094 // Methods to output traced methods and threads.
jeffhao2692b572011-12-16 15:42:28 -080095 void GetVisitedMethods(size_t end_offset);
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 void DumpMethodList(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080098
jeffhao2692b572011-12-16 15:42:28 -080099 // Maps a method to its original code pointer.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700100 SafeMap<const AbstractMethod*, const void*> saved_code_map_;
jeffhaoe343b762011-12-05 16:36:44 -0800101
jeffhao2692b572011-12-16 15:42:28 -0800102 // Set of methods visited by the profiler.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700103 std::set<const AbstractMethod*> visited_methods_;
jeffhaoe343b762011-12-05 16:36:44 -0800104
jeffhao2692b572011-12-16 15:42:28 -0800105 // Maps a thread to its clock base.
Elliott Hughesa0e18062012-04-13 15:59:59 -0700106 SafeMap<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800107
jeffhao2692b572011-12-16 15:42:28 -0800108 // File to write trace data out to, NULL if direct to ddms.
109 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800110
jeffhao2692b572011-12-16 15:42:28 -0800111 // Buffer to store trace data.
112 UniquePtr<uint8_t> buf_;
113
jeffhao0791adc2012-04-04 11:14:32 -0700114 // Flags enabling extra tracing of things such as alloc counts.
115 int flags_;
116
Elliott Hughescfbe73d2012-05-22 17:37:06 -0700117 ProfilerClockSource clock_source_;
118
jeffhao2692b572011-12-16 15:42:28 -0800119 bool overflow_;
120 int buffer_size_;
121 uint64_t start_time_;
122 uint16_t trace_version_;
123 uint16_t record_size_;
124
125 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800126
jeffhaoe343b762011-12-05 16:36:44 -0800127 DISALLOW_COPY_AND_ASSIGN(Trace);
128};
129
130} // namespace art
131
132#endif // ART_SRC_TRACE_H_