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