Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 75:f6990e1ba998
new sequential version
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 02 Jun 2011 13:55:51 +0200 |
| parents | 5ff1631c26ed |
| children | 9ddbb071142d |
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 );
279 flushRegisters();
281 }
282 #endif
284 /*Create stack, then create __cdecl structure on it and put initialData and
285 * pointer to the new structure instance into the parameter positions on
286 * the stack
287 *Then put function pointer into nextInstrPt -- the stack is setup in std
288 * call structure, so jumping to function ptr is same as a GCC generated
289 * function call
290 *No need to save registers on old stack frame, because there's no old
291 * animator state to return to --
292 *
293 */
294 inline VirtProcr *
295 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
296 void *initialData, char *stackLocs )
297 {
298 char *stackPtr;
300 newPr->startOfStack = stackLocs;
301 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
302 newPr->nextInstrPt = fnPtr;
303 newPr->initialData = initialData;
304 newPr->requests = NULL;
305 newPr->schedSlot = NULL;
307 //fnPtr takes two params -- void *initData & void *animProcr
308 //alloc stack locations, make stackPtr be the highest addr minus room
309 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
310 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
311 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
313 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
314 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
315 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
316 newPr->stackPtr = stackPtr; //core loop will switch to this, then
317 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
319 //============================= MEASUREMENT STUFF ========================
320 #ifdef STATS__TURN_ON_PROBES
321 struct timeval timeStamp;
322 gettimeofday( &(timeStamp), NULL);
323 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
324 _VMSMasterEnv->createPtInSecs;
325 #endif
326 //========================================================================
328 return newPr;
329 }
331 inline VirtProcr *
332 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
333 { VirtProcr *newPr;
334 char *stackLocs;
336 newPr = VMS__malloc( sizeof(VirtProcr) );
337 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
338 if( stackLocs == 0 )
339 { perror("VMS__malloc stack"); exit(1); }
341 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
342 }
344 /* "ext" designates that it's for use outside the VMS system -- should only
345 * be called from main thread or other thread -- never from code animated by
346 * a VMS virtual processor.
347 */
348 inline VirtProcr *
349 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
350 { VirtProcr *newPr;
351 char *stackLocs;
353 newPr = malloc( sizeof(VirtProcr) );
354 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
355 if( stackLocs == 0 )
356 { perror("malloc stack"); exit(1); }
358 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
359 }
362 /*Anticipating multi-tasking
363 */
364 void *
365 VMS__give_sem_env_for( VirtProcr *animPr )
366 {
367 return _VMSMasterEnv->semanticEnv;
368 }
369 //===========================================================================
370 /*there is a label inside this function -- save the addr of this label in
371 * the callingPr struc, as the pick-up point from which to start the next
372 * work-unit for that procr. If turns out have to save registers, then
373 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
374 * "done with work-unit" label. The procr struc is in the request in the
375 * slave that animated the just-ended work-unit, so all the state is saved
376 * there, and will get passed along, inside the request handler, to the
377 * next work-unit for that procr.
378 */
379 void
380 VMS__suspend_procr( VirtProcr *animatingPr )
381 {
383 //The request to master will cause this suspended virt procr to get
384 // scheduled again at some future point -- to resume, core loop jumps
385 // to the resume point (below), which causes restore of saved regs and
386 // "return" from this call.
387 //animatingPr->nextInstrPt = &&ResumePt;
389 //return ownership of the virt procr and sched slot to Master virt pr
390 animatingPr->schedSlot->workIsDone = TRUE;
392 //=========================== Measurement stuff ========================
393 #ifdef MEAS__TIME_STAMP_SUSP
394 //record time stamp: compare to time-stamp recorded below
395 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
396 #endif
397 //=======================================================================
399 switchToCoreLoop(animatingPr);
400 flushRegisters();
402 //=======================================================================
404 #ifdef MEAS__TIME_STAMP_SUSP
405 //NOTE: only take low part of count -- do sanity check when take diff
406 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
407 #endif
409 return;
410 }
414 /*For this implementation of VMS, it may not make much sense to have the
415 * system of requests for creating a new processor done this way.. but over
416 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
417 * distributed-memory, and so on, this gives VMS implementation a chance to
418 * do stuff before suspend, in the AppVP, and in the Master before the plugin
419 * is called, as well as in the lang-lib before this is called, and in the
420 * plugin. So, this gives both VMS and language implementations a chance to
421 * intercept at various points and do order-dependent stuff.
422 *Having a standard VMSNewPrReqData struc allows the language to create and
423 * free the struc, while VMS knows how to get the newPr if it wants it, and
424 * it lets the lang have lang-specific data related to creation transported
425 * to the plugin.
426 */
427 void
428 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
429 { VMSReqst req;
431 req.reqType = createReq;
432 req.semReqData = semReqData;
433 req.nextReqst = reqstingPr->requests;
434 reqstingPr->requests = &req;
436 VMS__suspend_procr( reqstingPr );
437 }
440 /*
441 *This adds a request to dissipate, then suspends the processor so that the
442 * request handler will receive the request. The request handler is what
443 * does the work of freeing memory and removing the processor from the
444 * semantic environment's data structures.
445 *The request handler also is what figures out when to shutdown the VMS
446 * system -- which causes all the core loop threads to die, and returns from
447 * the call that started up VMS to perform the work.
448 *
449 *This form is a bit misleading to understand if one is trying to figure out
450 * how VMS works -- it looks like a normal function call, but inside it
451 * sends a request to the request handler and suspends the processor, which
452 * jumps out of the VMS__dissipate_procr function, and out of all nestings
453 * above it, transferring the work of dissipating to the request handler,
454 * which then does the actual work -- causing the processor that animated
455 * the call of this function to disappear and the "hanging" state of this
456 * function to just poof into thin air -- the virtual processor's trace
457 * never returns from this call, but instead the virtual processor's trace
458 * gets suspended in this call and all the virt processor's state disap-
459 * pears -- making that suspend the last thing in the virt procr's trace.
460 */
461 void
462 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
463 { VMSReqst req;
465 req.reqType = dissipate;
466 req.nextReqst = procrToDissipate->requests;
467 procrToDissipate->requests = &req;
469 VMS__suspend_procr( procrToDissipate );
470 }
473 /* "ext" designates that it's for use outside the VMS system -- should only
474 * be called from main thread or other thread -- never from code animated by
475 * a VMS virtual processor.
476 *
477 *Use this version to dissipate VPs created outside the VMS system.
478 */
479 void
480 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
481 {
482 //NOTE: initialData was given to the processor, so should either have
483 // been alloc'd with VMS__malloc, or freed by the level above animPr.
484 //So, all that's left to free here is the stack and the VirtProcr struc
485 // itself
486 //Note, should not stack-allocate initial data -- no guarantee, in
487 // general that creating processor will outlive ones it creates.
488 free( procrToDissipate->startOfStack );
489 free( procrToDissipate );
490 }
494 /*This call's name indicates that request is malloc'd -- so req handler
495 * has to free any extra requests tacked on before a send, using this.
496 *
497 * This inserts the semantic-layer's request data into standard VMS carrier
498 * request data-struct that is mallocd. The sem request doesn't need to
499 * be malloc'd if this is called inside the same call chain before the
500 * send of the last request is called.
501 *
502 *The request handler has to call VMS__free_VMSReq for any of these
503 */
504 inline void
505 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
506 VirtProcr *callingPr )
507 { VMSReqst *req;
509 req = VMS__malloc( sizeof(VMSReqst) );
510 req->reqType = semantic;
511 req->semReqData = semReqData;
512 req->nextReqst = callingPr->requests;
513 callingPr->requests = req;
514 }
516 /*This inserts the semantic-layer's request data into standard VMS carrier
517 * request data-struct is allocated on stack of this call & ptr to it sent
518 * to plugin
519 *Then it does suspend, to cause request to be sent.
520 */
521 inline void
522 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
523 { VMSReqst req;
525 req.reqType = semantic;
526 req.semReqData = semReqData;
527 req.nextReqst = callingPr->requests;
528 callingPr->requests = &req;
530 VMS__suspend_procr( callingPr );
531 }
534 inline void
535 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
536 { VMSReqst req;
538 req.reqType = VMSSemantic;
539 req.semReqData = semReqData;
540 req.nextReqst = callingPr->requests; //gab any other preceeding
541 callingPr->requests = &req;
543 VMS__suspend_procr( callingPr );
544 }
547 /*
548 */
549 VMSReqst *
550 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
551 { VMSReqst *req;
553 req = procrWithReq->requests;
554 if( req == NULL ) return NULL;
556 procrWithReq->requests = procrWithReq->requests->nextReqst;
557 return req;
558 }
561 inline void *
562 VMS__take_sem_reqst_from( VMSReqst *req )
563 {
564 return req->semReqData;
565 }
569 /* This is for OS requests and VMS infrastructure requests, such as to create
570 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
571 * language -- but it's also a semantic thing that's triggered from and used
572 * in the application.. so it crosses abstractions.. so, need some special
573 * pattern here for handling such requests.
574 * Doing this just like it were a second language sharing VMS-core.
575 *
576 * This is called from the language's request handler when it sees a request
577 * of type VMSSemReq
578 *
579 * TODO: Later change this, to give probes their own separate plugin & have
580 * VMS-core steer the request to appropriate plugin
581 * Do the same for OS calls -- look later at it..
582 */
583 void inline
584 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
585 ResumePrFnPtr resumePrFnPtr )
586 { VMSSemReq *semReq;
587 IntervalProbe *newProbe;
588 int32 nameLen;
590 semReq = req->semReqData;
592 newProbe = VMS__malloc( sizeof(IntervalProbe) );
593 newProbe->nameStr = VMS__strDup( semReq->nameStr );
594 newProbe->hist = NULL;
595 newProbe->schedChoiceWasRecorded = FALSE;
597 //This runs in masterVP, so no race-condition worries
598 newProbe->probeID =
599 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
601 requestingPr->dataRetFromReq = newProbe;
603 (*resumePrFnPtr)( requestingPr, semEnv );
604 }
608 /*This must be called by the request handler plugin -- it cannot be called
609 * from the semantic library "dissipate processor" function -- instead, the
610 * semantic layer has to generate a request, and the plug-in calls this
611 * function.
612 *The reason is that this frees the virtual processor's stack -- which is
613 * still in use inside semantic library calls!
614 *
615 *This frees or recycles all the state owned by and comprising the VMS
616 * portion of the animating virtual procr. The request handler must first
617 * free any semantic data created for the processor that didn't use the
618 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
619 * system to disown any state that did use VMS_malloc, and then frees the
620 * statck and the processor-struct itself.
621 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
622 * state, then that state gets freed (or sent to recycling) as a side-effect
623 * of dis-owning it.
624 */
625 void
626 VMS__dissipate_procr( VirtProcr *animatingPr )
627 {
628 //dis-own all locations owned by this processor, causing to be freed
629 // any locations that it is (was) sole owner of
630 //TODO: implement VMS__malloc system, including "give up ownership"
633 //NOTE: initialData was given to the processor, so should either have
634 // been alloc'd with VMS__malloc, or freed by the level above animPr.
635 //So, all that's left to free here is the stack and the VirtProcr struc
636 // itself
637 //Note, should not stack-allocate initial data -- no guarantee, in
638 // general that creating processor will outlive ones it creates.
639 VMS__free( animatingPr->startOfStack );
640 VMS__free( animatingPr );
641 }
644 //TODO: look at architecting cleanest separation between request handler
645 // and master loop, for dissipate, create, shutdown, and other non-semantic
646 // requests. Issue is chain: one removes requests from AppVP, one dispatches
647 // on type of request, and one handles each type.. but some types require
648 // action from both request handler and master loop -- maybe just give the
649 // request handler calls like: VMS__handle_X_request_type
652 /*This is called by the semantic layer's request handler when it decides its
653 * time to shut down the VMS system. Calling this causes the core loop OS
654 * threads to exit, which unblocks the entry-point function that started up
655 * VMS, and allows it to grab the result and return to the original single-
656 * threaded application.
657 *
658 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
659 * and-wait function has to free a bunch of stuff after it detects the
660 * threads have all died: the masterEnv, the thread-related locations,
661 * masterVP any AppVPs that might still be allocated and sitting in the
662 * semantic environment, or have been orphaned in the _VMSWorkQ.
663 *
664 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
665 * locations it needs, and give ownership to masterVP. Then, they will be
666 * automatically freed.
667 *
668 *In here,create one core-loop shut-down processor for each core loop and put
669 * them all directly into the readyToAnimateQ.
670 *Note, this function can ONLY be called after the semantic environment no
671 * longer cares if AppVPs get animated after the point this is called. In
672 * other words, this can be used as an abort, or else it should only be
673 * called when all AppVPs have finished dissipate requests -- only at that
674 * point is it sure that all results have completed.
675 */
676 void
677 VMS__shutdown()
678 { int coreIdx;
679 VirtProcr *shutDownPr;
681 //create the shutdown processors, one for each core loop -- put them
682 // directly into the Q -- each core will die when gets one
683 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
684 { //Note, this is running in the master
685 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
686 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
687 }
689 }
692 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
693 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
694 *This function has the sole purpose of setting the stack and framePtr
695 * to the coreLoop's stack and framePtr.. it does that then jumps to the
696 * core loop's shutdown point -- might be able to just call Pthread_exit
697 * from here, but am going back to the pthread's stack and setting everything
698 * up just as if it never jumped out, before calling pthread_exit.
699 *The end-point of core loop will free the stack and so forth of the
700 * processor that animates this function, (this fn is transfering the
701 * animator of the AppVP that is in turn animating this function over
702 * to core loop function -- note that this slices out a level of virtual
703 * processors).
704 */
705 void
706 endOSThreadFn( void *initData, VirtProcr *animatingPr )
707 {
708 #ifdef SEQUENTIAL
709 asmTerminateCoreLoopSeq(animatingPr);
710 #else
711 asmTerminateCoreLoop(animatingPr);
712 #endif
713 }
716 /*This is called from the startup & shutdown
717 */
718 void
719 VMS__cleanup_at_end_of_shutdown()
720 {
721 VMSQueueStruc **readyToAnimateQs;
722 int coreIdx;
723 VirtProcr **masterVPs;
724 SchedSlot ***allSchedSlots; //ptr to array of ptrs
726 //Before getting rid of everything, print out any measurements made
727 forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&printHist );
728 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, &freeHistExt );
729 #ifdef MEAS__TIME_PLUGIN
730 printHist( _VMSMasterEnv->reqHdlrLowTimeHist );
731 printHist( _VMSMasterEnv->reqHdlrHighTimeHist );
732 freeHistExt( _VMSMasterEnv->reqHdlrLowTimeHist );
733 freeHistExt( _VMSMasterEnv->reqHdlrHighTimeHist );
734 #endif
735 #ifdef MEAS__TIME_MALLOC
736 printHist( _VMSMasterEnv->mallocTimeHist );
737 printHist( _VMSMasterEnv->freeTimeHist );
738 freeHistExt( _VMSMasterEnv->mallocTimeHist );
739 freeHistExt( _VMSMasterEnv->freeTimeHist );
740 #endif
741 #ifdef MEAS__TIME_MASTER_LOCK
742 printHist( _VMSMasterEnv->masterLockLowTimeHist );
743 printHist( _VMSMasterEnv->masterLockHighTimeHist );
744 #endif
745 #ifdef MEAS__TIME_MASTER
746 printHist( _VMSMasterEnv->pluginTimeHist );
747 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
748 {
749 freeVMSQ( readyToAnimateQs[ coreIdx ] );
750 //master VPs were created external to VMS, so use external free
751 VMS__dissipate_procr( masterVPs[ coreIdx ] );
753 freeSchedSlots( allSchedSlots[ coreIdx ] );
754 }
755 #endif
756 #ifdef MEAS__TIME_STAMP_SUSP
757 printHist( _VMSMasterEnv->pluginTimeHist );
758 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
759 {
760 freeVMSQ( readyToAnimateQs[ coreIdx ] );
761 //master VPs were created external to VMS, so use external free
762 VMS__dissipate_procr( masterVPs[ coreIdx ] );
764 freeSchedSlots( allSchedSlots[ coreIdx ] );
765 }
766 #endif
768 //All the environment data has been allocated with VMS__malloc, so just
769 // free its internal big-chunk and all inside it disappear.
770 /*
771 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
772 masterVPs = _VMSMasterEnv->masterVPs;
773 allSchedSlots = _VMSMasterEnv->allSchedSlots;
775 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
776 {
777 freeVMSQ( readyToAnimateQs[ coreIdx ] );
778 //master VPs were created external to VMS, so use external free
779 VMS__dissipate_procr( masterVPs[ coreIdx ] );
781 freeSchedSlots( allSchedSlots[ coreIdx ] );
782 }
784 VMS__free( _VMSMasterEnv->readyToAnimateQs );
785 VMS__free( _VMSMasterEnv->masterVPs );
786 VMS__free( _VMSMasterEnv->allSchedSlots );
788 //============================= MEASUREMENT STUFF ========================
789 #ifdef STATS__TURN_ON_PROBES
790 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
791 #endif
792 //========================================================================
793 */
794 //These are the only two that use system free
795 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
796 free( (void *)_VMSMasterEnv );
797 }
800 //================================
803 /*Later, improve this -- for now, just exits the application after printing
804 * the error message.
805 */
806 void
807 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
808 {
809 printf("%s",msgStr);
810 fflush(stdin);
811 exit(1);
812 }
