blob: 21d736768f88a55e90c6912b228c0f8273ea18d1 [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--;
41
42 /* Remember the high water mark of the queue length */
43 if (gDvmJit.compilerQueueLength > gDvmJit.compilerMaxQueued)
44 gDvmJit.compilerMaxQueued = gDvmJit.compilerQueueLength;
45
46 return work;
47}
48
49bool dvmCompilerWorkEnqueue(const u2 *pc, WorkOrderKind kind, void* info)
50{
51 int cc;
52 int i;
53 int numWork;
54
55 dvmLockMutex(&gDvmJit.compilerLock);
56
57 /* Queue full */
58 if (gDvmJit.compilerQueueLength == COMPILER_WORK_QUEUE_SIZE ||
59 gDvmJit.codeCacheFull == true) {
60 dvmUnlockMutex(&gDvmJit.compilerLock);
61 return false;
62 }
63
64 for (numWork = gDvmJit.compilerQueueLength,
65 i = gDvmJit.compilerWorkDequeueIndex;
66 numWork > 0;
67 numWork--) {
68 /* Already enqueued */
69 if (gDvmJit.compilerWorkQueue[i++].pc == pc)
70 goto done;
71 /* Wrap around */
72 if (i == COMPILER_WORK_QUEUE_SIZE)
73 i = 0;
74 }
75
Ben Chengccd6c012009-10-15 14:52:45 -070076 CompilerWorkOrder *newOrder =
77 &gDvmJit.compilerWorkQueue[gDvmJit.compilerWorkEnqueueIndex];
78 newOrder->pc = pc;
79 newOrder->kind = kind;
80 newOrder->info = info;
81 newOrder->result.codeAddress = NULL;
82 newOrder->result.discardResult =
83 (kind == kWorkOrderTraceDebug) ? true : false;
Ben Chengba4fc8b2009-06-01 13:00:29 -070084 gDvmJit.compilerWorkEnqueueIndex++;
85 if (gDvmJit.compilerWorkEnqueueIndex == COMPILER_WORK_QUEUE_SIZE)
86 gDvmJit.compilerWorkEnqueueIndex = 0;
87 gDvmJit.compilerQueueLength++;
88 cc = pthread_cond_signal(&gDvmJit.compilerQueueActivity);
89 assert(cc == 0);
90
91done:
92 dvmUnlockMutex(&gDvmJit.compilerLock);
93 return true;
94}
95
96/* Block until queue length is 0 */
97void dvmCompilerDrainQueue(void)
98{
99 dvmLockMutex(&gDvmJit.compilerLock);
100 while (workQueueLength() != 0 && !gDvmJit.haltCompilerThread) {
101 pthread_cond_wait(&gDvmJit.compilerQueueEmpty, &gDvmJit.compilerLock);
102 }
103 dvmUnlockMutex(&gDvmJit.compilerLock);
104}
105
106static void *compilerThreadStart(void *arg)
107{
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700108 dvmChangeStatus(NULL, THREAD_VMWAIT);
109
Ben Chengba4fc8b2009-06-01 13:00:29 -0700110 dvmLockMutex(&gDvmJit.compilerLock);
111 /*
112 * Since the compiler thread will not touch any objects on the heap once
113 * being created, we just fake its state as VMWAIT so that it can be a
114 * bit late when there is suspend request pending.
115 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700116 while (!gDvmJit.haltCompilerThread) {
117 if (workQueueLength() == 0) {
118 int cc;
119 cc = pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
120 assert(cc == 0);
121 pthread_cond_wait(&gDvmJit.compilerQueueActivity,
122 &gDvmJit.compilerLock);
123 continue;
124 } else {
125 do {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700126 CompilerWorkOrder work = workDequeue();
127 dvmUnlockMutex(&gDvmJit.compilerLock);
128 /* Check whether there is a suspend request on me */
129 dvmCheckSuspendPending(NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700130 /* Is JitTable filling up? */
131 if (gDvmJit.jitTableEntriesUsed >
132 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
133 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
134 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135 if (gDvmJit.haltCompilerThread) {
136 LOGD("Compiler shutdown in progress - discarding request");
137 } else {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138 /* Compilation is successful */
Bill Buzbee716f1202009-07-23 13:22:09 -0700139 if (dvmCompilerDoWork(&work)) {
140 dvmJitSetCodeAddr(work.pc, work.result.codeAddress,
141 work.result.instructionSet);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700142 }
143 }
144 free(work.info);
145 dvmLockMutex(&gDvmJit.compilerLock);
146 } while (workQueueLength() != 0);
147 }
148 }
149 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
150 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Chengef00a852009-06-22 22:53:35 -0700151
Ben Cheng5ccdf0b2009-10-08 16:09:49 -0700152 /*
153 * As part of detaching the thread we need to call into Java code to update
154 * the ThreadGroup, and we should not be in VMWAIT state while executing
155 * interpreted code.
156 */
157 dvmChangeStatus(NULL, THREAD_RUNNING);
158
Ben Chengef00a852009-06-22 22:53:35 -0700159 LOGD("Compiler thread shutting down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700160 return NULL;
161}
162
163bool dvmCompilerSetupCodeCache(void)
164{
165 extern void dvmCompilerTemplateStart(void);
166 extern void dmvCompilerTemplateEnd(void);
167
168 /* Allocate the code cache */
169 gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE,
170 PROT_READ | PROT_WRITE | PROT_EXEC,
Dan Bornsteincede69b2009-10-07 17:33:46 -0700171 MAP_PRIVATE | MAP_ANON, -1, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700172 if (gDvmJit.codeCache == MAP_FAILED) {
173 LOGE("Failed to create the code cache: %s\n", strerror(errno));
174 return false;
175 }
176
177 /* Copy the template code into the beginning of the code cache */
178 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
179 (intptr_t) dvmCompilerTemplateStart;
180 memcpy((void *) gDvmJit.codeCache,
181 (void *) dvmCompilerTemplateStart,
182 templateSize);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700183
184 gDvmJit.templateSize = templateSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700185 gDvmJit.codeCacheByteUsed = templateSize;
186
Ben Chengb28d3a82009-10-20 15:57:28 -0700187 /* Only flush the part in the code cache that is being used now */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700188 cacheflush((intptr_t) gDvmJit.codeCache,
Ben Chengb28d3a82009-10-20 15:57:28 -0700189 (intptr_t) gDvmJit.codeCache + templateSize, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700190 return true;
191}
192
193bool dvmCompilerStartup(void)
194{
195 /* Make sure the BBType enum is in sane state */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700196 assert(CHAINING_CELL_NORMAL == 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700197
198 /* Architecture-specific chores to initialize */
199 if (!dvmCompilerArchInit())
200 goto fail;
201
202 /*
203 * Setup the code cache if it is not done so already. For apps it should be
204 * done by the Zygote already, but for command-line dalvikvm invocation we
205 * need to do it here.
206 */
207 if (gDvmJit.codeCache == NULL) {
208 if (!dvmCompilerSetupCodeCache())
209 goto fail;
210 }
211
212 /* Allocate the initial arena block */
213 if (dvmCompilerHeapInit() == false) {
214 goto fail;
215 }
216
217 dvmInitMutex(&gDvmJit.compilerLock);
218 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
219 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
220
221 dvmLockMutex(&gDvmJit.compilerLock);
222
223 gDvmJit.haltCompilerThread = false;
224
225 /* Reset the work queue */
226 memset(gDvmJit.compilerWorkQueue, 0,
227 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
228 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
229 gDvmJit.compilerQueueLength = 0;
230 gDvmJit.compilerHighWater =
231 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
232
233 assert(gDvmJit.compilerHighWater < COMPILER_WORK_QUEUE_SIZE);
234 if (!dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
235 compilerThreadStart, NULL)) {
236 dvmUnlockMutex(&gDvmJit.compilerLock);
237 goto fail;
238 }
239
Ben Cheng8b258bf2009-06-24 17:27:07 -0700240 /* Track method-level compilation statistics */
241 gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL);
242
Ben Chengba4fc8b2009-06-01 13:00:29 -0700243 dvmUnlockMutex(&gDvmJit.compilerLock);
244
245 return true;
246
247fail:
248 return false;
249}
250
251void dvmCompilerShutdown(void)
252{
253 void *threadReturn;
254
255 if (gDvmJit.compilerHandle) {
256
257 gDvmJit.haltCompilerThread = true;
258
259 dvmLockMutex(&gDvmJit.compilerLock);
260 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
261 dvmUnlockMutex(&gDvmJit.compilerLock);
262
Ben Chengef00a852009-06-22 22:53:35 -0700263 if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0)
264 LOGW("Compiler thread join failed\n");
265 else
266 LOGD("Compiler thread has shut down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700267 }
268}