annotate VMS.c @ 76:9ddbb071142d

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