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