blob: 99642854c588ec19db3dfbaf566e8ec58250c94e [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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#include <sys/mman.h>
18#include <errno.h>
Ben Cheng7c4afdb2010-02-11 15:03:00 -080019#include <cutils/ashmem.h>
Ben Chengba4fc8b2009-06-01 13:00:29 -070020
21#include "Dalvik.h"
22#include "interp/Jit.h"
23#include "CompilerInternals.h"
24
Ben Chengba4fc8b2009-06-01 13:00:29 -070025static inline bool workQueueLength(void)
26{
27 return gDvmJit.compilerQueueLength;
28}
29
30static CompilerWorkOrder workDequeue(void)
31{
32 assert(gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex].kind
33 != kWorkOrderInvalid);
34 CompilerWorkOrder work =
35 gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex];
36 gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex++].kind =
37 kWorkOrderInvalid;
38 if (gDvmJit.compilerWorkDequeueIndex == COMPILER_WORK_QUEUE_SIZE) {
39 gDvmJit.compilerWorkDequeueIndex = 0;
40 }
41 gDvmJit.compilerQueueLength--;
Bill Buzbeef9f33282009-11-22 12:45:30 -080042 if (gDvmJit.compilerQueueLength == 0) {
43 int cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
44 }
Ben Chengba4fc8b2009-06-01 13:00:29 -070045
46 /* Remember the high water mark of the queue length */
47 if (gDvmJit.compilerQueueLength > gDvmJit.compilerMaxQueued)
48 gDvmJit.compilerMaxQueued = gDvmJit.compilerQueueLength;
49
50 return work;
51}
52
Bill Buzbee964a7b02010-01-28 12:54:19 -080053/*
54 * Attempt to enqueue a work order, returning true if successful.
55 * This routine will not block, but simply return if it couldn't
56 * aquire the lock or if the queue is full.
Ben Cheng1357e942010-02-10 17:21:39 -080057 *
58 * NOTE: Make sure that the caller frees the info pointer if the return value
59 * is false.
Bill Buzbee964a7b02010-01-28 12:54:19 -080060 */
Ben Chengba4fc8b2009-06-01 13:00:29 -070061bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info)
62{
63 int cc;
64 int i;
65 int numWork;
Ben Cheng60c24f42010-01-04 12:29:56 -080066 bool result = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -070067
Bill Buzbee964a7b02010-01-28 12:54:19 -080068 if (dvmTryLockMutex(&gDvmJit.compilerLock)) {
Ben Cheng3e5cd172010-02-08 20:57:59 -080069 return false; // Couldn't acquire the lock
Bill Buzbee964a7b02010-01-28 12:54:19 -080070 }
Ben Chengba4fc8b2009-06-01 13:00:29 -070071
Ben Cheng7a0bcd02010-01-22 16:45:45 -080072 /*
Ben Cheng6999d842010-01-26 16:46:15 -080073 * Return if queue or code cache is full.
Ben Cheng7a0bcd02010-01-22 16:45:45 -080074 */
Ben Cheng6999d842010-01-26 16:46:15 -080075 if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE ||
76 gDvmJit.codeCacheFull == true) {
Ben Cheng60c24f42010-01-04 12:29:56 -080077 result = false;
Bill Buzbee964a7b02010-01-28 12:54:19 -080078 goto unlockAndExit;
Ben Chengba4fc8b2009-06-01 13:00:29 -070079 }
80
81 for (numWork = gDvmJit.compilerQueueLength,
82 i = gDvmJit.compilerWorkDequeueIndex;
83 numWork > 0;
84 numWork--) {
85 /* Already enqueued */
86 if (gDvmJit.compilerWorkQueue[i++].pc == pc)
Bill Buzbee964a7b02010-01-28 12:54:19 -080087 goto unlockAndExit;
Ben Chengba4fc8b2009-06-01 13:00:29 -070088 /* Wrap around */
89 if (i == COMPILER_WORK_QUEUE_SIZE)
90 i = 0;
91 }
92
Ben Chengccd6c012009-10-15 14:52:45 -070093 CompilerWorkOrder *newOrder =
94 &gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex];
95 newOrder->pc = pc;
96 newOrder->kind = kind;
97 newOrder->info = info;
98 newOrder->result.codeAddress = NULL;
99 newOrder->result.discardResult =
Bill Buzbee1f748632010-03-02 16:14:41 -0800100 (kind == kWorkOrderTraceDebug) ? true : false;
Ben Cheng33672452010-01-12 14:59:30 -0800101 newOrder->result.requestingThread = dvmThreadSelf();
102
Ben Chengba4fc8b2009-06-01 13:00:29 -0700103 gDvmJit.compilerWorkEnqueueIndex++;
104 if (gDvmJit.compilerWorkEnqueueIndex == COMPILER_WORK_QUEUE_SIZE)
105 gDvmJit.compilerWorkEnqueueIndex = 0;
106 gDvmJit.compilerQueueLength++;
107 cc = pthread_cond_signal(&gDvmJit.compilerQueueActivity);
108 assert(cc == 0);
109
Bill Buzbee964a7b02010-01-28 12:54:19 -0800110unlockAndExit:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700111 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Cheng60c24f42010-01-04 12:29:56 -0800112 return result;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700113}
114
Ben Cheng11d8f142010-03-24 15:24:19 -0700115/* Block until the queue length is 0, or there is a pending suspend request */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700116void dvmCompilerDrainQueue(void)
117{
Ben Cheng11d8f142010-03-24 15:24:19 -0700118 Thread *self = dvmThreadSelf();
119
Ben Chengba4fc8b2009-06-01 13:00:29 -0700120 dvmLockMutex(&gDvmJit.compilerLock);
Ben Cheng11d8f142010-03-24 15:24:19 -0700121 while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread &&
122 self->suspendCount == 0) {
Ben Cheng812e6b12010-03-15 15:19:06 -0700123 /*
124 * Use timed wait here - more than one mutator threads may be blocked
125 * but the compiler thread will only signal once when the queue is
126 * emptied. Furthermore, the compiler thread may have been shutdown
127 * so the blocked thread may never get the wakeup signal.
128 */
129 dvmRelativeCondWait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock, 1000, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700130 }
131 dvmUnlockMutex(&gDvmJit.compilerLock);
132}
133
Ben Cheng60c24f42010-01-04 12:29:56 -0800134bool dvmCompilerSetupCodeCache(void)
135{
136 extern void dvmCompilerTemplateStart(void);
137 extern void dmvCompilerTemplateEnd(void);
Ben Cheng7c4afdb2010-02-11 15:03:00 -0800138 int fd;
Ben Cheng60c24f42010-01-04 12:29:56 -0800139
140 /* Allocate the code cache */
Ben Cheng7c4afdb2010-02-11 15:03:00 -0800141 fd = ashmem_create_region("dalvik-jit-code-cache", gDvmJit.codeCacheSize);
142 if (fd < 0) {
143 LOGE("Could not create %u-byte ashmem region for the JIT code cache",
144 gDvmJit.codeCacheSize);
145 return false;
146 }
147 gDvmJit.codeCache = mmap(NULL, gDvmJit.codeCacheSize,
148 PROT_READ | PROT_WRITE | PROT_EXEC,
149 MAP_PRIVATE , fd, 0);
150 close(fd);
Ben Cheng60c24f42010-01-04 12:29:56 -0800151 if (gDvmJit.codeCache == MAP_FAILED) {
Ben Cheng7c4afdb2010-02-11 15:03:00 -0800152 LOGE("Failed to mmap the JIT code cache: %s\n", strerror(errno));
Ben Cheng60c24f42010-01-04 12:29:56 -0800153 return false;
154 }
155
Ben Cheng7c4afdb2010-02-11 15:03:00 -0800156 /* This can be found through "dalvik-jit-code-cache" in /proc/<pid>/maps */
157 // LOGD("Code cache starts at %p", gDvmJit.codeCache);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800158
Ben Cheng60c24f42010-01-04 12:29:56 -0800159 /* Copy the template code into the beginning of the code cache */
160 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
161 (intptr_t) dvmCompilerTemplateStart;
162 memcpy((void *) gDvmJit.codeCache,
163 (void *) dvmCompilerTemplateStart,
164 templateSize);
165
Ben Cheng72621c92010-03-10 13:12:55 -0800166 /*
167 * Work around a CPU bug by keeping the 32-bit ARM handler code in its own
168 * page.
169 */
170 if (dvmCompilerInstructionSet() == DALVIK_JIT_THUMB2) {
171 templateSize = (templateSize + 4095) & ~4095;
172 }
173
Ben Cheng60c24f42010-01-04 12:29:56 -0800174 gDvmJit.templateSize = templateSize;
175 gDvmJit.codeCacheByteUsed = templateSize;
176
177 /* Only flush the part in the code cache that is being used now */
178 cacheflush((intptr_t) gDvmJit.codeCache,
179 (intptr_t) gDvmJit.codeCache + templateSize, 0);
180 return true;
181}
182
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800183static void crawlDalvikStack(Thread *thread, bool print)
184{
185 void *fp = thread->curFrame;
186 StackSaveArea* saveArea = NULL;
187 int stackLevel = 0;
188
189 if (print) {
190 LOGD("Crawling tid %d (%s / %p %s)", thread->systemTid,
191 dvmGetThreadStatusStr(thread->status),
192 thread->inJitCodeCache,
193 thread->inJitCodeCache ? "jit" : "interp");
194 }
195 /* Crawl the Dalvik stack frames to clear the returnAddr field */
196 while (fp != NULL) {
197 saveArea = SAVEAREA_FROM_FP(fp);
198
199 if (print) {
200 if (dvmIsBreakFrame(fp)) {
201 LOGD(" #%d: break frame (%p)",
202 stackLevel, saveArea->returnAddr);
203 }
204 else {
205 LOGD(" #%d: %s.%s%s (%p)",
206 stackLevel,
207 saveArea->method->clazz->descriptor,
208 saveArea->method->name,
209 dvmIsNativeMethod(saveArea->method) ?
210 " (native)" : "",
211 saveArea->returnAddr);
212 }
213 }
214 stackLevel++;
215 saveArea->returnAddr = NULL;
216 assert(fp != saveArea->prevFrame);
217 fp = saveArea->prevFrame;
218 }
219 /* Make sure the stack is fully unwound to the bottom */
220 assert(saveArea == NULL ||
221 (u1 *) (saveArea+1) == thread->interpStackStart);
222}
223
Ben Cheng60c24f42010-01-04 12:29:56 -0800224static void resetCodeCache(void)
225{
Ben Cheng60c24f42010-01-04 12:29:56 -0800226 Thread* thread;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800227 u8 startTime = dvmGetRelativeTimeUsec();
228 int inJit = 0;
Ben Cheng6999d842010-01-26 16:46:15 -0800229 int byteUsed = gDvmJit.codeCacheByteUsed;
Ben Cheng60c24f42010-01-04 12:29:56 -0800230
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800231 /* If any thread is found stuck in the JIT state, don't reset the cache */
232 for (thread = gDvm.threadList; thread != NULL; thread = thread->next) {
Ben Cheng6999d842010-01-26 16:46:15 -0800233 /*
234 * Crawl the stack to wipe out the returnAddr field so that
235 * 1) the soon-to-be-deleted code in the JIT cache won't be used
236 * 2) or the thread stuck in the JIT land will soon return
237 * to the interpreter land
238 */
239 crawlDalvikStack(thread, false);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800240 if (thread->inJitCodeCache) {
241 inJit++;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800242 }
243 }
244
245 if (inJit) {
Ben Cheng6999d842010-01-26 16:46:15 -0800246 LOGD("JIT code cache reset delayed (%d bytes %d/%d)",
247 gDvmJit.codeCacheByteUsed, gDvmJit.numCodeCacheReset,
248 ++gDvmJit.numCodeCacheResetDelayed);
249 return;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800250 }
251
Ben Cheng6999d842010-01-26 16:46:15 -0800252 /* Lock the mutex to clean up the work queue */
253 dvmLockMutex(&gDvmJit.compilerLock);
254
255 /* Drain the work queue to free the work orders */
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800256 while (workQueueLength()) {
257 CompilerWorkOrder work = workDequeue();
258 free(work.info);
259 }
260
Ben Cheng60c24f42010-01-04 12:29:56 -0800261 /* Reset the JitEntry table contents to the initial unpopulated state */
262 dvmJitResetTable();
263
Ben Cheng60c24f42010-01-04 12:29:56 -0800264 /*
Ben Cheng60c24f42010-01-04 12:29:56 -0800265 * Wipe out the code cache content to force immediate crashes if
266 * stale JIT'ed code is invoked.
267 */
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800268 memset((char *) gDvmJit.codeCache + gDvmJit.templateSize,
269 0,
270 gDvmJit.codeCacheByteUsed - gDvmJit.templateSize);
Ben Cheng60c24f42010-01-04 12:29:56 -0800271 cacheflush((intptr_t) gDvmJit.codeCache,
272 (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed, 0);
Ben Cheng60c24f42010-01-04 12:29:56 -0800273
274 /* Reset the current mark of used bytes to the end of template code */
275 gDvmJit.codeCacheByteUsed = gDvmJit.templateSize;
276 gDvmJit.numCompilations = 0;
277
278 /* Reset the work queue */
279 memset(gDvmJit.compilerWorkQueue, 0,
280 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
281 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
282 gDvmJit.compilerQueueLength = 0;
283
Ben Cheng6999d842010-01-26 16:46:15 -0800284 /* Reset the IC patch work queue */
285 dvmLockMutex(&gDvmJit.compilerICPatchLock);
286 gDvmJit.compilerICPatchIndex = 0;
287 dvmUnlockMutex(&gDvmJit.compilerICPatchLock);
288
Ben Cheng60c24f42010-01-04 12:29:56 -0800289 /* All clear now */
290 gDvmJit.codeCacheFull = false;
291
Ben Cheng6999d842010-01-26 16:46:15 -0800292 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800293
Ben Cheng6999d842010-01-26 16:46:15 -0800294 LOGD("JIT code cache reset in %lld ms (%d bytes %d/%d)",
295 (dvmGetRelativeTimeUsec() - startTime) / 1000,
296 byteUsed, ++gDvmJit.numCodeCacheReset,
297 gDvmJit.numCodeCacheResetDelayed);
298}
299
300/*
301 * Perform actions that are only safe when all threads are suspended. Currently
302 * we do:
303 * 1) Check if the code cache is full. If so reset it and restart populating it
304 * from scratch.
305 * 2) Patch predicted chaining cells by consuming recorded work orders.
306 */
307void dvmCompilerPerformSafePointChecks(void)
308{
309 if (gDvmJit.codeCacheFull) {
310 resetCodeCache();
311 }
312 dvmCompilerPatchInlineCache();
Ben Cheng60c24f42010-01-04 12:29:56 -0800313}
314
Bill Buzbee964a7b02010-01-28 12:54:19 -0800315bool compilerThreadStartup(void)
316{
317 JitEntry *pJitTable = NULL;
318 unsigned char *pJitProfTable = NULL;
319 unsigned int i;
320
321 if (!dvmCompilerArchInit())
322 goto fail;
323
324 /*
325 * Setup the code cache if we have not inherited a valid code cache
326 * from the zygote.
327 */
328 if (gDvmJit.codeCache == NULL) {
329 if (!dvmCompilerSetupCodeCache())
330 goto fail;
331 }
332
333 /* Allocate the initial arena block */
334 if (dvmCompilerHeapInit() == false) {
335 goto fail;
336 }
337
Bill Buzbee964a7b02010-01-28 12:54:19 -0800338 dvmLockMutex(&gDvmJit.compilerLock);
339
Ben Cheng1357e942010-02-10 17:21:39 -0800340#if defined(WITH_JIT_TUNING)
Bill Buzbee964a7b02010-01-28 12:54:19 -0800341 /* Track method-level compilation statistics */
342 gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL);
Ben Cheng1357e942010-02-10 17:21:39 -0800343#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800344
345 dvmUnlockMutex(&gDvmJit.compilerLock);
346
347 /* Set up the JitTable */
348
349 /* Power of 2? */
350 assert(gDvmJit.jitTableSize &&
351 !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1)));
352
353 dvmInitMutex(&gDvmJit.tableLock);
354 dvmLockMutex(&gDvmJit.tableLock);
355 pJitTable = (JitEntry*)
356 calloc(gDvmJit.jitTableSize, sizeof(*pJitTable));
357 if (!pJitTable) {
358 LOGE("jit table allocation failed\n");
359 dvmUnlockMutex(&gDvmJit.tableLock);
360 goto fail;
361 }
362 /*
363 * NOTE: the profile table must only be allocated once, globally.
364 * Profiling is turned on and off by nulling out gDvm.pJitProfTable
365 * and then restoring its original value. However, this action
366 * is not syncronized for speed so threads may continue to hold
367 * and update the profile table after profiling has been turned
368 * off by null'ng the global pointer. Be aware.
369 */
370 pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE);
371 if (!pJitProfTable) {
372 LOGE("jit prof table allocation failed\n");
373 dvmUnlockMutex(&gDvmJit.tableLock);
374 goto fail;
375 }
376 memset(pJitProfTable, gDvmJit.threshold, JIT_PROF_SIZE);
377 for (i=0; i < gDvmJit.jitTableSize; i++) {
378 pJitTable[i].u.info.chain = gDvmJit.jitTableSize;
379 }
380 /* Is chain field wide enough for termination pattern? */
381 assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize);
382
383 gDvmJit.pJitEntryTable = pJitTable;
384 gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1;
385 gDvmJit.jitTableEntriesUsed = 0;
386 gDvmJit.compilerHighWater =
387 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
Ben Chenga4973592010-03-31 11:59:18 -0700388 /*
389 * If the VM is launched with wait-on-the-debugger, we will need to hide
390 * the profile table here
391 */
392 gDvmJit.pProfTable = dvmDebuggerOrProfilerActive() ? NULL : pJitProfTable;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800393 gDvmJit.pProfTableCopy = pJitProfTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800394 dvmUnlockMutex(&gDvmJit.tableLock);
395
396 /* Signal running threads to refresh their cached pJitTable pointers */
397 dvmSuspendAllThreads(SUSPEND_FOR_REFRESH);
398 dvmResumeAllThreads(SUSPEND_FOR_REFRESH);
Ben Chengdca71432010-03-16 16:04:11 -0700399
400 /* Enable signature breakpoints by customizing the following code */
401#if defined(SIGNATURE_BREAKPOINT)
402 /*
403 * Suppose one sees the following native crash in the bugreport:
404 * I/DEBUG ( 1638): Build fingerprint: 'unknown'
405 * I/DEBUG ( 1638): pid: 2468, tid: 2507 >>> com.google.android.gallery3d
406 * I/DEBUG ( 1638): signal 11 (SIGSEGV), fault addr 00001400
407 * I/DEBUG ( 1638): r0 44ea7190 r1 44e4f7b8 r2 44ebc710 r3 00000000
408 * I/DEBUG ( 1638): r4 00000a00 r5 41862dec r6 4710dc10 r7 00000280
409 * I/DEBUG ( 1638): r8 ad010f40 r9 46a37a12 10 001116b0 fp 42a78208
410 * I/DEBUG ( 1638): ip 00000090 sp 4710dbc8 lr ad060e67 pc 46b90682
411 * cpsr 00000030
412 * I/DEBUG ( 1638): #00 pc 46b90682 /dev/ashmem/dalvik-jit-code-cache
413 * I/DEBUG ( 1638): #01 pc 00060e62 /system/lib/libdvm.so
414 *
415 * I/DEBUG ( 1638): code around pc:
416 * I/DEBUG ( 1638): 46b90660 6888d01c 34091dcc d2174287 4a186b68
417 * I/DEBUG ( 1638): 46b90670 d0052800 68006809 28004790 6b68d00e
418 * I/DEBUG ( 1638): 46b90680 512000bc 37016eaf 6ea866af 6f696028
419 * I/DEBUG ( 1638): 46b90690 682a6069 429a686b e003da08 6df1480b
420 * I/DEBUG ( 1638): 46b906a0 1c2d4788 47806d70 46a378fa 47806d70
421 *
422 * Clearly it is a JIT bug. To find out which translation contains the
423 * offending code, the content of the memory dump around the faulting PC
424 * can be pasted into the gDvmJit.signatureBreakpoint[] array and next time
425 * when a similar compilation is being created, the JIT compiler replay the
426 * trace in the verbose mode and one can investigate the instruction
427 * sequence in details.
428 *
429 * The length of the signature may need additional experiments to determine.
430 * The rule of thumb is don't include PC-relative instructions in the
431 * signature since it may be affected by the alignment of the compiled code.
432 * However, a signature that's too short might increase the chance of false
433 * positive matches. Using gdbjithelper to disassembly the memory content
434 * first might be a good companion approach.
435 *
436 * For example, if the next 4 words starting from 46b90680 is pasted into
437 * the data structure:
438 */
439
440 gDvmJit.signatureBreakpointSize = 4;
441 gDvmJit.signatureBreakpoint =
442 malloc(sizeof(u4) * gDvmJit.signatureBreakpointSize);
443 gDvmJit.signatureBreakpoint[0] = 0x512000bc;
444 gDvmJit.signatureBreakpoint[1] = 0x37016eaf;
445 gDvmJit.signatureBreakpoint[2] = 0x6ea866af;
446 gDvmJit.signatureBreakpoint[3] = 0x6f696028;
447
448 /*
449 * The following log will be printed when a match is found in subsequent
450 * testings:
451 *
452 * D/dalvikvm( 2468): Signature match starting from offset 0x34 (4 words)
453 * D/dalvikvm( 2468): --------
454 * D/dalvikvm( 2468): Compiler: Building trace for computeVisibleItems,
455 * offset 0x1f7
456 * D/dalvikvm( 2468): 0x46a37a12: 0x0090 add-int v42, v5, v26
457 * D/dalvikvm( 2468): 0x46a37a16: 0x004d aput-object v13, v14, v42
458 * D/dalvikvm( 2468): 0x46a37a1a: 0x0028 goto, (#0), (#0)
459 * D/dalvikvm( 2468): 0x46a3794e: 0x00d8 add-int/lit8 v26, v26, (#1)
460 * D/dalvikvm( 2468): 0x46a37952: 0x0028 goto, (#0), (#0)
461 * D/dalvikvm( 2468): 0x46a378ee: 0x0002 move/from16 v0, v26, (#0)
462 * D/dalvikvm( 2468): 0x46a378f2: 0x0002 move/from16 v1, v29, (#0)
463 * D/dalvikvm( 2468): 0x46a378f6: 0x0035 if-ge v0, v1, (#10)
464 * D/dalvikvm( 2468): TRACEINFO (554): 0x46a37624
465 * Lcom/cooliris/media/GridLayer;computeVisibleItems 0x1f7 14 of 934, 8
466 * blocks
467 * :
468 * :
469 * D/dalvikvm( 2468): 0x20 (0020): ldr r0, [r5, #52]
470 * D/dalvikvm( 2468): 0x22 (0022): ldr r2, [pc, #96]
471 * D/dalvikvm( 2468): 0x24 (0024): cmp r0, #0
472 * D/dalvikvm( 2468): 0x26 (0026): beq 0x00000034
473 * D/dalvikvm( 2468): 0x28 (0028): ldr r1, [r1, #0]
474 * D/dalvikvm( 2468): 0x2a (002a): ldr r0, [r0, #0]
475 * D/dalvikvm( 2468): 0x2c (002c): blx r2
476 * D/dalvikvm( 2468): 0x2e (002e): cmp r0, #0
477 * D/dalvikvm( 2468): 0x30 (0030): beq 0x00000050
478 * D/dalvikvm( 2468): 0x32 (0032): ldr r0, [r5, #52]
479 * D/dalvikvm( 2468): 0x34 (0034): lsls r4, r7, #2
480 * D/dalvikvm( 2468): 0x36 (0036): str r0, [r4, r4]
481 * D/dalvikvm( 2468): -------- dalvik offset: 0x01fb @ goto, (#0), (#0)
482 * D/dalvikvm( 2468): L0x0195:
483 * D/dalvikvm( 2468): -------- dalvik offset: 0x0195 @ add-int/lit8 v26,
484 * v26, (#1)
485 * D/dalvikvm( 2468): 0x38 (0038): ldr r7, [r5, #104]
486 * D/dalvikvm( 2468): 0x3a (003a): adds r7, r7, #1
487 * D/dalvikvm( 2468): 0x3c (003c): str r7, [r5, #104]
488 * D/dalvikvm( 2468): -------- dalvik offset: 0x0197 @ goto, (#0), (#0)
489 * D/dalvikvm( 2468): L0x0165:
490 * D/dalvikvm( 2468): -------- dalvik offset: 0x0165 @ move/from16 v0, v26,
491 * (#0)
492 * D/dalvikvm( 2468): 0x3e (003e): ldr r0, [r5, #104]
493 * D/dalvikvm( 2468): 0x40 (0040): str r0, [r5, #0]
494 *
495 * The "str r0, [r4, r4]" is indeed the culprit of the native crash.
496 */
497#endif
498
Bill Buzbee964a7b02010-01-28 12:54:19 -0800499 return true;
500
501fail:
502 return false;
503
504}
505
Ben Chengba4fc8b2009-06-01 13:00:29 -0700506static void *compilerThreadStart(void *arg)
507{
Bill Buzbee94d89f82010-01-29 13:44:19 -0800508 int ret;
509 struct timespec ts;
510
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700511 dvmChangeStatus(NULL, THREAD_VMWAIT);
512
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800513 /*
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800514 * If we're not running stand-alone, wait a little before
515 * recieving translation requests on the assumption that process start
516 * up code isn't worth compiling. We'll resume when the framework
517 * signals us that the first screen draw has happened, or the timer
518 * below expires (to catch daemons).
Ben Chengf30acbb2010-02-14 16:17:36 -0800519 *
520 * There is a theoretical race between the callback to
521 * VMRuntime.startJitCompiation and when the compiler thread reaches this
522 * point. In case the callback happens earlier, in order not to permanently
523 * hold the system_server (which is not using the timed wait) in
524 * interpreter-only mode we bypass the delay here.
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800525 */
Ben Chengf30acbb2010-02-14 16:17:36 -0800526 if (gDvmJit.runningInAndroidFramework &&
527 !gDvmJit.alreadyEnabledViaFramework) {
528 /*
529 * If the current VM instance is the system server (detected by having
530 * 0 in gDvm.systemServerPid), we will use the indefinite wait on the
531 * conditional variable to determine whether to start the JIT or not.
532 * If the system server detects that the whole system is booted in
533 * safe mode, the conditional variable will never be signaled and the
534 * system server will remain in the interpreter-only mode. All
535 * subsequent apps will be started with the --enable-safemode flag
536 * explicitly appended.
537 */
538 if (gDvm.systemServerPid == 0) {
539 dvmLockMutex(&gDvmJit.compilerLock);
540 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
541 &gDvmJit.compilerLock);
542 dvmUnlockMutex(&gDvmJit.compilerLock);
543 LOGD("JIT started for system_server");
544 } else {
545 dvmLockMutex(&gDvmJit.compilerLock);
546 /*
547 * TUNING: experiment with the delay & perhaps make it
548 * target-specific
549 */
550 dvmRelativeCondWait(&gDvmJit.compilerQueueActivity,
551 &gDvmJit.compilerLock, 3000, 0);
552 dvmUnlockMutex(&gDvmJit.compilerLock);
553 }
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800554 if (gDvmJit.haltCompilerThread) {
555 return NULL;
556 }
Bill Buzbee94d89f82010-01-29 13:44:19 -0800557 }
558
Bill Buzbee964a7b02010-01-28 12:54:19 -0800559 compilerThreadStartup();
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800560
Ben Chengba4fc8b2009-06-01 13:00:29 -0700561 dvmLockMutex(&gDvmJit.compilerLock);
562 /*
563 * Since the compiler thread will not touch any objects on the heap once
564 * being created, we just fake its state as VMWAIT so that it can be a
565 * bit late when there is suspend request pending.
566 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700567 while (!gDvmJit.haltCompilerThread) {
568 if (workQueueLength() == 0) {
569 int cc;
570 cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
571 assert(cc == 0);
572 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
573 &gDvmJit.compilerLock);
574 continue;
575 } else {
576 do {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700577 CompilerWorkOrder work = workDequeue();
578 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Cheng86717f72010-03-05 15:27:21 -0800579#if defined(JIT_STATS)
580 u8 startTime = dvmGetRelativeTimeUsec();
581#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800582 /*
583 * Check whether there is a suspend request on me. This
584 * is necessary to allow a clean shutdown.
Ben Cheng11d8f142010-03-24 15:24:19 -0700585 *
586 * However, in the blocking stress testing mode, let the
587 * compiler thread continue doing compilations to unblock
588 * other requesting threads. This may occasionally cause
589 * shutdown from proceeding cleanly in the standalone invocation
590 * of the vm but this should be acceptable.
Bill Buzbee964a7b02010-01-28 12:54:19 -0800591 */
Ben Cheng11d8f142010-03-24 15:24:19 -0700592 if (!gDvmJit.blockingMode)
Andy McFaddenab227f72010-04-06 12:37:48 -0700593 dvmCheckSuspendPending(dvmThreadSelf());
Bill Buzbee27176222009-06-09 09:20:16 -0700594 /* Is JitTable filling up? */
595 if (gDvmJit.jitTableEntriesUsed >
596 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
Ben Cheng6999d842010-01-26 16:46:15 -0800597 bool resizeFail =
598 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
599 /*
600 * If the jit table is full, consider it's time to reset
601 * the code cache too.
602 */
603 gDvmJit.codeCacheFull |= resizeFail;
Bill Buzbee27176222009-06-09 09:20:16 -0700604 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700605 if (gDvmJit.haltCompilerThread) {
606 LOGD("Compiler shutdown in progress - discarding request");
Ben Cheng6999d842010-01-26 16:46:15 -0800607 } else if (!gDvmJit.codeCacheFull) {
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800608 bool compileOK = false;
609 jmp_buf jmpBuf;
610 work.bailPtr = &jmpBuf;
611 bool aborted = setjmp(jmpBuf);
612 if (!aborted) {
613 compileOK = dvmCompilerDoWork(&work);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700614 }
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800615 if (aborted || !compileOK) {
616 dvmCompilerArenaReset();
617 work.result.codeAddress = gDvmJit.interpretTemplate;
618 } else if (!work.result.discardResult) {
Ben Cheng60c24f42010-01-04 12:29:56 -0800619 dvmJitSetCodeAddr(work.pc, work.result.codeAddress,
620 work.result.instructionSet);
621 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700622 }
623 free(work.info);
Ben Cheng86717f72010-03-05 15:27:21 -0800624#if defined(JIT_STATS)
625 gDvmJit.jitTime += dvmGetRelativeTimeUsec() - startTime;
626#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700627 dvmLockMutex(&gDvmJit.compilerLock);
628 } while (workQueueLength() != 0);
629 }
630 }
631 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
632 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Chengef00a852009-06-22 22:53:35 -0700633
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700634 /*
635 * As part of detaching the thread we need to call into Java code to update
636 * the ThreadGroup, and we should not be in VMWAIT state while executing
637 * interpreted code.
638 */
639 dvmChangeStatus(NULL, THREAD_RUNNING);
640
Andy McFadden43eb5012010-02-01 16:56:53 -0800641 if (gDvm.verboseShutdown)
642 LOGD("Compiler thread shutting down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700643 return NULL;
644}
645
Ben Chengba4fc8b2009-06-01 13:00:29 -0700646bool dvmCompilerStartup(void)
647{
Bill Buzbee94d89f82010-01-29 13:44:19 -0800648
649 dvmInitMutex(&gDvmJit.compilerLock);
Ben Cheng6999d842010-01-26 16:46:15 -0800650 dvmInitMutex(&gDvmJit.compilerICPatchLock);
Bill Buzbee94d89f82010-01-29 13:44:19 -0800651 dvmLockMutex(&gDvmJit.compilerLock);
652 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
653 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
654
655 /* Reset the work queue */
656 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
657 gDvmJit.compilerQueueLength = 0;
658 dvmUnlockMutex(&gDvmJit.compilerLock);
659
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660 /*
Bill Buzbee94d89f82010-01-29 13:44:19 -0800661 * Defer rest of initialization until we're sure JIT'ng makes sense. Launch
Bill Buzbee964a7b02010-01-28 12:54:19 -0800662 * the compiler thread, which will do the real initialization if and
663 * when it is signalled to do so.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700664 */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800665 return dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
666 compilerThreadStart, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700667}
668
669void dvmCompilerShutdown(void)
670{
671 void *threadReturn;
672
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800673 /* Disable new translation requests */
674 gDvmJit.pProfTable = NULL;
675 gDvmJit.pProfTableCopy = NULL;
676
Ben Cheng88a0f972010-02-24 15:00:40 -0800677 if (gDvm.verboseShutdown) {
678 dvmCompilerDumpStats();
679 while (gDvmJit.compilerQueueLength)
680 sleep(5);
681 }
682
Ben Chengba4fc8b2009-06-01 13:00:29 -0700683 if (gDvmJit.compilerHandle) {
684
685 gDvmJit.haltCompilerThread = true;
686
687 dvmLockMutex(&gDvmJit.compilerLock);
688 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
689 dvmUnlockMutex(&gDvmJit.compilerLock);
690
Ben Chengef00a852009-06-22 22:53:35 -0700691 if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0)
692 LOGW("Compiler thread join failed\n");
Andy McFadden43eb5012010-02-01 16:56:53 -0800693 else if (gDvm.verboseShutdown)
Ben Chengef00a852009-06-22 22:53:35 -0700694 LOGD("Compiler thread has shut down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 }
Bill Buzbee06bb8392010-01-31 18:53:15 -0800696
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800697 /* Break loops within the translation cache */
698 dvmJitUnchainAll();
Bill Buzbee96cfe6c2010-02-08 17:08:15 -0800699
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800700 /*
701 * NOTE: our current implementatation doesn't allow for the compiler
702 * thread to be restarted after it exits here. We aren't freeing
703 * the JitTable or the ProfTable because threads which still may be
704 * running or in the process of shutting down may hold references to
705 * them.
706 */
Bill Buzbee96cfe6c2010-02-08 17:08:15 -0800707}
Bill Buzbee06bb8392010-01-31 18:53:15 -0800708
709void dvmCompilerStateRefresh()
710{
711 bool jitActive;
712 bool jitActivate;
Bill Buzbee3e392682010-02-03 18:13:57 -0800713 bool needUnchain = false;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800714
Ben Chenga4973592010-03-31 11:59:18 -0700715 /*
716 * The tableLock might not be initialized yet by the compiler thread if
717 * debugger is attached from the very beginning of the VM launch. If
718 * pProfTableCopy is NULL, the lock is not initialized yet and we don't
719 * need to refresh anything either.
720 */
721 if (gDvmJit.pProfTableCopy == NULL) {
722 return;
723 }
724
Bill Buzbee06bb8392010-01-31 18:53:15 -0800725 dvmLockMutex(&gDvmJit.tableLock);
726 jitActive = gDvmJit.pProfTable != NULL;
727 jitActivate = !(gDvm.debuggerActive || (gDvm.activeProfilers > 0));
728
729 if (jitActivate && !jitActive) {
730 gDvmJit.pProfTable = gDvmJit.pProfTableCopy;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800731 } else if (!jitActivate && jitActive) {
732 gDvmJit.pProfTable = NULL;
Bill Buzbee3e392682010-02-03 18:13:57 -0800733 needUnchain = true;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800734 }
Bill Buzbee3e392682010-02-03 18:13:57 -0800735 dvmUnlockMutex(&gDvmJit.tableLock);
736 if (needUnchain)
737 dvmJitUnchainAll();
Bill Buzbee06bb8392010-01-31 18:53:15 -0800738}