Profile.h revision 9f640af99bebc6e96f6e1e9903557e2c8f567483
1/*
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 */
16
17/*
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 */
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;
52    bool    directToDdms;
53    int     bufferSize;
54    int     flags;
55
56    int     traceEnabled;
57    u1*     buf;
58    volatile int curOffset;
59    u8      startWhen;
60    int     overflow;
61
62    int     traceVersion;
63    size_t  recordSize;
64} MethodTraceState;
65
66/*
67 * Memory allocation profiler state.  This is used both globally and
68 * per-thread.
69 *
70 * If you add a field here, zero it out in dvmStartAllocCounting().
71 */
72typedef struct AllocProfState {
73    bool    enabled;            // is allocation tracking enabled?
74
75    int     allocCount;         // #of objects allocated
76    int     allocSize;          // cumulative size of objects
77
78    int     failedAllocCount;   // #of times an allocation failed
79    int     failedAllocSize;    // cumulative size of failed allocations
80
81    int     freeCount;          // #of objects freed
82    int     freeSize;           // cumulative size of freed objects
83
84    int     gcCount;            // #of times an allocation triggered a GC
85
86    int     classInitCount;     // #of initialized classes
87    u8      classInitTime;      // cumulative time spent in class init (nsec)
88
89#if PROFILE_EXTERNAL_ALLOCATIONS
90    int     externalAllocCount; // #of calls to dvmTrackExternalAllocation()
91    int     externalAllocSize;  // #of bytes passed to ...ExternalAllocation()
92
93    int     failedExternalAllocCount; // #of times an allocation failed
94    int     failedExternalAllocSize;  // cumulative size of failed allocations
95
96    int     externalFreeCount;  // #of calls to dvmTrackExternalFree()
97    int     externalFreeSize;   // #of bytes passed to ...ExternalFree()
98#endif  // PROFILE_EXTERNAL_ALLOCATIONS
99} AllocProfState;
100
101
102/*
103 * Start/stop method tracing.
104 */
105void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
106        int flags, bool directToDdms);
107bool dvmIsMethodTraceActive(void);
108void dvmMethodTraceStop(void);
109
110/*
111 * Start/stop emulator tracing.
112 */
113void dvmEmulatorTraceStart(void);
114void dvmEmulatorTraceStop(void);
115
116/*
117 * Start/stop Dalvik instruction counting.
118 */
119void dvmStartInstructionCounting();
120void dvmStopInstructionCounting();
121
122/*
123 * Bit flags for dvmMethodTraceStart "flags" argument.  These must match
124 * the values in android.os.Debug.
125 */
126enum {
127    TRACE_ALLOC_COUNTS      = 0x01,
128};
129
130/*
131 * Call these when a method enters or exits.
132 */
133#define TRACE_METHOD_ENTER(_self, _method)                                 \
134    do {                                                                    \
135        if (gDvm.activeProfilers != 0) {                                    \
136            if (gDvm.methodTrace.traceEnabled)                              \
137                dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER);      \
138            if (gDvm.emulatorTraceEnableCount != 0)                         \
139                dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER);          \
140        }                                                                   \
141    } while(0);
142#define TRACE_METHOD_EXIT(_self, _method)                                  \
143    do {                                                                    \
144        if (gDvm.activeProfilers != 0) {                                    \
145            if (gDvm.methodTrace.traceEnabled)                              \
146                dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT);       \
147            if (gDvm.emulatorTraceEnableCount != 0)                         \
148                dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT);           \
149        }                                                                   \
150    } while(0);
151#define TRACE_METHOD_UNROLL(_self, _method)                                \
152    do {                                                                    \
153        if (gDvm.activeProfilers != 0) {                                    \
154            if (gDvm.methodTrace.traceEnabled)                              \
155                dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL);     \
156            if (gDvm.emulatorTraceEnableCount != 0)                         \
157                dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL);         \
158        }                                                                   \
159    } while(0);
160
161void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
162void dvmEmitEmulatorTrace(const Method* method, int action);
163
164void dvmMethodTraceGCBegin(void);
165void dvmMethodTraceGCEnd(void);
166void dvmMethodTraceClassPrepBegin(void);
167void dvmMethodTraceClassPrepEnd(void);
168
169/*
170 * Start/stop alloc counting.
171 */
172void dvmStartAllocCounting(void);
173void dvmStopAllocCounting(void);
174
175#endif
176
177
178/*
179 * Enumeration for the two "action" bits.
180 */
181enum {
182    METHOD_TRACE_ENTER = 0x00,      // method entry
183    METHOD_TRACE_EXIT = 0x01,       // method exit
184    METHOD_TRACE_UNROLL = 0x02,     // method exited by exception unrolling
185    // 0x03 currently unused
186};
187
188#define TOKEN_CHAR      '*'
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*/
199