msach@94: /* msach@94: * Copyright 2010 OpenSourceStewardshipFoundation msach@94: * msach@94: * Licensed under BSD msach@94: */ msach@94: msach@94: msach@94: msach@94: #include msach@94: #include msach@94: msach@94: #include "VMS.h" msach@94: #include "ProcrContext.h" msach@94: msach@94: msach@94: //=========================================================================== msach@94: void inline msach@94: stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ, msach@94: VirtProcr *masterPr ); msach@94: msach@94: //=========================================================================== msach@94: msach@94: msach@94: msach@94: /*This code is animated by the virtual Master processor. msach@94: * msach@94: *Polls each sched slot exactly once, hands any requests made by a newly msach@94: * done slave to the "request handler" plug-in function msach@94: * msach@94: *Any slots that need a virt procr assigned are given to the "schedule" msach@94: * plug-in function, which tries to assign a virt procr (slave) to it. msach@94: * msach@94: *When all slots needing a processor have been given to the schedule plug-in, msach@94: * a fraction of the procrs successfully scheduled are put into the msach@94: * work queue, then a continuation of this function is put in, then the rest msach@94: * of the virt procrs that were successfully scheduled. msach@94: * msach@94: *The first thing the continuation does is busy-wait until the previous msach@94: * animation completes. This is because an (unlikely) continuation may msach@94: * sneak through queue before previous continuation is done putting second msach@94: * part of scheduled slaves in, which is the only race condition. msach@94: * msach@94: */ msach@94: msach@94: /*May 29, 2010 -- birth a Master during init so that first core loop to msach@94: * start running gets it and does all the stuff for a newly born -- msach@94: * from then on, will be doing continuation, but do suspension self msach@94: * directly at end of master loop msach@94: *So VMS__init just births the master virtual processor same way it births msach@94: * all the others -- then does any extra setup needed and puts it into the msach@94: * work queue. msach@94: *However means have to make masterEnv a global static volatile the same way msach@94: * did with readyToAnimateQ in core loop. -- for performance, put the msach@94: * jump to the core loop directly in here, and have it directly jump back. msach@94: * msach@94: * msach@94: *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this msach@94: * avoids the suspected bug in the system stack that causes bizarre faults msach@94: * at random places in the system code. msach@94: * msach@94: *So, this function is coupled to each of the MasterVPs, -- meaning this msach@94: * function can't rely on a particular stack and frame -- each MasterVP that msach@94: * animates this function has a different one. msach@94: * msach@94: *At this point, the masterLoop does not write itself into the queue anymore, msach@94: * instead, the coreLoop acquires the masterLock when it has nothing to msach@94: * animate, and then animates its own masterLoop. However, still try to put msach@94: * several AppVPs into the queue to amortize the startup cost of switching msach@94: * to the MasterVP. Note, don't have to worry about latency of requests much msach@94: * because most requests generate work for same core -- only latency issue msach@94: * is case when other cores starved and one core's requests generate work msach@94: * for them -- so keep max in queue to 3 or 4.. msach@94: */ msach@94: void masterLoop( void *initData, VirtProcr *animatingPr ) msach@94: { msach@94: int32 slotIdx, numSlotsFilled; msach@94: VirtProcr *schedVirtPr; msach@94: SchedSlot *currSlot, **schedSlots; msach@94: MasterEnv *masterEnv; msach@94: VMSQueueStruc *readyToAnimateQ; msach@94: msach@94: SlaveScheduler slaveScheduler; msach@94: RequestHandler requestHandler; msach@94: void *semanticEnv; msach@94: msach@94: int32 thisCoresIdx; msach@94: VirtProcr *masterPr; msach@94: volatile VirtProcr *volatileMasterPr; msach@94: msach@94: volatileMasterPr = animatingPr; msach@94: masterPr = (VirtProcr*)volatileMasterPr; //used to force re-define after jmp msach@94: msach@94: //First animation of each MasterVP will in turn animate this part msach@94: // of setup code.. (VP creator sets up the stack as if this function msach@94: // was called normally, but actually get here by jmp) msach@94: //So, setup values about stack ptr, jmp pt and all that msach@94: //masterPr->nextInstrPt = &&masterLoopStartPt; msach@94: msach@94: msach@94: //Note, got rid of writing the stack and frame ptr up here, because msach@94: // only one msach@94: // core can ever animate a given MasterVP, so don't need to communicate msach@94: // new frame and stack ptr to the MasterVP storage before a second msach@94: // version of that MasterVP can get animated on a different core. msach@94: //Also got rid of the busy-wait. msach@94: msach@94: msach@94: //masterLoopStartPt: msach@94: while(1){ msach@94: msach@94: //============================= MEASUREMENT STUFF ======================== msach@94: #ifdef MEAS__TIME_MASTER msach@94: //Total Master time includes one coreloop time -- just assume the core msach@94: // loop time is same for Master as for AppVPs, even though it may be msach@94: // smaller due to higher predictability of the fixed jmp. msach@94: saveLowTimeStampCountInto( masterPr->startMasterTSCLow ); msach@94: #endif msach@94: //======================================================================== msach@94: msach@94: masterEnv = (MasterEnv*)_VMSMasterEnv; msach@94: msach@94: //GCC may optimize so doesn't always re-define from frame-storage msach@94: masterPr = (VirtProcr*)volatileMasterPr; //just to make sure after jmp msach@94: thisCoresIdx = masterPr->coreAnimatedBy; msach@94: readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx]; msach@94: schedSlots = masterEnv->allSchedSlots[thisCoresIdx]; msach@94: msach@94: requestHandler = masterEnv->requestHandler; msach@94: slaveScheduler = masterEnv->slaveScheduler; msach@94: semanticEnv = masterEnv->semanticEnv; nengel@184: nengel@184: #ifdef MEAS__PERF_COUNTERS nengel@185: CounterHandler counterHandler = masterEnv->counterHandler; nengel@184: #endif msach@94: msach@94: //Poll each slot's Done flag msach@94: numSlotsFilled = 0; msach@94: for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++) msach@94: { msach@94: currSlot = schedSlots[ slotIdx ]; msach@94: msach@94: if( currSlot->workIsDone ) msach@94: { msach@94: currSlot->workIsDone = FALSE; msach@94: currSlot->needsProcrAssigned = TRUE; msach@94: msach@94: //process requests from slave to master msach@94: //====================== MEASUREMENT STUFF =================== msach@94: #ifdef MEAS__TIME_PLUGIN msach@94: int32 startStamp1, endStamp1; msach@94: saveLowTimeStampCountInto( startStamp1 ); msach@94: #endif Nina@109: #ifdef MEAS__PERF_COUNTERS nengel@185: uint64 cycles, instrs; nengel@184: saveCyclesAndInstrs(thisCoresIdx,cycles, instrs); nengel@185: (*counterHandler)(MasterLoop_beforeReqHdlr,currSlot->procrAssignedToSlot,cycles,instrs); engelhardt@108: #endif msach@94: //============================================================ msach@94: (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv ); msach@94: //====================== MEASUREMENT STUFF =================== msach@94: #ifdef MEAS__TIME_PLUGIN msach@94: saveLowTimeStampCountInto( endStamp1 ); msach@94: addIntervalToHist( startStamp1, endStamp1, msach@94: _VMSMasterEnv->reqHdlrLowTimeHist ); msach@94: addIntervalToHist( startStamp1, endStamp1, msach@94: _VMSMasterEnv->reqHdlrHighTimeHist ); msach@94: #endif Nina@109: #ifdef MEAS__PERF_COUNTERS Nina@109: //done with constraints check nengel@185: uint64 cycles2,instrs2; nengel@185: saveCyclesAndInstrs(thisCoresIdx,cycles2, instrs2); nengel@185: (*counterHandler)(MasterLoop_afterReqHdlr,currSlot->procrAssignedToSlot,cycles2,instrs2); engelhardt@108: #endif msach@94: //============================================================ msach@94: } msach@94: if( currSlot->needsProcrAssigned ) msach@94: { //give slot a new virt procr Nina@109: #ifdef MEAS__PERF_COUNTERS engelhardt@108: //start assigner engelhardt@108: uint64 tmp_cycles; engelhardt@108: uint64 tmp_instrs; Nina@109: saveCyclesAndInstrs(thisCoresIdx,tmp_cycles,tmp_instrs); engelhardt@108: #endif msach@94: schedVirtPr = nengel@177: (*slaveScheduler)( semanticEnv, thisCoresIdx, slotIdx ); Nina@109: msach@94: if( schedVirtPr != NULL ) msach@94: { currSlot->procrAssignedToSlot = schedVirtPr; msach@94: schedVirtPr->schedSlot = currSlot; msach@94: currSlot->needsProcrAssigned = FALSE; msach@94: numSlotsFilled += 1; nengel@177: nengel@185: writeVMSQ( schedVirtPr, readyToAnimateQ ); nengel@185: Nina@109: #ifdef MEAS__PERF_COUNTERS nengel@184: uint64 cycles; nengel@184: uint64 instrs; nengel@184: saveCyclesAndInstrs(thisCoresIdx,cycles,instrs); nengel@185: (*counterHandler)(MasterLoop_beforeAssign,schedVirtPr,tmp_cycles,tmp_instrs); nengel@185: (*counterHandler)(MasterLoop_afterAssign,schedVirtPr,cycles,instrs); engelhardt@108: #endif msach@94: } msach@94: } nengel@177: msach@94: } msach@94: msach@94: msach@94: #ifdef USE_WORK_STEALING msach@94: //If no slots filled, means no more work, look for work to steal. msach@94: if( numSlotsFilled == 0 ) msach@94: { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr ); msach@94: } msach@94: #endif msach@94: msach@94: msach@94: #ifdef MEAS__TIME_MASTER msach@94: saveLowTimeStampCountInto( masterPr->endMasterTSCLow ); msach@94: #endif msach@94: msach@94: masterSwitchToCoreLoop(animatingPr); msach@94: flushRegisters(); msach@94: }//MasterLoop msach@94: msach@94: msach@94: } msach@94: msach@94: msach@94: msach@94: /*This has a race condition -- the coreloops are accessing their own queues msach@94: * at the same time that this work-stealer on a different core is trying to msach@94: */ msach@94: void inline msach@94: stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ, msach@94: VirtProcr *masterPr ) msach@94: { msach@94: VirtProcr *stolenPr; msach@94: int32 coreIdx, i; msach@94: VMSQueueStruc *currQ; msach@94: msach@94: stolenPr = NULL; msach@94: coreIdx = masterPr->coreAnimatedBy; msach@94: for( i = 0; i < NUM_CORES -1; i++ ) msach@94: { msach@94: if( coreIdx >= NUM_CORES -1 ) msach@94: { coreIdx = 0; msach@94: } msach@94: else msach@94: { coreIdx++; msach@94: } msach@94: currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx]; msach@94: if( numInVMSQ( currQ ) > 0 ) msach@94: { stolenPr = readVMSQ (currQ ); msach@94: break; msach@94: } msach@94: } msach@94: msach@94: if( stolenPr != NULL ) msach@94: { currSlot->procrAssignedToSlot = stolenPr; msach@94: stolenPr->schedSlot = currSlot; msach@94: currSlot->needsProcrAssigned = FALSE; msach@94: msach@94: writeVMSQ( stolenPr, readyToAnimateQ ); msach@94: } msach@94: } msach@94: msach@94: /*This algorithm makes the common case fast. Make the coreloop passive, msach@94: * and show its progress. Make the stealer control a gate that coreloop msach@94: * has to pass. msach@94: *To avoid interference, only one stealer at a time. Use a global msach@94: * stealer-lock. msach@94: * msach@94: *The pattern is based on a gate -- stealer shuts the gate, then monitors msach@94: * to be sure any already past make it all the way out, before starting. msach@94: *So, have a "progress" measure just before the gate, then have two after it, msach@94: * one is in a "waiting room" outside the gate, the other is at the exit. msach@94: *Then, the stealer first shuts the gate, then checks the progress measure msach@94: * outside it, then looks to see if the progress measure at the exit is the msach@94: * same. If yes, it knows the protected area is empty 'cause no other way msach@94: * to get in and the last to get in also exited. msach@94: *If the progress measure at the exit is not the same, then the stealer goes msach@94: * into a loop checking both the waiting-area and the exit progress-measures msach@94: * until one of them shows the same as the measure outside the gate. Might msach@94: * as well re-read the measure outside the gate each go around, just to be msach@94: * sure. It is guaranteed that one of the two will eventually match the one msach@94: * outside the gate. msach@94: * msach@94: *Here's an informal proof of correctness: msach@94: *The gate can be closed at any point, and have only four cases: msach@94: * 1) coreloop made it past the gate-closing but not yet past the exit msach@94: * 2) coreloop made it past the pre-gate progress update but not yet past msach@94: * the gate, msach@94: * 3) coreloop is right before the pre-gate update msach@94: * 4) coreloop is past the exit and far from the pre-gate update. msach@94: * msach@94: * Covering the cases in reverse order, msach@94: * 4) is not a problem -- stealer will read pre-gate progress, see that it msach@94: * matches exit progress, and the gate is closed, so stealer can proceed. msach@94: * 3) stealer will read pre-gate progress just after coreloop updates it.. msach@94: * so stealer goes into a loop until the coreloop causes wait-progress msach@94: * to match pre-gate progress, so then stealer can proceed msach@94: * 2) same as 3.. msach@94: * 1) stealer reads pre-gate progress, sees that it's different than exit, msach@94: * so goes into loop until exit matches pre-gate, now it knows coreloop msach@94: * is not in protected and cannot get back in, so can proceed. msach@94: * msach@94: *Implementation for the stealer: msach@94: * msach@94: *First, acquire the stealer lock -- only cores with no work to do will msach@94: * compete to steal, so not a big performance penalty having only one -- msach@94: * will rarely have multiple stealers in a system with plenty of work -- and msach@94: * in a system with little work, it doesn't matter. msach@94: * msach@94: *Note, have single-reader, single-writer pattern for all variables used to msach@94: * communicate between stealer and victims msach@94: * msach@94: *So, scan the queues of the core loops, until find non-empty. Each core msach@94: * has its own list that it scans. The list goes in order from closest to msach@94: * furthest core, so it steals first from close cores. Later can add msach@94: * taking info from the app about overlapping footprints, and scan all the msach@94: * others then choose work with the most footprint overlap with the contents msach@94: * of this core's cache. msach@94: * msach@94: *Now, have a victim want to take work from. So, shut the gate in that msach@94: * coreloop, by setting the "gate closed" var on its stack to TRUE. msach@94: *Then, read the core's pre-gate progress and compare to the core's exit msach@94: * progress. msach@94: *If same, can proceed to take work from the coreloop's queue. When done, msach@94: * write FALSE to gate closed var. msach@94: *If different, then enter a loop that reads the pre-gate progress, then msach@94: * compares to exit progress then to wait progress. When one of two msach@94: * matches, proceed. Take work from the coreloop's queue. When done, msach@94: * write FALSE to the gate closed var. msach@94: * msach@94: */ msach@94: void inline msach@94: gateProtected_stealWorkInto( SchedSlot *currSlot, msach@94: VMSQueueStruc *myReadyToAnimateQ, msach@94: VirtProcr *masterPr ) msach@94: { msach@94: VirtProcr *stolenPr; msach@94: int32 coreIdx, i, haveAVictim, gotLock; msach@94: VMSQueueStruc *victimsQ; msach@94: msach@94: volatile GateStruc *vicGate; msach@94: int32 coreMightBeInProtected; msach@94: msach@94: msach@94: msach@94: //see if any other cores have work available to steal msach@94: haveAVictim = FALSE; msach@94: coreIdx = masterPr->coreAnimatedBy; msach@94: for( i = 0; i < NUM_CORES -1; i++ ) msach@94: { msach@94: if( coreIdx >= NUM_CORES -1 ) msach@94: { coreIdx = 0; msach@94: } msach@94: else msach@94: { coreIdx++; msach@94: } msach@94: victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx]; msach@94: if( numInVMSQ( victimsQ ) > 0 ) msach@94: { haveAVictim = TRUE; msach@94: vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ]; msach@94: break; msach@94: } msach@94: } msach@94: if( !haveAVictim ) return; //no work to steal, exit msach@94: msach@94: //have a victim core, now get the stealer-lock msach@94: gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock), msach@94: UNLOCKED, LOCKED ); msach@94: if( !gotLock ) return; //go back to core loop, which will re-start master msach@94: msach@94: msach@94: //====== Start Gate-protection ======= msach@94: vicGate->gateClosed = TRUE; msach@94: coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress; msach@94: while( coreMightBeInProtected ) msach@94: { //wait until sure msach@94: if( vicGate->preGateProgress == vicGate->waitProgress ) msach@94: coreMightBeInProtected = FALSE; msach@94: if( vicGate->preGateProgress == vicGate->exitProgress ) msach@94: coreMightBeInProtected = FALSE; msach@94: } msach@94: msach@94: stolenPr = readVMSQ ( victimsQ ); msach@94: msach@94: vicGate->gateClosed = FALSE; msach@94: //======= End Gate-protection ======= msach@94: msach@94: msach@94: if( stolenPr != NULL ) //victim could have been in protected and taken msach@94: { currSlot->procrAssignedToSlot = stolenPr; msach@94: stolenPr->schedSlot = currSlot; msach@94: currSlot->needsProcrAssigned = FALSE; msach@94: msach@94: writeVMSQ( stolenPr, myReadyToAnimateQ ); msach@94: } msach@94: msach@94: //unlock the work stealing lock msach@94: _VMSMasterEnv->workStealingLock = UNLOCKED; msach@94: }