view VMS.c @ 28:8b9e4c333fe6

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