annotate VMS.c @ 40:1df8d7f2c9b1

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