blob: 203a08020cb66c2c5a4cea162b4d7974dd83e3b9 [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
24
25static inline bool workQueueLength(void)
26{
27 return gDvmJit.compilerQueueLength;
28}
29
30static 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
50bool 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
87done:
88 dvmUnlockMutex(&gDvmJit.compilerLock);
89 return true;
90}
91
92/* Block until queue length is 0 */
93void 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
102static 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 {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700121 CompilerWorkOrder work = workDequeue();
122 dvmUnlockMutex(&gDvmJit.compilerLock);
123 /* Check whether there is a suspend request on me */
124 dvmCheckSuspendPending(NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700125 /* Is JitTable filling up? */
126 if (gDvmJit.jitTableEntriesUsed >
127 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
128 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
129 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700130 if (gDvmJit.haltCompilerThread) {
131 LOGD("Compiler shutdown in progress - discarding request");
132 } else {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700133 /* Compilation is successful */
Bill Buzbee716f1202009-07-23 13:22:09 -0700134 if (dvmCompilerDoWork(&work)) {
135 dvmJitSetCodeAddr(work.pc, work.result.codeAddress,
136 work.result.instructionSet);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700137 }
138 }
139 free(work.info);
140 dvmLockMutex(&gDvmJit.compilerLock);
141 } while (workQueueLength() != 0);
142 }
143 }
144 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
145 dvmUnlockMutex(&gDvmJit.compilerLock);
Ben Chengef00a852009-06-22 22:53:35 -0700146
147 LOGD("Compiler thread shutting down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700148 return NULL;
149}
150
151bool dvmCompilerSetupCodeCache(void)
152{
153 extern void dvmCompilerTemplateStart(void);
154 extern void dmvCompilerTemplateEnd(void);
155
156 /* Allocate the code cache */
157 gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE,
158 PROT_READ | PROT_WRITE | PROT_EXEC,
159 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
160 if (gDvmJit.codeCache == MAP_FAILED) {
161 LOGE("Failed to create the code cache: %s\n", strerror(errno));
162 return false;
163 }
164
165 /* Copy the template code into the beginning of the code cache */
166 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
167 (intptr_t) dvmCompilerTemplateStart;
168 memcpy((void *) gDvmJit.codeCache,
169 (void *) dvmCompilerTemplateStart,
170 templateSize);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700171
172 gDvmJit.templateSize = templateSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700173 gDvmJit.codeCacheByteUsed = templateSize;
174
175 /* Flush dcache and invalidate the icache to maintain coherence */
176 cacheflush((intptr_t) gDvmJit.codeCache,
177 (intptr_t) gDvmJit.codeCache + CODE_CACHE_SIZE, 0);
178 return true;
179}
180
181bool dvmCompilerStartup(void)
182{
183 /* Make sure the BBType enum is in sane state */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700184 assert(CHAINING_CELL_NORMAL == 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700185
186 /* Architecture-specific chores to initialize */
187 if (!dvmCompilerArchInit())
188 goto fail;
189
190 /*
191 * Setup the code cache if it is not done so already. For apps it should be
192 * done by the Zygote already, but for command-line dalvikvm invocation we
193 * need to do it here.
194 */
195 if (gDvmJit.codeCache == NULL) {
196 if (!dvmCompilerSetupCodeCache())
197 goto fail;
198 }
199
200 /* Allocate the initial arena block */
201 if (dvmCompilerHeapInit() == false) {
202 goto fail;
203 }
204
205 dvmInitMutex(&gDvmJit.compilerLock);
206 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
207 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
208
209 dvmLockMutex(&gDvmJit.compilerLock);
210
211 gDvmJit.haltCompilerThread = false;
212
213 /* Reset the work queue */
214 memset(gDvmJit.compilerWorkQueue, 0,
215 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
216 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
217 gDvmJit.compilerQueueLength = 0;
218 gDvmJit.compilerHighWater =
219 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
220
221 assert(gDvmJit.compilerHighWater < COMPILER_WORK_QUEUE_SIZE);
222 if (!dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
223 compilerThreadStart, NULL)) {
224 dvmUnlockMutex(&gDvmJit.compilerLock);
225 goto fail;
226 }
227
Ben Cheng8b258bf2009-06-24 17:27:07 -0700228 /* Track method-level compilation statistics */
229 gDvmJit.methodStatsTable = dvmHashTableCreate(32, NULL);
230
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231 dvmUnlockMutex(&gDvmJit.compilerLock);
232
233 return true;
234
235fail:
236 return false;
237}
238
239void dvmCompilerShutdown(void)
240{
241 void *threadReturn;
242
243 if (gDvmJit.compilerHandle) {
244
245 gDvmJit.haltCompilerThread = true;
246
247 dvmLockMutex(&gDvmJit.compilerLock);
248 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
249 dvmUnlockMutex(&gDvmJit.compilerLock);
250
Ben Chengef00a852009-06-22 22:53:35 -0700251 if (pthread_join(gDvmJit.compilerHandle, &threadReturn) != 0)
252 LOGW("Compiler thread join failed\n");
253 else
254 LOGD("Compiler thread has shut down\n");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700255 }
256}