view VMS.c @ 73:d8f12351f7cc

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