| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | |
| Dan Bornstein | df4daaf | 2010-12-01 14:23:44 -0800 | [diff] [blame] | 26 | #include "libdex/DexOpcodes.h" |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <pthread.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <signal.h> |
| 31 | #include "compiler/Compiler.h" |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 32 | #include "compiler/CompilerUtility.h" |
| 33 | #include "compiler/CompilerIR.h" |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 36 | #if defined(WITH_SELF_VERIFICATION) |
| 37 | /* Allocate space for per-thread ShadowSpace data structures */ |
| 38 | void* dvmSelfVerificationShadowSpaceAlloc(Thread* self) |
| 39 | { |
| 40 | self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace)); |
| 41 | if (self->shadowSpace == NULL) |
| 42 | return NULL; |
| 43 | |
| 44 | self->shadowSpace->registerSpaceSize = REG_SPACE; |
| 45 | self->shadowSpace->registerSpace = |
| 46 | (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int)); |
| 47 | |
| 48 | return self->shadowSpace->registerSpace; |
| 49 | } |
| 50 | |
| 51 | /* Free per-thread ShadowSpace data structures */ |
| 52 | void dvmSelfVerificationShadowSpaceFree(Thread* self) |
| 53 | { |
| 54 | free(self->shadowSpace->registerSpace); |
| 55 | free(self->shadowSpace); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Save out PC, FP, InterpState, and registers to shadow space. |
| 60 | * Return a pointer to the shadow space for JIT to use. |
| 61 | */ |
| 62 | void* dvmSelfVerificationSaveState(const u2* pc, const void* fp, |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 63 | InterpState* interpState, int targetTrace) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 64 | { |
| 65 | Thread *self = dvmThreadSelf(); |
| 66 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 67 | unsigned preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea); |
| 68 | unsigned postBytes = interpState->method->registersSize*4; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 69 | |
| 70 | //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x", |
| 71 | // self->threadId, (int)pc, (int)fp); |
| 72 | |
| 73 | if (shadowSpace->selfVerificationState != kSVSIdle) { |
| 74 | LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d", |
| 75 | self->threadId, shadowSpace->selfVerificationState); |
| 76 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 77 | LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 78 | } |
| 79 | shadowSpace->selfVerificationState = kSVSStart; |
| 80 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 81 | if (interpState->entryPoint == kInterpEntryResume) { |
| 82 | interpState->entryPoint = kInterpEntryInstr; |
| 83 | #if 0 |
| 84 | /* Tracking the success rate of resume after single-stepping */ |
| 85 | if (interpState->jitResumeDPC == pc) { |
| 86 | LOGD("SV single step resumed at %p", pc); |
| 87 | } |
| 88 | else { |
| 89 | LOGD("real %p DPC %p NPC %p", pc, interpState->jitResumeDPC, |
| 90 | interpState->jitResumeNPC); |
| 91 | } |
| 92 | #endif |
| 93 | } |
| 94 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 95 | // Dynamically grow shadow register space if necessary |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 96 | if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 97 | free(shadowSpace->registerSpace); |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 98 | shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 99 | shadowSpace->registerSpace = |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 100 | (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Remember original state |
| 104 | shadowSpace->startPC = pc; |
| 105 | shadowSpace->fp = fp; |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 106 | shadowSpace->glue = interpState; |
| 107 | /* |
| 108 | * Store the original method here in case the trace ends with a |
| 109 | * return/invoke, the last method. |
| 110 | */ |
| 111 | shadowSpace->method = interpState->method; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 112 | shadowSpace->shadowFP = shadowSpace->registerSpace + |
| 113 | shadowSpace->registerSpaceSize - postBytes/4; |
| 114 | |
| 115 | // Create a copy of the InterpState |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 116 | memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState)); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 117 | shadowSpace->interpState.fp = (u4*)shadowSpace->shadowFP; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 118 | shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace; |
| 119 | |
| 120 | // Create a copy of the stack |
| 121 | memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes, |
| 122 | preBytes+postBytes); |
| 123 | |
| 124 | // Setup the shadowed heap space |
| 125 | shadowSpace->heapSpaceTail = shadowSpace->heapSpace; |
| 126 | |
| 127 | // Reset trace length |
| 128 | shadowSpace->traceLength = 0; |
| 129 | |
| 130 | return shadowSpace; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Save ending PC, FP and compiled code exit point to shadow space. |
| 135 | * Return a pointer to the shadow space for JIT to restore state. |
| 136 | */ |
| 137 | void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 138 | SelfVerificationState exitState) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 139 | { |
| 140 | Thread *self = dvmThreadSelf(); |
| 141 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 142 | // Official InterpState structure |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 143 | InterpState *realGlue = (InterpState*)shadowSpace->glue; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 144 | shadowSpace->endPC = pc; |
| 145 | shadowSpace->endShadowFP = fp; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 146 | shadowSpace->jitExitState = exitState; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 147 | |
| 148 | //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x", |
| 149 | // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp, |
| 150 | // (int)pc); |
| 151 | |
| 152 | if (shadowSpace->selfVerificationState != kSVSStart) { |
| 153 | LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d", |
| 154 | self->threadId, shadowSpace->selfVerificationState); |
| 155 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 156 | LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 157 | (int)shadowSpace->endPC); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 158 | LOGD("Interp FP: 0x%x", (int)shadowSpace->fp); |
| 159 | LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 160 | (int)shadowSpace->endShadowFP); |
| 161 | } |
| 162 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 163 | // Move the resume [ND]PC from the shadow space to the real space so that |
| 164 | // the debug interpreter can return to the translation |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 165 | if (exitState == kSVSSingleStep) { |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 166 | realGlue->jitResumeNPC = shadowSpace->interpState.jitResumeNPC; |
| 167 | realGlue->jitResumeDPC = shadowSpace->interpState.jitResumeDPC; |
| 168 | } else { |
| 169 | realGlue->jitResumeNPC = NULL; |
| 170 | realGlue->jitResumeDPC = NULL; |
| 171 | } |
| 172 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 173 | // Special case when punting after a single instruction |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 174 | if (exitState == kSVSPunt && pc == shadowSpace->startPC) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 175 | shadowSpace->selfVerificationState = kSVSIdle; |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 176 | } else if (exitState == kSVSBackwardBranch && pc < shadowSpace->startPC) { |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 177 | /* |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 178 | * Consider a trace with a backward branch: |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 179 | * 1: .. |
| 180 | * 2: .. |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 181 | * 3: .. |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 182 | * 4: .. |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 183 | * 5: Goto {1 or 2 or 3 or 4} |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 184 | * |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 185 | * If there instruction 5 goes to 1 and there is no single-step |
| 186 | * instruction in the loop, pc is equal to shadowSpace->startPC and |
| 187 | * we will honor the backward branch condition. |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 188 | * |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 189 | * If the single-step instruction is outside the loop, then after |
| 190 | * resuming in the trace the startPC will be less than pc so we will |
| 191 | * also honor the backward branch condition. |
| 192 | * |
| 193 | * If the single-step is inside the loop, we won't hit the same endPC |
| 194 | * twice when the interpreter is re-executing the trace so we want to |
| 195 | * cancel the backward branch condition. In this case it can be |
| 196 | * detected as the endPC (ie pc) will be less than startPC. |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 197 | */ |
| 198 | shadowSpace->selfVerificationState = kSVSNormal; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 199 | } else { |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 200 | shadowSpace->selfVerificationState = exitState; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return shadowSpace; |
| 204 | } |
| 205 | |
| 206 | /* Print contents of virtual registers */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 207 | static void selfVerificationPrintRegisters(int* addr, int* addrRef, |
| 208 | int numWords) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 209 | { |
| 210 | int i; |
| 211 | for (i = 0; i < numWords; i++) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 212 | LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : ""); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Print values maintained in shadowSpace */ |
| 217 | static void selfVerificationDumpState(const u2* pc, Thread* self) |
| 218 | { |
| 219 | ShadowSpace* shadowSpace = self->shadowSpace; |
| 220 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| 221 | int frameBytes = (int) shadowSpace->registerSpace + |
| 222 | shadowSpace->registerSpaceSize*4 - |
| 223 | (int) shadowSpace->shadowFP; |
| 224 | int localRegs = 0; |
| 225 | int frameBytes2 = 0; |
| 226 | if (self->curFrame < shadowSpace->fp) { |
| 227 | localRegs = (stackSave->method->registersSize - |
| 228 | stackSave->method->insSize)*4; |
| 229 | frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs; |
| 230 | } |
| 231 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 232 | LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 233 | (int)(pc - stackSave->method->insns)); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 234 | LOGD("Class: %s", shadowSpace->method->clazz->descriptor); |
| 235 | LOGD("Method: %s", shadowSpace->method->name); |
| 236 | LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 237 | (int)shadowSpace->endPC); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 238 | LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 239 | (int)self->curFrame); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 240 | LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 241 | (int)shadowSpace->endShadowFP); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 242 | LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 243 | localRegs, frameBytes2); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 244 | LOGD("Trace length: %d State: %d", shadowSpace->traceLength, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 245 | shadowSpace->selfVerificationState); |
| 246 | } |
| 247 | |
| 248 | /* Print decoded instructions in the current trace */ |
| 249 | static void selfVerificationDumpTrace(const u2* pc, Thread* self) |
| 250 | { |
| 251 | ShadowSpace* shadowSpace = self->shadowSpace; |
| 252 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 253 | int i, addr, offset; |
| 254 | DecodedInstruction *decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 255 | |
| 256 | LOGD("********** SHADOW TRACE DUMP **********"); |
| 257 | for (i = 0; i < shadowSpace->traceLength; i++) { |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 258 | addr = shadowSpace->trace[i].addr; |
| 259 | offset = (int)((u2*)addr - stackSave->method->insns); |
| 260 | decInsn = &(shadowSpace->trace[i].decInsn); |
| 261 | /* Not properly decoding instruction, some registers may be garbage */ |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 262 | LOGD("0x%x: (0x%04x) %s", |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 263 | addr, offset, dexGetOpcodeName(decInsn->opcode)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 267 | /* Code is forced into this spin loop when a divergence is detected */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 268 | static void selfVerificationSpinLoop(ShadowSpace *shadowSpace) |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 269 | { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 270 | const u2 *startPC = shadowSpace->startPC; |
| Ben Cheng | 88a0f97 | 2010-02-24 15:00:40 -0800 | [diff] [blame] | 271 | JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 272 | if (desc) { |
| 273 | dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc); |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 274 | /* |
| 275 | * This function effectively terminates the VM right here, so not |
| 276 | * freeing the desc pointer when the enqueuing fails is acceptable. |
| 277 | */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 278 | } |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 279 | gDvmJit.selfVerificationSpin = true; |
| 280 | while(gDvmJit.selfVerificationSpin) sleep(10); |
| 281 | } |
| 282 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 283 | /* Manage self verification while in the debug interpreter */ |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 284 | static bool selfVerificationDebugInterp(const u2* pc, Thread* self, |
| 285 | InterpState *interpState) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 286 | { |
| 287 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 288 | SelfVerificationState state = shadowSpace->selfVerificationState; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 289 | |
| 290 | DecodedInstruction decInsn; |
| Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 291 | dexDecodeInstruction(pc, &decInsn); |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 292 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 293 | //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s", |
| 294 | // self->threadId, (int)pc, (int)shadowSpace->endPC, state, |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 295 | // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opcode)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 296 | |
| 297 | if (state == kSVSIdle || state == kSVSStart) { |
| 298 | LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d", |
| 299 | self->threadId, state); |
| 300 | selfVerificationDumpState(pc, self); |
| 301 | selfVerificationDumpTrace(pc, self); |
| 302 | } |
| 303 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 304 | /* |
| 305 | * Skip endPC once when trace has a backward branch. If the SV state is |
| 306 | * single step, keep it that way. |
| 307 | */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 308 | if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) || |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 309 | (state != kSVSBackwardBranch && state != kSVSSingleStep)) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 310 | shadowSpace->selfVerificationState = kSVSDebugInterp; |
| 311 | } |
| 312 | |
| 313 | /* Check that the current pc is the end of the trace */ |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 314 | if ((state == kSVSDebugInterp || state == kSVSSingleStep) && |
| 315 | pc == shadowSpace->endPC) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 316 | |
| 317 | shadowSpace->selfVerificationState = kSVSIdle; |
| 318 | |
| 319 | /* Check register space */ |
| 320 | int frameBytes = (int) shadowSpace->registerSpace + |
| 321 | shadowSpace->registerSpaceSize*4 - |
| 322 | (int) shadowSpace->shadowFP; |
| 323 | if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 324 | LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 325 | selfVerificationDumpState(pc, self); |
| 326 | selfVerificationDumpTrace(pc, self); |
| 327 | LOGD("*** Interp Registers: addr: 0x%x bytes: %d", |
| 328 | (int)shadowSpace->fp, frameBytes); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 329 | selfVerificationPrintRegisters((int*)shadowSpace->fp, |
| 330 | (int*)shadowSpace->shadowFP, |
| 331 | frameBytes/4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 332 | LOGD("*** Shadow Registers: addr: 0x%x bytes: %d", |
| 333 | (int)shadowSpace->shadowFP, frameBytes); |
| 334 | selfVerificationPrintRegisters((int*)shadowSpace->shadowFP, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 335 | (int*)shadowSpace->fp, |
| 336 | frameBytes/4); |
| 337 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 338 | } |
| 339 | /* Check new frame if it exists (invokes only) */ |
| 340 | if (self->curFrame < shadowSpace->fp) { |
| 341 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| 342 | int localRegs = (stackSave->method->registersSize - |
| 343 | stackSave->method->insSize)*4; |
| 344 | int frameBytes2 = (int) shadowSpace->fp - |
| 345 | (int) self->curFrame - localRegs; |
| 346 | if (memcmp(((char*)self->curFrame)+localRegs, |
| 347 | ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 348 | LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!", |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 349 | self->threadId); |
| 350 | selfVerificationDumpState(pc, self); |
| 351 | selfVerificationDumpTrace(pc, self); |
| 352 | LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d", |
| 353 | (int)self->curFrame, localRegs, frameBytes2); |
| 354 | selfVerificationPrintRegisters((int*)self->curFrame, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 355 | (int*)shadowSpace->endShadowFP, |
| 356 | (frameBytes2+localRegs)/4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 357 | LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d", |
| 358 | (int)shadowSpace->endShadowFP, localRegs, frameBytes2); |
| 359 | selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 360 | (int*)self->curFrame, |
| 361 | (frameBytes2+localRegs)/4); |
| 362 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /* Check memory space */ |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 367 | bool memDiff = false; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 368 | ShadowHeap* heapSpacePtr; |
| 369 | for (heapSpacePtr = shadowSpace->heapSpace; |
| 370 | heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) { |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 371 | int memData = *((unsigned int*) heapSpacePtr->addr); |
| 372 | if (heapSpacePtr->data != memData) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 373 | LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId); |
| 374 | LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x", |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 375 | heapSpacePtr->addr, memData, heapSpacePtr->data); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 376 | selfVerificationDumpState(pc, self); |
| 377 | selfVerificationDumpTrace(pc, self); |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 378 | memDiff = true; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 381 | if (memDiff) selfVerificationSpinLoop(shadowSpace); |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * Switch to JIT single step mode to stay in the debug interpreter for |
| 385 | * one more instruction |
| 386 | */ |
| 387 | if (state == kSVSSingleStep) { |
| 388 | interpState->jitState = kJitSingleStepEnd; |
| 389 | } |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 390 | return true; |
| 391 | |
| 392 | /* If end not been reached, make sure max length not exceeded */ |
| 393 | } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) { |
| 394 | LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 395 | LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x", |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 396 | (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc); |
| 397 | selfVerificationDumpState(pc, self); |
| 398 | selfVerificationDumpTrace(pc, self); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 399 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 400 | |
| 401 | return true; |
| 402 | } |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 403 | /* Log the instruction address and decoded instruction for debug */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 404 | shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 405 | shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 406 | shadowSpace->traceLength++; |
| 407 | |
| 408 | return false; |
| 409 | } |
| 410 | #endif |
| 411 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 412 | /* |
| 413 | * If one of our fixed tables or the translation buffer fills up, |
| 414 | * call this routine to avoid wasting cycles on future translation requests. |
| 415 | */ |
| 416 | void dvmJitStopTranslationRequests() |
| 417 | { |
| 418 | /* |
| 419 | * Note 1: This won't necessarily stop all translation requests, and |
| 420 | * operates on a delayed mechanism. Running threads look to the copy |
| 421 | * of this value in their private InterpState structures and won't see |
| 422 | * this change until it is refreshed (which happens on interpreter |
| 423 | * entry). |
| 424 | * Note 2: This is a one-shot memory leak on this table. Because this is a |
| 425 | * permanent off switch for Jit profiling, it is a one-time leak of 1K |
| 426 | * bytes, and no further attempt will be made to re-allocate it. Can't |
| 427 | * free it because some thread may be holding a reference. |
| 428 | */ |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 429 | gDvmJit.pProfTable = NULL; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 432 | #if defined(WITH_JIT_TUNING) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 433 | /* Convenience function to increment counter from assembly code */ |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 434 | void dvmBumpNoChain(int from) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 435 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 436 | gDvmJit.noChainExit[from]++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Convenience function to increment counter from assembly code */ |
| 440 | void dvmBumpNormal() |
| 441 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 442 | gDvmJit.normalExit++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Convenience function to increment counter from assembly code */ |
| 446 | void dvmBumpPunt(int from) |
| 447 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 448 | gDvmJit.puntExit++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 449 | } |
| 450 | #endif |
| 451 | |
| 452 | /* Dumps debugging & tuning stats to the log */ |
| 453 | void dvmJitStats() |
| 454 | { |
| 455 | int i; |
| 456 | int hit; |
| 457 | int not_hit; |
| 458 | int chains; |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 459 | int stubs; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 460 | if (gDvmJit.pJitEntryTable) { |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 461 | for (i=0, stubs=chains=hit=not_hit=0; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 462 | i < (int) gDvmJit.jitTableSize; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 463 | i++) { |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 464 | if (gDvmJit.pJitEntryTable[i].dPC != 0) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 465 | hit++; |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 466 | if (gDvmJit.pJitEntryTable[i].codeAddress == |
| Bill Buzbee | bd04724 | 2010-05-13 13:02:53 -0700 | [diff] [blame] | 467 | dvmCompilerGetInterpretTemplate()) |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 468 | stubs++; |
| 469 | } else |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 470 | not_hit++; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 471 | if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 472 | chains++; |
| 473 | } |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 474 | LOGD("JIT: table size is %d, entries used is %d", |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 475 | gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed); |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 476 | LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s", |
| 477 | hit, not_hit + hit, chains, gDvmJit.threshold, |
| 478 | gDvmJit.blockingMode ? "Blocking" : "Non-blocking"); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 479 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 480 | #if defined(WITH_JIT_TUNING) |
| 481 | LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches); |
| 482 | |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 483 | LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt", |
| 484 | gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound, |
| 485 | gDvmJit.normalExit, gDvmJit.puntExit); |
| Ben Cheng | 452efba | 2010-04-30 15:14:00 -0700 | [diff] [blame] | 486 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 487 | LOGD("JIT: ICHits: %d", gDvmICHitCount); |
| 488 | |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 489 | LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, " |
| 490 | "%d switch overflow", |
| 491 | gDvmJit.noChainExit[kInlineCacheMiss], |
| 492 | gDvmJit.noChainExit[kCallsiteInterpreted], |
| 493 | gDvmJit.noChainExit[kSwitchOverflow]); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 494 | |
| Ben Cheng | b88ec3c | 2010-05-17 12:50:33 -0700 | [diff] [blame] | 495 | LOGD("JIT: ICPatch: %d init, %d rejected, %d lock-free, %d queued, " |
| 496 | "%d dropped", |
| 497 | gDvmJit.icPatchInit, gDvmJit.icPatchRejected, |
| 498 | gDvmJit.icPatchLockFree, gDvmJit.icPatchQueued, |
| Ben Cheng | 452efba | 2010-04-30 15:14:00 -0700 | [diff] [blame] | 499 | gDvmJit.icPatchDropped); |
| 500 | |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 501 | LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return", |
| 502 | gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic, |
| 503 | gDvmJit.invokeNative, gDvmJit.returnOp); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 504 | LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter", |
| 505 | gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined, |
| 506 | gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 507 | LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000); |
| 508 | LOGD("JIT: Avg unit compilation time: %llu us", |
| 509 | gDvmJit.jitTime / gDvmJit.numCompilations); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 510 | #endif |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 511 | |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 512 | LOGD("JIT: %d Translation chains, %d interp stubs", |
| 513 | gDvmJit.translationChains, stubs); |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 514 | if (gDvmJit.profileMode == kTraceProfilingContinuous) { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 515 | dvmCompilerSortAndPrintTraceProfiles(); |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 516 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 520 | |
| Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame] | 521 | static void setTraceConstruction(JitEntry *slot, bool value) |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 522 | { |
| 523 | |
| 524 | JitEntryInfoUnion oldValue; |
| 525 | JitEntryInfoUnion newValue; |
| 526 | do { |
| 527 | oldValue = slot->u; |
| 528 | newValue = oldValue; |
| 529 | newValue.info.traceConstruction = value; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 530 | } while (android_atomic_release_cas(oldValue.infoWord, newValue.infoWord, |
| 531 | &slot->u.infoWord) != 0); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame] | 534 | static void resetTracehead(InterpState* interpState, JitEntry *slot) |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 535 | { |
| Bill Buzbee | bd04724 | 2010-05-13 13:02:53 -0700 | [diff] [blame] | 536 | slot->codeAddress = dvmCompilerGetInterpretTemplate(); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 537 | setTraceConstruction(slot, false); |
| 538 | } |
| 539 | |
| 540 | /* Clean up any pending trace builds */ |
| 541 | void dvmJitAbortTraceSelect(InterpState* interpState) |
| 542 | { |
| 543 | if (interpState->jitState == kJitTSelect) |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 544 | interpState->jitState = kJitDone; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 547 | /* |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 548 | * Find an entry in the JitTable, creating if necessary. |
| 549 | * Returns null if table is full. |
| 550 | */ |
| 551 | static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked) |
| 552 | { |
| 553 | u4 chainEndMarker = gDvmJit.jitTableSize; |
| 554 | u4 idx = dvmJitHash(dPC); |
| 555 | |
| 556 | /* Walk the bucket chain to find an exact match for our PC */ |
| 557 | while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) && |
| 558 | (gDvmJit.pJitEntryTable[idx].dPC != dPC)) { |
| 559 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 560 | } |
| 561 | |
| 562 | if (gDvmJit.pJitEntryTable[idx].dPC != dPC) { |
| 563 | /* |
| 564 | * No match. Aquire jitTableLock and find the last |
| 565 | * slot in the chain. Possibly continue the chain walk in case |
| 566 | * some other thread allocated the slot we were looking |
| 567 | * at previuosly (perhaps even the dPC we're trying to enter). |
| 568 | */ |
| 569 | if (!callerLocked) |
| 570 | dvmLockMutex(&gDvmJit.tableLock); |
| 571 | /* |
| 572 | * At this point, if .dPC is NULL, then the slot we're |
| 573 | * looking at is the target slot from the primary hash |
| 574 | * (the simple, and common case). Otherwise we're going |
| 575 | * to have to find a free slot and chain it. |
| 576 | */ |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 577 | ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */ |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 578 | if (gDvmJit.pJitEntryTable[idx].dPC != NULL) { |
| 579 | u4 prev; |
| 580 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 581 | if (gDvmJit.pJitEntryTable[idx].dPC == dPC) { |
| 582 | /* Another thread got there first for this dPC */ |
| 583 | if (!callerLocked) |
| 584 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 585 | return &gDvmJit.pJitEntryTable[idx]; |
| 586 | } |
| 587 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 588 | } |
| 589 | /* Here, idx should be pointing to the last cell of an |
| 590 | * active chain whose last member contains a valid dPC */ |
| 591 | assert(gDvmJit.pJitEntryTable[idx].dPC != NULL); |
| 592 | /* Linear walk to find a free cell and add it to the end */ |
| 593 | prev = idx; |
| 594 | while (true) { |
| 595 | idx++; |
| 596 | if (idx == chainEndMarker) |
| 597 | idx = 0; /* Wraparound */ |
| 598 | if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) || |
| 599 | (idx == prev)) |
| 600 | break; |
| 601 | } |
| 602 | if (idx != prev) { |
| 603 | JitEntryInfoUnion oldValue; |
| 604 | JitEntryInfoUnion newValue; |
| 605 | /* |
| 606 | * Although we hold the lock so that noone else will |
| 607 | * be trying to update a chain field, the other fields |
| 608 | * packed into the word may be in use by other threads. |
| 609 | */ |
| 610 | do { |
| 611 | oldValue = gDvmJit.pJitEntryTable[prev].u; |
| 612 | newValue = oldValue; |
| 613 | newValue.info.chain = idx; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 614 | } while (android_atomic_release_cas(oldValue.infoWord, |
| 615 | newValue.infoWord, |
| 616 | &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0); |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | if (gDvmJit.pJitEntryTable[idx].dPC == NULL) { |
| 620 | /* |
| 621 | * Initialize codeAddress and allocate the slot. Must |
| 622 | * happen in this order (since dPC is set, the entry is live. |
| 623 | */ |
| 624 | gDvmJit.pJitEntryTable[idx].dPC = dPC; |
| 625 | gDvmJit.jitTableEntriesUsed++; |
| 626 | } else { |
| 627 | /* Table is full */ |
| 628 | idx = chainEndMarker; |
| 629 | } |
| 630 | if (!callerLocked) |
| 631 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 632 | } |
| 633 | return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx]; |
| 634 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 635 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 636 | /* |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 637 | * Append the class ptr of "this" and the current method ptr to the current |
| 638 | * trace. That is, the trace runs will contain the following components: |
| 639 | * + trace run that ends with an invoke (existing entry) |
| 640 | * + thisClass (new) |
| 641 | * + calleeMethod (new) |
| 642 | */ |
| 643 | static void insertClassMethodInfo(InterpState* interpState, |
| 644 | const ClassObject* thisClass, |
| 645 | const Method* calleeMethod, |
| 646 | const DecodedInstruction* insn) |
| 647 | { |
| 648 | int currTraceRun = ++interpState->currTraceRun; |
| 649 | interpState->trace[currTraceRun].meta = (void *) thisClass; |
| 650 | currTraceRun = ++interpState->currTraceRun; |
| 651 | interpState->trace[currTraceRun].meta = (void *) calleeMethod; |
| 652 | } |
| 653 | |
| 654 | /* |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 655 | * Check if the next instruction following the invoke is a move-result and if |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 656 | * so add it to the trace. That is, this will add the trace run that includes |
| 657 | * the move-result to the trace list. |
| 658 | * |
| 659 | * + trace run that ends with an invoke (existing entry) |
| 660 | * + thisClass (existing entry) |
| 661 | * + calleeMethod (existing entry) |
| 662 | * + move result (new) |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 663 | * |
| 664 | * lastPC, len, offset are all from the preceding invoke instruction |
| 665 | */ |
| 666 | static void insertMoveResult(const u2 *lastPC, int len, int offset, |
| 667 | InterpState *interpState) |
| 668 | { |
| 669 | DecodedInstruction nextDecInsn; |
| 670 | const u2 *moveResultPC = lastPC + len; |
| 671 | |
| Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 672 | dexDecodeInstruction(moveResultPC, &nextDecInsn); |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 673 | if ((nextDecInsn.opcode != OP_MOVE_RESULT) && |
| 674 | (nextDecInsn.opcode != OP_MOVE_RESULT_WIDE) && |
| 675 | (nextDecInsn.opcode != OP_MOVE_RESULT_OBJECT)) |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 676 | return; |
| 677 | |
| 678 | /* We need to start a new trace run */ |
| 679 | int currTraceRun = ++interpState->currTraceRun; |
| 680 | interpState->currRunHead = moveResultPC; |
| 681 | interpState->trace[currTraceRun].frag.startOffset = offset + len; |
| 682 | interpState->trace[currTraceRun].frag.numInsts = 1; |
| 683 | interpState->trace[currTraceRun].frag.runEnd = false; |
| 684 | interpState->trace[currTraceRun].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 685 | interpState->trace[currTraceRun].frag.isCode = true; |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 686 | interpState->totalTraceLen++; |
| 687 | |
| Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 688 | interpState->currRunLen = dexGetWidthFromInstruction(moveResultPC); |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 692 | * Adds to the current trace request one instruction at a time, just |
| 693 | * before that instruction is interpreted. This is the primary trace |
| 694 | * selection function. NOTE: return instruction are handled a little |
| 695 | * differently. In general, instructions are "proposed" to be added |
| 696 | * to the current trace prior to interpretation. If the interpreter |
| 697 | * then successfully completes the instruction, is will be considered |
| 698 | * part of the request. This allows us to examine machine state prior |
| 699 | * to interpretation, and also abort the trace request if the instruction |
| 700 | * throws or does something unexpected. However, return instructions |
| 701 | * will cause an immediate end to the translation request - which will |
| 702 | * be passed to the compiler before the return completes. This is done |
| 703 | * in response to special handling of returns by the interpreter (and |
| 704 | * because returns cannot throw in a way that causes problems for the |
| 705 | * translated code. |
| 706 | */ |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 707 | int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState, |
| 708 | const ClassObject* thisClass, const Method* curMethod) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 709 | { |
| Carl Shapiro | e3c01da | 2010-05-20 22:54:18 -0700 | [diff] [blame] | 710 | int flags, len; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 711 | int switchInterp = false; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 712 | bool debugOrProfile = dvmDebuggerOrProfilerActive(); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 713 | /* Stay in the dbg interpreter for the next instruction */ |
| 714 | bool stayOneMoreInst = false; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 715 | |
| Ben Cheng | 1c52e6d | 2010-07-02 13:00:39 -0700 | [diff] [blame] | 716 | /* |
| 717 | * Bug 2710533 - dalvik crash when disconnecting debugger |
| 718 | * |
| 719 | * Reset the entry point to the default value. If needed it will be set to a |
| 720 | * specific value in the corresponding case statement (eg kJitSingleStepEnd) |
| 721 | */ |
| 722 | interpState->entryPoint = kInterpEntryInstr; |
| 723 | |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 724 | /* Prepare to handle last PC and stage the current PC */ |
| 725 | const u2 *lastPC = interpState->lastPC; |
| 726 | interpState->lastPC = pc; |
| 727 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 728 | switch (interpState->jitState) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 729 | int offset; |
| 730 | DecodedInstruction decInsn; |
| 731 | case kJitTSelect: |
| Ben Cheng | dc84bb2 | 2009-10-02 12:58:52 -0700 | [diff] [blame] | 732 | /* First instruction - just remember the PC and exit */ |
| 733 | if (lastPC == NULL) break; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 734 | /* Grow the trace around the last PC if jitState is kJitTSelect */ |
| Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 735 | dexDecodeInstruction(lastPC, &decInsn); |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due |
| 739 | * to the amount of space it takes to generate the chaining |
| 740 | * cells. |
| 741 | */ |
| 742 | if (interpState->totalTraceLen != 0 && |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 743 | (decInsn.opcode == OP_PACKED_SWITCH || |
| 744 | decInsn.opcode == OP_SPARSE_SWITCH)) { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 745 | interpState->jitState = kJitTSelectEnd; |
| 746 | break; |
| 747 | } |
| 748 | |
| Bill Buzbee | f9f3328 | 2009-11-22 12:45:30 -0800 | [diff] [blame] | 749 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 750 | #if defined(SHOW_TRACE) |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 751 | LOGD("TraceGen: adding %s", dexGetOpcodeName(decInsn.opcode)); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 752 | #endif |
| Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 753 | flags = dexGetFlagsFromOpcode(decInsn.opcode); |
| 754 | len = dexGetWidthFromInstruction(lastPC); |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 755 | offset = lastPC - interpState->method->insns; |
| 756 | assert((unsigned) offset < |
| 757 | dvmGetMethodInsnsSize(interpState->method)); |
| 758 | if (lastPC != interpState->currRunHead + interpState->currRunLen) { |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 759 | int currTraceRun; |
| 760 | /* We need to start a new trace run */ |
| 761 | currTraceRun = ++interpState->currTraceRun; |
| 762 | interpState->currRunLen = 0; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 763 | interpState->currRunHead = (u2*)lastPC; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 764 | interpState->trace[currTraceRun].frag.startOffset = offset; |
| 765 | interpState->trace[currTraceRun].frag.numInsts = 0; |
| 766 | interpState->trace[currTraceRun].frag.runEnd = false; |
| 767 | interpState->trace[currTraceRun].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 768 | interpState->trace[currTraceRun].frag.isCode = true; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 769 | } |
| 770 | interpState->trace[interpState->currTraceRun].frag.numInsts++; |
| 771 | interpState->totalTraceLen++; |
| 772 | interpState->currRunLen += len; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 773 | |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 774 | /* |
| 775 | * If the last instruction is an invoke, we will try to sneak in |
| 776 | * the move-result* (if existent) into a separate trace run. |
| 777 | */ |
| 778 | int needReservedRun = (flags & kInstrInvoke) ? 1 : 0; |
| 779 | |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 780 | /* Will probably never hit this with the current trace buildier */ |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 781 | if (interpState->currTraceRun == |
| 782 | (MAX_JIT_RUN_LEN - 1 - needReservedRun)) { |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 783 | interpState->jitState = kJitTSelectEnd; |
| 784 | } |
| 785 | |
| Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 786 | if (!dexIsGoto(flags) && |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 787 | ((flags & (kInstrCanBranch | |
| 788 | kInstrCanSwitch | |
| 789 | kInstrCanReturn | |
| 790 | kInstrInvoke)) != 0)) { |
| 791 | interpState->jitState = kJitTSelectEnd; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 792 | #if defined(SHOW_TRACE) |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 793 | LOGD("TraceGen: ending on %s, basic block end", |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 794 | dexGetOpcodeName(decInsn.opcode)); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 795 | #endif |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 796 | |
| 797 | /* |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 798 | * If the current invoke is a {virtual,interface}, get the |
| 799 | * current class/method pair into the trace as well. |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 800 | * If the next instruction is a variant of move-result, insert |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 801 | * it to the trace too. |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 802 | */ |
| 803 | if (flags & kInstrInvoke) { |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 804 | insertClassMethodInfo(interpState, thisClass, curMethod, |
| 805 | &decInsn); |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 806 | insertMoveResult(lastPC, len, offset, interpState); |
| 807 | } |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 808 | } |
| Bill Buzbee | 2ce8a6c | 2009-12-03 15:09:32 -0800 | [diff] [blame] | 809 | /* Break on throw or self-loop */ |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 810 | if ((decInsn.opcode == OP_THROW) || (lastPC == pc)){ |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 811 | interpState->jitState = kJitTSelectEnd; |
| 812 | } |
| 813 | if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) { |
| 814 | interpState->jitState = kJitTSelectEnd; |
| 815 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 816 | /* Abandon the trace request if debugger/profiler is attached */ |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 817 | if (debugOrProfile) { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 818 | interpState->jitState = kJitDone; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 819 | break; |
| 820 | } |
| 821 | if ((flags & kInstrCanReturn) != kInstrCanReturn) { |
| 822 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 823 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 824 | else { |
| 825 | /* |
| 826 | * Last instruction is a return - stay in the dbg interpreter |
| 827 | * for one more instruction if it is a non-void return, since |
| 828 | * we don't want to start a trace with move-result as the first |
| 829 | * instruction (which is already included in the trace |
| 830 | * containing the invoke. |
| 831 | */ |
| Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 832 | if (decInsn.opcode != OP_RETURN_VOID) { |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 833 | stayOneMoreInst = true; |
| 834 | } |
| 835 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 836 | /* NOTE: intentional fallthrough for returns */ |
| 837 | case kJitTSelectEnd: |
| 838 | { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 839 | /* Bad trace */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 840 | if (interpState->totalTraceLen == 0) { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 841 | /* Bad trace - mark as untranslatable */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 842 | interpState->jitState = kJitDone; |
| 843 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 844 | break; |
| 845 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 846 | |
| 847 | int lastTraceDesc = interpState->currTraceRun; |
| 848 | |
| 849 | /* Extend a new empty desc if the last slot is meta info */ |
| 850 | if (!interpState->trace[lastTraceDesc].frag.isCode) { |
| 851 | lastTraceDesc = ++interpState->currTraceRun; |
| 852 | interpState->trace[lastTraceDesc].frag.startOffset = 0; |
| 853 | interpState->trace[lastTraceDesc].frag.numInsts = 0; |
| 854 | interpState->trace[lastTraceDesc].frag.hint = kJitHintNone; |
| 855 | interpState->trace[lastTraceDesc].frag.isCode = true; |
| 856 | } |
| 857 | |
| 858 | /* Mark the end of the trace runs */ |
| 859 | interpState->trace[lastTraceDesc].frag.runEnd = true; |
| 860 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 861 | JitTraceDescription* desc = |
| 862 | (JitTraceDescription*)malloc(sizeof(JitTraceDescription) + |
| 863 | sizeof(JitTraceRun) * (interpState->currTraceRun+1)); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 864 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 865 | if (desc == NULL) { |
| 866 | LOGE("Out of memory in trace selection"); |
| 867 | dvmJitStopTranslationRequests(); |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 868 | interpState->jitState = kJitDone; |
| 869 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 870 | break; |
| 871 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 872 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 873 | desc->method = interpState->method; |
| 874 | memcpy((char*)&(desc->trace[0]), |
| 875 | (char*)&(interpState->trace[0]), |
| 876 | sizeof(JitTraceRun) * (interpState->currTraceRun+1)); |
| 877 | #if defined(SHOW_TRACE) |
| 878 | LOGD("TraceGen: trace done, adding to queue"); |
| 879 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 880 | if (dvmCompilerWorkEnqueue( |
| 881 | interpState->currTraceHead,kWorkOrderTrace,desc)) { |
| 882 | /* Work order successfully enqueued */ |
| 883 | if (gDvmJit.blockingMode) { |
| 884 | dvmCompilerDrainQueue(); |
| 885 | } |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 886 | } else { |
| 887 | /* |
| 888 | * Make sure the descriptor for the abandoned work order is |
| 889 | * freed. |
| 890 | */ |
| 891 | free(desc); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 892 | } |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 893 | /* |
| 894 | * Reset "trace in progress" flag whether or not we |
| 895 | * successfully entered a work order. |
| 896 | */ |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 897 | JitEntry *jitEntry = |
| 898 | lookupAndAdd(interpState->currTraceHead, false); |
| 899 | if (jitEntry) { |
| 900 | setTraceConstruction(jitEntry, false); |
| 901 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 902 | interpState->jitState = kJitDone; |
| 903 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 904 | } |
| 905 | break; |
| 906 | case kJitSingleStep: |
| 907 | interpState->jitState = kJitSingleStepEnd; |
| 908 | break; |
| 909 | case kJitSingleStepEnd: |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 910 | /* |
| 911 | * Clear the inJitCodeCache flag and abandon the resume attempt if |
| 912 | * we cannot switch back to the translation due to corner-case |
| 913 | * conditions. If the flag is not cleared and the code cache is full |
| 914 | * we will be stuck in the debug interpreter as the code cache |
| 915 | * cannot be reset. |
| 916 | */ |
| 917 | if (dvmJitStayInPortableInterpreter()) { |
| 918 | interpState->entryPoint = kInterpEntryInstr; |
| 919 | self->inJitCodeCache = 0; |
| 920 | } else { |
| 921 | interpState->entryPoint = kInterpEntryResume; |
| 922 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 923 | interpState->jitState = kJitDone; |
| 924 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 925 | break; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 926 | case kJitDone: |
| 927 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 928 | break; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 929 | #if defined(WITH_SELF_VERIFICATION) |
| 930 | case kJitSelfVerification: |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 931 | if (selfVerificationDebugInterp(pc, self, interpState)) { |
| 932 | /* |
| 933 | * If the next state is not single-step end, we can switch |
| 934 | * interpreter now. |
| 935 | */ |
| 936 | if (interpState->jitState != kJitSingleStepEnd) { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 937 | interpState->jitState = kJitDone; |
| 938 | switchInterp = true; |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 939 | } |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 940 | } |
| 941 | break; |
| 942 | #endif |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 943 | case kJitNot: |
| Ben Cheng | 1c52e6d | 2010-07-02 13:00:39 -0700 | [diff] [blame] | 944 | switchInterp = !debugOrProfile; |
| Ben Cheng | ed79ff0 | 2009-10-13 13:26:40 -0700 | [diff] [blame] | 945 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 946 | default: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 947 | LOGE("Unexpected JIT state: %d entry point: %d", |
| 948 | interpState->jitState, interpState->entryPoint); |
| 949 | dvmAbort(); |
| Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 950 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 951 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 952 | /* |
| 953 | * Final check to see if we can really switch the interpreter. Make sure |
| 954 | * the jitState is kJitDone or kJitNot when switchInterp is set to true. |
| 955 | */ |
| 956 | assert(switchInterp == false || interpState->jitState == kJitDone || |
| 957 | interpState->jitState == kJitNot); |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 958 | return switchInterp && !debugOrProfile && !stayOneMoreInst && |
| 959 | !dvmJitStayInPortableInterpreter(); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 962 | JitEntry *dvmFindJitEntry(const u2* pc) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 963 | { |
| 964 | int idx = dvmJitHash(pc); |
| 965 | |
| 966 | /* Expect a high hit rate on 1st shot */ |
| 967 | if (gDvmJit.pJitEntryTable[idx].dPC == pc) |
| 968 | return &gDvmJit.pJitEntryTable[idx]; |
| 969 | else { |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 970 | int chainEndMarker = gDvmJit.jitTableSize; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 971 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 972 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 973 | if (gDvmJit.pJitEntryTable[idx].dPC == pc) |
| 974 | return &gDvmJit.pJitEntryTable[idx]; |
| 975 | } |
| 976 | } |
| 977 | return NULL; |
| 978 | } |
| 979 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 980 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 981 | * If a translated code address exists for the davik byte code |
| 982 | * pointer return it. This routine needs to be fast. |
| 983 | */ |
| 984 | void* dvmJitGetCodeAddr(const u2* dPC) |
| 985 | { |
| 986 | int idx = dvmJitHash(dPC); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 987 | const u2* npc = gDvmJit.pJitEntryTable[idx].dPC; |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 988 | if (npc != NULL) { |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 989 | bool hideTranslation = dvmJitHideTranslation(); |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 990 | if (npc == dPC) { |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 991 | int offset = (gDvmJit.profileMode >= kTraceProfilingContinuous) ? |
| 992 | 0 : gDvmJit.pJitEntryTable[idx].u.info.profileOffset; |
| 993 | intptr_t codeAddress = |
| 994 | (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress; |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 995 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 996 | gDvmJit.addrLookupsFound++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 997 | #endif |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 998 | return hideTranslation ? NULL : (void *)(codeAddress + offset); |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 999 | } else { |
| 1000 | int chainEndMarker = gDvmJit.jitTableSize; |
| 1001 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 1002 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 1003 | if (gDvmJit.pJitEntryTable[idx].dPC == dPC) { |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1004 | int offset = (gDvmJit.profileMode >= |
| 1005 | kTraceProfilingContinuous) ? 0 : |
| 1006 | gDvmJit.pJitEntryTable[idx].u.info.profileOffset; |
| 1007 | intptr_t codeAddress = |
| 1008 | (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress; |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 1009 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 1010 | gDvmJit.addrLookupsFound++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1011 | #endif |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1012 | return hideTranslation ? NULL : |
| 1013 | (void *)(codeAddress + offset); |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 1014 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | } |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 1018 | #if defined(WITH_JIT_TUNING) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1019 | gDvmJit.addrLookupsNotFound++; |
| 1020 | #endif |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Register the translated code pointer into the JitTable. |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 1026 | * NOTE: Once a codeAddress field transitions from initial state to |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1027 | * JIT'd code, it must not be altered without first halting all |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1028 | * threads. This routine should only be called by the compiler |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1029 | * thread. We defer the setting of the profile prefix size until |
| 1030 | * after the new code address is set to ensure that the prefix offset |
| 1031 | * is never applied to the initial interpret-only translation. All |
| 1032 | * translations with non-zero profile prefixes will still be correct |
| 1033 | * if entered as if the profile offset is 0, but the interpret-only |
| 1034 | * template cannot handle a non-zero prefix. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1035 | */ |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1036 | void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set, |
| 1037 | int profilePrefixSize) |
| 1038 | { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1039 | JitEntryInfoUnion oldValue; |
| 1040 | JitEntryInfoUnion newValue; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1041 | JitEntry *jitEntry = lookupAndAdd(dPC, false); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1042 | assert(jitEntry); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1043 | /* Note: order of update is important */ |
| 1044 | do { |
| 1045 | oldValue = jitEntry->u; |
| 1046 | newValue = oldValue; |
| 1047 | newValue.info.instructionSet = set; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 1048 | } while (android_atomic_release_cas( |
| 1049 | oldValue.infoWord, newValue.infoWord, |
| 1050 | &jitEntry->u.infoWord) != 0); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1051 | jitEntry->codeAddress = nPC; |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1052 | newValue.info.profileOffset = profilePrefixSize; |
| 1053 | jitEntry->u = newValue; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * Determine if valid trace-bulding request is active. Return true |
| 1058 | * if we need to abort and switch back to the fast interpreter, false |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1059 | * otherwise. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1060 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1061 | bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState) |
| 1062 | { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1063 | bool switchInterp = false; /* Assume success */ |
| Bill Buzbee | 48f1824 | 2009-06-19 16:02:27 -0700 | [diff] [blame] | 1064 | int i; |
| buzbee | 852aacd | 2010-06-08 16:24:46 -0700 | [diff] [blame] | 1065 | /* |
| 1066 | * A note on trace "hotness" filtering: |
| 1067 | * |
| 1068 | * Our first level trigger is intentionally loose - we need it to |
| 1069 | * fire easily not just to identify potential traces to compile, but |
| 1070 | * also to allow re-entry into the code cache. |
| 1071 | * |
| 1072 | * The 2nd level filter (done here) exists to be selective about |
| 1073 | * what we actually compile. It works by requiring the same |
| 1074 | * trace head "key" (defined as filterKey below) to appear twice in |
| 1075 | * a relatively short period of time. The difficulty is defining the |
| 1076 | * shape of the filterKey. Unfortunately, there is no "one size fits |
| 1077 | * all" approach. |
| 1078 | * |
| 1079 | * For spiky execution profiles dominated by a smallish |
| 1080 | * number of very hot loops, we would want the second-level filter |
| 1081 | * to be very selective. A good selective filter is requiring an |
| 1082 | * exact match of the Dalvik PC. In other words, defining filterKey as: |
| 1083 | * intptr_t filterKey = (intptr_t)interpState->pc |
| 1084 | * |
| 1085 | * However, for flat execution profiles we do best when aggressively |
| 1086 | * translating. A heuristically decent proxy for this is to use |
| 1087 | * the value of the method pointer containing the trace as the filterKey. |
| 1088 | * Intuitively, this is saying that once any trace in a method appears hot, |
| 1089 | * immediately translate any other trace from that same method that |
| 1090 | * survives the first-level filter. Here, filterKey would be defined as: |
| 1091 | * intptr_t filterKey = (intptr_t)interpState->method |
| 1092 | * |
| 1093 | * The problem is that we can't easily detect whether we're dealing |
| 1094 | * with a spiky or flat profile. If we go with the "pc" match approach, |
| 1095 | * flat profiles perform poorly. If we go with the loose "method" match, |
| 1096 | * we end up generating a lot of useless translations. Probably the |
| 1097 | * best approach in the future will be to retain profile information |
| 1098 | * across runs of each application in order to determine it's profile, |
| 1099 | * and then choose once we have enough history. |
| 1100 | * |
| 1101 | * However, for now we've decided to chose a compromise filter scheme that |
| 1102 | * includes elements of both. The high order bits of the filter key |
| 1103 | * are drawn from the enclosing method, and are combined with a slice |
| 1104 | * of the low-order bits of the Dalvik pc of the trace head. The |
| 1105 | * looseness of the filter can be adjusted by changing with width of |
| 1106 | * the Dalvik pc slice (JIT_TRACE_THRESH_FILTER_PC_BITS). The wider |
| 1107 | * the slice, the tighter the filter. |
| 1108 | * |
| 1109 | * Note: the fixed shifts in the function below reflect assumed word |
| 1110 | * alignment for method pointers, and half-word alignment of the Dalvik pc. |
| 1111 | * for method pointers and half-word alignment for dalvik pc. |
| 1112 | */ |
| buzbee | c35294d | 2010-06-09 14:22:50 -0700 | [diff] [blame] | 1113 | u4 methodKey = (u4)interpState->method << |
| 1114 | (JIT_TRACE_THRESH_FILTER_PC_BITS - 2); |
| 1115 | u4 pcKey = ((u4)interpState->pc >> 1) & |
| 1116 | ((1 << JIT_TRACE_THRESH_FILTER_PC_BITS) - 1); |
| 1117 | intptr_t filterKey = (intptr_t)(methodKey | pcKey); |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1118 | bool debugOrProfile = dvmDebuggerOrProfilerActive(); |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1119 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1120 | /* Check if the JIT request can be handled now */ |
| 1121 | if (gDvmJit.pJitEntryTable != NULL && debugOrProfile == false) { |
| 1122 | /* Bypass the filter for hot trace requests or during stress mode */ |
| 1123 | if (interpState->jitState == kJitTSelectRequest && |
| 1124 | gDvmJit.threshold > 6) { |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1125 | /* Two-level filtering scheme */ |
| 1126 | for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) { |
| 1127 | if (filterKey == interpState->threshFilter[i]) { |
| buzbee | 852aacd | 2010-06-08 16:24:46 -0700 | [diff] [blame] | 1128 | interpState->threshFilter[i] = 0; // Reset filter entry |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1129 | break; |
| 1130 | } |
| Bill Buzbee | 48f1824 | 2009-06-19 16:02:27 -0700 | [diff] [blame] | 1131 | } |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1132 | if (i == JIT_TRACE_THRESH_FILTER_SIZE) { |
| 1133 | /* |
| 1134 | * Use random replacement policy - otherwise we could miss a |
| 1135 | * large loop that contains more traces than the size of our |
| 1136 | * filter array. |
| 1137 | */ |
| 1138 | i = rand() % JIT_TRACE_THRESH_FILTER_SIZE; |
| 1139 | interpState->threshFilter[i] = filterKey; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1140 | interpState->jitState = kJitDone; |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1141 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1142 | } |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1143 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1144 | /* If the compiler is backlogged, cancel any JIT actions */ |
| 1145 | if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) { |
| 1146 | interpState->jitState = kJitDone; |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1147 | } |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1148 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1149 | /* |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1150 | * Check for additional reasons that might force the trace select |
| 1151 | * request to be dropped |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1152 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1153 | if (interpState->jitState == kJitTSelectRequest || |
| 1154 | interpState->jitState == kJitTSelectRequestHot) { |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1155 | JitEntry *slot = lookupAndAdd(interpState->pc, false); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1156 | if (slot == NULL) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1157 | /* |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1158 | * Table is full. This should have been |
| 1159 | * detected by the compiler thread and the table |
| 1160 | * resized before we run into it here. Assume bad things |
| 1161 | * are afoot and disable profiling. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1162 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1163 | interpState->jitState = kJitDone; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1164 | LOGD("JIT: JitTable full, disabling profiling"); |
| 1165 | dvmJitStopTranslationRequests(); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1166 | } else if (slot->u.info.traceConstruction) { |
| 1167 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 1168 | * Trace request already in progress, but most likely it |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1169 | * aborted without cleaning up. Assume the worst and |
| 1170 | * mark trace head as untranslatable. If we're wrong, |
| 1171 | * the compiler thread will correct the entry when the |
| 1172 | * translation is completed. The downside here is that |
| 1173 | * some existing translation may chain to the interpret-only |
| 1174 | * template instead of the real translation during this |
| 1175 | * window. Performance, but not correctness, issue. |
| 1176 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1177 | interpState->jitState = kJitDone; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1178 | resetTracehead(interpState, slot); |
| 1179 | } else if (slot->codeAddress) { |
| 1180 | /* Nothing to do here - just return */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1181 | interpState->jitState = kJitDone; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1182 | } else { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1183 | /* |
| 1184 | * Mark request. Note, we are not guaranteed exclusivity |
| 1185 | * here. A window exists for another thread to be |
| 1186 | * attempting to build this same trace. Rather than |
| 1187 | * bear the cost of locking, we'll just allow that to |
| 1188 | * happen. The compiler thread, if it chooses, can |
| 1189 | * discard redundant requests. |
| 1190 | */ |
| 1191 | setTraceConstruction(slot, true); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1192 | } |
| 1193 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1194 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1195 | switch (interpState->jitState) { |
| 1196 | case kJitTSelectRequest: |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1197 | case kJitTSelectRequestHot: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1198 | interpState->jitState = kJitTSelect; |
| 1199 | interpState->currTraceHead = interpState->pc; |
| 1200 | interpState->currTraceRun = 0; |
| 1201 | interpState->totalTraceLen = 0; |
| 1202 | interpState->currRunHead = interpState->pc; |
| 1203 | interpState->currRunLen = 0; |
| 1204 | interpState->trace[0].frag.startOffset = |
| 1205 | interpState->pc - interpState->method->insns; |
| 1206 | interpState->trace[0].frag.numInsts = 0; |
| 1207 | interpState->trace[0].frag.runEnd = false; |
| 1208 | interpState->trace[0].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1209 | interpState->trace[0].frag.isCode = true; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1210 | interpState->lastPC = 0; |
| 1211 | break; |
| 1212 | /* |
| 1213 | * For JIT's perspective there is no need to stay in the debug |
| 1214 | * interpreter unless debugger/profiler is attached. |
| 1215 | */ |
| 1216 | case kJitDone: |
| 1217 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1218 | break; |
| 1219 | default: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1220 | LOGE("Unexpected JIT state: %d entry point: %d", |
| 1221 | interpState->jitState, interpState->entryPoint); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1222 | dvmAbort(); |
| 1223 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1224 | } else { |
| 1225 | /* |
| 1226 | * Cannot build trace this time - ready to leave the dbg interpreter |
| 1227 | */ |
| 1228 | interpState->jitState = kJitDone; |
| 1229 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1230 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * Final check to see if we can really switch the interpreter. Make sure |
| 1234 | * the jitState is kJitDone when switchInterp is set to true. |
| 1235 | */ |
| 1236 | assert(switchInterp == false || interpState->jitState == kJitDone); |
| Ben Cheng | 1a7b9d7 | 2010-09-20 22:20:31 -0700 | [diff] [blame] | 1237 | return switchInterp && !debugOrProfile && |
| 1238 | !dvmJitStayInPortableInterpreter(); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * Resizes the JitTable. Must be a power of 2, and returns true on failure. |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1243 | * Stops all threads, and thus is a heavyweight operation. May only be called |
| 1244 | * by the compiler thread. |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1245 | */ |
| 1246 | bool dvmJitResizeJitTable( unsigned int size ) |
| 1247 | { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1248 | JitEntry *pNewTable; |
| 1249 | JitEntry *pOldTable; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1250 | JitEntry tempEntry; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1251 | u4 newMask; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1252 | unsigned int oldSize; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1253 | unsigned int i; |
| 1254 | |
| Ben Cheng | 3f02aa4 | 2009-08-14 13:52:09 -0700 | [diff] [blame] | 1255 | assert(gDvmJit.pJitEntryTable != NULL); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1256 | assert(size && !(size & (size - 1))); /* Is power of 2? */ |
| 1257 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1258 | LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1259 | |
| 1260 | newMask = size - 1; |
| 1261 | |
| 1262 | if (size <= gDvmJit.jitTableSize) { |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1266 | /* Make sure requested size is compatible with chain field width */ |
| 1267 | tempEntry.u.info.chain = size; |
| 1268 | if (tempEntry.u.info.chain != size) { |
| 1269 | LOGD("Jit: JitTable request of %d too big", size); |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1273 | pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable)); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1274 | if (pNewTable == NULL) { |
| 1275 | return true; |
| 1276 | } |
| 1277 | for (i=0; i< size; i++) { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1278 | pNewTable[i].u.info.chain = size; /* Initialize chain termination */ |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* Stop all other interpreting/jit'ng threads */ |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 1282 | dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1283 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1284 | pOldTable = gDvmJit.pJitEntryTable; |
| 1285 | oldSize = gDvmJit.jitTableSize; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1286 | |
| 1287 | dvmLockMutex(&gDvmJit.tableLock); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1288 | gDvmJit.pJitEntryTable = pNewTable; |
| 1289 | gDvmJit.jitTableSize = size; |
| 1290 | gDvmJit.jitTableMask = size - 1; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1291 | gDvmJit.jitTableEntriesUsed = 0; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1292 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1293 | for (i=0; i < oldSize; i++) { |
| 1294 | if (pOldTable[i].dPC) { |
| 1295 | JitEntry *p; |
| 1296 | u2 chain; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1297 | p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/ ); |
| 1298 | p->codeAddress = pOldTable[i].codeAddress; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1299 | /* We need to preserve the new chain field, but copy the rest */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1300 | chain = p->u.info.chain; |
| 1301 | p->u = pOldTable[i].u; |
| 1302 | p->u.info.chain = chain; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1305 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1306 | dvmUnlockMutex(&gDvmJit.tableLock); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1307 | |
| 1308 | free(pOldTable); |
| 1309 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1310 | /* Restart the world */ |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 1311 | dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1312 | |
| 1313 | return false; |
| 1314 | } |
| 1315 | |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1316 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 1317 | * Reset the JitTable to the initial clean state. |
| 1318 | */ |
| 1319 | void dvmJitResetTable(void) |
| 1320 | { |
| 1321 | JitEntry *jitEntry = gDvmJit.pJitEntryTable; |
| 1322 | unsigned int size = gDvmJit.jitTableSize; |
| 1323 | unsigned int i; |
| 1324 | |
| 1325 | dvmLockMutex(&gDvmJit.tableLock); |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1326 | |
| 1327 | /* Note: If need to preserve any existing counts. Do so here. */ |
| 1328 | for (i=0; i < JIT_PROF_BLOCK_BUCKETS; i++) { |
| 1329 | if (gDvmJit.pJitTraceProfCounters->buckets[i]) |
| 1330 | memset((void *) gDvmJit.pJitTraceProfCounters->buckets[i], |
| 1331 | 0, sizeof(JitTraceCounter_t) * JIT_PROF_BLOCK_ENTRIES); |
| 1332 | } |
| 1333 | gDvmJit.pJitTraceProfCounters->next = 0; |
| 1334 | |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 1335 | memset((void *) jitEntry, 0, sizeof(JitEntry) * size); |
| 1336 | for (i=0; i< size; i++) { |
| 1337 | jitEntry[i].u.info.chain = size; /* Initialize chain termination */ |
| 1338 | } |
| 1339 | gDvmJit.jitTableEntriesUsed = 0; |
| 1340 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1344 | * Return the address of the next trace profile counter. This address |
| 1345 | * will be embedded in the generated code for the trace, and thus cannot |
| 1346 | * change while the trace exists. |
| 1347 | */ |
| 1348 | JitTraceCounter_t *dvmJitNextTraceCounter() |
| 1349 | { |
| 1350 | int idx = gDvmJit.pJitTraceProfCounters->next / JIT_PROF_BLOCK_ENTRIES; |
| 1351 | int elem = gDvmJit.pJitTraceProfCounters->next % JIT_PROF_BLOCK_ENTRIES; |
| 1352 | JitTraceCounter_t *res; |
| 1353 | /* Lazily allocate blocks of counters */ |
| 1354 | if (!gDvmJit.pJitTraceProfCounters->buckets[idx]) { |
| 1355 | JitTraceCounter_t *p = |
| 1356 | (JitTraceCounter_t*) calloc(JIT_PROF_BLOCK_ENTRIES, sizeof(*p)); |
| 1357 | if (!p) { |
| 1358 | LOGE("Failed to allocate block of trace profile counters"); |
| 1359 | dvmAbort(); |
| 1360 | } |
| 1361 | gDvmJit.pJitTraceProfCounters->buckets[idx] = p; |
| 1362 | } |
| 1363 | res = &gDvmJit.pJitTraceProfCounters->buckets[idx][elem]; |
| 1364 | gDvmJit.pJitTraceProfCounters->next++; |
| 1365 | return res; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1369 | * Float/double conversion requires clamping to min and max of integer form. If |
| 1370 | * target doesn't support this normally, use these. |
| 1371 | */ |
| 1372 | s8 dvmJitd2l(double d) |
| 1373 | { |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1374 | static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL; |
| 1375 | static const double kMinLong = (double)(s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1376 | if (d >= kMaxLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1377 | return (s8)0x7fffffffffffffffULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1378 | else if (d <= kMinLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1379 | return (s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1380 | else if (d != d) // NaN case |
| 1381 | return 0; |
| 1382 | else |
| 1383 | return (s8)d; |
| 1384 | } |
| 1385 | |
| 1386 | s8 dvmJitf2l(float f) |
| 1387 | { |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1388 | static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL; |
| 1389 | static const float kMinLong = (float)(s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1390 | if (f >= kMaxLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1391 | return (s8)0x7fffffffffffffffULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1392 | else if (f <= kMinLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1393 | return (s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1394 | else if (f != f) // NaN case |
| 1395 | return 0; |
| 1396 | else |
| 1397 | return (s8)f; |
| 1398 | } |
| 1399 | |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 1400 | /* Should only be called by the compiler thread */ |
| 1401 | void dvmJitChangeProfileMode(TraceProfilingModes newState) |
| 1402 | { |
| 1403 | if (gDvmJit.profileMode != newState) { |
| 1404 | gDvmJit.profileMode = newState; |
| 1405 | dvmJitUnchainAll(); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | void dvmJitTraceProfilingOn() |
| 1410 | { |
| 1411 | if (gDvmJit.profileMode == kTraceProfilingPeriodicOff) |
| 1412 | dvmCompilerWorkEnqueue(NULL, kWorkOrderProfileMode, |
| 1413 | (void*) kTraceProfilingPeriodicOn); |
| 1414 | else if (gDvmJit.profileMode == kTraceProfilingDisabled) |
| 1415 | dvmCompilerWorkEnqueue(NULL, kWorkOrderProfileMode, |
| 1416 | (void*) kTraceProfilingContinuous); |
| 1417 | } |
| 1418 | |
| 1419 | void dvmJitTraceProfilingOff() |
| 1420 | { |
| 1421 | if (gDvmJit.profileMode == kTraceProfilingPeriodicOn) |
| 1422 | dvmCompilerWorkEnqueue(NULL, kWorkOrderProfileMode, |
| 1423 | (void*) kTraceProfilingPeriodicOff); |
| 1424 | else if (gDvmJit.profileMode == kTraceProfilingContinuous) |
| 1425 | dvmCompilerWorkEnqueue(NULL, kWorkOrderProfileMode, |
| 1426 | (void*) kTraceProfilingDisabled); |
| 1427 | } |
| 1428 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1429 | #endif /* WITH_JIT */ |