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