Profile.h revision 375fb116bcb817b37509ab579dbd55cdbb765cbf
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_H_
21#define DALVIK_PROFILE_H_
22
23#ifndef NOT_VM      /* for utilities that sneakily include this file */
24
25#include <stdio.h>
26
27struct Thread;      // extern
28
29
30/* boot init */
31bool dvmProfilingStartup(void);
32void dvmProfilingShutdown(void);
33
34/*
35 * Method trace state.  This is currently global.  In theory we could make
36 * most of this per-thread.
37 */
38struct MethodTraceState {
39    /* active state */
40    pthread_mutex_t startStopLock;
41    pthread_cond_t  threadExitCond;
42    FILE*   traceFile;
43    bool    directToDdms;
44    int     bufferSize;
45    int     flags;
46
47    int     traceEnabled;
48    u1*     buf;
49    volatile int curOffset;
50    u8      startWhen;
51    int     overflow;
52};
53
54/*
55 * Memory allocation profiler state.  This is used both globally and
56 * per-thread.
57 *
58 * If you add a field here, zero it out in dvmStartAllocCounting().
59 */
60struct AllocProfState {
61    bool    enabled;            // is allocation tracking enabled?
62
63    int     allocCount;         // #of objects allocated
64    int     allocSize;          // cumulative size of objects
65
66    int     failedAllocCount;   // #of times an allocation failed
67    int     failedAllocSize;    // cumulative size of failed allocations
68
69    int     freeCount;          // #of objects freed
70    int     freeSize;           // cumulative size of freed objects
71
72    int     gcCount;            // #of times an allocation triggered a GC
73
74    int     classInitCount;     // #of initialized classes
75    u8      classInitTime;      // cumulative time spent in class init (nsec)
76};
77
78
79/*
80 * Start/stop method tracing.
81 */
82void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
83        int flags, bool directToDdms);
84bool dvmIsMethodTraceActive(void);
85void dvmMethodTraceStop(void);
86
87/*
88 * Start/stop emulator tracing.
89 */
90void dvmEmulatorTraceStart(void);
91void dvmEmulatorTraceStop(void);
92
93/*
94 * Start/stop Dalvik instruction counting.
95 */
96void dvmStartInstructionCounting();
97void dvmStopInstructionCounting();
98
99/*
100 * Bit flags for dvmMethodTraceStart "flags" argument.  These must match
101 * the values in android.os.Debug.
102 */
103enum {
104    TRACE_ALLOC_COUNTS      = 0x01,
105};
106
107/*
108 * Call these when a method enters or exits.
109 */
110#define TRACE_METHOD_ENTER(_self, _method)                                  \
111    do {                                                                    \
112        if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace)           \
113            dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER);          \
114        if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace)         \
115            dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER);              \
116    } while(0);
117#define TRACE_METHOD_EXIT(_self, _method)                                   \
118    do {                                                                    \
119        if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace)           \
120            dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT);           \
121        if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace)         \
122            dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT);               \
123    } while(0);
124#define TRACE_METHOD_UNROLL(_self, _method)                                 \
125    do {                                                                    \
126        if (_self->interpBreak.ctl.subMode & kSubModeMethodTrace)           \
127            dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL);         \
128        if (_self->interpBreak.ctl.subMode & kSubModeEmulatorTrace)         \
129            dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL);             \
130    } while(0);
131
132void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
133void dvmEmitEmulatorTrace(const Method* method, int action);
134
135void dvmMethodTraceGCBegin(void);
136void dvmMethodTraceGCEnd(void);
137void dvmMethodTraceClassPrepBegin(void);
138void dvmMethodTraceClassPrepEnd(void);
139
140extern "C" void dvmFastMethodTraceEnter(const Method* method, struct Thread* self);
141extern "C" void dvmFastMethodTraceExit(struct Thread* self);
142extern "C" void dvmFastNativeMethodTraceExit(const Method* method, struct Thread* self);
143
144/*
145 * Start/stop alloc counting.
146 */
147void dvmStartAllocCounting(void);
148void dvmStopAllocCounting(void);
149
150#endif
151
152
153/*
154 * Enumeration for the two "action" bits.
155 */
156enum {
157    METHOD_TRACE_ENTER = 0x00,      // method entry
158    METHOD_TRACE_EXIT = 0x01,       // method exit
159    METHOD_TRACE_UNROLL = 0x02,     // method exited by exception unrolling
160    // 0x03 currently unused
161};
162
163#define TOKEN_CHAR      '*'
164#define TRACE_VERSION   1
165
166/*
167 * Common definitions, shared with the dump tool.
168 */
169#define METHOD_ACTION_MASK      0x03            /* two bits */
170#define METHOD_ID(_method)      ((_method) & (~METHOD_ACTION_MASK))
171#define METHOD_ACTION(_method)  (((unsigned int)(_method)) & METHOD_ACTION_MASK)
172#define METHOD_COMBINE(_method, _action)    ((_method) | (_action))
173
174#endif  // DALVIK_PROFILE_H_
175