blob: 2368daea7f9ba78a7a6170ab395ad47e2fd6c4ef [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) {
Carl Shapirob31b3012010-05-25 18:35:37 -070043 dvmSignalCond(&gDvmJit.compilerQueueEmpty);
Bill Buzbeef9f33282009-11-22 12:45:30 -080044 }
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 Chengb88ec3c2010-05-17 12:50:33 -0700156 gDvmJit.pageSizeMask = getpagesize() - 1;
157
Ben Cheng7c4afdb2010-02-11 15:03:00 -0800158 /* This can be found through "dalvik-jit-code-cache" in /proc/<pid>/maps */
159 // LOGD("Code cache starts at %p", gDvmJit.codeCache);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800160
Ben Cheng60c24f42010-01-04 12:29:56 -0800161 /* Copy the template code into the beginning of the code cache */
162 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
163 (intptr_t) dvmCompilerTemplateStart;
164 memcpy((void *) gDvmJit.codeCache,
165 (void *) dvmCompilerTemplateStart,
166 templateSize);
167
Ben Cheng72621c92010-03-10 13:12:55 -0800168 /*
169 * Work around a CPU bug by keeping the 32-bit ARM handler code in its own
170 * page.
171 */
172 if (dvmCompilerInstructionSet() == DALVIK_JIT_THUMB2) {
173 templateSize = (templateSize + 4095) & ~4095;
174 }
175
Ben Cheng60c24f42010-01-04 12:29:56 -0800176 gDvmJit.templateSize = templateSize;
177 gDvmJit.codeCacheByteUsed = templateSize;
178
179 /* Only flush the part in the code cache that is being used now */
180 cacheflush((intptr_t) gDvmJit.codeCache,
181 (intptr_t) gDvmJit.codeCache + templateSize, 0);
Ben Chengb88ec3c2010-05-17 12:50:33 -0700182
Ben Cheng1f3da0b2010-06-03 13:52:42 -0700183 int result = mprotect(gDvmJit.codeCache, gDvmJit.codeCacheSize,
184 PROTECT_CODE_CACHE_ATTRS);
185
186 if (result == -1) {
187 LOGE("Failed to remove the write permission for the code cache");
188 dvmAbort();
189 }
Ben Chengb88ec3c2010-05-17 12:50:33 -0700190
Ben Cheng60c24f42010-01-04 12:29:56 -0800191 return true;
192}
193
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800194static void crawlDalvikStack(Thread *thread, bool print)
195{
196 void *fp = thread->curFrame;
197 StackSaveArea* saveArea = NULL;
198 int stackLevel = 0;
199
200 if (print) {
201 LOGD("Crawling tid %d (%s / %p %s)", thread->systemTid,
202 dvmGetThreadStatusStr(thread->status),
203 thread->inJitCodeCache,
204 thread->inJitCodeCache ? "jit" : "interp");
205 }
206 /* Crawl the Dalvik stack frames to clear the returnAddr field */
207 while (fp != NULL) {
208 saveArea = SAVEAREA_FROM_FP(fp);
209
210 if (print) {
211 if (dvmIsBreakFrame(fp)) {
212 LOGD(" #%d: break frame (%p)",
213 stackLevel, saveArea->returnAddr);
214 }
215 else {
216 LOGD(" #%d: %s.%s%s (%p)",
217 stackLevel,
218 saveArea->method->clazz->descriptor,
219 saveArea->method->name,
220 dvmIsNativeMethod(saveArea->method) ?
221 " (native)" : "",
222 saveArea->returnAddr);
223 }
224 }
225 stackLevel++;
226 saveArea->returnAddr = NULL;
227 assert(fp != saveArea->prevFrame);
228 fp = saveArea->prevFrame;
229 }
230 /* Make sure the stack is fully unwound to the bottom */
231 assert(saveArea == NULL ||
232 (u1 *) (saveArea+1) == thread->interpStackStart);
233}
234
Ben Cheng60c24f42010-01-04 12:29:56 -0800235static void resetCodeCache(void)
236{
Ben Cheng60c24f42010-01-04 12:29:56 -0800237 Thread* thread;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800238 u8 startTime = dvmGetRelativeTimeUsec();
239 int inJit = 0;
Ben Cheng6999d842010-01-26 16:46:15 -0800240 int byteUsed = gDvmJit.codeCacheByteUsed;
Ben Cheng60c24f42010-01-04 12:29:56 -0800241
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800242 /* If any thread is found stuck in the JIT state, don't reset the cache */
243 for (thread = gDvm.threadList; thread != NULL; thread = thread->next) {
Ben Cheng6999d842010-01-26 16:46:15 -0800244 /*
245 * Crawl the stack to wipe out the returnAddr field so that
246 * 1) the soon-to-be-deleted code in the JIT cache won't be used
247 * 2) or the thread stuck in the JIT land will soon return
248 * to the interpreter land
249 */
250 crawlDalvikStack(thread, false);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800251 if (thread->inJitCodeCache) {
252 inJit++;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800253 }
254 }
255
256 if (inJit) {
Ben Cheng6999d842010-01-26 16:46:15 -0800257 LOGD("JIT code cache reset delayed (%d bytes %d/%d)",
258 gDvmJit.codeCacheByteUsed, gDvmJit.numCodeCacheReset,
259 ++gDvmJit.numCodeCacheResetDelayed);
260 return;
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800261 }
262
Ben Cheng6999d842010-01-26 16:46:15 -0800263 /* Lock the mutex to clean up the work queue */
264 dvmLockMutex(&gDvmJit.compilerLock);
265
266 /* Drain the work queue to free the work orders */
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800267 while (workQueueLength()) {
268 CompilerWorkOrder work = workDequeue();
269 free(work.info);
270 }
271
Ben Cheng60c24f42010-01-04 12:29:56 -0800272 /* Reset the JitEntry table contents to the initial unpopulated state */
273 dvmJitResetTable();
274
Ben Chengb88ec3c2010-05-17 12:50:33 -0700275 UNPROTECT_CODE_CACHE(gDvmJit.codeCache, gDvmJit.codeCacheByteUsed);
Ben Cheng60c24f42010-01-04 12:29:56 -0800276 /*
Ben Cheng60c24f42010-01-04 12:29:56 -0800277 * Wipe out the code cache content to force immediate crashes if
278 * stale JIT'ed code is invoked.
279 */
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800280 memset((char *) gDvmJit.codeCache + gDvmJit.templateSize,
281 0,
282 gDvmJit.codeCacheByteUsed - gDvmJit.templateSize);
Ben Cheng60c24f42010-01-04 12:29:56 -0800283 cacheflush((intptr_t) gDvmJit.codeCache,
284 (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed, 0);
Ben Cheng60c24f42010-01-04 12:29:56 -0800285
Ben Chengb88ec3c2010-05-17 12:50:33 -0700286 PROTECT_CODE_CACHE(gDvmJit.codeCache, gDvmJit.codeCacheByteUsed);
287
Ben Cheng60c24f42010-01-04 12:29:56 -0800288 /* Reset the current mark of used bytes to the end of template code */
289 gDvmJit.codeCacheByteUsed = gDvmJit.templateSize;
290 gDvmJit.numCompilations = 0;
291
292 /* Reset the work queue */
293 memset(gDvmJit.compilerWorkQueue, 0,
294 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
295 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
296 gDvmJit.compilerQueueLength = 0;
297
Ben Cheng6999d842010-01-26 16:46:15 -0800298 /* Reset the IC patch work queue */
299 dvmLockMutex(&gDvmJit.compilerICPatchLock);
300 gDvmJit.compilerICPatchIndex = 0;
301 dvmUnlockMutex(&gDvmJit.compilerICPatchLock);
302
Ben Cheng60c24f42010-01-04 12:29:56 -0800303 /* All clear now */
304 gDvmJit.codeCacheFull = false;
305
Ben Cheng6999d842010-01-26 16:46:15 -0800306 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800307
Ben Cheng6999d842010-01-26 16:46:15 -0800308 LOGD("JIT code cache reset in %lld ms (%d bytes %d/%d)",
309 (dvmGetRelativeTimeUsec() - startTime) / 1000,
310 byteUsed, ++gDvmJit.numCodeCacheReset,
311 gDvmJit.numCodeCacheResetDelayed);
312}
313
314/*
315 * Perform actions that are only safe when all threads are suspended. Currently
316 * we do:
317 * 1) Check if the code cache is full. If so reset it and restart populating it
318 * from scratch.
319 * 2) Patch predicted chaining cells by consuming recorded work orders.
320 */
321void dvmCompilerPerformSafePointChecks(void)
322{
323 if (gDvmJit.codeCacheFull) {
324 resetCodeCache();
325 }
326 dvmCompilerPatchInlineCache();
Ben Cheng60c24f42010-01-04 12:29:56 -0800327}
328
Bill Buzbee964a7b02010-01-28 12:54:19 -0800329bool compilerThreadStartup(void)
330{
331 JitEntry *pJitTable = NULL;
332 unsigned char *pJitProfTable = NULL;
333 unsigned int i;
334
335 if (!dvmCompilerArchInit())
336 goto fail;
337
338 /*
339 * Setup the code cache if we have not inherited a valid code cache
340 * from the zygote.
341 */
342 if (gDvmJit.codeCache == NULL) {
343 if (!dvmCompilerSetupCodeCache())
344 goto fail;
345 }
346
347 /* Allocate the initial arena block */
348 if (dvmCompilerHeapInit() == false) {
349 goto fail;
350 }
351
Bill Buzbee964a7b02010-01-28 12:54:19 -0800352 dvmLockMutex(&gDvmJit.compilerLock);
353
Ben Cheng1357e942010-02-10 17:21:39 -0800354#if defined(WITH_JIT_TUNING)
Bill Buzbee964a7b02010-01-28 12:54:19 -0800355 /* Track method-level compilation statistics */
356 gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL);
Ben Cheng452efba2010-04-30 15:14:00 -0700357 gDvm.verboseShutdown = true;
Ben Cheng1357e942010-02-10 17:21:39 -0800358#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800359
360 dvmUnlockMutex(&gDvmJit.compilerLock);
361
362 /* Set up the JitTable */
363
364 /* Power of 2? */
365 assert(gDvmJit.jitTableSize &&
366 !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1)));
367
368 dvmInitMutex(&gDvmJit.tableLock);
369 dvmLockMutex(&gDvmJit.tableLock);
370 pJitTable = (JitEntry*)
371 calloc(gDvmJit.jitTableSize, sizeof(*pJitTable));
372 if (!pJitTable) {
373 LOGE("jit table allocation failed\n");
374 dvmUnlockMutex(&gDvmJit.tableLock);
375 goto fail;
376 }
377 /*
378 * NOTE: the profile table must only be allocated once, globally.
379 * Profiling is turned on and off by nulling out gDvm.pJitProfTable
380 * and then restoring its original value. However, this action
381 * is not syncronized for speed so threads may continue to hold
382 * and update the profile table after profiling has been turned
383 * off by null'ng the global pointer. Be aware.
384 */
385 pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE);
386 if (!pJitProfTable) {
387 LOGE("jit prof table allocation failed\n");
388 dvmUnlockMutex(&gDvmJit.tableLock);
389 goto fail;
390 }
391 memset(pJitProfTable, gDvmJit.threshold, JIT_PROF_SIZE);
392 for (i=0; i < gDvmJit.jitTableSize; i++) {
393 pJitTable[i].u.info.chain = gDvmJit.jitTableSize;
394 }
395 /* Is chain field wide enough for termination pattern? */
396 assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize);
397
398 gDvmJit.pJitEntryTable = pJitTable;
399 gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1;
400 gDvmJit.jitTableEntriesUsed = 0;
401 gDvmJit.compilerHighWater =
402 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
Ben Chenga4973592010-03-31 11:59:18 -0700403 /*
404 * If the VM is launched with wait-on-the-debugger, we will need to hide
405 * the profile table here
406 */
407 gDvmJit.pProfTable = dvmDebuggerOrProfilerActive() ? NULL : pJitProfTable;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800408 gDvmJit.pProfTableCopy = pJitProfTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800409 dvmUnlockMutex(&gDvmJit.tableLock);
410
411 /* Signal running threads to refresh their cached pJitTable pointers */
412 dvmSuspendAllThreads(SUSPEND_FOR_REFRESH);
413 dvmResumeAllThreads(SUSPEND_FOR_REFRESH);
Ben Chengdca71432010-03-16 16:04:11 -0700414
415 /* Enable signature breakpoints by customizing the following code */
416#if defined(SIGNATURE_BREAKPOINT)
417 /*
418 * Suppose one sees the following native crash in the bugreport:
419 * I/DEBUG ( 1638): Build fingerprint: 'unknown'
420 * I/DEBUG ( 1638): pid: 2468, tid: 2507 >>> com.google.android.gallery3d
421 * I/DEBUG ( 1638): signal 11 (SIGSEGV), fault addr 00001400
422 * I/DEBUG ( 1638): r0 44ea7190 r1 44e4f7b8 r2 44ebc710 r3 00000000
423 * I/DEBUG ( 1638): r4 00000a00 r5 41862dec r6 4710dc10 r7 00000280
424 * I/DEBUG ( 1638): r8 ad010f40 r9 46a37a12 10 001116b0 fp 42a78208
425 * I/DEBUG ( 1638): ip 00000090 sp 4710dbc8 lr ad060e67 pc 46b90682
426 * cpsr 00000030
427 * I/DEBUG ( 1638): #00 pc 46b90682 /dev/ashmem/dalvik-jit-code-cache
428 * I/DEBUG ( 1638): #01 pc 00060e62 /system/lib/libdvm.so
429 *
430 * I/DEBUG ( 1638): code around pc:
431 * I/DEBUG ( 1638): 46b90660 6888d01c 34091dcc d2174287 4a186b68
432 * I/DEBUG ( 1638): 46b90670 d0052800 68006809 28004790 6b68d00e
433 * I/DEBUG ( 1638): 46b90680 512000bc 37016eaf 6ea866af 6f696028
434 * I/DEBUG ( 1638): 46b90690 682a6069 429a686b e003da08 6df1480b
435 * I/DEBUG ( 1638): 46b906a0 1c2d4788 47806d70 46a378fa 47806d70
436 *
437 * Clearly it is a JIT bug. To find out which translation contains the
438 * offending code, the content of the memory dump around the faulting PC
439 * can be pasted into the gDvmJit.signatureBreakpoint[] array and next time
440 * when a similar compilation is being created, the JIT compiler replay the
441 * trace in the verbose mode and one can investigate the instruction
442 * sequence in details.
443 *
444 * The length of the signature may need additional experiments to determine.
445 * The rule of thumb is don't include PC-relative instructions in the
446 * signature since it may be affected by the alignment of the compiled code.
447 * However, a signature that's too short might increase the chance of false
448 * positive matches. Using gdbjithelper to disassembly the memory content
449 * first might be a good companion approach.
450 *
451 * For example, if the next 4 words starting from 46b90680 is pasted into
452 * the data structure:
453 */
454
455 gDvmJit.signatureBreakpointSize = 4;
456 gDvmJit.signatureBreakpoint =
457 malloc(sizeof(u4) * gDvmJit.signatureBreakpointSize);
458 gDvmJit.signatureBreakpoint[0] = 0x512000bc;
459 gDvmJit.signatureBreakpoint[1] = 0x37016eaf;
460 gDvmJit.signatureBreakpoint[2] = 0x6ea866af;
461 gDvmJit.signatureBreakpoint[3] = 0x6f696028;
462
463 /*
464 * The following log will be printed when a match is found in subsequent
465 * testings:
466 *
467 * D/dalvikvm( 2468): Signature match starting from offset 0x34 (4 words)
468 * D/dalvikvm( 2468): --------
469 * D/dalvikvm( 2468): Compiler: Building trace for computeVisibleItems,
470 * offset 0x1f7
471 * D/dalvikvm( 2468): 0x46a37a12: 0x0090 add-int v42, v5, v26
472 * D/dalvikvm( 2468): 0x46a37a16: 0x004d aput-object v13, v14, v42
473 * D/dalvikvm( 2468): 0x46a37a1a: 0x0028 goto, (#0), (#0)
474 * D/dalvikvm( 2468): 0x46a3794e: 0x00d8 add-int/lit8 v26, v26, (#1)
475 * D/dalvikvm( 2468): 0x46a37952: 0x0028 goto, (#0), (#0)
476 * D/dalvikvm( 2468): 0x46a378ee: 0x0002 move/from16 v0, v26, (#0)
477 * D/dalvikvm( 2468): 0x46a378f2: 0x0002 move/from16 v1, v29, (#0)
478 * D/dalvikvm( 2468): 0x46a378f6: 0x0035 if-ge v0, v1, (#10)
479 * D/dalvikvm( 2468): TRACEINFO (554): 0x46a37624
480 * Lcom/cooliris/media/GridLayer;computeVisibleItems 0x1f7 14 of 934, 8
481 * blocks
482 * :
483 * :
484 * D/dalvikvm( 2468): 0x20 (0020): ldr r0, [r5, #52]
485 * D/dalvikvm( 2468): 0x22 (0022): ldr r2, [pc, #96]
486 * D/dalvikvm( 2468): 0x24 (0024): cmp r0, #0
487 * D/dalvikvm( 2468): 0x26 (0026): beq 0x00000034
488 * D/dalvikvm( 2468): 0x28 (0028): ldr r1, [r1, #0]
489 * D/dalvikvm( 2468): 0x2a (002a): ldr r0, [r0, #0]
490 * D/dalvikvm( 2468): 0x2c (002c): blx r2
491 * D/dalvikvm( 2468): 0x2e (002e): cmp r0, #0
492 * D/dalvikvm( 2468): 0x30 (0030): beq 0x00000050
493 * D/dalvikvm( 2468): 0x32 (0032): ldr r0, [r5, #52]
494 * D/dalvikvm( 2468): 0x34 (0034): lsls r4, r7, #2
495 * D/dalvikvm( 2468): 0x36 (0036): str r0, [r4, r4]
496 * D/dalvikvm( 2468): -------- dalvik offset: 0x01fb @ goto, (#0), (#0)
497 * D/dalvikvm( 2468): L0x0195:
498 * D/dalvikvm( 2468): -------- dalvik offset: 0x0195 @ add-int/lit8 v26,
499 * v26, (#1)
500 * D/dalvikvm( 2468): 0x38 (0038): ldr r7, [r5, #104]
501 * D/dalvikvm( 2468): 0x3a (003a): adds r7, r7, #1
502 * D/dalvikvm( 2468): 0x3c (003c): str r7, [r5, #104]
503 * D/dalvikvm( 2468): -------- dalvik offset: 0x0197 @ goto, (#0), (#0)
504 * D/dalvikvm( 2468): L0x0165:
505 * D/dalvikvm( 2468): -------- dalvik offset: 0x0165 @ move/from16 v0, v26,
506 * (#0)
507 * D/dalvikvm( 2468): 0x3e (003e): ldr r0, [r5, #104]
508 * D/dalvikvm( 2468): 0x40 (0040): str r0, [r5, #0]
509 *
510 * The "str r0, [r4, r4]" is indeed the culprit of the native crash.
511 */
512#endif
513
Bill Buzbee964a7b02010-01-28 12:54:19 -0800514 return true;
515
516fail:
517 return false;
518
519}
520
Ben Chengba4fc8b2009-06-01 13:00:29 -0700521static void *compilerThreadStart(void *arg)
522{
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700523 dvmChangeStatus(NULL, THREAD_VMWAIT);
524
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800525 /*
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800526 * If we're not running stand-alone, wait a little before
527 * recieving translation requests on the assumption that process start
528 * up code isn't worth compiling. We'll resume when the framework
529 * signals us that the first screen draw has happened, or the timer
530 * below expires (to catch daemons).
Ben Chengf30acbb2010-02-14 16:17:36 -0800531 *
532 * There is a theoretical race between the callback to
533 * VMRuntime.startJitCompiation and when the compiler thread reaches this
534 * point. In case the callback happens earlier, in order not to permanently
535 * hold the system_server (which is not using the timed wait) in
536 * interpreter-only mode we bypass the delay here.
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800537 */
Ben Chengf30acbb2010-02-14 16:17:36 -0800538 if (gDvmJit.runningInAndroidFramework &&
539 !gDvmJit.alreadyEnabledViaFramework) {
540 /*
541 * If the current VM instance is the system server (detected by having
542 * 0 in gDvm.systemServerPid), we will use the indefinite wait on the
543 * conditional variable to determine whether to start the JIT or not.
544 * If the system server detects that the whole system is booted in
545 * safe mode, the conditional variable will never be signaled and the
546 * system server will remain in the interpreter-only mode. All
547 * subsequent apps will be started with the --enable-safemode flag
548 * explicitly appended.
549 */
550 if (gDvm.systemServerPid == 0) {
551 dvmLockMutex(&gDvmJit.compilerLock);
552 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
553 &gDvmJit.compilerLock);
554 dvmUnlockMutex(&gDvmJit.compilerLock);
555 LOGD("JIT started for system_server");
556 } else {
557 dvmLockMutex(&gDvmJit.compilerLock);
558 /*
559 * TUNING: experiment with the delay & perhaps make it
560 * target-specific
561 */
562 dvmRelativeCondWait(&gDvmJit.compilerQueueActivity,
563 &gDvmJit.compilerLock, 3000, 0);
564 dvmUnlockMutex(&gDvmJit.compilerLock);
565 }
Bill Buzbeeeb695c62010-02-04 16:09:55 -0800566 if (gDvmJit.haltCompilerThread) {
567 return NULL;
568 }
Bill Buzbee94d89f82010-01-29 13:44:19 -0800569 }
570
Bill Buzbee964a7b02010-01-28 12:54:19 -0800571 compilerThreadStartup();
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800572
Ben Chengba4fc8b2009-06-01 13:00:29 -0700573 dvmLockMutex(&gDvmJit.compilerLock);
574 /*
575 * Since the compiler thread will not touch any objects on the heap once
576 * being created, we just fake its state as VMWAIT so that it can be a
577 * bit late when there is suspend request pending.
578 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700579 while (!gDvmJit.haltCompilerThread) {
580 if (workQueueLength() == 0) {
581 int cc;
582 cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
583 assert(cc == 0);
584 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
585 &gDvmJit.compilerLock);
586 continue;
587 } else {
588 do {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700589 CompilerWorkOrder work = workDequeue();
590 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Cheng978738d2010-05-13 13:45:57 -0700591#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -0800592 u8 startTime = dvmGetRelativeTimeUsec();
593#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800594 /*
595 * Check whether there is a suspend request on me. This
596 * is necessary to allow a clean shutdown.
Ben Cheng11d8f142010-03-24 15:24:19 -0700597 *
598 * However, in the blocking stress testing mode, let the
599 * compiler thread continue doing compilations to unblock
600 * other requesting threads. This may occasionally cause
601 * shutdown from proceeding cleanly in the standalone invocation
602 * of the vm but this should be acceptable.
Bill Buzbee964a7b02010-01-28 12:54:19 -0800603 */
Ben Cheng11d8f142010-03-24 15:24:19 -0700604 if (!gDvmJit.blockingMode)
Andy McFaddenab227f72010-04-06 12:37:48 -0700605 dvmCheckSuspendPending(dvmThreadSelf());
Bill Buzbee27176222009-06-09 09:20:16 -0700606 /* Is JitTable filling up? */
607 if (gDvmJit.jitTableEntriesUsed >
608 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
Ben Cheng6999d842010-01-26 16:46:15 -0800609 bool resizeFail =
610 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
611 /*
612 * If the jit table is full, consider it's time to reset
613 * the code cache too.
614 */
615 gDvmJit.codeCacheFull |= resizeFail;
Bill Buzbee27176222009-06-09 09:20:16 -0700616 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617 if (gDvmJit.haltCompilerThread) {
618 LOGD("Compiler shutdown in progress - discarding request");
Ben Cheng6999d842010-01-26 16:46:15 -0800619 } else if (!gDvmJit.codeCacheFull) {
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800620 bool compileOK = false;
621 jmp_buf jmpBuf;
622 work.bailPtr = &jmpBuf;
623 bool aborted = setjmp(jmpBuf);
624 if (!aborted) {
625 compileOK = dvmCompilerDoWork(&work);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700626 }
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800627 if (aborted || !compileOK) {
628 dvmCompilerArenaReset();
Bill Buzbeebd047242010-05-13 13:02:53 -0700629 work.result.codeAddress = dvmCompilerGetInterpretTemplate();
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800630 } else if (!work.result.discardResult) {
Ben Cheng60c24f42010-01-04 12:29:56 -0800631 dvmJitSetCodeAddr(work.pc, work.result.codeAddress,
632 work.result.instructionSet);
633 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700634 }
635 free(work.info);
Ben Cheng978738d2010-05-13 13:45:57 -0700636#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -0800637 gDvmJit.jitTime += dvmGetRelativeTimeUsec() - startTime;
638#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700639 dvmLockMutex(&gDvmJit.compilerLock);
640 } while (workQueueLength() != 0);
641 }
642 }
643 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
644 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Chengef00a852009-06-22 22:53:35 -0700645
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700646 /*
647 * As part of detaching the thread we need to call into Java code to update
648 * the ThreadGroup, and we should not be in VMWAIT state while executing
649 * interpreted code.
650 */
651 dvmChangeStatus(NULL, THREAD_RUNNING);
652
Andy McFadden43eb5012010-02-01 16:56:53 -0800653 if (gDvm.verboseShutdown)
654 LOGD("Compiler thread shutting down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700655 return NULL;
656}
657
Ben Chengba4fc8b2009-06-01 13:00:29 -0700658bool dvmCompilerStartup(void)
659{
Bill Buzbee94d89f82010-01-29 13:44:19 -0800660
661 dvmInitMutex(&gDvmJit.compilerLock);
Ben Cheng6999d842010-01-26 16:46:15 -0800662 dvmInitMutex(&gDvmJit.compilerICPatchLock);
Ben Chengb88ec3c2010-05-17 12:50:33 -0700663 dvmInitMutex(&gDvmJit.codeCacheProtectionLock);
Bill Buzbee94d89f82010-01-29 13:44:19 -0800664 dvmLockMutex(&gDvmJit.compilerLock);
665 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
666 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
667
668 /* Reset the work queue */
669 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
670 gDvmJit.compilerQueueLength = 0;
671 dvmUnlockMutex(&gDvmJit.compilerLock);
672
Ben Chengba4fc8b2009-06-01 13:00:29 -0700673 /*
Bill Buzbee94d89f82010-01-29 13:44:19 -0800674 * Defer rest of initialization until we're sure JIT'ng makes sense. Launch
Bill Buzbee964a7b02010-01-28 12:54:19 -0800675 * the compiler thread, which will do the real initialization if and
676 * when it is signalled to do so.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700677 */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800678 return dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
679 compilerThreadStart, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680}
681
682void dvmCompilerShutdown(void)
683{
684 void *threadReturn;
685
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800686 /* Disable new translation requests */
687 gDvmJit.pProfTable = NULL;
688 gDvmJit.pProfTableCopy = NULL;
689
Ben Cheng88a0f972010-02-24 15:00:40 -0800690 if (gDvm.verboseShutdown) {
691 dvmCompilerDumpStats();
692 while (gDvmJit.compilerQueueLength)
693 sleep(5);
694 }
695
Ben Chengba4fc8b2009-06-01 13:00:29 -0700696 if (gDvmJit.compilerHandle) {
697
698 gDvmJit.haltCompilerThread = true;
699
700 dvmLockMutex(&gDvmJit.compilerLock);
701 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
702 dvmUnlockMutex(&gDvmJit.compilerLock);
703
Ben Chengef00a852009-06-22 22:53:35 -0700704 if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0)
705 LOGW("Compiler thread join failed\n");
Andy McFadden43eb5012010-02-01 16:56:53 -0800706 else if (gDvm.verboseShutdown)
Ben Chengef00a852009-06-22 22:53:35 -0700707 LOGD("Compiler thread has shut down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700708 }
Bill Buzbee06bb8392010-01-31 18:53:15 -0800709
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800710 /* Break loops within the translation cache */
711 dvmJitUnchainAll();
Bill Buzbee96cfe6c2010-02-08 17:08:15 -0800712
Bill Buzbee2fc03c32010-03-08 11:30:19 -0800713 /*
714 * NOTE: our current implementatation doesn't allow for the compiler
715 * thread to be restarted after it exits here. We aren't freeing
716 * the JitTable or the ProfTable because threads which still may be
717 * running or in the process of shutting down may hold references to
718 * them.
719 */
Bill Buzbee96cfe6c2010-02-08 17:08:15 -0800720}
Bill Buzbee06bb8392010-01-31 18:53:15 -0800721
722void dvmCompilerStateRefresh()
723{
724 bool jitActive;
725 bool jitActivate;
Bill Buzbee3e392682010-02-03 18:13:57 -0800726 bool needUnchain = false;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800727
Ben Chenga4973592010-03-31 11:59:18 -0700728 /*
729 * The tableLock might not be initialized yet by the compiler thread if
730 * debugger is attached from the very beginning of the VM launch. If
731 * pProfTableCopy is NULL, the lock is not initialized yet and we don't
732 * need to refresh anything either.
733 */
734 if (gDvmJit.pProfTableCopy == NULL) {
735 return;
736 }
737
Bill Buzbee06bb8392010-01-31 18:53:15 -0800738 dvmLockMutex(&gDvmJit.tableLock);
739 jitActive = gDvmJit.pProfTable != NULL;
Andy McFaddenc95e0fb2010-04-29 14:13:01 -0700740
741#if defined(WITH_DEBUGGER) && defined(WITH_PROFILER)
Bill Buzbee06bb8392010-01-31 18:53:15 -0800742 jitActivate = !(gDvm.debuggerActive || (gDvm.activeProfilers > 0));
Andy McFaddenc95e0fb2010-04-29 14:13:01 -0700743#elif defined(WITH_DEBUGGER)
744 jitActivate = !gDvm.debuggerActive;
745#elif defined(WITH_PROFILER)
746 jitActivate = !(gDvm.activeProfilers > 0);
747#else
748 jitActivate = true;
749#endif
Bill Buzbee06bb8392010-01-31 18:53:15 -0800750
751 if (jitActivate && !jitActive) {
752 gDvmJit.pProfTable = gDvmJit.pProfTableCopy;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800753 } else if (!jitActivate && jitActive) {
754 gDvmJit.pProfTable = NULL;
Bill Buzbee3e392682010-02-03 18:13:57 -0800755 needUnchain = true;
Bill Buzbee06bb8392010-01-31 18:53:15 -0800756 }
Bill Buzbee3e392682010-02-03 18:13:57 -0800757 dvmUnlockMutex(&gDvmJit.tableLock);
758 if (needUnchain)
759 dvmJitUnchainAll();
Bill Buzbee06bb8392010-01-31 18:53:15 -0800760}