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