blob: 5787378d219e3c0b787aecb63f7eef325bb625dd [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
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 Chengba4fc8b2009-06-01 13:00:29 -070024static inline bool workQueueLength(void)
25{
26 return gDvmJit.compilerQueueLength;
27}
28
29static 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 Buzbeef9f33282009-11-22 12:45:30 -080041 if (gDvmJit.compilerQueueLength == 0) {
42 int cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
43 }
Ben Chengba4fc8b2009-06-01 13:00:29 -070044
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
52bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info)
53{
54 int cc;
55 int i;
56 int numWork;
57
58 dvmLockMutex(&gDvmJit.compilerLock);
59
60 /* Queue full */
61 if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE ||
62 gDvmJit.codeCacheFull == true) {
63 dvmUnlockMutex(&gDvmJit.compilerLock);
64 return false;
65 }
66
67 for (numWork = gDvmJit.compilerQueueLength,
68 i = gDvmJit.compilerWorkDequeueIndex;
69 numWork > 0;
70 numWork--) {
71 /* Already enqueued */
72 if (gDvmJit.compilerWorkQueue[i++].pc == pc)
73 goto done;
74 /* Wrap around */
75 if (i == COMPILER_WORK_QUEUE_SIZE)
76 i = 0;
77 }
78
Ben Chengccd6c012009-10-15 14:52:45 -070079 CompilerWorkOrder *newOrder =
80 &gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex];
81 newOrder->pc = pc;
82 newOrder->kind = kind;
83 newOrder->info = info;
84 newOrder->result.codeAddress = NULL;
85 newOrder->result.discardResult =
86 (kind == kWorkOrderTraceDebug) ? true : false;
Ben Chengba4fc8b2009-06-01 13:00:29 -070087 gDvmJit.compilerWorkEnqueueIndex++;
88 if (gDvmJit.compilerWorkEnqueueIndex == COMPILER_WORK_QUEUE_SIZE)
89 gDvmJit.compilerWorkEnqueueIndex = 0;
90 gDvmJit.compilerQueueLength++;
91 cc = pthread_cond_signal(&gDvmJit.compilerQueueActivity);
92 assert(cc == 0);
93
94done:
95 dvmUnlockMutex(&gDvmJit.compilerLock);
96 return true;
97}
98
99/* Block until queue length is 0 */
100void dvmCompilerDrainQueue(void)
101{
Bill Buzbeed7269912009-11-10 14:31:32 -0800102 int oldStatus = dvmChangeStatus(NULL, THREAD_VMWAIT);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700103 dvmLockMutex(&gDvmJit.compilerLock);
104 while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread) {
105 pthread_cond_wait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock);
106 }
107 dvmUnlockMutex(&gDvmJit.compilerLock);
Bill Buzbeed7269912009-11-10 14:31:32 -0800108 dvmChangeStatus(NULL, oldStatus);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700109}
110
111static void *compilerThreadStart(void *arg)
112{
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700113 dvmChangeStatus(NULL, THREAD_VMWAIT);
114
Ben Chengba4fc8b2009-06-01 13:00:29 -0700115 dvmLockMutex(&gDvmJit.compilerLock);
116 /*
117 * Since the compiler thread will not touch any objects on the heap once
118 * being created, we just fake its state as VMWAIT so that it can be a
119 * bit late when there is suspend request pending.
120 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700121 while (!gDvmJit.haltCompilerThread) {
122 if (workQueueLength() == 0) {
123 int cc;
124 cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
125 assert(cc == 0);
126 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
127 &gDvmJit.compilerLock);
128 continue;
129 } else {
130 do {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700131 CompilerWorkOrder work = workDequeue();
132 dvmUnlockMutex(&gDvmJit.compilerLock);
133 /* Check whether there is a suspend request on me */
134 dvmCheckSuspendPending(NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700135 /* Is JitTable filling up? */
136 if (gDvmJit.jitTableEntriesUsed >
137 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
138 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
139 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700140 if (gDvmJit.haltCompilerThread) {
141 LOGD("Compiler shutdown in progress - discarding request");
142 } else {
Bill Buzbeed7269912009-11-10 14:31:32 -0800143 /* If compilation failed, use interpret-template */
144 if (!dvmCompilerDoWork(&work)) {
145 work.result.codeAddress = gDvmJit.interpretTemplate;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700146 }
Bill Buzbeed7269912009-11-10 14:31:32 -0800147 dvmJitSetCodeAddr(work.pc, work.result.codeAddress,
148 work.result.instructionSet);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700149 }
150 free(work.info);
151 dvmLockMutex(&gDvmJit.compilerLock);
152 } while (workQueueLength() != 0);
153 }
154 }
155 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
156 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Chengef00a852009-06-22 22:53:35 -0700157
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700158 /*
159 * As part of detaching the thread we need to call into Java code to update
160 * the ThreadGroup, and we should not be in VMWAIT state while executing
161 * interpreted code.
162 */
163 dvmChangeStatus(NULL, THREAD_RUNNING);
164
Ben Chengef00a852009-06-22 22:53:35 -0700165 LOGD("Compiler thread shutting down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700166 return NULL;
167}
168
169bool dvmCompilerSetupCodeCache(void)
170{
171 extern void dvmCompilerTemplateStart(void);
172 extern void dmvCompilerTemplateEnd(void);
173
174 /* Allocate the code cache */
175 gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE,
176 PROT_READ | PROT_WRITE | PROT_EXEC,
Dan Bornsteincede69b2009-10-07 17:33:46 -0700177 MAP_PRIVATE | MAP_ANON, -1, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700178 if (gDvmJit.codeCache == MAP_FAILED) {
179 LOGE("Failed to create the code cache: %s\n", strerror(errno));
180 return false;
181 }
182
183 /* Copy the template code into the beginning of the code cache */
184 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
185 (intptr_t) dvmCompilerTemplateStart;
186 memcpy((void *) gDvmJit.codeCache,
187 (void *) dvmCompilerTemplateStart,
188 templateSize);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700189
190 gDvmJit.templateSize = templateSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700191 gDvmJit.codeCacheByteUsed = templateSize;
192
Ben Chengb28d3a82009-10-20 15:57:28 -0700193 /* Only flush the part in the code cache that is being used now */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700194 cacheflush((intptr_t) gDvmJit.codeCache,
Ben Chengb28d3a82009-10-20 15:57:28 -0700195 (intptr_t) gDvmJit.codeCache + templateSize, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700196 return true;
197}
198
199bool dvmCompilerStartup(void)
200{
201 /* Make sure the BBType enum is in sane state */
Bill Buzbee1465db52009-09-23 17:17:35 -0700202 assert(kChainingCellNormal == 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700203
204 /* Architecture-specific chores to initialize */
205 if (!dvmCompilerArchInit())
206 goto fail;
207
208 /*
209 * Setup the code cache if it is not done so already. For apps it should be
210 * done by the Zygote already, but for command-line dalvikvm invocation we
211 * need to do it here.
212 */
213 if (gDvmJit.codeCache == NULL) {
214 if (!dvmCompilerSetupCodeCache())
215 goto fail;
216 }
217
218 /* Allocate the initial arena block */
219 if (dvmCompilerHeapInit() == false) {
220 goto fail;
221 }
222
223 dvmInitMutex(&gDvmJit.compilerLock);
224 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
225 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
226
227 dvmLockMutex(&gDvmJit.compilerLock);
228
229 gDvmJit.haltCompilerThread = false;
230
231 /* Reset the work queue */
232 memset(gDvmJit.compilerWorkQueue, 0,
233 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
234 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
235 gDvmJit.compilerQueueLength = 0;
236 gDvmJit.compilerHighWater =
237 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
238
239 assert(gDvmJit.compilerHighWater < COMPILER_WORK_QUEUE_SIZE);
240 if (!dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
241 compilerThreadStart, NULL)) {
242 dvmUnlockMutex(&gDvmJit.compilerLock);
243 goto fail;
244 }
245
Ben Cheng8b258bf2009-06-24 17:27:07 -0700246 /* Track method-level compilation statistics */
247 gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL);
248
Ben Chengba4fc8b2009-06-01 13:00:29 -0700249 dvmUnlockMutex(&gDvmJit.compilerLock);
250
251 return true;
252
253fail:
254 return false;
255}
256
257void dvmCompilerShutdown(void)
258{
259 void *threadReturn;
260
261 if (gDvmJit.compilerHandle) {
262
263 gDvmJit.haltCompilerThread = true;
264
265 dvmLockMutex(&gDvmJit.compilerLock);
266 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
267 dvmUnlockMutex(&gDvmJit.compilerLock);
268
Ben Chengef00a852009-06-22 22:53:35 -0700269 if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0)
270 LOGW("Compiler thread join failed\n");
271 else
272 LOGD("Compiler thread has shut down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700273 }
274}