annotate MasterLoop.c @ 120:d4c881c7f03a

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