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