view VMS.c @ 48:054006c26b92

Added instrumentation to measure master time, master lock time and create time
author Me
date Tue, 26 Oct 2010 18:18:30 -0700
parents 72373405c816
children 984f7d78bfdf
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <malloc.h>
11 #include "VMS.h"
12 #include "Queue_impl/BlockingQueue.h"
13 #include "Histogram/Histogram.h"
16 #define thdAttrs NULL
18 //===========================================================================
19 void
20 shutdownFn( void *dummy, VirtProcr *dummy2 );
22 SchedSlot **
23 create_sched_slots();
25 void
26 create_masterEnv();
28 void
29 create_the_coreLoop_OS_threads();
31 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
32 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
34 //===========================================================================
36 /*Setup has two phases:
37 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
38 * the master virt procr into the work-queue, ready for first "call"
39 * 2) Semantic layer then does its own init, which creates the seed virt
40 * procr inside the semantic layer, ready to schedule it when
41 * asked by the first run of the masterLoop.
42 *
43 *This part is bit weird because VMS really wants to be "always there", and
44 * have applications attach and detach.. for now, this VMS is part of
45 * the app, so the VMS system starts up as part of running the app.
46 *
47 *The semantic layer is isolated from the VMS internals by making the
48 * semantic layer do setup to a state that it's ready with its
49 * initial virt procrs, ready to schedule them to slots when the masterLoop
50 * asks. Without this pattern, the semantic layer's setup would
51 * have to modify slots directly to assign the initial virt-procrs, and put
52 * them into the readyToAnimateQ itself, breaking the isolation completely.
53 *
54 *
55 *The semantic layer creates the initial virt procr(s), and adds its
56 * own environment to masterEnv, and fills in the pointers to
57 * the requestHandler and slaveScheduler plug-in functions
58 */
60 /*This allocates VMS data structures, populates the master VMSProc,
61 * and master environment, and returns the master environment to the semantic
62 * layer.
63 */
64 void
65 VMS__init()
66 {
67 create_masterEnv();
68 create_the_coreLoop_OS_threads();
69 }
71 /*To initialize the sequential version, just don't create the threads
72 */
73 void
74 VMS__init_Seq()
75 {
76 create_masterEnv();
77 }
79 void
80 create_masterEnv()
81 { MasterEnv *masterEnv;
82 VMSQueueStruc **readyToAnimateQs;
83 int coreIdx;
84 VirtProcr **masterVPs;
85 SchedSlot ***allSchedSlots; //ptr to array of ptrs
87 //Make the master env, which holds everything else
88 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
89 masterEnv = _VMSMasterEnv;
90 //Need to set start pt here 'cause used by seed procr, which is created
91 // before the first core loop starts up. -- not sure how yet..
92 // masterEnv->coreLoopStartPt = ;
93 // masterEnv->coreLoopEndPt = ;
95 //Make a readyToAnimateQ for each core loop
96 readyToAnimateQs = malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
97 masterVPs = malloc( NUM_CORES * sizeof(VirtProcr *) );
99 //One array for each core, 3 in array, core's masterVP scheds all
100 allSchedSlots = malloc( NUM_CORES * sizeof(SchedSlot *) );
102 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
103 {
104 readyToAnimateQs[ coreIdx ] = makeSRSWQ();
106 //Q: should give masterVP core-specific into as its init data?
107 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
108 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
109 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
110 }
111 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
112 _VMSMasterEnv->masterVPs = masterVPs;
113 _VMSMasterEnv->allSchedSlots = allSchedSlots;
115 //============================= MEASUREMENT STUFF ========================
116 #ifdef MEAS__TIME_MASTER
118 _VMSMasterEnv->stats->masterTimeHist = makeHistogram( 25, 500, 800 );
119 _VMSMasterEnv->stats->masterLockHist = makeHistogram( 25, 0, 100000 );
120 _VMSMasterEnv->stats->createHist = makeHistogram( 25, 0, 5000 );
121 #endif
122 //========================================================================
124 //Aug 19, 2010: no longer need to place initial masterVP into queue
125 // because coreLoop now controls -- animates its masterVP when no work
128 //==================== malloc substitute ========================
129 //
130 //Testing whether malloc is using thread-local storage and therefore
131 // causing unreliable behavior.
132 //Just allocate a massive chunk of memory and roll own malloc/free and
133 // make app use VMS__malloc_to, which will suspend and perform malloc
134 // in the master, taking from this massive chunk.
136 // initFreeList();
138 }
140 /*
141 void
142 initMasterMalloc()
143 {
144 _VMSMasterEnv->mallocChunk = malloc( MASSIVE_MALLOC_SIZE );
146 //The free-list element is the first several locations of an
147 // allocated chunk -- the address given to the application is pre-
148 // pended with both the ownership structure and the free-list struc.
149 //So, write the values of these into the first locations of
150 // mallocChunk -- which marks it as free & puts in its size.
151 listElem = (FreeListElem *)_VMSMasterEnv->mallocChunk;
152 listElem->size = MASSIVE_MALLOC_SIZE - NUM_PREPEND_BYTES
153 listElem->next = NULL;
154 }
156 void
157 dissipateMasterMalloc()
158 {
159 //Just foo code -- to get going -- doing as if free list were link-list
160 currElem = _VMSMasterEnv->freeList;
161 while( currElem != NULL )
162 {
163 nextElem = currElem->next;
164 masterFree( currElem );
165 currElem = nextElem;
166 }
167 free( _VMSMasterEnv->freeList );
168 }
169 */
171 SchedSlot **
172 create_sched_slots()
173 { SchedSlot **schedSlots;
174 int i;
176 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
178 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
179 {
180 schedSlots[i] = malloc( sizeof(SchedSlot) );
182 //Set state to mean "handling requests done, slot needs filling"
183 schedSlots[i]->workIsDone = FALSE;
184 schedSlots[i]->needsProcrAssigned = TRUE;
185 }
186 return schedSlots;
187 }
190 void
191 freeSchedSlots( SchedSlot **schedSlots )
192 { int i;
193 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
194 {
195 free( schedSlots[i] );
196 }
197 free( schedSlots );
198 }
201 void
202 create_the_coreLoop_OS_threads()
203 {
204 //========================================================================
205 // Create the Threads
206 int coreIdx, retCode, i;
208 //create the arrays used to measure TSC offsets between cores
209 pongNums = malloc( NUM_CORES * sizeof( int ) );
210 pingTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
211 pongTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
213 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
214 {
215 pongNums[ coreIdx ] = 0;
216 for( i = 0; i < NUM_TSC_ROUND_TRIPS; i++ )
217 {
218 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
219 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
220 }
221 }
223 //Need the threads to be created suspended, and wait for a signal
224 // before proceeding -- gives time after creating to initialize other
225 // stuff before the coreLoops set off.
226 _VMSMasterEnv->setupComplete = 0;
228 //Make the threads that animate the core loops
229 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
230 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
231 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
233 retCode =
234 pthread_create( &(coreLoopThdHandles[coreIdx]),
235 thdAttrs,
236 &coreLoop,
237 (void *)(coreLoopThdParams[coreIdx]) );
238 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
239 }
240 }
242 /*Semantic layer calls this when it want the system to start running..
243 *
244 *This starts the core loops running then waits for them to exit.
245 */
246 void
247 VMS__start_the_work_then_wait_until_done()
248 { int coreIdx;
249 //Start the core loops running
250 //===========================================================================
251 TSCount startCount, endCount;
252 unsigned long long count = 0, freq = 0;
253 double runTime;
255 startCount = getTSC();
257 //tell the core loop threads that setup is complete
258 //get lock, to lock out any threads still starting up -- they'll see
259 // that setupComplete is true before entering while loop, and so never
260 // wait on the condition
261 pthread_mutex_lock( &suspendLock );
262 _VMSMasterEnv->setupComplete = 1;
263 pthread_mutex_unlock( &suspendLock );
264 pthread_cond_broadcast( &suspend_cond );
267 //wait for all to complete
268 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
269 {
270 pthread_join( coreLoopThdHandles[coreIdx], NULL );
271 }
273 //NOTE: do not clean up VMS env here -- semantic layer has to have
274 // a chance to clean up its environment first, then do a call to free
275 // the Master env and rest of VMS locations
278 endCount = getTSC();
279 count = endCount - startCount;
281 runTime = (double)count / (double)TSCOUNT_FREQ;
283 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
284 }
286 /*Only difference between version with an OS thread pinned to each core and
287 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
288 */
289 void
290 VMS__start_the_work_then_wait_until_done_Seq()
291 {
292 //Instead of un-suspending threads, just call the one and only
293 // core loop (sequential version), in the main thread.
294 coreLoop_Seq( NULL );
296 }
300 /*Create stack, then create __cdecl structure on it and put initialData and
301 * pointer to the new structure instance into the parameter positions on
302 * the stack
303 *Then put function pointer into nextInstrPt -- the stack is setup in std
304 * call structure, so jumping to function ptr is same as a GCC generated
305 * function call
306 *No need to save registers on old stack frame, because there's no old
307 * animator state to return to --
308 *
309 */
310 VirtProcr *
311 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
312 { VirtProcr *newPr;
313 char *stackLocs, *stackPtr;
315 //============================= MEASUREMENT STUFF ========================
316 #ifdef MEAS__TIME_MASTER
317 int32 startStamp;
318 saveLowTimeStampCountInto( startStamp );
319 #endif
320 //========================================================================
322 newPr = malloc( sizeof(VirtProcr) );
323 newPr->procrID = numProcrsCreated++;
324 newPr->nextInstrPt = fnPtr;
325 newPr->initialData = initialData;
326 newPr->requests = NULL;
327 newPr->schedSlot = NULL;
328 // newPr->coreLoopStartPt = _VMSMasterEnv->coreLoopStartPt;
330 //fnPtr takes two params -- void *initData & void *animProcr
331 //alloc stack locations, make stackPtr be the highest addr minus room
332 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
333 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
334 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
335 if(stackLocs == 0) {perror("error: malloc stack"); exit(1);}
336 newPr->startOfStack = stackLocs;
337 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
338 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
339 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
340 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
341 newPr->stackPtr = stackPtr; //core loop will switch to this, then
342 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
344 //============================= MEASUREMENT STUFF ========================
345 #ifdef MEAS__TIME_MASTER
346 int32 endStamp;
347 saveLowTimeStampCountInto( endStamp );
348 addIntervalToHist( startStamp, endStamp,
349 _VMSMasterEnv->stats->createHist );
350 #endif
351 //========================================================================
353 return newPr;
354 }
357 /*there is a label inside this function -- save the addr of this label in
358 * the callingPr struc, as the pick-up point from which to start the next
359 * work-unit for that procr. If turns out have to save registers, then
360 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
361 * "done with work-unit" label. The procr struc is in the request in the
362 * slave that animated the just-ended work-unit, so all the state is saved
363 * there, and will get passed along, inside the request handler, to the
364 * next work-unit for that procr.
365 */
366 void
367 VMS__suspend_procr( VirtProcr *animatingPr )
368 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
369 void *coreLoopFramePtr;
371 //The request to master will cause this suspended virt procr to get
372 // scheduled again at some future point -- to resume, core loop jumps
373 // to the resume point (below), which causes restore of saved regs and
374 // "return" from this call.
375 animatingPr->nextInstrPt = &&ResumePt;
377 //return ownership of the virt procr and sched slot to Master virt pr
378 animatingPr->schedSlot->workIsDone = TRUE;
379 // coreIdx = callingPr->coreAnimatedBy;
381 stackPtrAddr = &(animatingPr->stackPtr);
382 framePtrAddr = &(animatingPr->framePtr);
384 jmpPt = _VMSMasterEnv->coreLoopStartPt;
385 coreLoopFramePtr = animatingPr->coreLoopFramePtr;//need this only
386 coreLoopStackPtr = animatingPr->coreLoopStackPtr;//safety
388 //Save the virt procr's stack and frame ptrs,
389 asm volatile("movl %0, %%eax; \
390 movl %%esp, (%%eax); \
391 movl %1, %%eax; \
392 movl %%ebp, (%%eax) "\
393 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
394 /* inputs */ : \
395 /* clobber */ : "%eax" \
396 );
398 //=========================== Measurement stuff ========================
399 #ifdef MEAS__TIME_STAMP_SUSP
400 //record time stamp: compare to time-stamp recorded below
401 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
402 #endif
403 //=======================================================================
405 //restore coreloop's frame ptr, then jump back to "start" of core loop
406 //Note, GCC compiles to assembly that saves esp and ebp in the stack
407 // frame -- so have to explicitly do assembly that saves to memory
408 asm volatile("movl %0, %%eax; \
409 movl %1, %%esp; \
410 movl %2, %%ebp; \
411 jmp %%eax " \
412 /* outputs */ : \
413 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
414 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
415 ); //list everything as clobbered to force GCC to save all
416 // live vars that are in regs on stack before this
417 // assembly, so that stack pointer is correct, before jmp
419 ResumePt:
420 #ifdef MEAS__TIME_STAMP_SUSP
421 //NOTE: only take low part of count -- do sanity check when take diff
422 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
423 #endif
425 return;
426 }
431 /*
432 *This adds a request to dissipate, then suspends the processor so that the
433 * request handler will receive the request. The request handler is what
434 * does the work of freeing memory and removing the processor from the
435 * semantic environment's data structures.
436 *The request handler also is what figures out when to shutdown the VMS
437 * system -- which causes all the core loop threads to die, and returns from
438 * the call that started up VMS to perform the work.
439 *
440 *This form is a bit misleading to understand if one is trying to figure out
441 * how VMS works -- it looks like a normal function call, but inside it
442 * sends a request to the request handler and suspends the processor, which
443 * jumps out of the VMS__dissipate_procr function, and out of all nestings
444 * above it, transferring the work of dissipating to the request handler,
445 * which then does the actual work -- causing the processor that animated
446 * the call of this function to disappear and the "hanging" state of this
447 * function to just poof into thin air -- the virtual processor's trace
448 * never returns from this call, but instead the virtual processor's trace
449 * gets suspended in this call and all the virt processor's state disap-
450 * pears -- making that suspend the last thing in the virt procr's trace.
451 */
452 void
453 VMS__dissipate_procr( VirtProcr *procrToDissipate )
454 { VMSReqst *req;
456 req = malloc( sizeof(VMSReqst) );
457 // req->virtProcrFrom = callingPr;
458 req->reqType = dissipate;
459 req->nextReqst = procrToDissipate->requests;
460 procrToDissipate->requests = req;
462 VMS__suspend_procr( procrToDissipate );
463 }
466 /*This inserts the semantic-layer's request data into standard VMS carrier
467 */
468 inline void
469 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
470 { VMSReqst *req;
472 req = malloc( sizeof(VMSReqst) );
473 // req->virtProcrFrom = callingPr;
474 req->reqType = semantic;
475 req->semReqData = semReqData;
476 req->nextReqst = callingPr->requests;
477 callingPr->requests = req;
478 }
481 /*Use this to get first request before starting request handler's loop
482 */
483 VMSReqst *
484 VMS__take_top_request_from( VirtProcr *procrWithReq )
485 { VMSReqst *req;
487 req = procrWithReq->requests;
488 if( req == NULL ) return req;
490 procrWithReq->requests = procrWithReq->requests->nextReqst;
491 return req;
492 }
494 /*A subtle bug due to freeing then accessing "next" after freed caused this
495 * form of call to be put in -- so call this at end of request handler loop
496 * that iterates through the requests.
497 */
498 VMSReqst *
499 VMS__free_top_and_give_next_request_from( VirtProcr *procrWithReq )
500 { VMSReqst *req;
502 req = procrWithReq->requests;
503 if( req == NULL ) return NULL;
505 procrWithReq->requests = procrWithReq->requests->nextReqst;
506 VMS__free_request( req );
507 return procrWithReq->requests;
508 }
511 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
512 // of a request -- IE call with both a virt procr and a fn-ptr to request
513 // freer (also maybe put sem request freer as a field in virt procr?)
514 //MeasVMS relies right now on this only freeing VMS layer of request -- the
515 // semantic portion of request is alloc'd and freed by request handler
516 void
517 VMS__free_request( VMSReqst *req )
518 {
519 free( req );
520 }
524 inline int
525 VMS__isSemanticReqst( VMSReqst *req )
526 {
527 return ( req->reqType == semantic );
528 }
531 inline void *
532 VMS__take_sem_reqst_from( VMSReqst *req )
533 {
534 return req->semReqData;
535 }
537 inline int
538 VMS__isDissipateReqst( VMSReqst *req )
539 {
540 return ( req->reqType == dissipate );
541 }
543 inline int
544 VMS__isCreateReqst( VMSReqst *req )
545 {
546 return ( req->reqType == regCreated );
547 }
549 void
550 VMS__send_req_to_register_new_procr(VirtProcr *newPr, VirtProcr *reqstingPr)
551 { VMSReqst *req;
553 req = malloc( sizeof(VMSReqst) );
554 req->reqType = regCreated;
555 req->semReqData = newPr;
556 req->nextReqst = reqstingPr->requests;
557 reqstingPr->requests = req;
559 VMS__suspend_procr( reqstingPr );
560 }
564 /*This must be called by the request handler plugin -- it cannot be called
565 * from the semantic library "dissipate processor" function -- instead, the
566 * semantic layer has to generate a request for the plug-in to call this
567 * function.
568 *The reason is that this frees the virtual processor's stack -- which is
569 * still in use inside semantic library calls!
570 *
571 *This frees or recycles all the state owned by and comprising the VMS
572 * portion of the animating virtual procr. The request handler must first
573 * free any semantic data created for the processor that didn't use the
574 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
575 * system to disown any state that did use VMS_malloc, and then frees the
576 * statck and the processor-struct itself.
577 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
578 * state, then that state gets freed (or sent to recycling) as a side-effect
579 * of dis-owning it.
580 */
581 void
582 VMS__handle_dissipate_reqst( VirtProcr *animatingPr )
583 {
584 //dis-own all locations owned by this processor, causing to be freed
585 // any locations that it is (was) sole owner of
586 //TODO: implement VMS__malloc system, including "give up ownership"
588 //The dissipate request might still be attached, so remove and free it
589 VMS__free_top_and_give_next_request_from( animatingPr );
591 //NOTE: initialData was given to the processor, so should either have
592 // been alloc'd with VMS__malloc, or freed by the level above animPr.
593 //So, all that's left to free here is the stack and the VirtProcr struc
594 // itself
595 free( animatingPr->startOfStack );
596 free( animatingPr );
597 }
600 //TODO: re-architect so that have clean separation between request handler
601 // and master loop, for dissipate, create, shutdown, and other non-semantic
602 // requests. Issue is chain: one removes requests from AppVP, one dispatches
603 // on type of request, and one handles each type.. but some types require
604 // action from both request handler and master loop -- maybe just give the
605 // request handler calls like: VMS__handle_X_request_type
607 void
608 endOSThreadFn( void *initData, VirtProcr *animatingPr );
610 /*This is called by the semantic layer's request handler when it decides its
611 * time to shut down the VMS system. Calling this causes the core loop OS
612 * threads to exit, which unblocks the entry-point function that started up
613 * VMS, and allows it to grab the result and return to the original single-
614 * threaded application.
615 *
616 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
617 * and-wait function has to free a bunch of stuff after it detects the
618 * threads have all died: the masterEnv, the thread-related locations,
619 * masterVP any AppVPs that might still be allocated and sitting in the
620 * semantic environment, or have been orphaned in the _VMSWorkQ.
621 *
622 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
623 * locations it needs, and give ownership to masterVP. Then, they will be
624 * automatically freed when the masterVP is dissipated. (This happens after
625 * the core loop threads have all exited)
626 *
627 *In here,create one core-loop shut-down processor for each core loop and put
628 * them all directly into the readyToAnimateQ.
629 *Note, this function can ONLY be called after the semantic environment no
630 * longer cares if AppVPs get animated after the point this is called. In
631 * other words, this can be used as an abort, or else it should only be
632 * called when all AppVPs have finished dissipate requests -- only at that
633 * point is it sure that all results have completed.
634 */
635 void
636 VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
637 { int coreIdx;
638 VirtProcr *shutDownPr;
640 //create the shutdown processors, one for each core loop -- put them
641 // directly into the Q -- each core will die when gets one
642 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
643 {
644 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
645 writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
646 }
648 }
651 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
652 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
653 *This function has the sole purpose of setting the stack and framePtr
654 * to the coreLoop's stack and framePtr.. it does that then jumps to the
655 * core loop's shutdown point -- might be able to just call Pthread_exit
656 * from here, but am going back to the pthread's stack and setting everything
657 * up just as if it never jumped out, before calling pthread_exit.
658 *The end-point of core loop will free the stack and so forth of the
659 * processor that animates this function, (this fn is transfering the
660 * animator of the AppVP that is in turn animating this function over
661 * to core loop function -- note that this slices out a level of virtual
662 * processors).
663 */
664 void
665 endOSThreadFn( void *initData, VirtProcr *animatingPr )
666 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
668 jmpPt = _VMSMasterEnv->coreLoopEndPt;
669 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
670 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
673 asm volatile("movl %0, %%eax; \
674 movl %1, %%esp; \
675 movl %2, %%ebp; \
676 jmp %%eax " \
677 /* outputs */ : \
678 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
679 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
680 );
681 }
684 /*This is called after the threads have shut down and control has returned
685 * to the semantic layer, in the entry point function in the main thread.
686 * It has to free anything allocated during VMS_init, and any other alloc'd
687 * locations that might be left over.
688 */
689 void
690 VMS__cleanup_after_shutdown()
691 {
692 VMSQueueStruc **readyToAnimateQs;
693 int coreIdx;
694 VirtProcr **masterVPs;
695 SchedSlot ***allSchedSlots; //ptr to array of ptrs
697 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
698 masterVPs = _VMSMasterEnv->masterVPs;
699 allSchedSlots = _VMSMasterEnv->allSchedSlots;
701 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
702 {
703 freeSRSWQ( readyToAnimateQs[ coreIdx ] );
705 VMS__handle_dissipate_reqst( masterVPs[ coreIdx ] );
707 freeSchedSlots( allSchedSlots[ coreIdx ] );
708 }
710 free( _VMSMasterEnv->readyToAnimateQs );
711 free( _VMSMasterEnv->masterVPs );
712 free( _VMSMasterEnv->allSchedSlots );
714 free( _VMSMasterEnv );
715 }
718 //===========================================================================
720 inline TSCount getTSC()
721 { unsigned int low, high;
722 TSCount out;
724 saveTimeStampCountInto( low, high );
725 out = high;
726 out = (out << 32) + low;
727 return out;
728 }