Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 76:9ddbb071142d
make hardware independent and port to 64bit
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 16 Jun 2011 14:41:15 +0200 |
| parents | f6990e1ba998 |
| children | fe5ec83f1baf |
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <malloc.h>
11 #include <inttypes.h>
12 #include <sys/time.h>
14 #include "VMS.h"
15 #include "SwitchAnimators.h"
16 #include "Queue_impl/BlockingQueue.h"
17 #include "Histogram/Histogram.h"
20 #define thdAttrs NULL
22 //===========================================================================
23 void
24 shutdownFn( void *dummy, VirtProcr *dummy2 );
26 SchedSlot **
27 create_sched_slots();
29 void
30 create_masterEnv();
32 void
33 create_the_coreLoop_OS_threads();
35 MallocProlog *
36 create_free_list();
38 void
39 endOSThreadFn( void *initData, VirtProcr *animatingPr );
41 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
42 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
44 //===========================================================================
46 /*Setup has two phases:
47 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
48 * the master virt procr into the work-queue, ready for first "call"
49 * 2) Semantic layer then does its own init, which creates the seed virt
50 * procr inside the semantic layer, ready to schedule it when
51 * asked by the first run of the masterLoop.
52 *
53 *This part is bit weird because VMS really wants to be "always there", and
54 * have applications attach and detach.. for now, this VMS is part of
55 * the app, so the VMS system starts up as part of running the app.
56 *
57 *The semantic layer is isolated from the VMS internals by making the
58 * semantic layer do setup to a state that it's ready with its
59 * initial virt procrs, ready to schedule them to slots when the masterLoop
60 * asks. Without this pattern, the semantic layer's setup would
61 * have to modify slots directly to assign the initial virt-procrs, and put
62 * them into the readyToAnimateQ itself, breaking the isolation completely.
63 *
64 *
65 *The semantic layer creates the initial virt procr(s), and adds its
66 * own environment to masterEnv, and fills in the pointers to
67 * the requestHandler and slaveScheduler plug-in functions
68 */
70 /*This allocates VMS data structures, populates the master VMSProc,
71 * and master environment, and returns the master environment to the semantic
72 * layer.
73 */
74 void
75 VMS__init()
76 {
77 create_masterEnv();
78 create_the_coreLoop_OS_threads();
79 }
81 #ifdef SEQUENTIAL
83 /*To initialize the sequential version, just don't create the threads
84 */
85 void
86 VMS__init_Seq()
87 {
88 create_masterEnv();
89 }
91 #endif
93 void
94 create_masterEnv()
95 { MasterEnv *masterEnv;
96 VMSQueueStruc **readyToAnimateQs;
97 int coreIdx;
98 VirtProcr **masterVPs;
99 SchedSlot ***allSchedSlots; //ptr to array of ptrs
102 //Make the master env, which holds everything else
103 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
105 //Very first thing put into the master env is the free-list, seeded
106 // with a massive initial chunk of memory.
107 //After this, all other mallocs are VMS__malloc.
108 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
111 //============================= MEASUREMENT STUFF ========================
112 #ifdef MEAS__TIME_MALLOC
113 _VMSMasterEnv->mallocTimeHist = makeFixedBinHistExt( 50, 0, 100,
114 "malloc time hist");
115 _VMSMasterEnv->freeTimeHist = makeFixedBinHistExt( 50, 0, 100,
116 "free time hist");
117 #endif
118 #ifdef MEAS__TIME_PLUGIN
119 _VMSMasterEnv->reqHdlrLowTimeHist = makeFixedBinHistExt( 50, 0, 10,
120 "plugin low time hist");
121 _VMSMasterEnv->reqHdlrHighTimeHist = makeFixedBinHistExt( 50, 0, 100,
122 "plugin high time hist");
123 #endif
124 //========================================================================
126 //===================== Only VMS__malloc after this ====================
127 masterEnv = (MasterEnv*)_VMSMasterEnv;
129 //Make a readyToAnimateQ for each core loop
130 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
131 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
133 //One array for each core, 3 in array, core's masterVP scheds all
134 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
136 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
137 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
138 {
139 readyToAnimateQs[ coreIdx ] = makeVMSQ();
141 //Q: should give masterVP core-specific info as its init data?
142 masterVPs[ coreIdx ] = VMS__create_procr( (VirtProcrFnPtr)&masterLoop, (void*)masterEnv );
143 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
144 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
145 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
146 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
147 }
148 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
149 _VMSMasterEnv->masterVPs = masterVPs;
150 _VMSMasterEnv->masterLock = UNLOCKED;
151 _VMSMasterEnv->allSchedSlots = allSchedSlots;
152 _VMSMasterEnv->workStealingLock = UNLOCKED;
155 //Aug 19, 2010: no longer need to place initial masterVP into queue
156 // because coreLoop now controls -- animates its masterVP when no work
159 //============================= MEASUREMENT STUFF ========================
160 #ifdef STATS__TURN_ON_PROBES
161 _VMSMasterEnv->dynIntervalProbesInfo =
162 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->intervalProbes), 200);
164 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
166 //put creation time directly into master env, for fast retrieval
167 struct timeval timeStamp;
168 gettimeofday( &(timeStamp), NULL);
169 _VMSMasterEnv->createPtInSecs =
170 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
171 #endif
172 #ifdef MEAS__TIME_MASTER_LOCK
173 _VMSMasterEnv->masterLockLowTimeHist = makeFixedBinHist( 50, 0, 2,
174 "master lock low time hist");
175 _VMSMasterEnv->masterLockHighTimeHist = makeFixedBinHist( 50, 0, 100,
176 "master lock high time hist");
177 #endif
179 MakeTheMeasHists();
180 //========================================================================
182 }
184 SchedSlot **
185 create_sched_slots()
186 { SchedSlot **schedSlots;
187 int i;
189 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
191 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
192 {
193 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
195 //Set state to mean "handling requests done, slot needs filling"
196 schedSlots[i]->workIsDone = FALSE;
197 schedSlots[i]->needsProcrAssigned = TRUE;
198 }
199 return schedSlots;
200 }
203 void
204 freeSchedSlots( SchedSlot **schedSlots )
205 { int i;
206 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
207 {
208 VMS__free( schedSlots[i] );
209 }
210 VMS__free( schedSlots );
211 }
214 void
215 create_the_coreLoop_OS_threads()
216 {
217 //========================================================================
218 // Create the Threads
219 int coreIdx, retCode;
221 //Need the threads to be created suspended, and wait for a signal
222 // before proceeding -- gives time after creating to initialize other
223 // stuff before the coreLoops set off.
224 _VMSMasterEnv->setupComplete = 0;
226 //Make the threads that animate the core loops
227 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
228 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
229 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
231 retCode =
232 pthread_create( &(coreLoopThdHandles[coreIdx]),
233 thdAttrs,
234 &coreLoop,
235 (void *)(coreLoopThdParams[coreIdx]) );
236 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
237 }
238 }
240 /*Semantic layer calls this when it want the system to start running..
241 *
242 *This starts the core loops running then waits for them to exit.
243 */
244 void
245 VMS__start_the_work_then_wait_until_done()
246 { int coreIdx;
247 //Start the core loops running
249 //tell the core loop threads that setup is complete
250 //get lock, to lock out any threads still starting up -- they'll see
251 // that setupComplete is true before entering while loop, and so never
252 // wait on the condition
253 pthread_mutex_lock( &suspendLock );
254 _VMSMasterEnv->setupComplete = 1;
255 pthread_mutex_unlock( &suspendLock );
256 pthread_cond_broadcast( &suspend_cond );
259 //wait for all to complete
260 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
261 {
262 pthread_join( coreLoopThdHandles[coreIdx], NULL );
263 }
265 //NOTE: do not clean up VMS env here -- semantic layer has to have
266 // a chance to clean up its environment first, then do a call to free
267 // the Master env and rest of VMS locations
268 }
270 #ifdef SEQUENTIAL
271 /*Only difference between version with an OS thread pinned to each core and
272 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
273 */
274 void
275 VMS__start_the_work_then_wait_until_done_Seq()
276 {
277 //Instead of un-suspending threads, just call the one and only
278 // core loop (sequential version), in the main thread.
279 coreLoop_Seq( NULL );
280 flushRegisters();
282 }
283 #endif
285 /*Create stack, then create __cdecl structure on it and put initialData and
286 * pointer to the new structure instance into the parameter positions on
287 * the stack
288 *Then put function pointer into nextInstrPt -- the stack is setup in std
289 * call structure, so jumping to function ptr is same as a GCC generated
290 * function call
291 *No need to save registers on old stack frame, because there's no old
292 * animator state to return to --
293 *
294 */
295 inline VirtProcr *
296 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
297 void *initialData, void *stackLocs )
298 {
299 void *stackPtr;
301 newPr->startOfStack = stackLocs;
302 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
303 newPr->initialData = initialData;
304 newPr->requests = NULL;
305 newPr->schedSlot = NULL;
307 /*
308 * Hardware dependent part
309 */
310 //instead of calling the function directly, call a wrapper function to fetch
311 //arguments from stack
312 newPr->nextInstrPt = (VirtProcrFnPtr)&startVirtProcrFn;
314 //fnPtr takes two params -- void *initData & void *animProcr
315 //alloc stack locations, make stackPtr be the highest addr minus room
316 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
317 // by stackPtr, initData at stackPtr + 8 bytes, animatingPr just above
318 stackPtr = ( (void *)stackLocs + VIRT_PROCR_STACK_SIZE - 4*sizeof(void*));
320 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
321 *((VirtProcr**)stackPtr + 2 ) = newPr; //rightmost param
322 *((void**)stackPtr + 1 ) = initialData; //next param to left
323 *((void**)stackPtr) = (void*)fnPtr;
325 /*
326 * end of Hardware dependent part
327 */
329 newPr->stackPtr = stackPtr; //core loop will switch to this, then
330 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
332 //============================= MEASUREMENT STUFF ========================
333 #ifdef STATS__TURN_ON_PROBES
334 struct timeval timeStamp;
335 gettimeofday( &(timeStamp), NULL);
336 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
337 _VMSMasterEnv->createPtInSecs;
338 #endif
339 //========================================================================
341 return newPr;
342 }
344 inline VirtProcr *
345 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
346 { VirtProcr *newPr;
347 void *stackLocs;
349 newPr = VMS__malloc( sizeof(VirtProcr) );
350 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
351 if( stackLocs == 0 )
352 { perror("VMS__malloc stack"); exit(1); }
354 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
355 }
357 /* "ext" designates that it's for use outside the VMS system -- should only
358 * be called from main thread or other thread -- never from code animated by
359 * a VMS virtual processor.
360 */
361 inline VirtProcr *
362 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
363 { VirtProcr *newPr;
364 char *stackLocs;
366 newPr = malloc( sizeof(VirtProcr) );
367 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
368 if( stackLocs == 0 )
369 { perror("malloc stack"); exit(1); }
371 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
372 }
375 /*Anticipating multi-tasking
376 */
377 void *
378 VMS__give_sem_env_for( VirtProcr *animPr )
379 {
380 return _VMSMasterEnv->semanticEnv;
381 }
382 //===========================================================================
383 /*there is a label inside this function -- save the addr of this label in
384 * the callingPr struc, as the pick-up point from which to start the next
385 * work-unit for that procr. If turns out have to save registers, then
386 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
387 * "done with work-unit" label. The procr struc is in the request in the
388 * slave that animated the just-ended work-unit, so all the state is saved
389 * there, and will get passed along, inside the request handler, to the
390 * next work-unit for that procr.
391 */
392 void
393 VMS__suspend_procr( VirtProcr *animatingPr )
394 {
396 //The request to master will cause this suspended virt procr to get
397 // scheduled again at some future point -- to resume, core loop jumps
398 // to the resume point (below), which causes restore of saved regs and
399 // "return" from this call.
400 //animatingPr->nextInstrPt = &&ResumePt;
402 //return ownership of the virt procr and sched slot to Master virt pr
403 animatingPr->schedSlot->workIsDone = TRUE;
405 //=========================== Measurement stuff ========================
406 #ifdef MEAS__TIME_STAMP_SUSP
407 //record time stamp: compare to time-stamp recorded below
408 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
409 #endif
410 //=======================================================================
412 switchToCoreLoop(animatingPr);
413 flushRegisters();
415 //=======================================================================
417 #ifdef MEAS__TIME_STAMP_SUSP
418 //NOTE: only take low part of count -- do sanity check when take diff
419 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
420 #endif
422 return;
423 }
427 /*For this implementation of VMS, it may not make much sense to have the
428 * system of requests for creating a new processor done this way.. but over
429 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
430 * distributed-memory, and so on, this gives VMS implementation a chance to
431 * do stuff before suspend, in the AppVP, and in the Master before the plugin
432 * is called, as well as in the lang-lib before this is called, and in the
433 * plugin. So, this gives both VMS and language implementations a chance to
434 * intercept at various points and do order-dependent stuff.
435 *Having a standard VMSNewPrReqData struc allows the language to create and
436 * free the struc, while VMS knows how to get the newPr if it wants it, and
437 * it lets the lang have lang-specific data related to creation transported
438 * to the plugin.
439 */
440 void
441 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
442 { VMSReqst req;
444 req.reqType = createReq;
445 req.semReqData = semReqData;
446 req.nextReqst = reqstingPr->requests;
447 reqstingPr->requests = &req;
449 VMS__suspend_procr( reqstingPr );
450 }
453 /*
454 *This adds a request to dissipate, then suspends the processor so that the
455 * request handler will receive the request. The request handler is what
456 * does the work of freeing memory and removing the processor from the
457 * semantic environment's data structures.
458 *The request handler also is what figures out when to shutdown the VMS
459 * system -- which causes all the core loop threads to die, and returns from
460 * the call that started up VMS to perform the work.
461 *
462 *This form is a bit misleading to understand if one is trying to figure out
463 * how VMS works -- it looks like a normal function call, but inside it
464 * sends a request to the request handler and suspends the processor, which
465 * jumps out of the VMS__dissipate_procr function, and out of all nestings
466 * above it, transferring the work of dissipating to the request handler,
467 * which then does the actual work -- causing the processor that animated
468 * the call of this function to disappear and the "hanging" state of this
469 * function to just poof into thin air -- the virtual processor's trace
470 * never returns from this call, but instead the virtual processor's trace
471 * gets suspended in this call and all the virt processor's state disap-
472 * pears -- making that suspend the last thing in the virt procr's trace.
473 */
474 void
475 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
476 { VMSReqst req;
478 req.reqType = dissipate;
479 req.nextReqst = procrToDissipate->requests;
480 procrToDissipate->requests = &req;
482 VMS__suspend_procr( procrToDissipate );
483 }
486 /* "ext" designates that it's for use outside the VMS system -- should only
487 * be called from main thread or other thread -- never from code animated by
488 * a VMS virtual processor.
489 *
490 *Use this version to dissipate VPs created outside the VMS system.
491 */
492 void
493 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
494 {
495 //NOTE: initialData was given to the processor, so should either have
496 // been alloc'd with VMS__malloc, or freed by the level above animPr.
497 //So, all that's left to free here is the stack and the VirtProcr struc
498 // itself
499 //Note, should not stack-allocate initial data -- no guarantee, in
500 // general that creating processor will outlive ones it creates.
501 free( procrToDissipate->startOfStack );
502 free( procrToDissipate );
503 }
507 /*This call's name indicates that request is malloc'd -- so req handler
508 * has to free any extra requests tacked on before a send, using this.
509 *
510 * This inserts the semantic-layer's request data into standard VMS carrier
511 * request data-struct that is mallocd. The sem request doesn't need to
512 * be malloc'd if this is called inside the same call chain before the
513 * send of the last request is called.
514 *
515 *The request handler has to call VMS__free_VMSReq for any of these
516 */
517 inline void
518 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
519 VirtProcr *callingPr )
520 { VMSReqst *req;
522 req = VMS__malloc( sizeof(VMSReqst) );
523 req->reqType = semantic;
524 req->semReqData = semReqData;
525 req->nextReqst = callingPr->requests;
526 callingPr->requests = req;
527 }
529 /*This inserts the semantic-layer's request data into standard VMS carrier
530 * request data-struct is allocated on stack of this call & ptr to it sent
531 * to plugin
532 *Then it does suspend, to cause request to be sent.
533 */
534 inline void
535 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
536 { VMSReqst req;
538 req.reqType = semantic;
539 req.semReqData = semReqData;
540 req.nextReqst = callingPr->requests;
541 callingPr->requests = &req;
543 VMS__suspend_procr( callingPr );
544 }
547 inline void
548 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
549 { VMSReqst req;
551 req.reqType = VMSSemantic;
552 req.semReqData = semReqData;
553 req.nextReqst = callingPr->requests; //gab any other preceeding
554 callingPr->requests = &req;
556 VMS__suspend_procr( callingPr );
557 }
560 /*
561 */
562 VMSReqst *
563 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
564 { VMSReqst *req;
566 req = procrWithReq->requests;
567 if( req == NULL ) return NULL;
569 procrWithReq->requests = procrWithReq->requests->nextReqst;
570 return req;
571 }
574 inline void *
575 VMS__take_sem_reqst_from( VMSReqst *req )
576 {
577 return req->semReqData;
578 }
582 /* This is for OS requests and VMS infrastructure requests, such as to create
583 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
584 * language -- but it's also a semantic thing that's triggered from and used
585 * in the application.. so it crosses abstractions.. so, need some special
586 * pattern here for handling such requests.
587 * Doing this just like it were a second language sharing VMS-core.
588 *
589 * This is called from the language's request handler when it sees a request
590 * of type VMSSemReq
591 *
592 * TODO: Later change this, to give probes their own separate plugin & have
593 * VMS-core steer the request to appropriate plugin
594 * Do the same for OS calls -- look later at it..
595 */
596 void inline
597 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
598 ResumePrFnPtr resumePrFnPtr )
599 { VMSSemReq *semReq;
600 IntervalProbe *newProbe;
602 semReq = req->semReqData;
604 newProbe = VMS__malloc( sizeof(IntervalProbe) );
605 newProbe->nameStr = VMS__strDup( semReq->nameStr );
606 newProbe->hist = NULL;
607 newProbe->schedChoiceWasRecorded = FALSE;
609 //This runs in masterVP, so no race-condition worries
610 newProbe->probeID =
611 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
613 requestingPr->dataRetFromReq = newProbe;
615 (*resumePrFnPtr)( requestingPr, semEnv );
616 }
620 /*This must be called by the request handler plugin -- it cannot be called
621 * from the semantic library "dissipate processor" function -- instead, the
622 * semantic layer has to generate a request, and the plug-in calls this
623 * function.
624 *The reason is that this frees the virtual processor's stack -- which is
625 * still in use inside semantic library calls!
626 *
627 *This frees or recycles all the state owned by and comprising the VMS
628 * portion of the animating virtual procr. The request handler must first
629 * free any semantic data created for the processor that didn't use the
630 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
631 * system to disown any state that did use VMS_malloc, and then frees the
632 * statck and the processor-struct itself.
633 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
634 * state, then that state gets freed (or sent to recycling) as a side-effect
635 * of dis-owning it.
636 */
637 void
638 VMS__dissipate_procr( VirtProcr *animatingPr )
639 {
640 //dis-own all locations owned by this processor, causing to be freed
641 // any locations that it is (was) sole owner of
642 //TODO: implement VMS__malloc system, including "give up ownership"
645 //NOTE: initialData was given to the processor, so should either have
646 // been alloc'd with VMS__malloc, or freed by the level above animPr.
647 //So, all that's left to free here is the stack and the VirtProcr struc
648 // itself
649 //Note, should not stack-allocate initial data -- no guarantee, in
650 // general that creating processor will outlive ones it creates.
651 VMS__free( animatingPr->startOfStack );
652 VMS__free( animatingPr );
653 }
656 //TODO: look at architecting cleanest separation between request handler
657 // and master loop, for dissipate, create, shutdown, and other non-semantic
658 // requests. Issue is chain: one removes requests from AppVP, one dispatches
659 // on type of request, and one handles each type.. but some types require
660 // action from both request handler and master loop -- maybe just give the
661 // request handler calls like: VMS__handle_X_request_type
664 /*This is called by the semantic layer's request handler when it decides its
665 * time to shut down the VMS system. Calling this causes the core loop OS
666 * threads to exit, which unblocks the entry-point function that started up
667 * VMS, and allows it to grab the result and return to the original single-
668 * threaded application.
669 *
670 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
671 * and-wait function has to free a bunch of stuff after it detects the
672 * threads have all died: the masterEnv, the thread-related locations,
673 * masterVP any AppVPs that might still be allocated and sitting in the
674 * semantic environment, or have been orphaned in the _VMSWorkQ.
675 *
676 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
677 * locations it needs, and give ownership to masterVP. Then, they will be
678 * automatically freed.
679 *
680 *In here,create one core-loop shut-down processor for each core loop and put
681 * them all directly into the readyToAnimateQ.
682 *Note, this function can ONLY be called after the semantic environment no
683 * longer cares if AppVPs get animated after the point this is called. In
684 * other words, this can be used as an abort, or else it should only be
685 * called when all AppVPs have finished dissipate requests -- only at that
686 * point is it sure that all results have completed.
687 */
688 void
689 VMS__shutdown()
690 { int coreIdx;
691 VirtProcr *shutDownPr;
693 //create the shutdown processors, one for each core loop -- put them
694 // directly into the Q -- each core will die when gets one
695 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
696 { //Note, this is running in the master
697 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
698 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
699 }
701 }
704 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
705 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
706 *This function has the sole purpose of setting the stack and framePtr
707 * to the coreLoop's stack and framePtr.. it does that then jumps to the
708 * core loop's shutdown point -- might be able to just call Pthread_exit
709 * from here, but am going back to the pthread's stack and setting everything
710 * up just as if it never jumped out, before calling pthread_exit.
711 *The end-point of core loop will free the stack and so forth of the
712 * processor that animates this function, (this fn is transfering the
713 * animator of the AppVP that is in turn animating this function over
714 * to core loop function -- note that this slices out a level of virtual
715 * processors).
716 */
717 void
718 endOSThreadFn( void *initData, VirtProcr *animatingPr )
719 {
720 #ifdef SEQUENTIAL
721 asmTerminateCoreLoopSeq(animatingPr);
722 #else
723 asmTerminateCoreLoop(animatingPr);
724 #endif
725 }
728 /*This is called from the startup & shutdown
729 */
730 void
731 VMS__cleanup_at_end_of_shutdown()
732 {
733 VMSQueueStruc **readyToAnimateQs;
734 int coreIdx;
735 VirtProcr **masterVPs;
736 SchedSlot ***allSchedSlots; //ptr to array of ptrs
738 //Before getting rid of everything, print out any measurements made
739 forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&printHist );
740 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, &freeHistExt );
741 #ifdef MEAS__TIME_PLUGIN
742 printHist( _VMSMasterEnv->reqHdlrLowTimeHist );
743 printHist( _VMSMasterEnv->reqHdlrHighTimeHist );
744 freeHistExt( _VMSMasterEnv->reqHdlrLowTimeHist );
745 freeHistExt( _VMSMasterEnv->reqHdlrHighTimeHist );
746 #endif
747 #ifdef MEAS__TIME_MALLOC
748 printHist( _VMSMasterEnv->mallocTimeHist );
749 printHist( _VMSMasterEnv->freeTimeHist );
750 freeHistExt( _VMSMasterEnv->mallocTimeHist );
751 freeHistExt( _VMSMasterEnv->freeTimeHist );
752 #endif
753 #ifdef MEAS__TIME_MASTER_LOCK
754 printHist( _VMSMasterEnv->masterLockLowTimeHist );
755 printHist( _VMSMasterEnv->masterLockHighTimeHist );
756 #endif
757 #ifdef MEAS__TIME_MASTER
758 printHist( _VMSMasterEnv->pluginTimeHist );
759 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
760 {
761 freeVMSQ( readyToAnimateQs[ coreIdx ] );
762 //master VPs were created external to VMS, so use external free
763 VMS__dissipate_procr( masterVPs[ coreIdx ] );
765 freeSchedSlots( allSchedSlots[ coreIdx ] );
766 }
767 #endif
768 #ifdef MEAS__TIME_STAMP_SUSP
769 printHist( _VMSMasterEnv->pluginTimeHist );
770 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
771 {
772 freeVMSQ( readyToAnimateQs[ coreIdx ] );
773 //master VPs were created external to VMS, so use external free
774 VMS__dissipate_procr( masterVPs[ coreIdx ] );
776 freeSchedSlots( allSchedSlots[ coreIdx ] );
777 }
778 #endif
780 //All the environment data has been allocated with VMS__malloc, so just
781 // free its internal big-chunk and all inside it disappear.
782 /*
783 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
784 masterVPs = _VMSMasterEnv->masterVPs;
785 allSchedSlots = _VMSMasterEnv->allSchedSlots;
787 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
788 {
789 freeVMSQ( readyToAnimateQs[ coreIdx ] );
790 //master VPs were created external to VMS, so use external free
791 VMS__dissipate_procr( masterVPs[ coreIdx ] );
793 freeSchedSlots( allSchedSlots[ coreIdx ] );
794 }
796 VMS__free( _VMSMasterEnv->readyToAnimateQs );
797 VMS__free( _VMSMasterEnv->masterVPs );
798 VMS__free( _VMSMasterEnv->allSchedSlots );
800 //============================= MEASUREMENT STUFF ========================
801 #ifdef STATS__TURN_ON_PROBES
802 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
803 #endif
804 //========================================================================
805 */
806 //These are the only two that use system free
807 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
808 free( (void *)_VMSMasterEnv );
809 }
812 //================================
815 /*Later, improve this -- for now, just exits the application after printing
816 * the error message.
817 */
818 void
819 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
820 {
821 printf("%s",msgStr);
822 fflush(stdin);
823 exit(1);
824 }
