view VMS.c @ 186:69eb54ce9c4b

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