view VMS.c @ 129:ce02441b77cf

dependency tracking
author Nina Engelhardt
date Mon, 29 Aug 2011 19:12:06 +0200
parents 73fc5aafbe45
children 5475f90c248a
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 <unistd.h>
20 #include <fcntl.h>
21 #include <linux/types.h>
22 #include <linux/perf_event.h>
23 #include <errno.h>
24 #include <sys/syscall.h>
25 #include <linux/prctl.h>
28 #define thdAttrs NULL
30 //===========================================================================
31 void
32 shutdownFn( void *dummy, VirtProcr *dummy2 );
34 SchedSlot **
35 create_sched_slots();
37 void
38 create_masterEnv();
40 void
41 create_the_coreLoop_OS_threads();
43 MallocProlog *
44 create_free_list();
46 void
47 endOSThreadFn( void *initData, VirtProcr *animatingPr );
49 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
50 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
52 //===========================================================================
54 /*Setup has two phases:
55 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
56 * the master virt procr into the work-queue, ready for first "call"
57 * 2) Semantic layer then does its own init, which creates the seed virt
58 * procr inside the semantic layer, ready to schedule it when
59 * asked by the first run of the masterLoop.
60 *
61 *This part is bit weird because VMS really wants to be "always there", and
62 * have applications attach and detach.. for now, this VMS is part of
63 * the app, so the VMS system starts up as part of running the app.
64 *
65 *The semantic layer is isolated from the VMS internals by making the
66 * semantic layer do setup to a state that it's ready with its
67 * initial virt procrs, ready to schedule them to slots when the masterLoop
68 * asks. Without this pattern, the semantic layer's setup would
69 * have to modify slots directly to assign the initial virt-procrs, and put
70 * them into the readyToAnimateQ itself, breaking the isolation completely.
71 *
72 *
73 *The semantic layer creates the initial virt procr(s), and adds its
74 * own environment to masterEnv, and fills in the pointers to
75 * the requestHandler and slaveScheduler plug-in functions
76 */
78 /*This allocates VMS data structures, populates the master VMSProc,
79 * and master environment, and returns the master environment to the semantic
80 * layer.
81 */
82 void
83 VMS__init()
84 {
85 create_masterEnv();
86 create_the_coreLoop_OS_threads();
87 }
89 #ifdef SEQUENTIAL
91 /*To initialize the sequential version, just don't create the threads
92 */
93 void
94 VMS__init_Seq()
95 {
96 create_masterEnv();
97 }
99 #endif
101 void
102 create_masterEnv()
103 { MasterEnv *masterEnv;
104 VMSQueueStruc **readyToAnimateQs;
105 int coreIdx;
106 VirtProcr **masterVPs;
107 SchedSlot ***allSchedSlots; //ptr to array of ptrs
110 //Make the master env, which holds everything else
111 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
113 //Very first thing put into the master env is the free-list, seeded
114 // with a massive initial chunk of memory.
115 //After this, all other mallocs are VMS__malloc.
116 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
119 //============================= MEASUREMENT STUFF ========================
120 #ifdef MEAS__TIME_MALLOC
121 _VMSMasterEnv->mallocTimeHist = makeFixedBinHistExt( 100, 0, 100,
122 "malloc_time_hist");
123 _VMSMasterEnv->freeTimeHist = makeFixedBinHistExt( 80, 0, 100,
124 "free_time_hist");
125 #endif
126 #ifdef MEAS__TIME_PLUGIN
127 _VMSMasterEnv->reqHdlrLowTimeHist = makeFixedBinHistExt( 1000, 0, 100,
128 "plugin_low_time_hist");
129 _VMSMasterEnv->reqHdlrHighTimeHist = makeFixedBinHistExt( 1000, 0, 100,
130 "plugin_high_time_hist");
131 #endif
132 //========================================================================
134 //===================== Only VMS__malloc after this ====================
135 masterEnv = (MasterEnv*)_VMSMasterEnv;
137 //Make a readyToAnimateQ for each core loop
138 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
139 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
141 //One array for each core, 3 in array, core's masterVP scheds all
142 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
144 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
145 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
146 {
147 readyToAnimateQs[ coreIdx ] = makeVMSQ();
149 //Q: should give masterVP core-specific info as its init data?
150 masterVPs[ coreIdx ] = VMS__create_procr( (VirtProcrFnPtr)&masterLoop, (void*)masterEnv );
151 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
152 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
153 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
154 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
155 }
156 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
157 _VMSMasterEnv->masterVPs = masterVPs;
158 _VMSMasterEnv->masterLock = UNLOCKED;
159 _VMSMasterEnv->allSchedSlots = allSchedSlots;
160 _VMSMasterEnv->workStealingLock = UNLOCKED;
163 //Aug 19, 2010: no longer need to place initial masterVP into queue
164 // because coreLoop now controls -- animates its masterVP when no work
167 //============================= MEASUREMENT STUFF ========================
168 #ifdef STATS__TURN_ON_PROBES
169 _VMSMasterEnv->dynIntervalProbesInfo =
170 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->intervalProbes), 200);
172 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
174 //put creation time directly into master env, for fast retrieval
175 struct timeval timeStamp;
176 gettimeofday( &(timeStamp), NULL);
177 _VMSMasterEnv->createPtInSecs =
178 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
179 #endif
180 #ifdef MEAS__TIME_MASTER_LOCK
181 _VMSMasterEnv->masterLockLowTimeHist = makeFixedBinHist( 50, 0, 2,
182 "master lock low time hist");
183 _VMSMasterEnv->masterLockHighTimeHist = makeFixedBinHist( 50, 0, 100,
184 "master lock high time hist");
185 #endif
187 MakeTheMeasHists();
189 #ifdef DETECT_DEPENDENCIES
190 _VMSMasterEnv->dependencies = VMS__malloc(10*sizeof(void*));
191 _VMSMasterEnv->dependenciesInfo = makePrivDynArrayInfoFrom((void***)&(_VMSMasterEnv->dependencies),10);
192 #endif
194 #ifdef MEAS__PERF_COUNTERS
195 //printf("Creating HW counters...");
196 FILE* output;
197 int n;
198 char filename[255];
199 for(n=0;n<255;n++)
200 {
201 sprintf(filename, "./counters/Counters.%d.csv",n);
202 output = fopen(filename,"r");
203 if(output)
204 {
205 fclose(output);
206 }else{
207 break;
208 }
209 }
210 printf("Saving Counter measurements to File: %s ...\n", filename);
211 output = fopen(filename,"w+");
212 _VMSMasterEnv->counteroutput = output;
214 struct perf_event_attr hw_event;
215 memset(&hw_event,0,sizeof(hw_event));
216 hw_event.type = PERF_TYPE_HARDWARE;
217 hw_event.size = sizeof(hw_event);
218 hw_event.disabled = 1;
219 hw_event.freq = 0;
220 hw_event.inherit = 1; /* children inherit it */
221 hw_event.pinned = 1; /* must always be on PMU */
222 hw_event.exclusive = 0; /* only group on PMU */
223 hw_event.exclude_user = 0; /* don't count user */
224 hw_event.exclude_kernel = 1; /* ditto kernel */
225 hw_event.exclude_hv = 1; /* ditto hypervisor */
226 hw_event.exclude_idle = 0; /* don't count when idle */
227 hw_event.mmap = 0; /* include mmap data */
228 hw_event.comm = 0; /* include comm data */
231 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
232 {
233 hw_event.config = 0x0000000000000000; //cycles
234 _VMSMasterEnv->cycles_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
235 0,//pid_t pid,
236 -1,//int cpu,
237 -1,//int group_fd,
238 0//unsigned long flags
239 );
240 if (_VMSMasterEnv->cycles_counter_fd[coreIdx]<0){
241 fprintf(stderr,"On core %d: ",coreIdx);
242 perror("Failed to open cycles counter");
243 }
244 hw_event.config = 0x0000000000000001; //instrs
245 _VMSMasterEnv->instrs_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
246 0,//pid_t pid,
247 -1,//int cpu,
248 -1,//int group_fd,
249 0//unsigned long flags
250 );
251 if (_VMSMasterEnv->instrs_counter_fd[coreIdx]<0){
252 fprintf(stderr,"On core %d: ",coreIdx);
253 perror("Failed to open instrs counter");
254 }
255 }
256 prctl(PR_TASK_PERF_EVENTS_ENABLE);
257 uint64 tmpc,tmpi;
258 saveCyclesAndInstrs(0,tmpc,tmpi);
259 printf("Start: cycles = %lu, instrs = %lu\n",tmpc,tmpi);
260 #endif
262 //========================================================================
264 }
266 SchedSlot **
267 create_sched_slots()
268 { SchedSlot **schedSlots;
269 int i;
271 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
273 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
274 {
275 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
277 //Set state to mean "handling requests done, slot needs filling"
278 schedSlots[i]->workIsDone = FALSE;
279 schedSlots[i]->needsProcrAssigned = TRUE;
280 }
281 return schedSlots;
282 }
285 void
286 freeSchedSlots( SchedSlot **schedSlots )
287 { int i;
288 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
289 {
290 VMS__free( schedSlots[i] );
291 }
292 VMS__free( schedSlots );
293 }
296 void
297 create_the_coreLoop_OS_threads()
298 {
299 //========================================================================
300 // Create the Threads
301 int coreIdx, retCode;
303 //Need the threads to be created suspended, and wait for a signal
304 // before proceeding -- gives time after creating to initialize other
305 // stuff before the coreLoops set off.
306 _VMSMasterEnv->setupComplete = 0;
308 //Make the threads that animate the core loops
309 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
310 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
311 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
313 retCode =
314 pthread_create( &(coreLoopThdHandles[coreIdx]),
315 thdAttrs,
316 &coreLoop,
317 (void *)(coreLoopThdParams[coreIdx]) );
318 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
319 }
320 }
322 /*Semantic layer calls this when it want the system to start running..
323 *
324 *This starts the core loops running then waits for them to exit.
325 */
326 void
327 VMS__start_the_work_then_wait_until_done()
328 { int coreIdx;
329 //Start the core loops running
331 //tell the core loop threads that setup is complete
332 //get lock, to lock out any threads still starting up -- they'll see
333 // that setupComplete is true before entering while loop, and so never
334 // wait on the condition
335 pthread_mutex_lock( &suspendLock );
336 _VMSMasterEnv->setupComplete = 1;
337 pthread_mutex_unlock( &suspendLock );
338 pthread_cond_broadcast( &suspend_cond );
341 //wait for all to complete
342 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
343 {
344 pthread_join( coreLoopThdHandles[coreIdx], NULL );
345 }
347 //NOTE: do not clean up VMS env here -- semantic layer has to have
348 // a chance to clean up its environment first, then do a call to free
349 // the Master env and rest of VMS locations
350 }
352 #ifdef SEQUENTIAL
353 /*Only difference between version with an OS thread pinned to each core and
354 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
355 */
356 void
357 VMS__start_the_work_then_wait_until_done_Seq()
358 {
359 //Instead of un-suspending threads, just call the one and only
360 // core loop (sequential version), in the main thread.
361 coreLoop_Seq( NULL );
362 flushRegisters();
364 }
365 #endif
367 inline VirtProcr *
368 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
369 { VirtProcr *newPr;
370 void *stackLocs;
372 newPr = VMS__malloc( sizeof(VirtProcr) );
373 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
374 if( stackLocs == 0 )
375 { perror("VMS__malloc stack"); exit(1); }
377 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
378 }
380 /* "ext" designates that it's for use outside the VMS system -- should only
381 * be called from main thread or other thread -- never from code animated by
382 * a VMS virtual processor.
383 */
384 inline VirtProcr *
385 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
386 { VirtProcr *newPr;
387 char *stackLocs;
389 newPr = malloc( sizeof(VirtProcr) );
390 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
391 if( stackLocs == 0 )
392 { perror("malloc stack"); exit(1); }
394 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
395 }
398 /*Anticipating multi-tasking
399 */
400 void *
401 VMS__give_sem_env_for( VirtProcr *animPr )
402 {
403 return _VMSMasterEnv->semanticEnv;
404 }
405 //===========================================================================
406 /*there is a label inside this function -- save the addr of this label in
407 * the callingPr struc, as the pick-up point from which to start the next
408 * work-unit for that procr. If turns out have to save registers, then
409 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
410 * "done with work-unit" label. The procr struc is in the request in the
411 * slave that animated the just-ended work-unit, so all the state is saved
412 * there, and will get passed along, inside the request handler, to the
413 * next work-unit for that procr.
414 */
415 void
416 VMS__suspend_procr( VirtProcr *animatingPr )
417 {
419 //The request to master will cause this suspended virt procr to get
420 // scheduled again at some future point -- to resume, core loop jumps
421 // to the resume point (below), which causes restore of saved regs and
422 // "return" from this call.
423 //animatingPr->nextInstrPt = &&ResumePt;
425 //return ownership of the virt procr and sched slot to Master virt pr
426 animatingPr->schedSlot->workIsDone = TRUE;
428 //=========================== Measurement stuff ========================
429 #ifdef MEAS__TIME_STAMP_SUSP
430 //record time stamp: compare to time-stamp recorded below
431 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
432 #endif
433 //=======================================================================
435 switchToCoreLoop(animatingPr);
436 flushRegisters();
438 //=======================================================================
440 #ifdef MEAS__TIME_STAMP_SUSP
441 //NOTE: only take low part of count -- do sanity check when take diff
442 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
443 #endif
445 return;
446 }
450 /*For this implementation of VMS, it may not make much sense to have the
451 * system of requests for creating a new processor done this way.. but over
452 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
453 * distributed-memory, and so on, this gives VMS implementation a chance to
454 * do stuff before suspend, in the AppVP, and in the Master before the plugin
455 * is called, as well as in the lang-lib before this is called, and in the
456 * plugin. So, this gives both VMS and language implementations a chance to
457 * intercept at various points and do order-dependent stuff.
458 *Having a standard VMSNewPrReqData struc allows the language to create and
459 * free the struc, while VMS knows how to get the newPr if it wants it, and
460 * it lets the lang have lang-specific data related to creation transported
461 * to the plugin.
462 */
463 __attribute__ ((noinline)) void
464 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
466 { VMSReqst req;
468 req.reqType = createReq;
469 req.semReqData = semReqData;
470 req.nextReqst = reqstingPr->requests;
471 reqstingPr->requests = &req;
473 VMS__suspend_procr( reqstingPr );
474 }
477 /*
478 *This adds a request to dissipate, then suspends the processor so that the
479 * request handler will receive the request. The request handler is what
480 * does the work of freeing memory and removing the processor from the
481 * semantic environment's data structures.
482 *The request handler also is what figures out when to shutdown the VMS
483 * system -- which causes all the core loop threads to die, and returns from
484 * the call that started up VMS to perform the work.
485 *
486 *This form is a bit misleading to understand if one is trying to figure out
487 * how VMS works -- it looks like a normal function call, but inside it
488 * sends a request to the request handler and suspends the processor, which
489 * jumps out of the VMS__dissipate_procr function, and out of all nestings
490 * above it, transferring the work of dissipating to the request handler,
491 * which then does the actual work -- causing the processor that animated
492 * the call of this function to disappear and the "hanging" state of this
493 * function to just poof into thin air -- the virtual processor's trace
494 * never returns from this call, but instead the virtual processor's trace
495 * gets suspended in this call and all the virt processor's state disap-
496 * pears -- making that suspend the last thing in the virt procr's trace.
497 */
498 __attribute__ ((noinline)) void
499 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
500 { VMSReqst req;
502 req.reqType = dissipate;
503 req.nextReqst = procrToDissipate->requests;
504 procrToDissipate->requests = &req;
506 VMS__suspend_procr( procrToDissipate );
507 }
510 /* "ext" designates that it's for use outside the VMS system -- should only
511 * be called from main thread or other thread -- never from code animated by
512 * a VMS virtual processor.
513 *
514 *Use this version to dissipate VPs created outside the VMS system.
515 */
516 void
517 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
518 {
519 //NOTE: initialData was given to the processor, so should either have
520 // been alloc'd with VMS__malloc, or freed by the level above animPr.
521 //So, all that's left to free here is the stack and the VirtProcr struc
522 // itself
523 //Note, should not stack-allocate initial data -- no guarantee, in
524 // general that creating processor will outlive ones it creates.
525 free( procrToDissipate->startOfStack );
526 free( procrToDissipate );
527 }
531 /*This call's name indicates that request is malloc'd -- so req handler
532 * has to free any extra requests tacked on before a send, using this.
533 *
534 * This inserts the semantic-layer's request data into standard VMS carrier
535 * request data-struct that is mallocd. The sem request doesn't need to
536 * be malloc'd if this is called inside the same call chain before the
537 * send of the last request is called.
538 *
539 *The request handler has to call VMS__free_VMSReq for any of these
540 */
541 inline void
542 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
543 VirtProcr *callingPr )
544 { VMSReqst *req;
546 req = VMS__malloc( sizeof(VMSReqst) );
547 req->reqType = semantic;
548 req->semReqData = semReqData;
549 req->nextReqst = callingPr->requests;
550 callingPr->requests = req;
551 }
553 /*This inserts the semantic-layer's request data into standard VMS carrier
554 * request data-struct is allocated on stack of this call & ptr to it sent
555 * to plugin
556 *Then it does suspend, to cause request to be sent.
557 */
558 /*inline*/__attribute__ ((noinline)) void
559 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
560 { VMSReqst req;
562 req.reqType = semantic;
563 req.semReqData = semReqData;
564 req.nextReqst = callingPr->requests;
565 callingPr->requests = &req;
567 VMS__suspend_procr( callingPr );
568 }
571 /*inline*/ __attribute__ ((noinline)) void
572 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
574 { VMSReqst req;
576 req.reqType = VMSSemantic;
577 req.semReqData = semReqData;
578 req.nextReqst = callingPr->requests; //gab any other preceeding
579 callingPr->requests = &req;
581 VMS__suspend_procr( callingPr );
582 }
585 /*
586 */
587 VMSReqst *
588 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
589 { VMSReqst *req;
591 req = procrWithReq->requests;
592 if( req == NULL ) return NULL;
594 procrWithReq->requests = procrWithReq->requests->nextReqst;
595 return req;
596 }
599 inline void *
600 VMS__take_sem_reqst_from( VMSReqst *req )
601 {
602 return req->semReqData;
603 }
607 /* This is for OS requests and VMS infrastructure requests, such as to create
608 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
609 * language -- but it's also a semantic thing that's triggered from and used
610 * in the application.. so it crosses abstractions.. so, need some special
611 * pattern here for handling such requests.
612 * Doing this just like it were a second language sharing VMS-core.
613 *
614 * This is called from the language's request handler when it sees a request
615 * of type VMSSemReq
616 *
617 * TODO: Later change this, to give probes their own separate plugin & have
618 * VMS-core steer the request to appropriate plugin
619 * Do the same for OS calls -- look later at it..
620 */
621 void inline
622 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
623 ResumePrFnPtr resumePrFnPtr )
624 { VMSSemReq *semReq;
625 IntervalProbe *newProbe;
627 semReq = req->semReqData;
629 newProbe = VMS__malloc( sizeof(IntervalProbe) );
630 newProbe->nameStr = VMS__strDup( semReq->nameStr );
631 newProbe->hist = NULL;
632 newProbe->schedChoiceWasRecorded = FALSE;
634 //This runs in masterVP, so no race-condition worries
635 newProbe->probeID =
636 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
638 requestingPr->dataRetFromReq = newProbe;
640 (*resumePrFnPtr)( requestingPr, semEnv );
641 }
645 /*This must be called by the request handler plugin -- it cannot be called
646 * from the semantic library "dissipate processor" function -- instead, the
647 * semantic layer has to generate a request, and the plug-in calls this
648 * function.
649 *The reason is that this frees the virtual processor's stack -- which is
650 * still in use inside semantic library calls!
651 *
652 *This frees or recycles all the state owned by and comprising the VMS
653 * portion of the animating virtual procr. The request handler must first
654 * free any semantic data created for the processor that didn't use the
655 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
656 * system to disown any state that did use VMS_malloc, and then frees the
657 * statck and the processor-struct itself.
658 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
659 * state, then that state gets freed (or sent to recycling) as a side-effect
660 * of dis-owning it.
661 */
662 void
663 VMS__dissipate_procr( VirtProcr *animatingPr )
664 {
665 //dis-own all locations owned by this processor, causing to be freed
666 // any locations that it is (was) sole owner of
667 //TODO: implement VMS__malloc system, including "give up ownership"
670 //NOTE: initialData was given to the processor, so should either have
671 // been alloc'd with VMS__malloc, or freed by the level above animPr.
672 //So, all that's left to free here is the stack and the VirtProcr struc
673 // itself
674 //Note, should not stack-allocate initial data -- no guarantee, in
675 // general that creating processor will outlive ones it creates.
676 VMS__free( animatingPr->startOfStack );
677 VMS__free( animatingPr );
678 }
681 //TODO: look at architecting cleanest separation between request handler
682 // and master loop, for dissipate, create, shutdown, and other non-semantic
683 // requests. Issue is chain: one removes requests from AppVP, one dispatches
684 // on type of request, and one handles each type.. but some types require
685 // action from both request handler and master loop -- maybe just give the
686 // request handler calls like: VMS__handle_X_request_type
689 /*This is called by the semantic layer's request handler when it decides its
690 * time to shut down the VMS system. Calling this causes the core loop OS
691 * threads to exit, which unblocks the entry-point function that started up
692 * VMS, and allows it to grab the result and return to the original single-
693 * threaded application.
694 *
695 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
696 * and-wait function has to free a bunch of stuff after it detects the
697 * threads have all died: the masterEnv, the thread-related locations,
698 * masterVP any AppVPs that might still be allocated and sitting in the
699 * semantic environment, or have been orphaned in the _VMSWorkQ.
700 *
701 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
702 * locations it needs, and give ownership to masterVP. Then, they will be
703 * automatically freed.
704 *
705 *In here,create one core-loop shut-down processor for each core loop and put
706 * them all directly into the readyToAnimateQ.
707 *Note, this function can ONLY be called after the semantic environment no
708 * longer cares if AppVPs get animated after the point this is called. In
709 * other words, this can be used as an abort, or else it should only be
710 * called when all AppVPs have finished dissipate requests -- only at that
711 * point is it sure that all results have completed.
712 */
713 void
714 VMS__shutdown()
715 { int coreIdx;
716 VirtProcr *shutDownPr;
718 //create the shutdown processors, one for each core loop -- put them
719 // directly into the Q -- each core will die when gets one
720 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
721 { //Note, this is running in the master
722 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
723 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
724 }
725 #ifdef MEAS__PERF_COUNTERS
726 uint64 tmpc,tmpi;
727 saveCyclesAndInstrs(0,tmpc,tmpi);
728 printf("End: cycles = %lu, instrs = %lu\n",tmpc,tmpi);
729 prctl(PR_TASK_PERF_EVENTS_DISABLE);
730 /*
731 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ ){
732 close(_VMSMasterEnv->cycles_counter_fd[coreIdx]);
733 close(_VMSMasterEnv->instrs_counter_fd[coreIdx]);
734 }
735 */
736 #endif
737 }
740 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
741 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
742 *This function has the sole purpose of setting the stack and framePtr
743 * to the coreLoop's stack and framePtr.. it does that then jumps to the
744 * core loop's shutdown point -- might be able to just call Pthread_exit
745 * from here, but am going back to the pthread's stack and setting everything
746 * up just as if it never jumped out, before calling pthread_exit.
747 *The end-point of core loop will free the stack and so forth of the
748 * processor that animates this function, (this fn is transfering the
749 * animator of the AppVP that is in turn animating this function over
750 * to core loop function -- note that this slices out a level of virtual
751 * processors).
752 */
753 void
754 endOSThreadFn( void *initData, VirtProcr *animatingPr )
755 {
756 #ifdef SEQUENTIAL
757 asmTerminateCoreLoopSeq(animatingPr);
758 #else
759 asmTerminateCoreLoop(animatingPr);
760 #endif
761 }
764 /*This is called from the startup & shutdown
765 */
766 void
767 VMS__cleanup_at_end_of_shutdown()
768 {
769 //unused
770 //VMSQueueStruc **readyToAnimateQs;
771 //int coreIdx;
772 //VirtProcr **masterVPs;
773 //SchedSlot ***allSchedSlots; //ptr to array of ptrs
775 //Before getting rid of everything, print out any measurements made
776 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&printHist );
777 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&saveHistToFile);
778 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, &freeHistExt );
779 #ifdef DETECT_DEPENDENCIES
780 FILE* output;
781 int n;
782 char filename[255];
783 for(n=0;n<255;n++)
784 {
785 sprintf(filename, "./counters/Dependencies.%d.csv",n);
786 output = fopen(filename,"r");
787 if(output)
788 {
789 fclose(output);
790 }else{
791 break;
792 }
793 }
794 printf("Saving Dependencies to File: %s ...\n", filename);
795 output = fopen(filename,"w+");
796 set_dependency_file(output);
797 fprintf(output,"digraph Dependencies {\n");
799 forAllInDynArrayDo( _VMSMasterEnv->dependenciesInfo, &print_dependency_to_file );
800 fprintf(output,"}\n");
801 #endif
802 #ifdef MEAS__TIME_PLUGIN
803 printHist( _VMSMasterEnv->reqHdlrLowTimeHist );
804 saveHistToFile( _VMSMasterEnv->reqHdlrLowTimeHist );
805 printHist( _VMSMasterEnv->reqHdlrHighTimeHist );
806 saveHistToFile( _VMSMasterEnv->reqHdlrHighTimeHist );
807 freeHistExt( _VMSMasterEnv->reqHdlrLowTimeHist );
808 freeHistExt( _VMSMasterEnv->reqHdlrHighTimeHist );
809 #endif
810 #ifdef MEAS__TIME_MALLOC
811 printHist( _VMSMasterEnv->mallocTimeHist );
812 saveHistToFile( _VMSMasterEnv->mallocTimeHist );
813 printHist( _VMSMasterEnv->freeTimeHist );
814 saveHistToFile( _VMSMasterEnv->freeTimeHist );
815 freeHistExt( _VMSMasterEnv->mallocTimeHist );
816 freeHistExt( _VMSMasterEnv->freeTimeHist );
817 #endif
818 #ifdef MEAS__TIME_MASTER_LOCK
819 printHist( _VMSMasterEnv->masterLockLowTimeHist );
820 printHist( _VMSMasterEnv->masterLockHighTimeHist );
821 #endif
822 #ifdef MEAS__TIME_MASTER
823 printHist( _VMSMasterEnv->pluginTimeHist );
824 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
825 {
826 freeVMSQ( readyToAnimateQs[ coreIdx ] );
827 //master VPs were created external to VMS, so use external free
828 VMS__dissipate_procr( masterVPs[ coreIdx ] );
830 freeSchedSlots( allSchedSlots[ coreIdx ] );
831 }
832 #endif
833 #ifdef MEAS__TIME_STAMP_SUSP
834 printHist( _VMSMasterEnv->pluginTimeHist );
835 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
836 {
837 freeVMSQ( readyToAnimateQs[ coreIdx ] );
838 //master VPs were created external to VMS, so use external free
839 VMS__dissipate_procr( masterVPs[ coreIdx ] );
841 freeSchedSlots( allSchedSlots[ coreIdx ] );
842 }
843 #endif
845 //All the environment data has been allocated with VMS__malloc, so just
846 // free its internal big-chunk and all inside it disappear.
847 /*
848 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
849 masterVPs = _VMSMasterEnv->masterVPs;
850 allSchedSlots = _VMSMasterEnv->allSchedSlots;
852 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
853 {
854 freeVMSQ( readyToAnimateQs[ coreIdx ] );
855 //master VPs were created external to VMS, so use external free
856 VMS__dissipate_procr( masterVPs[ coreIdx ] );
858 freeSchedSlots( allSchedSlots[ coreIdx ] );
859 }
861 VMS__free( _VMSMasterEnv->readyToAnimateQs );
862 VMS__free( _VMSMasterEnv->masterVPs );
863 VMS__free( _VMSMasterEnv->allSchedSlots );
865 //============================= MEASUREMENT STUFF ========================
866 #ifdef STATS__TURN_ON_PROBES
867 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
868 #endif
869 //========================================================================
870 */
871 //These are the only two that use system free
872 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
873 free( (void *)_VMSMasterEnv );
874 }
877 //================================
880 /*Later, improve this -- for now, just exits the application after printing
881 * the error message.
882 */
883 void
884 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
885 {
886 printf("%s",msgStr);
887 fflush(stdin);
888 exit(1);
889 }