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