blob: c877ab254578e468b69dc6fb39f587d00540f1fb [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 * Variables with library scope.
19 *
20 * Prefer this over scattered static and global variables -- it's easier to
21 * view the state in a debugger, it makes clean shutdown simpler, we can
22 * trivially dump the state into a crash log, and it dodges most naming
23 * collisions that will arise when we are embedded in a larger program.
24 *
25 * If we want multiple VMs per process, this can get stuffed into TLS (or
26 * accessed through a Thread field). May need to pass it around for some
27 * of the early initialization functions.
28 */
29#ifndef _DALVIK_GLOBALS
30#define _DALVIK_GLOBALS
31
Elliott Hughes49dc0602011-02-10 12:03:34 -080032#include <cutils/array.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080033#include <stdarg.h>
34#include <pthread.h>
35
36#define MAX_BREAKPOINTS 20 /* used for a debugger optimization */
37
Andy McFadden96516932009-10-28 17:39:02 -070038/* private structures */
39typedef struct GcHeap GcHeap;
40typedef struct BreakpointSet BreakpointSet;
Andy McFaddencb3c5422010-04-07 15:56:16 -070041typedef struct InlineSub InlineSub;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080042
43/*
44 * One of these for each -ea/-da/-esa/-dsa on the command line.
45 */
46typedef struct AssertionControl {
47 char* pkgOrClass; /* package/class string, or NULL for esa/dsa */
48 int pkgOrClassLen; /* string length, for quick compare */
49 bool enable; /* enable or disable */
50 bool isPackage; /* string ended with "..."? */
51} AssertionControl;
52
53/*
54 * Execution mode, e.g. interpreter vs. JIT.
55 */
56typedef enum ExecutionMode {
57 kExecutionModeUnknown = 0,
58 kExecutionModeInterpPortable,
59 kExecutionModeInterpFast,
Ben Chengba4fc8b2009-06-01 13:00:29 -070060#if defined(WITH_JIT)
61 kExecutionModeJit,
62#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080063} ExecutionMode;
64
65/*
buzbeecb3081f2011-01-14 13:37:31 -080066 * Execution sub modes, e.g. debugging, profiling, etc.
67 * Treated as bit flags for fast access. These values are used directly
68 * by assembly code in the mterp interpeter and may also be used by
69 * code generated by the JIT. Take care when changing.
70 */
71typedef enum ExecutionSubModes {
72 kSubModeNormal = 0x00,
73 kSubModeMethodTrace = 0x01,
74 kSubModeEmulatorTrace = 0x02,
75 kSubModeInstCounting = 0x04,
76 kSubModeDebuggerActive = 0x08,
77 kSubModeSuspendRequest = 0x10, /* Set if any suspend request active */
78} ExecutionSubModes;
79
80/*
Andy McFadden701d2722010-12-01 16:18:19 -080081 * Register map generation mode. Only applicable when generateRegisterMaps
82 * is enabled. (The "disabled" state is not folded into this because
83 * there are callers like dexopt that want to enable/disable without
84 * specifying the configuration details.)
85 *
86 * "TypePrecise" is slower and requires additional storage for the register
87 * maps, but allows type-precise GC. "LivePrecise" is even slower and
88 * requires additional heap during processing, but allows live-precise GC.
89 */
90typedef enum {
91 kRegisterMapModeUnknown = 0,
92 kRegisterMapModeTypePrecise,
93 kRegisterMapModeLivePrecise
94} RegisterMapMode;
95
96/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080097 * All fields are initialized to zero.
98 *
99 * Storage allocated here must be freed by a subsystem shutdown function or
100 * from within freeGlobals().
101 */
102struct DvmGlobals {
103 /*
104 * Some options from the command line or environment.
105 */
106 char* bootClassPathStr;
107 char* classPathStr;
108
Carl Shapirodf9f08b2011-01-18 17:59:30 -0800109 size_t heapStartingSize;
110 size_t heapMaximumSize;
111 size_t heapGrowthLimit;
112 size_t stackSize;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800113
114 bool verboseGc;
115 bool verboseJni;
116 bool verboseClass;
Andy McFadden43eb5012010-02-01 16:56:53 -0800117 bool verboseShutdown;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118
119 bool jdwpAllowed; // debugging allowed for this process?
120 bool jdwpConfigured; // has debugging info been provided?
121 int jdwpTransport;
122 bool jdwpServer;
123 char* jdwpHost;
124 int jdwpPort;
125 bool jdwpSuspend;
126
Andy McFaddenea414342010-08-25 12:05:44 -0700127 /* use wall clock as method profiler clock source? */
128 bool profilerWallClock;
129
Carl Shapirob8fcf572010-04-16 17:33:15 -0700130 /*
131 * Lock profiling threshold value in milliseconds. Acquires that
132 * exceed threshold are logged. Acquires within the threshold are
133 * logged with a probability of $\frac{time}{threshold}$ . If the
134 * threshold is unset no additional logging occurs.
135 */
Carl Shapirof0c514c2010-04-09 15:03:33 -0700136 u4 lockProfThreshold;
Carl Shapirof0c514c2010-04-09 15:03:33 -0700137
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800138 int (*vfprintfHook)(FILE*, const char*, va_list);
139 void (*exitHook)(int);
140 void (*abortHook)(void);
Brad Fitzpatrick3b556752010-12-13 16:53:28 -0800141 bool (*isSensitiveThreadHook)(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800142
143 int jniGrefLimit; // 0 means no limit
Elliott Hughes8afa9df2010-07-07 14:47:25 -0700144 char* jniTrace;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800145 bool reduceSignals;
146 bool noQuitHandler;
147 bool verifyDexChecksum;
148 char* stackTraceFile; // for SIGQUIT-inspired output
149
150 bool logStdio;
151
152 DexOptimizerMode dexOptMode;
153 DexClassVerifyMode classVerifyMode;
Barry Hayes5cbb2302010-02-02 14:07:37 -0800154
Andy McFadden701d2722010-12-01 16:18:19 -0800155 bool generateRegisterMaps;
156 RegisterMapMode registerMapMode;
157
Andy McFadden3f64a022010-11-12 16:55:21 -0800158 bool monitorVerification;
159
Andy McFaddenc58b9ef2010-09-09 12:54:43 -0700160 bool dexOptForSmp;
161
Barry Hayes962adba2010-03-17 12:12:39 -0700162 /*
163 * GC option flags.
164 */
The Android Open Source Project99409882009-03-18 22:20:24 -0700165 bool preciseGc;
Barry Hayes962adba2010-03-17 12:12:39 -0700166 bool preVerify;
167 bool postVerify;
Carl Shapiroec805ea2010-06-28 16:28:26 -0700168 bool concurrentMarkSweep;
Barry Hayes6e5cf602010-06-22 12:32:59 -0700169 bool verifyCardTable;
Carl Shapiro821fd062011-01-19 12:56:14 -0800170 bool disableExplicitGc;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800171
172 int assertionCtrlCount;
173 AssertionControl* assertionCtrl;
174
175 ExecutionMode executionMode;
176
177 /*
178 * VM init management.
179 */
180 bool initializing;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800181 bool optimizing;
182
183 /*
Elliott Hughes49dc0602011-02-10 12:03:34 -0800184 * java.lang.System properties set from the command line with -D.
185 * This is effectively a set, where later entries override earlier
186 * ones.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800187 */
Elliott Hughes49dc0602011-02-10 12:03:34 -0800188 Array* properties;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800189
190 /*
191 * Where the VM goes to find system classes.
192 */
193 ClassPathEntry* bootClassPath;
194 /* used by the DEX optimizer to load classes from an unfinished DEX */
195 DvmDex* bootClassPathOptExtra;
196 bool optimizingBootstrapClass;
197
198 /*
199 * Loaded classes, hashed by class name. Each entry is a ClassObject*,
200 * allocated in GC space.
201 */
202 HashTable* loadedClasses;
203
204 /*
205 * Value for the next class serial number to be assigned. This is
206 * incremented as we load classes. Failed loads and races may result
207 * in some numbers being skipped, and the serial number is not
208 * guaranteed to start at 1, so the current value should not be used
209 * as a count of loaded classes.
210 */
211 volatile int classSerialNumber;
212
213 /*
Barry Hayes2c987472009-04-06 10:03:48 -0700214 * Classes with a low classSerialNumber are probably in the zygote, and
215 * their InitiatingLoaderList is not used, to promote sharing. The list is
216 * kept here instead.
217 */
218 InitiatingLoaderList* initiatingLoaderList;
219
220 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800221 * Interned strings.
222 */
Carl Shapirobb1e0e92010-07-21 14:49:25 -0700223
224 /* A mutex that guards access to the interned string tables. */
225 pthread_mutex_t internLock;
226
227 /* Hash table of strings interned by the user. */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800228 HashTable* internedStrings;
229
Carl Shapirobb1e0e92010-07-21 14:49:25 -0700230 /* Hash table of strings interned by the class loader. */
231 HashTable* literalStrings;
232
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800233 /*
Dan Bornstein318839c2011-03-14 11:00:35 -0700234 * Classes constructed directly by the vm.
235 */
236
237 /* the class Class */
238 ClassObject* classJavaLangClass;
239
240 /* synthetic classes representing primitive types */
241 ClassObject* typeVoid;
242 ClassObject* typeBoolean;
243 ClassObject* typeByte;
244 ClassObject* typeShort;
245 ClassObject* typeChar;
246 ClassObject* typeInt;
247 ClassObject* typeLong;
248 ClassObject* typeFloat;
249 ClassObject* typeDouble;
250
251 /* synthetic classes for arrays of primitives */
252 ClassObject* classArrayBoolean;
253 ClassObject* classArrayByte;
254 ClassObject* classArrayShort;
255 ClassObject* classArrayChar;
256 ClassObject* classArrayInt;
257 ClassObject* classArrayLong;
258 ClassObject* classArrayFloat;
259 ClassObject* classArrayDouble;
260
261 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800262 * Quick lookups for popular classes used internally.
263 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800264 ClassObject* classJavaLangClassArray;
Andy McFadden09709762011-03-18 14:27:06 -0700265 ClassObject* classJavaLangClassLoader;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800266 ClassObject* classJavaLangObject;
267 ClassObject* classJavaLangObjectArray;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800268 ClassObject* classJavaLangString;
269 ClassObject* classJavaLangThread;
270 ClassObject* classJavaLangVMThread;
271 ClassObject* classJavaLangThreadGroup;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800272 ClassObject* classJavaLangStackTraceElement;
273 ClassObject* classJavaLangStackTraceElementArray;
274 ClassObject* classJavaLangAnnotationAnnotationArray;
275 ClassObject* classJavaLangAnnotationAnnotationArrayArray;
276 ClassObject* classJavaLangReflectAccessibleObject;
277 ClassObject* classJavaLangReflectConstructor;
278 ClassObject* classJavaLangReflectConstructorArray;
279 ClassObject* classJavaLangReflectField;
280 ClassObject* classJavaLangReflectFieldArray;
281 ClassObject* classJavaLangReflectMethod;
282 ClassObject* classJavaLangReflectMethodArray;
283 ClassObject* classJavaLangReflectProxy;
Andy McFadden8e5c7842009-07-23 17:47:18 -0700284 ClassObject* classJavaNioReadWriteDirectByteBuffer;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800285 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationFactory;
286 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationMember;
287 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationMemberArray;
Carl Shapiro3475f9c2011-03-21 13:35:24 -0700288 ClassObject* classJavaLangRefFinalizerReference;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800289
Dan Bornstein32bb3da2011-02-24 15:47:20 -0800290 /*
291 * classes representing exception types. The names here don't include
292 * packages, just to keep the use sites a bit less verbose. All are
293 * in java.lang, except where noted.
294 */
Dan Bornstein85ba81d2011-03-03 13:10:04 -0800295 ClassObject* exAbstractMethodError;
Dan Bornstein6d167a42011-02-25 13:31:45 -0800296 ClassObject* exArithmeticException;
297 ClassObject* exArrayIndexOutOfBoundsException;
298 ClassObject* exArrayStoreException;
299 ClassObject* exClassCastException;
Dan Bornstein85ba81d2011-03-03 13:10:04 -0800300 ClassObject* exClassCircularityError;
Dan Bornstein2c8e25b2011-02-25 15:49:29 -0800301 ClassObject* exClassFormatError;
Dan Bornstein9b598e32011-03-01 10:28:42 -0800302 ClassObject* exClassNotFoundException;
Dan Bornstein32bb3da2011-02-24 15:47:20 -0800303 ClassObject* exError;
304 ClassObject* exExceptionInInitializerError;
Dan Bornstein2c8e25b2011-02-25 15:49:29 -0800305 ClassObject* exFileNotFoundException; /* in java.io */
306 ClassObject* exIOException; /* in java.io */
Dan Bornstein537e29e2011-03-02 16:28:04 -0800307 ClassObject* exIllegalAccessError;
Dan Bornstein9b598e32011-03-01 10:28:42 -0800308 ClassObject* exIllegalAccessException;
Dan Bornsteinbc606f52011-03-01 13:22:13 -0800309 ClassObject* exIllegalArgumentException;
310 ClassObject* exIllegalMonitorStateException;
311 ClassObject* exIllegalStateException;
312 ClassObject* exIllegalThreadStateException;
Dan Bornstein537e29e2011-03-02 16:28:04 -0800313 ClassObject* exIncompatibleClassChangeError;
Dan Bornsteina3b35122011-03-03 16:17:37 -0800314 ClassObject* exInstantiationError;
Dan Bornsteinbc606f52011-03-01 13:22:13 -0800315 ClassObject* exInstantiationException;
Dan Bornstein85213112011-03-01 16:16:12 -0800316 ClassObject* exInternalError;
Dan Bornstein9b598e32011-03-01 10:28:42 -0800317 ClassObject* exInterruptedException;
Dan Bornstein537e29e2011-03-02 16:28:04 -0800318 ClassObject* exLinkageError;
Dan Bornstein2c8e25b2011-02-25 15:49:29 -0800319 ClassObject* exNegativeArraySizeException;
Dan Bornstein85213112011-03-01 16:16:12 -0800320 ClassObject* exNoClassDefFoundError;
Dan Bornstein537e29e2011-03-02 16:28:04 -0800321 ClassObject* exNoSuchFieldError;
Dan Bornstein9b598e32011-03-01 10:28:42 -0800322 ClassObject* exNoSuchFieldException;
Dan Bornstein537e29e2011-03-02 16:28:04 -0800323 ClassObject* exNoSuchMethodError;
Dan Bornstein2c8e25b2011-02-25 15:49:29 -0800324 ClassObject* exNullPointerException;
Dan Bornstein85ba81d2011-03-03 13:10:04 -0800325 ClassObject* exOutOfMemoryError;
Dan Bornstein32bb3da2011-02-24 15:47:20 -0800326 ClassObject* exRuntimeException;
327 ClassObject* exStackOverflowError;
Dan Bornstein9b598e32011-03-01 10:28:42 -0800328 ClassObject* exStaleDexCacheError; /* in dalvik.system */
Dan Bornstein7a86c442011-02-28 11:23:26 -0800329 ClassObject* exStringIndexOutOfBoundsException;
Dan Bornstein32bb3da2011-02-24 15:47:20 -0800330 ClassObject* exThrowable;
Dan Bornstein85ba81d2011-03-03 13:10:04 -0800331 ClassObject* exTypeNotPresentException;
Dan Bornstein85213112011-03-01 16:16:12 -0800332 ClassObject* exUnsatisfiedLinkError;
Dan Bornstein7a86c442011-02-28 11:23:26 -0800333 ClassObject* exUnsupportedOperationException;
Dan Bornstein85ba81d2011-03-03 13:10:04 -0800334 ClassObject* exVerifyError;
Dan Bornstein7a86c442011-02-28 11:23:26 -0800335 ClassObject* exVirtualMachineError;
Dan Bornstein32bb3da2011-02-24 15:47:20 -0800336
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800337 /* method offsets - Object */
338 int voffJavaLangObject_equals;
339 int voffJavaLangObject_hashCode;
340 int voffJavaLangObject_toString;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800341
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800342 /* field offsets - String */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800343 int offJavaLangString_value;
344 int offJavaLangString_count;
345 int offJavaLangString_offset;
346 int offJavaLangString_hashCode;
347
348 /* field offsets - Thread */
349 int offJavaLangThread_vmThread;
350 int offJavaLangThread_group;
351 int offJavaLangThread_daemon;
352 int offJavaLangThread_name;
353 int offJavaLangThread_priority;
354
355 /* method offsets - Thread */
356 int voffJavaLangThread_run;
357
Andy McFadden86c95932011-03-23 16:15:36 -0700358 /* field offsets - ThreadGroup */
359 int offJavaLangThreadGroup_name;
360 int offJavaLangThreadGroup_parent;
361
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800362 /* field offsets - VMThread */
363 int offJavaLangVMThread_thread;
364 int offJavaLangVMThread_vmData;
365
366 /* method offsets - ThreadGroup */
367 int voffJavaLangThreadGroup_removeThread;
368
369 /* field offsets - Throwable */
370 int offJavaLangThrowable_stackState;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800371 int offJavaLangThrowable_cause;
372
Andy McFadden04771392010-10-07 15:12:14 -0700373 /* method offsets - ClassLoader */
374 int voffJavaLangClassLoader_loadClass;
375
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800376 /* field offsets - java.lang.reflect.* */
377 int offJavaLangReflectAccessibleObject_flag;
378 int offJavaLangReflectConstructor_slot;
379 int offJavaLangReflectConstructor_declClass;
380 int offJavaLangReflectField_slot;
381 int offJavaLangReflectField_declClass;
382 int offJavaLangReflectMethod_slot;
383 int offJavaLangReflectMethod_declClass;
384
385 /* field offsets - java.lang.ref.Reference */
386 int offJavaLangRefReference_referent;
387 int offJavaLangRefReference_queue;
388 int offJavaLangRefReference_queueNext;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700389 int offJavaLangRefReference_pendingNext;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800390
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800391 /* method pointers - java.lang.ref.Reference */
392 Method* methJavaLangRefReference_enqueueInternal;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800393
Carl Shapiro3475f9c2011-03-21 13:35:24 -0700394 /* more method pointers - java.lang.ref.FinalizerReference */
395 Method* methJavaLangRefFinalizerReferenceAdd;
396
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800397 /* constructor method pointers; no vtable involved, so use Method* */
398 Method* methJavaLangStackTraceElement_init;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800399 Method* methJavaLangReflectConstructor_init;
400 Method* methJavaLangReflectField_init;
401 Method* methJavaLangReflectMethod_init;
402 Method* methOrgApacheHarmonyLangAnnotationAnnotationMember_init;
403
404 /* static method pointers - android.lang.annotation.* */
405 Method*
406 methOrgApacheHarmonyLangAnnotationAnnotationFactory_createAnnotation;
407
408 /* direct method pointers - java.lang.reflect.Proxy */
409 Method* methJavaLangReflectProxy_constructorPrototype;
410
411 /* field offsets - java.lang.reflect.Proxy */
412 int offJavaLangReflectProxy_h;
413
414 /* fake native entry point method */
415 Method* methFakeNativeEntry;
416
Andy McFadden8e5c7842009-07-23 17:47:18 -0700417 /* assorted direct buffer helpers */
418 Method* methJavaNioReadWriteDirectByteBuffer_init;
Andy McFadden8e5c7842009-07-23 17:47:18 -0700419 int offJavaNioBuffer_capacity;
Andy McFadden8e696dc2009-07-24 15:28:16 -0700420 int offJavaNioBuffer_effectiveDirectAddress;
Andy McFadden5f612b82009-07-22 15:07:27 -0700421
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800422 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800423 * Thread list. This always has at least one element in it (main),
424 * and main is always the first entry.
425 *
426 * The threadListLock is used for several things, including the thread
427 * start condition variable. Generally speaking, you must hold the
428 * threadListLock when:
429 * - adding/removing items from the list
430 * - waiting on or signaling threadStartCond
431 * - examining the Thread struct for another thread (this is to avoid
432 * one thread freeing the Thread struct while another thread is
433 * perusing it)
434 */
435 Thread* threadList;
436 pthread_mutex_t threadListLock;
437
438 pthread_cond_t threadStartCond;
439
440 /*
441 * The thread code grabs this before suspending all threads. There
Andy McFadden2aa43612009-06-17 16:29:30 -0700442 * are a few things that can cause a "suspend all":
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800443 * (1) the GC is starting;
444 * (2) the debugger has sent a "suspend all" request;
445 * (3) a thread has hit a breakpoint or exception that the debugger
446 * has marked as a "suspend all" event;
447 * (4) the SignalCatcher caught a signal that requires suspension.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700448 * (5) (if implemented) the JIT needs to perform a heavyweight
449 * rearrangement of the translation cache or JitTable.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800450 *
451 * Because we use "safe point" self-suspension, it is never safe to
452 * do a blocking "lock" call on this mutex -- if it has been acquired,
453 * somebody is probably trying to put you to sleep. The leading '_' is
454 * intended as a reminder that this lock is special.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800455 */
456 pthread_mutex_t _threadSuspendLock;
457
458 /*
459 * Guards Thread->suspendCount for all threads, and provides the lock
460 * for the condition variable that all suspended threads sleep on
461 * (threadSuspendCountCond).
462 *
463 * This has to be separate from threadListLock because of the way
464 * threads put themselves to sleep.
465 */
466 pthread_mutex_t threadSuspendCountLock;
467
468 /*
469 * Suspended threads sleep on this. They should sleep on the condition
470 * variable until their "suspend count" is zero.
471 *
472 * Paired with "threadSuspendCountLock".
473 */
474 pthread_cond_t threadSuspendCountCond;
475
476 /*
buzbeecb3081f2011-01-14 13:37:31 -0800477 * Sum of all threads' suspendCount fields. Guarded by
478 * threadSuspendCountLock.
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700479 */
480 int sumThreadSuspendCount;
481
482 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800483 * MUTEX ORDERING: when locking multiple mutexes, always grab them in
484 * this order to avoid deadlock:
485 *
486 * (1) _threadSuspendLock (use lockThreadSuspend())
487 * (2) threadListLock (use dvmLockThreadList())
488 * (3) threadSuspendCountLock (use lockThreadSuspendCount())
489 */
490
491
492 /*
493 * Thread ID bitmap. We want threads to have small integer IDs so
494 * we can use them in "thin locks".
495 */
496 BitVector* threadIdMap;
497
498 /*
499 * Manage exit conditions. The VM exits when all non-daemon threads
500 * have exited. If the main thread returns early, we need to sleep
501 * on a condition variable.
502 */
503 int nonDaemonThreadCount; /* must hold threadListLock to access */
504 //pthread_mutex_t vmExitLock;
505 pthread_cond_t vmExitCond;
506
507 /*
508 * The set of DEX files loaded by custom class loaders.
509 */
510 HashTable* userDexFiles;
511
512 /*
513 * JNI global reference table.
514 */
Andy McFaddend5ab7262009-08-25 07:19:34 -0700515 IndirectRefTable jniGlobalRefTable;
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800516 IndirectRefTable jniWeakGlobalRefTable;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800517 pthread_mutex_t jniGlobalRefLock;
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800518 pthread_mutex_t jniWeakGlobalRefLock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800519 int jniGlobalRefHiMark;
520 int jniGlobalRefLoMark;
521
522 /*
Andy McFaddenc26bb632009-08-21 12:01:31 -0700523 * JNI pinned object table (used for primitive arrays).
524 */
525 ReferenceTable jniPinRefTable;
526 pthread_mutex_t jniPinRefLock;
527
528 /*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800529 * Native shared library table.
530 */
531 HashTable* nativeLibs;
532
533 /*
534 * GC heap lock. Functions like gcMalloc() acquire this before making
535 * any changes to the heap. It is held throughout garbage collection.
536 */
537 pthread_mutex_t gcHeapLock;
538
Carl Shapiroec47e2e2010-07-01 17:44:46 -0700539 /*
540 * Condition variable to queue threads waiting to retry an
541 * allocation. Signaled after a concurrent GC is completed.
542 */
543 pthread_cond_t gcHeapCond;
544
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800545 /* Opaque pointer representing the heap. */
546 GcHeap* gcHeap;
547
Barry Hayes4496ed92010-07-12 09:52:20 -0700548 /* The card table base, modified as needed for marking cards. */
549 u1* biasedCardTableBase;
550
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800551 /*
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700552 * Pre-allocated throwables.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800553 */
554 Object* outOfMemoryObj;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800555 Object* internalErrorObj;
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700556 Object* noClassDefFoundErrorObj;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800557
558 /* Monitor list, so we can free them */
559 /*volatile*/ Monitor* monitorList;
560
561 /* Monitor for Thread.sleep() implementation */
562 Monitor* threadSleepMon;
563
564 /* set when we create a second heap inside the zygote */
565 bool newZygoteHeapAllocated;
566
567 /*
568 * TLS keys.
569 */
570 pthread_key_t pthreadKeySelf; /* Thread*, for dvmThreadSelf */
571
572 /*
573 * JNI allows you to have multiple VMs, but we limit ourselves to 1,
574 * so "vmList" is really just a pointer to the one and only VM.
575 */
576 JavaVM* vmList;
577
578 /*
579 * Cache results of "A instanceof B".
580 */
581 AtomicCache* instanceofCache;
582
Andy McFaddencb3c5422010-04-07 15:56:16 -0700583 /* inline substitution table, used during optimization */
584 InlineSub* inlineSubs;
585
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800586 /*
587 * Bootstrap class loader linear allocator.
588 */
589 LinearAllocHdr* pBootLoaderAlloc;
590
591
592 /*
593 * Heap worker thread.
594 */
595 bool heapWorkerInitialized;
596 bool heapWorkerReady;
597 bool haltHeapWorker;
598 pthread_t heapWorkerHandle;
599 pthread_mutex_t heapWorkerLock;
600 pthread_cond_t heapWorkerCond;
601 pthread_cond_t heapWorkerIdleCond;
602 pthread_mutex_t heapWorkerListLock;
603
604 /*
605 * Compute some stats on loaded classes.
606 */
The Android Open Source Project99409882009-03-18 22:20:24 -0700607 int numLoadedClasses;
608 int numDeclaredMethods;
609 int numDeclaredInstFields;
610 int numDeclaredStaticFields;
611
612 /* when using a native debugger, set this to suppress watchdog timers */
613 bool nativeDebuggerActive;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800614
615 /*
616 * JDWP debugger support.
617 */
618 bool debuggerConnected; /* debugger or DDMS is connected */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800619 JdwpState* jdwpState;
620
621 /*
622 * Registry of objects known to the debugger.
623 */
624 HashTable* dbgRegistry;
625
626 /*
Andy McFadden96516932009-10-28 17:39:02 -0700627 * Debugger breakpoint table.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800628 */
Andy McFadden96516932009-10-28 17:39:02 -0700629 BreakpointSet* breakpointSet;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800630
631 /*
632 * Single-step control struct. We currently only allow one thread to
633 * be single-stepping at a time, which is all that really makes sense,
634 * but it's possible we may need to expand this to be per-thread.
635 */
636 StepControl stepControl;
637
638 /*
639 * DDM features embedded in the VM.
640 */
641 bool ddmThreadNotification;
642
643 /*
644 * Zygote (partially-started process) support
645 */
646 bool zygote;
647
648 /*
649 * Used for tracking allocations that we report to DDMS. When the feature
650 * is enabled (through a DDMS request) the "allocRecords" pointer becomes
651 * non-NULL.
652 */
653 pthread_mutex_t allocTrackerLock;
654 AllocRecord* allocRecords;
655 int allocRecordHead; /* most-recently-added entry */
656 int allocRecordCount; /* #of valid entries */
657
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800658 /*
buzbeecb3081f2011-01-14 13:37:31 -0800659 * When normal control flow needs to be interrupted because
660 * of an attached debugger, profiler, thread stop request, etc.,
661 * a bit is set here. We collapse all stop reasons into
662 * a single location for performance reasons.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800663 */
buzbeecb3081f2011-01-14 13:37:31 -0800664 volatile int interpBreak;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800665
666 /*
667 * State for method-trace profiling.
668 */
669 MethodTraceState methodTrace;
Andy McFadden2ff04ab2011-03-08 15:22:14 -0800670 Method* methodTraceGcMethod;
671 Method* methodTraceClassPrepMethod;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800672
673 /*
674 * State for emulator tracing.
675 */
676 void* emulatorTracePage;
677 int emulatorTraceEnableCount;
678
679 /*
680 * Global state for memory allocation profiling.
681 */
682 AllocProfState allocProf;
683
684 /*
685 * Pointers to the original methods for things that have been inlined.
686 * This makes it easy for us to output method entry/exit records for
Andy McFadden0d615c32010-08-18 12:19:51 -0700687 * the method calls we're not actually making. (Used by method
688 * profiling.)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800689 */
690 Method** inlinedMethods;
691
692 /*
Dan Bornsteinccaab182010-12-03 15:32:40 -0800693 * Dalvik instruction counts (kNumPackedOpcodes entries).
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800694 */
695 int* executedInstrCounts;
Andy McFadden5183a192010-10-22 15:26:07 -0700696 int instructionCountEnableCount;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800697
698 /*
699 * Signal catcher thread (for SIGQUIT).
700 */
701 pthread_t signalCatcherHandle;
702 bool haltSignalCatcher;
703
704 /*
705 * Stdout/stderr conversion thread.
706 */
707 bool haltStdioConverter;
708 bool stdioConverterReady;
709 pthread_t stdioConverterHandle;
710 pthread_mutex_t stdioConverterLock;
711 pthread_cond_t stdioConverterCond;
712
713 /*
714 * pid of the system_server process. We track it so that when system server
715 * crashes the Zygote process will be killed and restarted.
716 */
717 pid_t systemServerPid;
718
San Mehat894dd462009-09-08 20:29:15 -0700719 int kernelGroupScheduling;
720
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800721//#define COUNT_PRECISE_METHODS
722#ifdef COUNT_PRECISE_METHODS
723 PointerSet* preciseMethods;
724#endif
The Android Open Source Project99409882009-03-18 22:20:24 -0700725
726 /* some RegisterMap statistics, useful during development */
727 void* registerMapStats;
Andy McFadden470cbbb2010-11-04 16:31:37 -0700728
729#ifdef VERIFIER_STATS
730 VerifierStats verifierStats;
731#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800732};
733
734extern struct DvmGlobals gDvm;
735
Ben Chengba4fc8b2009-06-01 13:00:29 -0700736#if defined(WITH_JIT)
Ben Cheng38329f52009-07-07 14:19:20 -0700737
buzbee2e152ba2010-12-15 16:32:35 -0800738/* Trace profiling modes. Ordering matters - off states before on states */
739typedef enum TraceProfilingModes {
740 kTraceProfilingDisabled = 0, // Not profiling
741 kTraceProfilingPeriodicOff = 1, // Periodic profiling, off phase
742 kTraceProfilingContinuous = 2, // Always profiling
743 kTraceProfilingPeriodicOn = 3 // Periodic profiling, on phase
744} TraceProfilingModes;
745
Ben Chengba4fc8b2009-06-01 13:00:29 -0700746/*
Ben Cheng6c10a972009-10-29 14:39:18 -0700747 * Exiting the compiled code w/o chaining will incur overhead to look up the
748 * target in the code cache which is extra work only when JIT is enabled. So
749 * we want to monitor it closely to make sure we don't have performance bugs.
750 */
751typedef enum NoChainExits {
752 kInlineCacheMiss = 0,
753 kCallsiteInterpreted,
754 kSwitchOverflow,
Bill Buzbeefccb31d2010-02-04 16:09:55 -0800755 kHeavyweightMonitor,
Ben Cheng6c10a972009-10-29 14:39:18 -0700756 kNoChainExitLast,
757} NoChainExits;
758
759/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700760 * JIT-specific global state
761 */
762struct DvmJitGlobals {
763 /*
764 * Guards writes to Dalvik PC (dPC), translated code address (codeAddr) and
765 * chain fields within the JIT hash table. Note carefully the access
766 * mechanism.
767 * Only writes are guarded, and the guarded fields must be updated in a
768 * specific order using atomic operations. Further, once a field is
769 * written it cannot be changed without halting all threads.
770 *
771 * The write order is:
772 * 1) codeAddr
773 * 2) dPC
774 * 3) chain [if necessary]
775 *
776 * This mutex also guards both read and write of curJitTableEntries.
777 */
778 pthread_mutex_t tableLock;
779
780 /* The JIT hash table. Note that for access speed, copies of this pointer
781 * are stored in each thread. */
782 struct JitEntry *pJitEntryTable;
783
buzbee2e152ba2010-12-15 16:32:35 -0800784 /* Array of compilation trigger threshold counters */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 unsigned char *pProfTable;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786
buzbee2e152ba2010-12-15 16:32:35 -0800787 /* Trace profiling counters */
788 struct JitTraceProfCounters *pJitTraceProfCounters;
789
Bill Buzbee06bb8392010-01-31 18:53:15 -0800790 /* Copy of pProfTable used for temporarily disabling the Jit */
791 unsigned char *pProfTableCopy;
792
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 /* Size of JIT hash table in entries. Must be a power of 2 */
Bill Buzbee27176222009-06-09 09:20:16 -0700794 unsigned int jitTableSize;
795
796 /* Mask used in hash function for JitTable. Should be jitTableSize-1 */
797 unsigned int jitTableMask;
798
799 /* How many entries in the JitEntryTable are in use */
800 unsigned int jitTableEntriesUsed;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801
Ben Cheng94e79eb2010-02-04 16:15:59 -0800802 /* Bytes allocated for the code cache */
803 unsigned int codeCacheSize;
804
Ben Chengba4fc8b2009-06-01 13:00:29 -0700805 /* Trigger for trace selection */
806 unsigned short threshold;
807
808 /* JIT Compiler Control */
809 bool haltCompilerThread;
810 bool blockingMode;
buzbee18fba342011-01-19 15:31:15 -0800811 bool methodTraceSupport;
Ben Cheng7ab74e12011-02-03 14:02:06 -0800812 bool genSuspendPoll;
Ben Cheng385828e2011-03-04 16:48:33 -0800813 Thread* compilerThread;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700814 pthread_t compilerHandle;
815 pthread_mutex_t compilerLock;
Ben Chengc3b92b22010-01-26 16:46:15 -0800816 pthread_mutex_t compilerICPatchLock;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700817 pthread_cond_t compilerQueueActivity;
818 pthread_cond_t compilerQueueEmpty;
Ben Cheng88a0f972010-02-24 15:00:40 -0800819 volatile int compilerQueueLength;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700820 int compilerHighWater;
821 int compilerWorkEnqueueIndex;
822 int compilerWorkDequeueIndex;
Ben Chengc3b92b22010-01-26 16:46:15 -0800823 int compilerICPatchIndex;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700824
825 /* JIT internal stats */
826 int compilerMaxQueued;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700827 int translationChains;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828
829 /* Compiled code cache */
830 void* codeCache;
831
Ben Cheng385828e2011-03-04 16:48:33 -0800832 /*
833 * This is used to store the base address of an in-flight compilation whose
834 * class object pointers have been calculated to populate literal pool.
835 * Once the compiler thread has changed its status to VM_WAIT, we cannot
836 * guarantee whether GC has happened before the code address has been
837 * installed to the JIT table. Because of that, this field can only
838 * been cleared/overwritten by the compiler thread if it is in the
839 * THREAD_RUNNING state or in a safe point.
840 */
841 void *inflightBaseAddr;
842
buzbee18fba342011-01-19 15:31:15 -0800843 /* Translation cache version (protected by compilerLock */
844 int cacheVersion;
845
Ben Cheng8b258bf2009-06-24 17:27:07 -0700846 /* Bytes used by the code templates */
847 unsigned int templateSize;
848
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849 /* Bytes already used in the code cache */
850 unsigned int codeCacheByteUsed;
851
852 /* Number of installed compilations in the cache */
853 unsigned int numCompilations;
854
855 /* Flag to indicate that the code cache is full */
856 bool codeCacheFull;
857
Ben Chengb88ec3c2010-05-17 12:50:33 -0700858 /* Page size - 1 */
859 unsigned int pageSizeMask;
860
861 /* Lock to change the protection type of the code cache */
862 pthread_mutex_t codeCacheProtectionLock;
863
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800864 /* Number of times that the code cache has been reset */
865 int numCodeCacheReset;
866
Ben Chengc3b92b22010-01-26 16:46:15 -0800867 /* Number of times that the code cache reset request has been delayed */
868 int numCodeCacheResetDelayed;
869
Ben Chengba4fc8b2009-06-01 13:00:29 -0700870 /* true/false: compile/reject opcodes specified in the -Xjitop list */
871 bool includeSelectedOp;
872
873 /* true/false: compile/reject methods specified in the -Xjitmethod list */
874 bool includeSelectedMethod;
875
876 /* Disable JIT for selected opcodes - one bit for each opcode */
877 char opList[32];
878
879 /* Disable JIT for selected methods */
880 HashTable *methodTable;
881
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 /* Flag to dump all compiled code */
883 bool printMe;
Bill Buzbee6e963e12009-06-17 16:56:19 -0700884
Ben Cheng46cd4fb2011-03-16 17:19:06 -0700885 /* Per-process debug flag toggled when receiving a SIGUSR2 */
886 bool receivedSIGUSR2;
887
buzbee2e152ba2010-12-15 16:32:35 -0800888 /* Trace profiling mode */
889 TraceProfilingModes profileMode;
890
891 /* Periodic trace profiling countdown timer */
892 int profileCountdown;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700893
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700894 /* Vector to disable selected optimizations */
895 int disableOpt;
896
Ben Cheng8b258bf2009-06-24 17:27:07 -0700897 /* Table to track the overall and trace statistics of hot methods */
898 HashTable* methodStatsTable;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700899
Ben Cheng33672452010-01-12 14:59:30 -0800900 /* Filter method compilation blacklist with call-graph information */
901 bool checkCallGraph;
902
Ben Chengc3b92b22010-01-26 16:46:15 -0800903 /* New translation chain has been set up */
904 volatile bool hasNewChain;
905
Ben Chengbcdc1de2009-08-21 16:18:46 -0700906#if defined(WITH_SELF_VERIFICATION)
907 /* Spin when error is detected, volatile so GDB can reset it */
908 volatile bool selfVerificationSpin;
909#endif
Ben Chengc3b92b22010-01-26 16:46:15 -0800910
Bill Buzbeefccb31d2010-02-04 16:09:55 -0800911 /* Framework or stand-alone? */
912 bool runningInAndroidFramework;
913
Ben Chengc5285b32010-02-14 16:17:36 -0800914 /* Framework callback happened? */
915 bool alreadyEnabledViaFramework;
916
917 /* Framework requests to disable the JIT for good */
918 bool disableJit;
919
Ben Chengdca71432010-03-16 16:04:11 -0700920#if defined(SIGNATURE_BREAKPOINT)
921 /* Signature breakpoint */
922 u4 signatureBreakpointSize; // # of words
923 u4 *signatureBreakpoint; // Signature content
924#endif
925
Ben Cheng978738d2010-05-13 13:45:57 -0700926#if defined(WITH_JIT_TUNING)
927 /* Performance tuning counters */
928 int addrLookupsFound;
929 int addrLookupsNotFound;
930 int noChainExit[kNoChainExitLast];
931 int normalExit;
932 int puntExit;
933 int invokeMonomorphic;
934 int invokePolymorphic;
935 int invokeNative;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700936 int invokeMonoGetterInlined;
937 int invokeMonoSetterInlined;
938 int invokePolyGetterInlined;
939 int invokePolySetterInlined;
Ben Cheng978738d2010-05-13 13:45:57 -0700940 int returnOp;
Ben Chengb88ec3c2010-05-17 12:50:33 -0700941 int icPatchInit;
942 int icPatchLockFree;
Ben Cheng978738d2010-05-13 13:45:57 -0700943 int icPatchQueued;
Ben Chengb88ec3c2010-05-17 12:50:33 -0700944 int icPatchRejected;
Ben Cheng978738d2010-05-13 13:45:57 -0700945 int icPatchDropped;
Ben Cheng978738d2010-05-13 13:45:57 -0700946 int codeCachePatches;
Ben Cheng385828e2011-03-04 16:48:33 -0800947 int numCompilerThreadBlockGC;
948 u8 jitTime;
949 u8 compilerThreadBlockGCStart;
950 u8 compilerThreadBlockGCTime;
951 u8 maxCompilerThreadBlockGCTime;
Ben Cheng978738d2010-05-13 13:45:57 -0700952#endif
953
Ben Chengc3b92b22010-01-26 16:46:15 -0800954 /* Place arrays at the end to ease the display in gdb sessions */
955
956 /* Work order queue for compilations */
957 CompilerWorkOrder compilerWorkQueue[COMPILER_WORK_QUEUE_SIZE];
958
959 /* Work order queue for predicted chain patching */
960 ICPatchWorkOrder compilerICPatchQueue[COMPILER_IC_PATCH_QUEUE_SIZE];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961};
962
963extern struct DvmJitGlobals gDvmJit;
964
Ben Cheng978738d2010-05-13 13:45:57 -0700965#if defined(WITH_JIT_TUNING)
966extern int gDvmICHitCount;
967#endif
968
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969#endif
970
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800971#endif /*_DALVIK_GLOBALS*/