annotate VMS.c @ 38:17d20e5cf924

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