diff 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
line diff
     1.1 --- a/VMS.h	Sat Nov 20 08:19:05 2010 +0100
     1.2 +++ b/VMS.h	Thu May 12 14:23:41 2011 +0200
     1.3 @@ -1,473 +1,473 @@
     1.4 -/*
     1.5 - *  Copyright 2009 OpenSourceStewardshipFoundation.org
     1.6 - *  Licensed under GNU General Public License version 2
     1.7 - *
     1.8 - * Author: seanhalle@yahoo.com
     1.9 - * 
    1.10 - */
    1.11 -
    1.12 -#ifndef _VMS_H
    1.13 -#define	_VMS_H
    1.14 -#define __USE_GNU
    1.15 -
    1.16 -#include "VMS_primitive_data_types.h"
    1.17 -#include "Queue_impl/PrivateQueue.h"
    1.18 -#include "Histogram/Histogram.h"
    1.19 -#include "DynArray/DynArray.h"
    1.20 -#include "Hash_impl/PrivateHash.h"
    1.21 -#include "vmalloc.h"
    1.22 -
    1.23 -#include <pthread.h>
    1.24 -#include <sys/time.h>
    1.25 -
    1.26 -
    1.27 -//===============================  Debug  ===================================
    1.28 -//
    1.29 -   //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
    1.30 -   // It still does co-routines and all the mechanisms are the same, it just
    1.31 -   // has only a single thread and animates VPs one at a time
    1.32 -//#define SEQUENTIAL
    1.33 -
    1.34 -//#define USE_WORK_STEALING
    1.35 -
    1.36 -   //turns on the probe-instrumentation in the application -- when not
    1.37 -   // defined, the calls to the probe functions turn into comments
    1.38 -#define STATS__ENABLE_PROBES
    1.39 -//#define TURN_ON_DEBUG_PROBES
    1.40 -
    1.41 -   //These defines turn types of bug messages on and off
    1.42 -   // be sure debug messages are un-commented (next block of defines)
    1.43 -#define dbgAppFlow   TRUE /* Top level flow of application code -- general*/
    1.44 -#define dbgProbes    FALSE /* for issues inside probes themselves*/
    1.45 -#define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
    1.46 -#define dbgRqstHdlr  FALSE /* in request handler code*/
    1.47 -
    1.48 -   //Comment or un- the substitute half to turn on/off types of debug message
    1.49 -#define DEBUG(  bool, msg)         \
    1.50 -//  if( bool){ printf(msg); fflush(stdin);}
    1.51 -#define DEBUG1( bool, msg, param)  \
    1.52 -//   if(bool){printf(msg, param); fflush(stdin);}
    1.53 -#define DEBUG2( bool, msg, p1, p2) \
    1.54 -//   if(bool) {printf(msg, p1, p2); fflush(stdin);}
    1.55 -
    1.56 -#define ERROR(msg) printf(msg); fflush(stdin);
    1.57 -#define ERROR1(msg, param) printf(msg, param); fflush(stdin);
    1.58 -#define ERROR2(msg, p1, p2) printf(msg, p1, p2); fflush(stdin);
    1.59 -
    1.60 -//===========================  STATS =======================
    1.61 -
    1.62 -   //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
    1.63 -   // compiled-in that saves the low part of the time stamp count just before
    1.64 -   // suspending a processor and just after resuming that processor.  It is
    1.65 -   // saved into a field added to VirtProcr.  Have to sanity-check for
    1.66 -   // rollover of low portion into high portion.
    1.67 -//#define MEAS__TIME_STAMP_SUSP
    1.68 -//#define MEAS__TIME_MASTER
    1.69 -#define MEAS__TIME_PLUGIN
    1.70 -#define MEAS__TIME_MALLOC
    1.71 -//#define MEAS__TIME_MASTER_LOCK
    1.72 -#define MEAS__NUM_TIMES_TO_RUN 100000
    1.73 -
    1.74 -   //For code that calculates normalization-offset between TSC counts of
    1.75 -   // different cores.
    1.76 -#define NUM_TSC_ROUND_TRIPS 10
    1.77 -
    1.78 -
    1.79 -//=========================  Hardware related Constants =====================
    1.80 -   //This value is the number of hardware threads in the shared memory
    1.81 -   // machine
    1.82 -#define NUM_CORES        4
    1.83 -
    1.84 -   // tradeoff amortizing master fixed overhead vs imbalance potential
    1.85 -   // when work-stealing, can make bigger, at risk of losing cache affinity
    1.86 -#define NUM_SCHED_SLOTS  5
    1.87 -
    1.88 -#define MIN_WORK_UNIT_CYCLES 20000
    1.89 -
    1.90 -#define MASTERLOCK_RETRIES 10000
    1.91 -
    1.92 -   // stack size in virtual processors created
    1.93 -#define VIRT_PROCR_STACK_SIZE 0x4000 /* 16K */
    1.94 -
    1.95 -   // memory for VMS__malloc
    1.96 -#define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
    1.97 -
    1.98 -
    1.99 -//==============================
   1.100 -
   1.101 -#define SUCCESS 0
   1.102 -
   1.103 -#define writeVMSQ     writePrivQ
   1.104 -#define readVMSQ      readPrivQ
   1.105 -#define makeVMSQ      makeVMSPrivQ
   1.106 -#define numInVMSQ     numInPrivQ
   1.107 -#define VMSQueueStruc PrivQueueStruc
   1.108 -
   1.109 -
   1.110 -
   1.111 -//===========================================================================
   1.112 -typedef unsigned long long TSCount;
   1.113 -
   1.114 -typedef struct _SchedSlot     SchedSlot;
   1.115 -typedef struct _VMSReqst      VMSReqst;
   1.116 -typedef struct _VirtProcr     VirtProcr;
   1.117 -typedef struct _IntervalProbe IntervalProbe;
   1.118 -typedef struct _GateStruc     GateStruc;
   1.119 -
   1.120 -
   1.121 -typedef VirtProcr * (*SlaveScheduler)  ( void *, int );   //semEnv, coreIdx
   1.122 -typedef void  (*RequestHandler)  ( VirtProcr *, void * ); //prWReqst, semEnv
   1.123 -typedef void  (*VirtProcrFnPtr)  ( void *, VirtProcr * ); //initData, animPr
   1.124 -typedef void    VirtProcrFn      ( void *, VirtProcr * ); //initData, animPr
   1.125 -typedef void  (*ResumePrFnPtr)   ( VirtProcr *, void * );
   1.126 -
   1.127 -
   1.128 -//============= Requests ===========
   1.129 -//
   1.130 -
   1.131 -enum VMSReqstType   //avoid starting enums at 0, for debug reasons
   1.132 - {
   1.133 -   semantic = 1,
   1.134 -   createReq,
   1.135 -   dissipate,
   1.136 -   VMSSemantic      //goes with VMSSemReqst below
   1.137 - };
   1.138 -
   1.139 -struct _VMSReqst
   1.140 - {
   1.141 -   enum VMSReqstType  reqType;//used for dissipate and in future for IO requests
   1.142 -   void              *semReqData;
   1.143 -
   1.144 -   VMSReqst *nextReqst;
   1.145 - };
   1.146 -//VMSReqst
   1.147 -
   1.148 -enum VMSSemReqstType   //These are equivalent to semantic requests, but for
   1.149 - {                     // VMS's services available directly to app, like OS
   1.150 -   createProbe = 1,    // and probe services -- like a VMS-wide built-in lang
   1.151 -   openFile,
   1.152 -   otherIO
   1.153 - };
   1.154 -
   1.155 -typedef struct
   1.156 - { enum VMSSemReqstType reqType;
   1.157 -   VirtProcr           *requestingPr;
   1.158 -   char                *nameStr;  //for create probe
   1.159 - }
   1.160 - VMSSemReq;
   1.161 -
   1.162 -
   1.163 -//====================  Core data structures  ===================
   1.164 -
   1.165 -struct _SchedSlot
   1.166 - {
   1.167 -   int         workIsDone;
   1.168 -   int         needsProcrAssigned;
   1.169 -   VirtProcr  *procrAssignedToSlot;
   1.170 - };
   1.171 -//SchedSlot
   1.172 -
   1.173 -/*WARNING: re-arranging this data structure could cause VP switching
   1.174 - *         assembly code to fail -- hard-codes offsets of fields
   1.175 - */
   1.176 -struct _VirtProcr
   1.177 - { int         procrID;  //for debugging -- count up each time create
   1.178 -   int         coreAnimatedBy;
   1.179 -   void       *startOfStack;
   1.180 -   void       *stackPtr;
   1.181 -   void       *framePtr;
   1.182 -   void       *nextInstrPt;
   1.183 -   
   1.184 -   void       *coreLoopStartPt;  //allows proto-runtime to be linked later
   1.185 -   void       *coreLoopFramePtr; //restore before jmp back to core loop
   1.186 -   void       *coreLoopStackPtr; //restore before jmp back to core loop
   1.187 -
   1.188 -   void       *initialData;
   1.189 -
   1.190 -   SchedSlot  *schedSlot;
   1.191 -   VMSReqst   *requests;
   1.192 -
   1.193 -   void       *semanticData; //this lives here for the life of VP
   1.194 -   void       *dataRetFromReq;//values returned from plugin to VP go here
   1.195 -
   1.196 -      //=========== MEASUREMENT STUFF ==========
   1.197 -   #ifdef MEAS__TIME_STAMP_SUSP
   1.198 -   unsigned int preSuspTSCLow;
   1.199 -   unsigned int postSuspTSCLow;
   1.200 -   #endif
   1.201 -   #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
   1.202 -   unsigned int startMasterTSCLow;
   1.203 -   unsigned int endMasterTSCLow;
   1.204 -   #endif
   1.205 -      //========================================
   1.206 -   
   1.207 -   float64      createPtInSecs;  //have space but don't use on some configs
   1.208 - };
   1.209 -//VirtProcr
   1.210 -
   1.211 -
   1.212 -/*WARNING: re-arranging this data structure could cause VP-switching
   1.213 - *         assembly code to fail -- hard-codes offsets of fields
   1.214 - *         (because -O3 messes with things otherwise)
   1.215 - */
   1.216 -typedef struct
   1.217 - {
   1.218 -   SlaveScheduler   slaveScheduler;
   1.219 -   RequestHandler   requestHandler;
   1.220 -   
   1.221 -   SchedSlot     ***allSchedSlots;
   1.222 -   VMSQueueStruc **readyToAnimateQs;
   1.223 -   VirtProcr      **masterVPs;
   1.224 -
   1.225 -   void            *semanticEnv;
   1.226 -   void            *OSEventStruc;   //for future, when add I/O to BLIS
   1.227 -   MallocProlog    *freeListHead;
   1.228 -   int32            amtOfOutstandingMem; //total currently allocated
   1.229 -
   1.230 -   void            *coreLoopStartPt;//addr to jump to to re-enter coreLoop
   1.231 -   void            *coreLoopEndPt;  //addr to jump to to shut down a coreLoop
   1.232 -
   1.233 -   int32            setupComplete;
   1.234 -   int32            masterLock;
   1.235 -
   1.236 -   int32            numMasterInARow[NUM_CORES];//detect back-to-back masterVP
   1.237 -   GateStruc       *workStealingGates[ NUM_CORES ]; //concurrent work-steal
   1.238 -   int32            workStealingLock;
   1.239 -   
   1.240 -   int32            numProcrsCreated; //gives ordering to processor creation
   1.241 -
   1.242 -      //=========== MEASUREMENT STUFF =============
   1.243 -   IntervalProbe  **intervalProbes;
   1.244 -   PrivDynArrayInfo    *dynIntervalProbesInfo;
   1.245 -   HashTable       *probeNameHashTbl;
   1.246 -   int32            masterCreateProbeID;
   1.247 -   float64          createPtInSecs;
   1.248 -   Histogram      **measHists;
   1.249 -   PrivDynArrayInfo *measHistsInfo;
   1.250 -   #ifdef MEAS__TIME_PLUGIN
   1.251 -   Histogram       *reqHdlrLowTimeHist;
   1.252 -   Histogram       *reqHdlrHighTimeHist;
   1.253 -   #endif
   1.254 -   #ifdef MEAS__TIME_MALLOC
   1.255 -   Histogram       *mallocTimeHist;
   1.256 -   Histogram       *freeTimeHist;
   1.257 -   #endif
   1.258 -   #ifdef MEAS__TIME_MASTER_LOCK
   1.259 -   Histogram       *masterLockLowTimeHist;
   1.260 -   Histogram       *masterLockHighTimeHist;
   1.261 -   #endif
   1.262 - }
   1.263 -MasterEnv;
   1.264 -
   1.265 -//=========================  Extra Stuff Data Strucs  =======================
   1.266 -typedef struct
   1.267 - {
   1.268 -
   1.269 - }
   1.270 -VMSExcp;
   1.271 -
   1.272 -struct _GateStruc
   1.273 - {
   1.274 -   int32 gateClosed;
   1.275 -   int32 preGateProgress;
   1.276 -   int32 waitProgress;
   1.277 -   int32 exitProgress;
   1.278 - };
   1.279 -//GateStruc
   1.280 -
   1.281 -//=======================  OS Thread related  ===============================
   1.282 -
   1.283 -void * coreLoop( void *paramsIn );  //standard PThreads fn prototype
   1.284 -void * coreLoop_Seq( void *paramsIn );  //standard PThreads fn prototype
   1.285 -void masterLoop( void *initData, VirtProcr *masterPr );
   1.286 -
   1.287 -
   1.288 -typedef struct
   1.289 - {
   1.290 -   void           *endThdPt;
   1.291 -   unsigned int    coreNum;
   1.292 - }
   1.293 -ThdParams;
   1.294 -
   1.295 -pthread_t       coreLoopThdHandles[ NUM_CORES ];  //pthread's virt-procr state
   1.296 -ThdParams      *coreLoopThdParams [ NUM_CORES ];
   1.297 -pthread_mutex_t suspendLock;
   1.298 -pthread_cond_t  suspend_cond;
   1.299 -
   1.300 -
   1.301 -
   1.302 -//=====================  Global Vars ===================
   1.303 -
   1.304 -volatile MasterEnv      *_VMSMasterEnv;
   1.305 -
   1.306 -
   1.307 -
   1.308 -
   1.309 -//===========================  Function Prototypes  =========================
   1.310 -
   1.311 -
   1.312 -//========== Setup and shutdown ==========
   1.313 -void
   1.314 -VMS__init();
   1.315 -
   1.316 -void
   1.317 -VMS__init_Seq();
   1.318 -
   1.319 -void
   1.320 -VMS__start_the_work_then_wait_until_done();
   1.321 -
   1.322 -void
   1.323 -VMS__start_the_work_then_wait_until_done_Seq();
   1.324 -
   1.325 -VirtProcr *
   1.326 -VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
   1.327 -
   1.328 -void
   1.329 -VMS__dissipate_procr( VirtProcr *procrToDissipate );
   1.330 -
   1.331 -   //Use this to create processor inside entry point & other places outside
   1.332 -   // the VMS system boundary (IE, not run in slave nor Master)
   1.333 -VirtProcr *
   1.334 -VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
   1.335 -
   1.336 -void
   1.337 -VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
   1.338 -
   1.339 -void
   1.340 -VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
   1.341 -
   1.342 -void
   1.343 -VMS__shutdown();
   1.344 -
   1.345 -void
   1.346 -VMS__cleanup_at_end_of_shutdown();
   1.347 -
   1.348 -void *
   1.349 -VMS__give_sem_env_for( VirtProcr *animPr );
   1.350 -
   1.351 -
   1.352 -//==============  Request Related  ===============
   1.353 -
   1.354 -void
   1.355 -VMS__suspend_procr( VirtProcr *callingPr );
   1.356 -
   1.357 -inline void
   1.358 -VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
   1.359 -
   1.360 -inline void
   1.361 -VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
   1.362 -
   1.363 -void
   1.364 -VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
   1.365 -
   1.366 -void inline
   1.367 -VMS__send_dissipate_req( VirtProcr *prToDissipate );
   1.368 -
   1.369 -inline void
   1.370 -VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
   1.371 -
   1.372 -VMSReqst *
   1.373 -VMS__take_next_request_out_of( VirtProcr *procrWithReq );
   1.374 -
   1.375 -inline void *
   1.376 -VMS__take_sem_reqst_from( VMSReqst *req );
   1.377 -
   1.378 -//======================== STATS ======================
   1.379 -
   1.380 -//===== RDTSC wrapper =====
   1.381 -
   1.382 -#define saveTimeStampCountInto(low, high) \
   1.383 -   asm volatile("RDTSC;                   \
   1.384 -                 movl %%eax, %0;          \
   1.385 -                 movl %%edx, %1;"         \
   1.386 -   /* outputs */ : "=m" (low), "=m" (high)\
   1.387 -   /* inputs  */ :                        \
   1.388 -   /* clobber */ : "%eax", "%edx"         \
   1.389 -                );
   1.390 -
   1.391 -#define saveLowTimeStampCountInto(low)    \
   1.392 -   asm volatile("RDTSC;                   \
   1.393 -                 movl %%eax, %0;"         \
   1.394 -   /* outputs */ : "=m" (low)             \
   1.395 -   /* inputs  */ :                        \
   1.396 -   /* clobber */ : "%eax", "%edx"         \
   1.397 -                );
   1.398 -
   1.399 -//====================
   1.400 -#define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
   1.401 -   makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
   1.402 -   _VMSMasterEnv->measHists[idx] =  \
   1.403 -                       makeFixedBinHist( numBins, startVal, binWidth, name );
   1.404 -
   1.405 -
   1.406 -#define MEAS__SUB_CREATE  /*turn on/off subtraction of create from plugin*/
   1.407 -#define createHistIdx      1
   1.408 -#define mutexLockHistIdx   2
   1.409 -#define mutexUnlockHistIdx 3
   1.410 -#define condWaitHistIdx    4
   1.411 -#define condSignalHistIdx  5
   1.412 -
   1.413 -
   1.414 -#define MakeTheMeasHists \
   1.415 -   _VMSMasterEnv->measHistsInfo = \
   1.416 -              makePrivDynArrayOfSize( &(_VMSMasterEnv->measHists), 200);\
   1.417 -   makeAMeasHist( createHistIdx,      "Create",        50, 0, 200 ) \
   1.418 -   makeAMeasHist( mutexLockHistIdx,   "mutex lock",    50, 0, 100 ) \
   1.419 -   makeAMeasHist( mutexUnlockHistIdx, "mutex unlock",  50, 0, 100 ) \
   1.420 -   makeAMeasHist( condWaitHistIdx,    "cond wait",     50, 0, 100 ) \
   1.421 -   makeAMeasHist( condSignalHistIdx,  "cond signal",   50, 0, 100 )
   1.422 -
   1.423 -#define Meas_startCreate \
   1.424 -    int32 startStamp, endStamp; \
   1.425 -    saveLowTimeStampCountInto( startStamp ); \
   1.426 -
   1.427 -#define Meas_endCreate \
   1.428 -    saveLowTimeStampCountInto( endStamp ); \
   1.429 -    addIntervalToHist( startStamp, endStamp, \
   1.430 -                                 _VMSMasterEnv->measHists[ createHistIdx ] );
   1.431 -
   1.432 -#define Meas_startMutexLock \
   1.433 -    int32 startStamp, endStamp; \
   1.434 -    saveLowTimeStampCountInto( startStamp ); \
   1.435 -
   1.436 -#define Meas_endMutexLock \
   1.437 -    saveLowTimeStampCountInto( endStamp ); \
   1.438 -    addIntervalToHist( startStamp, endStamp, \
   1.439 -                              _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
   1.440 -
   1.441 -#define Meas_startMutexUnlock \
   1.442 -    int32 startStamp, endStamp; \
   1.443 -    saveLowTimeStampCountInto( startStamp ); \
   1.444 -
   1.445 -#define Meas_endMutexUnlock \
   1.446 -    saveLowTimeStampCountInto( endStamp ); \
   1.447 -    addIntervalToHist( startStamp, endStamp, \
   1.448 -                            _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
   1.449 -
   1.450 -#define Meas_startCondWait \
   1.451 -    int32 startStamp, endStamp; \
   1.452 -    saveLowTimeStampCountInto( startStamp ); \
   1.453 -
   1.454 -#define Meas_endCondWait \
   1.455 -    saveLowTimeStampCountInto( endStamp ); \
   1.456 -    addIntervalToHist( startStamp, endStamp, \
   1.457 -                               _VMSMasterEnv->measHists[ condWaitHistIdx ] );
   1.458 -
   1.459 -#define Meas_startCondSignal \
   1.460 -    int32 startStamp, endStamp; \
   1.461 -    saveLowTimeStampCountInto( startStamp ); \
   1.462 -
   1.463 -#define Meas_endCondSignal \
   1.464 -    saveLowTimeStampCountInto( endStamp ); \
   1.465 -    addIntervalToHist( startStamp, endStamp, \
   1.466 -                             _VMSMasterEnv->measHists[ condSignalHistIdx ] );
   1.467 -
   1.468 -
   1.469 -//=====
   1.470 -
   1.471 -#include "SwitchAnimators.h"
   1.472 -#include "probes.h"
   1.473 -#include "vutilities.h"
   1.474 -
   1.475 -#endif	/* _VMS_H */
   1.476 -
   1.477 +/*
   1.478 + *  Copyright 2009 OpenSourceStewardshipFoundation.org
   1.479 + *  Licensed under GNU General Public License version 2
   1.480 + *
   1.481 + * Author: seanhalle@yahoo.com
   1.482 + * 
   1.483 + */
   1.484 +
   1.485 +#ifndef _VMS_H
   1.486 +#define	_VMS_H
   1.487 +#define __USE_GNU
   1.488 +
   1.489 +#include "VMS_primitive_data_types.h"
   1.490 +#include "Queue_impl/PrivateQueue.h"
   1.491 +#include "Histogram/Histogram.h"
   1.492 +#include "DynArray/DynArray.h"
   1.493 +#include "Hash_impl/PrivateHash.h"
   1.494 +#include "vmalloc.h"
   1.495 +
   1.496 +#include <pthread.h>
   1.497 +#include <sys/time.h>
   1.498 +
   1.499 +
   1.500 +//===============================  Debug  ===================================
   1.501 +//
   1.502 +   //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
   1.503 +   // It still does co-routines and all the mechanisms are the same, it just
   1.504 +   // has only a single thread and animates VPs one at a time
   1.505 +//#define SEQUENTIAL
   1.506 +
   1.507 +//#define USE_WORK_STEALING
   1.508 +
   1.509 +   //turns on the probe-instrumentation in the application -- when not
   1.510 +   // defined, the calls to the probe functions turn into comments
   1.511 +#define STATS__ENABLE_PROBES
   1.512 +//#define TURN_ON_DEBUG_PROBES
   1.513 +
   1.514 +   //These defines turn types of bug messages on and off
   1.515 +   // be sure debug messages are un-commented (next block of defines)
   1.516 +#define dbgAppFlow   TRUE /* Top level flow of application code -- general*/
   1.517 +#define dbgProbes    FALSE /* for issues inside probes themselves*/
   1.518 +#define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
   1.519 +#define dbgRqstHdlr  FALSE /* in request handler code*/
   1.520 +
   1.521 +   //Comment or un- the substitute half to turn on/off types of debug message
   1.522 +#define DEBUG(  bool, msg)         \
   1.523 +//  if( bool){ printf(msg); fflush(stdin);}
   1.524 +#define DEBUG1( bool, msg, param)  \
   1.525 +//   if(bool){printf(msg, param); fflush(stdin);}
   1.526 +#define DEBUG2( bool, msg, p1, p2) \
   1.527 +//   if(bool) {printf(msg, p1, p2); fflush(stdin);}
   1.528 +
   1.529 +#define ERROR(msg) printf(msg);
   1.530 +#define ERROR1(msg, param) printf(msg, param);
   1.531 +#define ERROR2(msg, p1, p2) printf(msg, p1, p2);
   1.532 +
   1.533 +//===========================  STATS =======================
   1.534 +
   1.535 +   //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
   1.536 +   // compiled-in that saves the low part of the time stamp count just before
   1.537 +   // suspending a processor and just after resuming that processor.  It is
   1.538 +   // saved into a field added to VirtProcr.  Have to sanity-check for
   1.539 +   // rollover of low portion into high portion.
   1.540 +//#define MEAS__TIME_STAMP_SUSP
   1.541 +//#define MEAS__TIME_MASTER
   1.542 +#define MEAS__TIME_PLUGIN
   1.543 +#define MEAS__TIME_MALLOC
   1.544 +//#define MEAS__TIME_MASTER_LOCK
   1.545 +#define MEAS__NUM_TIMES_TO_RUN 100000
   1.546 +
   1.547 +   //For code that calculates normalization-offset between TSC counts of
   1.548 +   // different cores.
   1.549 +#define NUM_TSC_ROUND_TRIPS 10
   1.550 +
   1.551 +
   1.552 +//=========================  Hardware related Constants =====================
   1.553 +   //This value is the number of hardware threads in the shared memory
   1.554 +   // machine
   1.555 +#define NUM_CORES        4
   1.556 +
   1.557 +   // tradeoff amortizing master fixed overhead vs imbalance potential
   1.558 +   // when work-stealing, can make bigger, at risk of losing cache affinity
   1.559 +#define NUM_SCHED_SLOTS  5
   1.560 +
   1.561 +#define MIN_WORK_UNIT_CYCLES 20000
   1.562 +
   1.563 +#define MASTERLOCK_RETRIES 10000
   1.564 +
   1.565 +   // stack size in virtual processors created
   1.566 +#define VIRT_PROCR_STACK_SIZE 0x4000 /* 16K */
   1.567 +
   1.568 +   // memory for VMS__malloc
   1.569 +#define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
   1.570 +
   1.571 +
   1.572 +//==============================
   1.573 +
   1.574 +#define SUCCESS 0
   1.575 +
   1.576 +#define writeVMSQ     writePrivQ
   1.577 +#define readVMSQ      readPrivQ
   1.578 +#define makeVMSQ      makeVMSPrivQ
   1.579 +#define numInVMSQ     numInPrivQ
   1.580 +#define VMSQueueStruc PrivQueueStruc
   1.581 +
   1.582 +
   1.583 +
   1.584 +//===========================================================================
   1.585 +typedef unsigned long long TSCount;
   1.586 +
   1.587 +typedef struct _SchedSlot     SchedSlot;
   1.588 +typedef struct _VMSReqst      VMSReqst;
   1.589 +typedef struct _VirtProcr     VirtProcr;
   1.590 +typedef struct _IntervalProbe IntervalProbe;
   1.591 +typedef struct _GateStruc     GateStruc;
   1.592 +
   1.593 +
   1.594 +typedef VirtProcr * (*SlaveScheduler)  ( void *, int );   //semEnv, coreIdx
   1.595 +typedef void  (*RequestHandler)  ( VirtProcr *, void * ); //prWReqst, semEnv
   1.596 +typedef void  (*VirtProcrFnPtr)  ( void *, VirtProcr * ); //initData, animPr
   1.597 +typedef void    VirtProcrFn      ( void *, VirtProcr * ); //initData, animPr
   1.598 +typedef void  (*ResumePrFnPtr)   ( VirtProcr *, void * );
   1.599 +
   1.600 +
   1.601 +//============= Requests ===========
   1.602 +//
   1.603 +
   1.604 +enum VMSReqstType   //avoid starting enums at 0, for debug reasons
   1.605 + {
   1.606 +   semantic = 1,
   1.607 +   createReq,
   1.608 +   dissipate,
   1.609 +   VMSSemantic      //goes with VMSSemReqst below
   1.610 + };
   1.611 +
   1.612 +struct _VMSReqst
   1.613 + {
   1.614 +   enum VMSReqstType  reqType;//used for dissipate and in future for IO requests
   1.615 +   void              *semReqData;
   1.616 +
   1.617 +   VMSReqst *nextReqst;
   1.618 + };
   1.619 +//VMSReqst
   1.620 +
   1.621 +enum VMSSemReqstType   //These are equivalent to semantic requests, but for
   1.622 + {                     // VMS's services available directly to app, like OS
   1.623 +   createProbe = 1,    // and probe services -- like a VMS-wide built-in lang
   1.624 +   openFile,
   1.625 +   otherIO
   1.626 + };
   1.627 +
   1.628 +typedef struct
   1.629 + { enum VMSSemReqstType reqType;
   1.630 +   VirtProcr           *requestingPr;
   1.631 +   char                *nameStr;  //for create probe
   1.632 + }
   1.633 + VMSSemReq;
   1.634 +
   1.635 +
   1.636 +//====================  Core data structures  ===================
   1.637 +
   1.638 +struct _SchedSlot
   1.639 + {
   1.640 +   int         workIsDone;
   1.641 +   int         needsProcrAssigned;
   1.642 +   VirtProcr  *procrAssignedToSlot;
   1.643 + };
   1.644 +//SchedSlot
   1.645 +
   1.646 +/*WARNING: re-arranging this data structure could cause VP switching
   1.647 + *         assembly code to fail -- hard-codes offsets of fields
   1.648 + */
   1.649 +struct _VirtProcr
   1.650 + { int         procrID;  //for debugging -- count up each time create
   1.651 +   int         coreAnimatedBy;
   1.652 +   void       *startOfStack;
   1.653 +   void       *stackPtr;
   1.654 +   void       *framePtr;
   1.655 +   void       *nextInstrPt;
   1.656 +   
   1.657 +   void       *coreLoopStartPt;  //allows proto-runtime to be linked later
   1.658 +   void       *coreLoopFramePtr; //restore before jmp back to core loop
   1.659 +   void       *coreLoopStackPtr; //restore before jmp back to core loop
   1.660 +
   1.661 +   void       *initialData;
   1.662 +
   1.663 +   SchedSlot  *schedSlot;
   1.664 +   VMSReqst   *requests;
   1.665 +
   1.666 +   void       *semanticData; //this lives here for the life of VP
   1.667 +   void       *dataRetFromReq;//values returned from plugin to VP go here
   1.668 +
   1.669 +      //=========== MEASUREMENT STUFF ==========
   1.670 +   #ifdef MEAS__TIME_STAMP_SUSP
   1.671 +   unsigned int preSuspTSCLow;
   1.672 +   unsigned int postSuspTSCLow;
   1.673 +   #endif
   1.674 +   #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
   1.675 +   unsigned int startMasterTSCLow;
   1.676 +   unsigned int endMasterTSCLow;
   1.677 +   #endif
   1.678 +      //========================================
   1.679 +   
   1.680 +   float64      createPtInSecs;  //have space but don't use on some configs
   1.681 + };
   1.682 +//VirtProcr
   1.683 +
   1.684 +
   1.685 +/*WARNING: re-arranging this data structure could cause VP-switching
   1.686 + *         assembly code to fail -- hard-codes offsets of fields
   1.687 + *         (because -O3 messes with things otherwise)
   1.688 + */
   1.689 +typedef struct
   1.690 + {
   1.691 +   SlaveScheduler   slaveScheduler;
   1.692 +   RequestHandler   requestHandler;
   1.693 +   
   1.694 +   SchedSlot     ***allSchedSlots;
   1.695 +   VMSQueueStruc **readyToAnimateQs;
   1.696 +   VirtProcr      **masterVPs;
   1.697 +
   1.698 +   void            *semanticEnv;
   1.699 +   void            *OSEventStruc;   //for future, when add I/O to BLIS
   1.700 +   MallocProlog    *freeListHead;
   1.701 +   int32            amtOfOutstandingMem; //total currently allocated
   1.702 +
   1.703 +   void            *coreLoopStartPt;//addr to jump to to re-enter coreLoop
   1.704 +   void            *coreLoopEndPt;  //addr to jump to to shut down a coreLoop
   1.705 +
   1.706 +   int32            setupComplete;
   1.707 +   int32            masterLock;
   1.708 +
   1.709 +   int32            numMasterInARow[NUM_CORES];//detect back-to-back masterVP
   1.710 +   GateStruc       *workStealingGates[ NUM_CORES ]; //concurrent work-steal
   1.711 +   int32            workStealingLock;
   1.712 +   
   1.713 +   int32            numProcrsCreated; //gives ordering to processor creation
   1.714 +
   1.715 +      //=========== MEASUREMENT STUFF =============
   1.716 +   IntervalProbe  **intervalProbes;
   1.717 +   PrivDynArrayInfo    *dynIntervalProbesInfo;
   1.718 +   HashTable       *probeNameHashTbl;
   1.719 +   int32            masterCreateProbeID;
   1.720 +   float64          createPtInSecs;
   1.721 +   Histogram      **measHists;
   1.722 +   PrivDynArrayInfo *measHistsInfo;
   1.723 +   #ifdef MEAS__TIME_PLUGIN
   1.724 +   Histogram       *reqHdlrLowTimeHist;
   1.725 +   Histogram       *reqHdlrHighTimeHist;
   1.726 +   #endif
   1.727 +   #ifdef MEAS__TIME_MALLOC
   1.728 +   Histogram       *mallocTimeHist;
   1.729 +   Histogram       *freeTimeHist;
   1.730 +   #endif
   1.731 +   #ifdef MEAS__TIME_MASTER_LOCK
   1.732 +   Histogram       *masterLockLowTimeHist;
   1.733 +   Histogram       *masterLockHighTimeHist;
   1.734 +   #endif
   1.735 + }
   1.736 +MasterEnv;
   1.737 +
   1.738 +//=========================  Extra Stuff Data Strucs  =======================
   1.739 +typedef struct
   1.740 + {
   1.741 +
   1.742 + }
   1.743 +VMSExcp;
   1.744 +
   1.745 +struct _GateStruc
   1.746 + {
   1.747 +   int32 gateClosed;
   1.748 +   int32 preGateProgress;
   1.749 +   int32 waitProgress;
   1.750 +   int32 exitProgress;
   1.751 + };
   1.752 +//GateStruc
   1.753 +
   1.754 +//=======================  OS Thread related  ===============================
   1.755 +
   1.756 +void * coreLoop( void *paramsIn );  //standard PThreads fn prototype
   1.757 +void * coreLoop_Seq( void *paramsIn );  //standard PThreads fn prototype
   1.758 +void masterLoop( void *initData, VirtProcr *masterPr );
   1.759 +
   1.760 +
   1.761 +typedef struct
   1.762 + {
   1.763 +   void           *endThdPt;
   1.764 +   unsigned int    coreNum;
   1.765 + }
   1.766 +ThdParams;
   1.767 +
   1.768 +pthread_t       coreLoopThdHandles[ NUM_CORES ];  //pthread's virt-procr state
   1.769 +ThdParams      *coreLoopThdParams [ NUM_CORES ];
   1.770 +pthread_mutex_t suspendLock;
   1.771 +pthread_cond_t  suspend_cond;
   1.772 +
   1.773 +
   1.774 +
   1.775 +//=====================  Global Vars ===================
   1.776 +
   1.777 +volatile MasterEnv      *_VMSMasterEnv;
   1.778 +
   1.779 +
   1.780 +
   1.781 +
   1.782 +//===========================  Function Prototypes  =========================
   1.783 +
   1.784 +
   1.785 +//========== Setup and shutdown ==========
   1.786 +void
   1.787 +VMS__init();
   1.788 +
   1.789 +void
   1.790 +VMS__init_Seq();
   1.791 +
   1.792 +void
   1.793 +VMS__start_the_work_then_wait_until_done();
   1.794 +
   1.795 +void
   1.796 +VMS__start_the_work_then_wait_until_done_Seq();
   1.797 +
   1.798 +VirtProcr *
   1.799 +VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
   1.800 +
   1.801 +void
   1.802 +VMS__dissipate_procr( VirtProcr *procrToDissipate );
   1.803 +
   1.804 +   //Use this to create processor inside entry point & other places outside
   1.805 +   // the VMS system boundary (IE, not run in slave nor Master)
   1.806 +VirtProcr *
   1.807 +VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
   1.808 +
   1.809 +void
   1.810 +VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
   1.811 +
   1.812 +void
   1.813 +VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
   1.814 +
   1.815 +void
   1.816 +VMS__shutdown();
   1.817 +
   1.818 +void
   1.819 +VMS__cleanup_at_end_of_shutdown();
   1.820 +
   1.821 +void *
   1.822 +VMS__give_sem_env_for( VirtProcr *animPr );
   1.823 +
   1.824 +
   1.825 +//==============  Request Related  ===============
   1.826 +
   1.827 +void
   1.828 +VMS__suspend_procr( VirtProcr *callingPr );
   1.829 +
   1.830 +inline void
   1.831 +VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
   1.832 +
   1.833 +inline void
   1.834 +VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
   1.835 +
   1.836 +void
   1.837 +VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
   1.838 +
   1.839 +void inline
   1.840 +VMS__send_dissipate_req( VirtProcr *prToDissipate );
   1.841 +
   1.842 +inline void
   1.843 +VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
   1.844 +
   1.845 +VMSReqst *
   1.846 +VMS__take_next_request_out_of( VirtProcr *procrWithReq );
   1.847 +
   1.848 +inline void *
   1.849 +VMS__take_sem_reqst_from( VMSReqst *req );
   1.850 +
   1.851 +//======================== STATS ======================
   1.852 +
   1.853 +//===== RDTSC wrapper =====
   1.854 +
   1.855 +#define saveTimeStampCountInto(low, high) \
   1.856 +   asm volatile("RDTSC;                   \
   1.857 +                 movl %%eax, %0;          \
   1.858 +                 movl %%edx, %1;"         \
   1.859 +   /* outputs */ : "=m" (low), "=m" (high)\
   1.860 +   /* inputs  */ :                        \
   1.861 +   /* clobber */ : "%eax", "%edx"         \
   1.862 +                );
   1.863 +
   1.864 +#define saveLowTimeStampCountInto(low)    \
   1.865 +   asm volatile("RDTSC;                   \
   1.866 +                 movl %%eax, %0;"         \
   1.867 +   /* outputs */ : "=m" (low)             \
   1.868 +   /* inputs  */ :                        \
   1.869 +   /* clobber */ : "%eax", "%edx"         \
   1.870 +                );
   1.871 +
   1.872 +//====================
   1.873 +#define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
   1.874 +   makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
   1.875 +   _VMSMasterEnv->measHists[idx] =  \
   1.876 +                       makeFixedBinHist( numBins, startVal, binWidth, name );
   1.877 +
   1.878 +
   1.879 +#define MEAS__SUB_CREATE  /*turn on/off subtraction of create from plugin*/
   1.880 +#define createHistIdx      0
   1.881 +#define mutexLockHistIdx   1
   1.882 +#define mutexUnlockHistIdx 2
   1.883 +#define condWaitHistIdx    3
   1.884 +#define condSignalHistIdx  4
   1.885 +
   1.886 +
   1.887 +#define MakeTheMeasHists \
   1.888 +   _VMSMasterEnv->measHistsInfo = \
   1.889 +              makePrivDynArrayOfSize( &(_VMSMasterEnv->measHists), 200);\
   1.890 +   makeAMeasHist( createHistIdx,      "Create",        50, 0, 200 ) \
   1.891 +   makeAMeasHist( mutexLockHistIdx,   "mutex lock",    50, 0, 100 ) \
   1.892 +   makeAMeasHist( mutexUnlockHistIdx, "mutex unlock",  50, 0, 100 ) \
   1.893 +   makeAMeasHist( condWaitHistIdx,    "cond wait",     50, 0, 100 ) \
   1.894 +   makeAMeasHist( condSignalHistIdx,  "cond signal",   50, 0, 100 )
   1.895 +
   1.896 +#define Meas_startCreate \
   1.897 +    int32 startStamp, endStamp; \
   1.898 +    saveLowTimeStampCountInto( startStamp ); \
   1.899 +
   1.900 +#define Meas_endCreate \
   1.901 +    saveLowTimeStampCountInto( endStamp ); \
   1.902 +    addIntervalToHist( startStamp, endStamp, \
   1.903 +                                 _VMSMasterEnv->measHists[ createHistIdx ] );
   1.904 +
   1.905 +#define Meas_startMutexLock \
   1.906 +    int32 startStamp, endStamp; \
   1.907 +    saveLowTimeStampCountInto( startStamp ); \
   1.908 +
   1.909 +#define Meas_endMutexLock \
   1.910 +    saveLowTimeStampCountInto( endStamp ); \
   1.911 +    addIntervalToHist( startStamp, endStamp, \
   1.912 +                              _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
   1.913 +
   1.914 +#define Meas_startMutexUnlock \
   1.915 +    int32 startStamp, endStamp; \
   1.916 +    saveLowTimeStampCountInto( startStamp ); \
   1.917 +
   1.918 +#define Meas_endMutexUnlock \
   1.919 +    saveLowTimeStampCountInto( endStamp ); \
   1.920 +    addIntervalToHist( startStamp, endStamp, \
   1.921 +                            _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
   1.922 +
   1.923 +#define Meas_startCondWait \
   1.924 +    int32 startStamp, endStamp; \
   1.925 +    saveLowTimeStampCountInto( startStamp ); \
   1.926 +
   1.927 +#define Meas_endCondWait \
   1.928 +    saveLowTimeStampCountInto( endStamp ); \
   1.929 +    addIntervalToHist( startStamp, endStamp, \
   1.930 +                               _VMSMasterEnv->measHists[ condWaitHistIdx ] );
   1.931 +
   1.932 +#define Meas_startCondSignal \
   1.933 +    int32 startStamp, endStamp; \
   1.934 +    saveLowTimeStampCountInto( startStamp ); \
   1.935 +
   1.936 +#define Meas_endCondSignal \
   1.937 +    saveLowTimeStampCountInto( endStamp ); \
   1.938 +    addIntervalToHist( startStamp, endStamp, \
   1.939 +                             _VMSMasterEnv->measHists[ condSignalHistIdx ] );
   1.940 +
   1.941 +
   1.942 +//=====
   1.943 +
   1.944 +#include "SwitchAnimators.h"
   1.945 +#include "probes.h"
   1.946 +#include "vutilities.h"
   1.947 +
   1.948 +#endif	/* _VMS_H */
   1.949 +