Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.c @ 66:bf08108405cc
Added recycle pool -- will merge later -- need to get PLDI results for now
| author | Me |
|---|---|
| date | Mon, 15 Nov 2010 12:11:24 -0800 |
| parents | 13b22ffb8a2f |
| children |
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 <sys/time.h>
13 #include "VMS.h"
14 #include "Queue_impl/BlockingQueue.h"
15 #include "Histogram/Histogram.h"
18 #define thdAttrs NULL
20 //===========================================================================
21 void
22 shutdownFn( void *dummy, VirtProcr *dummy2 );
24 SchedSlot **
25 create_sched_slots();
27 void
28 create_masterEnv();
30 void
31 create_the_coreLoop_OS_threads();
33 MallocProlog *
34 create_free_list();
36 void
37 endOSThreadFn( void *initData, VirtProcr *animatingPr );
39 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
40 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
42 //===========================================================================
44 /*Setup has two phases:
45 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
46 * the master virt procr into the work-queue, ready for first "call"
47 * 2) Semantic layer then does its own init, which creates the seed virt
48 * procr inside the semantic layer, ready to schedule it when
49 * asked by the first run of the masterLoop.
50 *
51 *This part is bit weird because VMS really wants to be "always there", and
52 * have applications attach and detach.. for now, this VMS is part of
53 * the app, so the VMS system starts up as part of running the app.
54 *
55 *The semantic layer is isolated from the VMS internals by making the
56 * semantic layer do setup to a state that it's ready with its
57 * initial virt procrs, ready to schedule them to slots when the masterLoop
58 * asks. Without this pattern, the semantic layer's setup would
59 * have to modify slots directly to assign the initial virt-procrs, and put
60 * them into the readyToAnimateQ itself, breaking the isolation completely.
61 *
62 *
63 *The semantic layer creates the initial virt procr(s), and adds its
64 * own environment to masterEnv, and fills in the pointers to
65 * the requestHandler and slaveScheduler plug-in functions
66 */
68 /*This allocates VMS data structures, populates the master VMSProc,
69 * and master environment, and returns the master environment to the semantic
70 * layer.
71 */
72 void
73 VMS__init()
74 {
75 create_masterEnv();
76 create_the_coreLoop_OS_threads();
77 }
79 /*To initialize the sequential version, just don't create the threads
80 */
81 void
82 VMS__init_Seq()
83 {
84 create_masterEnv();
85 }
87 void
88 create_masterEnv()
89 { MasterEnv *masterEnv;
90 VMSQueueStruc **readyToAnimateQs;
91 int coreIdx;
92 VirtProcr **masterVPs;
93 SchedSlot ***allSchedSlots; //ptr to array of ptrs
96 //Make the master env, which holds everything else
97 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
99 //Very first thing put into the master env is the free-list, seeded
100 // with a massive initial chunk of memory.
101 //After this, all other mallocs are VMS__malloc.
102 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
105 //============================= MEASUREMENT STUFF ========================
106 #ifdef MEAS__TIME_MALLOC
107 _VMSMasterEnv->mallocTimeHist = makeFixedBinHistExt( 100, 50, 10,
108 "malloc time hist");
109 _VMSMasterEnv->freeTimeHist = makeFixedBinHistExt( 100, 50, 10,
110 "free time hist");
111 #endif
112 //========================================================================
114 //===================== Only VMS__malloc after this ====================
115 masterEnv = _VMSMasterEnv;
117 //Make a readyToAnimateQ for each core loop
118 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
119 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
121 //One array for each core, 3 in array, core's masterVP scheds all
122 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
124 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
125 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
126 {
127 readyToAnimateQs[ coreIdx ] = makeVMSQ();
129 //Q: should give masterVP core-specific info as its init data?
130 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
131 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
132 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
133 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
134 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
135 }
136 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
137 _VMSMasterEnv->masterVPs = masterVPs;
138 _VMSMasterEnv->masterLock = UNLOCKED;
139 _VMSMasterEnv->allSchedSlots = allSchedSlots;
140 _VMSMasterEnv->workStealingLock = UNLOCKED;
143 //Aug 19, 2010: no longer need to place initial masterVP into queue
144 // because coreLoop now controls -- animates its masterVP when no work
147 //============================= MEASUREMENT STUFF ========================
148 #ifdef STATS__TURN_ON_PROBES
149 _VMSMasterEnv->dynIntervalProbesInfo =
150 makePrivDynArrayOfSize( &(_VMSMasterEnv->intervalProbes), 200);
152 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
154 //put creation time directly into master env, for fast retrieval
155 struct timeval timeStamp;
156 gettimeofday( &(timeStamp), NULL);
157 _VMSMasterEnv->createPtInSecs =
158 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
159 #endif
160 #ifdef MEAS__TIME_PLUGIN
161 _VMSMasterEnv->pluginLowTimeHist = makeFixedBinHist( 50, 0, 2,
162 "plugin low time hist");
163 _VMSMasterEnv->pluginHighTimeHist = makeFixedBinHist( 100, 0, 200,
164 "plugin high time hist");
165 #endif
166 #ifdef MEAS__TIME_MASTER_LOCK
167 _VMSMasterEnv->masterLockLowTimeHist = makeFixedBinHist( 50, 0, 2,
168 "master lock low time hist");
169 _VMSMasterEnv->masterLockHighTimeHist = makeFixedBinHist( 100, 0, 200,
170 "master lock high time hist");
171 #endif
172 //========================================================================
174 }
176 SchedSlot **
177 create_sched_slots()
178 { SchedSlot **schedSlots;
179 int i;
181 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
183 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
184 {
185 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
187 //Set state to mean "handling requests done, slot needs filling"
188 schedSlots[i]->workIsDone = FALSE;
189 schedSlots[i]->needsProcrAssigned = TRUE;
190 }
191 return schedSlots;
192 }
195 void
196 freeSchedSlots( SchedSlot **schedSlots )
197 { int i;
198 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
199 {
200 VMS__free( schedSlots[i] );
201 }
202 VMS__free( schedSlots );
203 }
206 void
207 create_the_coreLoop_OS_threads()
208 {
209 //========================================================================
210 // Create the Threads
211 int coreIdx, retCode;
213 //Need the threads to be created suspended, and wait for a signal
214 // before proceeding -- gives time after creating to initialize other
215 // stuff before the coreLoops set off.
216 _VMSMasterEnv->setupComplete = 0;
218 //Make the threads that animate the core loops
219 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
220 { coreLoopThdParams[coreIdx] = VMS__malloc( sizeof(ThdParams) );
221 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
223 retCode =
224 pthread_create( &(coreLoopThdHandles[coreIdx]),
225 thdAttrs,
226 &coreLoop,
227 (void *)(coreLoopThdParams[coreIdx]) );
228 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
229 }
230 }
232 /*Semantic layer calls this when it want the system to start running..
233 *
234 *This starts the core loops running then waits for them to exit.
235 */
236 void
237 VMS__start_the_work_then_wait_until_done()
238 { int coreIdx;
239 //Start the core loops running
241 //tell the core loop threads that setup is complete
242 //get lock, to lock out any threads still starting up -- they'll see
243 // that setupComplete is true before entering while loop, and so never
244 // wait on the condition
245 pthread_mutex_lock( &suspendLock );
246 _VMSMasterEnv->setupComplete = 1;
247 pthread_mutex_unlock( &suspendLock );
248 pthread_cond_broadcast( &suspend_cond );
251 //wait for all to complete
252 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
253 {
254 pthread_join( coreLoopThdHandles[coreIdx], NULL );
255 }
257 //NOTE: do not clean up VMS env here -- semantic layer has to have
258 // a chance to clean up its environment first, then do a call to free
259 // the Master env and rest of VMS locations
260 }
262 /*Only difference between version with an OS thread pinned to each core and
263 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
264 */
265 void
266 VMS__start_the_work_then_wait_until_done_Seq()
267 {
268 //Instead of un-suspending threads, just call the one and only
269 // core loop (sequential version), in the main thread.
270 coreLoop_Seq( NULL );
272 }
276 /*Create stack, then create __cdecl structure on it and put initialData and
277 * pointer to the new structure instance into the parameter positions on
278 * the stack
279 *Then put function pointer into nextInstrPt -- the stack is setup in std
280 * call structure, so jumping to function ptr is same as a GCC generated
281 * function call
282 *No need to save registers on old stack frame, because there's no old
283 * animator state to return to --
284 *
285 */
286 inline VirtProcr *
287 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
288 void *initialData, char *stackLocs )
289 {
290 char *stackPtr;
292 newPr->startOfStack = stackLocs;
293 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
294 newPr->nextInstrPt = fnPtr;
295 newPr->initialData = initialData;
296 newPr->requests = NULL;
297 newPr->schedSlot = NULL;
299 //fnPtr takes two params -- void *initData & void *animProcr
300 //alloc stack locations, make stackPtr be the highest addr minus room
301 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
302 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
303 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
305 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
306 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
307 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
308 newPr->stackPtr = stackPtr; //core loop will switch to this, then
309 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
311 //============================= MEASUREMENT STUFF ========================
312 #ifdef STATS__TURN_ON_PROBES
313 struct timeval timeStamp;
314 gettimeofday( &(timeStamp), NULL);
315 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
316 _VMSMasterEnv->createPtInSecs;
317 #endif
318 //========================================================================
320 return newPr;
321 }
323 inline VirtProcr *
324 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
325 { VirtProcr *newPr;
326 char *stackLocs;
328 newPr = VMS__malloc( sizeof(VirtProcr) );
329 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
330 if( stackLocs == 0 )
331 { perror("VMS__malloc stack"); exit(1); }
333 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
334 }
336 /* "ext" designates that it's for use outside the VMS system -- should only
337 * be called from main thread or other thread -- never from code animated by
338 * a VMS virtual processor.
339 */
340 inline VirtProcr *
341 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
342 { VirtProcr *newPr;
343 char *stackLocs;
345 newPr = malloc( sizeof(VirtProcr) );
346 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
347 if( stackLocs == 0 )
348 { perror("malloc stack"); exit(1); }
350 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
351 }
354 /*Anticipating multi-tasking
355 */
356 void *
357 VMS__give_sem_env_for( VirtProcr *animPr )
358 {
359 return _VMSMasterEnv->semanticEnv;
360 }
361 //===========================================================================
362 /*there is a label inside this function -- save the addr of this label in
363 * the callingPr struc, as the pick-up point from which to start the next
364 * work-unit for that procr. If turns out have to save registers, then
365 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
366 * "done with work-unit" label. The procr struc is in the request in the
367 * slave that animated the just-ended work-unit, so all the state is saved
368 * there, and will get passed along, inside the request handler, to the
369 * next work-unit for that procr.
370 */
371 void
372 VMS__suspend_procr( VirtProcr *animatingPr )
373 {
375 //The request to master will cause this suspended virt procr to get
376 // scheduled again at some future point -- to resume, core loop jumps
377 // to the resume point (below), which causes restore of saved regs and
378 // "return" from this call.
379 animatingPr->nextInstrPt = &&ResumePt;
381 //return ownership of the virt procr and sched slot to Master virt pr
382 animatingPr->schedSlot->workIsDone = TRUE;
384 //=========================== Measurement stuff ========================
385 #ifdef MEAS__TIME_STAMP_SUSP
386 //record time stamp: compare to time-stamp recorded below
387 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
388 #endif
389 //=======================================================================
391 /* VirtProcr offsets:
392 * 0xc stackPtr
393 * 0x10 framePtr
394 * 0x14 nextInstrPt
395 * 0x1c coreLoopFramePtr
396 * 0x20 coreLoopStackPtr
397 *
398 * _VMSMasterEnv offsets:
399 * 0x24 coreLoopStartPt
400 * 0x28 coreLoopEndPt
401 * 0x30 masterLock
402 */
403 // SwitchToCoreLoop( animatingPr )
404 asm volatile("movl %0, %%ebx; \
405 movl %1, %%ecx; \
406 movl %%esp, 0x0c(%%ecx); \
407 movl %%ebp, 0x10(%%ecx); \
408 movl 0x24(%%ebx), %%eax; \
409 movl 0x20(%%ecx), %%esp; \
410 movl 0x1c(%%ecx), %%ebp; \
411 jmp %%eax" \
412 /* outputs */ : \
413 /* inputs */ : "g"(_VMSMasterEnv), "g"(animatingPr) \
414 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
415 );
417 // asm volatile("mov %0,%%ebx; \
418 mov %%ebx, %%eax; \
419 add $0xc, %%eax; \
420 movl %%esp, (%%eax); \
421 mov %%ebx, %%eax; \
422 add $0x10, %%eax; \
423 movl %%ebp, (%%eax); \
424 movl %1, %%eax; \
425 movl %2, %%esp; \
426 movl %3, %%ebp; \
427 jmp %%eax" \
428 /* outputs */ : \
429 /* inputs */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \
430 "g" (coreLoopFramePtr) \
431 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
432 );
434 //=======================================================================
435 ResumePt:
436 #ifdef MEAS__TIME_STAMP_SUSP
437 //NOTE: only take low part of count -- do sanity check when take diff
438 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
439 #endif
441 return;
442 }
446 /*For this implementation of VMS, it may not make much sense to have the
447 * system of requests for creating a new processor done this way.. but over
448 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
449 * distributed-memory, and so on, this gives VMS implementation a chance to
450 * do stuff before suspend, in the AppVP, and in the Master before the plugin
451 * is called, as well as in the lang-lib before this is called, and in the
452 * plugin. So, this gives both VMS and language implementations a chance to
453 * intercept at various points and do order-dependent stuff.
454 *Having a standard VMSNewPrReqData struc allows the language to create and
455 * free the struc, while VMS knows how to get the newPr if it wants it, and
456 * it lets the lang have lang-specific data related to creation transported
457 * to the plugin.
458 */
459 void
460 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
461 { VMSReqst req;
463 req.reqType = createReq;
464 req.semReqData = semReqData;
465 req.nextReqst = reqstingPr->requests;
466 reqstingPr->requests = &req;
468 VMS__suspend_procr( reqstingPr );
469 }
472 /*
473 *This adds a request to dissipate, then suspends the processor so that the
474 * request handler will receive the request. The request handler is what
475 * does the work of freeing memory and removing the processor from the
476 * semantic environment's data structures.
477 *The request handler also is what figures out when to shutdown the VMS
478 * system -- which causes all the core loop threads to die, and returns from
479 * the call that started up VMS to perform the work.
480 *
481 *This form is a bit misleading to understand if one is trying to figure out
482 * how VMS works -- it looks like a normal function call, but inside it
483 * sends a request to the request handler and suspends the processor, which
484 * jumps out of the VMS__dissipate_procr function, and out of all nestings
485 * above it, transferring the work of dissipating to the request handler,
486 * which then does the actual work -- causing the processor that animated
487 * the call of this function to disappear and the "hanging" state of this
488 * function to just poof into thin air -- the virtual processor's trace
489 * never returns from this call, but instead the virtual processor's trace
490 * gets suspended in this call and all the virt processor's state disap-
491 * pears -- making that suspend the last thing in the virt procr's trace.
492 */
493 void
494 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
495 { VMSReqst req;
497 req.reqType = dissipate;
498 req.nextReqst = procrToDissipate->requests;
499 procrToDissipate->requests = &req;
501 VMS__suspend_procr( procrToDissipate );
502 }
505 /* "ext" designates that it's for use outside the VMS system -- should only
506 * be called from main thread or other thread -- never from code animated by
507 * a VMS virtual processor.
508 *
509 *Use this version to dissipate VPs created outside the VMS system.
510 */
511 void
512 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
513 {
514 //NOTE: initialData was given to the processor, so should either have
515 // been alloc'd with VMS__malloc, or freed by the level above animPr.
516 //So, all that's left to free here is the stack and the VirtProcr struc
517 // itself
518 //Note, should not stack-allocate initial data -- no guarantee, in
519 // general that creating processor will outlive ones it creates.
520 free( procrToDissipate->startOfStack );
521 free( procrToDissipate );
522 }
526 /*This call's name indicates that request is malloc'd -- so req handler
527 * has to free any extra requests tacked on before a send, using this.
528 *
529 * This inserts the semantic-layer's request data into standard VMS carrier
530 * request data-struct that is mallocd. The sem request doesn't need to
531 * be malloc'd if this is called inside the same call chain before the
532 * send of the last request is called.
533 *
534 *The request handler has to call VMS__free_VMSReq for any of these
535 */
536 inline void
537 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
538 VirtProcr *callingPr )
539 { VMSReqst *req;
541 req = VMS__malloc( sizeof(VMSReqst) );
542 req->reqType = semantic;
543 req->semReqData = semReqData;
544 req->nextReqst = callingPr->requests;
545 callingPr->requests = req;
546 }
548 /*This inserts the semantic-layer's request data into standard VMS carrier
549 * request data-struct is allocated on stack of this call & ptr to it sent
550 * to plugin
551 *Then it does suspend, to cause request to be sent.
552 */
553 inline void
554 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
555 { VMSReqst req;
557 req.reqType = semantic;
558 req.semReqData = semReqData;
559 req.nextReqst = callingPr->requests;
560 callingPr->requests = &req;
562 VMS__suspend_procr( callingPr );
563 }
566 inline void
567 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
568 { VMSReqst req;
570 req.reqType = VMSSemantic;
571 req.semReqData = semReqData;
572 req.nextReqst = callingPr->requests; //gab any other preceeding
573 callingPr->requests = &req;
575 VMS__suspend_procr( callingPr );
576 }
579 /*
580 */
581 VMSReqst *
582 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
583 { VMSReqst *req;
585 req = procrWithReq->requests;
586 if( req == NULL ) return NULL;
588 procrWithReq->requests = procrWithReq->requests->nextReqst;
589 return req;
590 }
593 inline void *
594 VMS__take_sem_reqst_from( VMSReqst *req )
595 {
596 return req->semReqData;
597 }
601 /* This is for OS requests and VMS infrastructure requests, such as to create
602 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
603 * language -- but it's also a semantic thing that's triggered from and used
604 * in the application.. so it crosses abstractions.. so, need some special
605 * pattern here for handling such requests.
606 * Doing this just like it were a second language sharing VMS-core.
607 *
608 * This is called from the language's request handler when it sees a request
609 * of type VMSSemReq
610 *
611 * TODO: Later change this, to give probes their own separate plugin & have
612 * VMS-core steer the request to appropriate plugin
613 * Do the same for OS calls -- look later at it..
614 */
615 void inline
616 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
617 ResumePrFnPtr resumePrFnPtr )
618 { VMSSemReq *semReq;
619 IntervalProbe *newProbe;
620 int32 nameLen;
622 semReq = req->semReqData;
624 newProbe = VMS__malloc( sizeof(IntervalProbe) );
625 newProbe->nameStr = VMS__strDup( semReq->nameStr );
626 newProbe->hist = NULL;
627 newProbe->schedChoiceWasRecorded = FALSE;
629 //This runs in masterVP, so no race-condition worries
630 newProbe->probeID =
631 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
633 requestingPr->dataRetFromReq = newProbe;
635 (*resumePrFnPtr)( requestingPr, semEnv );
636 }
640 /*This must be called by the request handler plugin -- it cannot be called
641 * from the semantic library "dissipate processor" function -- instead, the
642 * semantic layer has to generate a request, and the plug-in calls this
643 * function.
644 *The reason is that this frees the virtual processor's stack -- which is
645 * still in use inside semantic library calls!
646 *
647 *This frees or recycles all the state owned by and comprising the VMS
648 * portion of the animating virtual procr. The request handler must first
649 * free any semantic data created for the processor that didn't use the
650 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
651 * system to disown any state that did use VMS_malloc, and then frees the
652 * statck and the processor-struct itself.
653 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
654 * state, then that state gets freed (or sent to recycling) as a side-effect
655 * of dis-owning it.
656 */
657 void
658 VMS__dissipate_procr( VirtProcr *animatingPr )
659 {
660 //dis-own all locations owned by this processor, causing to be freed
661 // any locations that it is (was) sole owner of
662 //TODO: implement VMS__malloc system, including "give up ownership"
665 //NOTE: initialData was given to the processor, so should either have
666 // been alloc'd with VMS__malloc, or freed by the level above animPr.
667 //So, all that's left to free here is the stack and the VirtProcr struc
668 // itself
669 //Note, should not stack-allocate initial data -- no guarantee, in
670 // general that creating processor will outlive ones it creates.
671 VMS__free( animatingPr->startOfStack );
672 VMS__free( animatingPr );
673 }
676 //TODO: look at architecting cleanest separation between request handler
677 // and master loop, for dissipate, create, shutdown, and other non-semantic
678 // requests. Issue is chain: one removes requests from AppVP, one dispatches
679 // on type of request, and one handles each type.. but some types require
680 // action from both request handler and master loop -- maybe just give the
681 // request handler calls like: VMS__handle_X_request_type
684 /*This is called by the semantic layer's request handler when it decides its
685 * time to shut down the VMS system. Calling this causes the core loop OS
686 * threads to exit, which unblocks the entry-point function that started up
687 * VMS, and allows it to grab the result and return to the original single-
688 * threaded application.
689 *
690 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
691 * and-wait function has to free a bunch of stuff after it detects the
692 * threads have all died: the masterEnv, the thread-related locations,
693 * masterVP any AppVPs that might still be allocated and sitting in the
694 * semantic environment, or have been orphaned in the _VMSWorkQ.
695 *
696 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
697 * locations it needs, and give ownership to masterVP. Then, they will be
698 * automatically freed.
699 *
700 *In here,create one core-loop shut-down processor for each core loop and put
701 * them all directly into the readyToAnimateQ.
702 *Note, this function can ONLY be called after the semantic environment no
703 * longer cares if AppVPs get animated after the point this is called. In
704 * other words, this can be used as an abort, or else it should only be
705 * called when all AppVPs have finished dissipate requests -- only at that
706 * point is it sure that all results have completed.
707 */
708 void
709 VMS__shutdown()
710 { int coreIdx;
711 VirtProcr *shutDownPr;
713 //create the shutdown processors, one for each core loop -- put them
714 // directly into the Q -- each core will die when gets one
715 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
716 { //Note, this is running in the master
717 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
718 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
719 }
721 }
724 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
725 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
726 *This function has the sole purpose of setting the stack and framePtr
727 * to the coreLoop's stack and framePtr.. it does that then jumps to the
728 * core loop's shutdown point -- might be able to just call Pthread_exit
729 * from here, but am going back to the pthread's stack and setting everything
730 * up just as if it never jumped out, before calling pthread_exit.
731 *The end-point of core loop will free the stack and so forth of the
732 * processor that animates this function, (this fn is transfering the
733 * animator of the AppVP that is in turn animating this function over
734 * to core loop function -- note that this slices out a level of virtual
735 * processors).
736 */
737 void
738 endOSThreadFn( void *initData, VirtProcr *animatingPr )
739 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
741 jmpPt = _VMSMasterEnv->coreLoopEndPt;
742 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
743 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
746 asm volatile("movl %0, %%eax; \
747 movl %1, %%esp; \
748 movl %2, %%ebp; \
749 jmp %%eax " \
750 /* outputs */ : \
751 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
752 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
753 );
754 }
757 /*This is called from the startup & shutdown
758 */
759 void
760 VMS__cleanup_at_end_of_shutdown()
761 {
762 VMSQueueStruc **readyToAnimateQs;
763 int coreIdx;
764 VirtProcr **masterVPs;
765 SchedSlot ***allSchedSlots; //ptr to array of ptrs
767 //Before getting rid of everything, print out any measurements made
768 #ifdef MEAS__TIME_PLUGIN
769 printHist( _VMSMasterEnv->pluginLowTimeHist );
770 printHist( _VMSMasterEnv->pluginHighTimeHist );
771 #endif
772 #ifdef MEAS__TIME_MALLOC
773 printHist( _VMSMasterEnv->mallocTimeHist );
774 printHist( _VMSMasterEnv->freeTimeHist );
775 freeHistExt( _VMSMasterEnv->mallocTimeHist );
776 freeHistExt( _VMSMasterEnv->freeTimeHist );
777 #endif
778 #ifdef MEAS__TIME_MASTER_LOCK
779 // printHist( _VMSMasterEnv->masterLockLowTimeHist );
780 printHist( _VMSMasterEnv->masterLockHighTimeHist );
781 #endif
782 #ifdef MEAS__TIME_MASTER
783 printHist( _VMSMasterEnv->pluginTimeHist );
784 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
785 {
786 freeVMSQ( readyToAnimateQs[ coreIdx ] );
787 //master VPs were created external to VMS, so use external free
788 VMS__dissipate_procr( masterVPs[ coreIdx ] );
790 freeSchedSlots( allSchedSlots[ coreIdx ] );
791 }
792 #endif
793 #ifdef MEAS__TIME_STAMP_SUSP
794 printHist( _VMSMasterEnv->pluginTimeHist );
795 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
796 {
797 freeVMSQ( readyToAnimateQs[ coreIdx ] );
798 //master VPs were created external to VMS, so use external free
799 VMS__dissipate_procr( masterVPs[ coreIdx ] );
801 freeSchedSlots( allSchedSlots[ coreIdx ] );
802 }
803 #endif
805 //All the environment data has been allocated with VMS__malloc, so just
806 // free its internal big-chunk and all inside it disappear.
807 /*
808 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
809 masterVPs = _VMSMasterEnv->masterVPs;
810 allSchedSlots = _VMSMasterEnv->allSchedSlots;
812 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
813 {
814 freeVMSQ( readyToAnimateQs[ coreIdx ] );
815 //master VPs were created external to VMS, so use external free
816 VMS__dissipate_procr( masterVPs[ coreIdx ] );
818 freeSchedSlots( allSchedSlots[ coreIdx ] );
819 }
821 VMS__free( _VMSMasterEnv->readyToAnimateQs );
822 VMS__free( _VMSMasterEnv->masterVPs );
823 VMS__free( _VMSMasterEnv->allSchedSlots );
825 //============================= MEASUREMENT STUFF ========================
826 #ifdef STATS__TURN_ON_PROBES
827 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
828 #endif
829 //========================================================================
830 */
831 //These are the only two that use system free
832 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
833 free( (void *)_VMSMasterEnv );
834 }
837 //================================
840 /*Later, improve this -- for now, just exits the application after printing
841 * the error message.
842 */
843 void
844 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
845 {
846 printf(msgStr);
847 fflush(stdin);
848 exit(1);
849 }
