blob: cdaf02767f300ad9be34542c182cdb74fbbe159f [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.
42 *
43 */
44typedef struct MethodTraceState {
45 /* these are set during VM init */
46 Method* gcMethod;
47 Method* classPrepMethod;
48
49 /* active state */
50 pthread_mutex_t startStopLock;
51 pthread_cond_t threadExitCond;
52 FILE* traceFile;
53 int bufferSize;
54 int flags;
55
56 bool traceEnabled;
57 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
83#if PROFILE_EXTERNAL_ALLOCATIONS
84 int externalAllocCount; // #of calls to dvmTrackExternalAllocation()
85 int externalAllocSize; // #of bytes passed to ...ExternalAllocation()
86
87 int failedExternalAllocCount; // #of times an allocation failed
88 int failedExternalAllocSize; // cumulative size of failed allocations
89
90 int externalFreeCount; // #of calls to dvmTrackExternalFree()
91 int externalFreeSize; // #of bytes passed to ...ExternalFree()
92#endif // PROFILE_EXTERNAL_ALLOCATIONS
93} AllocProfState;
94
95
96/*
97 * Start/stop method tracing.
98 */
99void dvmMethodTraceStart(const char* traceFileName, int bufferSize, int flags);
Andy McFadden8c880b92009-03-24 18:15:56 -0700100bool dvmIsMethodTraceActive(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800101void dvmMethodTraceStop(void);
102
103/*
104 * Start/stop emulator tracing.
105 */
106void dvmEmulatorTraceStart(void);
107void dvmEmulatorTraceStop(void);
108
109/*
110 * Start/stop Dalvik instruction counting.
111 */
112void dvmStartInstructionCounting();
113void dvmStopInstructionCounting();
114
115/*
116 * Bit flags for dvmMethodTraceStart "flags" argument. These must match
117 * the values in android.os.Debug.
118 */
119enum {
120 TRACE_ALLOC_COUNTS = 0x01,
121};
122
123/*
124 * Call these when a method enters or exits.
125 */
126#ifdef WITH_PROFILER
127# define TRACE_METHOD_ENTER(_self, _method) \
128 do { \
129 if (gDvm.activeProfilers != 0) { \
130 if (gDvm.methodTrace.traceEnabled) \
131 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER); \
132 if (gDvm.emulatorTraceEnableCount != 0) \
133 dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER); \
134 } \
135 } while(0);
136# define TRACE_METHOD_EXIT(_self, _method) \
137 do { \
138 if (gDvm.activeProfilers != 0) { \
139 if (gDvm.methodTrace.traceEnabled) \
140 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT); \
141 if (gDvm.emulatorTraceEnableCount != 0) \
142 dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT); \
143 } \
144 } while(0);
145# define TRACE_METHOD_UNROLL(_self, _method) \
146 do { \
147 if (gDvm.activeProfilers != 0) { \
148 if (gDvm.methodTrace.traceEnabled) \
149 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL); \
150 if (gDvm.emulatorTraceEnableCount != 0) \
151 dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL); \
152 } \
153 } while(0);
154#else
155# define TRACE_METHOD_ENTER(_self, _method) ((void) 0)
156# define TRACE_METHOD_EXIT(_self, _method) ((void) 0)
157# define TRACE_METHOD_UNROLL(_self, _method) ((void) 0)
158#endif
159
160void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
161void dvmEmitEmulatorTrace(const Method* method, int action);
162
163void dvmMethodTraceGCBegin(void);
164void dvmMethodTraceGCEnd(void);
165void dvmMethodTraceClassPrepBegin(void);
166void dvmMethodTraceClassPrepEnd(void);
167
168/*
169 * Start/stop alloc counting.
170 */
171void dvmStartAllocCounting(void);
172void dvmStopAllocCounting(void);
173
174#endif
175
176
177/*
178 * Enumeration for the two "action" bits.
179 */
180enum {
181 METHOD_TRACE_ENTER = 0x00, // method entry
182 METHOD_TRACE_EXIT = 0x01, // method exit
183 METHOD_TRACE_UNROLL = 0x02, // method exited by exception unrolling
184 // 0x03 currently unused
185};
186
187#define TOKEN_CHAR '*'
188#define TRACE_VERSION 1
189
190/*
191 * Common definitions, shared with the dump tool.
192 */
193#define METHOD_ACTION_MASK 0x03 /* two bits */
194#define METHOD_ID(_method) ((_method) & (~METHOD_ACTION_MASK))
195#define METHOD_ACTION(_method) (((unsigned int)(_method)) & METHOD_ACTION_MASK)
196#define METHOD_COMBINE(_method, _action) ((_method) | (_action))
197
198#endif /*_DALVIK_PROFILE*/