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