Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view MasterLoop.c @ 66:bf08108405cc
Added recycle pool -- will merge later -- need to get PLDI results for now
| author | Me |
|---|---|
| date | Mon, 15 Nov 2010 12:11:24 -0800 |
| parents | 3bac84e4e56e |
| children | a6c442d52590 9c3107044f86 |
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 = 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 will be
112 // smaller due to high predictability of the fixed jmp.
113 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
114 #endif
115 //========================================================================
117 masterEnv = _VMSMasterEnv;
119 //GCC may optimize so doesn't always re-define from frame-storage
120 masterPr = 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 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
143 }
144 if( currSlot->needsProcrAssigned )
145 { //give slot a new virt procr
146 schedVirtPr =
147 (*slaveScheduler)( semanticEnv, thisCoresIdx );
149 if( schedVirtPr != NULL )
150 { currSlot->procrAssignedToSlot = schedVirtPr;
151 schedVirtPr->schedSlot = currSlot;
152 currSlot->needsProcrAssigned = FALSE;
153 numSlotsFilled += 1;
155 writeVMSQ( schedVirtPr, readyToAnimateQ );
156 }
157 }
158 }
161 #ifdef USE_WORK_STEALING
162 //If no slots filled, means no more work, look for work to steal.
163 if( numSlotsFilled == 0 )
164 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
165 }
166 #endif
169 #ifdef MEAS__TIME_MASTER
170 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
171 #endif
175 /* VirtProcr offsets:
176 * 0xc stackPtr
177 * 0x10 framePtr
178 * 0x14 nextInstrPt
179 * 0x1c coreLoopFramePtr
180 * 0x20 coreLoopStackPtr
181 *
182 * _VMSMasterEnv offsets:
183 * 0x24 coreLoopStartPt
184 * 0x28 coreLoopEndPt
185 * 0x30 masterLock
186 */
187 // masterSwitchToCoreLoop( masterPr )
188 asm volatile("movl %0, %%ebx; \
189 movl %1, %%ecx; \
190 movl %%esp, 0x0c(%%ecx); \
191 movl %%ebp, 0x10(%%ecx); \
192 movl 0x24(%%ebx), %%eax; \
193 movl 0x20(%%ecx), %%esp; \
194 movl 0x1c(%%ecx), %%ebp; \
195 movl $0x0, 0x30(%%ebx); \
196 jmp %%eax" \
197 /* outputs */ : \
198 /* inputs */ : "g"(_VMSMasterEnv), "g"(masterPr) \
199 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
200 );
202 }
206 /*This has a race condition -- the coreloops are accessing their own queues
207 * at the same time that this work-stealer on a different core is trying to
208 */
209 void inline
210 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
211 VirtProcr *masterPr )
212 {
213 VirtProcr *stolenPr;
214 int32 coreIdx, i;
215 VMSQueueStruc *currQ;
217 stolenPr = NULL;
218 coreIdx = masterPr->coreAnimatedBy;
219 for( i = 0; i < NUM_CORES -1; i++ )
220 {
221 if( coreIdx >= NUM_CORES -1 )
222 { coreIdx = 0;
223 }
224 else
225 { coreIdx++;
226 }
227 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
228 if( numInVMSQ( currQ ) > 0 )
229 { stolenPr = readVMSQ (currQ );
230 break;
231 }
232 }
234 if( stolenPr != NULL )
235 { currSlot->procrAssignedToSlot = stolenPr;
236 stolenPr->schedSlot = currSlot;
237 currSlot->needsProcrAssigned = FALSE;
239 writeVMSQ( stolenPr, readyToAnimateQ );
240 }
241 }
243 /*This algorithm makes the common case fast. Make the coreloop passive,
244 * and show its progress. Make the stealer control a gate that coreloop
245 * has to pass.
246 *To avoid interference, only one stealer at a time. Use a global
247 * stealer-lock.
248 *
249 *The pattern is based on a gate -- stealer shuts the gate, then monitors
250 * to be sure any already past make it all the way out, before starting.
251 *So, have a "progress" measure just before the gate, then have two after it,
252 * one is in a "waiting room" outside the gate, the other is at the exit.
253 *Then, the stealer first shuts the gate, then checks the progress measure
254 * outside it, then looks to see if the progress measure at the exit is the
255 * same. If yes, it knows the protected area is empty 'cause no other way
256 * to get in and the last to get in also exited.
257 *If the progress measure at the exit is not the same, then the stealer goes
258 * into a loop checking both the waiting-area and the exit progress-measures
259 * until one of them shows the same as the measure outside the gate. Might
260 * as well re-read the measure outside the gate each go around, just to be
261 * sure. It is guaranteed that one of the two will eventually match the one
262 * outside the gate.
263 *
264 *Here's an informal proof of correctness:
265 *The gate can be closed at any point, and have only four cases:
266 * 1) coreloop made it past the gate-closing but not yet past the exit
267 * 2) coreloop made it past the pre-gate progress update but not yet past
268 * the gate,
269 * 3) coreloop is right before the pre-gate update
270 * 4) coreloop is past the exit and far from the pre-gate update.
271 *
272 * Covering the cases in reverse order,
273 * 4) is not a problem -- stealer will read pre-gate progress, see that it
274 * matches exit progress, and the gate is closed, so stealer can proceed.
275 * 3) stealer will read pre-gate progress just after coreloop updates it..
276 * so stealer goes into a loop until the coreloop causes wait-progress
277 * to match pre-gate progress, so then stealer can proceed
278 * 2) same as 3..
279 * 1) stealer reads pre-gate progress, sees that it's different than exit,
280 * so goes into loop until exit matches pre-gate, now it knows coreloop
281 * is not in protected and cannot get back in, so can proceed.
282 *
283 *Implementation for the stealer:
284 *
285 *First, acquire the stealer lock -- only cores with no work to do will
286 * compete to steal, so not a big performance penalty having only one --
287 * will rarely have multiple stealers in a system with plenty of work -- and
288 * in a system with little work, it doesn't matter.
289 *
290 *Note, have single-reader, single-writer pattern for all variables used to
291 * communicate between stealer and victims
292 *
293 *So, scan the queues of the core loops, until find non-empty. Each core
294 * has its own list that it scans. The list goes in order from closest to
295 * furthest core, so it steals first from close cores. Later can add
296 * taking info from the app about overlapping footprints, and scan all the
297 * others then choose work with the most footprint overlap with the contents
298 * of this core's cache.
299 *
300 *Now, have a victim want to take work from. So, shut the gate in that
301 * coreloop, by setting the "gate closed" var on its stack to TRUE.
302 *Then, read the core's pre-gate progress and compare to the core's exit
303 * progress.
304 *If same, can proceed to take work from the coreloop's queue. When done,
305 * write FALSE to gate closed var.
306 *If different, then enter a loop that reads the pre-gate progress, then
307 * compares to exit progress then to wait progress. When one of two
308 * matches, proceed. Take work from the coreloop's queue. When done,
309 * write FALSE to the gate closed var.
310 *
311 */
312 void inline
313 gateProtected_stealWorkInto( SchedSlot *currSlot,
314 VMSQueueStruc *myReadyToAnimateQ,
315 VirtProcr *masterPr )
316 {
317 VirtProcr *stolenPr;
318 int32 coreIdx, i, haveAVictim, gotLock;
319 VMSQueueStruc *victimsQ;
321 volatile GateStruc *vicGate;
322 int32 coreMightBeInProtected;
326 //see if any other cores have work available to steal
327 haveAVictim = FALSE;
328 coreIdx = masterPr->coreAnimatedBy;
329 for( i = 0; i < NUM_CORES -1; i++ )
330 {
331 if( coreIdx >= NUM_CORES -1 )
332 { coreIdx = 0;
333 }
334 else
335 { coreIdx++;
336 }
337 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
338 if( numInVMSQ( victimsQ ) > 0 )
339 { haveAVictim = TRUE;
340 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
341 break;
342 }
343 }
344 if( !haveAVictim ) return; //no work to steal, exit
346 //have a victim core, now get the stealer-lock
347 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
348 UNLOCKED, LOCKED );
349 if( !gotLock ) return; //go back to core loop, which will re-start master
352 //====== Start Gate-protection =======
353 vicGate->gateClosed = TRUE;
354 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
355 while( coreMightBeInProtected )
356 { //wait until sure
357 if( vicGate->preGateProgress == vicGate->waitProgress )
358 coreMightBeInProtected = FALSE;
359 if( vicGate->preGateProgress == vicGate->exitProgress )
360 coreMightBeInProtected = FALSE;
361 }
363 stolenPr = readVMSQ ( victimsQ );
365 vicGate->gateClosed = FALSE;
366 //======= End Gate-protection =======
369 if( stolenPr != NULL ) //victim could have been in protected and taken
370 { currSlot->procrAssignedToSlot = stolenPr;
371 stolenPr->schedSlot = currSlot;
372 currSlot->needsProcrAssigned = FALSE;
374 writeVMSQ( stolenPr, myReadyToAnimateQ );
375 }
377 //unlock the work stealing lock
378 _VMSMasterEnv->workStealingLock = UNLOCKED;
379 }
