view VMS.c @ 22:1dbc7f6e3e67

Full VMS test -- works
author Me
date Wed, 30 Jun 2010 13:10:59 -0700
parents 734c665500e4
children 2b161e1a50ee
line source
1 /*
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <malloc.h>
11 #include "VMS.h"
12 #include "Queue_impl/BlockingQueue.h"
15 //===========================================================================
16 void
17 shutdownFn( void *dummy, VirtProcr *dummy2 );
19 void
20 create_sched_slots( MasterEnv *masterEnv );
22 //===========================================================================
24 /*Setup has two phases:
25 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
26 * the master virt procr into the work-queue, ready for first "call"
27 * 2) Semantic layer then does its own init, which creates the seed virt
28 * procr inside the semantic layer, ready to schedule it when
29 * asked by the first run of the masterLoop.
30 *
31 *This part is bit weird because VMS really wants to be "always there", and
32 * have applications attach and detach.. for now, this VMS is part of
33 * the app, so the VMS system starts up as part of running the app.
34 *
35 *The semantic layer is isolated from the VMS internals by making the
36 * semantic layer do setup to a state that it's ready with its
37 * initial virt procrs, ready to schedule them to slots when the masterLoop
38 * asks. Without this pattern, the semantic layer's setup would
39 * have to modify slots directly to assign the initial virt-procrs, and put
40 * them into the workQ itself, breaking the isolation completely.
41 *
42 *
43 *The semantic layer creates the initial virt procr(s), and adds its
44 * own environment to masterEnv, and fills in the pointers to
45 * the requestHandler and slaveScheduler plug-in functions
46 */
48 /*This allocates VMS data structures, populates the master VMSProc,
49 * and master environment, and returns the master environment to the semantic
50 * layer.
51 */
52 void
53 VMS__init()
54 { MasterEnv *masterEnv;
55 CASQueueStruc *workQ;
57 //Make the central work-queue
58 _VMSWorkQ = makeCASQ();
59 workQ = _VMSWorkQ;
61 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
62 masterEnv = _VMSMasterEnv;
64 //create the master virtual processor
65 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
67 create_sched_slots( masterEnv );
69 //Set slot 0 to be the master virt procr & set flags just in case
70 masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch
71 masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch
72 masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr;
73 masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0];
75 //First core loop to start up gets this, which will schedule seed Pr
76 //TODO: debug: check address of masterVirtPr
77 writeCASQ( masterEnv->masterVirtPr, workQ );
79 numProcrsCreated = 1;
80 }
83 void
84 create_sched_slots( MasterEnv *masterEnv )
85 { SchedSlot **schedSlots, **filledSlots;
86 int i;
88 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
89 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
90 masterEnv->schedSlots = schedSlots;
91 masterEnv->filledSlots = filledSlots;
93 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
94 {
95 schedSlots[i] = malloc( sizeof(SchedSlot) );
97 //Set state to mean "handling requests done, slot needs filling"
98 schedSlots[i]->workIsDone = FALSE;
99 schedSlots[i]->needsProcrAssigned = TRUE;
100 }
101 }
104 /*Semantic layer calls this when it want the system to start running..
105 *
106 *This creates the core loops, pins them to physical cores, gives them the
107 * pointer to the workQ, and starts them running.
108 */
109 void
110 VMS__start()
111 { int coreIdx;
113 //TODO: Save "orig" stack pointer and frame ptr -- restore in VMS__end()
114 //Create the win threads that animate the core loops
115 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
116 {
117 coreLoopThdParams[coreIdx] = (ThdParams *)malloc( sizeof(ThdParams) );
118 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
120 coreLoopThdHandles[coreIdx] =
121 CreateThread ( NULL, // Security attributes
122 0, // Stack size
123 coreLoop,
124 coreLoopThdParams[coreIdx],
125 CREATE_SUSPENDED,
126 &(coreLoopThdIds[coreIdx])
127 );
128 ResumeThread( coreLoopThdHandles[coreIdx] ); //starts thread
129 }
130 }
134 /*Create stack, then create __cdecl structure on it and put initialData and
135 * pointer to the new structure instance into the parameter positions on
136 * the stack
137 *Then put function pointer into nextInstrPt -- the stack is setup in std
138 * call structure, so jumping to function ptr is same as a GCC generated
139 * function call
140 *No need to save registers on old stack frame, because there's no old
141 * animator state to return to --
142 *
143 */
144 VirtProcr *
145 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
146 { VirtProcr *newPr;
147 char *stackLocs, *stackPtr;
149 newPr = malloc( sizeof(VirtProcr) );
150 newPr->procrID = numProcrsCreated++;
151 newPr->nextInstrPt = fnPtr;
152 newPr->initialData = initialData;
154 //fnPtr takes two params -- void *initData & void *animProcr
155 //alloc stack locations, make stackPtr be the highest addr minus room
156 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
157 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
158 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
159 newPr->startOfStack = stackLocs;
160 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
161 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
162 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
163 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
164 newPr->stackPtr = stackPtr; //core loop will switch to this, then
165 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
167 return newPr;
168 }
171 /*there is a label inside this function -- save the addr of this label in
172 * the callingPr struc, as the pick-up point from which to start the next
173 * work-unit for that procr. If turns out have to save registers, then
174 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
175 * "done with work-unit" label. The procr struc is in the request in the
176 * slave that animated the just-ended work-unit, so all the state is saved
177 * there, and will get passed along, inside the request handler, to the
178 * next work-unit for that procr.
179 */
180 void
181 VMS__suspend_procr( VirtProcr *callingPr )
182 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
183 void *coreLoopFramePtr;
185 //The request to master will cause this suspended virt procr to get
186 // scheduled again at some future point -- to resume, core loop jumps
187 // to the resume point (below), which causes restore of saved regs and
188 // "return" from this call.
189 callingPr->nextInstrPt = &&ResumePt;
191 //return ownership of the virt procr and sched slot to Master virt pr
192 callingPr->schedSlot->workIsDone = TRUE;
193 // coreIdx = callingPr->coreAnimatedBy;
195 stackPtrAddr = &(callingPr->stackPtr);
196 framePtrAddr = &(callingPr->framePtr);
198 jmpPt = callingPr->coreLoopStartPt;
199 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
200 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
202 //Save the virt procr's stack and frame ptrs, restore coreloop's frame
203 // ptr, then jump back to "start" of core loop
204 //Note, GCC compiles to assembly that saves esp and ebp in the stack
205 // frame -- so have to explicitly do assembly that saves to memory
206 asm volatile("movl %0, %%eax; \
207 movl %%esp, (%%eax); \
208 movl %1, %%eax; \
209 movl %%ebp, (%%eax); \
210 movl %2, %%eax; \
211 movl %3, %%esp; \
212 movl %4, %%ebp; \
213 jmp %%eax " \
214 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
215 /* inputs */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
216 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
217 ); //list everything as clobbered to force GCC to save all
218 // live vars that are in regs on stack before this
219 // assembly, so that stack pointer is correct, before jmp
221 ResumePt:
222 return;
223 }
227 /*This is equivalent to "jump back to core loop" -- it's mainly only used
228 * just after adding dissipate request to a processor -- so the semantic
229 * layer is the only place it will be seen and/or used.
230 *
231 *It does almost the same thing as suspend, except don't need to save the
232 * stack nor set the nextInstrPt
233 *
234 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
235 */
236 void
237 VMS__return_from_fn( VirtProcr *animatingPr )
238 {
239 VMS__suspend_procr( animatingPr );
240 }
243 /*Not sure yet the form going to put "dissipate" in, so this is the third
244 * possibility -- the semantic layer can just make a macro that looks like
245 * a call to its name, then expands to a call to this.
246 *
247 *As of June 30, 2010 this looks like the top choice..
248 *
249 *This adds a request to dissipate, then suspends the processor so that the
250 * request handler will receive the request. The request handler is what
251 * does the work of freeing memory and removing the processor from the
252 * semantic environment's data structures.
253 *The request handler also is what figures out when to shutdown the VMS
254 * system -- which causes all the core loop threads to die, and returns from
255 * the call that started up VMS to perform the work.
256 *
257 *This form is a bit misleading to understand if one is trying to figure out
258 * how VMS works -- it looks like a normal function call, but inside it
259 * sends a request to the request handler and suspends the processor, which
260 * jumps out of the VMS__dissipate_procr function, and out of all nestings
261 * above it, transferring the work of dissipating to the request handler,
262 * which then does the actual work -- causing the processor that animated
263 * the call of this function to disappear and the "hanging" state of this
264 * function to just poof into thin air -- the virtual processor's trace
265 * never returns from this call, but instead the virtual processor's trace
266 * gets suspended in this call and all the virt processor's state disap-
267 * pears -- making that suspend the last thing in the virt procr's trace.
268 */
269 void
270 VMS__dissipate_procr( VirtProcr *procrToDissipate )
271 { VMSReqst *req;
273 req = malloc( sizeof(VMSReqst) );
274 // req->virtProcrFrom = callingPr;
275 req->reqType = dissipate;
276 req->nextReqst = procrToDissipate->requests;
277 procrToDissipate->requests = req;
279 VMS__suspend_procr( procrToDissipate );
280 }
283 /*This inserts the semantic-layer's request data into standard VMS carrier
284 */
285 inline void
286 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
287 { VMSReqst *req;
289 req = malloc( sizeof(VMSReqst) );
290 // req->virtProcrFrom = callingPr;
291 req->reqType = semantic;
292 req->semReqData = semReqData;
293 req->nextReqst = callingPr->requests;
294 callingPr->requests = req;
295 }
298 /*This creates a request of type "dissipate" -- which will cause the virt
299 * processor's state and owned locations to be freed
300 */
301 inline void
302 VMS__send_dissipate_request( VirtProcr *procrToDissipate )
303 { VMSReqst *req;
305 req = malloc( sizeof(VMSReqst) );
306 // req->virtProcrFrom = callingPr;
307 req->reqType = dissipate;
308 req->nextReqst = procrToDissipate->requests;
309 procrToDissipate->requests = req;
310 }
313 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
314 // of a request -- IE call with both a virt procr and a fn-ptr to request
315 // freer (or maybe put request freer as a field in virt procr?)
316 void
317 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
318 { VMSReqst *req;
320 req = procrWithReq->requests;
321 procrWithReq->requests = procrWithReq->requests->nextReqst;
322 free( req );
323 }
325 /*This must be called by the request handler plugin -- it cannot be called
326 * from the semantic library "dissipate processor" function -- instead, the
327 * semantic layer has to generate a request for the plug-in to call this
328 * function.
329 *The reason is that this frees the virtual processor's stack -- which is
330 * still in use inside semantic library calls!
331 *
332 *This frees or recycles all the state owned by and comprising the animating
333 * virtual procr. It frees any state that was malloc'd by the VMS system
334 * itself, and asks the VMS system to dis-own any VMS__malloc'd locations.
335 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
336 * state, then that state gets freed (or sent to recycling) as a side-effect
337 * of dis-owning it.
338 */
339 void
340 VMS__free_procr_locs( VirtProcr *animatingPr )
341 {
342 //dis-own all locations owned by this processor, causing to be freed
343 // any locations that it is (was) sole owner of
344 //TODO: implement VMS__malloc system, including "give up ownership"
346 VMS__remove_and_free_top_request( animatingPr );
347 free( animatingPr->startOfStack );
349 //NOTE: animatingPr->semanticData should either have been allocated
350 // with VMS__malloc, or else freed in the request handler plug-in.
351 //NOTE: initialData was given to the processor, so should either have
352 // been alloc'd with VMS__malloc, or freed by the level above animPr.
353 //So, all that's left to free here is the VirtProcr struc itself
354 free( animatingPr );
355 }
358 /*The semantic layer figures out when the work is done ( perhaps by a call
359 * in the application to "work all done", or perhaps all the virtual
360 * processors have dissipated.. a.s.o. )
361 *
362 *The semantic layer is responsible for making sure all work has fully
363 * completed before using this to shutdown the VMS system.
364 *
365 *After the semantic layer has determined it wants to shut down, the
366 * next time the Master Loop calls the scheduler plug-in, the scheduler
367 * then calls this function and returns the virtual processor it gets back.
368 *
369 *When the shut-down processor runs, it first frees all locations malloc'd to
370 * the VMS system (that wasn't
371 * specified as return-locations). Then it creates one core-loop shut-down
372 * processor for each core loop and puts them all into the workQ. When a
373 * core loop animates a core loop shut-down processor, it causes exit-thread
374 * to run, and when all core loop threads have exited, then the "wait for
375 * work to finish" in the main thread is woken, and the function-call that
376 * started all the work returns.
377 *
378 *The function animated by this processor performs the shut-down work.
379 */
380 VirtProcr *
381 VMS__create_the_shutdown_procr()
382 {
383 return VMS__create_procr( &shutdownFn, NULL );
384 }
387 /*This is the function run by the special "shut-down" processor
388 *
389 *The _VMSMasterEnv is needed by this shut down function, so the "wait"
390 * function run in the main loop has to free it, and the thread-related
391 * locations (coreLoopThdParams a.s.o.).
392 *However, the semantic environment and all data malloc'd to VMS can be
393 * freed here.
394 *
395 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
396 * locations it needs -- they will be automatically freed by the standard
397 * "free all owned locations"
398 *
399 *Free any locations malloc'd to the VMS system (that weren't
400 * specified as return-locations).
401 *Then create one core-loop shut-down processor for each core loop and puts
402 * them all into the workQ.
403 */
404 void
405 shutdownFn( void *dummy, VirtProcr *animatingPr )
406 { int coreIdx;
407 VirtProcr *shutDownPr;
408 CASQueueStruc *workQ = _VMSWorkQ;
410 //free all the locations owned within the VMS system
411 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
413 //make the core loop shut-down processors and put them into the workQ
414 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
415 {
416 shutDownPr = VMS__create_procr( NULL, NULL );
417 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
418 writeCASQ( shutDownPr, workQ );
419 }
421 //This is an issue: the animating processor of this function may not
422 // get its request handled before all the cores have shutdown.
423 //TODO: after all the threads stop, clean out the MasterEnv, the
424 // SemanticEnv, and the workQ before returning.
425 VMS__send_dissipate_request( animatingPr );
426 VMS__suspend_procr( animatingPr ); //will never come back from this
427 }
431 inline TSCount getTSCount()
432 { unsigned int low, high;
433 TSCount out;
435 saveTimeStampCountInto( low, high );
436 out = high;
437 out = (out << 32) + low;
438 return out;
439 }