blob: bca5096fb048426dc230bf1f2f50476e01abd0ff [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 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 */
Andy McFadden8c880b92009-03-24 18:15:56 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * Android's method call profiling goodies.
19 */
20#ifndef _DALVIK_PROFILE
21#define _DALVIK_PROFILE
22
23#ifndef NOT_VM /* for utilities that sneakily include this file */
24
25#include <stdio.h>
26
27/* External allocations are hackish enough that it's worthwhile
28 * separating them for possible removal later.
29 */
30#define PROFILE_EXTERNAL_ALLOCATIONS 1
31
32struct Thread; // extern
33
34
35/* boot init */
36bool dvmProfilingStartup(void);
37void dvmProfilingShutdown(void);
38
39/*
40 * Method trace state. This is currently global. In theory we could make
41 * most of this per-thread.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080042 */
43typedef struct MethodTraceState {
44 /* these are set during VM init */
45 Method* gcMethod;
46 Method* classPrepMethod;
47
48 /* active state */
49 pthread_mutex_t startStopLock;
50 pthread_cond_t threadExitCond;
51 FILE* traceFile;
Andy McFadden01718122010-01-22 16:36:30 -080052 bool directToDdms;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080053 int bufferSize;
54 int flags;
55
Andy McFaddenfc3d3162010-08-05 14:34:26 -070056 int traceEnabled;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080057 u1* buf;
58 volatile int curOffset;
59 u8 startWhen;
60 int overflow;
61} MethodTraceState;
62
63/*
64 * Memory allocation profiler state. This is used both globally and
65 * per-thread.
66 *
67 * If you add a field here, zero it out in dvmStartAllocCounting().
68 */
69typedef struct AllocProfState {
70 bool enabled; // is allocation tracking enabled?
71
72 int allocCount; // #of objects allocated
73 int allocSize; // cumulative size of objects
74
75 int failedAllocCount; // #of times an allocation failed
76 int failedAllocSize; // cumulative size of failed allocations
77
78 int freeCount; // #of objects freed
79 int freeSize; // cumulative size of freed objects
80
81 int gcCount; // #of times an allocation triggered a GC
82
Andy McFaddene15a8eb2010-02-22 17:07:23 -080083 int classInitCount; // #of initialized classes
84 u8 classInitTime; // cumulative time spent in class init (nsec)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080085} AllocProfState;
86
87
88/*
89 * Start/stop method tracing.
90 */
Dianne Hackborn0f0ae022009-06-23 19:21:10 -070091void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
Andy McFadden01718122010-01-22 16:36:30 -080092 int flags, bool directToDdms);
Andy McFadden8c880b92009-03-24 18:15:56 -070093bool dvmIsMethodTraceActive(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080094void dvmMethodTraceStop(void);
95
96/*
97 * Start/stop emulator tracing.
98 */
99void dvmEmulatorTraceStart(void);
100void dvmEmulatorTraceStop(void);
101
102/*
103 * Start/stop Dalvik instruction counting.
104 */
105void dvmStartInstructionCounting();
106void dvmStopInstructionCounting();
107
108/*
109 * Bit flags for dvmMethodTraceStart "flags" argument. These must match
110 * the values in android.os.Debug.
111 */
112enum {
113 TRACE_ALLOC_COUNTS = 0x01,
114};
115
116/*
117 * Call these when a method enters or exits.
118 */
Andy McFadden0d615c32010-08-18 12:19:51 -0700119#define TRACE_METHOD_ENTER(_self, _method) \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120 do { \
121 if (gDvm.activeProfilers != 0) { \
122 if (gDvm.methodTrace.traceEnabled) \
123 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER); \
124 if (gDvm.emulatorTraceEnableCount != 0) \
125 dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER); \
126 } \
127 } while(0);
Andy McFadden0d615c32010-08-18 12:19:51 -0700128#define TRACE_METHOD_EXIT(_self, _method) \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800129 do { \
130 if (gDvm.activeProfilers != 0) { \
131 if (gDvm.methodTrace.traceEnabled) \
132 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT); \
133 if (gDvm.emulatorTraceEnableCount != 0) \
134 dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT); \
135 } \
136 } while(0);
Andy McFadden0d615c32010-08-18 12:19:51 -0700137#define TRACE_METHOD_UNROLL(_self, _method) \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800138 do { \
139 if (gDvm.activeProfilers != 0) { \
140 if (gDvm.methodTrace.traceEnabled) \
141 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL); \
142 if (gDvm.emulatorTraceEnableCount != 0) \
143 dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL); \
144 } \
145 } while(0);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800146
147void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
148void dvmEmitEmulatorTrace(const Method* method, int action);
149
150void dvmMethodTraceGCBegin(void);
151void dvmMethodTraceGCEnd(void);
152void dvmMethodTraceClassPrepBegin(void);
153void dvmMethodTraceClassPrepEnd(void);
154
Ben Cheng5cc61d72010-08-31 09:30:37 -0700155#if defined(WITH_INLINE_PROFILING)
156struct InterpState; // extern
157void dvmFastMethodTraceEnter(const Method* method,
158 const struct InterpState* interpState);
159void dvmFastJavaMethodTraceExit(const struct InterpState* interpState);
160void dvmFastNativeMethodTraceExit(const Method*method,
161 const struct InterpState* interpState);
162#endif
163
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800164/*
165 * Start/stop alloc counting.
166 */
167void dvmStartAllocCounting(void);
168void dvmStopAllocCounting(void);
169
170#endif
171
172
173/*
174 * Enumeration for the two "action" bits.
175 */
176enum {
177 METHOD_TRACE_ENTER = 0x00, // method entry
178 METHOD_TRACE_EXIT = 0x01, // method exit
179 METHOD_TRACE_UNROLL = 0x02, // method exited by exception unrolling
180 // 0x03 currently unused
181};
182
183#define TOKEN_CHAR '*'
184#define TRACE_VERSION 1
185
186/*
187 * Common definitions, shared with the dump tool.
188 */
189#define METHOD_ACTION_MASK 0x03 /* two bits */
190#define METHOD_ID(_method) ((_method) & (~METHOD_ACTION_MASK))
191#define METHOD_ACTION(_method) (((unsigned int)(_method)) & METHOD_ACTION_MASK)
192#define METHOD_COMBINE(_method, _action) ((_method) | (_action))
193
194#endif /*_DALVIK_PROFILE*/