annotate VMS.c @ 50:8f7141a9272e

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