annotate VMS.c @ 109:659299627e70

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