annotate VMS.c @ 41:cf3e9238aeb0

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