OSDN Git Service

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