annotate VMS.c @ 61:984f7d78bfdf

Merge See what happens -- merged test stuff into Nov 8 VMS version
author SeanHalle
date Thu, 11 Nov 2010 06:19:51 -0800
parents 054006c26b92 26d53313a8f2
children
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@50 9 #include <string.h>
Me@0 10 #include <malloc.h>
Me@50 11 #include <sys/time.h>
Me@0 12
Me@0 13 #include "VMS.h"
Me@0 14 #include "Queue_impl/BlockingQueue.h"
Me@38 15 #include "Histogram/Histogram.h"
Me@0 16
Me@0 17
Me@26 18 #define thdAttrs NULL
Me@26 19
Me@22 20 //===========================================================================
Me@22 21 void
Me@22 22 shutdownFn( void *dummy, VirtProcr *dummy2 );
Me@22 23
Me@31 24 SchedSlot **
Me@31 25 create_sched_slots();
Me@22 26
Me@28 27 void
Me@28 28 create_masterEnv();
Me@28 29
Me@28 30 void
Me@28 31 create_the_coreLoop_OS_threads();
Me@28 32
Me@50 33 MallocProlog *
Me@50 34 create_free_list();
Me@50 35
Me@53 36 void
Me@53 37 endOSThreadFn( void *initData, VirtProcr *animatingPr );
Me@50 38
Me@26 39 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
Me@26 40 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
Me@26 41
Me@22 42 //===========================================================================
Me@22 43
Me@0 44 /*Setup has two phases:
Me@0 45 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
Me@8 46 * the master virt procr into the work-queue, ready for first "call"
Me@8 47 * 2) Semantic layer then does its own init, which creates the seed virt
Me@8 48 * procr inside the semantic layer, ready to schedule it when
Me@0 49 * asked by the first run of the masterLoop.
Me@0 50 *
Me@0 51 *This part is bit weird because VMS really wants to be "always there", and
Me@0 52 * have applications attach and detach.. for now, this VMS is part of
Me@0 53 * the app, so the VMS system starts up as part of running the app.
Me@0 54 *
Me@8 55 *The semantic layer is isolated from the VMS internals by making the
Me@8 56 * semantic layer do setup to a state that it's ready with its
Me@8 57 * initial virt procrs, ready to schedule them to slots when the masterLoop
Me@0 58 * asks. Without this pattern, the semantic layer's setup would
Me@8 59 * have to modify slots directly to assign the initial virt-procrs, and put
Me@31 60 * them into the readyToAnimateQ itself, breaking the isolation completely.
Me@0 61 *
Me@0 62 *
Me@8 63 *The semantic layer creates the initial virt procr(s), and adds its
Me@8 64 * own environment to masterEnv, and fills in the pointers to
Me@0 65 * the requestHandler and slaveScheduler plug-in functions
Me@8 66 */
Me@8 67
Me@8 68 /*This allocates VMS data structures, populates the master VMSProc,
Me@0 69 * and master environment, and returns the master environment to the semantic
Me@0 70 * layer.
Me@0 71 */
Me@8 72 void
Me@8 73 VMS__init()
Me@28 74 {
Me@28 75 create_masterEnv();
Me@28 76 create_the_coreLoop_OS_threads();
Me@28 77 }
Me@28 78
Me@28 79 /*To initialize the sequential version, just don't create the threads
Me@28 80 */
Me@28 81 void
Me@28 82 VMS__init_Seq()
Me@28 83 {
Me@28 84 create_masterEnv();
Me@28 85 }
Me@28 86
Me@28 87 void
Me@28 88 create_masterEnv()
Me@31 89 { MasterEnv *masterEnv;
Me@55 90 VMSQueueStruc **readyToAnimateQs;
Me@31 91 int coreIdx;
Me@31 92 VirtProcr **masterVPs;
Me@31 93 SchedSlot ***allSchedSlots; //ptr to array of ptrs
Me@53 94
Me@53 95
Me@31 96 //Make the master env, which holds everything else
Me@1 97 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
Me@53 98
Me@53 99 //Very first thing put into the master env is the free-list, seeded
Me@53 100 // with a massive initial chunk of memory.
Me@53 101 //After this, all other mallocs are VMS__malloc.
Me@53 102 _VMSMasterEnv->freeListHead = VMS_ext__create_free_list();
Me@53 103
Me@53 104 //===================== Only VMS__malloc after this ====================
Me@1 105 masterEnv = _VMSMasterEnv;
Me@31 106
Me@31 107 //Make a readyToAnimateQ for each core loop
Me@55 108 readyToAnimateQs = VMS__malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
Me@53 109 masterVPs = VMS__malloc( NUM_CORES * sizeof(VirtProcr *) );
Me@0 110
Me@31 111 //One array for each core, 3 in array, core's masterVP scheds all
Me@53 112 allSchedSlots = VMS__malloc( NUM_CORES * sizeof(SchedSlot *) );
Me@0 113
Me@53 114 _VMSMasterEnv->numProcrsCreated = 0; //used by create procr
Me@31 115 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
Me@53 116 {
Me@55 117 readyToAnimateQs[ coreIdx ] = makeVMSQ();
Me@31 118
Me@50 119 //Q: should give masterVP core-specific info as its init data?
Me@53 120 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
Me@31 121 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
Me@31 122 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
Me@53 123 _VMSMasterEnv->numMasterInARow[ coreIdx ] = 0;
Me@55 124 _VMSMasterEnv->workStealingGates[ coreIdx ] = NULL;
Me@31 125 }
Me@31 126 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
Me@31 127 _VMSMasterEnv->masterVPs = masterVPs;
Me@50 128 _VMSMasterEnv->masterLock = UNLOCKED;
Me@31 129 _VMSMasterEnv->allSchedSlots = allSchedSlots;
Me@55 130 _VMSMasterEnv->workStealingLock = UNLOCKED;
Me@28 131
Me@48 132 //============================= MEASUREMENT STUFF ========================
Me@48 133 #ifdef MEAS__TIME_MASTER
Me@41 134
Me@48 135 _VMSMasterEnv->stats->masterTimeHist = makeHistogram( 25, 500, 800 );
Me@48 136 _VMSMasterEnv->stats->masterLockHist = makeHistogram( 25, 0, 100000 );
Me@48 137 _VMSMasterEnv->stats->createHist = makeHistogram( 25, 0, 5000 );
Me@48 138 #endif
Me@48 139 //========================================================================
Me@12 140
Me@31 141 //Aug 19, 2010: no longer need to place initial masterVP into queue
Me@31 142 // because coreLoop now controls -- animates its masterVP when no work
Me@31 143
Me@30 144
Me@50 145 //============================= MEASUREMENT STUFF ========================
Me@50 146 #ifdef STATS__TURN_ON_PROBES
Me@50 147 _VMSMasterEnv->dynIntervalProbesInfo =
Me@53 148 makePrivDynArrayOfSize( &(_VMSMasterEnv->intervalProbes), 200);
Me@30 149
Me@53 150 _VMSMasterEnv->probeNameHashTbl = makeHashTable( 1000, &VMS__free );
Me@53 151
Me@53 152 //put creation time directly into master env, for fast retrieval
Me@50 153 struct timeval timeStamp;
Me@50 154 gettimeofday( &(timeStamp), NULL);
Me@50 155 _VMSMasterEnv->createPtInSecs =
Me@50 156 timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0);
Me@50 157 #endif
Me@50 158 //========================================================================
Me@38 159
Me@0 160 }
Me@0 161
Me@31 162 SchedSlot **
Me@31 163 create_sched_slots()
Me@31 164 { SchedSlot **schedSlots;
Me@0 165 int i;
Me@0 166
Me@53 167 schedSlots = VMS__malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 168
Me@1 169 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@0 170 {
Me@53 171 schedSlots[i] = VMS__malloc( sizeof(SchedSlot) );
Me@8 172
Me@1 173 //Set state to mean "handling requests done, slot needs filling"
Me@8 174 schedSlots[i]->workIsDone = FALSE;
Me@8 175 schedSlots[i]->needsProcrAssigned = TRUE;
Me@0 176 }
Me@31 177 return schedSlots;
Me@31 178 }
Me@31 179
Me@31 180
Me@31 181 void
Me@31 182 freeSchedSlots( SchedSlot **schedSlots )
Me@31 183 { int i;
Me@31 184 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@31 185 {
Me@53 186 VMS__free( schedSlots[i] );
Me@31 187 }
Me@53 188 VMS__free( schedSlots );
Me@0 189 }
Me@0 190
Me@8 191
Me@28 192 void
Me@28 193 create_the_coreLoop_OS_threads()
Me@28 194 {
Me@28 195 //========================================================================
Me@28 196 // Create the Threads
Me@28 197 int coreIdx, retCode;
Me@28 198
Me@47 199 //create the arrays used to measure TSC offsets between cores
Me@47 200 pongNums = malloc( NUM_CORES * sizeof( int ) );
Me@47 201 pingTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
Me@47 202 pongTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
Me@47 203
Me@47 204 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
Me@47 205 {
Me@47 206 pongNums[ coreIdx ] = 0;
Me@47 207 for( i = 0; i < NUM_TSC_ROUND_TRIPS; i++ )
Me@47 208 {
Me@47 209 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
Me@47 210 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
Me@47 211 }
Me@47 212 }
Me@41 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@53 221 { coreLoopThdParams[coreIdx] = VMS__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@50 229 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(1);}
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@41 241 //===========================================================================
Me@41 242 TSCount startCount, endCount;
Me@41 243 unsigned long long count = 0, freq = 0;
Me@41 244 double runTime;
Me@41 245
Me@47 246 startCount = getTSC();
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@41 267
Me@41 268
Me@47 269 endCount = getTSC();
Me@41 270 count = endCount - startCount;
Me@41 271
Me@41 272 runTime = (double)count / (double)TSCOUNT_FREQ;
Me@41 273
Me@41 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@50 301 inline VirtProcr *
Me@50 302 create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr,
Me@50 303 void *initialData, char *stackLocs )
Me@50 304 {
Me@50 305 char *stackPtr;
Me@8 306
Me@48 307 //============================= MEASUREMENT STUFF ========================
Me@48 308 #ifdef MEAS__TIME_MASTER
Me@48 309 int32 startStamp;
Me@48 310 saveLowTimeStampCountInto( startStamp );
Me@48 311 #endif
Me@48 312 //========================================================================
Me@53 313 newPr->startOfStack = stackLocs;
Me@53 314 newPr->procrID = _VMSMasterEnv->numProcrsCreated++;
Me@53 315 newPr->nextInstrPt = fnPtr;
Me@53 316 newPr->initialData = initialData;
Me@53 317 newPr->requests = NULL;
Me@53 318 newPr->schedSlot = NULL;
Me@8 319
Me@14 320 //fnPtr takes two params -- void *initData & void *animProcr
Me@8 321 //alloc stack locations, make stackPtr be the highest addr minus room
Me@14 322 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
Me@14 323 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
Me@22 324 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
Me@50 325
Me@8 326 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
Me@22 327 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
Me@14 328 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
Me@8 329 newPr->stackPtr = stackPtr; //core loop will switch to this, then
Me@8 330 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
Me@8 331
Me@48 332 //============================= MEASUREMENT STUFF ========================
Me@48 333 #ifdef MEAS__TIME_MASTER
Me@48 334 int32 endStamp;
Me@48 335 saveLowTimeStampCountInto( endStamp );
Me@48 336 addIntervalToHist( startStamp, endStamp,
Me@48 337 _VMSMasterEnv->stats->createHist );
Me@50 338 //============================= MEASUREMENT STUFF ========================
Me@50 339 #ifdef STATS__TURN_ON_PROBES
Me@50 340 struct timeval timeStamp;
Me@50 341 gettimeofday( &(timeStamp), NULL);
Me@54 342 newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
Me@54 343 _VMSMasterEnv->createPtInSecs;
Me@50 344 #endif
Me@50 345 //========================================================================
Me@50 346
Me@8 347 return newPr;
Me@8 348 }
Me@8 349
Me@50 350 inline VirtProcr *
Me@50 351 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
Me@50 352 { VirtProcr *newPr;
Me@50 353 char *stackLocs;
Me@50 354
Me@50 355 newPr = VMS__malloc( sizeof(VirtProcr) );
Me@50 356 stackLocs = VMS__malloc( VIRT_PROCR_STACK_SIZE );
Me@50 357 if( stackLocs == 0 )
Me@50 358 { perror("VMS__malloc stack"); exit(1); }
Me@50 359
Me@50 360 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
Me@50 361 }
Me@50 362
Me@50 363 /* "ext" designates that it's for use outside the VMS system -- should only
Me@50 364 * be called from main thread or other thread -- never from code animated by
Me@50 365 * a VMS virtual processor.
Me@50 366 */
Me@50 367 inline VirtProcr *
Me@50 368 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
Me@50 369 { VirtProcr *newPr;
Me@50 370 char *stackLocs;
Me@50 371
Me@50 372 newPr = malloc( sizeof(VirtProcr) );
Me@50 373 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
Me@50 374 if( stackLocs == 0 )
Me@50 375 { perror("malloc stack"); exit(1); }
Me@50 376
Me@50 377 return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
Me@50 378 }
Me@50 379
Me@8 380
Me@26 381 /*there is a label inside this function -- save the addr of this label in
Me@0 382 * the callingPr struc, as the pick-up point from which to start the next
Me@0 383 * work-unit for that procr. If turns out have to save registers, then
Me@0 384 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
Me@0 385 * "done with work-unit" label. The procr struc is in the request in the
Me@0 386 * slave that animated the just-ended work-unit, so all the state is saved
Me@0 387 * there, and will get passed along, inside the request handler, to the
Me@0 388 * next work-unit for that procr.
Me@0 389 */
Me@8 390 void
Me@38 391 VMS__suspend_procr( VirtProcr *animatingPr )
Me@55 392 {
Me@0 393
Me@14 394 //The request to master will cause this suspended virt procr to get
Me@14 395 // scheduled again at some future point -- to resume, core loop jumps
Me@14 396 // to the resume point (below), which causes restore of saved regs and
Me@14 397 // "return" from this call.
Me@38 398 animatingPr->nextInstrPt = &&ResumePt;
Me@1 399
Me@1 400 //return ownership of the virt procr and sched slot to Master virt pr
Me@38 401 animatingPr->schedSlot->workIsDone = TRUE;
Me@1 402
Me@41 403 //=========================== Measurement stuff ========================
Me@38 404 #ifdef MEAS__TIME_STAMP_SUSP
Me@41 405 //record time stamp: compare to time-stamp recorded below
Me@38 406 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
Me@38 407 #endif
Me@41 408 //=======================================================================
Me@38 409
Me@1 410
Me@55 411 SwitchToCoreLoop( animatingPr )
Me@55 412
Me@55 413 //=======================================================================
Me@1 414 ResumePt:
Me@38 415 #ifdef MEAS__TIME_STAMP_SUSP
Me@41 416 //NOTE: only take low part of count -- do sanity check when take diff
Me@38 417 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
Me@38 418 #endif
Me@38 419
Me@0 420 return;
Me@0 421 }
Me@0 422
Me@22 423
Me@22 424
Me@50 425 /*For this implementation of VMS, it may not make much sense to have the
Me@50 426 * system of requests for creating a new processor done this way.. but over
Me@50 427 * the scope of single-master, multi-master, mult-tasking, OS-implementing,
Me@50 428 * distributed-memory, and so on, this gives VMS implementation a chance to
Me@50 429 * do stuff before suspend, in the AppVP, and in the Master before the plugin
Me@50 430 * is called, as well as in the lang-lib before this is called, and in the
Me@50 431 * plugin. So, this gives both VMS and language implementations a chance to
Me@50 432 * intercept at various points and do order-dependent stuff.
Me@50 433 *Having a standard VMSNewPrReqData struc allows the language to create and
Me@50 434 * free the struc, while VMS knows how to get the newPr if it wants it, and
Me@50 435 * it lets the lang have lang-specific data related to creation transported
Me@50 436 * to the plugin.
Me@50 437 */
Me@50 438 void
Me@50 439 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr )
Me@50 440 { VMSReqst req;
Me@50 441
Me@50 442 req.reqType = createReq;
Me@50 443 req.semReqData = semReqData;
Me@50 444 req.nextReqst = reqstingPr->requests;
Me@50 445 reqstingPr->requests = &req;
Me@50 446
Me@50 447 VMS__suspend_procr( reqstingPr );
Me@50 448 }
Me@50 449
Me@22 450
Me@38 451 /*
Me@22 452 *This adds a request to dissipate, then suspends the processor so that the
Me@22 453 * request handler will receive the request. The request handler is what
Me@22 454 * does the work of freeing memory and removing the processor from the
Me@22 455 * semantic environment's data structures.
Me@22 456 *The request handler also is what figures out when to shutdown the VMS
Me@22 457 * system -- which causes all the core loop threads to die, and returns from
Me@22 458 * the call that started up VMS to perform the work.
Me@22 459 *
Me@22 460 *This form is a bit misleading to understand if one is trying to figure out
Me@22 461 * how VMS works -- it looks like a normal function call, but inside it
Me@22 462 * sends a request to the request handler and suspends the processor, which
Me@22 463 * jumps out of the VMS__dissipate_procr function, and out of all nestings
Me@22 464 * above it, transferring the work of dissipating to the request handler,
Me@22 465 * which then does the actual work -- causing the processor that animated
Me@22 466 * the call of this function to disappear and the "hanging" state of this
Me@22 467 * function to just poof into thin air -- the virtual processor's trace
Me@22 468 * never returns from this call, but instead the virtual processor's trace
Me@22 469 * gets suspended in this call and all the virt processor's state disap-
Me@22 470 * pears -- making that suspend the last thing in the virt procr's trace.
Me@8 471 */
Me@8 472 void
Me@53 473 VMS__send_dissipate_req( VirtProcr *procrToDissipate )
Me@50 474 { VMSReqst req;
Me@22 475
Me@50 476 req.reqType = dissipate;
Me@50 477 req.nextReqst = procrToDissipate->requests;
Me@50 478 procrToDissipate->requests = &req;
Me@50 479
Me@22 480 VMS__suspend_procr( procrToDissipate );
Me@50 481 }
Me@50 482
Me@50 483
Me@50 484 /* "ext" designates that it's for use outside the VMS system -- should only
Me@50 485 * be called from main thread or other thread -- never from code animated by
Me@50 486 * a VMS virtual processor.
Me@50 487 *
Me@50 488 *Use this version to dissipate VPs created outside the VMS system.
Me@50 489 */
Me@50 490 void
Me@50 491 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate )
Me@50 492 {
Me@50 493 //NOTE: initialData was given to the processor, so should either have
Me@50 494 // been alloc'd with VMS__malloc, or freed by the level above animPr.
Me@50 495 //So, all that's left to free here is the stack and the VirtProcr struc
Me@50 496 // itself
Me@50 497 //Note, should not stack-allocate initial data -- no guarantee, in
Me@50 498 // general that creating processor will outlive ones it creates.
Me@50 499 free( procrToDissipate->startOfStack );
Me@50 500 free( procrToDissipate );
Me@50 501 }
Me@50 502
Me@22 503
Me@22 504
Me@53 505 /*This call's name indicates that request is malloc'd -- so req handler
Me@53 506 * has to free any extra requests tacked on before a send, using this.
Me@53 507 *
Me@53 508 * This inserts the semantic-layer's request data into standard VMS carrier
Me@53 509 * request data-struct that is mallocd. The sem request doesn't need to
Me@53 510 * be malloc'd if this is called inside the same call chain before the
Me@53 511 * send of the last request is called.
Me@53 512 *
Me@53 513 *The request handler has to call VMS__free_VMSReq for any of these
Me@22 514 */
Me@22 515 inline void
Me@53 516 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData,
Me@53 517 VirtProcr *callingPr )
Me@53 518 { VMSReqst *req;
Me@22 519
Me@53 520 req = VMS__malloc( sizeof(VMSReqst) );
Me@53 521 req->reqType = semantic;
Me@53 522 req->semReqData = semReqData;
Me@53 523 req->nextReqst = callingPr->requests;
Me@53 524 callingPr->requests = req;
Me@22 525 }
Me@22 526
Me@50 527 /*This inserts the semantic-layer's request data into standard VMS carrier
Me@50 528 * request data-struct is allocated on stack of this call & ptr to it sent
Me@50 529 * to plugin
Me@50 530 *Then it does suspend, to cause request to be sent.
Me@50 531 */
Me@50 532 inline void
Me@50 533 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
Me@50 534 { VMSReqst req;
Me@22 535
Me@50 536 req.reqType = semantic;
Me@50 537 req.semReqData = semReqData;
Me@50 538 req.nextReqst = callingPr->requests;
Me@50 539 callingPr->requests = &req;
Me@50 540
Me@50 541 VMS__suspend_procr( callingPr );
Me@50 542 }
Me@50 543
Me@50 544
Me@50 545 inline void
Me@50 546 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr )
Me@50 547 { VMSReqst req;
Me@50 548
Me@50 549 req.reqType = VMSSemantic;
Me@50 550 req.semReqData = semReqData;
Me@50 551 req.nextReqst = callingPr->requests; //gab any other preceeding
Me@50 552 callingPr->requests = &req;
Me@50 553
Me@50 554 VMS__suspend_procr( callingPr );
Me@50 555 }
Me@50 556
Me@50 557
Me@50 558 /*
Me@38 559 */
Me@24 560 VMSReqst *
Me@50 561 VMS__take_next_request_out_of( VirtProcr *procrWithReq )
Me@31 562 { VMSReqst *req;
Me@31 563
Me@31 564 req = procrWithReq->requests;
Me@38 565 if( req == NULL ) return NULL;
Me@31 566
Me@31 567 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@50 568 return req;
Me@24 569 }
Me@22 570
Me@24 571
Me@24 572 inline void *
Me@24 573 VMS__take_sem_reqst_from( VMSReqst *req )
Me@24 574 {
Me@24 575 return req->semReqData;
Me@24 576 }
Me@24 577
Me@24 578
Me@24 579
Me@50 580 /* This is for OS requests and VMS infrastructure requests, such as to create
Me@50 581 * a probe -- a probe is inside the heart of VMS-core, it's not part of any
Me@50 582 * language -- but it's also a semantic thing that's triggered from and used
Me@50 583 * in the application.. so it crosses abstractions.. so, need some special
Me@50 584 * pattern here for handling such requests.
Me@52 585 * Doing this just like it were a second language sharing VMS-core.
Me@52 586 *
Me@50 587 * This is called from the language's request handler when it sees a request
Me@50 588 * of type VMSSemReq
Me@52 589 *
Me@52 590 * TODO: Later change this, to give probes their own separate plugin & have
Me@52 591 * VMS-core steer the request to appropriate plugin
Me@52 592 * Do the same for OS calls -- look later at it..
Me@50 593 */
Me@50 594 void inline
Me@50 595 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
Me@50 596 ResumePrFnPtr resumePrFnPtr )
Me@50 597 { VMSSemReq *semReq;
Me@50 598 IntervalProbe *newProbe;
Me@50 599 int32 nameLen;
Me@24 600
Me@50 601 semReq = req->semReqData;
Me@24 602
Me@50 603 newProbe = VMS__malloc( sizeof(IntervalProbe) );
Me@50 604 nameLen = strlen( semReq->nameStr );
Me@50 605 newProbe->nameStr = VMS__malloc( nameLen );
Me@50 606 memcpy( newProbe->nameStr, semReq->nameStr, nameLen );
Me@50 607 newProbe->hist = NULL;
Me@50 608 newProbe->schedChoiceWasRecorded = FALSE;
Me@53 609
Me@53 610 //This runs in masterVP, so no race-condition worries
Me@50 611 newProbe->probeID =
Me@50 612 addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo );
Me@50 613
Me@53 614 requestingPr->dataRetFromReq = newProbe;
Me@50 615
Me@50 616 (*resumePrFnPtr)( requestingPr, semEnv );
Me@22 617 }
Me@22 618
Me@22 619
Me@22 620
Me@24 621 /*This must be called by the request handler plugin -- it cannot be called
Me@24 622 * from the semantic library "dissipate processor" function -- instead, the
Me@50 623 * semantic layer has to generate a request, and the plug-in calls this
Me@24 624 * function.
Me@24 625 *The reason is that this frees the virtual processor's stack -- which is
Me@24 626 * still in use inside semantic library calls!
Me@24 627 *
Me@24 628 *This frees or recycles all the state owned by and comprising the VMS
Me@24 629 * portion of the animating virtual procr. The request handler must first
Me@24 630 * free any semantic data created for the processor that didn't use the
Me@24 631 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
Me@24 632 * system to disown any state that did use VMS_malloc, and then frees the
Me@24 633 * statck and the processor-struct itself.
Me@24 634 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
Me@24 635 * state, then that state gets freed (or sent to recycling) as a side-effect
Me@24 636 * of dis-owning it.
Me@24 637 */
Me@24 638 void
Me@53 639 VMS__dissipate_procr( VirtProcr *animatingPr )
Me@24 640 {
Me@24 641 //dis-own all locations owned by this processor, causing to be freed
Me@24 642 // any locations that it is (was) sole owner of
Me@29 643 //TODO: implement VMS__malloc system, including "give up ownership"
Me@24 644
Me@24 645
Me@24 646 //NOTE: initialData was given to the processor, so should either have
Me@24 647 // been alloc'd with VMS__malloc, or freed by the level above animPr.
Me@24 648 //So, all that's left to free here is the stack and the VirtProcr struc
Me@24 649 // itself
Me@50 650 //Note, should not stack-allocate initial data -- no guarantee, in
Me@50 651 // general that creating processor will outlive ones it creates.
Me@50 652 VMS__free( animatingPr->startOfStack );
Me@50 653 VMS__free( animatingPr );
Me@24 654 }
Me@24 655
Me@24 656
Me@53 657 //TODO: look at architecting cleanest separation between request handler
Me@29 658 // and master loop, for dissipate, create, shutdown, and other non-semantic
Me@29 659 // requests. Issue is chain: one removes requests from AppVP, one dispatches
Me@29 660 // on type of request, and one handles each type.. but some types require
Me@29 661 // action from both request handler and master loop -- maybe just give the
Me@29 662 // request handler calls like: VMS__handle_X_request_type
Me@24 663
Me@29 664
Me@29 665 /*This is called by the semantic layer's request handler when it decides its
Me@29 666 * time to shut down the VMS system. Calling this causes the core loop OS
Me@29 667 * threads to exit, which unblocks the entry-point function that started up
Me@29 668 * VMS, and allows it to grab the result and return to the original single-
Me@29 669 * threaded application.
Me@22 670 *
Me@29 671 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
Me@29 672 * and-wait function has to free a bunch of stuff after it detects the
Me@29 673 * threads have all died: the masterEnv, the thread-related locations,
Me@29 674 * masterVP any AppVPs that might still be allocated and sitting in the
Me@29 675 * semantic environment, or have been orphaned in the _VMSWorkQ.
Me@29 676 *
Me@53 677 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
Me@29 678 * locations it needs, and give ownership to masterVP. Then, they will be
Me@53 679 * automatically freed.
Me@22 680 *
Me@29 681 *In here,create one core-loop shut-down processor for each core loop and put
Me@31 682 * them all directly into the readyToAnimateQ.
Me@29 683 *Note, this function can ONLY be called after the semantic environment no
Me@29 684 * longer cares if AppVPs get animated after the point this is called. In
Me@29 685 * other words, this can be used as an abort, or else it should only be
Me@29 686 * called when all AppVPs have finished dissipate requests -- only at that
Me@29 687 * point is it sure that all results have completed.
Me@22 688 */
Me@22 689 void
Me@53 690 VMS__shutdown()
Me@8 691 { int coreIdx;
Me@14 692 VirtProcr *shutDownPr;
Me@22 693
Me@29 694 //create the shutdown processors, one for each core loop -- put them
Me@31 695 // directly into the Q -- each core will die when gets one
Me@8 696 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@50 697 { //Note, this is running in the master
Me@29 698 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
Me@55 699 writeVMSQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
Me@8 700 }
Me@22 701
Me@12 702 }
Me@12 703
Me@12 704
Me@29 705 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
Me@29 706 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
Me@29 707 *This function has the sole purpose of setting the stack and framePtr
Me@29 708 * to the coreLoop's stack and framePtr.. it does that then jumps to the
Me@29 709 * core loop's shutdown point -- might be able to just call Pthread_exit
Me@30 710 * from here, but am going back to the pthread's stack and setting everything
Me@29 711 * up just as if it never jumped out, before calling pthread_exit.
Me@29 712 *The end-point of core loop will free the stack and so forth of the
Me@29 713 * processor that animates this function, (this fn is transfering the
Me@29 714 * animator of the AppVP that is in turn animating this function over
Me@29 715 * to core loop function -- note that this slices out a level of virtual
Me@29 716 * processors).
Me@29 717 */
Me@29 718 void
Me@29 719 endOSThreadFn( void *initData, VirtProcr *animatingPr )
Me@29 720 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
Me@29 721
Me@29 722 jmpPt = _VMSMasterEnv->coreLoopEndPt;
Me@29 723 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
Me@29 724 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
Me@29 725
Me@29 726
Me@29 727 asm volatile("movl %0, %%eax; \
Me@29 728 movl %1, %%esp; \
Me@29 729 movl %2, %%ebp; \
Me@29 730 jmp %%eax " \
Me@29 731 /* outputs */ : \
Me@29 732 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
Me@29 733 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
Me@29 734 );
Me@29 735 }
Me@29 736
Me@29 737
Me@53 738 /*This is called from the startup & shutdown
Me@24 739 */
Me@24 740 void
Me@53 741 VMS__cleanup_at_end_of_shutdown()
Me@31 742 {
Me@55 743 VMSQueueStruc **readyToAnimateQs;
Me@31 744 int coreIdx;
Me@31 745 VirtProcr **masterVPs;
Me@31 746 SchedSlot ***allSchedSlots; //ptr to array of ptrs
Me@31 747
Me@53 748 //All the environment data has been allocated with VMS__malloc, so just
Me@53 749 // free its internal big-chunk and all inside it disappear.
Me@53 750 /*
Me@31 751 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
Me@31 752 masterVPs = _VMSMasterEnv->masterVPs;
Me@31 753 allSchedSlots = _VMSMasterEnv->allSchedSlots;
Me@31 754
Me@31 755 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
Me@24 756 {
Me@55 757 freeVMSQ( readyToAnimateQs[ coreIdx ] );
Me@50 758 //master VPs were created external to VMS, so use external free
Me@53 759 VMS__dissipate_procr( masterVPs[ coreIdx ] );
Me@31 760
Me@31 761 freeSchedSlots( allSchedSlots[ coreIdx ] );
Me@24 762 }
Me@31 763
Me@53 764 VMS__free( _VMSMasterEnv->readyToAnimateQs );
Me@53 765 VMS__free( _VMSMasterEnv->masterVPs );
Me@53 766 VMS__free( _VMSMasterEnv->allSchedSlots );
Me@50 767
Me@50 768 //============================= MEASUREMENT STUFF ========================
Me@50 769 #ifdef STATS__TURN_ON_PROBES
Me@53 770 freeDynArrayDeep( _VMSMasterEnv->dynIntervalProbesInfo, &VMS__free_probe);
Me@50 771 #endif
Me@50 772 //========================================================================
Me@53 773 */
Me@53 774 //These are the only two that use system free
Me@53 775 VMS_ext__free_free_list( _VMSMasterEnv->freeListHead );
Me@53 776 free( (void *)_VMSMasterEnv );
Me@24 777 }
Me@24 778
Me@54 779
Me@54 780 //================================
Me@54 781
Me@54 782
Me@54 783 /*Later, improve this -- for now, just exits the application after printing
Me@54 784 * the error message.
Me@54 785 */
Me@54 786 void
Me@54 787 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData )
Me@54 788 {
Me@54 789 printf(msgStr);
Me@54 790 fflush(stdin);
Me@54 791 exit(1);
Me@54 792 }
Me@54 793
Me@54 794