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