| 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 | |
| 24 | |
| 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--; |
| 42 | |
| 43 | /* Remember the high water mark of the queue length */ |
| 44 | if (gDvmJit.compilerQueueLength > gDvmJit.compilerMaxQueued) |
| 45 | gDvmJit.compilerMaxQueued = gDvmJit.compilerQueueLength; |
| 46 | |
| 47 | return work; |
| 48 | } |
| 49 | |
| 50 | bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info) |
| 51 | { |
| 52 | int cc; |
| 53 | int i; |
| 54 | int numWork; |
| 55 | |
| 56 | dvmLockMutex(&gDvmJit.compilerLock); |
| 57 | |
| 58 | /* Queue full */ |
| 59 | if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE || |
| 60 | gDvmJit.codeCacheFull == true) { |
| 61 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | for (numWork = gDvmJit.compilerQueueLength, |
| 66 | i = gDvmJit.compilerWorkDequeueIndex; |
| 67 | numWork > 0; |
| 68 | numWork--) { |
| 69 | /* Already enqueued */ |
| 70 | if (gDvmJit.compilerWorkQueue[i++].pc == pc) |
| 71 | goto done; |
| 72 | /* Wrap around */ |
| 73 | if (i == COMPILER_WORK_QUEUE_SIZE) |
| 74 | i = 0; |
| 75 | } |
| 76 | |
| 77 | gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex].pc = pc; |
| 78 | gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex].kind = kind; |
| 79 | gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex].info = info; |
| 80 | gDvmJit.compilerWorkEnqueueIndex++; |
| 81 | if (gDvmJit.compilerWorkEnqueueIndex == COMPILER_WORK_QUEUE_SIZE) |
| 82 | gDvmJit.compilerWorkEnqueueIndex = 0; |
| 83 | gDvmJit.compilerQueueLength++; |
| 84 | cc = pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 85 | assert(cc == 0); |
| 86 | |
| 87 | done: |
| 88 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /* Block until queue length is 0 */ |
| 93 | void dvmCompilerDrainQueue(void) |
| 94 | { |
| 95 | dvmLockMutex(&gDvmJit.compilerLock); |
| 96 | while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread) { |
| 97 | pthread_cond_wait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock); |
| 98 | } |
| 99 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 100 | } |
| 101 | |
| 102 | static void *compilerThreadStart(void *arg) |
| 103 | { |
| 104 | dvmLockMutex(&gDvmJit.compilerLock); |
| 105 | /* |
| 106 | * Since the compiler thread will not touch any objects on the heap once |
| 107 | * being created, we just fake its state as VMWAIT so that it can be a |
| 108 | * bit late when there is suspend request pending. |
| 109 | */ |
| 110 | dvmChangeStatus(NULL, THREAD_VMWAIT); |
| 111 | while (!gDvmJit.haltCompilerThread) { |
| 112 | if (workQueueLength() == 0) { |
| 113 | int cc; |
| 114 | cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 115 | assert(cc == 0); |
| 116 | pthread_cond_wait(&gDvmJit.compilerQueueActivity, |
| 117 | &gDvmJit.compilerLock); |
| 118 | continue; |
| 119 | } else { |
| 120 | do { |
| 121 | void *compiledCodePtr; |
| 122 | CompilerWorkOrder work = workDequeue(); |
| 123 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 124 | /* Check whether there is a suspend request on me */ |
| 125 | dvmCheckSuspendPending(NULL); |
| 126 | if (gDvmJit.haltCompilerThread) { |
| 127 | LOGD("Compiler shutdown in progress - discarding request"); |
| 128 | } else { |
| 129 | compiledCodePtr = dvmCompilerDoWork(&work); |
| 130 | /* Compilation is successful */ |
| 131 | if (compiledCodePtr) { |
| 132 | dvmJitSetCodeAddr(work.pc, compiledCodePtr); |
| 133 | } |
| 134 | } |
| 135 | free(work.info); |
| 136 | dvmLockMutex(&gDvmJit.compilerLock); |
| 137 | } while (workQueueLength() != 0); |
| 138 | } |
| 139 | } |
| 140 | pthread_cond_signal(&gDvmJit.compilerQueueEmpty); |
| 141 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | bool dvmCompilerSetupCodeCache(void) |
| 146 | { |
| 147 | extern void dvmCompilerTemplateStart(void); |
| 148 | extern void dmvCompilerTemplateEnd(void); |
| 149 | |
| 150 | /* Allocate the code cache */ |
| 151 | gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE, |
| 152 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 153 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 154 | if (gDvmJit.codeCache == MAP_FAILED) { |
| 155 | LOGE("Failed to create the code cache: %s\n", strerror(errno)); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /* Copy the template code into the beginning of the code cache */ |
| 160 | int templateSize = (intptr_t) dmvCompilerTemplateEnd - |
| 161 | (intptr_t) dvmCompilerTemplateStart; |
| 162 | memcpy((void *) gDvmJit.codeCache, |
| 163 | (void *) dvmCompilerTemplateStart, |
| 164 | templateSize); |
| 165 | gDvmJit.codeCacheByteUsed = templateSize; |
| 166 | |
| 167 | /* Flush dcache and invalidate the icache to maintain coherence */ |
| 168 | cacheflush((intptr_t) gDvmJit.codeCache, |
| 169 | (intptr_t) gDvmJit.codeCache + CODE_CACHE_SIZE, 0); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool dvmCompilerStartup(void) |
| 174 | { |
| 175 | /* Make sure the BBType enum is in sane state */ |
| 176 | assert(CHAINING_CELL_GENERIC == 0); |
| 177 | |
| 178 | /* Architecture-specific chores to initialize */ |
| 179 | if (!dvmCompilerArchInit()) |
| 180 | goto fail; |
| 181 | |
| 182 | /* |
| 183 | * Setup the code cache if it is not done so already. For apps it should be |
| 184 | * done by the Zygote already, but for command-line dalvikvm invocation we |
| 185 | * need to do it here. |
| 186 | */ |
| 187 | if (gDvmJit.codeCache == NULL) { |
| 188 | if (!dvmCompilerSetupCodeCache()) |
| 189 | goto fail; |
| 190 | } |
| 191 | |
| 192 | /* Allocate the initial arena block */ |
| 193 | if (dvmCompilerHeapInit() == false) { |
| 194 | goto fail; |
| 195 | } |
| 196 | |
| 197 | dvmInitMutex(&gDvmJit.compilerLock); |
| 198 | pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL); |
| 199 | pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL); |
| 200 | |
| 201 | dvmLockMutex(&gDvmJit.compilerLock); |
| 202 | |
| 203 | gDvmJit.haltCompilerThread = false; |
| 204 | |
| 205 | /* Reset the work queue */ |
| 206 | memset(gDvmJit.compilerWorkQueue, 0, |
| 207 | sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE); |
| 208 | gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0; |
| 209 | gDvmJit.compilerQueueLength = 0; |
| 210 | gDvmJit.compilerHighWater = |
| 211 | COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4); |
| 212 | |
| 213 | assert(gDvmJit.compilerHighWater < COMPILER_WORK_QUEUE_SIZE); |
| 214 | if (!dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler", |
| 215 | compilerThreadStart, NULL)) { |
| 216 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 217 | goto fail; |
| 218 | } |
| 219 | |
| 220 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 221 | |
| 222 | return true; |
| 223 | |
| 224 | fail: |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | void dvmCompilerShutdown(void) |
| 229 | { |
| 230 | void *threadReturn; |
| 231 | |
| 232 | if (gDvmJit.compilerHandle) { |
| 233 | |
| 234 | gDvmJit.haltCompilerThread = true; |
| 235 | |
| 236 | dvmLockMutex(&gDvmJit.compilerLock); |
| 237 | pthread_cond_signal(&gDvmJit.compilerQueueActivity); |
| 238 | dvmUnlockMutex(&gDvmJit.compilerLock); |
| 239 | |
| 240 | pthread_join(gDvmJit.compilerHandle, &threadReturn); |
| 241 | } |
| 242 | } |