2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Android's method call profiling goodies.
20 #ifndef DALVIK_PROFILE_H_
21 #define DALVIK_PROFILE_H_
23 #ifndef NOT_VM /* for utilities that sneakily include this file */
27 struct Thread; // extern
31 bool dvmProfilingStartup(void);
32 void dvmProfilingShutdown(void);
35 * Method trace state. This is currently global. In theory we could make
36 * most of this per-thread.
38 struct MethodTraceState {
40 pthread_mutex_t startStopLock;
41 pthread_cond_t threadExitCond;
49 volatile int curOffset;
57 pthread_t samplingThreadHandle;
61 * Memory allocation profiler state. This is used both globally and
64 * If you add a field here, zero it out in dvmStartAllocCounting().
66 struct AllocProfState {
67 bool enabled; // is allocation tracking enabled?
69 int allocCount; // #of objects allocated
70 int allocSize; // cumulative size of objects
72 int failedAllocCount; // #of times an allocation failed
73 int failedAllocSize; // cumulative size of failed allocations
75 int freeCount; // #of objects freed
76 int freeSize; // cumulative size of freed objects
78 int gcCount; // #of times an allocation triggered a GC
80 int classInitCount; // #of initialized classes
81 u8 classInitTime; // cumulative time spent in class init (nsec)
86 * Start/stop method tracing.
88 void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
89 int flags, bool directToDdms, bool samplingEnabled, int intervalUs);
90 bool dvmIsMethodTraceActive(void);
91 void dvmMethodTraceStop(void);
94 * Start/stop emulator tracing.
96 void dvmEmulatorTraceStart(void);
97 void dvmEmulatorTraceStop(void);
100 * Start/stop Dalvik instruction counting.
102 void dvmStartInstructionCounting();
103 void dvmStopInstructionCounting();
106 * Bit flags for dvmMethodTraceStart "flags" argument. These must match
107 * the values in android.os.Debug.
110 TRACE_ALLOC_COUNTS = 0x01,
114 * Call these when a method enters or exits.
116 #define TRACE_METHOD_ENTER(_self, _method) \
118 if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) { \
119 u4 cpuClockDiff = 0; \
120 u4 wallClockDiff = 0; \
121 dvmMethodTraceReadClocks(_self, &cpuClockDiff, &wallClockDiff); \
122 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER, \
123 cpuClockDiff, wallClockDiff); \
125 if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \
126 dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER); \
128 #define TRACE_METHOD_EXIT(_self, _method) \
130 if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) { \
131 u4 cpuClockDiff = 0; \
132 u4 wallClockDiff = 0; \
133 dvmMethodTraceReadClocks(_self, &cpuClockDiff, &wallClockDiff); \
134 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT, \
135 cpuClockDiff, wallClockDiff); \
137 if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \
138 dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT); \
140 #define TRACE_METHOD_UNROLL(_self, _method) \
142 if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace) { \
143 u4 cpuClockDiff = 0; \
144 u4 wallClockDiff = 0; \
145 dvmMethodTraceReadClocks(_self, &cpuClockDiff, &wallClockDiff); \
146 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL, \
147 cpuClockDiff, wallClockDiff); \
149 if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace) \
150 dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL); \
153 void dvmMethodTraceReadClocks(Thread* self, u4* cpuClockDiff,
155 void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action,
156 u4 cpuClockDiff, u4 wallClockDiff);
157 void dvmEmitEmulatorTrace(const Method* method, int action);
159 void dvmMethodTraceGCBegin(void);
160 void dvmMethodTraceGCEnd(void);
161 void dvmMethodTraceClassPrepBegin(void);
162 void dvmMethodTraceClassPrepEnd(void);
164 extern "C" void dvmFastMethodTraceEnter(const Method* method, struct Thread* self);
165 extern "C" void dvmFastMethodTraceExit(struct Thread* self);
166 extern "C" void dvmFastNativeMethodTraceExit(const Method* method, struct Thread* self);
169 * Start/stop alloc counting.
171 void dvmStartAllocCounting(void);
172 void dvmStopAllocCounting(void);
178 * Enumeration for the two "action" bits.
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
187 #define TOKEN_CHAR '*'
190 * Common definitions, shared with the dump tool.
192 #define METHOD_ACTION_MASK 0x03 /* two bits */
193 #define METHOD_ID(_method) ((_method) & (~METHOD_ACTION_MASK))
194 #define METHOD_ACTION(_method) (((unsigned int)(_method)) & METHOD_ACTION_MASK)
195 #define METHOD_COMBINE(_method, _action) ((_method) | (_action))
197 #endif // DALVIK_PROFILE_H_