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