| 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 | |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 115 | /* Block until the queue length is 0, or there is a pending suspend request */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 116 | void dvmCompilerDrainQueue(void) |
| 117 | { |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 118 | Thread *self = dvmThreadSelf(); |
| 119 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 120 | dvmLockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 121 | while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread && |
| 122 | self->suspendCount == 0) { |
| Ben Cheng | 812e6b1 | 2010-03-15 15:19:06 -0700 | [diff] [blame] | 123 | /* |
| 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 Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 130 | } |
| 131 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 132 | } |
| 133 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 134 | bool dvmCompilerSetupCodeCache(void) |
| 135 | { |
| 136 | extern void dvmCompilerTemplateStart(void); |
| 137 | extern void dmvCompilerTemplateEnd(void); |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 138 | int fd; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 139 | |
| 140 | /* Allocate the code cache */ |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 141 | 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 Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 151 | if (gDvmJit.codeCache == MAP_FAILED) { |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 152 | LOGE("Failed to mmap the JIT code cache: %s\n", strerror(errno)); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
| Ben Cheng | 7c4afdb | 2010-02-11 15:03:00 -0800 | [diff] [blame] | 156 | /* This can be found through "dalvik-jit-code-cache" in /proc/<pid>/maps */ |
| 157 | // LOGD("Code cache starts at %p", gDvmJit.codeCache); |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 158 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 159 | /* 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 Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 166 | /* |
| 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 Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 174 | 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 Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 183 | static 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 Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 224 | static void resetCodeCache(void) |
| 225 | { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 226 | Thread* thread; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 227 | u8 startTime = dvmGetRelativeTimeUsec(); |
| 228 | int inJit = 0; |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 229 | int byteUsed = gDvmJit.codeCacheByteUsed; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 230 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 231 | /* 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 Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 233 | /* |
| 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 Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 240 | if (thread->inJitCodeCache) { |
| 241 | inJit++; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | if (inJit) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 246 | LOGD("JIT code cache reset delayed (%d bytes %d/%d)", |
| 247 | gDvmJit.codeCacheByteUsed, gDvmJit.numCodeCacheReset, |
| 248 | ++gDvmJit.numCodeCacheResetDelayed); |
| 249 | return; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 252 | /* 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 Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 256 | while (workQueueLength()) { |
| 257 | CompilerWorkOrder work = workDequeue(); |
| 258 | free(work.info); |
| 259 | } |
| 260 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 261 | /* Reset the JitEntry table contents to the initial unpopulated state */ |
| 262 | dvmJitResetTable(); |
| 263 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 264 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 265 | * Wipe out the code cache content to force immediate crashes if |
| 266 | * stale JIT'ed code is invoked. |
| 267 | */ |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 268 | memset((char *) gDvmJit.codeCache + gDvmJit.templateSize, |
| 269 | 0, |
| 270 | gDvmJit.codeCacheByteUsed - gDvmJit.templateSize); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 271 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 272 | (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed, 0); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 273 | |
| 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 Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 284 | /* Reset the IC patch work queue */ |
| 285 | dvmLockMutex(&gDvmJit.compilerICPatchLock); |
| 286 | gDvmJit.compilerICPatchIndex = 0; |
| 287 | dvmUnlockMutex(&gDvmJit.compilerICPatchLock); |
| 288 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 289 | /* All clear now */ |
| 290 | gDvmJit.codeCacheFull = false; |
| 291 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 292 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 293 | |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 294 | 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 | */ |
| 307 | void dvmCompilerPerformSafePointChecks(void) |
| 308 | { |
| 309 | if (gDvmJit.codeCacheFull) { |
| 310 | resetCodeCache(); |
| 311 | } |
| 312 | dvmCompilerPatchInlineCache(); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 315 | bool 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 Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 338 | dvmLockMutex(&gDvmJit.compilerLock); |
| 339 | |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 340 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 341 | /* Track method-level compilation statistics */ |
| 342 | gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL); |
| Ben Cheng | 452efba | 2010-04-30 15:14:00 -0700 | [diff] [blame^] | 343 | gDvm.verboseShutdown = true; |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 344 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 345 | |
| 346 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 347 | |
| 348 | /* Set up the JitTable */ |
| 349 | |
| 350 | /* Power of 2? */ |
| 351 | assert(gDvmJit.jitTableSize && |
| 352 | !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1))); |
| 353 | |
| 354 | dvmInitMutex(&gDvmJit.tableLock); |
| 355 | dvmLockMutex(&gDvmJit.tableLock); |
| 356 | pJitTable = (JitEntry*) |
| 357 | calloc(gDvmJit.jitTableSize, sizeof(*pJitTable)); |
| 358 | if (!pJitTable) { |
| 359 | LOGE("jit table allocation failed\n"); |
| 360 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 361 | goto fail; |
| 362 | } |
| 363 | /* |
| 364 | * NOTE: the profile table must only be allocated once, globally. |
| 365 | * Profiling is turned on and off by nulling out gDvm.pJitProfTable |
| 366 | * and then restoring its original value. However, this action |
| 367 | * is not syncronized for speed so threads may continue to hold |
| 368 | * and update the profile table after profiling has been turned |
| 369 | * off by null'ng the global pointer. Be aware. |
| 370 | */ |
| 371 | pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE); |
| 372 | if (!pJitProfTable) { |
| 373 | LOGE("jit prof table allocation failed\n"); |
| 374 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 375 | goto fail; |
| 376 | } |
| 377 | memset(pJitProfTable, gDvmJit.threshold, JIT_PROF_SIZE); |
| 378 | for (i=0; i < gDvmJit.jitTableSize; i++) { |
| 379 | pJitTable[i].u.info.chain = gDvmJit.jitTableSize; |
| 380 | } |
| 381 | /* Is chain field wide enough for termination pattern? */ |
| 382 | assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize); |
| 383 | |
| 384 | gDvmJit.pJitEntryTable = pJitTable; |
| 385 | gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1; |
| 386 | gDvmJit.jitTableEntriesUsed = 0; |
| 387 | gDvmJit.compilerHighWater = |
| 388 | COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4); |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 389 | /* |
| 390 | * If the VM is launched with wait-on-the-debugger, we will need to hide |
| 391 | * the profile table here |
| 392 | */ |
| 393 | gDvmJit.pProfTable = dvmDebuggerOrProfilerActive() ? NULL : pJitProfTable; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 394 | gDvmJit.pProfTableCopy = pJitProfTable; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 395 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 396 | |
| 397 | /* Signal running threads to refresh their cached pJitTable pointers */ |
| 398 | dvmSuspendAllThreads(SUSPEND_FOR_REFRESH); |
| 399 | dvmResumeAllThreads(SUSPEND_FOR_REFRESH); |
| Ben Cheng | dca7143 | 2010-03-16 16:04:11 -0700 | [diff] [blame] | 400 | |
| 401 | /* Enable signature breakpoints by customizing the following code */ |
| 402 | #if defined(SIGNATURE_BREAKPOINT) |
| 403 | /* |
| 404 | * Suppose one sees the following native crash in the bugreport: |
| 405 | * I/DEBUG ( 1638): Build fingerprint: 'unknown' |
| 406 | * I/DEBUG ( 1638): pid: 2468, tid: 2507 >>> com.google.android.gallery3d |
| 407 | * I/DEBUG ( 1638): signal 11 (SIGSEGV), fault addr 00001400 |
| 408 | * I/DEBUG ( 1638): r0 44ea7190 r1 44e4f7b8 r2 44ebc710 r3 00000000 |
| 409 | * I/DEBUG ( 1638): r4 00000a00 r5 41862dec r6 4710dc10 r7 00000280 |
| 410 | * I/DEBUG ( 1638): r8 ad010f40 r9 46a37a12 10 001116b0 fp 42a78208 |
| 411 | * I/DEBUG ( 1638): ip 00000090 sp 4710dbc8 lr ad060e67 pc 46b90682 |
| 412 | * cpsr 00000030 |
| 413 | * I/DEBUG ( 1638): #00 pc 46b90682 /dev/ashmem/dalvik-jit-code-cache |
| 414 | * I/DEBUG ( 1638): #01 pc 00060e62 /system/lib/libdvm.so |
| 415 | * |
| 416 | * I/DEBUG ( 1638): code around pc: |
| 417 | * I/DEBUG ( 1638): 46b90660 6888d01c 34091dcc d2174287 4a186b68 |
| 418 | * I/DEBUG ( 1638): 46b90670 d0052800 68006809 28004790 6b68d00e |
| 419 | * I/DEBUG ( 1638): 46b90680 512000bc 37016eaf 6ea866af 6f696028 |
| 420 | * I/DEBUG ( 1638): 46b90690 682a6069 429a686b e003da08 6df1480b |
| 421 | * I/DEBUG ( 1638): 46b906a0 1c2d4788 47806d70 46a378fa 47806d70 |
| 422 | * |
| 423 | * Clearly it is a JIT bug. To find out which translation contains the |
| 424 | * offending code, the content of the memory dump around the faulting PC |
| 425 | * can be pasted into the gDvmJit.signatureBreakpoint[] array and next time |
| 426 | * when a similar compilation is being created, the JIT compiler replay the |
| 427 | * trace in the verbose mode and one can investigate the instruction |
| 428 | * sequence in details. |
| 429 | * |
| 430 | * The length of the signature may need additional experiments to determine. |
| 431 | * The rule of thumb is don't include PC-relative instructions in the |
| 432 | * signature since it may be affected by the alignment of the compiled code. |
| 433 | * However, a signature that's too short might increase the chance of false |
| 434 | * positive matches. Using gdbjithelper to disassembly the memory content |
| 435 | * first might be a good companion approach. |
| 436 | * |
| 437 | * For example, if the next 4 words starting from 46b90680 is pasted into |
| 438 | * the data structure: |
| 439 | */ |
| 440 | |
| 441 | gDvmJit.signatureBreakpointSize = 4; |
| 442 | gDvmJit.signatureBreakpoint = |
| 443 | malloc(sizeof(u4) * gDvmJit.signatureBreakpointSize); |
| 444 | gDvmJit.signatureBreakpoint[0] = 0x512000bc; |
| 445 | gDvmJit.signatureBreakpoint[1] = 0x37016eaf; |
| 446 | gDvmJit.signatureBreakpoint[2] = 0x6ea866af; |
| 447 | gDvmJit.signatureBreakpoint[3] = 0x6f696028; |
| 448 | |
| 449 | /* |
| 450 | * The following log will be printed when a match is found in subsequent |
| 451 | * testings: |
| 452 | * |
| 453 | * D/dalvikvm( 2468): Signature match starting from offset 0x34 (4 words) |
| 454 | * D/dalvikvm( 2468): -------- |
| 455 | * D/dalvikvm( 2468): Compiler: Building trace for computeVisibleItems, |
| 456 | * offset 0x1f7 |
| 457 | * D/dalvikvm( 2468): 0x46a37a12: 0x0090 add-int v42, v5, v26 |
| 458 | * D/dalvikvm( 2468): 0x46a37a16: 0x004d aput-object v13, v14, v42 |
| 459 | * D/dalvikvm( 2468): 0x46a37a1a: 0x0028 goto, (#0), (#0) |
| 460 | * D/dalvikvm( 2468): 0x46a3794e: 0x00d8 add-int/lit8 v26, v26, (#1) |
| 461 | * D/dalvikvm( 2468): 0x46a37952: 0x0028 goto, (#0), (#0) |
| 462 | * D/dalvikvm( 2468): 0x46a378ee: 0x0002 move/from16 v0, v26, (#0) |
| 463 | * D/dalvikvm( 2468): 0x46a378f2: 0x0002 move/from16 v1, v29, (#0) |
| 464 | * D/dalvikvm( 2468): 0x46a378f6: 0x0035 if-ge v0, v1, (#10) |
| 465 | * D/dalvikvm( 2468): TRACEINFO (554): 0x46a37624 |
| 466 | * Lcom/cooliris/media/GridLayer;computeVisibleItems 0x1f7 14 of 934, 8 |
| 467 | * blocks |
| 468 | * : |
| 469 | * : |
| 470 | * D/dalvikvm( 2468): 0x20 (0020): ldr r0, [r5, #52] |
| 471 | * D/dalvikvm( 2468): 0x22 (0022): ldr r2, [pc, #96] |
| 472 | * D/dalvikvm( 2468): 0x24 (0024): cmp r0, #0 |
| 473 | * D/dalvikvm( 2468): 0x26 (0026): beq 0x00000034 |
| 474 | * D/dalvikvm( 2468): 0x28 (0028): ldr r1, [r1, #0] |
| 475 | * D/dalvikvm( 2468): 0x2a (002a): ldr r0, [r0, #0] |
| 476 | * D/dalvikvm( 2468): 0x2c (002c): blx r2 |
| 477 | * D/dalvikvm( 2468): 0x2e (002e): cmp r0, #0 |
| 478 | * D/dalvikvm( 2468): 0x30 (0030): beq 0x00000050 |
| 479 | * D/dalvikvm( 2468): 0x32 (0032): ldr r0, [r5, #52] |
| 480 | * D/dalvikvm( 2468): 0x34 (0034): lsls r4, r7, #2 |
| 481 | * D/dalvikvm( 2468): 0x36 (0036): str r0, [r4, r4] |
| 482 | * D/dalvikvm( 2468): -------- dalvik offset: 0x01fb @ goto, (#0), (#0) |
| 483 | * D/dalvikvm( 2468): L0x0195: |
| 484 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0195 @ add-int/lit8 v26, |
| 485 | * v26, (#1) |
| 486 | * D/dalvikvm( 2468): 0x38 (0038): ldr r7, [r5, #104] |
| 487 | * D/dalvikvm( 2468): 0x3a (003a): adds r7, r7, #1 |
| 488 | * D/dalvikvm( 2468): 0x3c (003c): str r7, [r5, #104] |
| 489 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0197 @ goto, (#0), (#0) |
| 490 | * D/dalvikvm( 2468): L0x0165: |
| 491 | * D/dalvikvm( 2468): -------- dalvik offset: 0x0165 @ move/from16 v0, v26, |
| 492 | * (#0) |
| 493 | * D/dalvikvm( 2468): 0x3e (003e): ldr r0, [r5, #104] |
| 494 | * D/dalvikvm( 2468): 0x40 (0040): str r0, [r5, #0] |
| 495 | * |
| 496 | * The "str r0, [r4, r4]" is indeed the culprit of the native crash. |
| 497 | */ |
| 498 | #endif |
| 499 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 500 | return true; |
| 501 | |
| 502 | fail: |
| 503 | return false; |
| 504 | |
| 505 | } |
| 506 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 507 | static void *compilerThreadStart(void *arg) |
| 508 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 509 | int ret; |
| 510 | struct timespec ts; |
| 511 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 512 | dvmChangeStatus(NULL, THREAD_VMWAIT); |
| 513 | |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 514 | /* |
| Bill Buzbee | eb695c6 | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 515 | * If we're not running stand-alone, wait a little before |
| 516 | * recieving translation requests on the assumption that process start |
| 517 | * up code isn't worth compiling. We'll resume when the framework |
| 518 | * signals us that the first screen draw has happened, or the timer |
| 519 | * below expires (to catch daemons). |
| Ben Cheng | f30acbb | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 520 | * |
| 521 | * There is a theoretical race between the callback to |
| 522 | * VMRuntime.startJitCompiation and when the compiler thread reaches this |
| 523 | * point. In case the callback happens earlier, in order not to permanently |
| 524 | * hold the system_server (which is not using the timed wait) in |
| 525 | * interpreter-only mode we bypass the delay here. |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 526 | */ |
| Ben Cheng | f30acbb | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 527 | if (gDvmJit.runningInAndroidFramework && |
| 528 | !gDvmJit.alreadyEnabledViaFramework) { |
| 529 | /* |
| 530 | * If the current VM instance is the system server (detected by having |
| 531 | * 0 in gDvm.systemServerPid), we will use the indefinite wait on the |
| 532 | * conditional variable to determine whether to start the JIT or not. |
| 533 | * If the system server detects that the whole system is booted in |
| 534 | * safe mode, the conditional variable will never be signaled and the |
| 535 | * system server will remain in the interpreter-only mode. All |
| 536 | * subsequent apps will be started with the --enable-safemode flag |
| 537 | * explicitly appended. |
| 538 | */ |
| 539 | if (gDvm.systemServerPid == 0) { |
| 540 | dvmLockMutex(&gDvmJit.compilerLock); |
| 541 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 542 | &gDvmJit.compilerLock); |
| 543 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 544 | LOGD("JIT started for system_server"); |
| 545 | } else { |
| 546 | dvmLockMutex(&gDvmJit.compilerLock); |
| 547 | /* |
| 548 | * TUNING: experiment with the delay & perhaps make it |
| 549 | * target-specific |
| 550 | */ |
| 551 | dvmRelativeCondWait(&gDvmJit.compilerQueueActivity, |
| 552 | &gDvmJit.compilerLock, 3000, 0); |
| 553 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 554 | } |
| Bill Buzbee | eb695c6 | 2010-02-04 16:09:55 -0800 | [diff] [blame] | 555 | if (gDvmJit.haltCompilerThread) { |
| 556 | return NULL; |
| 557 | } |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 560 | compilerThreadStartup(); |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 561 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 562 | dvmLockMutex(&gDvmJit.compilerLock); |
| 563 | /* |
| 564 | * Since the compiler thread will not touch any objects on the heap once |
| 565 | * being created, we just fake its state as VMWAIT so that it can be a |
| 566 | * bit late when there is suspend request pending. |
| 567 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 568 | while (!gDvmJit.haltCompilerThread) { |
| 569 | if (workQueueLength() == 0) { |
| 570 | int cc; |
| 571 | cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 572 | assert(cc == 0); |
| 573 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 574 | &gDvmJit.compilerLock); |
| 575 | continue; |
| 576 | } else { |
| 577 | do { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 578 | CompilerWorkOrder work = workDequeue(); |
| 579 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 580 | #if defined(JIT_STATS) |
| 581 | u8 startTime = dvmGetRelativeTimeUsec(); |
| 582 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 583 | /* |
| 584 | * Check whether there is a suspend request on me. This |
| 585 | * is necessary to allow a clean shutdown. |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 586 | * |
| 587 | * However, in the blocking stress testing mode, let the |
| 588 | * compiler thread continue doing compilations to unblock |
| 589 | * other requesting threads. This may occasionally cause |
| 590 | * shutdown from proceeding cleanly in the standalone invocation |
| 591 | * of the vm but this should be acceptable. |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 592 | */ |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 593 | if (!gDvmJit.blockingMode) |
| 594 | dvmCheckSuspendPending(NULL); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 595 | /* Is JitTable filling up? */ |
| 596 | if (gDvmJit.jitTableEntriesUsed > |
| 597 | (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 598 | bool resizeFail = |
| 599 | dvmJitResizeJitTable(gDvmJit.jitTableSize * 2); |
| 600 | /* |
| 601 | * If the jit table is full, consider it's time to reset |
| 602 | * the code cache too. |
| 603 | */ |
| 604 | gDvmJit.codeCacheFull |= resizeFail; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 605 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 606 | if (gDvmJit.haltCompilerThread) { |
| 607 | LOGD("Compiler shutdown in progress - discarding request"); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 608 | } else if (!gDvmJit.codeCacheFull) { |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 609 | bool compileOK = false; |
| 610 | jmp_buf jmpBuf; |
| 611 | work.bailPtr = &jmpBuf; |
| 612 | bool aborted = setjmp(jmpBuf); |
| 613 | if (!aborted) { |
| 614 | compileOK = dvmCompilerDoWork(&work); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 615 | } |
| Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 616 | if (aborted || !compileOK) { |
| 617 | dvmCompilerArenaReset(); |
| 618 | work.result.codeAddress = gDvmJit.interpretTemplate; |
| 619 | } else if (!work.result.discardResult) { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 620 | dvmJitSetCodeAddr(work.pc, work.result.codeAddress, |
| 621 | work.result.instructionSet); |
| 622 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 623 | } |
| 624 | free(work.info); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 625 | #if defined(JIT_STATS) |
| 626 | gDvmJit.jitTime += dvmGetRelativeTimeUsec() - startTime; |
| 627 | #endif |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 628 | dvmLockMutex(&gDvmJit.compilerLock); |
| 629 | } while (workQueueLength() != 0); |
| 630 | } |
| 631 | } |
| 632 | pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 633 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 634 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 635 | /* |
| 636 | * As part of detaching the thread we need to call into Java code to update |
| 637 | * the ThreadGroup, and we should not be in VMWAIT state while executing |
| 638 | * interpreted code. |
| 639 | */ |
| 640 | dvmChangeStatus(NULL, THREAD_RUNNING); |
| 641 | |
| Andy McFadden | 43eb501 | 2010-02-01 16:56:53 -0800 | [diff] [blame] | 642 | if (gDvm.verboseShutdown) |
| 643 | LOGD("Compiler thread shutting down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 644 | return NULL; |
| 645 | } |
| 646 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 647 | bool dvmCompilerStartup(void) |
| 648 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 649 | |
| 650 | dvmInitMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 651 | dvmInitMutex(&gDvmJit.compilerICPatchLock); |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 652 | dvmLockMutex(&gDvmJit.compilerLock); |
| 653 | pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL); |
| 654 | pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL); |
| 655 | |
| 656 | /* Reset the work queue */ |
| 657 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 658 | gDvmJit.compilerQueueLength = 0; |
| 659 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 660 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 661 | /* |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame] | 662 | * 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] | 663 | * the compiler thread, which will do the real initialization if and |
| 664 | * when it is signalled to do so. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 665 | */ |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 666 | return dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler", |
| 667 | compilerThreadStart, NULL); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void dvmCompilerShutdown(void) |
| 671 | { |
| 672 | void *threadReturn; |
| 673 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 674 | /* Disable new translation requests */ |
| 675 | gDvmJit.pProfTable = NULL; |
| 676 | gDvmJit.pProfTableCopy = NULL; |
| 677 | |
| Ben Cheng | 88a0f97 | 2010-02-24 15:00:40 -0800 | [diff] [blame] | 678 | if (gDvm.verboseShutdown) { |
| 679 | dvmCompilerDumpStats(); |
| 680 | while (gDvmJit.compilerQueueLength) |
| 681 | sleep(5); |
| 682 | } |
| 683 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 684 | if (gDvmJit.compilerHandle) { |
| 685 | |
| 686 | gDvmJit.haltCompilerThread = true; |
| 687 | |
| 688 | dvmLockMutex(&gDvmJit.compilerLock); |
| 689 | pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 690 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 691 | |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 692 | if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0) |
| 693 | LOGW("Compiler thread join failed\n"); |
| Andy McFadden | 43eb501 | 2010-02-01 16:56:53 -0800 | [diff] [blame] | 694 | else if (gDvm.verboseShutdown) |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 695 | LOGD("Compiler thread has shut down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 696 | } |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 697 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 698 | /* Break loops within the translation cache */ |
| 699 | dvmJitUnchainAll(); |
| Bill Buzbee | 96cfe6c | 2010-02-08 17:08:15 -0800 | [diff] [blame] | 700 | |
| Bill Buzbee | 2fc03c3 | 2010-03-08 11:30:19 -0800 | [diff] [blame] | 701 | /* |
| 702 | * NOTE: our current implementatation doesn't allow for the compiler |
| 703 | * thread to be restarted after it exits here. We aren't freeing |
| 704 | * the JitTable or the ProfTable because threads which still may be |
| 705 | * running or in the process of shutting down may hold references to |
| 706 | * them. |
| 707 | */ |
| Bill Buzbee | 96cfe6c | 2010-02-08 17:08:15 -0800 | [diff] [blame] | 708 | } |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 709 | |
| 710 | void dvmCompilerStateRefresh() |
| 711 | { |
| 712 | bool jitActive; |
| 713 | bool jitActivate; |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 714 | bool needUnchain = false; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 715 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 716 | /* |
| 717 | * The tableLock might not be initialized yet by the compiler thread if |
| 718 | * debugger is attached from the very beginning of the VM launch. If |
| 719 | * pProfTableCopy is NULL, the lock is not initialized yet and we don't |
| 720 | * need to refresh anything either. |
| 721 | */ |
| 722 | if (gDvmJit.pProfTableCopy == NULL) { |
| 723 | return; |
| 724 | } |
| 725 | |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 726 | dvmLockMutex(&gDvmJit.tableLock); |
| 727 | jitActive = gDvmJit.pProfTable != NULL; |
| 728 | jitActivate = !(gDvm.debuggerActive || (gDvm.activeProfilers > 0)); |
| 729 | |
| 730 | if (jitActivate && !jitActive) { |
| 731 | gDvmJit.pProfTable = gDvmJit.pProfTableCopy; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 732 | } else if (!jitActivate && jitActive) { |
| 733 | gDvmJit.pProfTable = NULL; |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 734 | needUnchain = true; |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 735 | } |
| Bill Buzbee | 3e39268 | 2010-02-03 18:13:57 -0800 | [diff] [blame] | 736 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 737 | if (needUnchain) |
| 738 | dvmJitUnchainAll(); |
| Bill Buzbee | 06bb839 | 2010-01-31 18:53:15 -0800 | [diff] [blame] | 739 | } |