blob: 936bbc8fbb814ff28710e5c2f91cf0c4371198e6 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2008 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#ifdef WITH_JIT
17
18/*
19 * Target independent portion of Android's Jit
20 */
21
22#include "Dalvik.h"
23#include "Jit.h"
24
Dan Bornsteindf4daaf2010-12-01 14:23:44 -080025#include "libdex/DexOpcodes.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070026#include <unistd.h>
27#include <pthread.h>
28#include <sys/time.h>
29#include <signal.h>
30#include "compiler/Compiler.h"
Bill Buzbee6e963e12009-06-17 16:56:19 -070031#include "compiler/CompilerUtility.h"
32#include "compiler/CompilerIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070033#include <errno.h>
34
Jeff Hao97319a82009-08-12 16:57:15 -070035#if defined(WITH_SELF_VERIFICATION)
36/* Allocate space for per-thread ShadowSpace data structures */
37void* dvmSelfVerificationShadowSpaceAlloc(Thread* self)
38{
39 self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace));
40 if (self->shadowSpace == NULL)
41 return NULL;
42
43 self->shadowSpace->registerSpaceSize = REG_SPACE;
44 self->shadowSpace->registerSpace =
45 (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int));
46
47 return self->shadowSpace->registerSpace;
48}
49
50/* Free per-thread ShadowSpace data structures */
51void dvmSelfVerificationShadowSpaceFree(Thread* self)
52{
53 free(self->shadowSpace->registerSpace);
54 free(self->shadowSpace);
55}
56
57/*
buzbee9f601a92011-02-11 17:48:20 -080058 * Save out PC, FP, thread state, and registers to shadow space.
Jeff Hao97319a82009-08-12 16:57:15 -070059 * Return a pointer to the shadow space for JIT to use.
buzbee9f601a92011-02-11 17:48:20 -080060 *
61 * The set of saved state from the Thread structure is:
62 * pc (Dalvik PC)
63 * fp (Dalvik FP)
64 * retval
65 * method
66 * methodClassDex
67 * interpStackEnd
Jeff Hao97319a82009-08-12 16:57:15 -070068 */
buzbee9f601a92011-02-11 17:48:20 -080069void* dvmSelfVerificationSaveState(const u2* pc, u4* fp,
70 Thread* self, int targetTrace)
Jeff Hao97319a82009-08-12 16:57:15 -070071{
Jeff Hao97319a82009-08-12 16:57:15 -070072 ShadowSpace *shadowSpace = self->shadowSpace;
buzbee9f601a92011-02-11 17:48:20 -080073 unsigned preBytes = self->interpSave.method->outsSize*4 +
74 sizeof(StackSaveArea);
75 unsigned postBytes = self->interpSave.method->registersSize*4;
Jeff Hao97319a82009-08-12 16:57:15 -070076
77 //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x",
78 // self->threadId, (int)pc, (int)fp);
79
80 if (shadowSpace->selfVerificationState != kSVSIdle) {
81 LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d",
82 self->threadId, shadowSpace->selfVerificationState);
83 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -070084 LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
Jeff Hao97319a82009-08-12 16:57:15 -070085 }
86 shadowSpace->selfVerificationState = kSVSStart;
87
88 // Dynamically grow shadow register space if necessary
Ben Cheng11d8f142010-03-24 15:24:19 -070089 if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) {
Jeff Hao97319a82009-08-12 16:57:15 -070090 free(shadowSpace->registerSpace);
Ben Cheng11d8f142010-03-24 15:24:19 -070091 shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4);
Jeff Hao97319a82009-08-12 16:57:15 -070092 shadowSpace->registerSpace =
Ben Cheng11d8f142010-03-24 15:24:19 -070093 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4));
Jeff Hao97319a82009-08-12 16:57:15 -070094 }
95
96 // Remember original state
97 shadowSpace->startPC = pc;
98 shadowSpace->fp = fp;
buzbee9f601a92011-02-11 17:48:20 -080099 shadowSpace->retval = self->retval;
100 shadowSpace->interpStackEnd = self->interpStackEnd;
101
Ben Chengccd6c012009-10-15 14:52:45 -0700102 /*
103 * Store the original method here in case the trace ends with a
104 * return/invoke, the last method.
105 */
buzbee9f601a92011-02-11 17:48:20 -0800106 shadowSpace->method = self->interpSave.method;
107 shadowSpace->methodClassDex = self->interpSave.methodClassDex;
108
Jeff Hao97319a82009-08-12 16:57:15 -0700109 shadowSpace->shadowFP = shadowSpace->registerSpace +
110 shadowSpace->registerSpaceSize - postBytes/4;
111
buzbee9f601a92011-02-11 17:48:20 -0800112 self->interpSave.fp = (u4*)shadowSpace->shadowFP;
113 self->interpStackEnd = (u1*)shadowSpace->registerSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700114
115 // Create a copy of the stack
116 memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
117 preBytes+postBytes);
118
119 // Setup the shadowed heap space
120 shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
121
122 // Reset trace length
123 shadowSpace->traceLength = 0;
124
125 return shadowSpace;
126}
127
128/*
129 * Save ending PC, FP and compiled code exit point to shadow space.
130 * Return a pointer to the shadow space for JIT to restore state.
131 */
buzbee9f601a92011-02-11 17:48:20 -0800132void* dvmSelfVerificationRestoreState(const u2* pc, u4* fp,
133 SelfVerificationState exitState,
134 Thread* self)
Jeff Hao97319a82009-08-12 16:57:15 -0700135{
Jeff Hao97319a82009-08-12 16:57:15 -0700136 ShadowSpace *shadowSpace = self->shadowSpace;
137 shadowSpace->endPC = pc;
138 shadowSpace->endShadowFP = fp;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700139 shadowSpace->jitExitState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700140
141 //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
142 // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
143 // (int)pc);
144
145 if (shadowSpace->selfVerificationState != kSVSStart) {
146 LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
147 self->threadId, shadowSpace->selfVerificationState);
148 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700149 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700150 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700151 LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
152 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700153 (int)shadowSpace->endShadowFP);
154 }
155
156 // Special case when punting after a single instruction
Ben Cheng7a2697d2010-06-07 13:44:23 -0700157 if (exitState == kSVSPunt && pc == shadowSpace->startPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700158 shadowSpace->selfVerificationState = kSVSIdle;
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700159 } else if (exitState == kSVSBackwardBranch && pc < shadowSpace->startPC) {
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700160 /*
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700161 * Consider a trace with a backward branch:
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700162 * 1: ..
163 * 2: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700164 * 3: ..
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700165 * 4: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700166 * 5: Goto {1 or 2 or 3 or 4}
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700167 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700168 * If there instruction 5 goes to 1 and there is no single-step
169 * instruction in the loop, pc is equal to shadowSpace->startPC and
170 * we will honor the backward branch condition.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700171 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700172 * If the single-step instruction is outside the loop, then after
173 * resuming in the trace the startPC will be less than pc so we will
174 * also honor the backward branch condition.
175 *
176 * If the single-step is inside the loop, we won't hit the same endPC
177 * twice when the interpreter is re-executing the trace so we want to
178 * cancel the backward branch condition. In this case it can be
179 * detected as the endPC (ie pc) will be less than startPC.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700180 */
181 shadowSpace->selfVerificationState = kSVSNormal;
Jeff Hao97319a82009-08-12 16:57:15 -0700182 } else {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700183 shadowSpace->selfVerificationState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700184 }
185
buzbee9f601a92011-02-11 17:48:20 -0800186 /* Restore state before returning */
187 self->interpSave.pc = shadowSpace->startPC;
188 self->interpSave.fp = shadowSpace->fp;
189 self->interpSave.method = shadowSpace->method;
190 self->interpSave.methodClassDex = shadowSpace->methodClassDex;
191 self->retval = shadowSpace->retval;
192 self->interpStackEnd = shadowSpace->interpStackEnd;
193
Jeff Hao97319a82009-08-12 16:57:15 -0700194 return shadowSpace;
195}
196
197/* Print contents of virtual registers */
Ben Chengccd6c012009-10-15 14:52:45 -0700198static void selfVerificationPrintRegisters(int* addr, int* addrRef,
199 int numWords)
Jeff Hao97319a82009-08-12 16:57:15 -0700200{
201 int i;
202 for (i = 0; i < numWords; i++) {
Ben Chengccd6c012009-10-15 14:52:45 -0700203 LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
Jeff Hao97319a82009-08-12 16:57:15 -0700204 }
205}
206
207/* Print values maintained in shadowSpace */
208static void selfVerificationDumpState(const u2* pc, Thread* self)
209{
210 ShadowSpace* shadowSpace = self->shadowSpace;
211 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
212 int frameBytes = (int) shadowSpace->registerSpace +
213 shadowSpace->registerSpaceSize*4 -
214 (int) shadowSpace->shadowFP;
215 int localRegs = 0;
216 int frameBytes2 = 0;
buzbee9f601a92011-02-11 17:48:20 -0800217 if ((uintptr_t)self->curFrame < (uintptr_t)shadowSpace->fp) {
Jeff Hao97319a82009-08-12 16:57:15 -0700218 localRegs = (stackSave->method->registersSize -
219 stackSave->method->insSize)*4;
220 frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
221 }
222 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700223 LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
Jeff Hao97319a82009-08-12 16:57:15 -0700224 (int)(pc - stackSave->method->insns));
Ben Chengccd6c012009-10-15 14:52:45 -0700225 LOGD("Class: %s", shadowSpace->method->clazz->descriptor);
226 LOGD("Method: %s", shadowSpace->method->name);
227 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700228 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700229 LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
Jeff Hao97319a82009-08-12 16:57:15 -0700230 (int)self->curFrame);
Ben Chengccd6c012009-10-15 14:52:45 -0700231 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700232 (int)shadowSpace->endShadowFP);
Ben Chengccd6c012009-10-15 14:52:45 -0700233 LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
Jeff Hao97319a82009-08-12 16:57:15 -0700234 localRegs, frameBytes2);
Ben Chengccd6c012009-10-15 14:52:45 -0700235 LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
Jeff Hao97319a82009-08-12 16:57:15 -0700236 shadowSpace->selfVerificationState);
237}
238
239/* Print decoded instructions in the current trace */
240static void selfVerificationDumpTrace(const u2* pc, Thread* self)
241{
242 ShadowSpace* shadowSpace = self->shadowSpace;
243 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700244 int i, addr, offset;
245 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700246
247 LOGD("********** SHADOW TRACE DUMP **********");
248 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700249 addr = shadowSpace->trace[i].addr;
250 offset = (int)((u2*)addr - stackSave->method->insns);
251 decInsn = &(shadowSpace->trace[i].decInsn);
252 /* Not properly decoding instruction, some registers may be garbage */
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700253 LOGD("0x%x: (0x%04x) %s",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800254 addr, offset, dexGetOpcodeName(decInsn->opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700255 }
256}
257
Ben Chengbcdc1de2009-08-21 16:18:46 -0700258/* Code is forced into this spin loop when a divergence is detected */
Ben Chengccd6c012009-10-15 14:52:45 -0700259static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
Ben Chengbcdc1de2009-08-21 16:18:46 -0700260{
Ben Chengccd6c012009-10-15 14:52:45 -0700261 const u2 *startPC = shadowSpace->startPC;
Ben Cheng88a0f972010-02-24 15:00:40 -0800262 JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL);
Ben Chengccd6c012009-10-15 14:52:45 -0700263 if (desc) {
264 dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
Ben Cheng1357e942010-02-10 17:21:39 -0800265 /*
266 * This function effectively terminates the VM right here, so not
267 * freeing the desc pointer when the enqueuing fails is acceptable.
268 */
Ben Chengccd6c012009-10-15 14:52:45 -0700269 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700270 gDvmJit.selfVerificationSpin = true;
271 while(gDvmJit.selfVerificationSpin) sleep(10);
272}
273
buzbee9a3147c2011-03-02 15:43:48 -0800274/*
275 * If here, we're re-interpreting an instruction that was included
276 * in a trace that was just executed. This routine is called for
277 * each instruction in the original trace, and compares state
278 * when it reaches the end point.
279 *
280 * TUNING: the interpretation mechanism now supports a counted
281 * single-step mechanism. If we were to associate an instruction
282 * count with each trace exit, we could just single-step the right
283 * number of cycles and then compare. This would improve detection
284 * of control divergences, as well as (slightly) simplify this code.
285 */
286void dvmCheckSelfVerification(const u2* pc, Thread* self)
Jeff Hao97319a82009-08-12 16:57:15 -0700287{
288 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700289 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700290
291 DecodedInstruction decInsn;
Dan Bornstein54322392010-11-17 14:16:56 -0800292 dexDecodeInstruction(pc, &decInsn);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700293
Jeff Hao97319a82009-08-12 16:57:15 -0700294 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
295 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800296 // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700297
298 if (state == kSVSIdle || state == kSVSStart) {
299 LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
300 self->threadId, state);
301 selfVerificationDumpState(pc, self);
302 selfVerificationDumpTrace(pc, self);
303 }
304
Ben Chengd5adae12010-03-26 17:45:28 -0700305 /*
306 * Skip endPC once when trace has a backward branch. If the SV state is
307 * single step, keep it that way.
308 */
Jeff Hao97319a82009-08-12 16:57:15 -0700309 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
Ben Chengd5adae12010-03-26 17:45:28 -0700310 (state != kSVSBackwardBranch && state != kSVSSingleStep)) {
Jeff Hao97319a82009-08-12 16:57:15 -0700311 shadowSpace->selfVerificationState = kSVSDebugInterp;
312 }
313
314 /* Check that the current pc is the end of the trace */
Ben Chengd5adae12010-03-26 17:45:28 -0700315 if ((state == kSVSDebugInterp || state == kSVSSingleStep) &&
316 pc == shadowSpace->endPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700317
318 shadowSpace->selfVerificationState = kSVSIdle;
319
320 /* Check register space */
321 int frameBytes = (int) shadowSpace->registerSpace +
322 shadowSpace->registerSpaceSize*4 -
323 (int) shadowSpace->shadowFP;
324 if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700325 LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
Jeff Hao97319a82009-08-12 16:57:15 -0700326 selfVerificationDumpState(pc, self);
327 selfVerificationDumpTrace(pc, self);
328 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
329 (int)shadowSpace->fp, frameBytes);
Ben Chengccd6c012009-10-15 14:52:45 -0700330 selfVerificationPrintRegisters((int*)shadowSpace->fp,
331 (int*)shadowSpace->shadowFP,
332 frameBytes/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700333 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
334 (int)shadowSpace->shadowFP, frameBytes);
335 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700336 (int*)shadowSpace->fp,
337 frameBytes/4);
338 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700339 }
340 /* Check new frame if it exists (invokes only) */
buzbee9f601a92011-02-11 17:48:20 -0800341 if ((uintptr_t)self->curFrame < (uintptr_t)shadowSpace->fp) {
Jeff Hao97319a82009-08-12 16:57:15 -0700342 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
343 int localRegs = (stackSave->method->registersSize -
344 stackSave->method->insSize)*4;
345 int frameBytes2 = (int) shadowSpace->fp -
346 (int) self->curFrame - localRegs;
347 if (memcmp(((char*)self->curFrame)+localRegs,
348 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700349 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
Jeff Hao97319a82009-08-12 16:57:15 -0700350 self->threadId);
351 selfVerificationDumpState(pc, self);
352 selfVerificationDumpTrace(pc, self);
353 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
354 (int)self->curFrame, localRegs, frameBytes2);
355 selfVerificationPrintRegisters((int*)self->curFrame,
Ben Chengccd6c012009-10-15 14:52:45 -0700356 (int*)shadowSpace->endShadowFP,
357 (frameBytes2+localRegs)/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700358 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
359 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
360 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700361 (int*)self->curFrame,
362 (frameBytes2+localRegs)/4);
363 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700364 }
365 }
366
367 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700368 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700369 ShadowHeap* heapSpacePtr;
370 for (heapSpacePtr = shadowSpace->heapSpace;
371 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700372 int memData = *((unsigned int*) heapSpacePtr->addr);
373 if (heapSpacePtr->data != memData) {
Ben Chengccd6c012009-10-15 14:52:45 -0700374 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
375 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700376 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700377 selfVerificationDumpState(pc, self);
378 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700379 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700380 }
381 }
Ben Chengccd6c012009-10-15 14:52:45 -0700382 if (memDiff) selfVerificationSpinLoop(shadowSpace);
Ben Chengd5adae12010-03-26 17:45:28 -0700383
buzbee9a3147c2011-03-02 15:43:48 -0800384
Ben Chengd5adae12010-03-26 17:45:28 -0700385 /*
buzbee9a3147c2011-03-02 15:43:48 -0800386 * Success. If this shadowed trace included a single-stepped
387 * instruction, we need to stay in the interpreter for one
388 * more interpretation before resuming.
Ben Chengd5adae12010-03-26 17:45:28 -0700389 */
390 if (state == kSVSSingleStep) {
buzbee9a3147c2011-03-02 15:43:48 -0800391 assert(self->jitResumeNPC != NULL);
392 assert(self->singleStepCount == 0);
393 self->singleStepCount = 1;
394 dvmUpdateInterpBreak(self, kInterpSingleStep, kSubModeNormal,
395 true /* enable */);
Ben Chengd5adae12010-03-26 17:45:28 -0700396 }
buzbee9a3147c2011-03-02 15:43:48 -0800397
398 /*
399 * Switch off shadow replay mode. The next shadowed trace
400 * execution will turn it back on.
401 */
402 dvmUpdateInterpBreak(self, kInterpJitBreak, kSubModeJitSV,
403 false /* disable */);
404 self->jitState = kJitDone;
405 return;
Jeff Hao97319a82009-08-12 16:57:15 -0700406
407 /* If end not been reached, make sure max length not exceeded */
408 } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
409 LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
Ben Chengccd6c012009-10-15 14:52:45 -0700410 LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700411 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
412 selfVerificationDumpState(pc, self);
413 selfVerificationDumpTrace(pc, self);
Ben Chengccd6c012009-10-15 14:52:45 -0700414 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700415
buzbee9a3147c2011-03-02 15:43:48 -0800416 return;
Jeff Hao97319a82009-08-12 16:57:15 -0700417 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700418 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700419 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700420 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700421 shadowSpace->traceLength++;
Jeff Hao97319a82009-08-12 16:57:15 -0700422}
423#endif
424
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425/*
426 * If one of our fixed tables or the translation buffer fills up,
427 * call this routine to avoid wasting cycles on future translation requests.
428 */
429void dvmJitStopTranslationRequests()
430{
431 /*
432 * Note 1: This won't necessarily stop all translation requests, and
433 * operates on a delayed mechanism. Running threads look to the copy
buzbee9f601a92011-02-11 17:48:20 -0800434 * of this value in their private thread structures and won't see
Ben Chengba4fc8b2009-06-01 13:00:29 -0700435 * this change until it is refreshed (which happens on interpreter
436 * entry).
437 * Note 2: This is a one-shot memory leak on this table. Because this is a
438 * permanent off switch for Jit profiling, it is a one-time leak of 1K
439 * bytes, and no further attempt will be made to re-allocate it. Can't
440 * free it because some thread may be holding a reference.
441 */
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800442 gDvmJit.pProfTable = NULL;
buzbee99e3e6e2011-03-29 10:26:07 -0700443 dvmJitUpdateThreadStateAll();
Ben Chengba4fc8b2009-06-01 13:00:29 -0700444}
445
Ben Cheng978738d2010-05-13 13:45:57 -0700446#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700447/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700448void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700449{
Ben Cheng6c10a972009-10-29 14:39:18 -0700450 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700451}
452
453/* Convenience function to increment counter from assembly code */
454void dvmBumpNormal()
455{
Ben Cheng6c10a972009-10-29 14:39:18 -0700456 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700457}
458
459/* Convenience function to increment counter from assembly code */
460void dvmBumpPunt(int from)
461{
Ben Cheng6c10a972009-10-29 14:39:18 -0700462 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700463}
464#endif
465
466/* Dumps debugging & tuning stats to the log */
467void dvmJitStats()
468{
469 int i;
470 int hit;
471 int not_hit;
472 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800473 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700474 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800475 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700476 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700477 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800478 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700479 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800480 if (gDvmJit.pJitEntryTable[i].codeAddress ==
Bill Buzbeebd047242010-05-13 13:02:53 -0700481 dvmCompilerGetInterpretTemplate())
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800482 stubs++;
483 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700484 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700485 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700486 chains++;
487 }
Ben Cheng72621c92010-03-10 13:12:55 -0800488 LOGD("JIT: table size is %d, entries used is %d",
Ben Cheng86717f72010-03-05 15:27:21 -0800489 gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed);
Ben Cheng72621c92010-03-10 13:12:55 -0800490 LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s",
491 hit, not_hit + hit, chains, gDvmJit.threshold,
492 gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
Ben Cheng86717f72010-03-05 15:27:21 -0800493
Ben Cheng978738d2010-05-13 13:45:57 -0700494#if defined(WITH_JIT_TUNING)
495 LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches);
496
Ben Cheng72621c92010-03-10 13:12:55 -0800497 LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
498 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
499 gDvmJit.normalExit, gDvmJit.puntExit);
Ben Cheng452efba2010-04-30 15:14:00 -0700500
Ben Cheng978738d2010-05-13 13:45:57 -0700501 LOGD("JIT: ICHits: %d", gDvmICHitCount);
502
Ben Cheng72621c92010-03-10 13:12:55 -0800503 LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, "
504 "%d switch overflow",
505 gDvmJit.noChainExit[kInlineCacheMiss],
506 gDvmJit.noChainExit[kCallsiteInterpreted],
507 gDvmJit.noChainExit[kSwitchOverflow]);
Ben Cheng86717f72010-03-05 15:27:21 -0800508
Ben Chengb88ec3c2010-05-17 12:50:33 -0700509 LOGD("JIT: ICPatch: %d init, %d rejected, %d lock-free, %d queued, "
510 "%d dropped",
511 gDvmJit.icPatchInit, gDvmJit.icPatchRejected,
512 gDvmJit.icPatchLockFree, gDvmJit.icPatchQueued,
Ben Cheng452efba2010-04-30 15:14:00 -0700513 gDvmJit.icPatchDropped);
514
Ben Cheng86717f72010-03-05 15:27:21 -0800515 LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return",
516 gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic,
517 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700518 LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter",
519 gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined,
520 gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined);
Ben Cheng86717f72010-03-05 15:27:21 -0800521 LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000);
522 LOGD("JIT: Avg unit compilation time: %llu us",
Andy McFaddenb7a797d2011-02-24 16:55:40 -0800523 gDvmJit.numCompilations == 0 ? 0 :
Ben Cheng86717f72010-03-05 15:27:21 -0800524 gDvmJit.jitTime / gDvmJit.numCompilations);
Ben Cheng385828e2011-03-04 16:48:33 -0800525 LOGD("JIT: Potential GC blocked by compiler: max %llu us / "
526 "avg %llu us (%d)",
527 gDvmJit.maxCompilerThreadBlockGCTime,
528 gDvmJit.numCompilerThreadBlockGC == 0 ?
529 0 : gDvmJit.compilerThreadBlockGCTime /
530 gDvmJit.numCompilerThreadBlockGC,
531 gDvmJit.numCompilerThreadBlockGC);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700532#endif
Ben Cheng86717f72010-03-05 15:27:21 -0800533
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800534 LOGD("JIT: %d Translation chains, %d interp stubs",
535 gDvmJit.translationChains, stubs);
buzbee2e152ba2010-12-15 16:32:35 -0800536 if (gDvmJit.profileMode == kTraceProfilingContinuous) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700537 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700538 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700539 }
540}
541
Bill Buzbee716f1202009-07-23 13:22:09 -0700542
buzbee9a3147c2011-03-02 15:43:48 -0800543/* End current trace now & don't include current instruction */
544void dvmJitEndTraceSelect(Thread* self, const u2* dPC)
Bill Buzbeed7269912009-11-10 14:31:32 -0800545{
buzbee9a3147c2011-03-02 15:43:48 -0800546 if (self->jitState == kJitTSelect) {
buzbee9f601a92011-02-11 17:48:20 -0800547 self->jitState = kJitTSelectEnd;
buzbee9a3147c2011-03-02 15:43:48 -0800548 }
549 if (self->jitState == kJitTSelectEnd) {
550 // Clean up and finish now.
551 dvmCheckJit(dPC, self);
552 }
Bill Buzbeed7269912009-11-10 14:31:32 -0800553}
554
Ben Chengba4fc8b2009-06-01 13:00:29 -0700555/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800556 * Find an entry in the JitTable, creating if necessary.
557 * Returns null if table is full.
558 */
Ben Chengcfdeca32011-01-14 11:36:46 -0800559static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked,
560 bool isMethodEntry)
Bill Buzbee964a7b02010-01-28 12:54:19 -0800561{
562 u4 chainEndMarker = gDvmJit.jitTableSize;
563 u4 idx = dvmJitHash(dPC);
564
Ben Chengcfdeca32011-01-14 11:36:46 -0800565 /*
566 * Walk the bucket chain to find an exact match for our PC and trace/method
567 * type
568 */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800569 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
Ben Chengcfdeca32011-01-14 11:36:46 -0800570 ((gDvmJit.pJitEntryTable[idx].dPC != dPC) ||
571 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry !=
572 isMethodEntry))) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800573 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
574 }
575
Ben Chengcfdeca32011-01-14 11:36:46 -0800576 if (gDvmJit.pJitEntryTable[idx].dPC != dPC ||
577 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry != isMethodEntry) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800578 /*
579 * No match. Aquire jitTableLock and find the last
580 * slot in the chain. Possibly continue the chain walk in case
581 * some other thread allocated the slot we were looking
582 * at previuosly (perhaps even the dPC we're trying to enter).
583 */
584 if (!callerLocked)
585 dvmLockMutex(&gDvmJit.tableLock);
586 /*
587 * At this point, if .dPC is NULL, then the slot we're
588 * looking at is the target slot from the primary hash
589 * (the simple, and common case). Otherwise we're going
590 * to have to find a free slot and chain it.
591 */
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700592 ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800593 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
594 u4 prev;
595 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
Ben Chengcfdeca32011-01-14 11:36:46 -0800596 if (gDvmJit.pJitEntryTable[idx].dPC == dPC &&
597 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
598 isMethodEntry) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800599 /* Another thread got there first for this dPC */
600 if (!callerLocked)
601 dvmUnlockMutex(&gDvmJit.tableLock);
602 return &gDvmJit.pJitEntryTable[idx];
603 }
604 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
605 }
606 /* Here, idx should be pointing to the last cell of an
607 * active chain whose last member contains a valid dPC */
608 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
609 /* Linear walk to find a free cell and add it to the end */
610 prev = idx;
611 while (true) {
612 idx++;
613 if (idx == chainEndMarker)
614 idx = 0; /* Wraparound */
615 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
616 (idx == prev))
617 break;
618 }
619 if (idx != prev) {
620 JitEntryInfoUnion oldValue;
621 JitEntryInfoUnion newValue;
622 /*
623 * Although we hold the lock so that noone else will
624 * be trying to update a chain field, the other fields
625 * packed into the word may be in use by other threads.
626 */
627 do {
628 oldValue = gDvmJit.pJitEntryTable[prev].u;
629 newValue = oldValue;
630 newValue.info.chain = idx;
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700631 } while (android_atomic_release_cas(oldValue.infoWord,
632 newValue.infoWord,
633 &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800634 }
635 }
636 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
Ben Chengcfdeca32011-01-14 11:36:46 -0800637 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry = isMethodEntry;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800638 /*
639 * Initialize codeAddress and allocate the slot. Must
640 * happen in this order (since dPC is set, the entry is live.
641 */
Ben Chengcfdeca32011-01-14 11:36:46 -0800642 android_atomic_release_store((int32_t)dPC,
643 (volatile int32_t *)(void *)&gDvmJit.pJitEntryTable[idx].dPC);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800644 gDvmJit.pJitEntryTable[idx].dPC = dPC;
645 gDvmJit.jitTableEntriesUsed++;
646 } else {
647 /* Table is full */
648 idx = chainEndMarker;
649 }
650 if (!callerLocked)
651 dvmUnlockMutex(&gDvmJit.tableLock);
652 }
653 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
654}
Ben Chenga4973592010-03-31 11:59:18 -0700655
buzbeed82cebc2011-03-14 12:25:24 -0700656/* Dump a trace description */
657void dvmJitDumpTraceDesc(JitTraceDescription *trace)
658{
659 int i;
660 bool done = false;
661 const u2* dpc;
662 const u2* dpcBase;
663 int curFrag = 0;
664 LOGD("===========================================");
buzbee9a3147c2011-03-02 15:43:48 -0800665 LOGD("Trace dump 0x%x, Method %s off 0x%x",(int)trace,
buzbeed82cebc2011-03-14 12:25:24 -0700666 trace->method->name,trace->trace[curFrag].info.frag.startOffset);
667 dpcBase = trace->method->insns;
668 while (!done) {
669 DecodedInstruction decInsn;
670 if (trace->trace[curFrag].isCode) {
671 LOGD("Frag[%d]- Insts: %d, start: 0x%x, hint: 0x%x, end: %d",
672 curFrag, trace->trace[curFrag].info.frag.numInsts,
673 trace->trace[curFrag].info.frag.startOffset,
674 trace->trace[curFrag].info.frag.hint,
675 trace->trace[curFrag].info.frag.runEnd);
676 dpc = dpcBase + trace->trace[curFrag].info.frag.startOffset;
677 for (i=0; i<trace->trace[curFrag].info.frag.numInsts; i++) {
678 dexDecodeInstruction(dpc, &decInsn);
buzbee9a3147c2011-03-02 15:43:48 -0800679 LOGD(" 0x%04x - %s 0x%x",(dpc-dpcBase),
680 dexGetOpcodeName(decInsn.opcode),(int)dpc);
buzbeed82cebc2011-03-14 12:25:24 -0700681 dpc += dexGetWidthFromOpcode(decInsn.opcode);
682 }
683 if (trace->trace[curFrag].info.frag.runEnd) {
684 done = true;
685 }
686 } else {
687 LOGD("Frag[%d]- META info: 0x%08x", curFrag,
688 (int)trace->trace[curFrag].info.meta);
689 }
690 curFrag++;
691 }
692 LOGD("-------------------------------------------");
693}
694
Bill Buzbee964a7b02010-01-28 12:54:19 -0800695/*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700696 * Append the class ptr of "this" and the current method ptr to the current
697 * trace. That is, the trace runs will contain the following components:
698 * + trace run that ends with an invoke (existing entry)
699 * + thisClass (new)
700 * + calleeMethod (new)
701 */
buzbee9f601a92011-02-11 17:48:20 -0800702static void insertClassMethodInfo(Thread* self,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700703 const ClassObject* thisClass,
704 const Method* calleeMethod,
705 const DecodedInstruction* insn)
706{
buzbee9f601a92011-02-11 17:48:20 -0800707 int currTraceRun = ++self->currTraceRun;
Ben Cheng385828e2011-03-04 16:48:33 -0800708 self->trace[currTraceRun].info.meta = thisClass ?
709 (void *) thisClass->descriptor : NULL;
710 self->trace[currTraceRun].isCode = false;
711
buzbee9f601a92011-02-11 17:48:20 -0800712 currTraceRun = ++self->currTraceRun;
Ben Cheng385828e2011-03-04 16:48:33 -0800713 self->trace[currTraceRun].info.meta = thisClass ?
714 (void *) thisClass->classLoader : NULL;
715 self->trace[currTraceRun].isCode = false;
716
717 currTraceRun = ++self->currTraceRun;
718 self->trace[currTraceRun].info.meta = (void *) calleeMethod;
719 self->trace[currTraceRun].isCode = false;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700720}
721
722/*
Ben Chengd44faf52010-06-02 15:33:51 -0700723 * Check if the next instruction following the invoke is a move-result and if
Ben Cheng7a2697d2010-06-07 13:44:23 -0700724 * so add it to the trace. That is, this will add the trace run that includes
725 * the move-result to the trace list.
726 *
727 * + trace run that ends with an invoke (existing entry)
728 * + thisClass (existing entry)
729 * + calleeMethod (existing entry)
730 * + move result (new)
Ben Chengd44faf52010-06-02 15:33:51 -0700731 *
732 * lastPC, len, offset are all from the preceding invoke instruction
733 */
734static void insertMoveResult(const u2 *lastPC, int len, int offset,
buzbee9f601a92011-02-11 17:48:20 -0800735 Thread *self)
Ben Chengd44faf52010-06-02 15:33:51 -0700736{
737 DecodedInstruction nextDecInsn;
738 const u2 *moveResultPC = lastPC + len;
739
Dan Bornstein54322392010-11-17 14:16:56 -0800740 dexDecodeInstruction(moveResultPC, &nextDecInsn);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800741 if ((nextDecInsn.opcode != OP_MOVE_RESULT) &&
742 (nextDecInsn.opcode != OP_MOVE_RESULT_WIDE) &&
743 (nextDecInsn.opcode != OP_MOVE_RESULT_OBJECT))
Ben Chengd44faf52010-06-02 15:33:51 -0700744 return;
745
746 /* We need to start a new trace run */
buzbee9f601a92011-02-11 17:48:20 -0800747 int currTraceRun = ++self->currTraceRun;
748 self->currRunHead = moveResultPC;
Ben Cheng385828e2011-03-04 16:48:33 -0800749 self->trace[currTraceRun].info.frag.startOffset = offset + len;
750 self->trace[currTraceRun].info.frag.numInsts = 1;
751 self->trace[currTraceRun].info.frag.runEnd = false;
752 self->trace[currTraceRun].info.frag.hint = kJitHintNone;
753 self->trace[currTraceRun].isCode = true;
buzbee9f601a92011-02-11 17:48:20 -0800754 self->totalTraceLen++;
Ben Chengd44faf52010-06-02 15:33:51 -0700755
buzbee9f601a92011-02-11 17:48:20 -0800756 self->currRunLen = dexGetWidthFromInstruction(moveResultPC);
Ben Chengd44faf52010-06-02 15:33:51 -0700757}
758
759/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700760 * Adds to the current trace request one instruction at a time, just
761 * before that instruction is interpreted. This is the primary trace
762 * selection function. NOTE: return instruction are handled a little
763 * differently. In general, instructions are "proposed" to be added
764 * to the current trace prior to interpretation. If the interpreter
765 * then successfully completes the instruction, is will be considered
766 * part of the request. This allows us to examine machine state prior
767 * to interpretation, and also abort the trace request if the instruction
768 * throws or does something unexpected. However, return instructions
769 * will cause an immediate end to the translation request - which will
770 * be passed to the compiler before the return completes. This is done
771 * in response to special handling of returns by the interpreter (and
772 * because returns cannot throw in a way that causes problems for the
773 * translated code.
774 */
buzbee9a3147c2011-03-02 15:43:48 -0800775void dvmCheckJit(const u2* pc, Thread* self)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700776{
buzbee9a3147c2011-03-02 15:43:48 -0800777 const ClassObject *thisClass = self->callsiteClass;
778 const Method* curMethod = self->methodToCall;
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700779 int flags, len;
buzbee9a3147c2011-03-02 15:43:48 -0800780 int allDone = false;
781 /* Stay in break/single-stop mode for the next instruction */
Ben Cheng7a2697d2010-06-07 13:44:23 -0700782 bool stayOneMoreInst = false;
Bill Buzbeed7269912009-11-10 14:31:32 -0800783
buzbee9a3147c2011-03-02 15:43:48 -0800784 /* Prepare to handle last PC and stage the current PC & method*/
buzbee9f601a92011-02-11 17:48:20 -0800785 const u2 *lastPC = self->lastPC;
buzbee9a3147c2011-03-02 15:43:48 -0800786
buzbee9f601a92011-02-11 17:48:20 -0800787 self->lastPC = pc;
Ben Cheng79d173c2009-09-29 16:12:51 -0700788
buzbee9f601a92011-02-11 17:48:20 -0800789 switch (self->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 int offset;
791 DecodedInstruction decInsn;
792 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700793 /* First instruction - just remember the PC and exit */
794 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700795 /* Grow the trace around the last PC if jitState is kJitTSelect */
Dan Bornstein54322392010-11-17 14:16:56 -0800796 dexDecodeInstruction(lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700797
798 /*
799 * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
800 * to the amount of space it takes to generate the chaining
801 * cells.
802 */
buzbee9f601a92011-02-11 17:48:20 -0800803 if (self->totalTraceLen != 0 &&
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800804 (decInsn.opcode == OP_PACKED_SWITCH ||
805 decInsn.opcode == OP_SPARSE_SWITCH)) {
buzbee9f601a92011-02-11 17:48:20 -0800806 self->jitState = kJitTSelectEnd;
Ben Cheng6c10a972009-10-29 14:39:18 -0700807 break;
808 }
809
Ben Chengba4fc8b2009-06-01 13:00:29 -0700810#if defined(SHOW_TRACE)
buzbee9a3147c2011-03-02 15:43:48 -0800811 LOGD("TraceGen: adding %s. lpc:0x%x, pc:0x%x",
812 dexGetOpcodeName(decInsn.opcode), (int)lastPC, (int)pc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700813#endif
Dan Bornsteine4852762010-12-02 12:45:00 -0800814 flags = dexGetFlagsFromOpcode(decInsn.opcode);
815 len = dexGetWidthFromInstruction(lastPC);
buzbee9a3147c2011-03-02 15:43:48 -0800816 offset = lastPC - self->traceMethod->insns;
Ben Cheng79d173c2009-09-29 16:12:51 -0700817 assert((unsigned) offset <
buzbee9a3147c2011-03-02 15:43:48 -0800818 dvmGetMethodInsnsSize(self->traceMethod));
buzbee9f601a92011-02-11 17:48:20 -0800819 if (lastPC != self->currRunHead + self->currRunLen) {
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700820 int currTraceRun;
821 /* We need to start a new trace run */
buzbee9f601a92011-02-11 17:48:20 -0800822 currTraceRun = ++self->currTraceRun;
823 self->currRunLen = 0;
824 self->currRunHead = (u2*)lastPC;
Ben Cheng385828e2011-03-04 16:48:33 -0800825 self->trace[currTraceRun].info.frag.startOffset = offset;
826 self->trace[currTraceRun].info.frag.numInsts = 0;
827 self->trace[currTraceRun].info.frag.runEnd = false;
828 self->trace[currTraceRun].info.frag.hint = kJitHintNone;
829 self->trace[currTraceRun].isCode = true;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700830 }
Ben Cheng385828e2011-03-04 16:48:33 -0800831 self->trace[self->currTraceRun].info.frag.numInsts++;
buzbee9f601a92011-02-11 17:48:20 -0800832 self->totalTraceLen++;
833 self->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700834
Ben Chengd44faf52010-06-02 15:33:51 -0700835 /*
836 * If the last instruction is an invoke, we will try to sneak in
837 * the move-result* (if existent) into a separate trace run.
838 */
839 int needReservedRun = (flags & kInstrInvoke) ? 1 : 0;
840
Ben Cheng79d173c2009-09-29 16:12:51 -0700841 /* Will probably never hit this with the current trace buildier */
buzbee9f601a92011-02-11 17:48:20 -0800842 if (self->currTraceRun ==
Ben Chengd44faf52010-06-02 15:33:51 -0700843 (MAX_JIT_RUN_LEN - 1 - needReservedRun)) {
buzbee9f601a92011-02-11 17:48:20 -0800844 self->jitState = kJitTSelectEnd;
Ben Cheng79d173c2009-09-29 16:12:51 -0700845 }
846
Dan Bornsteinc2b486f2010-11-12 16:07:16 -0800847 if (!dexIsGoto(flags) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700848 ((flags & (kInstrCanBranch |
849 kInstrCanSwitch |
850 kInstrCanReturn |
851 kInstrInvoke)) != 0)) {
buzbee9f601a92011-02-11 17:48:20 -0800852 self->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700853#if defined(SHOW_TRACE)
Ben Chengd44faf52010-06-02 15:33:51 -0700854 LOGD("TraceGen: ending on %s, basic block end",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800855 dexGetOpcodeName(decInsn.opcode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856#endif
Ben Chengd44faf52010-06-02 15:33:51 -0700857
858 /*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700859 * If the current invoke is a {virtual,interface}, get the
860 * current class/method pair into the trace as well.
Ben Chengd44faf52010-06-02 15:33:51 -0700861 * If the next instruction is a variant of move-result, insert
Ben Cheng7a2697d2010-06-07 13:44:23 -0700862 * it to the trace too.
Ben Chengd44faf52010-06-02 15:33:51 -0700863 */
864 if (flags & kInstrInvoke) {
buzbee9f601a92011-02-11 17:48:20 -0800865 insertClassMethodInfo(self, thisClass, curMethod,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700866 &decInsn);
buzbee9f601a92011-02-11 17:48:20 -0800867 insertMoveResult(lastPC, len, offset, self);
Ben Chengd44faf52010-06-02 15:33:51 -0700868 }
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700869 }
Bill Buzbee2ce8a6c2009-12-03 15:09:32 -0800870 /* Break on throw or self-loop */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800871 if ((decInsn.opcode == OP_THROW) || (lastPC == pc)){
buzbee9f601a92011-02-11 17:48:20 -0800872 self->jitState = kJitTSelectEnd;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700873 }
buzbee9f601a92011-02-11 17:48:20 -0800874 if (self->totalTraceLen >= JIT_MAX_TRACE_LEN) {
875 self->jitState = kJitTSelectEnd;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700876 }
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700877 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
878 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700879 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700880 else {
881 /*
882 * Last instruction is a return - stay in the dbg interpreter
883 * for one more instruction if it is a non-void return, since
884 * we don't want to start a trace with move-result as the first
885 * instruction (which is already included in the trace
886 * containing the invoke.
887 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800888 if (decInsn.opcode != OP_RETURN_VOID) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700889 stayOneMoreInst = true;
890 }
891 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892 /* NOTE: intentional fallthrough for returns */
893 case kJitTSelectEnd:
894 {
Bill Buzbee1b3da592011-02-03 07:38:22 -0800895 /* Empty trace - set to bail to interpreter */
buzbee9f601a92011-02-11 17:48:20 -0800896 if (self->totalTraceLen == 0) {
897 dvmJitSetCodeAddr(self->currTraceHead,
Bill Buzbee1b3da592011-02-03 07:38:22 -0800898 dvmCompilerGetInterpretTemplate(),
899 dvmCompilerGetInterpretTemplateSet(),
900 false /* Not method entry */, 0);
buzbee9f601a92011-02-11 17:48:20 -0800901 self->jitState = kJitDone;
buzbee9a3147c2011-03-02 15:43:48 -0800902 allDone = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700903 break;
904 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700905
buzbee9f601a92011-02-11 17:48:20 -0800906 int lastTraceDesc = self->currTraceRun;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700907
908 /* Extend a new empty desc if the last slot is meta info */
Ben Cheng385828e2011-03-04 16:48:33 -0800909 if (!self->trace[lastTraceDesc].isCode) {
buzbee9f601a92011-02-11 17:48:20 -0800910 lastTraceDesc = ++self->currTraceRun;
Ben Cheng385828e2011-03-04 16:48:33 -0800911 self->trace[lastTraceDesc].info.frag.startOffset = 0;
912 self->trace[lastTraceDesc].info.frag.numInsts = 0;
913 self->trace[lastTraceDesc].info.frag.hint = kJitHintNone;
914 self->trace[lastTraceDesc].isCode = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700915 }
916
917 /* Mark the end of the trace runs */
Ben Cheng385828e2011-03-04 16:48:33 -0800918 self->trace[lastTraceDesc].info.frag.runEnd = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700919
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 JitTraceDescription* desc =
921 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
buzbee9f601a92011-02-11 17:48:20 -0800922 sizeof(JitTraceRun) * (self->currTraceRun+1));
Ben Cheng7a2697d2010-06-07 13:44:23 -0700923
Ben Chengba4fc8b2009-06-01 13:00:29 -0700924 if (desc == NULL) {
925 LOGE("Out of memory in trace selection");
926 dvmJitStopTranslationRequests();
buzbee9f601a92011-02-11 17:48:20 -0800927 self->jitState = kJitDone;
buzbee9a3147c2011-03-02 15:43:48 -0800928 allDone = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700929 break;
930 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700931
buzbee9a3147c2011-03-02 15:43:48 -0800932 desc->method = self->traceMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700933 memcpy((char*)&(desc->trace[0]),
buzbee9f601a92011-02-11 17:48:20 -0800934 (char*)&(self->trace[0]),
935 sizeof(JitTraceRun) * (self->currTraceRun+1));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700936#if defined(SHOW_TRACE)
937 LOGD("TraceGen: trace done, adding to queue");
buzbee9a3147c2011-03-02 15:43:48 -0800938 dvmJitDumpTraceDesc(desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800940 if (dvmCompilerWorkEnqueue(
buzbee9f601a92011-02-11 17:48:20 -0800941 self->currTraceHead,kWorkOrderTrace,desc)) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800942 /* Work order successfully enqueued */
943 if (gDvmJit.blockingMode) {
944 dvmCompilerDrainQueue();
945 }
Ben Cheng1357e942010-02-10 17:21:39 -0800946 } else {
947 /*
948 * Make sure the descriptor for the abandoned work order is
949 * freed.
950 */
951 free(desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700952 }
buzbee9f601a92011-02-11 17:48:20 -0800953 self->jitState = kJitDone;
buzbee9a3147c2011-03-02 15:43:48 -0800954 allDone = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 }
956 break;
Ben Chenga4973592010-03-31 11:59:18 -0700957 case kJitDone:
buzbee9a3147c2011-03-02 15:43:48 -0800958 allDone = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700959 break;
Ben Chenga4973592010-03-31 11:59:18 -0700960 case kJitNot:
buzbee9a3147c2011-03-02 15:43:48 -0800961 allDone = true;
Ben Chenged79ff02009-10-13 13:26:40 -0700962 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963 default:
buzbee9a3147c2011-03-02 15:43:48 -0800964 LOGE("Unexpected JIT state: %d", self->jitState);
Ben Chenga4973592010-03-31 11:59:18 -0700965 dvmAbort();
Ben Cheng9c147b82009-10-07 16:41:46 -0700966 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967 }
buzbee9a3147c2011-03-02 15:43:48 -0800968
Ben Chenga4973592010-03-31 11:59:18 -0700969 /*
buzbee9a3147c2011-03-02 15:43:48 -0800970 * If we're done with trace selection, switch off the control flags.
Ben Chenga4973592010-03-31 11:59:18 -0700971 */
buzbee9a3147c2011-03-02 15:43:48 -0800972 if (allDone) {
973 dvmUpdateInterpBreak(self, kInterpJitBreak,
974 kSubModeJitTraceBuild, false);
975 if (stayOneMoreInst) {
976 // Keep going in single-step mode for at least one more inst
977 assert(self->jitResumeNPC == NULL);
978 self->singleStepCount = MIN(1, self->singleStepCount);
979 dvmUpdateInterpBreak(self, kInterpSingleStep, kSubModeNormal,
980 true /* enable */);
981 }
982 }
983 return;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700984}
985
Bill Buzbee1b3da592011-02-03 07:38:22 -0800986JitEntry *dvmJitFindEntry(const u2* pc, bool isMethodEntry)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700987{
988 int idx = dvmJitHash(pc);
989
990 /* Expect a high hit rate on 1st shot */
Bill Buzbee1b3da592011-02-03 07:38:22 -0800991 if ((gDvmJit.pJitEntryTable[idx].dPC == pc) &&
992 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry == isMethodEntry))
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 return &gDvmJit.pJitEntryTable[idx];
994 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700995 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700996 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
997 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Bill Buzbee1b3da592011-02-03 07:38:22 -0800998 if ((gDvmJit.pJitEntryTable[idx].dPC == pc) &&
999 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
1000 isMethodEntry))
Ben Chengba4fc8b2009-06-01 13:00:29 -07001001 return &gDvmJit.pJitEntryTable[idx];
1002 }
1003 }
1004 return NULL;
1005}
1006
Bill Buzbee27176222009-06-09 09:20:16 -07001007/*
Ben Chengcfdeca32011-01-14 11:36:46 -08001008 * Walk through the JIT profile table and find the corresponding JIT code, in
1009 * the specified format (ie trace vs method). This routine needs to be fast.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010 */
Ben Chengcfdeca32011-01-14 11:36:46 -08001011void* getCodeAddrCommon(const u2* dPC, bool methodEntry)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012{
1013 int idx = dvmJitHash(dPC);
Ben Chengcfdeca32011-01-14 11:36:46 -08001014 const u2* pc = gDvmJit.pJitEntryTable[idx].dPC;
1015 if (pc != NULL) {
Ben Cheng1a7b9d72010-09-20 22:20:31 -07001016 bool hideTranslation = dvmJitHideTranslation();
Ben Chengcfdeca32011-01-14 11:36:46 -08001017 if (pc == dPC &&
1018 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry == methodEntry) {
buzbee2e152ba2010-12-15 16:32:35 -08001019 int offset = (gDvmJit.profileMode >= kTraceProfilingContinuous) ?
1020 0 : gDvmJit.pJitEntryTable[idx].u.info.profileOffset;
1021 intptr_t codeAddress =
1022 (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress;
Ben Cheng978738d2010-05-13 13:45:57 -07001023#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -08001024 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001025#endif
buzbee99ddb1e2011-01-28 10:44:30 -08001026 return hideTranslation || !codeAddress ? NULL :
1027 (void *)(codeAddress + offset);
Bill Buzbee9797a232010-01-12 12:20:13 -08001028 } else {
1029 int chainEndMarker = gDvmJit.jitTableSize;
1030 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
1031 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengcfdeca32011-01-14 11:36:46 -08001032 if (gDvmJit.pJitEntryTable[idx].dPC == dPC &&
1033 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
1034 methodEntry) {
buzbee2e152ba2010-12-15 16:32:35 -08001035 int offset = (gDvmJit.profileMode >=
1036 kTraceProfilingContinuous) ? 0 :
1037 gDvmJit.pJitEntryTable[idx].u.info.profileOffset;
1038 intptr_t codeAddress =
1039 (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress;
Ben Cheng978738d2010-05-13 13:45:57 -07001040#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -08001041 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042#endif
buzbee99ddb1e2011-01-28 10:44:30 -08001043 return hideTranslation || !codeAddress ? NULL :
buzbee2e152ba2010-12-15 16:32:35 -08001044 (void *)(codeAddress + offset);
Bill Buzbee9797a232010-01-12 12:20:13 -08001045 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047 }
1048 }
Ben Cheng978738d2010-05-13 13:45:57 -07001049#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001050 gDvmJit.addrLookupsNotFound++;
1051#endif
1052 return NULL;
1053}
1054
1055/*
Ben Chengcfdeca32011-01-14 11:36:46 -08001056 * If a translated code address, in trace format, exists for the davik byte code
1057 * pointer return it.
1058 */
1059void* dvmJitGetTraceAddr(const u2* dPC)
1060{
1061 return getCodeAddrCommon(dPC, false /* method entry */);
1062}
1063
1064/*
1065 * If a translated code address, in whole-method format, exists for the davik
1066 * byte code pointer return it.
1067 */
1068void* dvmJitGetMethodAddr(const u2* dPC)
1069{
1070 return getCodeAddrCommon(dPC, true /* method entry */);
1071}
1072
1073/*
buzbee9a3147c2011-03-02 15:43:48 -08001074 * Similar to dvmJitGetTraceAddr, but returns null if the calling
1075 * thread is in a single-step mode.
1076 */
1077void* dvmJitGetTraceAddrThread(const u2* dPC, Thread* self)
1078{
1079 return (self->interpBreak.ctl.breakFlags != 0) ? NULL :
1080 getCodeAddrCommon(dPC, false /* method entry */);
1081}
1082
1083/*
1084 * Similar to dvmJitGetMethodAddr, but returns null if the calling
1085 * thread is in a single-step mode.
1086 */
1087void* dvmJitGetMethodAddrThread(const u2* dPC, Thread* self)
1088{
1089 return (self->interpBreak.ctl.breakFlags != 0) ? NULL :
1090 getCodeAddrCommon(dPC, true /* method entry */);
1091}
1092
1093/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -08001095 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -07001097 * threads. This routine should only be called by the compiler
buzbee2e152ba2010-12-15 16:32:35 -08001098 * thread. We defer the setting of the profile prefix size until
1099 * after the new code address is set to ensure that the prefix offset
1100 * is never applied to the initial interpret-only translation. All
1101 * translations with non-zero profile prefixes will still be correct
1102 * if entered as if the profile offset is 0, but the interpret-only
1103 * template cannot handle a non-zero prefix.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001104 */
buzbee2e152ba2010-12-15 16:32:35 -08001105void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set,
Ben Chengcfdeca32011-01-14 11:36:46 -08001106 bool isMethodEntry, int profilePrefixSize)
buzbee2e152ba2010-12-15 16:32:35 -08001107{
Bill Buzbee716f1202009-07-23 13:22:09 -07001108 JitEntryInfoUnion oldValue;
1109 JitEntryInfoUnion newValue;
Ben Cheng20d7e6c2011-02-18 17:12:42 -08001110 /*
1111 * Method-based JIT doesn't go through the normal profiling phase, so use
1112 * lookupAndAdd here to request a new entry in the table.
1113 */
1114 JitEntry *jitEntry = isMethodEntry ?
1115 lookupAndAdd(dPC, false /* caller locked */, true) :
1116 dvmJitFindEntry(dPC, isMethodEntry);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001117 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -07001118 /* Note: order of update is important */
1119 do {
1120 oldValue = jitEntry->u;
1121 newValue = oldValue;
Ben Chengcfdeca32011-01-14 11:36:46 -08001122 newValue.info.isMethodEntry = isMethodEntry;
Bill Buzbee716f1202009-07-23 13:22:09 -07001123 newValue.info.instructionSet = set;
buzbee99ddb1e2011-01-28 10:44:30 -08001124 newValue.info.profileOffset = profilePrefixSize;
Andy McFadden6e10b9a2010-06-14 15:24:39 -07001125 } while (android_atomic_release_cas(
1126 oldValue.infoWord, newValue.infoWord,
1127 &jitEntry->u.infoWord) != 0);
Bill Buzbee716f1202009-07-23 13:22:09 -07001128 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001129}
1130
1131/*
buzbee9a3147c2011-03-02 15:43:48 -08001132 * Determine if valid trace-bulding request is active. If so, set
1133 * the proper flags in interpBreak and return. Trace selection will
1134 * then begin normally via dvmCheckBefore.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001135 */
buzbee9a3147c2011-03-02 15:43:48 -08001136void dvmJitCheckTraceRequest(Thread* self)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001137{
Bill Buzbee48f18242009-06-19 16:02:27 -07001138 int i;
buzbee852aacd2010-06-08 16:24:46 -07001139 /*
1140 * A note on trace "hotness" filtering:
1141 *
1142 * Our first level trigger is intentionally loose - we need it to
1143 * fire easily not just to identify potential traces to compile, but
1144 * also to allow re-entry into the code cache.
1145 *
1146 * The 2nd level filter (done here) exists to be selective about
1147 * what we actually compile. It works by requiring the same
1148 * trace head "key" (defined as filterKey below) to appear twice in
1149 * a relatively short period of time. The difficulty is defining the
1150 * shape of the filterKey. Unfortunately, there is no "one size fits
1151 * all" approach.
1152 *
1153 * For spiky execution profiles dominated by a smallish
1154 * number of very hot loops, we would want the second-level filter
1155 * to be very selective. A good selective filter is requiring an
1156 * exact match of the Dalvik PC. In other words, defining filterKey as:
buzbee9f601a92011-02-11 17:48:20 -08001157 * intptr_t filterKey = (intptr_t)self->interpSave.pc
buzbee852aacd2010-06-08 16:24:46 -07001158 *
1159 * However, for flat execution profiles we do best when aggressively
1160 * translating. A heuristically decent proxy for this is to use
1161 * the value of the method pointer containing the trace as the filterKey.
1162 * Intuitively, this is saying that once any trace in a method appears hot,
1163 * immediately translate any other trace from that same method that
1164 * survives the first-level filter. Here, filterKey would be defined as:
buzbee9f601a92011-02-11 17:48:20 -08001165 * intptr_t filterKey = (intptr_t)self->interpSave.method
buzbee852aacd2010-06-08 16:24:46 -07001166 *
1167 * The problem is that we can't easily detect whether we're dealing
1168 * with a spiky or flat profile. If we go with the "pc" match approach,
1169 * flat profiles perform poorly. If we go with the loose "method" match,
1170 * we end up generating a lot of useless translations. Probably the
1171 * best approach in the future will be to retain profile information
1172 * across runs of each application in order to determine it's profile,
1173 * and then choose once we have enough history.
1174 *
1175 * However, for now we've decided to chose a compromise filter scheme that
1176 * includes elements of both. The high order bits of the filter key
1177 * are drawn from the enclosing method, and are combined with a slice
1178 * of the low-order bits of the Dalvik pc of the trace head. The
1179 * looseness of the filter can be adjusted by changing with width of
1180 * the Dalvik pc slice (JIT_TRACE_THRESH_FILTER_PC_BITS). The wider
1181 * the slice, the tighter the filter.
1182 *
1183 * Note: the fixed shifts in the function below reflect assumed word
1184 * alignment for method pointers, and half-word alignment of the Dalvik pc.
1185 * for method pointers and half-word alignment for dalvik pc.
1186 */
buzbee9f601a92011-02-11 17:48:20 -08001187 u4 methodKey = (u4)self->interpSave.method <<
buzbeec35294d2010-06-09 14:22:50 -07001188 (JIT_TRACE_THRESH_FILTER_PC_BITS - 2);
buzbee9f601a92011-02-11 17:48:20 -08001189 u4 pcKey = ((u4)self->interpSave.pc >> 1) &
buzbeec35294d2010-06-09 14:22:50 -07001190 ((1 << JIT_TRACE_THRESH_FILTER_PC_BITS) - 1);
1191 intptr_t filterKey = (intptr_t)(methodKey | pcKey);
buzbee9a3147c2011-03-02 15:43:48 -08001192
1193 // Shouldn't be here if already building a trace.
1194 assert((self->interpBreak.ctl.subMode & kSubModeJitTraceBuild)==0);
Ben Cheng40094c12010-02-24 20:58:44 -08001195
Ben Chenga4973592010-03-31 11:59:18 -07001196 /* Check if the JIT request can be handled now */
buzbee9a3147c2011-03-02 15:43:48 -08001197 if ((gDvmJit.pJitEntryTable != NULL) &&
1198 ((self->interpBreak.ctl.breakFlags & kInterpSingleStep) == 0)){
Ben Chenga4973592010-03-31 11:59:18 -07001199 /* Bypass the filter for hot trace requests or during stress mode */
buzbee9f601a92011-02-11 17:48:20 -08001200 if (self->jitState == kJitTSelectRequest &&
Ben Chenga4973592010-03-31 11:59:18 -07001201 gDvmJit.threshold > 6) {
Ben Cheng40094c12010-02-24 20:58:44 -08001202 /* Two-level filtering scheme */
1203 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
buzbee9f601a92011-02-11 17:48:20 -08001204 if (filterKey == self->threshFilter[i]) {
1205 self->threshFilter[i] = 0; // Reset filter entry
Ben Cheng40094c12010-02-24 20:58:44 -08001206 break;
1207 }
Bill Buzbee48f18242009-06-19 16:02:27 -07001208 }
Ben Cheng40094c12010-02-24 20:58:44 -08001209 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
1210 /*
1211 * Use random replacement policy - otherwise we could miss a
1212 * large loop that contains more traces than the size of our
1213 * filter array.
1214 */
1215 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
buzbee9f601a92011-02-11 17:48:20 -08001216 self->threshFilter[i] = filterKey;
1217 self->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001218 }
Ben Chenga4973592010-03-31 11:59:18 -07001219 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001220
Ben Chenga4973592010-03-31 11:59:18 -07001221 /* If the compiler is backlogged, cancel any JIT actions */
1222 if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) {
buzbee9f601a92011-02-11 17:48:20 -08001223 self->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001224 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001225
Ben Chengba4fc8b2009-06-01 13:00:29 -07001226 /*
Ben Chenga4973592010-03-31 11:59:18 -07001227 * Check for additional reasons that might force the trace select
1228 * request to be dropped
Ben Chengba4fc8b2009-06-01 13:00:29 -07001229 */
buzbee9f601a92011-02-11 17:48:20 -08001230 if (self->jitState == kJitTSelectRequest ||
1231 self->jitState == kJitTSelectRequestHot) {
1232 if (dvmJitFindEntry(self->interpSave.pc, false)) {
Bill Buzbee1b3da592011-02-03 07:38:22 -08001233 /* In progress - nothing do do */
buzbee9f601a92011-02-11 17:48:20 -08001234 self->jitState = kJitDone;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001235 } else {
buzbee9f601a92011-02-11 17:48:20 -08001236 JitEntry *slot = lookupAndAdd(self->interpSave.pc,
Bill Buzbee1b3da592011-02-03 07:38:22 -08001237 false /* lock */,
1238 false /* method entry */);
1239 if (slot == NULL) {
1240 /*
1241 * Table is full. This should have been
1242 * detected by the compiler thread and the table
1243 * resized before we run into it here. Assume bad things
1244 * are afoot and disable profiling.
1245 */
buzbee9f601a92011-02-11 17:48:20 -08001246 self->jitState = kJitDone;
Bill Buzbee1b3da592011-02-03 07:38:22 -08001247 LOGD("JIT: JitTable full, disabling profiling");
1248 dvmJitStopTranslationRequests();
1249 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001250 }
1251 }
Ben Chenga4973592010-03-31 11:59:18 -07001252
buzbee9f601a92011-02-11 17:48:20 -08001253 switch (self->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001254 case kJitTSelectRequest:
Ben Cheng40094c12010-02-24 20:58:44 -08001255 case kJitTSelectRequestHot:
buzbee9f601a92011-02-11 17:48:20 -08001256 self->jitState = kJitTSelect;
buzbee9a3147c2011-03-02 15:43:48 -08001257 self->traceMethod = self->interpSave.method;
buzbee9f601a92011-02-11 17:48:20 -08001258 self->currTraceHead = self->interpSave.pc;
1259 self->currTraceRun = 0;
1260 self->totalTraceLen = 0;
1261 self->currRunHead = self->interpSave.pc;
1262 self->currRunLen = 0;
Ben Cheng385828e2011-03-04 16:48:33 -08001263 self->trace[0].info.frag.startOffset =
buzbee9f601a92011-02-11 17:48:20 -08001264 self->interpSave.pc - self->interpSave.method->insns;
Ben Cheng385828e2011-03-04 16:48:33 -08001265 self->trace[0].info.frag.numInsts = 0;
1266 self->trace[0].info.frag.runEnd = false;
1267 self->trace[0].info.frag.hint = kJitHintNone;
1268 self->trace[0].isCode = true;
buzbee9f601a92011-02-11 17:48:20 -08001269 self->lastPC = 0;
buzbee9a3147c2011-03-02 15:43:48 -08001270 /* Turn on trace selection mode */
1271 dvmUpdateInterpBreak(self, kInterpJitBreak,
1272 kSubModeJitTraceBuild, true);
1273#if defined(SHOW_TRACE)
1274 LOGD("Starting trace for %s at 0x%x",
1275 self->interpSave.method->name, (int)self->interpSave.pc);
1276#endif
Ben Chenga4973592010-03-31 11:59:18 -07001277 break;
Ben Chenga4973592010-03-31 11:59:18 -07001278 case kJitDone:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001279 break;
1280 default:
buzbee9a3147c2011-03-02 15:43:48 -08001281 LOGE("Unexpected JIT state: %d", self->jitState);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001282 dvmAbort();
1283 }
Ben Chenga4973592010-03-31 11:59:18 -07001284 } else {
buzbee9a3147c2011-03-02 15:43:48 -08001285 /* Cannot build trace this time */
buzbee9f601a92011-02-11 17:48:20 -08001286 self->jitState = kJitDone;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001287 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001288}
1289
Bill Buzbee27176222009-06-09 09:20:16 -07001290/*
1291 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
Bill Buzbee964a7b02010-01-28 12:54:19 -08001292 * Stops all threads, and thus is a heavyweight operation. May only be called
1293 * by the compiler thread.
Bill Buzbee27176222009-06-09 09:20:16 -07001294 */
1295bool dvmJitResizeJitTable( unsigned int size )
1296{
Bill Buzbee716f1202009-07-23 13:22:09 -07001297 JitEntry *pNewTable;
1298 JitEntry *pOldTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001299 JitEntry tempEntry;
Bill Buzbee27176222009-06-09 09:20:16 -07001300 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -07001301 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001302 unsigned int i;
1303
Ben Cheng3f02aa42009-08-14 13:52:09 -07001304 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -07001305 assert(size && !(size & (size - 1))); /* Is power of 2? */
1306
Ben Chenga4973592010-03-31 11:59:18 -07001307 LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
Bill Buzbee27176222009-06-09 09:20:16 -07001308
1309 newMask = size - 1;
1310
1311 if (size <= gDvmJit.jitTableSize) {
1312 return true;
1313 }
1314
Bill Buzbee964a7b02010-01-28 12:54:19 -08001315 /* Make sure requested size is compatible with chain field width */
1316 tempEntry.u.info.chain = size;
1317 if (tempEntry.u.info.chain != size) {
1318 LOGD("Jit: JitTable request of %d too big", size);
1319 return true;
1320 }
1321
Bill Buzbee716f1202009-07-23 13:22:09 -07001322 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -07001323 if (pNewTable == NULL) {
1324 return true;
1325 }
1326 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001327 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001328 }
1329
1330 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001331 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001332
Bill Buzbee716f1202009-07-23 13:22:09 -07001333 pOldTable = gDvmJit.pJitEntryTable;
1334 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001335
1336 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001337 gDvmJit.pJitEntryTable = pNewTable;
1338 gDvmJit.jitTableSize = size;
1339 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001340 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001341
Bill Buzbee716f1202009-07-23 13:22:09 -07001342 for (i=0; i < oldSize; i++) {
1343 if (pOldTable[i].dPC) {
1344 JitEntry *p;
1345 u2 chain;
Ben Chengcfdeca32011-01-14 11:36:46 -08001346 p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/,
1347 pOldTable[i].u.info.isMethodEntry);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001348 p->codeAddress = pOldTable[i].codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -07001349 /* We need to preserve the new chain field, but copy the rest */
Bill Buzbee716f1202009-07-23 13:22:09 -07001350 chain = p->u.info.chain;
1351 p->u = pOldTable[i].u;
1352 p->u.info.chain = chain;
Bill Buzbee716f1202009-07-23 13:22:09 -07001353 }
1354 }
buzbee2e152ba2010-12-15 16:32:35 -08001355
Bill Buzbee964a7b02010-01-28 12:54:19 -08001356 dvmUnlockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -07001357
1358 free(pOldTable);
1359
Bill Buzbee27176222009-06-09 09:20:16 -07001360 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001361 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001362
1363 return false;
1364}
1365
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001366/*
Ben Cheng60c24f42010-01-04 12:29:56 -08001367 * Reset the JitTable to the initial clean state.
1368 */
1369void dvmJitResetTable(void)
1370{
1371 JitEntry *jitEntry = gDvmJit.pJitEntryTable;
1372 unsigned int size = gDvmJit.jitTableSize;
1373 unsigned int i;
1374
1375 dvmLockMutex(&gDvmJit.tableLock);
buzbee2e152ba2010-12-15 16:32:35 -08001376
1377 /* Note: If need to preserve any existing counts. Do so here. */
buzbee38c41342011-01-11 15:45:49 -08001378 if (gDvmJit.pJitTraceProfCounters) {
1379 for (i=0; i < JIT_PROF_BLOCK_BUCKETS; i++) {
1380 if (gDvmJit.pJitTraceProfCounters->buckets[i])
1381 memset((void *) gDvmJit.pJitTraceProfCounters->buckets[i],
1382 0, sizeof(JitTraceCounter_t) * JIT_PROF_BLOCK_ENTRIES);
1383 }
1384 gDvmJit.pJitTraceProfCounters->next = 0;
buzbee2e152ba2010-12-15 16:32:35 -08001385 }
buzbee2e152ba2010-12-15 16:32:35 -08001386
Ben Cheng60c24f42010-01-04 12:29:56 -08001387 memset((void *) jitEntry, 0, sizeof(JitEntry) * size);
1388 for (i=0; i< size; i++) {
1389 jitEntry[i].u.info.chain = size; /* Initialize chain termination */
1390 }
1391 gDvmJit.jitTableEntriesUsed = 0;
1392 dvmUnlockMutex(&gDvmJit.tableLock);
1393}
1394
1395/*
buzbee2e152ba2010-12-15 16:32:35 -08001396 * Return the address of the next trace profile counter. This address
1397 * will be embedded in the generated code for the trace, and thus cannot
1398 * change while the trace exists.
1399 */
1400JitTraceCounter_t *dvmJitNextTraceCounter()
1401{
1402 int idx = gDvmJit.pJitTraceProfCounters->next / JIT_PROF_BLOCK_ENTRIES;
1403 int elem = gDvmJit.pJitTraceProfCounters->next % JIT_PROF_BLOCK_ENTRIES;
1404 JitTraceCounter_t *res;
1405 /* Lazily allocate blocks of counters */
1406 if (!gDvmJit.pJitTraceProfCounters->buckets[idx]) {
1407 JitTraceCounter_t *p =
1408 (JitTraceCounter_t*) calloc(JIT_PROF_BLOCK_ENTRIES, sizeof(*p));
1409 if (!p) {
1410 LOGE("Failed to allocate block of trace profile counters");
1411 dvmAbort();
1412 }
1413 gDvmJit.pJitTraceProfCounters->buckets[idx] = p;
1414 }
1415 res = &gDvmJit.pJitTraceProfCounters->buckets[idx][elem];
1416 gDvmJit.pJitTraceProfCounters->next++;
1417 return res;
1418}
1419
1420/*
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001421 * Float/double conversion requires clamping to min and max of integer form. If
1422 * target doesn't support this normally, use these.
1423 */
1424s8 dvmJitd2l(double d)
1425{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001426 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1427 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001428 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001429 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001430 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001431 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001432 else if (d != d) // NaN case
1433 return 0;
1434 else
1435 return (s8)d;
1436}
1437
1438s8 dvmJitf2l(float f)
1439{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001440 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1441 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001442 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001443 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001444 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001445 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001446 else if (f != f) // NaN case
1447 return 0;
1448 else
1449 return (s8)f;
1450}
1451
buzbee2e152ba2010-12-15 16:32:35 -08001452/* Should only be called by the compiler thread */
1453void dvmJitChangeProfileMode(TraceProfilingModes newState)
1454{
1455 if (gDvmJit.profileMode != newState) {
1456 gDvmJit.profileMode = newState;
1457 dvmJitUnchainAll();
1458 }
1459}
1460
1461void dvmJitTraceProfilingOn()
1462{
1463 if (gDvmJit.profileMode == kTraceProfilingPeriodicOff)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001464 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1465 (void*) kTraceProfilingPeriodicOn);
buzbee2e152ba2010-12-15 16:32:35 -08001466 else if (gDvmJit.profileMode == kTraceProfilingDisabled)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001467 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1468 (void*) kTraceProfilingContinuous);
buzbee2e152ba2010-12-15 16:32:35 -08001469}
1470
1471void dvmJitTraceProfilingOff()
1472{
1473 if (gDvmJit.profileMode == kTraceProfilingPeriodicOn)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001474 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1475 (void*) kTraceProfilingPeriodicOff);
buzbee2e152ba2010-12-15 16:32:35 -08001476 else if (gDvmJit.profileMode == kTraceProfilingContinuous)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001477 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1478 (void*) kTraceProfilingDisabled);
buzbee2e152ba2010-12-15 16:32:35 -08001479}
1480
buzbee9a3147c2011-03-02 15:43:48 -08001481/*
buzbee99e3e6e2011-03-29 10:26:07 -07001482 * Update JIT-specific info in Thread structure for a single thread
1483 */
1484void dvmJitUpdateThreadStateSingle(Thread* thread)
1485{
1486 thread->pJitProfTable = gDvmJit.pProfTable;
1487 thread->jitThreshold = gDvmJit.threshold;
1488}
1489
1490/*
buzbee9a3147c2011-03-02 15:43:48 -08001491 * Walk through the thread list and refresh all local copies of
1492 * JIT global state (which was placed there for fast access).
1493 */
buzbee99e3e6e2011-03-29 10:26:07 -07001494void dvmJitUpdateThreadStateAll()
buzbee9a3147c2011-03-02 15:43:48 -08001495{
1496 Thread* self = dvmThreadSelf();
1497 Thread* thread;
1498
1499 dvmLockThreadList(self);
1500 for (thread = gDvm.threadList; thread != NULL; thread = thread->next) {
buzbee99e3e6e2011-03-29 10:26:07 -07001501 dvmJitUpdateThreadStateSingle(thread);
buzbee9a3147c2011-03-02 15:43:48 -08001502 }
1503 dvmUnlockThreadList();
1504
1505}
Ben Chengba4fc8b2009-06-01 13:00:29 -07001506#endif /* WITH_JIT */