annotate MasterLoop.c @ 196:7cff4e13d5c4

remove old second default head
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 10 Feb 2012 12:05:17 +0100
parents c1784868dcea ac11b50220bd
children 6db9e4898978
rev   line source
Me@178 1 /*
Me@178 2 * Copyright 2010 OpenSourceStewardshipFoundation
Me@178 3 *
Me@178 4 * Licensed under BSD
Me@178 5 */
Me@178 6
Me@178 7
Me@178 8
Me@178 9 #include <stdio.h>
Me@178 10 #include <stddef.h>
Me@178 11
Me@178 12 #include "VMS.h"
Me@178 13 #include "ProcrContext.h"
Me@178 14
Me@178 15
Me@178 16 //===========================================================================
Me@178 17 void inline
Me@178 18 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
Me@178 19 VirtProcr *masterPr );
Me@178 20
Me@178 21 //===========================================================================
Me@178 22
Me@178 23
Me@178 24
Me@178 25 /*This code is animated by the virtual Master processor.
Me@178 26 *
Me@178 27 *Polls each sched slot exactly once, hands any requests made by a newly
Me@178 28 * done slave to the "request handler" plug-in function
Me@178 29 *
Me@178 30 *Any slots that need a virt procr assigned are given to the "schedule"
Me@178 31 * plug-in function, which tries to assign a virt procr (slave) to it.
Me@178 32 *
Me@178 33 *When all slots needing a processor have been given to the schedule plug-in,
Me@178 34 * a fraction of the procrs successfully scheduled are put into the
Me@178 35 * work queue, then a continuation of this function is put in, then the rest
Me@178 36 * of the virt procrs that were successfully scheduled.
Me@178 37 *
Me@178 38 *The first thing the continuation does is busy-wait until the previous
Me@178 39 * animation completes. This is because an (unlikely) continuation may
Me@178 40 * sneak through queue before previous continuation is done putting second
Me@178 41 * part of scheduled slaves in, which is the only race condition.
Me@178 42 *
Me@178 43 */
Me@178 44
Me@178 45 /*May 29, 2010 -- birth a Master during init so that first core loop to
Me@178 46 * start running gets it and does all the stuff for a newly born --
Me@178 47 * from then on, will be doing continuation, but do suspension self
Me@178 48 * directly at end of master loop
Me@178 49 *So VMS__init just births the master virtual processor same way it births
Me@178 50 * all the others -- then does any extra setup needed and puts it into the
Me@178 51 * work queue.
Me@178 52 *However means have to make masterEnv a global static volatile the same way
Me@178 53 * did with readyToAnimateQ in core loop. -- for performance, put the
Me@178 54 * jump to the core loop directly in here, and have it directly jump back.
Me@178 55 *
Me@178 56 *
Me@178 57 *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this
Me@178 58 * avoids the suspected bug in the system stack that causes bizarre faults
Me@178 59 * at random places in the system code.
Me@178 60 *
Me@178 61 *So, this function is coupled to each of the MasterVPs, -- meaning this
Me@178 62 * function can't rely on a particular stack and frame -- each MasterVP that
Me@178 63 * animates this function has a different one.
Me@178 64 *
Me@178 65 *At this point, the masterLoop does not write itself into the queue anymore,
Me@178 66 * instead, the coreLoop acquires the masterLock when it has nothing to
Me@178 67 * animate, and then animates its own masterLoop. However, still try to put
Me@178 68 * several AppVPs into the queue to amortize the startup cost of switching
Me@178 69 * to the MasterVP. Note, don't have to worry about latency of requests much
Me@178 70 * because most requests generate work for same core -- only latency issue
Me@178 71 * is case when other cores starved and one core's requests generate work
Me@178 72 * for them -- so keep max in queue to 3 or 4..
Me@178 73 */
Me@178 74 void masterLoop( void *initData, VirtProcr *animatingPr )
Me@178 75 {
Me@178 76 int32 slotIdx, numSlotsFilled;
Me@178 77 VirtProcr *schedVirtPr;
Me@178 78 SchedSlot *currSlot, **schedSlots;
Me@178 79 MasterEnv *masterEnv;
Me@178 80 VMSQueueStruc *readyToAnimateQ;
Me@178 81
Me@178 82 SlaveScheduler slaveScheduler;
Me@178 83 RequestHandler requestHandler;
Me@178 84 void *semanticEnv;
Me@178 85
Me@178 86 int32 thisCoresIdx;
Me@178 87 VirtProcr *masterPr;
Me@178 88 volatile VirtProcr *volatileMasterPr;
Me@178 89
Me@178 90 volatileMasterPr = animatingPr;
Me@178 91 masterPr = (VirtProcr*)volatileMasterPr; //used to force re-define after jmp
Me@178 92
Me@178 93 //First animation of each MasterVP will in turn animate this part
Me@178 94 // of setup code.. (VP creator sets up the stack as if this function
Me@178 95 // was called normally, but actually get here by jmp)
Me@178 96 //So, setup values about stack ptr, jmp pt and all that
Me@178 97 //masterPr->nextInstrPt = &&masterLoopStartPt;
Me@178 98
Me@178 99
Me@178 100 //Note, got rid of writing the stack and frame ptr up here, because
Me@178 101 // only one
Me@178 102 // core can ever animate a given MasterVP, so don't need to communicate
Me@178 103 // new frame and stack ptr to the MasterVP storage before a second
Me@178 104 // version of that MasterVP can get animated on a different core.
Me@178 105 //Also got rid of the busy-wait.
Me@178 106
Me@178 107
Me@178 108 //masterLoopStartPt:
Me@178 109 while(1){
Me@178 110
Me@178 111 //============================= MEASUREMENT STUFF ========================
Me@178 112 #ifdef MEAS__TIME_MASTER
Me@178 113 //Total Master time includes one coreloop time -- just assume the core
Me@178 114 // loop time is same for Master as for AppVPs, even though it may be
Me@178 115 // smaller due to higher predictability of the fixed jmp.
Me@178 116 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
Me@178 117 #endif
Me@178 118 //========================================================================
Me@178 119
Me@178 120 masterEnv = (MasterEnv*)_VMSMasterEnv;
Me@178 121
Me@178 122 //GCC may optimize so doesn't always re-define from frame-storage
Me@178 123 masterPr = (VirtProcr*)volatileMasterPr; //just to make sure after jmp
Me@178 124 thisCoresIdx = masterPr->coreAnimatedBy;
Me@178 125 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
Me@178 126 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
Me@178 127
Me@178 128 requestHandler = masterEnv->requestHandler;
Me@178 129 slaveScheduler = masterEnv->slaveScheduler;
Me@178 130 semanticEnv = masterEnv->semanticEnv;
Me@178 131
Me@178 132
Me@178 133 //Poll each slot's Done flag
Me@178 134 numSlotsFilled = 0;
Me@178 135 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
Me@178 136 {
Me@178 137 currSlot = schedSlots[ slotIdx ];
Me@178 138
Me@178 139 if( currSlot->workIsDone )
Me@178 140 {
Me@178 141 currSlot->workIsDone = FALSE;
Me@178 142 currSlot->needsProcrAssigned = TRUE;
Me@178 143
Me@178 144 //process requests from slave to master
Me@178 145 //====================== MEASUREMENT STUFF ===================
Me@178 146 #ifdef MEAS__TIME_PLUGIN
Me@178 147 int32 startStamp1, endStamp1;
Me@178 148 saveLowTimeStampCountInto( startStamp1 );
Me@178 149 #endif
Me@178 150 //============================================================
Me@178 151 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
Me@178 152 //====================== MEASUREMENT STUFF ===================
Me@178 153 #ifdef MEAS__TIME_PLUGIN
Me@178 154 saveLowTimeStampCountInto( endStamp1 );
Me@178 155 addIntervalToHist( startStamp1, endStamp1,
Me@178 156 _VMSMasterEnv->reqHdlrLowTimeHist );
Me@178 157 addIntervalToHist( startStamp1, endStamp1,
Me@178 158 _VMSMasterEnv->reqHdlrHighTimeHist );
Me@178 159 #endif
Me@178 160 //============================================================
Me@178 161 }
Me@178 162 if( currSlot->needsProcrAssigned )
Me@178 163 { //give slot a new virt procr
Me@178 164 schedVirtPr =
Me@178 165 (*slaveScheduler)( semanticEnv, thisCoresIdx );
Me@178 166
Me@178 167 if( schedVirtPr != NULL )
Me@178 168 { currSlot->procrAssignedToSlot = schedVirtPr;
Me@178 169 schedVirtPr->schedSlot = currSlot;
Me@178 170 currSlot->needsProcrAssigned = FALSE;
Me@178 171 numSlotsFilled += 1;
Me@178 172
Me@178 173 writeVMSQ( schedVirtPr, readyToAnimateQ );
Me@178 174 }
Me@178 175 }
Me@178 176 }
Me@178 177
Me@178 178
Me@178 179 #ifdef USE_WORK_STEALING
Me@178 180 //If no slots filled, means no more work, look for work to steal.
Me@178 181 if( numSlotsFilled == 0 )
Me@178 182 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
Me@178 183 }
Me@178 184 #endif
Me@178 185
Me@178 186
Me@178 187 #ifdef MEAS__TIME_MASTER
Me@178 188 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
Me@178 189 #endif
Me@178 190
Me@178 191 masterSwitchToCoreLoop(animatingPr);
Me@178 192 flushRegisters();
Me@178 193 }//MasterLoop
Me@178 194
Me@178 195
Me@178 196 }
Me@178 197
Me@178 198
Me@178 199
Me@178 200 /*This has a race condition -- the coreloops are accessing their own queues
Me@178 201 * at the same time that this work-stealer on a different core is trying to
Me@178 202 */
Me@178 203 void inline
Me@178 204 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
Me@178 205 VirtProcr *masterPr )
Me@178 206 {
Me@178 207 VirtProcr *stolenPr;
Me@178 208 int32 coreIdx, i;
Me@178 209 VMSQueueStruc *currQ;
Me@178 210
Me@178 211 stolenPr = NULL;
Me@178 212 coreIdx = masterPr->coreAnimatedBy;
Me@178 213 for( i = 0; i < NUM_CORES -1; i++ )
Me@178 214 {
Me@178 215 if( coreIdx >= NUM_CORES -1 )
Me@178 216 { coreIdx = 0;
Me@178 217 }
Me@178 218 else
Me@178 219 { coreIdx++;
Me@178 220 }
Me@178 221 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@178 222 if( numInVMSQ( currQ ) > 0 )
Me@178 223 { stolenPr = readVMSQ (currQ );
Me@178 224 break;
Me@178 225 }
Me@178 226 }
Me@178 227
Me@178 228 if( stolenPr != NULL )
Me@178 229 { currSlot->procrAssignedToSlot = stolenPr;
Me@178 230 stolenPr->schedSlot = currSlot;
Me@178 231 currSlot->needsProcrAssigned = FALSE;
Me@178 232
Me@178 233 writeVMSQ( stolenPr, readyToAnimateQ );
Me@178 234 }
Me@178 235 }
Me@178 236
Me@178 237 /*This algorithm makes the common case fast. Make the coreloop passive,
Me@178 238 * and show its progress. Make the stealer control a gate that coreloop
Me@178 239 * has to pass.
Me@178 240 *To avoid interference, only one stealer at a time. Use a global
Me@178 241 * stealer-lock.
Me@178 242 *
Me@178 243 *The pattern is based on a gate -- stealer shuts the gate, then monitors
Me@178 244 * to be sure any already past make it all the way out, before starting.
Me@178 245 *So, have a "progress" measure just before the gate, then have two after it,
Me@178 246 * one is in a "waiting room" outside the gate, the other is at the exit.
Me@178 247 *Then, the stealer first shuts the gate, then checks the progress measure
Me@178 248 * outside it, then looks to see if the progress measure at the exit is the
Me@178 249 * same. If yes, it knows the protected area is empty 'cause no other way
Me@178 250 * to get in and the last to get in also exited.
Me@178 251 *If the progress measure at the exit is not the same, then the stealer goes
Me@178 252 * into a loop checking both the waiting-area and the exit progress-measures
Me@178 253 * until one of them shows the same as the measure outside the gate. Might
Me@178 254 * as well re-read the measure outside the gate each go around, just to be
Me@178 255 * sure. It is guaranteed that one of the two will eventually match the one
Me@178 256 * outside the gate.
Me@178 257 *
Me@178 258 *Here's an informal proof of correctness:
Me@178 259 *The gate can be closed at any point, and have only four cases:
Me@178 260 * 1) coreloop made it past the gate-closing but not yet past the exit
Me@178 261 * 2) coreloop made it past the pre-gate progress update but not yet past
Me@178 262 * the gate,
Me@178 263 * 3) coreloop is right before the pre-gate update
Me@178 264 * 4) coreloop is past the exit and far from the pre-gate update.
Me@178 265 *
Me@178 266 * Covering the cases in reverse order,
Me@178 267 * 4) is not a problem -- stealer will read pre-gate progress, see that it
Me@178 268 * matches exit progress, and the gate is closed, so stealer can proceed.
Me@178 269 * 3) stealer will read pre-gate progress just after coreloop updates it..
Me@178 270 * so stealer goes into a loop until the coreloop causes wait-progress
Me@178 271 * to match pre-gate progress, so then stealer can proceed
Me@178 272 * 2) same as 3..
Me@178 273 * 1) stealer reads pre-gate progress, sees that it's different than exit,
Me@178 274 * so goes into loop until exit matches pre-gate, now it knows coreloop
Me@178 275 * is not in protected and cannot get back in, so can proceed.
Me@178 276 *
Me@178 277 *Implementation for the stealer:
Me@178 278 *
Me@178 279 *First, acquire the stealer lock -- only cores with no work to do will
Me@178 280 * compete to steal, so not a big performance penalty having only one --
Me@178 281 * will rarely have multiple stealers in a system with plenty of work -- and
Me@178 282 * in a system with little work, it doesn't matter.
Me@178 283 *
Me@178 284 *Note, have single-reader, single-writer pattern for all variables used to
Me@178 285 * communicate between stealer and victims
Me@178 286 *
Me@178 287 *So, scan the queues of the core loops, until find non-empty. Each core
Me@178 288 * has its own list that it scans. The list goes in order from closest to
Me@178 289 * furthest core, so it steals first from close cores. Later can add
Me@178 290 * taking info from the app about overlapping footprints, and scan all the
Me@178 291 * others then choose work with the most footprint overlap with the contents
Me@178 292 * of this core's cache.
Me@178 293 *
Me@178 294 *Now, have a victim want to take work from. So, shut the gate in that
Me@178 295 * coreloop, by setting the "gate closed" var on its stack to TRUE.
Me@178 296 *Then, read the core's pre-gate progress and compare to the core's exit
Me@178 297 * progress.
Me@178 298 *If same, can proceed to take work from the coreloop's queue. When done,
Me@178 299 * write FALSE to gate closed var.
Me@178 300 *If different, then enter a loop that reads the pre-gate progress, then
Me@178 301 * compares to exit progress then to wait progress. When one of two
Me@178 302 * matches, proceed. Take work from the coreloop's queue. When done,
Me@178 303 * write FALSE to the gate closed var.
Me@178 304 *
Me@178 305 */
Me@178 306 void inline
Me@178 307 gateProtected_stealWorkInto( SchedSlot *currSlot,
Me@178 308 VMSQueueStruc *myReadyToAnimateQ,
Me@178 309 VirtProcr *masterPr )
Me@178 310 {
Me@178 311 VirtProcr *stolenPr;
Me@178 312 int32 coreIdx, i, haveAVictim, gotLock;
Me@178 313 VMSQueueStruc *victimsQ;
Me@178 314
Me@178 315 volatile GateStruc *vicGate;
Me@178 316 int32 coreMightBeInProtected;
Me@178 317
Me@178 318
Me@178 319
Me@178 320 //see if any other cores have work available to steal
Me@178 321 haveAVictim = FALSE;
Me@178 322 coreIdx = masterPr->coreAnimatedBy;
Me@178 323 for( i = 0; i < NUM_CORES -1; i++ )
Me@178 324 {
Me@178 325 if( coreIdx >= NUM_CORES -1 )
Me@178 326 { coreIdx = 0;
Me@178 327 }
Me@178 328 else
Me@178 329 { coreIdx++;
Me@178 330 }
Me@178 331 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@178 332 if( numInVMSQ( victimsQ ) > 0 )
Me@178 333 { haveAVictim = TRUE;
Me@178 334 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
Me@178 335 break;
Me@178 336 }
Me@178 337 }
Me@178 338 if( !haveAVictim ) return; //no work to steal, exit
Me@178 339
Me@178 340 //have a victim core, now get the stealer-lock
Me@178 341 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
Me@178 342 UNLOCKED, LOCKED );
Me@178 343 if( !gotLock ) return; //go back to core loop, which will re-start master
Me@178 344
Me@178 345
Me@178 346 //====== Start Gate-protection =======
Me@178 347 vicGate->gateClosed = TRUE;
Me@178 348 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
Me@178 349 while( coreMightBeInProtected )
Me@178 350 { //wait until sure
Me@178 351 if( vicGate->preGateProgress == vicGate->waitProgress )
Me@178 352 coreMightBeInProtected = FALSE;
Me@178 353 if( vicGate->preGateProgress == vicGate->exitProgress )
Me@178 354 coreMightBeInProtected = FALSE;
Me@178 355 }
Me@178 356
Me@178 357 stolenPr = readVMSQ ( victimsQ );
Me@178 358
Me@178 359 vicGate->gateClosed = FALSE;
Me@178 360 //======= End Gate-protection =======
Me@178 361
Me@178 362
Me@178 363 if( stolenPr != NULL ) //victim could have been in protected and taken
Me@178 364 { currSlot->procrAssignedToSlot = stolenPr;
Me@178 365 stolenPr->schedSlot = currSlot;
Me@178 366 currSlot->needsProcrAssigned = FALSE;
Me@178 367
Me@178 368 writeVMSQ( stolenPr, myReadyToAnimateQ );
Me@178 369 }
Me@178 370
Me@178 371 //unlock the work stealing lock
Me@178 372 _VMSMasterEnv->workStealingLock = UNLOCKED;
Me@178 373 }