| 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> |
| 19 | |
| 20 | #include "Dalvik.h" |
| 21 | #include "interp/Jit.h" |
| 22 | #include "CompilerInternals.h" |
| 23 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 24 | static inline bool workQueueLength(void) |
| 25 | { |
| 26 | return gDvmJit.compilerQueueLength; |
| 27 | } |
| 28 | |
| 29 | static CompilerWorkOrder workDequeue(void) |
| 30 | { |
| 31 | assert(gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex].kind |
| 32 | != kWorkOrderInvalid); |
| 33 | CompilerWorkOrder work = |
| 34 | gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex]; |
| 35 | gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkDequeueIndex++].kind = |
| 36 | kWorkOrderInvalid; |
| 37 | if (gDvmJit.compilerWorkDequeueIndex == COMPILER_WORK_QUEUE_SIZE) { |
| 38 | gDvmJit.compilerWorkDequeueIndex = 0; |
| 39 | } |
| 40 | gDvmJit.compilerQueueLength--; |
| Bill Buzbee | f9f3328 | 2009-11-22 12:45:30 -0800 | [diff] [blame] | 41 | if (gDvmJit.compilerQueueLength == 0) { |
| 42 | int cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 43 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 44 | |
| 45 | /* Remember the high water mark of the queue length */ |
| 46 | if (gDvmJit.compilerQueueLength > gDvmJit.compilerMaxQueued) |
| 47 | gDvmJit.compilerMaxQueued = gDvmJit.compilerQueueLength; |
| 48 | |
| 49 | return work; |
| 50 | } |
| 51 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 52 | /* |
| 53 | * Attempt to enqueue a work order, returning true if successful. |
| 54 | * This routine will not block, but simply return if it couldn't |
| 55 | * aquire the lock or if the queue is full. |
| 56 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 57 | bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info) |
| 58 | { |
| 59 | int cc; |
| 60 | int i; |
| 61 | int numWork; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 62 | bool result = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 63 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 64 | if (dvmTryLockMutex(&gDvmJit.compilerLock)) { |
| 65 | return false; // Couldn't aquire the lock |
| 66 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 67 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Return if queue is full. |
| 70 | * If the code cache is full, we will allow the work order to be added and |
| 71 | * we use that to trigger code cache reset. |
| 72 | */ |
| 73 | if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE) { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 74 | result = false; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 75 | goto unlockAndExit; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | for (numWork = gDvmJit.compilerQueueLength, |
| 79 | i = gDvmJit.compilerWorkDequeueIndex; |
| 80 | numWork > 0; |
| 81 | numWork--) { |
| 82 | /* Already enqueued */ |
| 83 | if (gDvmJit.compilerWorkQueue[i++].pc == pc) |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 84 | goto unlockAndExit; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 85 | /* Wrap around */ |
| 86 | if (i == COMPILER_WORK_QUEUE_SIZE) |
| 87 | i = 0; |
| 88 | } |
| 89 | |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 90 | CompilerWorkOrder *newOrder = |
| 91 | &gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex]; |
| 92 | newOrder->pc = pc; |
| 93 | newOrder->kind = kind; |
| 94 | newOrder->info = info; |
| 95 | newOrder->result.codeAddress = NULL; |
| 96 | newOrder->result.discardResult = |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 97 | (kind == kWorkOrderTraceDebug || kind == kWorkOrderICPatch) ? |
| 98 | true : false; |
| Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 99 | newOrder->result.requestingThread = dvmThreadSelf(); |
| 100 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 101 | gDvmJit.compilerWorkEnqueueIndex++; |
| 102 | if (gDvmJit.compilerWorkEnqueueIndex == COMPILER_WORK_QUEUE_SIZE) |
| 103 | gDvmJit.compilerWorkEnqueueIndex = 0; |
| 104 | gDvmJit.compilerQueueLength++; |
| 105 | cc = pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 106 | assert(cc == 0); |
| 107 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 108 | unlockAndExit: |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 109 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 110 | return result; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Block until queue length is 0 */ |
| 114 | void dvmCompilerDrainQueue(void) |
| 115 | { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 116 | int oldStatus = dvmChangeStatus(NULL, THREAD_VMWAIT); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 117 | dvmLockMutex(&gDvmJit.compilerLock); |
| 118 | while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread) { |
| 119 | pthread_cond_wait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock); |
| 120 | } |
| 121 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 122 | dvmChangeStatus(NULL, oldStatus); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 125 | bool dvmCompilerSetupCodeCache(void) |
| 126 | { |
| 127 | extern void dvmCompilerTemplateStart(void); |
| 128 | extern void dmvCompilerTemplateEnd(void); |
| 129 | |
| 130 | /* Allocate the code cache */ |
| 131 | gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE, |
| 132 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 133 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 134 | if (gDvmJit.codeCache == MAP_FAILED) { |
| 135 | LOGE("Failed to create the code cache: %s\n", strerror(errno)); |
| 136 | return false; |
| 137 | } |
| 138 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 139 | // For debugging only |
| 140 | // LOGD("Code cache starts at %p", gDvmJit.codeCache); |
| 141 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 142 | /* Copy the template code into the beginning of the code cache */ |
| 143 | int templateSize = (intptr_t) dmvCompilerTemplateEnd - |
| 144 | (intptr_t) dvmCompilerTemplateStart; |
| 145 | memcpy((void *) gDvmJit.codeCache, |
| 146 | (void *) dvmCompilerTemplateStart, |
| 147 | templateSize); |
| 148 | |
| 149 | gDvmJit.templateSize = templateSize; |
| 150 | gDvmJit.codeCacheByteUsed = templateSize; |
| 151 | |
| 152 | /* Only flush the part in the code cache that is being used now */ |
| 153 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 154 | (intptr_t) gDvmJit.codeCache + templateSize, 0); |
| 155 | return true; |
| 156 | } |
| 157 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 158 | static void crawlDalvikStack(Thread *thread, bool print) |
| 159 | { |
| 160 | void *fp = thread->curFrame; |
| 161 | StackSaveArea* saveArea = NULL; |
| 162 | int stackLevel = 0; |
| 163 | |
| 164 | if (print) { |
| 165 | LOGD("Crawling tid %d (%s / %p %s)", thread->systemTid, |
| 166 | dvmGetThreadStatusStr(thread->status), |
| 167 | thread->inJitCodeCache, |
| 168 | thread->inJitCodeCache ? "jit" : "interp"); |
| 169 | } |
| 170 | /* Crawl the Dalvik stack frames to clear the returnAddr field */ |
| 171 | while (fp != NULL) { |
| 172 | saveArea = SAVEAREA_FROM_FP(fp); |
| 173 | |
| 174 | if (print) { |
| 175 | if (dvmIsBreakFrame(fp)) { |
| 176 | LOGD(" #%d: break frame (%p)", |
| 177 | stackLevel, saveArea->returnAddr); |
| 178 | } |
| 179 | else { |
| 180 | LOGD(" #%d: %s.%s%s (%p)", |
| 181 | stackLevel, |
| 182 | saveArea->method->clazz->descriptor, |
| 183 | saveArea->method->name, |
| 184 | dvmIsNativeMethod(saveArea->method) ? |
| 185 | " (native)" : "", |
| 186 | saveArea->returnAddr); |
| 187 | } |
| 188 | } |
| 189 | stackLevel++; |
| 190 | saveArea->returnAddr = NULL; |
| 191 | assert(fp != saveArea->prevFrame); |
| 192 | fp = saveArea->prevFrame; |
| 193 | } |
| 194 | /* Make sure the stack is fully unwound to the bottom */ |
| 195 | assert(saveArea == NULL || |
| 196 | (u1 *) (saveArea+1) == thread->interpStackStart); |
| 197 | } |
| 198 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 199 | static void resetCodeCache(void) |
| 200 | { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 201 | Thread* thread; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 202 | u8 startTime = dvmGetRelativeTimeUsec(); |
| 203 | int inJit = 0; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 204 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 205 | LOGD("Reset the JIT code cache (%d bytes used / %d time(s))", |
| 206 | gDvmJit.codeCacheByteUsed, ++gDvmJit.numCodeCacheReset); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 207 | |
| 208 | /* Stop the world */ |
| 209 | dvmSuspendAllThreads(SUSPEND_FOR_CC_RESET); |
| 210 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 211 | /* If any thread is found stuck in the JIT state, don't reset the cache */ |
| 212 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 213 | if (thread->inJitCodeCache) { |
| 214 | inJit++; |
| 215 | /* |
| 216 | * STOPSHIP |
| 217 | * Change the verbose mode to false after the new code receives |
| 218 | * more QA love. |
| 219 | */ |
| 220 | crawlDalvikStack(thread, true); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (inJit) { |
| 225 | /* Wait a while for the busy threads to rest and try again */ |
| 226 | gDvmJit.delayCodeCacheReset = 256; |
| 227 | goto done; |
| 228 | } |
| 229 | |
| 230 | /* Drain the work queue to free the work order */ |
| 231 | while (workQueueLength()) { |
| 232 | CompilerWorkOrder work = workDequeue(); |
| 233 | free(work.info); |
| 234 | } |
| 235 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 236 | /* Wipe out the returnAddr field that soon will point to stale code */ |
| 237 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 238 | crawlDalvikStack(thread, false); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* Reset the JitEntry table contents to the initial unpopulated state */ |
| 242 | dvmJitResetTable(); |
| 243 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 244 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 245 | * Wipe out the code cache content to force immediate crashes if |
| 246 | * stale JIT'ed code is invoked. |
| 247 | */ |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 248 | memset((char *) gDvmJit.codeCache + gDvmJit.templateSize, |
| 249 | 0, |
| 250 | gDvmJit.codeCacheByteUsed - gDvmJit.templateSize); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 251 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 252 | (intptr_t) gDvmJit.codeCache + gDvmJit.codeCacheByteUsed, 0); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 253 | |
| 254 | /* Reset the current mark of used bytes to the end of template code */ |
| 255 | gDvmJit.codeCacheByteUsed = gDvmJit.templateSize; |
| 256 | gDvmJit.numCompilations = 0; |
| 257 | |
| 258 | /* Reset the work queue */ |
| 259 | memset(gDvmJit.compilerWorkQueue, 0, |
| 260 | sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE); |
| 261 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 262 | gDvmJit.compilerQueueLength = 0; |
| 263 | |
| 264 | /* All clear now */ |
| 265 | gDvmJit.codeCacheFull = false; |
| 266 | |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 267 | LOGD("Code cache reset takes %lld usec", |
| 268 | dvmGetRelativeTimeUsec() - startTime); |
| 269 | |
| 270 | done: |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 271 | /* Resume all threads */ |
| 272 | dvmResumeAllThreads(SUSPEND_FOR_CC_RESET); |
| 273 | } |
| 274 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 275 | bool compilerThreadStartup(void) |
| 276 | { |
| 277 | JitEntry *pJitTable = NULL; |
| 278 | unsigned char *pJitProfTable = NULL; |
| 279 | unsigned int i; |
| 280 | |
| 281 | if (!dvmCompilerArchInit()) |
| 282 | goto fail; |
| 283 | |
| 284 | /* |
| 285 | * Setup the code cache if we have not inherited a valid code cache |
| 286 | * from the zygote. |
| 287 | */ |
| 288 | if (gDvmJit.codeCache == NULL) { |
| 289 | if (!dvmCompilerSetupCodeCache()) |
| 290 | goto fail; |
| 291 | } |
| 292 | |
| 293 | /* Allocate the initial arena block */ |
| 294 | if (dvmCompilerHeapInit() == false) { |
| 295 | goto fail; |
| 296 | } |
| 297 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 298 | dvmLockMutex(&gDvmJit.compilerLock); |
| 299 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 300 | /* Track method-level compilation statistics */ |
| 301 | gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL); |
| 302 | |
| 303 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 304 | |
| 305 | /* Set up the JitTable */ |
| 306 | |
| 307 | /* Power of 2? */ |
| 308 | assert(gDvmJit.jitTableSize && |
| 309 | !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1))); |
| 310 | |
| 311 | dvmInitMutex(&gDvmJit.tableLock); |
| 312 | dvmLockMutex(&gDvmJit.tableLock); |
| 313 | pJitTable = (JitEntry*) |
| 314 | calloc(gDvmJit.jitTableSize, sizeof(*pJitTable)); |
| 315 | if (!pJitTable) { |
| 316 | LOGE("jit table allocation failed\n"); |
| 317 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 318 | goto fail; |
| 319 | } |
| 320 | /* |
| 321 | * NOTE: the profile table must only be allocated once, globally. |
| 322 | * Profiling is turned on and off by nulling out gDvm.pJitProfTable |
| 323 | * and then restoring its original value. However, this action |
| 324 | * is not syncronized for speed so threads may continue to hold |
| 325 | * and update the profile table after profiling has been turned |
| 326 | * off by null'ng the global pointer. Be aware. |
| 327 | */ |
| 328 | pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE); |
| 329 | if (!pJitProfTable) { |
| 330 | LOGE("jit prof table allocation failed\n"); |
| 331 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 332 | goto fail; |
| 333 | } |
| 334 | memset(pJitProfTable, gDvmJit.threshold, JIT_PROF_SIZE); |
| 335 | for (i=0; i < gDvmJit.jitTableSize; i++) { |
| 336 | pJitTable[i].u.info.chain = gDvmJit.jitTableSize; |
| 337 | } |
| 338 | /* Is chain field wide enough for termination pattern? */ |
| 339 | assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize); |
| 340 | |
| 341 | gDvmJit.pJitEntryTable = pJitTable; |
| 342 | gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1; |
| 343 | gDvmJit.jitTableEntriesUsed = 0; |
| 344 | gDvmJit.compilerHighWater = |
| 345 | COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4); |
| 346 | gDvmJit.pProfTable = pJitProfTable; |
| 347 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 348 | |
| 349 | /* Signal running threads to refresh their cached pJitTable pointers */ |
| 350 | dvmSuspendAllThreads(SUSPEND_FOR_REFRESH); |
| 351 | dvmResumeAllThreads(SUSPEND_FOR_REFRESH); |
| 352 | return true; |
| 353 | |
| 354 | fail: |
| 355 | return false; |
| 356 | |
| 357 | } |
| 358 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 359 | static void *compilerThreadStart(void *arg) |
| 360 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame^] | 361 | int ret; |
| 362 | struct timespec ts; |
| 363 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 364 | dvmChangeStatus(NULL, THREAD_VMWAIT); |
| 365 | |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 366 | /* |
| 367 | * Wait a little before recieving translation requests on the assumption |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 368 | * that process start-up code isn't worth compiling. |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 369 | */ |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame^] | 370 | |
| 371 | dvmLockMutex(&gDvmJit.compilerLock); |
| 372 | /* |
| 373 | * TUNING: once framework is calling VMRuntime.startJitCompilation, |
| 374 | * experiment with the delay time (and perhaps have target-dependent |
| 375 | * values? |
| 376 | */ |
| 377 | dvmAbsoluteTime(1000, 0, &ts); |
| 378 | #if defined(HAVE_TIMEDWAIT_MONOTONIC) |
| 379 | ret = pthread_cond_timedwait_monotonic(&gDvmJit.compilerQueueActivity, |
| 380 | &gDvmJit.compilerLock, &ts); |
| 381 | #else |
| 382 | ret = pthread_cond_timedwait(&gDvmJit.compilerQueueActivity, |
| 383 | &gDvmJit.compilerLock, &ts); |
| 384 | #endif |
| 385 | assert(ret == 0 || ret == ETIMEDOUT); |
| 386 | |
| 387 | if (gDvmJit.haltCompilerThread) { |
| 388 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 393 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 394 | compilerThreadStartup(); |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 395 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 396 | dvmLockMutex(&gDvmJit.compilerLock); |
| 397 | /* |
| 398 | * Since the compiler thread will not touch any objects on the heap once |
| 399 | * being created, we just fake its state as VMWAIT so that it can be a |
| 400 | * bit late when there is suspend request pending. |
| 401 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 402 | while (!gDvmJit.haltCompilerThread) { |
| 403 | if (workQueueLength() == 0) { |
| 404 | int cc; |
| 405 | cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 406 | assert(cc == 0); |
| 407 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 408 | &gDvmJit.compilerLock); |
| 409 | continue; |
| 410 | } else { |
| 411 | do { |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 412 | bool resizeFail = false; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 413 | CompilerWorkOrder work = workDequeue(); |
| 414 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 415 | /* |
| 416 | * Check whether there is a suspend request on me. This |
| 417 | * is necessary to allow a clean shutdown. |
| 418 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 419 | dvmCheckSuspendPending(NULL); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 420 | /* Is JitTable filling up? */ |
| 421 | if (gDvmJit.jitTableEntriesUsed > |
| 422 | (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) { |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 423 | resizeFail = dvmJitResizeJitTable(gDvmJit.jitTableSize * 2); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 424 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 425 | if (gDvmJit.haltCompilerThread) { |
| 426 | LOGD("Compiler shutdown in progress - discarding request"); |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 427 | } else if (!resizeFail) { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 428 | /* If compilation failed, use interpret-template */ |
| 429 | if (!dvmCompilerDoWork(&work)) { |
| 430 | work.result.codeAddress = gDvmJit.interpretTemplate; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 431 | } |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 432 | if (!work.result.discardResult) { |
| 433 | dvmJitSetCodeAddr(work.pc, work.result.codeAddress, |
| 434 | work.result.instructionSet); |
| 435 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 436 | } |
| 437 | free(work.info); |
| 438 | dvmLockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 439 | |
| Ben Cheng | 49ca786 | 2010-01-20 12:03:51 -0800 | [diff] [blame] | 440 | /* |
| 441 | * FIXME - temporarily disable code cache reset until |
| 442 | * stale code stops leaking. |
| 443 | */ |
| 444 | #if 0 |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 445 | if (gDvmJit.codeCacheFull == true || resizeFail) { |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 446 | if (gDvmJit.delayCodeCacheReset == 0) { |
| 447 | resetCodeCache(); |
| 448 | assert(workQueueLength() == 0 || |
| 449 | gDvmJit.delayCodeCacheReset != 0); |
| 450 | } else { |
| 451 | LOGD("Delay the next %d tries to reset code cache", |
| 452 | gDvmJit.delayCodeCacheReset); |
| 453 | gDvmJit.delayCodeCacheReset--; |
| 454 | } |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 455 | } |
| Ben Cheng | 49ca786 | 2010-01-20 12:03:51 -0800 | [diff] [blame] | 456 | #endif |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 457 | } while (workQueueLength() != 0); |
| 458 | } |
| 459 | } |
| 460 | pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 461 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 462 | |
| Ben Cheng | 5ccdf0b | 2009-10-08 16:09:49 -0700 | [diff] [blame] | 463 | /* |
| 464 | * As part of detaching the thread we need to call into Java code to update |
| 465 | * the ThreadGroup, and we should not be in VMWAIT state while executing |
| 466 | * interpreted code. |
| 467 | */ |
| 468 | dvmChangeStatus(NULL, THREAD_RUNNING); |
| 469 | |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 470 | LOGD("Compiler thread shutting down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 471 | return NULL; |
| 472 | } |
| 473 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 474 | bool dvmCompilerStartup(void) |
| 475 | { |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame^] | 476 | |
| 477 | dvmInitMutex(&gDvmJit.compilerLock); |
| 478 | dvmLockMutex(&gDvmJit.compilerLock); |
| 479 | pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL); |
| 480 | pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL); |
| 481 | |
| 482 | /* Reset the work queue */ |
| 483 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 484 | gDvmJit.compilerQueueLength = 0; |
| 485 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 486 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 487 | /* |
| Bill Buzbee | 94d89f8 | 2010-01-29 13:44:19 -0800 | [diff] [blame^] | 488 | * 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] | 489 | * the compiler thread, which will do the real initialization if and |
| 490 | * when it is signalled to do so. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 491 | */ |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 492 | return dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler", |
| 493 | compilerThreadStart, NULL); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void dvmCompilerShutdown(void) |
| 497 | { |
| 498 | void *threadReturn; |
| 499 | |
| 500 | if (gDvmJit.compilerHandle) { |
| 501 | |
| 502 | gDvmJit.haltCompilerThread = true; |
| 503 | |
| 504 | dvmLockMutex(&gDvmJit.compilerLock); |
| 505 | pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 506 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 507 | |
| Ben Cheng | ef00a85 | 2009-06-22 22:53:35 -0700 | [diff] [blame] | 508 | if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0) |
| 509 | LOGW("Compiler thread join failed\n"); |
| 510 | else |
| 511 | LOGD("Compiler thread has shut down\n"); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 512 | } |
| 513 | } |