annotate MasterLoop.c @ 108:3bc3b89630c7

perf counters
author engelhardt@cray1
date Tue, 26 Jul 2011 15:36:24 +0200
parents fe5ec83f1baf
children 659299627e70
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,
Me@55 19 VirtProcr *masterPr );
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;
Me@31 87 VirtProcr *masterPr;
Me@31 88 volatile VirtProcr *volatileMasterPr;
Me@31 89
Me@31 90 volatileMasterPr = animatingPr;
msach@69 91 masterPr = (VirtProcr*)volatileMasterPr; //used to force re-define after jmp
Me@31 92
Me@31 93 //First animation of each MasterVP will in turn animate this part
Me@31 94 // of setup code.. (VP creator sets up the stack as if this function
Me@31 95 // was called normally, but actually get here by jmp)
Me@31 96 //So, setup values about stack ptr, jmp pt and all that
msach@71 97 //masterPr->nextInstrPt = &&masterLoopStartPt;
Me@0 98
Me@26 99
Me@31 100 //Note, got rid of writing the stack and frame ptr up here, because
Me@31 101 // only one
Me@31 102 // core can ever animate a given MasterVP, so don't need to communicate
Me@31 103 // new frame and stack ptr to the MasterVP storage before a second
Me@31 104 // version of that MasterVP can get animated on a different core.
Me@31 105 //Also got rid of the busy-wait.
Me@26 106
Me@31 107
msach@71 108 //masterLoopStartPt:
msach@71 109 while(1){
msach@71 110
Me@38 111 //============================= MEASUREMENT STUFF ========================
Me@38 112 #ifdef MEAS__TIME_MASTER
Me@38 113 //Total Master time includes one coreloop time -- just assume the core
Me@68 114 // loop time is same for Master as for AppVPs, even though it may be
Me@68 115 // smaller due to higher predictability of the fixed jmp.
Me@38 116 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
Me@38 117 #endif
Me@38 118 //========================================================================
Me@0 119
msach@69 120 masterEnv = (MasterEnv*)_VMSMasterEnv;
Me@4 121
Me@55 122 //GCC may optimize so doesn't always re-define from frame-storage
msach@69 123 masterPr = (VirtProcr*)volatileMasterPr; //just to make sure after jmp
Me@31 124 thisCoresIdx = masterPr->coreAnimatedBy;
Me@31 125 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
Me@31 126 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
Me@4 127
Me@0 128 requestHandler = masterEnv->requestHandler;
Me@0 129 slaveScheduler = masterEnv->slaveScheduler;
Me@21 130 semanticEnv = masterEnv->semanticEnv;
Me@0 131
Me@0 132
Me@31 133 //Poll each slot's Done flag
Me@55 134 numSlotsFilled = 0;
Me@26 135 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
Me@0 136 {
Me@4 137 currSlot = schedSlots[ slotIdx ];
Me@0 138
Me@4 139 if( currSlot->workIsDone )
Me@0 140 {
Me@4 141 currSlot->workIsDone = FALSE;
Me@4 142 currSlot->needsProcrAssigned = TRUE;
Me@0 143
Me@0 144 //process requests from slave to master
Me@68 145 //====================== MEASUREMENT STUFF ===================
Me@68 146 #ifdef MEAS__TIME_PLUGIN
Me@68 147 int32 startStamp1, endStamp1;
Me@68 148 saveLowTimeStampCountInto( startStamp1 );
Me@68 149 #endif
engelhardt@108 150 #ifdef MEAS__PERF_COUNTER
engelhardt@108 151 int lastRecordIdx = currSlot->procrAssignedToSlot->counter_history_array_info->numInArray -1;
engelhardt@108 152 CounterRecord* lastRecord = currSlot->procrAssignedToSlot->counter_history[lastRecordIdx];
engelhardt@108 153 lastRecord->req_core = thisCoresIdx;
engelhardt@108 154 int cycles_fd = masterEnv->cycles_counter_fd[thisCoresIdx];
engelhardt@108 155 int instrs_fd = masterEnv->instrs_counter_fd[thisCoresIdx];
engelhardt@108 156 int nread;
engelhardt@108 157
engelhardt@108 158 nread = read(cycles_fd,&(lastRecord->req_cycles),sizeof(lastRecord->req_cycles));
engelhardt@108 159 if(nread<0){
engelhardt@108 160 lastRecord->req_cycles = 0;
engelhardt@108 161 }
engelhardt@108 162
engelhardt@108 163 nread = read(instrs_fd,&(lastRecord->req_instrs),sizeof(lastRecord->req_instrs));
engelhardt@108 164 if(nread<0){
engelhardt@108 165 lastRecord->req_instrs = 0;
engelhardt@108 166 }
engelhardt@108 167 //End of task, start of next task
engelhardt@108 168 //print counters from last run
engelhardt@108 169 print_record(lastRecord);
engelhardt@108 170 //create new entry in record array here
engelhardt@108 171 CounterRecord* newRecord = VMS__malloc(sizeof(CounterRecord));
engelhardt@108 172 newRecord->req_core = thisCoresIdx;
engelhardt@108 173 addToDynArray( (void*) newRecord, currSlot->procrAssignedToSlot->counter_history_array_info);
engelhardt@108 174 lastRecord = newRecord;
engelhardt@108 175 #endif
Me@68 176 //============================================================
Me@21 177 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
Me@68 178 //====================== MEASUREMENT STUFF ===================
Me@68 179 #ifdef MEAS__TIME_PLUGIN
Me@68 180 saveLowTimeStampCountInto( endStamp1 );
Me@68 181 addIntervalToHist( startStamp1, endStamp1,
Me@68 182 _VMSMasterEnv->reqHdlrLowTimeHist );
Me@68 183 addIntervalToHist( startStamp1, endStamp1,
Me@68 184 _VMSMasterEnv->reqHdlrHighTimeHist );
Me@68 185 #endif
engelhardt@108 186 #ifdef MEAS__PERF_COUNTER
engelhardt@108 187
engelhardt@108 188 nread = read(cycles_fd,&(lastRecord->sc_done_cycles),sizeof(lastRecord->sc_done_cycles));
engelhardt@108 189 if(nread<0){
engelhardt@108 190 lastRecord->sc_done_cycles = 0;
engelhardt@108 191 }
engelhardt@108 192 nread = read(instrs_fd,&(lastRecord->sc_done_instrs),sizeof(lastRecord->sc_done_instrs));
engelhardt@108 193 if(nread<0){
engelhardt@108 194 lastRecord->sc_done_instrs = 0;
engelhardt@108 195 }
engelhardt@108 196 #endif
Me@68 197 //============================================================
Me@0 198 }
Me@4 199 if( currSlot->needsProcrAssigned )
Me@4 200 { //give slot a new virt procr
engelhardt@108 201 #ifdef MEAS__PERF_COUNTER
engelhardt@108 202 //start assigner
engelhardt@108 203 int cycles_fd = masterEnv->cycles_counter_fd[thisCoresIdx];
engelhardt@108 204 int instrs_fd = masterEnv->instrs_counter_fd[thisCoresIdx];
engelhardt@108 205 uint64 tmp_cycles;
engelhardt@108 206 uint64 tmp_instrs;
engelhardt@108 207 int nread=0;
engelhardt@108 208
engelhardt@108 209 nread = read(cycles_fd,&tmp_cycles,sizeof(uint64));
engelhardt@108 210 if(nread<0){
engelhardt@108 211 tmp_cycles = 0;
engelhardt@108 212 }
engelhardt@108 213
engelhardt@108 214 nread = read(instrs_fd,&tmp_instrs,sizeof(uint64));
engelhardt@108 215 if(nread<0){
engelhardt@108 216 tmp_instrs = 0;
engelhardt@108 217 }
engelhardt@108 218 #endif
Me@21 219 schedVirtPr =
Me@31 220 (*slaveScheduler)( semanticEnv, thisCoresIdx );
engelhardt@108 221 #ifdef MEAS__PERF_COUNTER
engelhardt@108 222 //end assigner
engelhardt@108 223 int lastRecordIdx = currSlot->procrAssignedToSlot->counter_history_array_info->numInArray -1;
engelhardt@108 224 CounterRecord* lastRecord = currSlot->procrAssignedToSlot->counter_history[lastRecordIdx];
engelhardt@108 225 lastRecord->assigning_core = thisCoresIdx;
engelhardt@108 226 lastRecord->start_assign_cycles = tmp_cycles;
engelhardt@108 227 lastRecord->start_assign_instrs = tmp_instrs;
engelhardt@108 228
engelhardt@108 229 nread = read(cycles_fd,&(lastRecord->end_assign_cycles),sizeof(lastRecord->end_assign_cycles));
engelhardt@108 230 if(nread<0){
engelhardt@108 231 lastRecord->end_assign_cycles = 0;
engelhardt@108 232 }
engelhardt@108 233
engelhardt@108 234 nread = read(instrs_fd,&(lastRecord->end_assign_instrs),sizeof(lastRecord->end_assign_instrs));
engelhardt@108 235 if(nread<0){
engelhardt@108 236 lastRecord->end_assign_instrs = 0;
engelhardt@108 237 }
engelhardt@108 238 #endif
Me@21 239 if( schedVirtPr != NULL )
Me@21 240 { currSlot->procrAssignedToSlot = schedVirtPr;
Me@26 241 schedVirtPr->schedSlot = currSlot;
Me@26 242 currSlot->needsProcrAssigned = FALSE;
Me@55 243 numSlotsFilled += 1;
Me@55 244
Me@55 245 writeVMSQ( schedVirtPr, readyToAnimateQ );
Me@0 246 }
Me@0 247 }
Me@0 248 }
Me@0 249
Me@55 250
Me@55 251 #ifdef USE_WORK_STEALING
Me@55 252 //If no slots filled, means no more work, look for work to steal.
Me@55 253 if( numSlotsFilled == 0 )
Me@55 254 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
Me@55 255 }
Me@55 256 #endif
Me@26 257
Me@21 258
Me@38 259 #ifdef MEAS__TIME_MASTER
Me@38 260 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
Me@38 261 #endif
Me@38 262
msach@71 263 masterSwitchToCoreLoop(animatingPr);
msach@71 264 flushRegisters();
msach@71 265 }//MasterLoop
Me@62 266
Me@62 267
Me@0 268 }
Me@0 269
Me@55 270
Me@55 271
Me@55 272 /*This has a race condition -- the coreloops are accessing their own queues
Me@55 273 * at the same time that this work-stealer on a different core is trying to
Me@55 274 */
Me@55 275 void inline
Me@55 276 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
Me@55 277 VirtProcr *masterPr )
Me@55 278 {
Me@55 279 VirtProcr *stolenPr;
Me@55 280 int32 coreIdx, i;
Me@55 281 VMSQueueStruc *currQ;
Me@55 282
Me@55 283 stolenPr = NULL;
Me@55 284 coreIdx = masterPr->coreAnimatedBy;
Me@55 285 for( i = 0; i < NUM_CORES -1; i++ )
Me@55 286 {
Me@55 287 if( coreIdx >= NUM_CORES -1 )
Me@55 288 { coreIdx = 0;
Me@55 289 }
Me@55 290 else
Me@55 291 { coreIdx++;
Me@55 292 }
Me@55 293 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@55 294 if( numInVMSQ( currQ ) > 0 )
Me@55 295 { stolenPr = readVMSQ (currQ );
Me@55 296 break;
Me@55 297 }
Me@55 298 }
Me@55 299
Me@55 300 if( stolenPr != NULL )
Me@55 301 { currSlot->procrAssignedToSlot = stolenPr;
Me@55 302 stolenPr->schedSlot = currSlot;
Me@55 303 currSlot->needsProcrAssigned = FALSE;
Me@55 304
Me@55 305 writeVMSQ( stolenPr, readyToAnimateQ );
Me@55 306 }
Me@55 307 }
Me@55 308
Me@55 309 /*This algorithm makes the common case fast. Make the coreloop passive,
Me@55 310 * and show its progress. Make the stealer control a gate that coreloop
Me@55 311 * has to pass.
Me@55 312 *To avoid interference, only one stealer at a time. Use a global
Me@55 313 * stealer-lock.
Me@55 314 *
Me@55 315 *The pattern is based on a gate -- stealer shuts the gate, then monitors
Me@55 316 * to be sure any already past make it all the way out, before starting.
Me@55 317 *So, have a "progress" measure just before the gate, then have two after it,
Me@55 318 * one is in a "waiting room" outside the gate, the other is at the exit.
Me@55 319 *Then, the stealer first shuts the gate, then checks the progress measure
Me@55 320 * outside it, then looks to see if the progress measure at the exit is the
Me@55 321 * same. If yes, it knows the protected area is empty 'cause no other way
Me@55 322 * to get in and the last to get in also exited.
Me@55 323 *If the progress measure at the exit is not the same, then the stealer goes
Me@55 324 * into a loop checking both the waiting-area and the exit progress-measures
Me@55 325 * until one of them shows the same as the measure outside the gate. Might
Me@55 326 * as well re-read the measure outside the gate each go around, just to be
Me@55 327 * sure. It is guaranteed that one of the two will eventually match the one
Me@55 328 * outside the gate.
Me@55 329 *
Me@55 330 *Here's an informal proof of correctness:
Me@55 331 *The gate can be closed at any point, and have only four cases:
Me@55 332 * 1) coreloop made it past the gate-closing but not yet past the exit
Me@55 333 * 2) coreloop made it past the pre-gate progress update but not yet past
Me@55 334 * the gate,
Me@55 335 * 3) coreloop is right before the pre-gate update
Me@55 336 * 4) coreloop is past the exit and far from the pre-gate update.
Me@55 337 *
Me@55 338 * Covering the cases in reverse order,
Me@55 339 * 4) is not a problem -- stealer will read pre-gate progress, see that it
Me@55 340 * matches exit progress, and the gate is closed, so stealer can proceed.
Me@55 341 * 3) stealer will read pre-gate progress just after coreloop updates it..
Me@55 342 * so stealer goes into a loop until the coreloop causes wait-progress
Me@55 343 * to match pre-gate progress, so then stealer can proceed
Me@55 344 * 2) same as 3..
Me@55 345 * 1) stealer reads pre-gate progress, sees that it's different than exit,
Me@55 346 * so goes into loop until exit matches pre-gate, now it knows coreloop
Me@55 347 * is not in protected and cannot get back in, so can proceed.
Me@55 348 *
Me@55 349 *Implementation for the stealer:
Me@55 350 *
Me@55 351 *First, acquire the stealer lock -- only cores with no work to do will
Me@55 352 * compete to steal, so not a big performance penalty having only one --
Me@55 353 * will rarely have multiple stealers in a system with plenty of work -- and
Me@55 354 * in a system with little work, it doesn't matter.
Me@55 355 *
Me@55 356 *Note, have single-reader, single-writer pattern for all variables used to
Me@55 357 * communicate between stealer and victims
Me@55 358 *
Me@55 359 *So, scan the queues of the core loops, until find non-empty. Each core
Me@55 360 * has its own list that it scans. The list goes in order from closest to
Me@55 361 * furthest core, so it steals first from close cores. Later can add
Me@55 362 * taking info from the app about overlapping footprints, and scan all the
Me@55 363 * others then choose work with the most footprint overlap with the contents
Me@55 364 * of this core's cache.
Me@55 365 *
Me@55 366 *Now, have a victim want to take work from. So, shut the gate in that
Me@55 367 * coreloop, by setting the "gate closed" var on its stack to TRUE.
Me@55 368 *Then, read the core's pre-gate progress and compare to the core's exit
Me@55 369 * progress.
Me@55 370 *If same, can proceed to take work from the coreloop's queue. When done,
Me@55 371 * write FALSE to gate closed var.
Me@55 372 *If different, then enter a loop that reads the pre-gate progress, then
Me@55 373 * compares to exit progress then to wait progress. When one of two
Me@55 374 * matches, proceed. Take work from the coreloop's queue. When done,
Me@55 375 * write FALSE to the gate closed var.
Me@55 376 *
Me@55 377 */
Me@55 378 void inline
Me@55 379 gateProtected_stealWorkInto( SchedSlot *currSlot,
Me@55 380 VMSQueueStruc *myReadyToAnimateQ,
Me@55 381 VirtProcr *masterPr )
Me@55 382 {
Me@55 383 VirtProcr *stolenPr;
Me@55 384 int32 coreIdx, i, haveAVictim, gotLock;
Me@55 385 VMSQueueStruc *victimsQ;
Me@55 386
Me@55 387 volatile GateStruc *vicGate;
Me@55 388 int32 coreMightBeInProtected;
Me@55 389
Me@55 390
Me@55 391
Me@55 392 //see if any other cores have work available to steal
Me@55 393 haveAVictim = FALSE;
Me@55 394 coreIdx = masterPr->coreAnimatedBy;
Me@55 395 for( i = 0; i < NUM_CORES -1; i++ )
Me@55 396 {
Me@55 397 if( coreIdx >= NUM_CORES -1 )
Me@55 398 { coreIdx = 0;
Me@55 399 }
Me@55 400 else
Me@55 401 { coreIdx++;
Me@55 402 }
Me@55 403 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
Me@55 404 if( numInVMSQ( victimsQ ) > 0 )
Me@55 405 { haveAVictim = TRUE;
Me@55 406 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
Me@55 407 break;
Me@55 408 }
Me@55 409 }
Me@55 410 if( !haveAVictim ) return; //no work to steal, exit
Me@55 411
Me@55 412 //have a victim core, now get the stealer-lock
Me@55 413 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
Me@55 414 UNLOCKED, LOCKED );
Me@55 415 if( !gotLock ) return; //go back to core loop, which will re-start master
Me@55 416
Me@55 417
Me@55 418 //====== Start Gate-protection =======
Me@55 419 vicGate->gateClosed = TRUE;
Me@55 420 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
Me@55 421 while( coreMightBeInProtected )
Me@55 422 { //wait until sure
Me@55 423 if( vicGate->preGateProgress == vicGate->waitProgress )
Me@55 424 coreMightBeInProtected = FALSE;
Me@55 425 if( vicGate->preGateProgress == vicGate->exitProgress )
Me@55 426 coreMightBeInProtected = FALSE;
Me@55 427 }
Me@55 428
Me@55 429 stolenPr = readVMSQ ( victimsQ );
Me@55 430
Me@55 431 vicGate->gateClosed = FALSE;
Me@55 432 //======= End Gate-protection =======
Me@55 433
Me@55 434
Me@55 435 if( stolenPr != NULL ) //victim could have been in protected and taken
Me@55 436 { currSlot->procrAssignedToSlot = stolenPr;
Me@55 437 stolenPr->schedSlot = currSlot;
Me@55 438 currSlot->needsProcrAssigned = FALSE;
Me@55 439
Me@55 440 writeVMSQ( stolenPr, myReadyToAnimateQ );
Me@55 441 }
Me@55 442
Me@55 443 //unlock the work stealing lock
Me@55 444 _VMSMasterEnv->workStealingLock = UNLOCKED;
Me@55 445 }