annotate MasterLoop.c @ 174:c3f458403cd6

Always allocate more than 256 byte, measurements removed in master loop, aligned slots
author Merten Sach <msach@mailbox.tu-berlin.de>
date Tue, 20 Dec 2011 16:50:21 +0100
parents d1dd9e6ee72c
children 91d0d2e06719
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@0 14
Me@0 15
Me@55 16 //===========================================================================
Me@55 17 void inline
Me@55 18 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
msach@169 19 VirtProcr *masterVP );
Me@55 20
Me@55 21 //===========================================================================
Me@55 22
Me@55 23
Me@0 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@4 52 *However means have to make masterEnv a global static volatile the same way
Me@31 53 * did with readyToAnimateQ in core loop. -- for performance, put the
Me@11 54 * jump to the core loop directly in here, and have it directly jump back.
Me@31 55 *
Me@31 56 *
Me@31 57 *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this
Me@31 58 * avoids the suspected bug in the system stack that causes bizarre faults
Me@31 59 * at random places in the system code.
Me@31 60 *
Me@31 61 *So, this function is coupled to each of the MasterVPs, -- meaning this
Me@31 62 * function can't rely on a particular stack and frame -- each MasterVP that
Me@31 63 * animates this function has a different one.
Me@31 64 *
Me@31 65 *At this point, the masterLoop does not write itself into the queue anymore,
Me@31 66 * instead, the coreLoop acquires the masterLock when it has nothing to
Me@31 67 * animate, and then animates its own masterLoop. However, still try to put
Me@31 68 * several AppVPs into the queue to amortize the startup cost of switching
Me@31 69 * to the MasterVP. Note, don't have to worry about latency of requests much
Me@31 70 * because most requests generate work for same core -- only latency issue
Me@31 71 * is case when other cores starved and one core's requests generate work
Me@31 72 * for them -- so keep max in queue to 3 or 4..
Me@4 73 */
Me@31 74 void masterLoop( void *initData, VirtProcr *animatingPr )
Me@21 75 {
Me@55 76 int32 slotIdx, numSlotsFilled;
Me@21 77 VirtProcr *schedVirtPr;
Me@31 78 SchedSlot *currSlot, **schedSlots;
Me@0 79 MasterEnv *masterEnv;
Me@31 80 VMSQueueStruc *readyToAnimateQ;
Me@4 81
Me@0 82 SlaveScheduler slaveScheduler;
Me@0 83 RequestHandler requestHandler;
Me@31 84 void *semanticEnv;
Me@0 85
Me@55 86 int32 thisCoresIdx;
msach@169 87 VirtProcr *masterVP;
msach@169 88 volatile VirtProcr *volatilemasterVP;
Me@31 89
msach@169 90 volatilemasterVP = animatingPr;
msach@169 91 masterVP = (VirtProcr*)volatilemasterVP; //used to force re-define after jmp
msach@169 92
msach@169 93 //====================== Measurement =====================
msach@169 94 TSCountLowHigh endMaster;
msach@169 95 uint64 numCycles;
msach@169 96 //==========================================================
Me@31 97
msach@174 98 masterEnv = (MasterEnv*)_VMSMasterEnv;
msach@174 99
msach@174 100 masterVP = (VirtProcr*)volatilemasterVP; //just to make sure after jmp
msach@174 101 thisCoresIdx = masterVP->coreAnimatedBy;
msach@174 102 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
msach@174 103 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
msach@174 104
msach@174 105 requestHandler = masterEnv->requestHandler;
msach@174 106 slaveScheduler = masterEnv->slaveScheduler;
msach@174 107 semanticEnv = masterEnv->semanticEnv;
msach@174 108
msach@174 109 //masterLoopStartPt:
msach@169 110 while(1){ //switch to core_loop and back to here is at end of loop
msach@71 111
msach@169 112 //============================= MEASUREMENT STUFF =======================
msach@169 113 #ifdef MEAS__TIME_MASTER
msach@169 114 //Total Master time includes one coreloop time -- just assume the core
msach@169 115 // loop time is same for Master as for AppVPs, even though it may be
msach@169 116 // smaller due to higher predictability of the fixed jmp.
msach@169 117 saveLowTimeStampCountInto( masterVP->startMasterTSCLow );
msach@169 118 #endif
msach@169 119 //=======================================================================
Me@0 120
Me@0 121
Me@31 122 //Poll each slot's Done flag
Me@55 123 numSlotsFilled = 0;
msach@174 124 /*
msach@169 125 Meas_startMasterLoop
msach@174 126 */
Me@26 127 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
Me@0 128 {
Me@4 129 currSlot = schedSlots[ slotIdx ];
Me@0 130
Me@4 131 if( currSlot->workIsDone )
Me@0 132 {
Me@4 133 currSlot->workIsDone = FALSE;
Me@4 134 currSlot->needsProcrAssigned = TRUE;
Me@0 135
Me@0 136 //process requests from slave to master
msach@174 137 /*
msach@169 138 Meas_startReqHdlr
msach@174 139 */
Me@21 140 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
msach@174 141 /*
msach@169 142 Meas_endReqHdlr
msach@174 143 */
Me@0 144 }
Me@4 145 if( currSlot->needsProcrAssigned )
Me@4 146 { //give slot a new virt procr
Me@21 147 schedVirtPr =
Me@31 148 (*slaveScheduler)( semanticEnv, thisCoresIdx );
Me@0 149
Me@21 150 if( schedVirtPr != NULL )
Me@21 151 { currSlot->procrAssignedToSlot = schedVirtPr;
Me@26 152 schedVirtPr->schedSlot = currSlot;
Me@26 153 currSlot->needsProcrAssigned = FALSE;
Me@55 154 numSlotsFilled += 1;
Me@55 155
Me@55 156 writeVMSQ( schedVirtPr, readyToAnimateQ );
Me@0 157 }
Me@0 158 }
Me@0 159 }
msach@174 160 /*
msach@169 161 Meas_endMasterLoop
msach@174 162 */
Me@55 163
Me@55 164 #ifdef USE_WORK_STEALING
Me@55 165 //If no slots filled, means no more work, look for work to steal.
Me@55 166 if( numSlotsFilled == 0 )
msach@169 167 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterVP );
Me@55 168 }
Me@55 169 #endif
Me@26 170
Me@21 171
Me@38 172
msach@169 173 //=================== Meas =======================
msach@169 174 #ifdef MEAS__TIME_MASTER
msach@169 175 saveLowTimeStampCountInto( masterVP->endMasterTSCLow );
msach@169 176 #endif
msach@169 177 #ifdef MEAS__TIME_2011_SYS
msach@169 178 //Take meas here, to get cycles since entered Master
msach@169 179 saveTSCLowHigh(endMaster);
msach@169 180 numCycles = endMaster.longVal - _VMSMasterEnv->startMaster.longVal;
msach@169 181
msach@169 182 if( numCycles < 200000 ) //sanity check against swap thd out)
msach@169 183 { masterEnv->totalMasterCycles += numCycles;
msach@169 184 masterEnv->numMasterAnimations++;
msach@169 185 }
msach@169 186 #endif
msach@169 187 //==================================================
msach@169 188 masterSwitchToCoreLoop(animatingPr); //"finishes" when switch back to Master
msach@71 189 flushRegisters();
msach@71 190 }//MasterLoop
Me@62 191
Me@0 192 }
Me@0 193
Me@55 194
Me@55 195
Me@55 196 /*This has a race condition -- the coreloops are accessing their own queues
Me@55 197 * at the same time that this work-stealer on a different core is trying to
Me@55 198 */
Me@55 199 void inline
Me@55 200 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
msach@169 201 VirtProcr *masterVP )
Me@55 202 {
Me@55 203 VirtProcr *stolenPr;
Me@55 204 int32 coreIdx, i;
Me@55 205 VMSQueueStruc *currQ;
Me@55 206
Me@55 207 stolenPr = NULL;
msach@169 208 coreIdx = masterVP->coreAnimatedBy;
Me@55 209 for( i = 0; i < NUM_CORES -1; i++ )
Me@55 210 {
Me@55 211 if( coreIdx >= NUM_CORES -1 )
Me@55 212 { coreIdx = 0;
Me@55 213 }
Me@55 214 else
Me@55 215 { coreIdx++;
Me@55 216 }
Me@55 217 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@55 218 if( numInVMSQ( currQ ) > 0 )
Me@55 219 { stolenPr = readVMSQ (currQ );
Me@55 220 break;
Me@55 221 }
Me@55 222 }
Me@55 223
Me@55 224 if( stolenPr != NULL )
Me@55 225 { currSlot->procrAssignedToSlot = stolenPr;
Me@55 226 stolenPr->schedSlot = currSlot;
Me@55 227 currSlot->needsProcrAssigned = FALSE;
Me@55 228
Me@55 229 writeVMSQ( stolenPr, readyToAnimateQ );
Me@55 230 }
Me@55 231 }
Me@55 232
Me@55 233 /*This algorithm makes the common case fast. Make the coreloop passive,
Me@55 234 * and show its progress. Make the stealer control a gate that coreloop
Me@55 235 * has to pass.
Me@55 236 *To avoid interference, only one stealer at a time. Use a global
Me@55 237 * stealer-lock.
Me@55 238 *
Me@55 239 *The pattern is based on a gate -- stealer shuts the gate, then monitors
Me@55 240 * to be sure any already past make it all the way out, before starting.
Me@55 241 *So, have a "progress" measure just before the gate, then have two after it,
Me@55 242 * one is in a "waiting room" outside the gate, the other is at the exit.
Me@55 243 *Then, the stealer first shuts the gate, then checks the progress measure
Me@55 244 * outside it, then looks to see if the progress measure at the exit is the
Me@55 245 * same. If yes, it knows the protected area is empty 'cause no other way
Me@55 246 * to get in and the last to get in also exited.
Me@55 247 *If the progress measure at the exit is not the same, then the stealer goes
Me@55 248 * into a loop checking both the waiting-area and the exit progress-measures
Me@55 249 * until one of them shows the same as the measure outside the gate. Might
Me@55 250 * as well re-read the measure outside the gate each go around, just to be
Me@55 251 * sure. It is guaranteed that one of the two will eventually match the one
Me@55 252 * outside the gate.
Me@55 253 *
Me@55 254 *Here's an informal proof of correctness:
Me@55 255 *The gate can be closed at any point, and have only four cases:
Me@55 256 * 1) coreloop made it past the gate-closing but not yet past the exit
Me@55 257 * 2) coreloop made it past the pre-gate progress update but not yet past
Me@55 258 * the gate,
Me@55 259 * 3) coreloop is right before the pre-gate update
Me@55 260 * 4) coreloop is past the exit and far from the pre-gate update.
Me@55 261 *
Me@55 262 * Covering the cases in reverse order,
Me@55 263 * 4) is not a problem -- stealer will read pre-gate progress, see that it
Me@55 264 * matches exit progress, and the gate is closed, so stealer can proceed.
Me@55 265 * 3) stealer will read pre-gate progress just after coreloop updates it..
Me@55 266 * so stealer goes into a loop until the coreloop causes wait-progress
Me@55 267 * to match pre-gate progress, so then stealer can proceed
Me@55 268 * 2) same as 3..
Me@55 269 * 1) stealer reads pre-gate progress, sees that it's different than exit,
Me@55 270 * so goes into loop until exit matches pre-gate, now it knows coreloop
Me@55 271 * is not in protected and cannot get back in, so can proceed.
Me@55 272 *
Me@55 273 *Implementation for the stealer:
Me@55 274 *
Me@55 275 *First, acquire the stealer lock -- only cores with no work to do will
Me@55 276 * compete to steal, so not a big performance penalty having only one --
Me@55 277 * will rarely have multiple stealers in a system with plenty of work -- and
Me@55 278 * in a system with little work, it doesn't matter.
Me@55 279 *
Me@55 280 *Note, have single-reader, single-writer pattern for all variables used to
Me@55 281 * communicate between stealer and victims
Me@55 282 *
Me@55 283 *So, scan the queues of the core loops, until find non-empty. Each core
Me@55 284 * has its own list that it scans. The list goes in order from closest to
Me@55 285 * furthest core, so it steals first from close cores. Later can add
Me@55 286 * taking info from the app about overlapping footprints, and scan all the
Me@55 287 * others then choose work with the most footprint overlap with the contents
Me@55 288 * of this core's cache.
Me@55 289 *
Me@55 290 *Now, have a victim want to take work from. So, shut the gate in that
Me@55 291 * coreloop, by setting the "gate closed" var on its stack to TRUE.
Me@55 292 *Then, read the core's pre-gate progress and compare to the core's exit
Me@55 293 * progress.
Me@55 294 *If same, can proceed to take work from the coreloop's queue. When done,
Me@55 295 * write FALSE to gate closed var.
Me@55 296 *If different, then enter a loop that reads the pre-gate progress, then
Me@55 297 * compares to exit progress then to wait progress. When one of two
Me@55 298 * matches, proceed. Take work from the coreloop's queue. When done,
Me@55 299 * write FALSE to the gate closed var.
Me@55 300 *
Me@55 301 */
Me@55 302 void inline
Me@55 303 gateProtected_stealWorkInto( SchedSlot *currSlot,
Me@55 304 VMSQueueStruc *myReadyToAnimateQ,
msach@169 305 VirtProcr *masterVP )
Me@55 306 {
Me@55 307 VirtProcr *stolenPr;
Me@55 308 int32 coreIdx, i, haveAVictim, gotLock;
Me@55 309 VMSQueueStruc *victimsQ;
Me@55 310
Me@55 311 volatile GateStruc *vicGate;
Me@55 312 int32 coreMightBeInProtected;
Me@55 313
Me@55 314
Me@55 315
Me@55 316 //see if any other cores have work available to steal
Me@55 317 haveAVictim = FALSE;
msach@169 318 coreIdx = masterVP->coreAnimatedBy;
Me@55 319 for( i = 0; i < NUM_CORES -1; i++ )
Me@55 320 {
Me@55 321 if( coreIdx >= NUM_CORES -1 )
Me@55 322 { coreIdx = 0;
Me@55 323 }
Me@55 324 else
Me@55 325 { coreIdx++;
Me@55 326 }
Me@55 327 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@55 328 if( numInVMSQ( victimsQ ) > 0 )
Me@55 329 { haveAVictim = TRUE;
Me@55 330 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
Me@55 331 break;
Me@55 332 }
Me@55 333 }
Me@55 334 if( !haveAVictim ) return; //no work to steal, exit
Me@55 335
Me@55 336 //have a victim core, now get the stealer-lock
Me@55 337 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
Me@55 338 UNLOCKED, LOCKED );
Me@55 339 if( !gotLock ) return; //go back to core loop, which will re-start master
Me@55 340
Me@55 341
Me@55 342 //====== Start Gate-protection =======
Me@55 343 vicGate->gateClosed = TRUE;
Me@55 344 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
Me@55 345 while( coreMightBeInProtected )
Me@55 346 { //wait until sure
Me@55 347 if( vicGate->preGateProgress == vicGate->waitProgress )
Me@55 348 coreMightBeInProtected = FALSE;
Me@55 349 if( vicGate->preGateProgress == vicGate->exitProgress )
Me@55 350 coreMightBeInProtected = FALSE;
Me@55 351 }
Me@55 352
Me@55 353 stolenPr = readVMSQ ( victimsQ );
Me@55 354
Me@55 355 vicGate->gateClosed = FALSE;
Me@55 356 //======= End Gate-protection =======
Me@55 357
Me@55 358
Me@55 359 if( stolenPr != NULL ) //victim could have been in protected and taken
Me@55 360 { currSlot->procrAssignedToSlot = stolenPr;
Me@55 361 stolenPr->schedSlot = currSlot;
Me@55 362 currSlot->needsProcrAssigned = FALSE;
Me@55 363
Me@55 364 writeVMSQ( stolenPr, myReadyToAnimateQ );
Me@55 365 }
Me@55 366
Me@55 367 //unlock the work stealing lock
Me@55 368 _VMSMasterEnv->workStealingLock = UNLOCKED;
Me@55 369 }