view VMS.c @ 26:668278fa7a63

Sequential -- just starting to add sequential version
author Me
date Mon, 26 Jul 2010 15:25:53 -0700
parents c556193f7211
children 8b9e4c333fe6
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 #define thdAttrs NULL
17 //===========================================================================
18 void
19 shutdownFn( void *dummy, VirtProcr *dummy2 );
21 void
22 create_sched_slots( MasterEnv *masterEnv );
24 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
25 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
27 //===========================================================================
29 /*Setup has two phases:
30 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
31 * the master virt procr into the work-queue, ready for first "call"
32 * 2) Semantic layer then does its own init, which creates the seed virt
33 * procr inside the semantic layer, ready to schedule it when
34 * asked by the first run of the masterLoop.
35 *
36 *This part is bit weird because VMS really wants to be "always there", and
37 * have applications attach and detach.. for now, this VMS is part of
38 * the app, so the VMS system starts up as part of running the app.
39 *
40 *The semantic layer is isolated from the VMS internals by making the
41 * semantic layer do setup to a state that it's ready with its
42 * initial virt procrs, ready to schedule them to slots when the masterLoop
43 * asks. Without this pattern, the semantic layer's setup would
44 * have to modify slots directly to assign the initial virt-procrs, and put
45 * them into the workQ itself, breaking the isolation completely.
46 *
47 *
48 *The semantic layer creates the initial virt procr(s), and adds its
49 * own environment to masterEnv, and fills in the pointers to
50 * the requestHandler and slaveScheduler plug-in functions
51 */
53 /*This allocates VMS data structures, populates the master VMSProc,
54 * and master environment, and returns the master environment to the semantic
55 * layer.
56 */
57 void
58 VMS__init()
59 { MasterEnv *masterEnv;
60 VMSQueueStruc *workQ;
62 //Make the central work-queue
63 _VMSWorkQ = makeVMSQ();
64 workQ = _VMSWorkQ;
66 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
67 masterEnv = _VMSMasterEnv;
69 //create the master virtual processor
70 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
72 create_sched_slots( masterEnv );
74 masterEnv->stillRunning = FALSE;
75 masterEnv->numToPrecede = NUM_CORES;
77 //First core loop to start up gets this, which will schedule seed Pr
78 //TODO: debug: check address of masterVirtPr
79 writeVMSQ( masterEnv->masterVirtPr, workQ );
81 numProcrsCreated = 1;
83 //========================================================================
84 // Create the Threads
85 int coreIdx, retCode;
87 //Need the threads to be created suspended, and wait for a signal
88 // before proceeding -- gives time after creating to initialize other
89 // stuff before the coreLoops set off.
90 _VMSMasterEnv->setupComplete = 0;
92 //Make the threads that animate the core loops
93 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
94 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
95 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
97 retCode =
98 pthread_create( &(coreLoopThdHandles[coreIdx]),
99 thdAttrs,
100 &coreLoop,
101 (void *)(coreLoopThdParams[coreIdx]) );
102 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
103 }
104 }
106 void
107 create_sched_slots( MasterEnv *masterEnv )
108 { SchedSlot **schedSlots, **filledSlots;
109 int i;
111 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
112 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
113 masterEnv->schedSlots = schedSlots;
114 masterEnv->filledSlots = filledSlots;
116 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
117 {
118 schedSlots[i] = malloc( sizeof(SchedSlot) );
120 //Set state to mean "handling requests done, slot needs filling"
121 schedSlots[i]->workIsDone = FALSE;
122 schedSlots[i]->needsProcrAssigned = TRUE;
123 }
124 }
127 /*Semantic layer calls this when it want the system to start running..
128 *
129 *This starts the core loops running then waits for them to exit.
130 */
131 void
132 VMS__start_the_work_then_wait_until_done()
133 { int coreIdx;
134 //Start the core loops running
135 //===========================================================================
136 TSCount startCount, endCount;
137 unsigned long long count = 0, freq = 0;
138 double runTime;
140 startCount = getTSCount();
142 //tell the core loop threads that setup is complete
143 //get lock, to lock out any threads still starting up -- they'll see
144 // that setupComplete is true before entering while loop, and so never
145 // wait on the condition
146 pthread_mutex_lock( &suspendLock );
147 _VMSMasterEnv->setupComplete = 1;
148 pthread_mutex_unlock( &suspendLock );
149 pthread_cond_broadcast( &suspend_cond );
152 //wait for all to complete
153 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
154 {
155 pthread_join( coreLoopThdHandles[coreIdx], NULL );
156 }
158 //NOTE: do not clean up VMS env here -- semantic layer has to have
159 // a chance to clean up its environment first, then do a call to free
160 // the Master env and rest of VMS locations
163 endCount = getTSCount();
164 count = endCount - startCount;
166 runTime = (double)count / (double)TSCOUNT_FREQ;
168 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
169 }
173 /*Create stack, then create __cdecl structure on it and put initialData and
174 * pointer to the new structure instance into the parameter positions on
175 * the stack
176 *Then put function pointer into nextInstrPt -- the stack is setup in std
177 * call structure, so jumping to function ptr is same as a GCC generated
178 * function call
179 *No need to save registers on old stack frame, because there's no old
180 * animator state to return to --
181 *
182 */
183 VirtProcr *
184 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
185 { VirtProcr *newPr;
186 char *stackLocs, *stackPtr;
188 newPr = malloc( sizeof(VirtProcr) );
189 newPr->procrID = numProcrsCreated++;
190 newPr->nextInstrPt = fnPtr;
191 newPr->initialData = initialData;
193 //fnPtr takes two params -- void *initData & void *animProcr
194 //alloc stack locations, make stackPtr be the highest addr minus room
195 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
196 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
197 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
198 if(stackLocs == 0)
199 {perror("malloc stack"); exit(1);}
200 newPr->startOfStack = stackLocs;
201 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
202 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
203 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
204 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
205 newPr->stackPtr = stackPtr; //core loop will switch to this, then
206 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
208 return newPr;
209 }
212 /*there is a label inside this function -- save the addr of this label in
213 * the callingPr struc, as the pick-up point from which to start the next
214 * work-unit for that procr. If turns out have to save registers, then
215 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
216 * "done with work-unit" label. The procr struc is in the request in the
217 * slave that animated the just-ended work-unit, so all the state is saved
218 * there, and will get passed along, inside the request handler, to the
219 * next work-unit for that procr.
220 */
221 void
222 VMS__suspend_procr( VirtProcr *callingPr )
223 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
224 void *coreLoopFramePtr;
226 //The request to master will cause this suspended virt procr to get
227 // scheduled again at some future point -- to resume, core loop jumps
228 // to the resume point (below), which causes restore of saved regs and
229 // "return" from this call.
230 callingPr->nextInstrPt = &&ResumePt;
232 //return ownership of the virt procr and sched slot to Master virt pr
233 callingPr->schedSlot->workIsDone = TRUE;
234 // coreIdx = callingPr->coreAnimatedBy;
236 stackPtrAddr = &(callingPr->stackPtr);
237 framePtrAddr = &(callingPr->framePtr);
239 jmpPt = callingPr->coreLoopStartPt;
240 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
241 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
243 //Eclipse's compilation sequence complains -- so break into two
244 // separate in-line assembly pieces
245 //Save the virt procr's stack and frame ptrs,
246 asm volatile("movl %0, %%eax; \
247 movl %%esp, (%%eax); \
248 movl %1, %%eax; \
249 movl %%ebp, (%%eax) "\
250 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
251 /* inputs */ : \
252 /* clobber */ : "%eax" \
253 );
255 //restore coreloop's frame ptr, then jump back to "start" of core loop
256 //Note, GCC compiles to assembly that saves esp and ebp in the stack
257 // frame -- so have to explicitly do assembly that saves to memory
258 asm volatile("movl %0, %%eax; \
259 movl %1, %%esp; \
260 movl %2, %%ebp; \
261 jmp %%eax " \
262 /* outputs */ : \
263 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
264 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
265 ); //list everything as clobbered to force GCC to save all
266 // live vars that are in regs on stack before this
267 // assembly, so that stack pointer is correct, before jmp
269 ResumePt:
270 return;
271 }
275 /*This is equivalent to "jump back to core loop" -- it's mainly only used
276 * just after adding dissipate request to a processor -- so the semantic
277 * layer is the only place it will be seen and/or used.
278 *
279 *It does almost the same thing as suspend, except don't need to save the
280 * stack nor set the nextInstrPt
281 *
282 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
283 */
284 void
285 VMS__return_from_fn( VirtProcr *animatingPr )
286 {
287 VMS__suspend_procr( animatingPr );
288 }
291 /*Not sure yet the form going to put "dissipate" in, so this is the third
292 * possibility -- the semantic layer can just make a macro that looks like
293 * a call to its name, then expands to a call to this.
294 *
295 *As of June 30, 2010 this looks like the top choice..
296 *
297 *This adds a request to dissipate, then suspends the processor so that the
298 * request handler will receive the request. The request handler is what
299 * does the work of freeing memory and removing the processor from the
300 * semantic environment's data structures.
301 *The request handler also is what figures out when to shutdown the VMS
302 * system -- which causes all the core loop threads to die, and returns from
303 * the call that started up VMS to perform the work.
304 *
305 *This form is a bit misleading to understand if one is trying to figure out
306 * how VMS works -- it looks like a normal function call, but inside it
307 * sends a request to the request handler and suspends the processor, which
308 * jumps out of the VMS__dissipate_procr function, and out of all nestings
309 * above it, transferring the work of dissipating to the request handler,
310 * which then does the actual work -- causing the processor that animated
311 * the call of this function to disappear and the "hanging" state of this
312 * function to just poof into thin air -- the virtual processor's trace
313 * never returns from this call, but instead the virtual processor's trace
314 * gets suspended in this call and all the virt processor's state disap-
315 * pears -- making that suspend the last thing in the virt procr's trace.
316 */
317 void
318 VMS__dissipate_procr( VirtProcr *procrToDissipate )
319 { VMSReqst *req;
321 req = malloc( sizeof(VMSReqst) );
322 // req->virtProcrFrom = callingPr;
323 req->reqType = dissipate;
324 req->nextReqst = procrToDissipate->requests;
325 procrToDissipate->requests = req;
327 VMS__suspend_procr( procrToDissipate );
328 }
331 /*This inserts the semantic-layer's request data into standard VMS carrier
332 */
333 inline void
334 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
335 { VMSReqst *req;
337 req = malloc( sizeof(VMSReqst) );
338 // req->virtProcrFrom = callingPr;
339 req->reqType = semantic;
340 req->semReqData = semReqData;
341 req->nextReqst = callingPr->requests;
342 callingPr->requests = req;
343 }
347 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
348 // of a request -- IE call with both a virt procr and a fn-ptr to request
349 // freer (or maybe put request freer as a field in virt procr?)
350 void
351 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
352 { VMSReqst *req;
354 req = procrWithReq->requests;
355 procrWithReq->requests = procrWithReq->requests->nextReqst;
356 free( req );
357 }
360 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
361 // of a request -- IE call with both a virt procr and a fn-ptr to request
362 // freer (also maybe put sem request freer as a field in virt procr?)
363 //VMSHW relies right now on this only freeing VMS layer of request -- the
364 // semantic portion of request is alloc'd and freed by request handler
365 void
366 VMS__free_request( VMSReqst *req )
367 {
368 free( req );
369 }
371 VMSReqst *
372 VMS__take_top_request_from( VirtProcr *procrWithReq )
373 { VMSReqst *req;
375 req = procrWithReq->requests;
376 if( req == NULL ) return req;
378 procrWithReq->requests = procrWithReq->requests->nextReqst;
379 return req;
380 }
382 inline int
383 VMS__isSemanticReqst( VMSReqst *req )
384 {
385 return ( req->reqType == semantic );
386 }
389 inline void *
390 VMS__take_sem_reqst_from( VMSReqst *req )
391 {
392 return req->semReqData;
393 }
395 inline int
396 VMS__isDissipateReqst( VMSReqst *req )
397 {
398 return ( req->reqType == dissipate );
399 }
401 inline int
402 VMS__isCreateReqst( VMSReqst *req )
403 {
404 return ( req->reqType == regCreated );
405 }
407 void
408 VMS__send_register_new_procr_request(VirtProcr *newPr, VirtProcr *reqstingPr)
409 { VMSReqst *req;
411 req = malloc( sizeof(VMSReqst) );
412 req->reqType = regCreated;
413 req->semReqData = newPr;
414 req->nextReqst = reqstingPr->requests;
415 reqstingPr->requests = req;
417 VMS__suspend_procr( reqstingPr );
418 }
421 /*The semantic layer figures out when the work is done ( perhaps by a call
422 * in the application to "work all done", or perhaps all the virtual
423 * processors have dissipated.. a.s.o. )
424 *
425 *The semantic layer is responsible for making sure all work has fully
426 * completed before using this to shutdown the VMS system.
427 *
428 *After the semantic layer has determined it wants to shut down, the
429 * next time the Master Loop calls the scheduler plug-in, the scheduler
430 * then calls this function and returns the virtual processor it gets back.
431 *
432 *When the shut-down processor runs, it first frees all locations malloc'd to
433 * the VMS system (that wasn't
434 * specified as return-locations). Then it creates one core-loop shut-down
435 * processor for each core loop and puts them all into the workQ. When a
436 * core loop animates a core loop shut-down processor, it causes exit-thread
437 * to run, and when all core loop threads have exited, then the "wait for
438 * work to finish" in the main thread is woken, and the function-call that
439 * started all the work returns.
440 *
441 *The function animated by this processor performs the shut-down work.
442 */
443 VirtProcr *
444 VMS__create_the_shutdown_procr()
445 {
446 return VMS__create_procr( &shutdownFn, NULL );
447 }
450 /*This must be called by the request handler plugin -- it cannot be called
451 * from the semantic library "dissipate processor" function -- instead, the
452 * semantic layer has to generate a request for the plug-in to call this
453 * function.
454 *The reason is that this frees the virtual processor's stack -- which is
455 * still in use inside semantic library calls!
456 *
457 *This frees or recycles all the state owned by and comprising the VMS
458 * portion of the animating virtual procr. The request handler must first
459 * free any semantic data created for the processor that didn't use the
460 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
461 * system to disown any state that did use VMS_malloc, and then frees the
462 * statck and the processor-struct itself.
463 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
464 * state, then that state gets freed (or sent to recycling) as a side-effect
465 * of dis-owning it.
466 */
467 void
468 VMS__free_procr_locs( VirtProcr *animatingPr )
469 {
470 //dis-own all locations owned by this processor, causing to be freed
471 // any locations that it is (was) sole owner of
472 //TODO: implement VMS__malloc system, including "give up ownership"
474 //The dissipate request might still be attached, so remove and free it
475 VMS__remove_and_free_top_request( animatingPr );
476 free( animatingPr->startOfStack );
478 //NOTE: initialData was given to the processor, so should either have
479 // been alloc'd with VMS__malloc, or freed by the level above animPr.
480 //So, all that's left to free here is the stack and the VirtProcr struc
481 // itself
482 free( animatingPr->startOfStack );
483 free( animatingPr );
484 }
488 /*This is the function run by the special "shut-down" processor
489 *
490 *The _VMSMasterEnv is needed by this shut down function, so the "wait"
491 * function run in the main loop has to free it, and the thread-related
492 * locations (coreLoopThdParams a.s.o.).
493 *However, the semantic environment and all data malloc'd to VMS can be
494 * freed here.
495 *
496 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
497 * locations it needs -- they will be automatically freed by the standard
498 * "free all owned locations"
499 *
500 *Free any locations malloc'd to the VMS system (that weren't
501 * specified as return-locations).
502 *Then create one core-loop shut-down processor for each core loop and puts
503 * them all into the workQ.
504 */
505 void
506 shutdownFn( void *dummy, VirtProcr *animatingPr )
507 { int coreIdx;
508 VirtProcr *shutDownPr;
509 VMSQueueStruc *workQ = _VMSWorkQ;
511 //free all the locations owned within the VMS system
512 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
514 //make the core loop shut-down processors and put them into the workQ
515 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
516 {
517 shutDownPr = VMS__create_procr( NULL, NULL );
518 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
519 writeVMSQ( shutDownPr, workQ );
520 }
522 //This is an issue: the animating processor of this function may not
523 // get its request handled before all the cores have shutdown.
524 //TODO: after all the threads stop, clean out the MasterEnv, the
525 // SemanticEnv, and the workQ before returning.
526 VMS__dissipate_procr( animatingPr ); //will never come back from this
527 }
530 /*This has to free anything allocated during VMS_init, and any other alloc'd
531 * locations that might be left over.
532 */
533 void
534 VMS__shutdown()
535 { int i;
537 free( _VMSWorkQ );
538 free( _VMSMasterEnv->filledSlots );
539 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
540 {
541 free( _VMSMasterEnv->schedSlots[i] );
542 }
544 free( _VMSMasterEnv->schedSlots);
545 VMS__free_procr_locs( _VMSMasterEnv->masterVirtPr );
547 free( _VMSMasterEnv );
548 }
551 //===========================================================================
553 inline TSCount getTSCount()
554 { unsigned int low, high;
555 TSCount out;
557 saveTimeStampCountInto( low, high );
558 out = high;
559 out = (out << 32) + low;
560 return out;
561 }