annotate VMS.h @ 146:a49f02980151

fixed: hist index error, zero devision
author Merten Sach <msach@mailbox.tu-berlin.de>
date Thu, 12 May 2011 14:23:41 +0200
parents 9c3107044f86
children c11b9dcf6d24
rev   line source
msach@146 1 /*
msach@146 2 * Copyright 2009 OpenSourceStewardshipFoundation.org
msach@146 3 * Licensed under GNU General Public License version 2
msach@146 4 *
msach@146 5 * Author: seanhalle@yahoo.com
msach@146 6 *
msach@146 7 */
msach@146 8
msach@146 9 #ifndef _VMS_H
msach@146 10 #define _VMS_H
msach@146 11 #define __USE_GNU
msach@146 12
msach@146 13 #include "VMS_primitive_data_types.h"
msach@146 14 #include "Queue_impl/PrivateQueue.h"
msach@146 15 #include "Histogram/Histogram.h"
msach@146 16 #include "DynArray/DynArray.h"
msach@146 17 #include "Hash_impl/PrivateHash.h"
msach@146 18 #include "vmalloc.h"
msach@146 19
msach@146 20 #include <pthread.h>
msach@146 21 #include <sys/time.h>
msach@146 22
msach@146 23
msach@146 24 //=============================== Debug ===================================
msach@146 25 //
msach@146 26 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
msach@146 27 // It still does co-routines and all the mechanisms are the same, it just
msach@146 28 // has only a single thread and animates VPs one at a time
msach@146 29 //#define SEQUENTIAL
msach@146 30
msach@146 31 //#define USE_WORK_STEALING
msach@146 32
msach@146 33 //turns on the probe-instrumentation in the application -- when not
msach@146 34 // defined, the calls to the probe functions turn into comments
msach@146 35 #define STATS__ENABLE_PROBES
msach@146 36 //#define TURN_ON_DEBUG_PROBES
msach@146 37
msach@146 38 //These defines turn types of bug messages on and off
msach@146 39 // be sure debug messages are un-commented (next block of defines)
msach@146 40 #define dbgAppFlow TRUE /* Top level flow of application code -- general*/
msach@146 41 #define dbgProbes FALSE /* for issues inside probes themselves*/
msach@146 42 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
msach@146 43 #define dbgRqstHdlr FALSE /* in request handler code*/
msach@146 44
msach@146 45 //Comment or un- the substitute half to turn on/off types of debug message
msach@146 46 #define DEBUG( bool, msg) \
msach@146 47 // if( bool){ printf(msg); fflush(stdin);}
msach@146 48 #define DEBUG1( bool, msg, param) \
msach@146 49 // if(bool){printf(msg, param); fflush(stdin);}
msach@146 50 #define DEBUG2( bool, msg, p1, p2) \
msach@146 51 // if(bool) {printf(msg, p1, p2); fflush(stdin);}
msach@146 52
msach@146 53 #define ERROR(msg) printf(msg);
msach@146 54 #define ERROR1(msg, param) printf(msg, param);
msach@146 55 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
msach@146 56
msach@146 57 //=========================== STATS =======================
msach@146 58
msach@146 59 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
msach@146 60 // compiled-in that saves the low part of the time stamp count just before
msach@146 61 // suspending a processor and just after resuming that processor. It is
msach@146 62 // saved into a field added to VirtProcr. Have to sanity-check for
msach@146 63 // rollover of low portion into high portion.
msach@146 64 //#define MEAS__TIME_STAMP_SUSP
msach@146 65 //#define MEAS__TIME_MASTER
msach@146 66 #define MEAS__TIME_PLUGIN
msach@146 67 #define MEAS__TIME_MALLOC
msach@146 68 //#define MEAS__TIME_MASTER_LOCK
msach@146 69 #define MEAS__NUM_TIMES_TO_RUN 100000
msach@146 70
msach@146 71 //For code that calculates normalization-offset between TSC counts of
msach@146 72 // different cores.
msach@146 73 #define NUM_TSC_ROUND_TRIPS 10
msach@146 74
msach@146 75
msach@146 76 //========================= Hardware related Constants =====================
msach@146 77 //This value is the number of hardware threads in the shared memory
msach@146 78 // machine
msach@146 79 #define NUM_CORES 4
msach@146 80
msach@146 81 // tradeoff amortizing master fixed overhead vs imbalance potential
msach@146 82 // when work-stealing, can make bigger, at risk of losing cache affinity
msach@146 83 #define NUM_SCHED_SLOTS 5
msach@146 84
msach@146 85 #define MIN_WORK_UNIT_CYCLES 20000
msach@146 86
msach@146 87 #define MASTERLOCK_RETRIES 10000
msach@146 88
msach@146 89 // stack size in virtual processors created
msach@146 90 #define VIRT_PROCR_STACK_SIZE 0x4000 /* 16K */
msach@146 91
msach@146 92 // memory for VMS__malloc
msach@146 93 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
msach@146 94
msach@146 95
msach@146 96 //==============================
msach@146 97
msach@146 98 #define SUCCESS 0
msach@146 99
msach@146 100 #define writeVMSQ writePrivQ
msach@146 101 #define readVMSQ readPrivQ
msach@146 102 #define makeVMSQ makeVMSPrivQ
msach@146 103 #define numInVMSQ numInPrivQ
msach@146 104 #define VMSQueueStruc PrivQueueStruc
msach@146 105
msach@146 106
msach@146 107
msach@146 108 //===========================================================================
msach@146 109 typedef unsigned long long TSCount;
msach@146 110
msach@146 111 typedef struct _SchedSlot SchedSlot;
msach@146 112 typedef struct _VMSReqst VMSReqst;
msach@146 113 typedef struct _VirtProcr VirtProcr;
msach@146 114 typedef struct _IntervalProbe IntervalProbe;
msach@146 115 typedef struct _GateStruc GateStruc;
msach@146 116
msach@146 117
msach@146 118 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
msach@146 119 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
msach@146 120 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
msach@146 121 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
msach@146 122 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
msach@146 123
msach@146 124
msach@146 125 //============= Requests ===========
msach@146 126 //
msach@146 127
msach@146 128 enum VMSReqstType //avoid starting enums at 0, for debug reasons
msach@146 129 {
msach@146 130 semantic = 1,
msach@146 131 createReq,
msach@146 132 dissipate,
msach@146 133 VMSSemantic //goes with VMSSemReqst below
msach@146 134 };
msach@146 135
msach@146 136 struct _VMSReqst
msach@146 137 {
msach@146 138 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
msach@146 139 void *semReqData;
msach@146 140
msach@146 141 VMSReqst *nextReqst;
msach@146 142 };
msach@146 143 //VMSReqst
msach@146 144
msach@146 145 enum VMSSemReqstType //These are equivalent to semantic requests, but for
msach@146 146 { // VMS's services available directly to app, like OS
msach@146 147 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
msach@146 148 openFile,
msach@146 149 otherIO
msach@146 150 };
msach@146 151
msach@146 152 typedef struct
msach@146 153 { enum VMSSemReqstType reqType;
msach@146 154 VirtProcr *requestingPr;
msach@146 155 char *nameStr; //for create probe
msach@146 156 }
msach@146 157 VMSSemReq;
msach@146 158
msach@146 159
msach@146 160 //==================== Core data structures ===================
msach@146 161
msach@146 162 struct _SchedSlot
msach@146 163 {
msach@146 164 int workIsDone;
msach@146 165 int needsProcrAssigned;
msach@146 166 VirtProcr *procrAssignedToSlot;
msach@146 167 };
msach@146 168 //SchedSlot
msach@146 169
msach@146 170 /*WARNING: re-arranging this data structure could cause VP switching
msach@146 171 * assembly code to fail -- hard-codes offsets of fields
msach@146 172 */
msach@146 173 struct _VirtProcr
msach@146 174 { int procrID; //for debugging -- count up each time create
msach@146 175 int coreAnimatedBy;
msach@146 176 void *startOfStack;
msach@146 177 void *stackPtr;
msach@146 178 void *framePtr;
msach@146 179 void *nextInstrPt;
msach@146 180
msach@146 181 void *coreLoopStartPt; //allows proto-runtime to be linked later
msach@146 182 void *coreLoopFramePtr; //restore before jmp back to core loop
msach@146 183 void *coreLoopStackPtr; //restore before jmp back to core loop
msach@146 184
msach@146 185 void *initialData;
msach@146 186
msach@146 187 SchedSlot *schedSlot;
msach@146 188 VMSReqst *requests;
msach@146 189
msach@146 190 void *semanticData; //this lives here for the life of VP
msach@146 191 void *dataRetFromReq;//values returned from plugin to VP go here
msach@146 192
msach@146 193 //=========== MEASUREMENT STUFF ==========
msach@146 194 #ifdef MEAS__TIME_STAMP_SUSP
msach@146 195 unsigned int preSuspTSCLow;
msach@146 196 unsigned int postSuspTSCLow;
msach@146 197 #endif
msach@146 198 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
msach@146 199 unsigned int startMasterTSCLow;
msach@146 200 unsigned int endMasterTSCLow;
msach@146 201 #endif
msach@146 202 //========================================
msach@146 203
msach@146 204 float64 createPtInSecs; //have space but don't use on some configs
msach@146 205 };
msach@146 206 //VirtProcr
msach@146 207
msach@146 208
msach@146 209 /*WARNING: re-arranging this data structure could cause VP-switching
msach@146 210 * assembly code to fail -- hard-codes offsets of fields
msach@146 211 * (because -O3 messes with things otherwise)
msach@146 212 */
msach@146 213 typedef struct
msach@146 214 {
msach@146 215 SlaveScheduler slaveScheduler;
msach@146 216 RequestHandler requestHandler;
msach@146 217
msach@146 218 SchedSlot ***allSchedSlots;
msach@146 219 VMSQueueStruc **readyToAnimateQs;
msach@146 220 VirtProcr **masterVPs;
msach@146 221
msach@146 222 void *semanticEnv;
msach@146 223 void *OSEventStruc; //for future, when add I/O to BLIS
msach@146 224 MallocProlog *freeListHead;
msach@146 225 int32 amtOfOutstandingMem; //total currently allocated
msach@146 226
msach@146 227 void *coreLoopStartPt;//addr to jump to to re-enter coreLoop
msach@146 228 void *coreLoopEndPt; //addr to jump to to shut down a coreLoop
msach@146 229
msach@146 230 int32 setupComplete;
msach@146 231 int32 masterLock;
msach@146 232
msach@146 233 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
msach@146 234 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
msach@146 235 int32 workStealingLock;
msach@146 236
msach@146 237 int32 numProcrsCreated; //gives ordering to processor creation
msach@146 238
msach@146 239 //=========== MEASUREMENT STUFF =============
msach@146 240 IntervalProbe **intervalProbes;
msach@146 241 PrivDynArrayInfo *dynIntervalProbesInfo;
msach@146 242 HashTable *probeNameHashTbl;
msach@146 243 int32 masterCreateProbeID;
msach@146 244 float64 createPtInSecs;
msach@146 245 Histogram **measHists;
msach@146 246 PrivDynArrayInfo *measHistsInfo;
msach@146 247 #ifdef MEAS__TIME_PLUGIN
msach@146 248 Histogram *reqHdlrLowTimeHist;
msach@146 249 Histogram *reqHdlrHighTimeHist;
msach@146 250 #endif
msach@146 251 #ifdef MEAS__TIME_MALLOC
msach@146 252 Histogram *mallocTimeHist;
msach@146 253 Histogram *freeTimeHist;
msach@146 254 #endif
msach@146 255 #ifdef MEAS__TIME_MASTER_LOCK
msach@146 256 Histogram *masterLockLowTimeHist;
msach@146 257 Histogram *masterLockHighTimeHist;
msach@146 258 #endif
msach@146 259 }
msach@146 260 MasterEnv;
msach@146 261
msach@146 262 //========================= Extra Stuff Data Strucs =======================
msach@146 263 typedef struct
msach@146 264 {
msach@146 265
msach@146 266 }
msach@146 267 VMSExcp;
msach@146 268
msach@146 269 struct _GateStruc
msach@146 270 {
msach@146 271 int32 gateClosed;
msach@146 272 int32 preGateProgress;
msach@146 273 int32 waitProgress;
msach@146 274 int32 exitProgress;
msach@146 275 };
msach@146 276 //GateStruc
msach@146 277
msach@146 278 //======================= OS Thread related ===============================
msach@146 279
msach@146 280 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
msach@146 281 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
msach@146 282 void masterLoop( void *initData, VirtProcr *masterPr );
msach@146 283
msach@146 284
msach@146 285 typedef struct
msach@146 286 {
msach@146 287 void *endThdPt;
msach@146 288 unsigned int coreNum;
msach@146 289 }
msach@146 290 ThdParams;
msach@146 291
msach@146 292 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
msach@146 293 ThdParams *coreLoopThdParams [ NUM_CORES ];
msach@146 294 pthread_mutex_t suspendLock;
msach@146 295 pthread_cond_t suspend_cond;
msach@146 296
msach@146 297
msach@146 298
msach@146 299 //===================== Global Vars ===================
msach@146 300
msach@146 301 volatile MasterEnv *_VMSMasterEnv;
msach@146 302
msach@146 303
msach@146 304
msach@146 305
msach@146 306 //=========================== Function Prototypes =========================
msach@146 307
msach@146 308
msach@146 309 //========== Setup and shutdown ==========
msach@146 310 void
msach@146 311 VMS__init();
msach@146 312
msach@146 313 void
msach@146 314 VMS__init_Seq();
msach@146 315
msach@146 316 void
msach@146 317 VMS__start_the_work_then_wait_until_done();
msach@146 318
msach@146 319 void
msach@146 320 VMS__start_the_work_then_wait_until_done_Seq();
msach@146 321
msach@146 322 VirtProcr *
msach@146 323 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
msach@146 324
msach@146 325 void
msach@146 326 VMS__dissipate_procr( VirtProcr *procrToDissipate );
msach@146 327
msach@146 328 //Use this to create processor inside entry point & other places outside
msach@146 329 // the VMS system boundary (IE, not run in slave nor Master)
msach@146 330 VirtProcr *
msach@146 331 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
msach@146 332
msach@146 333 void
msach@146 334 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
msach@146 335
msach@146 336 void
msach@146 337 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
msach@146 338
msach@146 339 void
msach@146 340 VMS__shutdown();
msach@146 341
msach@146 342 void
msach@146 343 VMS__cleanup_at_end_of_shutdown();
msach@146 344
msach@146 345 void *
msach@146 346 VMS__give_sem_env_for( VirtProcr *animPr );
msach@146 347
msach@146 348
msach@146 349 //============== Request Related ===============
msach@146 350
msach@146 351 void
msach@146 352 VMS__suspend_procr( VirtProcr *callingPr );
msach@146 353
msach@146 354 inline void
msach@146 355 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
msach@146 356
msach@146 357 inline void
msach@146 358 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
msach@146 359
msach@146 360 void
msach@146 361 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
msach@146 362
msach@146 363 void inline
msach@146 364 VMS__send_dissipate_req( VirtProcr *prToDissipate );
msach@146 365
msach@146 366 inline void
msach@146 367 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
msach@146 368
msach@146 369 VMSReqst *
msach@146 370 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
msach@146 371
msach@146 372 inline void *
msach@146 373 VMS__take_sem_reqst_from( VMSReqst *req );
msach@146 374
msach@146 375 //======================== STATS ======================
msach@146 376
msach@146 377 //===== RDTSC wrapper =====
msach@146 378
msach@146 379 #define saveTimeStampCountInto(low, high) \
msach@146 380 asm volatile("RDTSC; \
msach@146 381 movl %%eax, %0; \
msach@146 382 movl %%edx, %1;" \
msach@146 383 /* outputs */ : "=m" (low), "=m" (high)\
msach@146 384 /* inputs */ : \
msach@146 385 /* clobber */ : "%eax", "%edx" \
msach@146 386 );
msach@146 387
msach@146 388 #define saveLowTimeStampCountInto(low) \
msach@146 389 asm volatile("RDTSC; \
msach@146 390 movl %%eax, %0;" \
msach@146 391 /* outputs */ : "=m" (low) \
msach@146 392 /* inputs */ : \
msach@146 393 /* clobber */ : "%eax", "%edx" \
msach@146 394 );
msach@146 395
msach@146 396 //====================
msach@146 397 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
msach@146 398 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
msach@146 399 _VMSMasterEnv->measHists[idx] = \
msach@146 400 makeFixedBinHist( numBins, startVal, binWidth, name );
msach@146 401
msach@146 402
msach@146 403 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
msach@146 404 #define createHistIdx 0
msach@146 405 #define mutexLockHistIdx 1
msach@146 406 #define mutexUnlockHistIdx 2
msach@146 407 #define condWaitHistIdx 3
msach@146 408 #define condSignalHistIdx 4
msach@146 409
msach@146 410
msach@146 411 #define MakeTheMeasHists \
msach@146 412 _VMSMasterEnv->measHistsInfo = \
msach@146 413 makePrivDynArrayOfSize( &(_VMSMasterEnv->measHists), 200);\
msach@146 414 makeAMeasHist( createHistIdx, "Create", 50, 0, 200 ) \
msach@146 415 makeAMeasHist( mutexLockHistIdx, "mutex lock", 50, 0, 100 ) \
msach@146 416 makeAMeasHist( mutexUnlockHistIdx, "mutex unlock", 50, 0, 100 ) \
msach@146 417 makeAMeasHist( condWaitHistIdx, "cond wait", 50, 0, 100 ) \
msach@146 418 makeAMeasHist( condSignalHistIdx, "cond signal", 50, 0, 100 )
msach@146 419
msach@146 420 #define Meas_startCreate \
msach@146 421 int32 startStamp, endStamp; \
msach@146 422 saveLowTimeStampCountInto( startStamp ); \
msach@146 423
msach@146 424 #define Meas_endCreate \
msach@146 425 saveLowTimeStampCountInto( endStamp ); \
msach@146 426 addIntervalToHist( startStamp, endStamp, \
msach@146 427 _VMSMasterEnv->measHists[ createHistIdx ] );
msach@146 428
msach@146 429 #define Meas_startMutexLock \
msach@146 430 int32 startStamp, endStamp; \
msach@146 431 saveLowTimeStampCountInto( startStamp ); \
msach@146 432
msach@146 433 #define Meas_endMutexLock \
msach@146 434 saveLowTimeStampCountInto( endStamp ); \
msach@146 435 addIntervalToHist( startStamp, endStamp, \
msach@146 436 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
msach@146 437
msach@146 438 #define Meas_startMutexUnlock \
msach@146 439 int32 startStamp, endStamp; \
msach@146 440 saveLowTimeStampCountInto( startStamp ); \
msach@146 441
msach@146 442 #define Meas_endMutexUnlock \
msach@146 443 saveLowTimeStampCountInto( endStamp ); \
msach@146 444 addIntervalToHist( startStamp, endStamp, \
msach@146 445 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
msach@146 446
msach@146 447 #define Meas_startCondWait \
msach@146 448 int32 startStamp, endStamp; \
msach@146 449 saveLowTimeStampCountInto( startStamp ); \
msach@146 450
msach@146 451 #define Meas_endCondWait \
msach@146 452 saveLowTimeStampCountInto( endStamp ); \
msach@146 453 addIntervalToHist( startStamp, endStamp, \
msach@146 454 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
msach@146 455
msach@146 456 #define Meas_startCondSignal \
msach@146 457 int32 startStamp, endStamp; \
msach@146 458 saveLowTimeStampCountInto( startStamp ); \
msach@146 459
msach@146 460 #define Meas_endCondSignal \
msach@146 461 saveLowTimeStampCountInto( endStamp ); \
msach@146 462 addIntervalToHist( startStamp, endStamp, \
msach@146 463 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
msach@146 464
msach@146 465
msach@146 466 //=====
msach@146 467
msach@146 468 #include "SwitchAnimators.h"
msach@146 469 #include "probes.h"
msach@146 470 #include "vutilities.h"
msach@146 471
msach@146 472 #endif /* _VMS_H */
msach@146 473