view VMS.c @ 108:3bc3b89630c7

perf counters
author engelhardt@cray1
date Tue, 26 Jul 2011 15:36:24 +0200
parents 97e26095c01f
children 659299627e70
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 "ProcrContext.h"
16 #include "Queue_impl/BlockingQueue.h"
17 #include "Histogram/Histogram.h"
19 #include <linux/perf_event.h>
20 #include <syscall.h>
21 #include <sys/prctl.h>
23 #define thdAttrs NULL
25 //===========================================================================
26 void
27 shutdownFn( void *dummy, VirtProcr *dummy2 );
29 SchedSlot **
30 create_sched_slots();
32 void
33 create_masterEnv();
35 void
36 create_the_coreLoop_OS_threads();
38 MallocProlog *
39 create_free_list();
41 void
42 endOSThreadFn( void *initData, VirtProcr *animatingPr );
44 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
45 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
47 //===========================================================================
49 /*Setup has two phases:
50 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
51 * the master virt procr into the work-queue, ready for first "call"
52 * 2) Semantic layer then does its own init, which creates the seed virt
53 * procr inside the semantic layer, ready to schedule it when
54 * asked by the first run of the masterLoop.
55 *
56 *This part is bit weird because VMS really wants to be "always there", and
57 * have applications attach and detach.. for now, this VMS is part of
58 * the app, so the VMS system starts up as part of running the app.
59 *
60 *The semantic layer is isolated from the VMS internals by making the
61 * semantic layer do setup to a state that it's ready with its
62 * initial virt procrs, ready to schedule them to slots when the masterLoop
63 * asks. Without this pattern, the semantic layer's setup would
64 * have to modify slots directly to assign the initial virt-procrs, and put
65 * them into the readyToAnimateQ itself, breaking the isolation completely.
66 *
67 *
68 *The semantic layer creates the initial virt procr(s), and adds its
69 * own environment to masterEnv, and fills in the pointers to
70 * the requestHandler and slaveScheduler plug-in functions
71 */
73 /*This allocates VMS data structures, populates the master VMSProc,
74 * and master environment, and returns the master environment to the semantic
75 * layer.
76 */
77 void
78 VMS__init()
79 {
80 create_masterEnv();
81 create_the_coreLoop_OS_threads();
82 }
84 #ifdef SEQUENTIAL
86 /*To initialize the sequential version, just don't create the threads
87 */
88 void
89 VMS__init_Seq()
90 {
91 create_masterEnv();
92 }
94 #endif
96 void
97 create_masterEnv()
98 { MasterEnv *masterEnv;
99 VMSQueueStruc **readyToAnimateQs;
100 int coreIdx;
101 VirtProcr **masterVPs;
102 SchedSlot ***allSchedSlots; //ptr to array of ptrs
105 //Make the master env, which holds everything else
106 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
108 //Very first thing put into the master env is the free-list, seeded
109 // with a massive initial chunk of memory.
110 //After this, all other mallocs are VMS__malloc.
111 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
114 //============================= MEASUREMENT STUFF ========================
115 #ifdef MEAS__TIME_MALLOC
116 _VMSMasterEnv->mallocTimeHist = makeFixedBinHistExt( 50, 0, 100,
117 "malloc_time_hist");
118 _VMSMasterEnv->freeTimeHist = makeFixedBinHistExt( 50, 0, 100,
119 "free_time_hist");
120 #endif
121 #ifdef MEAS__TIME_PLUGIN
122 _VMSMasterEnv->reqHdlrLowTimeHist = makeFixedBinHistExt( 50, 0, 10,
123 "plugin_low_time_hist");
124 _VMSMasterEnv->reqHdlrHighTimeHist = makeFixedBinHistExt( 50, 0, 100,
125 "plugin_high_time_hist");
126 #endif
127 //========================================================================
129 //===================== Only VMS__malloc after this ====================
130 masterEnv = (MasterEnv*)_VMSMasterEnv;
132 //Make a readyToAnimateQ for each core loop
133 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
134 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
136 //One array for each core, 3 in array, core's masterVP scheds all
137 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
139 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
140 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
141 {
142 readyToAnimateQs[ coreIdx ] = makeVMSQ();
144 //Q: should give masterVP core-specific info as its init data?
145 masterVPs[ coreIdx ] = VMS__create_procr( (VirtProcrFnPtr)&masterLoop, (void*)masterEnv );
146 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
147 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
148 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
149 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
150 }
151 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
152 _VMSMasterEnv->masterVPs = masterVPs;
153 _VMSMasterEnv->masterLock = UNLOCKED;
154 _VMSMasterEnv->allSchedSlots = allSchedSlots;
155 _VMSMasterEnv->workStealingLock = UNLOCKED;
158 //Aug 19, 2010: no longer need to place initial masterVP into queue
159 // because coreLoop now controls -- animates its masterVP when no work
162 //============================= MEASUREMENT STUFF ========================
163 #ifdef STATS__TURN_ON_PROBES
164 _VMSMasterEnv->dynIntervalProbesInfo =
165 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->intervalProbes), 200);
167 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
169 //put creation time directly into master env, for fast retrieval
170 struct timeval timeStamp;
171 gettimeofday( &(timeStamp), NULL);
172 _VMSMasterEnv->createPtInSecs =
173 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
174 #endif
175 #ifdef MEAS__TIME_MASTER_LOCK
176 _VMSMasterEnv->masterLockLowTimeHist = makeFixedBinHist( 50, 0, 2,
177 "master lock low time hist");
178 _VMSMasterEnv->masterLockHighTimeHist = makeFixedBinHist( 50, 0, 100,
179 "master lock high time hist");
180 #endif
182 MakeTheMeasHists();
184 #ifdef MEAS__PERF_COUNTER
185 printf("Creating HW counters...");
186 struct perf_event_attr hw_event;
187 hw_event.type = PERF_TYPE_HARDWARE;
188 hw_event.size = sizeof(struct perf_event_attr);
189 hw_event.disabled = 1;
190 hw_event.inherit = 1; /* children inherit it */
191 hw_event.pinned = 1; /* must always be on PMU */
192 hw_event.exclusive = 0; /* only group on PMU */
193 hw_event.exclude_user = 0; /* don't count user */
194 hw_event.exclude_kernel = 1; /* ditto kernel */
195 hw_event.exclude_hv = 1; /* ditto hypervisor */
196 hw_event.exclude_idle = 0; /* don't count when idle */
197 hw_event.mmap = 0; /* include mmap data */
198 hw_event.comm = 0; /* include comm data */
200 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
201 {
202 hw_event.config = PERF_COUNT_HW_CPU_CYCLES; //cycles
203 _VMSMasterEnv->cycles_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
204 0,//pid_t pid,
205 coreIdx,//int cpu,
206 -1,//int group_fd,
207 0//unsigned long flags
208 );
209 hw_event.config = PERF_COUNT_HW_INSTRUCTIONS; //instrs
210 _VMSMasterEnv->instrs_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
211 0,//pid_t pid,
212 coreIdx,//int cpu,
213 -1,//int group_fd,
214 0//unsigned long flags
215 );
216 }
217 prctl(PR_TASK_PERF_EVENTS_ENABLE);
218 #endif
220 //========================================================================
222 }
224 SchedSlot **
225 create_sched_slots()
226 { SchedSlot **schedSlots;
227 int i;
229 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
231 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
232 {
233 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
235 //Set state to mean "handling requests done, slot needs filling"
236 schedSlots[i]->workIsDone = FALSE;
237 schedSlots[i]->needsProcrAssigned = TRUE;
238 }
239 return schedSlots;
240 }
243 void
244 freeSchedSlots( SchedSlot **schedSlots )
245 { int i;
246 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
247 {
248 VMS__free( schedSlots[i] );
249 }
250 VMS__free( schedSlots );
251 }
254 void
255 create_the_coreLoop_OS_threads()
256 {
257 //========================================================================
258 // Create the Threads
259 int coreIdx, retCode;
261 //Need the threads to be created suspended, and wait for a signal
262 // before proceeding -- gives time after creating to initialize other
263 // stuff before the coreLoops set off.
264 _VMSMasterEnv->setupComplete = 0;
266 //Make the threads that animate the core loops
267 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
268 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
269 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
271 retCode =
272 pthread_create( &(coreLoopThdHandles[coreIdx]),
273 thdAttrs,
274 &coreLoop,
275 (void *)(coreLoopThdParams[coreIdx]) );
276 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
277 }
278 }
280 /*Semantic layer calls this when it want the system to start running..
281 *
282 *This starts the core loops running then waits for them to exit.
283 */
284 void
285 VMS__start_the_work_then_wait_until_done()
286 { int coreIdx;
287 //Start the core loops running
289 //tell the core loop threads that setup is complete
290 //get lock, to lock out any threads still starting up -- they'll see
291 // that setupComplete is true before entering while loop, and so never
292 // wait on the condition
293 pthread_mutex_lock( &suspendLock );
294 _VMSMasterEnv->setupComplete = 1;
295 pthread_mutex_unlock( &suspendLock );
296 pthread_cond_broadcast( &suspend_cond );
299 //wait for all to complete
300 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
301 {
302 pthread_join( coreLoopThdHandles[coreIdx], NULL );
303 }
305 //NOTE: do not clean up VMS env here -- semantic layer has to have
306 // a chance to clean up its environment first, then do a call to free
307 // the Master env and rest of VMS locations
308 }
310 #ifdef SEQUENTIAL
311 /*Only difference between version with an OS thread pinned to each core and
312 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
313 */
314 void
315 VMS__start_the_work_then_wait_until_done_Seq()
316 {
317 //Instead of un-suspending threads, just call the one and only
318 // core loop (sequential version), in the main thread.
319 coreLoop_Seq( NULL );
320 flushRegisters();
322 }
323 #endif
325 inline VirtProcr *
326 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
327 { VirtProcr *newPr;
328 void *stackLocs;
330 newPr = VMS__malloc( sizeof(VirtProcr) );
331 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
332 if( stackLocs == 0 )
333 { perror("VMS__malloc stack"); exit(1); }
335 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
336 }
338 /* "ext" designates that it's for use outside the VMS system -- should only
339 * be called from main thread or other thread -- never from code animated by
340 * a VMS virtual processor.
341 */
342 inline VirtProcr *
343 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
344 { VirtProcr *newPr;
345 char *stackLocs;
347 newPr = malloc( sizeof(VirtProcr) );
348 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
349 if( stackLocs == 0 )
350 { perror("malloc stack"); exit(1); }
352 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
353 }
356 /*Anticipating multi-tasking
357 */
358 void *
359 VMS__give_sem_env_for( VirtProcr *animPr )
360 {
361 return _VMSMasterEnv->semanticEnv;
362 }
363 //===========================================================================
364 /*there is a label inside this function -- save the addr of this label in
365 * the callingPr struc, as the pick-up point from which to start the next
366 * work-unit for that procr. If turns out have to save registers, then
367 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
368 * "done with work-unit" label. The procr struc is in the request in the
369 * slave that animated the just-ended work-unit, so all the state is saved
370 * there, and will get passed along, inside the request handler, to the
371 * next work-unit for that procr.
372 */
373 void
374 VMS__suspend_procr( VirtProcr *animatingPr )
375 {
377 //The request to master will cause this suspended virt procr to get
378 // scheduled again at some future point -- to resume, core loop jumps
379 // to the resume point (below), which causes restore of saved regs and
380 // "return" from this call.
381 //animatingPr->nextInstrPt = &&ResumePt;
383 //return ownership of the virt procr and sched slot to Master virt pr
384 animatingPr->schedSlot->workIsDone = TRUE;
386 //=========================== Measurement stuff ========================
387 #ifdef MEAS__TIME_STAMP_SUSP
388 //record time stamp: compare to time-stamp recorded below
389 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
390 #endif
391 //=======================================================================
393 switchToCoreLoop(animatingPr);
394 flushRegisters();
396 //=======================================================================
398 #ifdef MEAS__TIME_STAMP_SUSP
399 //NOTE: only take low part of count -- do sanity check when take diff
400 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
401 #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;
583 semReq = req->semReqData;
585 newProbe = VMS__malloc( sizeof(IntervalProbe) );
586 newProbe->nameStr = VMS__strDup( semReq->nameStr );
587 newProbe->hist = NULL;
588 newProbe->schedChoiceWasRecorded = FALSE;
590 //This runs in masterVP, so no race-condition worries
591 newProbe->probeID =
592 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
594 requestingPr->dataRetFromReq = newProbe;
596 (*resumePrFnPtr)( requestingPr, semEnv );
597 }
601 /*This must be called by the request handler plugin -- it cannot be called
602 * from the semantic library "dissipate processor" function -- instead, the
603 * semantic layer has to generate a request, and the plug-in calls this
604 * function.
605 *The reason is that this frees the virtual processor's stack -- which is
606 * still in use inside semantic library calls!
607 *
608 *This frees or recycles all the state owned by and comprising the VMS
609 * portion of the animating virtual procr. The request handler must first
610 * free any semantic data created for the processor that didn't use the
611 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
612 * system to disown any state that did use VMS_malloc, and then frees the
613 * statck and the processor-struct itself.
614 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
615 * state, then that state gets freed (or sent to recycling) as a side-effect
616 * of dis-owning it.
617 */
618 void
619 VMS__dissipate_procr( VirtProcr *animatingPr )
620 {
621 //dis-own all locations owned by this processor, causing to be freed
622 // any locations that it is (was) sole owner of
623 //TODO: implement VMS__malloc system, including "give up ownership"
626 //NOTE: initialData was given to the processor, so should either have
627 // been alloc'd with VMS__malloc, or freed by the level above animPr.
628 //So, all that's left to free here is the stack and the VirtProcr struc
629 // itself
630 //Note, should not stack-allocate initial data -- no guarantee, in
631 // general that creating processor will outlive ones it creates.
632 VMS__free( animatingPr->startOfStack );
633 VMS__free( animatingPr );
634 }
637 //TODO: look at architecting cleanest separation between request handler
638 // and master loop, for dissipate, create, shutdown, and other non-semantic
639 // requests. Issue is chain: one removes requests from AppVP, one dispatches
640 // on type of request, and one handles each type.. but some types require
641 // action from both request handler and master loop -- maybe just give the
642 // request handler calls like: VMS__handle_X_request_type
645 /*This is called by the semantic layer's request handler when it decides its
646 * time to shut down the VMS system. Calling this causes the core loop OS
647 * threads to exit, which unblocks the entry-point function that started up
648 * VMS, and allows it to grab the result and return to the original single-
649 * threaded application.
650 *
651 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
652 * and-wait function has to free a bunch of stuff after it detects the
653 * threads have all died: the masterEnv, the thread-related locations,
654 * masterVP any AppVPs that might still be allocated and sitting in the
655 * semantic environment, or have been orphaned in the _VMSWorkQ.
656 *
657 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
658 * locations it needs, and give ownership to masterVP. Then, they will be
659 * automatically freed.
660 *
661 *In here,create one core-loop shut-down processor for each core loop and put
662 * them all directly into the readyToAnimateQ.
663 *Note, this function can ONLY be called after the semantic environment no
664 * longer cares if AppVPs get animated after the point this is called. In
665 * other words, this can be used as an abort, or else it should only be
666 * called when all AppVPs have finished dissipate requests -- only at that
667 * point is it sure that all results have completed.
668 */
669 void
670 VMS__shutdown()
671 { int coreIdx;
672 VirtProcr *shutDownPr;
674 //create the shutdown processors, one for each core loop -- put them
675 // directly into the Q -- each core will die when gets one
676 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
677 { //Note, this is running in the master
678 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
679 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
680 }
682 }
685 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
686 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
687 *This function has the sole purpose of setting the stack and framePtr
688 * to the coreLoop's stack and framePtr.. it does that then jumps to the
689 * core loop's shutdown point -- might be able to just call Pthread_exit
690 * from here, but am going back to the pthread's stack and setting everything
691 * up just as if it never jumped out, before calling pthread_exit.
692 *The end-point of core loop will free the stack and so forth of the
693 * processor that animates this function, (this fn is transfering the
694 * animator of the AppVP that is in turn animating this function over
695 * to core loop function -- note that this slices out a level of virtual
696 * processors).
697 */
698 void
699 endOSThreadFn( void *initData, VirtProcr *animatingPr )
700 {
701 #ifdef SEQUENTIAL
702 asmTerminateCoreLoopSeq(animatingPr);
703 #else
704 asmTerminateCoreLoop(animatingPr);
705 #endif
706 }
709 /*This is called from the startup & shutdown
710 */
711 void
712 VMS__cleanup_at_end_of_shutdown()
713 {
714 //unused
715 //VMSQueueStruc **readyToAnimateQs;
716 //int coreIdx;
717 //VirtProcr **masterVPs;
718 //SchedSlot ***allSchedSlots; //ptr to array of ptrs
720 //Before getting rid of everything, print out any measurements made
721 forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&printHist );
722 forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&saveHistToFile);
723 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, &freeHistExt );
724 #ifdef MEAS__TIME_PLUGIN
725 printHist( _VMSMasterEnv->reqHdlrLowTimeHist );
726 saveHistToFile( _VMSMasterEnv->reqHdlrHighTimeHist );
727 printHist( _VMSMasterEnv->reqHdlrHighTimeHist );
728 saveHistToFile( _VMSMasterEnv->reqHdlrHighTimeHist );
729 freeHistExt( _VMSMasterEnv->reqHdlrLowTimeHist );
730 freeHistExt( _VMSMasterEnv->reqHdlrHighTimeHist );
731 #endif
732 #ifdef MEAS__TIME_MALLOC
733 printHist( _VMSMasterEnv->mallocTimeHist );
734 saveHistToFile( _VMSMasterEnv->mallocTimeHist );
735 printHist( _VMSMasterEnv->freeTimeHist );
736 saveHistToFile( _VMSMasterEnv->freeTimeHist );
737 freeHistExt( _VMSMasterEnv->mallocTimeHist );
738 freeHistExt( _VMSMasterEnv->freeTimeHist );
739 #endif
740 #ifdef MEAS__TIME_MASTER_LOCK
741 printHist( _VMSMasterEnv->masterLockLowTimeHist );
742 printHist( _VMSMasterEnv->masterLockHighTimeHist );
743 #endif
744 #ifdef MEAS__TIME_MASTER
745 printHist( _VMSMasterEnv->pluginTimeHist );
746 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
747 {
748 freeVMSQ( readyToAnimateQs[ coreIdx ] );
749 //master VPs were created external to VMS, so use external free
750 VMS__dissipate_procr( masterVPs[ coreIdx ] );
752 freeSchedSlots( allSchedSlots[ coreIdx ] );
753 }
754 #endif
755 #ifdef MEAS__TIME_STAMP_SUSP
756 printHist( _VMSMasterEnv->pluginTimeHist );
757 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
758 {
759 freeVMSQ( readyToAnimateQs[ coreIdx ] );
760 //master VPs were created external to VMS, so use external free
761 VMS__dissipate_procr( masterVPs[ coreIdx ] );
763 freeSchedSlots( allSchedSlots[ coreIdx ] );
764 }
765 #endif
767 //All the environment data has been allocated with VMS__malloc, so just
768 // free its internal big-chunk and all inside it disappear.
769 /*
770 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
771 masterVPs = _VMSMasterEnv->masterVPs;
772 allSchedSlots = _VMSMasterEnv->allSchedSlots;
774 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
775 {
776 freeVMSQ( readyToAnimateQs[ coreIdx ] );
777 //master VPs were created external to VMS, so use external free
778 VMS__dissipate_procr( masterVPs[ coreIdx ] );
780 freeSchedSlots( allSchedSlots[ coreIdx ] );
781 }
783 VMS__free( _VMSMasterEnv->readyToAnimateQs );
784 VMS__free( _VMSMasterEnv->masterVPs );
785 VMS__free( _VMSMasterEnv->allSchedSlots );
787 //============================= MEASUREMENT STUFF ========================
788 #ifdef STATS__TURN_ON_PROBES
789 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
790 #endif
791 //========================================================================
792 */
793 //These are the only two that use system free
794 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
795 free( (void *)_VMSMasterEnv );
796 }
799 //================================
802 /*Later, improve this -- for now, just exits the application after printing
803 * the error message.
804 */
805 void
806 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
807 {
808 printf("%s",msgStr);
809 fflush(stdin);
810 exit(1);
811 }