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