annotate VMS.c @ 201:0320b49ca013

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