blob: 77a2397d3c9015303848d08bb990399a96b0fcc4 [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 "Dalvik.h"
18#include "CompilerInternals.h"
19
20static ArenaMemBlock *arenaHead, *currentArena;
21static int numArenaBlocks;
22
23/* Allocate the initial memory block for arena-based allocation */
24bool dvmCompilerHeapInit(void)
25{
26 assert(arenaHead == NULL);
27 arenaHead =
28 (ArenaMemBlock *) malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE);
29 if (arenaHead == NULL) {
30 LOGE("No memory left to create compiler heap memory\n");
31 return false;
32 }
33 currentArena = arenaHead;
34 currentArena->bytesAllocated = 0;
35 currentArena->next = NULL;
36 numArenaBlocks = 1;
37
38 return true;
39}
40
41/* Arena-based malloc for compilation tasks */
42void * dvmCompilerNew(size_t size, bool zero)
43{
44 size = (size + 3) & ~3;
45retry:
46 /* Normal case - space is available in the current page */
47 if (size + currentArena->bytesAllocated <= ARENA_DEFAULT_SIZE) {
48 void *ptr;
49 ptr = &currentArena->ptr[currentArena->bytesAllocated];
50 currentArena->bytesAllocated += size;
51 if (zero) {
52 memset(ptr, 0, size);
53 }
54 return ptr;
55 } else {
56 /*
57 * See if there are previously allocated arena blocks before the last
58 * reset
59 */
60 if (currentArena->next) {
61 currentArena = currentArena->next;
62 goto retry;
63 }
64 /*
65 * If we allocate really large variable-sized data structures that
66 * could go above the limit we need to enhance the allocation
67 * mechanism.
68 */
69 if (size > ARENA_DEFAULT_SIZE) {
70 LOGE("Requesting %d bytes which exceed the maximal size allowed\n",
71 size);
Ben Cheng6c10a972009-10-29 14:39:18 -070072 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -070073 }
74 /* Time to allocate a new arena */
75 ArenaMemBlock *newArena = (ArenaMemBlock *)
76 malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE);
77 newArena->bytesAllocated = 0;
78 newArena->next = NULL;
79 currentArena->next = newArena;
80 currentArena = newArena;
81 numArenaBlocks++;
Ben Chenge9695e52009-06-16 16:11:47 -070082 LOGD("Total arena pages for JIT: %d", numArenaBlocks);
Ben Chengba4fc8b2009-06-01 13:00:29 -070083 goto retry;
84 }
85 return NULL;
86}
87
88/* Reclaim all the arena blocks allocated so far */
89void dvmCompilerArenaReset(void)
90{
91 ArenaMemBlock *block;
92
93 for (block = arenaHead; block; block = block->next) {
94 block->bytesAllocated = 0;
95 }
96 currentArena = arenaHead;
97}
98
99/* Growable List initialization */
100void dvmInitGrowableList(GrowableList *gList, size_t initLength)
101{
102 gList->numAllocated = initLength;
103 gList->numUsed = 0;
104 gList->elemList = (void **) dvmCompilerNew(sizeof(void *) * initLength,
105 true);
106}
107
108/* Expand the capacity of a growable list */
109static void expandGrowableList(GrowableList *gList)
110{
111 int newLength = gList->numAllocated;
112 if (newLength < 128) {
113 newLength <<= 1;
114 } else {
115 newLength += 128;
116 }
117 void *newArray = dvmCompilerNew(sizeof(void *) * newLength, true);
118 memcpy(newArray, gList->elemList, sizeof(void *) * gList->numAllocated);
119 gList->numAllocated = newLength;
120 gList->elemList = newArray;
121}
122
123/* Insert a new element into the growable list */
124void dvmInsertGrowableList(GrowableList *gList, void *elem)
125{
126 if (gList->numUsed == gList->numAllocated) {
127 expandGrowableList(gList);
128 }
129 gList->elemList[gList->numUsed++] = elem;
130}
131
132/* Debug Utility - dump a compilation unit */
133void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit)
134{
135 int i;
136 BasicBlock *bb;
137 LOGD("%d blocks in total\n", cUnit->numBlocks);
138
139 for (i = 0; i < cUnit->numBlocks; i++) {
140 bb = cUnit->blockList[i];
141 LOGD("Block %d (insn %04x - %04x%s)\n",
142 bb->id, bb->startOffset,
143 bb->lastMIRInsn ? bb->lastMIRInsn->offset : bb->startOffset,
144 bb->lastMIRInsn ? "" : " empty");
145 if (bb->taken) {
146 LOGD(" Taken branch: block %d (%04x)\n",
147 bb->taken->id, bb->taken->startOffset);
148 }
149 if (bb->fallThrough) {
150 LOGD(" Fallthrough : block %d (%04x)\n",
151 bb->fallThrough->id, bb->fallThrough->startOffset);
152 }
153 }
154}
155
156/*
Ben Cheng8b258bf2009-06-24 17:27:07 -0700157 * dvmHashForeach callback.
158 */
159static int dumpMethodStats(void *compilerMethodStats, void *totalMethodStats)
160{
161 CompilerMethodStats *methodStats =
162 (CompilerMethodStats *) compilerMethodStats;
163 CompilerMethodStats *totalStats =
164 (CompilerMethodStats *) totalMethodStats;
165 const Method *method = methodStats->method;
166
167 totalStats->dalvikSize += methodStats->dalvikSize;
168 totalStats->compiledDalvikSize += methodStats->compiledDalvikSize;
169 totalStats->nativeSize += methodStats->nativeSize;
170
Ben Chenge80cd942009-07-17 15:54:23 -0700171 /* Enable the following when fine-tuning the JIT performance */
172#if 0
Ben Cheng8b258bf2009-06-24 17:27:07 -0700173 int limit = (methodStats->dalvikSize >> 2) * 3;
174
175 /* If over 3/4 of the Dalvik code is compiled, print something */
176 if (methodStats->compiledDalvikSize >= limit) {
177 LOGD("Method stats: %s%s, %d/%d (compiled/total Dalvik), %d (native)",
178 method->clazz->descriptor, method->name,
179 methodStats->compiledDalvikSize,
180 methodStats->dalvikSize,
181 methodStats->nativeSize);
182 }
Ben Chenge80cd942009-07-17 15:54:23 -0700183#endif
Ben Cheng8b258bf2009-06-24 17:27:07 -0700184 return 0;
185}
186
187/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700188 * Dump the current stats of the compiler, including number of bytes used in
189 * the code cache, arena size, and work queue length, and various JIT stats.
190 */
191void dvmCompilerDumpStats(void)
192{
Ben Cheng8b258bf2009-06-24 17:27:07 -0700193 CompilerMethodStats totalMethodStats;
194
195 memset(&totalMethodStats, 0, sizeof(CompilerMethodStats));
196 LOGD("%d compilations using %d + %d bytes",
197 gDvmJit.numCompilations,
198 gDvmJit.templateSize,
199 gDvmJit.codeCacheByteUsed - gDvmJit.templateSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700200 LOGD("Compiler arena uses %d blocks (%d bytes each)",
201 numArenaBlocks, ARENA_DEFAULT_SIZE);
202 LOGD("Compiler work queue length is %d/%d", gDvmJit.compilerQueueLength,
203 gDvmJit.compilerMaxQueued);
204 dvmJitStats();
205 dvmCompilerArchDump();
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700206 if (gDvmJit.methodStatsTable) {
207 dvmHashForeach(gDvmJit.methodStatsTable, dumpMethodStats,
208 &totalMethodStats);
209 }
Ben Cheng8b258bf2009-06-24 17:27:07 -0700210 LOGD("Code size stats: %d/%d (compiled/total Dalvik), %d (native)",
211 totalMethodStats.compiledDalvikSize,
212 totalMethodStats.dalvikSize,
213 totalMethodStats.nativeSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700214}
Ben Cheng4238ec22009-08-24 16:32:22 -0700215
216/*
217 * Allocate a bit vector with enough space to hold at least the specified
218 * number of bits.
219 *
220 * NOTE: this is the sister implementation of dvmAllocBitVector. In this version
221 * memory is allocated from the compiler arena.
222 */
223BitVector* dvmCompilerAllocBitVector(int startBits, bool expandable)
224{
225 BitVector* bv;
226 int count;
227
228 assert(sizeof(bv->storage[0]) == 4); /* assuming 32-bit units */
229 assert(startBits >= 0);
230
231 bv = (BitVector*) dvmCompilerNew(sizeof(BitVector), false);
232
233 count = (startBits + 31) >> 5;
234
235 bv->storageSize = count;
236 bv->expandable = expandable;
237 bv->storage = (u4*) dvmCompilerNew(count * sizeof(u4), true);
238 return bv;
239}
240
241/*
242 * Mark the specified bit as "set".
243 *
244 * Returns "false" if the bit is outside the range of the vector and we're
245 * not allowed to expand.
246 *
247 * NOTE: this is the sister implementation of dvmSetBit. In this version
248 * memory is allocated from the compiler arena.
249 */
250bool dvmCompilerSetBit(BitVector *pBits, int num)
251{
252 assert(num >= 0);
253 if (num >= pBits->storageSize * (int)sizeof(u4) * 8) {
254 if (!pBits->expandable)
255 return false;
256
257 int newSize = (num + 31) >> 5;
258 assert(newSize > pBits->storageSize);
259 u4 *newStorage = dvmCompilerNew(newSize * sizeof(u4), false);
260 memcpy(newStorage, pBits->storage, pBits->storageSize * sizeof(u4));
261 memset(&newStorage[pBits->storageSize], 0,
262 (newSize - pBits->storageSize) * sizeof(u4));
263 pBits->storage = newStorage;
264 pBits->storageSize = newSize;
265 }
266
267 pBits->storage[num >> 5] |= 1 << (num & 0x1f);
268 return true;
269}
270
271/* FIXME */
272void dvmDebugBitVector(char *msg, const BitVector *bv, int length)
273{
274 int i;
275
276 LOGE("%s", msg);
277 for (i = 0; i < length; i++) {
278 if (dvmIsBitSet(bv, i)) {
279 LOGE("Bit %d is set", i);
280 }
281 }
282}