blob: 0b5c2116c2de89227d900a5823ea1a30da6f7d6f [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 {
121 void *compiledCodePtr;
122 CompilerWorkOrder work = workDequeue();
123 dvmUnlockMutex(&gDvmJit.compilerLock);
124 /* Check whether there is a suspend request on me */
125 dvmCheckSuspendPending(NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700126 /* Is JitTable filling up? */
127 if (gDvmJit.jitTableEntriesUsed >
128 (gDvmJit.jitTableSize - gDvmJit.jitTableSize/4)) {
129 dvmJitResizeJitTable(gDvmJit.jitTableSize * 2);
130 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700131 if (gDvmJit.haltCompilerThread) {
132 LOGD("Compiler shutdown in progress - discarding request");
133 } else {
134 compiledCodePtr = dvmCompilerDoWork(&work);
135 /* Compilation is successful */
136 if (compiledCodePtr) {
137 dvmJitSetCodeAddr(work.pc, compiledCodePtr);
138 }
139 }
140 free(work.info);
141 dvmLockMutex(&gDvmJit.compilerLock);
142 } while (workQueueLength() != 0);
143 }
144 }
145 pthread_cond_signal(&gDvmJit.compilerQueueEmpty);
146 dvmUnlockMutex(&gDvmJit.compilerLock);
147 return NULL;
148}
149
150bool dvmCompilerSetupCodeCache(void)
151{
152 extern void dvmCompilerTemplateStart(void);
153 extern void dmvCompilerTemplateEnd(void);
154
155 /* Allocate the code cache */
156 gDvmJit.codeCache = mmap(0, CODE_CACHE_SIZE,
157 PROT_READ | PROT_WRITE | PROT_EXEC,
158 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
159 if (gDvmJit.codeCache == MAP_FAILED) {
160 LOGE("Failed to create the code cache: %s\n", strerror(errno));
161 return false;
162 }
163
164 /* Copy the template code into the beginning of the code cache */
165 int templateSize = (intptr_t) dmvCompilerTemplateEnd -
166 (intptr_t) dvmCompilerTemplateStart;
167 memcpy((void *) gDvmJit.codeCache,
168 (void *) dvmCompilerTemplateStart,
169 templateSize);
170 gDvmJit.codeCacheByteUsed = templateSize;
171
172 /* Flush dcache and invalidate the icache to maintain coherence */
173 cacheflush((intptr_t) gDvmJit.codeCache,
174 (intptr_t) gDvmJit.codeCache + CODE_CACHE_SIZE, 0);
175 return true;
176}
177
178bool dvmCompilerStartup(void)
179{
180 /* Make sure the BBType enum is in sane state */
Ben Cheng1efc9c52009-06-08 18:25:27 -0700181 assert(CHAINING_CELL_NORMAL == 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700182
183 /* Architecture-specific chores to initialize */
184 if (!dvmCompilerArchInit())
185 goto fail;
186
187 /*
188 * Setup the code cache if it is not done so already. For apps it should be
189 * done by the Zygote already, but for command-line dalvikvm invocation we
190 * need to do it here.
191 */
192 if (gDvmJit.codeCache == NULL) {
193 if (!dvmCompilerSetupCodeCache())
194 goto fail;
195 }
196
197 /* Allocate the initial arena block */
198 if (dvmCompilerHeapInit() == false) {
199 goto fail;
200 }
201
202 dvmInitMutex(&gDvmJit.compilerLock);
203 pthread_cond_init(&gDvmJit.compilerQueueActivity, NULL);
204 pthread_cond_init(&gDvmJit.compilerQueueEmpty, NULL);
205
206 dvmLockMutex(&gDvmJit.compilerLock);
207
208 gDvmJit.haltCompilerThread = false;
209
210 /* Reset the work queue */
211 memset(gDvmJit.compilerWorkQueue, 0,
212 sizeof(CompilerWorkOrder) * COMPILER_WORK_QUEUE_SIZE);
213 gDvmJit.compilerWorkEnqueueIndex = gDvmJit.compilerWorkDequeueIndex = 0;
214 gDvmJit.compilerQueueLength = 0;
215 gDvmJit.compilerHighWater =
216 COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
217
218 assert(gDvmJit.compilerHighWater < COMPILER_WORK_QUEUE_SIZE);
219 if (!dvmCreateInternalThread(&gDvmJit.compilerHandle, "Compiler",
220 compilerThreadStart, NULL)) {
221 dvmUnlockMutex(&gDvmJit.compilerLock);
222 goto fail;
223 }
224
225 dvmUnlockMutex(&gDvmJit.compilerLock);
226
227 return true;
228
229fail:
230 return false;
231}
232
233void dvmCompilerShutdown(void)
234{
235 void *threadReturn;
236
237 if (gDvmJit.compilerHandle) {
238
239 gDvmJit.haltCompilerThread = true;
240
241 dvmLockMutex(&gDvmJit.compilerLock);
242 pthread_cond_signal(&gDvmJit.compilerQueueActivity);
243 dvmUnlockMutex(&gDvmJit.compilerLock);
244
245 pthread_join(gDvmJit.compilerHandle, &threadReturn);
246 }
247}