| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 19 | #include <cutils/ashmem.h> |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 20 | |
| 21 | #include "Dalvik.h" |
| 22 | #include "interp/Jit.h" |
| 23 | #include "CompilerInternals.h" |
| 24 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 25 | static inline bool workQueueLength(void) |
| 26 | { |
| 27 | return gDvmJit.compilerQueueLength; |
| 28 | } |
| 29 | |
| 30 | static 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 Buzbee | f9f3328 | 2009-11-22 12:45:30 -0800 | [diff] [blame] | 42 | if (gDvmJit.compilerQueueLength == 0) { |
| 43 | int cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 44 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 45 | |
| 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 Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 53 | /* |
| 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 Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 57 | * |
| 58 | * NOTE: Make sure that the caller frees the info pointer if the return value |
| 59 | * is false. |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 60 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 61 | bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info) |
| 62 | { |
| 63 | int cc; |
| 64 | int i; |
| 65 | int numWork; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 66 | bool result = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 67 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 68 | if (dvmTryLockMutex(&gDvmJit.compilerLock)) { |
| Ben Cheng | 3e5cd17 | 2010-02-08 20:57:59 -0800 | [diff] [blame] | 69 | return false; // Couldn't acquire the lock |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 70 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 71 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 72 | /* |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 73 | * Return if queue or code cache is full. |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 74 | */ |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 75 | if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE || |
| 76 | gDvmJit.codeCacheFull == true) { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 77 | result = false; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 78 | goto unlockAndExit; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 79 | } |
| 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 Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 87 | goto unlockAndExit; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 88 | /* Wrap around */ |
| 89 | if (i == COMPILER_WORK_QUEUE_SIZE) |
| 90 | i = 0; |
| 91 | } |
| 92 | |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 93 | 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 Buzbee | 1f74863 | 2010-03-02 16:14:41 -0800 | [diff] [blame] | 100 | (kind == kWorkOrderTraceDebug) ? true : false; |
| Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 101 | newOrder->result.requestingThread = dvmThreadSelf(); |
| 102 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 103 | 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 Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 110 | unlockAndExit: |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 111 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 112 | return result; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Block until queue length is 0 */ |
| 116 | void dvmCompilerDrainQueue(void) |
| 117 | { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 118 | int oldStatus = dvmChangeStatus(NULL, THREAD_VMWAIT); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 119 | dvmLockMutex(&gDvmJit.compilerLock); |
| 120 | while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread) { |
| Ben Cheng | 812e6b1 | 2010-03-15 15:19:06 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Use timed wait here - more than one mutator threads may be blocked |
| 123 | * but the compiler thread will only signal once when the queue is |
| 124 | * emptied. Furthermore, the compiler thread may have been shutdown |
| 125 | * so the blocked thread may never get the wakeup signal. |
| 126 | */ |
| 127 | dvmRelativeCondWait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock, 1000, 0); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 128 | } |
| 129 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 130 | dvmChangeStatus(NULL, oldStatus); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 133 | bool dvmCompilerSetupCodeCache(void) |
| 134 | { |
| 135 | extern void dvmCompilerTemplateStart(void); |
| 136 | extern void dmvCompilerTemplateEnd(void); |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 137 | int fd; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 138 | |
| 139 | /* Allocate the code cache */ |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 140 | fd = ashmem_create_region("dalvik-jit-code-cache", gDvmJit.codeCacheSize); |
| 141 | if (fd < 0) { |
| 142 | LOGE("Could not create %u-byte ashmem region for the JIT code cache", |
| 143 | gDvmJit.codeCacheSize); |
| 144 | return false; |
| 145 | } |
| 146 | gDvmJit.codeCache = mmap(NULL, gDvmJit.codeCacheSize, |
| 147 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 148 | MAP_PRIVATE , fd, 0); |
| 149 | close(fd); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 150 | if (gDvmJit.codeCache == MAP_FAILED) { |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 151 | LOGE("Failed to mmap the JIT code cache: %s\n", strerror(errno)); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 155 | /* This can be found through "dalvik-jit-code-cache" in /proc/<pid>/maps */ |
| 156 | // LOGD("Code cache starts at %p", gDvmJit.codeCache); |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 157 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 158 | /* Copy the template code into the beginning of the code cache */ |
| 159 | int templateSize = (intptr_t) dmvCompilerTemplateEnd - |
| 160 | (intptr_t) dvmCompilerTemplateStart; |
| 161 | memcpy((void *) gDvmJit.codeCache, |
| 162 | (void *) dvmCompilerTemplateStart, |
| 163 | templateSize); |
| 164 | |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Work around a CPU bug by keeping the 32-bit ARM handler code in its own |
| 167 | * page. |
| 168 | */ |
| 169 | if (dvmCompilerInstructionSet() == DALVIK_JIT_THUMB2) { |
| 170 | templateSize = (templateSize + 4095) & ~4095; |
| 171 | } |
| 172 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 173 | gDvmJit.templateSize = templateSize; |
| 174 | gDvmJit.codeCacheByteUsed = templateSize; |
| 175 | |
| 176 | /* Only flush the part in the code cache that is being used now */ |
| 177 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 178 | (intptr_t) gDvmJit.codeCache + templateSize, 0); |
| 179 | return true; |
| 180 | } |
| 181 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 182 | static void crawlDalvikStack(Thread *thread, bool print) |
| 183 | { |
| 184 | void *fp = thread->curFrame; |
| 185 | StackSaveArea* saveArea = NULL; |
| 186 | int stackLevel = 0; |
| 187 | |
| 188 | if (print) { |
| 189 | LOGD("Crawling tid %d (%s / %p %s)", thread->systemTid, |
| 190 | dvmGetThreadStatusStr(thread->status), |
| 191 | thread->inJitCodeCache, |
| 192 | thread->inJitCodeCache ? "jit" : "interp"); |
| 193 | } |
| 194 | /* Crawl the Dalvik stack frames to clear the returnAddr field */ |
| 195 | while (fp != NULL) { |
| 196 | saveArea = SAVEAREA_FROM_FP(fp); |
| 197 | |
| 198 | if (print) { |
| 199 | if (dvmIsBreakFrame(fp)) { |
| 200 | LOGD(" #%d: break frame (%p)", |
| 201 | stackLevel, saveArea->returnAddr); |
| 202 | } |
| 203 | else { |
| 204 | LOGD(" #%d: %s.%s%s (%p)", |
| 205 | stackLevel, |
| 206 | saveArea->method->clazz->descriptor, |
| 207 | saveArea->method->name, |
| 208 | dvmIsNativeMethod(saveArea->method) ? |
| 209 | " (native)" : "", |
| 210 | saveArea->returnAddr); |
| 211 | } |
| 212 | } |
| 213 | stackLevel++; |
| 214 | saveArea->returnAddr = NULL; |
| 215 | assert(fp != saveArea->prevFrame); |
| 216 | fp = saveArea->prevFrame; |
| 217 | } |
| 218 | /* Make sure the stack is fully unwound to the bottom */ |
| 219 | assert(saveArea == NULL || |
| 220 | (u1 *) (saveArea+1) == thread->interpStackStart); |
| 221 | } |
| 222 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 223 | static void resetCodeCache(void) |
| 224 | { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 225 | Thread* thread; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 226 | u8 startTime = dvmGetRelativeTimeUsec(); |
| 227 | int inJit = 0; |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 228 | int byteUsed = gDvmJit.codeCacheByteUsed; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 229 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 230 | /* If any thread is found stuck in the JIT state, don't reset the cache */ |
| 231 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 232 | /* |
| 233 | * Crawl the stack to wipe out the returnAddr field so that |
| 234 | * 1) the soon-to-be-deleted code in the JIT cache won't be used |
| 235 | * 2) or the thread stuck in the JIT land will soon return |
| 236 | * to the interpreter land |
| 237 | */ |
| 238 | crawlDalvikStack(thread, false); |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 239 | if (thread->inJitCodeCache) { |
| 240 | inJit++; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | if (inJit) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 245 | LOGD("JIT code cache reset delayed (%d bytes %d/%d)", |
| 246 | gDvmJit.codeCacheByteUsed, gDvmJit.numCodeCacheReset, |
| 247 | ++gDvmJit.numCodeCacheResetDelayed); |
| 248 | return; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 251 | /* Lock the mutex to clean up the work queue */ |
| 252 | dvmLockMutex(&gDvmJit.compilerLock); |
| 253 | |
| 254 | /* Drain the work queue to free the work orders */ |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 255 | while (workQueueLength()) { |
| 256 | CompilerWorkOrder work = workDequeue(); |
| 257 | free(work.info); |
| 258 | } |
| 259 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 260 | /* Reset the JitEntry table contents to the initial unpopulated state */ |
| 261 | dvmJitResetTable(); |
| 262 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 263 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 264 | * Wipe out the code cache content to force immediate crashes if |
| 265 | * stale JIT'ed code is invoked. |
| 266 | */ |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 267 | memset((char *) gDvmJit.codeCache + gDvmJit.templateSize, |
| 268 | 0, |
| 269 | gDvmJit.codeCacheByteUsed - gDvmJit.templateSize); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 270 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 271 | (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed, 0); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 272 | |
| 273 | /* Reset the current mark of used bytes to the end of template code */ |
| 274 | gDvmJit.codeCacheByteUsed = gDvmJit.templateSize; |
| 275 | gDvmJit.numCompilations = 0; |
| 276 | |
| 277 | /* Reset the work queue */ |
| 278 | memset(gDvmJit.compilerWorkQueue, 0, |
| 279 | sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE); |
| 280 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 281 | gDvmJit.compilerQueueLength = 0; |
| 282 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 283 | /* Reset the IC patch work queue */ |
| 284 | dvmLockMutex(&gDvmJit.compilerICPatchLock); |
| 285 | gDvmJit.compilerICPatchIndex = 0; |
| 286 | dvmUnlockMutex(&gDvmJit.compilerICPatchLock); |
| 287 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 288 | /* All clear now */ |
| 289 | gDvmJit.codeCacheFull = false; |
| 290 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 291 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 292 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 293 | LOGD("JIT code cache reset in %lld ms (%d bytes %d/%d)", |
| 294 | (dvmGetRelativeTimeUsec() - startTime) / 1000, |
| 295 | byteUsed, ++gDvmJit.numCodeCacheReset, |
| 296 | gDvmJit.numCodeCacheResetDelayed); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Perform actions that are only safe when all threads are suspended. Currently |
| 301 | * we do: |
| 302 | * 1) Check if the code cache is full. If so reset it and restart populating it |
| 303 | * from scratch. |
| 304 | * 2) Patch predicted chaining cells by consuming recorded work orders. |
| 305 | */ |
| 306 | void dvmCompilerPerformSafePointChecks(void) |
| 307 | { |
| 308 | if (gDvmJit.codeCacheFull) { |
| 309 | resetCodeCache(); |
| 310 | } |
| 311 | dvmCompilerPatchInlineCache(); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 314 | bool compilerThreadStartup(void) |
| 315 | { |
| 316 | JitEntry *pJitTable = NULL; |
| 317 | unsigned char *pJitProfTable = NULL; |
| 318 | unsigned int i; |
| 319 | |
| 320 | if (!dvmCompilerArchInit()) |
| 321 | goto fail; |
| 322 | |
| 323 | /* |
| 324 | * Setup the code cache if we have not inherited a valid code cache |
| 325 | * from the zygote. |
| 326 | */ |
| 327 | if (gDvmJit.codeCache == NULL) { |
| 328 | if (!dvmCompilerSetupCodeCache()) |
| 329 | goto fail; |
| 330 | } |
| 331 | |
| 332 | /* Allocate the initial arena block */ |
| 333 | if (dvmCompilerHeapInit() == false) { |
| 334 | goto fail; |
| 335 | } |
| 336 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 337 | dvmLockMutex(&gDvmJit.compilerLock); |
| 338 | |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 339 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 340 | /* Track method-level compilation statistics */ |
| 341 | gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL); |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 342 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 343 | |
| 344 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 345 | |
| 346 | /* Set up the JitTable */ |
| 347 | |
| 348 | /* Power of 2? */ |
| 349 | assert(gDvmJit.jitTableSize && |
| 350 | !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1))); |
| 351 | |
| 352 | dvmInitMutex(&gDvmJit.tableLock); |
| 353 | dvmLockMutex(&gDvmJit.tableLock); |
| 354 | pJitTable = (JitEntry*) |
| 355 | calloc(gDvmJit.jitTableSize, sizeof(*pJitTable)); |
| 356 | if (!pJitTable) { |
| 357 | LOGE("jit table allocation failed\n"); |
| 358 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 359 | goto fail; |
| 360 | } |
| 361 | /* |
| 362 | * NOTE: the profile table must only be allocated once, globally. |
| 363 | * Profiling is turned on and off by nulling out gDvm.pJitProfTable |
| 364 | * and then restoring its original value. However, this action |
| 365 | * is not syncronized for speed so threads may continue to hold |
| 366 | * and update the profile table after profiling has been turned |
| 367 | * off by null'ng the global pointer. Be aware. |
| 368 | */ |
| 369 | pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE); |
| 370 | if (!pJitProfTable) { |
| 371 | LOGE("jit prof table allocation failed\n"); |
| 372 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 373 | goto fail; |
| 374 | } |
| 375 | memset(pJitProfTable, gDvmJit.threshold, JIT_PROF_SIZE); |
| 376 | for (i=0; i < gDvmJit.jitTableSize; i++) { |
| 377 | pJitTable[i].u.info.chain = gDvmJit.jitTableSize; |
| 378 | } |
| 379 | /* Is chain field wide enough for termination pattern? */ |
| 380 | assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize); |
| 381 | |
| 382 | gDvmJit.pJitEntryTable = pJitTable; |
| 383 | gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1; |
| 384 | gDvmJit.jitTableEntriesUsed = 0; |
| 385 | gDvmJit.compilerHighWater = |
| 386 | COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4); |
| 387 | gDvmJit.pProfTable = pJitProfTable; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 388 | gDvmJit.pProfTableCopy = pJitProfTable; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 389 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 390 | |
| 391 | /* Signal running threads to refresh their cached pJitTable pointers */ |
| 392 | dvmSuspendAllThreads(SUSPEND_FOR_REFRESH); |
| 393 | dvmResumeAllThreads(SUSPEND_FOR_REFRESH); |
| Ben Cheng | dca7143 | 2010-03-16 16:04:11 -0700 | [diff] [blame^] | 394 | |
| 395 | /* Enable signature breakpoints by customizing the following code */ |
| 396 | #if defined(SIGNATURE_BREAKPOINT) |
| 397 | /* |
| 398 | * Suppose one sees the following native crash in the bugreport: |
| 399 | * I/DEBUG ( 1638): Build fingerprint: 'unknown' |
| 400 | * I/DEBUG ( 1638): pid: 2468, tid: 2507 >>> com.google.android.gallery3d |
| 401 | * I/DEBUG ( 1638): signal 11 (SIGSEGV), fault addr 00001400 |
| 402 | * I/DEBUG ( 1638): r0 44ea7190 r1 44e4f7b8 r2 44ebc710 r3 00000000 |
| 403 | * I/DEBUG ( 1638): r4 00000a00 r5 41862dec r6 4710dc10 r7 00000280 |
| 404 | * I/DEBUG ( 1638): r8 ad010f40 r9 46a37a12 10 001116b0 fp 42a78208 |
| 405 | * I/DEBUG ( 1638): ip 00000090 sp 4710dbc8 lr ad060e67 pc 46b90682 |
| 406 | * cpsr 00000030 |
| 407 | * I/DEBUG ( 1638): #00 pc 46b90682 /dev/ashmem/dalvik-jit-code-cache |
| 408 | * I/DEBUG ( 1638): #01 pc 00060e62 /system/lib/libdvm.so |
| 409 | * |
| 410 | * I/DEBUG ( 1638): code around pc: |
| 411 | * I/DEBUG ( 1638): 46b90660 6888d01c 34091dcc d2174287 4a186b68 |
| 412 | * I/DEBUG ( 1638): 46b90670 d0052800 68006809 28004790 6b68d00e |
| 413 | * I/DEBUG ( 1638): 46b90680 512000bc 37016eaf 6ea866af 6f696028 |
| 414 | * I/DEBUG ( 1638): 46b90690 682a6069 429a686b e003da08 6df1480b |
| 415 | * I/DEBUG ( 1638): 46b906a0 1c2d4788 47806d70 46a378fa 47806d70 |
| 416 | * |
| 417 | * Clearly it is a JIT bug. To find out which translation contains the |
| 418 | * offending code, the content of the memory dump around the faulting PC |
| 419 | * can be pasted into the gDvmJit.signatureBreakpoint[] array and next time |
| 420 | * when a similar compilation is being created, the JIT compiler replay the |
| 421 | * trace in the verbose mode and one can investigate the instruction |
| 422 | * sequence in details. |
| 423 | * |
| 424 | * The length of the signature may need additional experiments to determine. |
| 425 | * The rule of thumb is don't include PC-relative instructions in the |
| 426 | * signature since it may be affected by the alignment of the compiled code. |
| 427 | * However, a signature that's too short might increase the chance of false |
| 428 | * positive matches. Using gdbjithelper to disassembly the memory content |
| 429 | * first might be a good companion approach. |
| 430 | * |
| 431 | * For example, if the next 4 words starting from 46b90680 is pasted into |
| 432 | * the data structure: |
| 433 | */ |
| 434 | |
| 435 | gDvmJit.signatureBreakpointSize = 4; |
| 436 | gDvmJit.signatureBreakpoint = |
| 437 | malloc(sizeof(u4) * gDvmJit.signatureBreakpointSize); |
| 438 | gDvmJit.signatureBreakpoint[0] = 0x512000bc; |
| 439 | gDvmJit.signatureBreakpoint[1] = 0x37016eaf; |
| 440 | gDvmJit.signatureBreakpoint[2] = 0x6ea866af; |
| 441 | gDvmJit.signatureBreakpoint[3] = 0x6f696028; |
| 442 | |
| 443 | /* |
| 444 | * The following log will be printed when a match is found in subsequent |
| 445 | * testings: |
| 446 | * |
| 447 | * D/dalvikvm( 2468): Signature match starting from offset 0x34 (4 words) |
| 448 | * D/dalvikvm( 2468): -------- |
| 449 | * D/dalvikvm( 2468): Compiler: Building trace for computeVisibleItems, |
| 450 | * offset 0x1f7 |
| 451 | * D/dalvikvm( 2468): 0x46a37a12: 0x0090 add-int v42, v5, v26 |
| 452 | * D/dalvikvm( 2468): 0x46a37a16: 0x004d aput-object v13, v14, v42 |
| 453 | * D/dalvikvm( 2468): 0x46a37a1a: 0x0028 goto, (#0), (#0) |
| 454 | * D/dalvikvm( 2468): 0x46a3794e: 0x00d8 add-int/lit8 v26, v26, (#1) |
| 455 | * D/dalvikvm( 2468): 0x46a37952: 0x0028 goto, (#0), (#0) |
| 456 | * D/dalvikvm( 2468): 0x46a378ee: 0x0002 move/from16 v0, v26, (#0) |
| 457 | * D/dalvikvm( 2468): 0x46a378f2: 0x0002 move/from16 v1, v29, (#0) |
| 458 | * D/dalvikvm( 2468): 0x46a378f6: 0x0035 if-ge v0, v1, (#10) |
| 459 | * D/dalvikvm( 2468): TRACEINFO (554): 0x46a37624 |
| 460 | * Lcom/cooliris/media/GridLayer;computeVisibleItems 0x1f7 14 of 934, 8 |
| 461 | * blocks |
| 462 | * : |
| 463 | * : |
| 464 | * D/dalvikvm( 2468): 0x20 (0020): ldr r0, [r5, #52] |
| 465 | * D/dalvikvm( 2468): 0x22 (0022): ldr r2, [pc, #96] |
| 466 | * D/dalvikvm( 2468): 0x24 (0024): cmp r0, #0 |
| 467 | * D/dalvikvm( 2468): 0x26 (0026): beq 0x00000034 |
| 468 | * D/dalvikvm( 2468): 0x28 (0028): ldr r1, [r1, #0] |
| 469 | * D/dalvikvm( 2468): 0x2a (002a): ldr r0, [r0, #0] |
| 470 | * D/dalvikvm( 2468): 0x2c (002c): blx r2 |
| 471 | * D/dalvikvm( 2468): 0x2e (002e): cmp r0, #0 |
| 472 | * D/dalvikvm( 2468): 0x30 (0030): beq 0x00000050 |
| 473 | * D/dalvikvm( 2468): 0x32 (0032): ldr r0, [r5, #52] |
| 474 | * D/dalvikvm( 2468): 0x34 (0034): lsls r4, r7, #2 |
| 475 | * D/dalvikvm( 2468): 0x36 (0036): str r0, [r4, r4] |
| 476 | * D/dalvikvm( 2468): -------- dalvik offset: 0x01fb @ goto, (#0), (#0) |
| 477 | * D/dalvikvm( 2468): L0x0195: |
| 478 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0195 @ add-int/lit8 v26, |
| 479 | * v26, (#1) |
| 480 | * D/dalvikvm( 2468): 0x38 (0038): ldr r7, [r5, #104] |
| 481 | * D/dalvikvm( 2468): 0x3a (003a): adds r7, r7, #1 |
| 482 | * D/dalvikvm( 2468): 0x3c (003c): str r7, [r5, #104] |
| 483 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0197 @ goto, (#0), (#0) |
| 484 | * D/dalvikvm( 2468): L0x0165: |
| 485 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0165 @ move/from16 v0, v26, |
| 486 | * (#0) |
| 487 | * D/dalvikvm( 2468): 0x3e (003e): ldr r0, [r5, #104] |
| 488 | * D/dalvikvm( 2468): 0x40 (0040): str r0, [r5, #0] |
| 489 | * |
| 490 | * The "str r0, [r4, r4]" is indeed the culprit of the native crash. |
| 491 | */ |
| 492 | #endif |
| 493 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 494 | return true; |
| 495 | |
| 496 | fail: |
| 497 | return false; |
| 498 | |
| 499 | } |
| 500 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 501 | static void *compilerThreadStart(void *arg) |
| 502 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 503 | int ret; |
| 504 | struct timespec ts; |
| 505 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 506 | dvmChangeStatus(NULL, THREAD_VMWAIT); |
| 507 | |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 508 | /* |
| Bill Buzbee | eb695c6 | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 509 | * If we're not running stand-alone, wait a little before |
| 510 | * recieving translation requests on the assumption that process start |
| 511 | * up code isn't worth compiling. We'll resume when the framework |
| 512 | * signals us that the first screen draw has happened, or the timer |
| 513 | * below expires (to catch daemons). |
| Ben Cheng | f30acbb | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 514 | * |
| 515 | * There is a theoretical race between the callback to |
| 516 | * VMRuntime.startJitCompiation and when the compiler thread reaches this |
| 517 | * point. In case the callback happens earlier, in order not to permanently |
| 518 | * hold the system_server (which is not using the timed wait) in |
| 519 | * interpreter-only mode we bypass the delay here. |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 520 | */ |
| Ben Cheng | f30acbb | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 521 | if (gDvmJit.runningInAndroidFramework && |
| 522 | !gDvmJit.alreadyEnabledViaFramework) { |
| 523 | /* |
| 524 | * If the current VM instance is the system server (detected by having |
| 525 | * 0 in gDvm.systemServerPid), we will use the indefinite wait on the |
| 526 | * conditional variable to determine whether to start the JIT or not. |
| 527 | * If the system server detects that the whole system is booted in |
| 528 | * safe mode, the conditional variable will never be signaled and the |
| 529 | * system server will remain in the interpreter-only mode. All |
| 530 | * subsequent apps will be started with the --enable-safemode flag |
| 531 | * explicitly appended. |
| 532 | */ |
| 533 | if (gDvm.systemServerPid == 0) { |
| 534 | dvmLockMutex(&gDvmJit.compilerLock); |
| 535 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 536 | &gDvmJit.compilerLock); |
| 537 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 538 | LOGD("JIT started for system_server"); |
| 539 | } else { |
| 540 | dvmLockMutex(&gDvmJit.compilerLock); |
| 541 | /* |
| 542 | * TUNING: experiment with the delay & perhaps make it |
| 543 | * target-specific |
| 544 | */ |
| 545 | dvmRelativeCondWait(&gDvmJit.compilerQueueActivity, |
| 546 | &gDvmJit.compilerLock, 3000, 0); |
| 547 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 548 | } |
| Bill Buzbee | eb695c6 | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 549 | if (gDvmJit.haltCompilerThread) { |
| 550 | return NULL; |
| 551 | } |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 554 | compilerThreadStartup(); |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 555 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 556 | dvmLockMutex(&gDvmJit.compilerLock); |
| 557 | /* |
| 558 | * Since the compiler thread will not touch any objects on the heap once |
| 559 | * being created, we just fake its state as VMWAIT so that it can be a |
| 560 | * bit late when there is suspend request pending. |
| 561 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 562 | while (!gDvmJit.haltCompilerThread) { |
| 563 | if (workQueueLength() == 0) { |
| 564 | int cc; |
| 565 | cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 566 | assert(cc == 0); |
| 567 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 568 | &gDvmJit.compilerLock); |
| 569 | continue; |
| 570 | } else { |
| 571 | do { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 572 | CompilerWorkOrder work = workDequeue(); |
| 573 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 574 | #if defined(JIT_STATS) |
| 575 | u8 startTime = dvmGetRelativeTimeUsec(); |
| 576 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 577 | /* |
| 578 | * Check whether there is a suspend request on me. This |
| 579 | * is necessary to allow a clean shutdown. |
| 580 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 581 | dvmCheckSuspendPending(NULL); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 582 | /* Is JitTable filling up? */ |
| 583 | if (gDvmJit.jitTableEntriesUsed > |
| 584 | (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 585 | bool resizeFail = |
| 586 | dvmJitResizeJitTable(gDvmJit.jitTableSize * 2); |
| 587 | /* |
| 588 | * If the jit table is full, consider it's time to reset |
| 589 | * the code cache too. |
| 590 | */ |
| 591 | gDvmJit.codeCacheFull |= resizeFail; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 592 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 593 | if (gDvmJit.haltCompilerThread) { |
| 594 | LOGD("Compiler shutdown in progress - discarding request"); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 595 | } else if (!gDvmJit.codeCacheFull) { |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 596 | bool compileOK = false; |
| 597 | jmp_buf jmpBuf; |
| 598 | work.bailPtr = &jmpBuf; |
| 599 | bool aborted = setjmp(jmpBuf); |
| 600 | if (!aborted) { |
| 601 | compileOK = dvmCompilerDoWork(&work); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 602 | } |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 603 | if (aborted || !compileOK) { |
| 604 | dvmCompilerArenaReset(); |
| 605 | work.result.codeAddress = gDvmJit.interpretTemplate; |
| 606 | } else if (!work.result.discardResult) { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 607 | dvmJitSetCodeAddr(work.pc, work.result.codeAddress, |
| 608 | work.result.instructionSet); |
| 609 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 610 | } |
| 611 | free(work.info); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 612 | #if defined(JIT_STATS) |
| 613 | gDvmJit.jitTime += dvmGetRelativeTimeUsec() - startTime; |
| 614 | #endif |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 615 | dvmLockMutex(&gDvmJit.compilerLock); |
| 616 | } while (workQueueLength() != 0); |
| 617 | } |
| 618 | } |
| 619 | pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 620 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 621 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 622 | /* |
| 623 | * As part of detaching the thread we need to call into Java code to update |
| 624 | * the ThreadGroup, and we should not be in VMWAIT state while executing |
| 625 | * interpreted code. |
| 626 | */ |
| 627 | dvmChangeStatus(NULL, THREAD_RUNNING); |
| 628 | |
| Andy McFadden | 43eb501 | 2010-02-01 16:56:53 -0800 | [diff] [blame] | 629 | if (gDvm.verboseShutdown) |
| 630 | LOGD("Compiler thread shutting down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 631 | return NULL; |
| 632 | } |
| 633 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 634 | bool dvmCompilerStartup(void) |
| 635 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 636 | |
| 637 | dvmInitMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 638 | dvmInitMutex(&gDvmJit.compilerICPatchLock); |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 639 | dvmLockMutex(&gDvmJit.compilerLock); |
| 640 | pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL); |
| 641 | pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL); |
| 642 | |
| 643 | /* Reset the work queue */ |
| 644 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 645 | gDvmJit.compilerQueueLength = 0; |
| 646 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 647 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 648 | /* |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 649 | * Defer rest of initialization until we're sure JIT'ng makes sense. Launch |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 650 | * the compiler thread, which will do the real initialization if and |
| 651 | * when it is signalled to do so. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 652 | */ |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 653 | return dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler", |
| 654 | compilerThreadStart, NULL); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void dvmCompilerShutdown(void) |
| 658 | { |
| 659 | void *threadReturn; |
| 660 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 661 | /* Disable new translation requests */ |
| 662 | gDvmJit.pProfTable = NULL; |
| 663 | gDvmJit.pProfTableCopy = NULL; |
| 664 | |
| Ben Cheng | 88a0f97 | 2010-02-24 15:00:40 -0800 | [diff] [blame] | 665 | if (gDvm.verboseShutdown) { |
| 666 | dvmCompilerDumpStats(); |
| 667 | while (gDvmJit.compilerQueueLength) |
| 668 | sleep(5); |
| 669 | } |
| 670 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 671 | if (gDvmJit.compilerHandle) { |
| 672 | |
| 673 | gDvmJit.haltCompilerThread = true; |
| 674 | |
| 675 | dvmLockMutex(&gDvmJit.compilerLock); |
| 676 | pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 677 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 678 | |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 679 | if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0) |
| 680 | LOGW("Compiler thread join failed\n"); |
| Andy McFadden | 43eb501 | 2010-02-01 16:56:53 -0800 | [diff] [blame] | 681 | else if (gDvm.verboseShutdown) |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 682 | LOGD("Compiler thread has shut down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 683 | } |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 684 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 685 | /* Break loops within the translation cache */ |
| 686 | dvmJitUnchainAll(); |
| Bill Buzbee | 96cfe6c | 2010-02-08 17:08:15 -0800 | [diff] [blame] | 687 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 688 | /* |
| 689 | * NOTE: our current implementatation doesn't allow for the compiler |
| 690 | * thread to be restarted after it exits here. We aren't freeing |
| 691 | * the JitTable or the ProfTable because threads which still may be |
| 692 | * running or in the process of shutting down may hold references to |
| 693 | * them. |
| 694 | */ |
| Bill Buzbee | 96cfe6c | 2010-02-08 17:08:15 -0800 | [diff] [blame] | 695 | } |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 696 | |
| 697 | void dvmCompilerStateRefresh() |
| 698 | { |
| 699 | bool jitActive; |
| 700 | bool jitActivate; |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 701 | bool needUnchain = false; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 702 | |
| 703 | dvmLockMutex(&gDvmJit.tableLock); |
| 704 | jitActive = gDvmJit.pProfTable != NULL; |
| 705 | jitActivate = !(gDvm.debuggerActive || (gDvm.activeProfilers > 0)); |
| 706 | |
| 707 | if (jitActivate && !jitActive) { |
| 708 | gDvmJit.pProfTable = gDvmJit.pProfTableCopy; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 709 | } else if (!jitActivate && jitActive) { |
| 710 | gDvmJit.pProfTable = NULL; |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 711 | needUnchain = true; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 712 | } |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 713 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 714 | if (needUnchain) |
| 715 | dvmJitUnchainAll(); |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 716 | } |