annotate VMS.c @ 108:3bc3b89630c7

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