annotate MasterLoop.c @ 68:9c3107044f86

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