Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 167:981acd1db6af
Separate UCC recording from VMS core and put it into SSR plugin
| author | Nina Engelhardt |
|---|---|
| date | Mon, 05 Dec 2011 18:59:48 +0100 |
| parents | aefd87f9d12f |
| children | 3bd35fc83c61 |
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();
190 #ifdef DETECT_LOOP_GRAPH
191 _VMSMasterEnv->loop_graph = VMS__malloc(10*sizeof(void*));
192 _VMSMasterEnv->loop_graph_array_info = makePrivDynArrayInfoFrom((void***)&(_VMSMasterEnv->loop_graph),10);
193 int loop_i;
194 for(loop_i=0;loop_i<NUM_CORES;loop_i++){
195 _VMSMasterEnv->loop_counter[loop_i]=0;
196 }
198 #endif
200 #ifdef MEAS__PERF_COUNTERS
201 _VMSMasterEnv->counter_history = VMS__malloc(10*sizeof(void*));
202 _VMSMasterEnv->counter_history_array_info = makePrivDynArrayInfoFrom((void***)&(_VMSMasterEnv->counter_history),10);
203 //printf("Creating HW counters...");
204 FILE* output;
205 int n;
206 char filename[255];
207 for(n=0;n<255;n++)
208 {
209 sprintf(filename, "./counters/Counters.%d.csv",n);
210 output = fopen(filename,"r");
211 if(output)
212 {
213 fclose(output);
214 }else{
215 break;
216 }
217 }
218 printf("Saving Counter measurements to File: %s ...\n", filename);
219 output = fopen(filename,"w+");
220 _VMSMasterEnv->counteroutput = output;
222 struct perf_event_attr hw_event;
223 memset(&hw_event,0,sizeof(hw_event));
224 hw_event.type = PERF_TYPE_HARDWARE;
225 hw_event.size = sizeof(hw_event);
226 hw_event.disabled = 1;
227 hw_event.freq = 0;
228 hw_event.inherit = 1; /* children inherit it */
229 hw_event.pinned = 1; /* must always be on PMU */
230 hw_event.exclusive = 0; /* only group on PMU */
231 hw_event.exclude_user = 0; /* don't count user */
232 hw_event.exclude_kernel = 1; /* ditto kernel */
233 hw_event.exclude_hv = 1; /* ditto hypervisor */
234 hw_event.exclude_idle = 0; /* don't count when idle */
235 hw_event.mmap = 0; /* include mmap data */
236 hw_event.comm = 0; /* include comm data */
239 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
240 {
241 hw_event.config = 0x0000000000000000; //cycles
242 _VMSMasterEnv->cycles_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
243 0,//pid_t pid,
244 coreIdx,//int cpu,
245 -1,//int group_fd,
246 0//unsigned long flags
247 );
248 if (_VMSMasterEnv->cycles_counter_fd[coreIdx]<0){
249 fprintf(stderr,"On core %d: ",coreIdx);
250 perror("Failed to open cycles counter");
251 }
252 hw_event.config = 0x0000000000000001; //instrs
253 _VMSMasterEnv->instrs_counter_fd[coreIdx] = syscall(__NR_perf_event_open, &hw_event,
254 0,//pid_t pid,
255 coreIdx,//int cpu,
256 -1,//int group_fd,
257 0//unsigned long flags
258 );
259 if (_VMSMasterEnv->instrs_counter_fd[coreIdx]<0){
260 fprintf(stderr,"On core %d: ",coreIdx);
261 perror("Failed to open instrs counter");
262 }
263 }
264 prctl(PR_TASK_PERF_EVENTS_ENABLE);
265 uint64 tmpc,tmpi;
266 saveCyclesAndInstrs(0,tmpc,tmpi);
267 printf("Start: cycles = %llu, instrs = %llu\n",tmpc,tmpi);
268 #endif
270 //========================================================================
272 }
274 SchedSlot **
275 create_sched_slots()
276 { SchedSlot **schedSlots;
277 int i;
279 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
281 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
282 {
283 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
285 //Set state to mean "handling requests done, slot needs filling"
286 schedSlots[i]->workIsDone = FALSE;
287 schedSlots[i]->needsProcrAssigned = TRUE;
288 }
289 return schedSlots;
290 }
293 void
294 freeSchedSlots( SchedSlot **schedSlots )
295 { int i;
296 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
297 {
298 VMS__free( schedSlots[i] );
299 }
300 VMS__free( schedSlots );
301 }
304 void
305 create_the_coreLoop_OS_threads()
306 {
307 //========================================================================
308 // Create the Threads
309 int coreIdx, retCode;
311 //Need the threads to be created suspended, and wait for a signal
312 // before proceeding -- gives time after creating to initialize other
313 // stuff before the coreLoops set off.
314 _VMSMasterEnv->setupComplete = 0;
316 //Make the threads that animate the core loops
317 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
318 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
319 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
321 retCode =
322 pthread_create( &(coreLoopThdHandles[coreIdx]),
323 thdAttrs,
324 &coreLoop,
325 (void *)(coreLoopThdParams[coreIdx]) );
326 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
327 }
328 }
330 /*Semantic layer calls this when it want the system to start running..
331 *
332 *This starts the core loops running then waits for them to exit.
333 */
334 void
335 VMS__start_the_work_then_wait_until_done()
336 { int coreIdx;
337 //Start the core loops running
339 //tell the core loop threads that setup is complete
340 //get lock, to lock out any threads still starting up -- they'll see
341 // that setupComplete is true before entering while loop, and so never
342 // wait on the condition
343 pthread_mutex_lock( &suspendLock );
344 _VMSMasterEnv->setupComplete = 1;
345 pthread_mutex_unlock( &suspendLock );
346 pthread_cond_broadcast( &suspend_cond );
349 //wait for all to complete
350 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
351 {
352 pthread_join( coreLoopThdHandles[coreIdx], NULL );
353 }
355 //NOTE: do not clean up VMS env here -- semantic layer has to have
356 // a chance to clean up its environment first, then do a call to free
357 // the Master env and rest of VMS locations
358 }
360 #ifdef SEQUENTIAL
361 /*Only difference between version with an OS thread pinned to each core and
362 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
363 */
364 void
365 VMS__start_the_work_then_wait_until_done_Seq()
366 {
367 //Instead of un-suspending threads, just call the one and only
368 // core loop (sequential version), in the main thread.
369 coreLoop_Seq( NULL );
370 flushRegisters();
372 }
373 #endif
375 inline VirtProcr *
376 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
377 { VirtProcr *newPr;
378 void *stackLocs;
380 newPr = VMS__malloc( sizeof(VirtProcr) );
381 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
382 if( stackLocs == 0 )
383 { perror("VMS__malloc stack"); exit(1); }
385 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
386 }
388 /* "ext" designates that it's for use outside the VMS system -- should only
389 * be called from main thread or other thread -- never from code animated by
390 * a VMS virtual processor.
391 */
392 inline VirtProcr *
393 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
394 { VirtProcr *newPr;
395 char *stackLocs;
397 newPr = malloc( sizeof(VirtProcr) );
398 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
399 if( stackLocs == 0 )
400 { perror("malloc stack"); exit(1); }
402 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
403 }
406 /*Anticipating multi-tasking
407 */
408 void *
409 VMS__give_sem_env_for( VirtProcr *animPr )
410 {
411 return _VMSMasterEnv->semanticEnv;
412 }
413 //===========================================================================
414 /*there is a label inside this function -- save the addr of this label in
415 * the callingPr struc, as the pick-up point from which to start the next
416 * work-unit for that procr. If turns out have to save registers, then
417 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
418 * "done with work-unit" label. The procr struc is in the request in the
419 * slave that animated the just-ended work-unit, so all the state is saved
420 * there, and will get passed along, inside the request handler, to the
421 * next work-unit for that procr.
422 */
423 void
424 VMS__suspend_procr( VirtProcr *animatingPr )
425 {
427 //The request to master will cause this suspended virt procr to get
428 // scheduled again at some future point -- to resume, core loop jumps
429 // to the resume point (below), which causes restore of saved regs and
430 // "return" from this call.
431 //animatingPr->nextInstrPt = &&ResumePt;
433 //return ownership of the virt procr and sched slot to Master virt pr
434 animatingPr->schedSlot->workIsDone = TRUE;
436 //=========================== Measurement stuff ========================
437 #ifdef MEAS__TIME_STAMP_SUSP
438 //record time stamp: compare to time-stamp recorded below
439 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
440 #endif
441 //=======================================================================
443 switchToCoreLoop(animatingPr);
444 flushRegisters();
446 //=======================================================================
448 #ifdef MEAS__TIME_STAMP_SUSP
449 //NOTE: only take low part of count -- do sanity check when take diff
450 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
451 #endif
453 return;
454 }
458 /*For this implementation of VMS, it may not make much sense to have the
459 * system of requests for creating a new processor done this way.. but over
460 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
461 * distributed-memory, and so on, this gives VMS implementation a chance to
462 * do stuff before suspend, in the AppVP, and in the Master before the plugin
463 * is called, as well as in the lang-lib before this is called, and in the
464 * plugin. So, this gives both VMS and language implementations a chance to
465 * intercept at various points and do order-dependent stuff.
466 *Having a standard VMSNewPrReqData struc allows the language to create and
467 * free the struc, while VMS knows how to get the newPr if it wants it, and
468 * it lets the lang have lang-specific data related to creation transported
469 * to the plugin.
470 */
471 __attribute__ ((noinline)) void
472 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
474 { VMSReqst req;
476 req.reqType = createReq;
477 req.semReqData = semReqData;
478 req.nextReqst = reqstingPr->requests;
479 reqstingPr->requests = &req;
481 VMS__suspend_procr( reqstingPr );
482 }
485 /*
486 *This adds a request to dissipate, then suspends the processor so that the
487 * request handler will receive the request. The request handler is what
488 * does the work of freeing memory and removing the processor from the
489 * semantic environment's data structures.
490 *The request handler also is what figures out when to shutdown the VMS
491 * system -- which causes all the core loop threads to die, and returns from
492 * the call that started up VMS to perform the work.
493 *
494 *This form is a bit misleading to understand if one is trying to figure out
495 * how VMS works -- it looks like a normal function call, but inside it
496 * sends a request to the request handler and suspends the processor, which
497 * jumps out of the VMS__dissipate_procr function, and out of all nestings
498 * above it, transferring the work of dissipating to the request handler,
499 * which then does the actual work -- causing the processor that animated
500 * the call of this function to disappear and the "hanging" state of this
501 * function to just poof into thin air -- the virtual processor's trace
502 * never returns from this call, but instead the virtual processor's trace
503 * gets suspended in this call and all the virt processor's state disap-
504 * pears -- making that suspend the last thing in the virt procr's trace.
505 */
506 __attribute__ ((noinline)) void
507 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
508 { VMSReqst req;
510 req.reqType = dissipate;
511 req.nextReqst = procrToDissipate->requests;
512 procrToDissipate->requests = &req;
514 VMS__suspend_procr( procrToDissipate );
515 }
518 /* "ext" designates that it's for use outside the VMS system -- should only
519 * be called from main thread or other thread -- never from code animated by
520 * a VMS virtual processor.
521 *
522 *Use this version to dissipate VPs created outside the VMS system.
523 */
524 void
525 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
526 {
527 //NOTE: initialData was given to the processor, so should either have
528 // been alloc'd with VMS__malloc, or freed by the level above animPr.
529 //So, all that's left to free here is the stack and the VirtProcr struc
530 // itself
531 //Note, should not stack-allocate initial data -- no guarantee, in
532 // general that creating processor will outlive ones it creates.
533 free( procrToDissipate->startOfStack );
534 free( procrToDissipate );
535 }
539 /*This call's name indicates that request is malloc'd -- so req handler
540 * has to free any extra requests tacked on before a send, using this.
541 *
542 * This inserts the semantic-layer's request data into standard VMS carrier
543 * request data-struct that is mallocd. The sem request doesn't need to
544 * be malloc'd if this is called inside the same call chain before the
545 * send of the last request is called.
546 *
547 *The request handler has to call VMS__free_VMSReq for any of these
548 */
549 inline void
550 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
551 VirtProcr *callingPr )
552 { VMSReqst *req;
554 req = VMS__malloc( sizeof(VMSReqst) );
555 req->reqType = semantic;
556 req->semReqData = semReqData;
557 req->nextReqst = callingPr->requests;
558 callingPr->requests = req;
559 }
561 /*This inserts the semantic-layer's request data into standard VMS carrier
562 * request data-struct is allocated on stack of this call & ptr to it sent
563 * to plugin
564 *Then it does suspend, to cause request to be sent.
565 */
566 /*inline*/__attribute__ ((noinline)) void
567 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
568 { VMSReqst req;
570 req.reqType = semantic;
571 req.semReqData = semReqData;
572 req.nextReqst = callingPr->requests;
573 callingPr->requests = &req;
575 VMS__suspend_procr( callingPr );
576 }
579 /*inline*/ __attribute__ ((noinline)) void
580 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
582 { VMSReqst req;
584 req.reqType = VMSSemantic;
585 req.semReqData = semReqData;
586 req.nextReqst = callingPr->requests; //gab any other preceeding
587 callingPr->requests = &req;
589 VMS__suspend_procr( callingPr );
590 }
593 /*
594 */
595 VMSReqst *
596 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
597 { VMSReqst *req;
599 req = procrWithReq->requests;
600 if( req == NULL ) return NULL;
602 procrWithReq->requests = procrWithReq->requests->nextReqst;
603 return req;
604 }
607 inline void *
608 VMS__take_sem_reqst_from( VMSReqst *req )
609 {
610 return req->semReqData;
611 }
615 /* This is for OS requests and VMS infrastructure requests, such as to create
616 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
617 * language -- but it's also a semantic thing that's triggered from and used
618 * in the application.. so it crosses abstractions.. so, need some special
619 * pattern here for handling such requests.
620 * Doing this just like it were a second language sharing VMS-core.
621 *
622 * This is called from the language's request handler when it sees a request
623 * of type VMSSemReq
624 *
625 * TODO: Later change this, to give probes their own separate plugin & have
626 * VMS-core steer the request to appropriate plugin
627 * Do the same for OS calls -- look later at it..
628 */
629 void inline
630 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
631 ResumePrFnPtr resumePrFnPtr )
632 { VMSSemReq *semReq;
633 IntervalProbe *newProbe;
635 semReq = req->semReqData;
637 newProbe = VMS__malloc( sizeof(IntervalProbe) );
638 newProbe->nameStr = VMS__strDup( semReq->nameStr );
639 newProbe->hist = NULL;
640 newProbe->schedChoiceWasRecorded = FALSE;
642 //This runs in masterVP, so no race-condition worries
643 newProbe->probeID =
644 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
646 requestingPr->dataRetFromReq = newProbe;
648 (*resumePrFnPtr)( requestingPr, semEnv );
649 }
653 /*This must be called by the request handler plugin -- it cannot be called
654 * from the semantic library "dissipate processor" function -- instead, the
655 * semantic layer has to generate a request, and the plug-in calls this
656 * function.
657 *The reason is that this frees the virtual processor's stack -- which is
658 * still in use inside semantic library calls!
659 *
660 *This frees or recycles all the state owned by and comprising the VMS
661 * portion of the animating virtual procr. The request handler must first
662 * free any semantic data created for the processor that didn't use the
663 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
664 * system to disown any state that did use VMS_malloc, and then frees the
665 * statck and the processor-struct itself.
666 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
667 * state, then that state gets freed (or sent to recycling) as a side-effect
668 * of dis-owning it.
669 */
670 void
671 VMS__dissipate_procr( VirtProcr *animatingPr )
672 {
673 //dis-own all locations owned by this processor, causing to be freed
674 // any locations that it is (was) sole owner of
675 //TODO: implement VMS__malloc system, including "give up ownership"
678 //NOTE: initialData was given to the processor, so should either have
679 // been alloc'd with VMS__malloc, or freed by the level above animPr.
680 //So, all that's left to free here is the stack and the VirtProcr struc
681 // itself
682 //Note, should not stack-allocate initial data -- no guarantee, in
683 // general that creating processor will outlive ones it creates.
684 VMS__free( animatingPr->startOfStack );
685 VMS__free( animatingPr );
686 }
689 //TODO: look at architecting cleanest separation between request handler
690 // and master loop, for dissipate, create, shutdown, and other non-semantic
691 // requests. Issue is chain: one removes requests from AppVP, one dispatches
692 // on type of request, and one handles each type.. but some types require
693 // action from both request handler and master loop -- maybe just give the
694 // request handler calls like: VMS__handle_X_request_type
697 /*This is called by the semantic layer's request handler when it decides its
698 * time to shut down the VMS system. Calling this causes the core loop OS
699 * threads to exit, which unblocks the entry-point function that started up
700 * VMS, and allows it to grab the result and return to the original single-
701 * threaded application.
702 *
703 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
704 * and-wait function has to free a bunch of stuff after it detects the
705 * threads have all died: the masterEnv, the thread-related locations,
706 * masterVP any AppVPs that might still be allocated and sitting in the
707 * semantic environment, or have been orphaned in the _VMSWorkQ.
708 *
709 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
710 * locations it needs, and give ownership to masterVP. Then, they will be
711 * automatically freed.
712 *
713 *In here,create one core-loop shut-down processor for each core loop and put
714 * them all directly into the readyToAnimateQ.
715 *Note, this function can ONLY be called after the semantic environment no
716 * longer cares if AppVPs get animated after the point this is called. In
717 * other words, this can be used as an abort, or else it should only be
718 * called when all AppVPs have finished dissipate requests -- only at that
719 * point is it sure that all results have completed.
720 */
721 void
722 VMS__shutdown()
723 { int coreIdx;
724 VirtProcr *shutDownPr;
726 //create the shutdown processors, one for each core loop -- put them
727 // directly into the Q -- each core will die when gets one
728 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
729 { //Note, this is running in the master
730 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
731 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
732 }
733 #ifdef MEAS__PERF_COUNTERS
734 uint64 tmpc,tmpi;
735 saveCyclesAndInstrs(0,tmpc,tmpi);
736 printf("End: cycles = %llu, instrs = %llu\n",tmpc,tmpi);
737 prctl(PR_TASK_PERF_EVENTS_DISABLE);
738 /*
739 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ ){
740 close(_VMSMasterEnv->cycles_counter_fd[coreIdx]);
741 close(_VMSMasterEnv->instrs_counter_fd[coreIdx]);
742 }
743 */
744 #endif
745 }
748 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
749 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
750 *This function has the sole purpose of setting the stack and framePtr
751 * to the coreLoop's stack and framePtr.. it does that then jumps to the
752 * core loop's shutdown point -- might be able to just call Pthread_exit
753 * from here, but am going back to the pthread's stack and setting everything
754 * up just as if it never jumped out, before calling pthread_exit.
755 *The end-point of core loop will free the stack and so forth of the
756 * processor that animates this function, (this fn is transfering the
757 * animator of the AppVP that is in turn animating this function over
758 * to core loop function -- note that this slices out a level of virtual
759 * processors).
760 */
761 void
762 endOSThreadFn( void *initData, VirtProcr *animatingPr )
763 {
764 #ifdef SEQUENTIAL
765 asmTerminateCoreLoopSeq(animatingPr);
766 #else
767 asmTerminateCoreLoop(animatingPr);
768 #endif
769 }
772 /*This is called from the startup & shutdown
773 */
774 void
775 VMS__cleanup_at_end_of_shutdown()
776 {
777 //unused
778 //VMSQueueStruc **readyToAnimateQs;
779 //int coreIdx;
780 //VirtProcr **masterVPs;
781 //SchedSlot ***allSchedSlots; //ptr to array of ptrs
783 //Before getting rid of everything, print out any measurements made
784 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&printHist );
785 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, (DynArrayFnPtr)&saveHistToFile);
786 //forAllInDynArrayDo( _VMSMasterEnv->measHistsInfo, &freeHistExt );
788 #ifdef DETECT_LOOP_GRAPH
789 FILE* loop_output;
790 int loop_n;
791 char loop_filename[255];
792 for(loop_n=0;loop_n<255;loop_n++)
793 {
794 sprintf(loop_filename, "./counters/LoopGraph.%d.dot",loop_n);
795 loop_output = fopen(loop_filename,"r");
796 if(loop_output)
797 {
798 fclose(loop_output);
799 }else{
800 break;
801 }
802 }
803 if(loop_n<255){
804 printf("Saving Loop Graph to File: %s ...\n", loop_filename);
805 loop_output = fopen(loop_filename,"w+");
806 if(loop_output!=NULL){
807 set_dependency_file(loop_output);
808 fprintf(loop_output,"digraph Loop {\n");
809 set_loop_file(loop_output);
810 forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
811 forAllInDynArrayDo( _VMSMasterEnv->loop_graph_array_info, &print_per_slot_to_file );
812 //int coreIdx;
813 //for(coreIdx=0;coreIdx<NUM_CORES;coreIdx++){
814 // fprintf(loop_output,"sync%d_%d [shape=rect,label=\"Sync\"];\n",coreIdx,_VMSMasterEnv->loop_counter[coreIdx]);
815 //}
816 fprintf(loop_output,"}\n");
817 } else
818 printf("Opening Loop Graph file failed. Please check that folder \"counters\" exists in run directory.\n");
819 } else {
820 printf("Could not open Loop Graph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
821 }
822 #endif
823 #ifdef MEAS__TIME_PLUGIN
824 printHist( _VMSMasterEnv->reqHdlrLowTimeHist );
825 saveHistToFile( _VMSMasterEnv->reqHdlrLowTimeHist );
826 printHist( _VMSMasterEnv->reqHdlrHighTimeHist );
827 saveHistToFile( _VMSMasterEnv->reqHdlrHighTimeHist );
828 freeHistExt( _VMSMasterEnv->reqHdlrLowTimeHist );
829 freeHistExt( _VMSMasterEnv->reqHdlrHighTimeHist );
830 #endif
831 #ifdef MEAS__TIME_MALLOC
832 printHist( _VMSMasterEnv->mallocTimeHist );
833 saveHistToFile( _VMSMasterEnv->mallocTimeHist );
834 printHist( _VMSMasterEnv->freeTimeHist );
835 saveHistToFile( _VMSMasterEnv->freeTimeHist );
836 freeHistExt( _VMSMasterEnv->mallocTimeHist );
837 freeHistExt( _VMSMasterEnv->freeTimeHist );
838 #endif
839 #ifdef MEAS__TIME_MASTER_LOCK
840 printHist( _VMSMasterEnv->masterLockLowTimeHist );
841 printHist( _VMSMasterEnv->masterLockHighTimeHist );
842 #endif
843 #ifdef MEAS__TIME_MASTER
844 printHist( _VMSMasterEnv->pluginTimeHist );
845 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
846 {
847 freeVMSQ( readyToAnimateQs[ coreIdx ] );
848 //master VPs were created external to VMS, so use external free
849 VMS__dissipate_procr( masterVPs[ coreIdx ] );
851 freeSchedSlots( allSchedSlots[ coreIdx ] );
852 }
853 #endif
854 #ifdef MEAS__TIME_STAMP_SUSP
855 printHist( _VMSMasterEnv->pluginTimeHist );
856 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
857 {
858 freeVMSQ( readyToAnimateQs[ coreIdx ] );
859 //master VPs were created external to VMS, so use external free
860 VMS__dissipate_procr( masterVPs[ coreIdx ] );
862 freeSchedSlots( allSchedSlots[ coreIdx ] );
863 }
864 #endif
866 //All the environment data has been allocated with VMS__malloc, so just
867 // free its internal big-chunk and all inside it disappear.
868 /*
869 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
870 masterVPs = _VMSMasterEnv->masterVPs;
871 allSchedSlots = _VMSMasterEnv->allSchedSlots;
873 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
874 {
875 freeVMSQ( readyToAnimateQs[ coreIdx ] );
876 //master VPs were created external to VMS, so use external free
877 VMS__dissipate_procr( masterVPs[ coreIdx ] );
879 freeSchedSlots( allSchedSlots[ coreIdx ] );
880 }
882 VMS__free( _VMSMasterEnv->readyToAnimateQs );
883 VMS__free( _VMSMasterEnv->masterVPs );
884 VMS__free( _VMSMasterEnv->allSchedSlots );
886 //============================= MEASUREMENT STUFF ========================
887 #ifdef STATS__TURN_ON_PROBES
888 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
889 #endif
890 //========================================================================
891 */
892 //These are the only two that use system free
893 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
894 free( (void *)_VMSMasterEnv );
895 }
898 //================================
901 /*Later, improve this -- for now, just exits the application after printing
902 * the error message.
903 */
904 void
905 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
906 {
907 printf("%s",msgStr);
908 fflush(stdin);
909 exit(1);
910 }
