OSDN Git Service

More SMP fixes.
[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 /* External allocations are hackish enough that it's worthwhile
28  * separating them for possible removal later.
29  */
30 #define PROFILE_EXTERNAL_ALLOCATIONS 1
31
32 struct Thread;      // extern
33
34
35 /* boot init */
36 bool dvmProfilingStartup(void);
37 void 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 typedef 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 } 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  */
69 typedef 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     int     classInitCount;     // #of initialized classes
84     u8      classInitTime;      // cumulative time spent in class init (nsec)
85
86 #if PROFILE_EXTERNAL_ALLOCATIONS
87     int     externalAllocCount; // #of calls to dvmTrackExternalAllocation()
88     int     externalAllocSize;  // #of bytes passed to ...ExternalAllocation()
89
90     int     failedExternalAllocCount; // #of times an allocation failed
91     int     failedExternalAllocSize;  // cumulative size of failed allocations
92
93     int     externalFreeCount;  // #of calls to dvmTrackExternalFree()
94     int     externalFreeSize;   // #of bytes passed to ...ExternalFree()
95 #endif  // PROFILE_EXTERNAL_ALLOCATIONS
96 } AllocProfState;
97
98
99 /*
100  * Start/stop method tracing.
101  */
102 void dvmMethodTraceStart(const char* traceFileName, int traceFd, int bufferSize,
103         int flags, bool directToDdms);
104 bool dvmIsMethodTraceActive(void);
105 void dvmMethodTraceStop(void);
106
107 /*
108  * Start/stop emulator tracing.
109  */
110 void dvmEmulatorTraceStart(void);
111 void dvmEmulatorTraceStop(void);
112
113 /*
114  * Start/stop Dalvik instruction counting.
115  */
116 void dvmStartInstructionCounting();
117 void dvmStopInstructionCounting();
118
119 /*
120  * Bit flags for dvmMethodTraceStart "flags" argument.  These must match
121  * the values in android.os.Debug.
122  */
123 enum {
124     TRACE_ALLOC_COUNTS      = 0x01,
125 };
126
127 /*
128  * Call these when a method enters or exits.
129  */
130 #ifdef WITH_PROFILER
131 # define TRACE_METHOD_ENTER(_self, _method)                                 \
132     do {                                                                    \
133         if (gDvm.activeProfilers != 0) {                                    \
134             if (gDvm.methodTrace.traceEnabled)                              \
135                 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_ENTER);      \
136             if (gDvm.emulatorTraceEnableCount != 0)                         \
137                 dvmEmitEmulatorTrace(_method, METHOD_TRACE_ENTER);          \
138         }                                                                   \
139     } while(0);
140 # define TRACE_METHOD_EXIT(_self, _method)                                  \
141     do {                                                                    \
142         if (gDvm.activeProfilers != 0) {                                    \
143             if (gDvm.methodTrace.traceEnabled)                              \
144                 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_EXIT);       \
145             if (gDvm.emulatorTraceEnableCount != 0)                         \
146                 dvmEmitEmulatorTrace(_method, METHOD_TRACE_EXIT);           \
147         }                                                                   \
148     } while(0);
149 # define TRACE_METHOD_UNROLL(_self, _method)                                \
150     do {                                                                    \
151         if (gDvm.activeProfilers != 0) {                                    \
152             if (gDvm.methodTrace.traceEnabled)                              \
153                 dvmMethodTraceAdd(_self, _method, METHOD_TRACE_UNROLL);     \
154             if (gDvm.emulatorTraceEnableCount != 0)                         \
155                 dvmEmitEmulatorTrace(_method, METHOD_TRACE_UNROLL);         \
156         }                                                                   \
157     } while(0);
158 #else
159 # define TRACE_METHOD_ENTER(_self, _method)     ((void) 0)
160 # define TRACE_METHOD_EXIT(_self, _method)      ((void) 0)
161 # define TRACE_METHOD_UNROLL(_self, _method)    ((void) 0)
162 #endif
163
164 void dvmMethodTraceAdd(struct Thread* self, const Method* method, int action);
165 void dvmEmitEmulatorTrace(const Method* method, int action);
166
167 void dvmMethodTraceGCBegin(void);
168 void dvmMethodTraceGCEnd(void);
169 void dvmMethodTraceClassPrepBegin(void);
170 void dvmMethodTraceClassPrepEnd(void);
171
172 /*
173  * Start/stop alloc counting.
174  */
175 void dvmStartAllocCounting(void);
176 void dvmStopAllocCounting(void);
177
178 #endif
179
180
181 /*
182  * Enumeration for the two "action" bits.
183  */
184 enum {
185     METHOD_TRACE_ENTER = 0x00,      // method entry
186     METHOD_TRACE_EXIT = 0x01,       // method exit
187     METHOD_TRACE_UNROLL = 0x02,     // method exited by exception unrolling
188     // 0x03 currently unused
189 };
190
191 #define TOKEN_CHAR      '*'
192 #define TRACE_VERSION   1
193
194 /*
195  * Common definitions, shared with the dump tool.
196  */
197 #define METHOD_ACTION_MASK      0x03            /* two bits */
198 #define METHOD_ID(_method)      ((_method) & (~METHOD_ACTION_MASK))
199 #define METHOD_ACTION(_method)  (((unsigned int)(_method)) & METHOD_ACTION_MASK)
200 #define METHOD_COMBINE(_method, _action)    ((_method) | (_action))
201
202 #endif /*_DALVIK_PROFILE*/