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