# HG changeset patch # User Sean Halle # Date 1338413027 25200 # Node ID f1267bc7b3428573f29f5f377614e32c6b5074a4 # Parent 5e315ce69d8221e6e76e93614357357b4b937087 added exceptions, make malloc in WL and App get master lock, added rand num diff -r 5e315ce69d82 -r f1267bc7b342 CoreController.c --- a/CoreController.c Fri May 25 12:23:03 2012 +0200 +++ b/CoreController.c Wed May 30 14:23:47 2012 -0700 @@ -72,7 +72,7 @@ int32 thisCoresIdx; int32 numRepetitionsWithNoWork; SlaveVP *currVP; - AnimSlot *currSlot, **animSlots; + AnimSlot *currSlot, **animSlots; int32 currSlotIdx; volatile int32 *addrOfMasterLock; //thing pointed to is volatile, not ptr SlaveVP *thisCoresMasterVP; @@ -223,18 +223,6 @@ } -/*Used by the backoff to pick a random amount of busy-wait. Can't use the - * system rand because it takes much too long. - *Note, are passing pointers to the seeds, which are then modified - */ -inline uint32_t -randomNumber(uint32_t* seed1, uint32_t* seed2) - { - *seed1 = 36969 * (*seed1 & 65535) + (*seed1 >> 16); - *seed2 = 18000 * (*seed2 & 65535) + (*seed2 >> 16); - return (*seed1 << 16) + *seed2; - } - /*Busy-wait for a random number of cycles -- chooses number of cycles * differently than for the too-many-tries-to-get-lock backoff */ @@ -265,7 +253,7 @@ waitIterations = randomNumber(seed1, seed2) % - (numTriesToGetLock * NUM_TRIES_TO_GET_LOCK_BACKOFF_WEIGHT); + (numTriesToGetLock * GET_LOCK_BACKOFF_WEIGHT); //addToHist( wait_iterations, coreLoopThdParams->wait_iterations_hist ); for( i = 0; i < waitIterations; i++ ) { fakeWorkVar += (fakeWorkVar + 32.0) / 2.0; //busy-wait diff -r 5e315ce69d82 -r f1267bc7b342 Defines/VMS_defs__HW_constants.h --- a/Defines/VMS_defs__HW_constants.h Fri May 25 12:23:03 2012 +0200 +++ b/Defines/VMS_defs__HW_constants.h Wed May 30 14:23:47 2012 -0700 @@ -25,7 +25,7 @@ #define NUM_REPS_W_NO_WORK_BEFORE_BACKOFF 2 #define MASTERLOCK_RETRIES_BEFORE_YIELD 100 #define NUM_TRIES_BEFORE_DO_BACKOFF 10 -#define NUM_TRIES_TO_GET_LOCK_BACKOFF_WEIGHT 100 +#define GET_LOCK_BACKOFF_WEIGHT 100 // stack size in virtual processors created #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */ diff -r 5e315ce69d82 -r f1267bc7b342 Services_Offered_by_VMS/Measurement_and_Stats/probes.c --- a/Services_Offered_by_VMS/Measurement_and_Stats/probes.c Fri May 25 12:23:03 2012 +0200 +++ b/Services_Offered_by_VMS/Measurement_and_Stats/probes.c Wed May 30 14:23:47 2012 -0700 @@ -51,7 +51,7 @@ { VMSSemReq reqData; - reqData.reqType = createProbe; + reqData.reqType = make_probe; reqData.nameStr = nameStr; VMS_WL__send_VMSSem_request( &reqData, animSlv ); diff -r 5e315ce69d82 -r f1267bc7b342 Services_Offered_by_VMS/Memory_Handling/vmalloc.c --- a/Services_Offered_by_VMS/Memory_Handling/vmalloc.c Fri May 25 12:23:03 2012 +0200 +++ b/Services_Offered_by_VMS/Memory_Handling/vmalloc.c Wed May 30 14:23:47 2012 -0700 @@ -284,6 +284,15 @@ return foundChunk + 1; } +void * +VMS_WL__malloc( int32 sizeRequested ) + { + VMS_int__get_master_lock(); + VMS_int__malloc( sizeRequested ); + VMS_int__release_master_lock(); + } + + /* * This is sequential code, meant to only be called from the Master, not from * any slave Slvs. @@ -337,6 +346,14 @@ MEAS__Capture_Post_Free_Point; } +void +VMS_WL__free( void *ptrToFree ) + { + VMS_int__get_master_lock(); + VMS_int__free( ptrToFree ); + VMS_int__release_master_lock(); + } + /* * Designed to be called from the main thread outside of VMS, during init */ diff -r 5e315ce69d82 -r f1267bc7b342 Services_Offered_by_VMS/Memory_Handling/vmalloc.h --- a/Services_Offered_by_VMS/Memory_Handling/vmalloc.h Fri May 25 12:23:03 2012 +0200 +++ b/Services_Offered_by_VMS/Memory_Handling/vmalloc.h Wed May 30 14:23:47 2012 -0700 @@ -55,19 +55,22 @@ void * VMS_int__malloc( size_t sizeRequested ); #define VMS_PI__malloc VMS_int__malloc -#define VMS_WL__malloc VMS_int__malloc /*TODO: Bug -- get master lock */ -#define VMS_App__malloc VMS_int__malloc /*TODO: Bug -- get master lock */ + +void * +VMS_WL__malloc( int32 sizeRequested ); /*BUG: -- get master lock */ +#define VMS_App__malloc VMS_WL__malloc void * VMS_int__malloc_aligned( size_t sizeRequested ); #define VMS_PI__malloc_aligned VMS_int__malloc_aligned -#define VMS_WL__malloc_aligned VMS_int__malloc_aligned void VMS_int__free( void *ptrToFree ); #define VMS_PI__free VMS_int__free -#define VMS_WL__free VMS_int__free /*TODO: Bug -- Not protected!! */ -#define VMS_App__free VMS_int__free /*TODO: Bug -- Not protected!! */ + +void +VMS_WL__free( void *ptrToFree ); +#define VMS_App__free VMS_WL__free diff -r 5e315ce69d82 -r f1267bc7b342 VMS.h --- a/VMS.h Fri May 25 12:23:03 2012 +0200 +++ b/VMS.h Wed May 30 14:23:47 2012 -0700 @@ -76,15 +76,18 @@ enum VMSSemReqstType //These are equivalent to semantic requests, but for { // VMS's services available directly to app, like OS - createProbe = 1, // and probe services -- like a VMS-wide built-in lang + make_probe = 1, // and probe services -- like a VMS-wide built-in lang + throw_excp, openFile, otherIO }; typedef struct { enum VMSSemReqstType reqType; - SlaveVP *requestingSlv; + SlaveVP *requestingSlv; char *nameStr; //for create probe + char *msgStr; //for exception + void *exceptionData; } VMSSemReq; @@ -182,7 +185,11 @@ //Memory management related MallocArrays *freeLists; int32 amtOfOutstandingMem;//total currently allocated - + + //Random number seeds -- random nums used in various places + uint32_t seed1; + uint32_t seed2; + //=========== MEASUREMENT STUFF ============= IntervalProbe **intervalProbes; PrivDynArrayInfo *dynIntervalProbesInfo; @@ -311,8 +318,10 @@ void VMS_int__throw_exception( char *msgStr, SlaveVP *reqstSlv, VMSExcp *excpData ); -#define VMS_PI__throw_exception VMS_int__throw_exception -#define VMS_WL__throw_exception VMS_int__throw_exception +#define VMS_PI__throw_exception VMS_int__throw_exception +void +VMS_WL__throw_exception( char *msgStr, SlaveVP *reqstSlv, VMSExcp *excpData ); +#define VMS_App__throw_exception VMS_WL__throw_exception void * VMS_int__give_sem_env_for( SlaveVP *animSlv ); @@ -320,6 +329,15 @@ #define VMS_SS__give_sem_env_for VMS_int__give_sem_env_for //No WL version -- not safe! if use in WL, be sure data rd & wr is stable + +inline void +VMS_int__get_master_lock(); + +#define VMS_int__release_master_lock() _VMSMasterEnv->masterLock = UNLOCKED + +inline uint32_t +VMS_int__randomNumber(); + //============== Request Related =============== void diff -r 5e315ce69d82 -r f1267bc7b342 VMS__PI.c --- a/VMS__PI.c Fri May 25 12:23:03 2012 +0200 +++ b/VMS__PI.c Wed May 30 14:23:47 2012 -0700 @@ -22,9 +22,17 @@ * int: internal to the VMS implementation */ +//========================= Local Declarations ======================== +void inline +handleMakeProbe( VMSSemReq *semReq, void *semEnv, ResumeSlvFnPtr resumeFn ); -/* - */ +void inline +handleThrowException( VMSSemReq *semReq, void *semEnv, ResumeSlvFnPtr resumeFn ); +//======================================================================= + +/*May 2012 + *DEPRECATED -- turn into a macro, that just accesses the request field + VMSReqst * VMS_PI__take_next_request_out_of( SlaveVP *slaveWithReq ) { VMSReqst *req; @@ -35,14 +43,22 @@ slaveWithReq->requests = slaveWithReq->requests->nextReqst; return req; } +*/ +#define VMS_PI__take_next_request_out_of( slave ) slave->requests + - +/*May 2012 + *DEPRECATED + * + *Turn function into macro that just accesses the request field + * inline void * VMS_PI__take_sem_reqst_from( VMSReqst *req ) { return req->semReqData; } - +*/ +#define VMS_PI__take_sem_reqst_from( req ) req->semReqData /* This is for OS requests and VMS infrastructure requests, such as to create @@ -61,11 +77,25 @@ */ void inline VMS_PI__handle_VMSSemReq( VMSReqst *req, SlaveVP *requestingSlv, void *semEnv, - ResumeSlvFnPtr resumeSlvFnPtr ) - { VMSSemReq *semReq; - IntervalProbe *newProbe; + ResumeSlvFnPtr resumeFn ) + { VMSSemReq *semReq; - semReq = req->semReqData; + semReq = VMS_PI__take_sem_reqst_from(req); + if( semReq == NULL ) return; + switch( semReq->reqType ) //sem handlers are all in other file + { + case make_probe: handleMakeProbe( semReq, semEnv, resumeFn); + break; + case throw_excp: handleThrowException( semReq, semEnv, resumeFn); + break; + } + } + +/* + */ +void inline +handleMakeProbe( VMSSemReq *semReq, void *semEnv, ResumeSlvFnPtr resumeFn ) + { IntervalProbe *newProbe; newProbe = VMS_int__malloc( sizeof(IntervalProbe) ); newProbe->nameStr = VMS_int__strDup( semReq->nameStr ); @@ -74,23 +104,22 @@ //This runs in masterVP, so no race-condition worries newProbe->probeID = - addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo ); + addToDynArray( newProbe, _VMSMasterEnv->dynIntervalProbesInfo ); - requestingSlv->dataRetFromReq = newProbe; + semReq->requestingSlv->dataRetFromReq = newProbe; - (*resumeSlvFnPtr)( requestingSlv, semEnv ); + //This in inside VMS, while resume_slaveVP fn is inside language, so pass + // pointer from lang to here, then call it. + (*resumeFn)( semReq->requestingSlv, semEnv ); } - -/*Later, improve this -- for now, just exits the application after printing - * the error message. - */ -void -VMS_PI__throw_exception( char *msgStr, SlaveVP *reqstSlv, VMSExcp *excpData ) +void inline +handleThrowException( VMSSemReq *semReq, void *semEnv, ResumeSlvFnPtr resumeFn ) { - printf("%s",msgStr); - fflush(stdin); - exit(1); + VMS_int__throw_exception( semReq->msgStr, semReq->requestingSlv, semReq->exceptionData ); + + (*resumeFn)( semReq->requestingSlv, semEnv ); } + diff -r 5e315ce69d82 -r f1267bc7b342 VMS__WL.c --- a/VMS__WL.c Fri May 25 12:23:03 2012 +0200 +++ b/VMS__WL.c Wed May 30 14:23:47 2012 -0700 @@ -124,6 +124,9 @@ } +/*May 2012 Not sure what this is.. looks like old idea for VMS semantic + * request + */ inline void VMS_WL__send_VMSSem_request( void *semReqData, SlaveVP *callingSlv ) { VMSReqst req; @@ -136,4 +139,21 @@ VMS_int__suspend_slaveVP_and_send_req( callingSlv ); } +/*May 2012 + *To throw exception from wrapper lib or application, first turn + * it into a request, then send the request + */ +VMS_WL__throw_exception( char *msgStr, SlaveVP *reqstSlv, VMSExcp *excpData ) + { VMSReqst req; + VMSSemReq semReq; + req.reqType = VMSSemantic; + req.semReqData = &semReq; + req.nextReqst = reqstSlv->requests; //gab any other preceeding + reqstSlv->requests = &req; + + semReq.msgStr = msgStr; + semReq.exceptionData = excpData; + + VMS_int__suspend_slaveVP_and_send_req( reqstSlv ); + } diff -r 5e315ce69d82 -r f1267bc7b342 VMS__int.c --- a/VMS__int.c Fri May 25 12:23:03 2012 +0200 +++ b/VMS__int.c Wed May 30 14:23:47 2012 -0700 @@ -200,6 +200,18 @@ } +/*Later, improve this -- for now, just exits the application after printing + * the error message. + */ +void +VMS_int__throw_exception( char *msgStr, SlaveVP *reqstSlv, VMSExcp *excpData ) + { + printf("%s",msgStr); + fflush(stdin); + exit(1); + } + + inline char * VMS_int__strDup( char *str ) { char *retStr; @@ -210,3 +222,67 @@ return (char *)retStr; } + + +inline void +VMS_int__backoff_for_TooLongToGetLock( int32 numTriesToGetLock ); + +inline void +VMS_int__get_master_lock() + { int32 *addrOfMasterLock; + + addrOfMasterLock = &(_VMSMasterEnv->masterLock); + + int numTriesToGetLock = 0; + int gotLock = 0; + + MEAS__Capture_Pre_Master_Lock_Point; + + while( !gotLock ) //keep going until get master lock + { + numTriesToGetLock++; //if too many, means too much contention + if( numTriesToGetLock > NUM_TRIES_BEFORE_DO_BACKOFF ) + { VMS_int__backoff_for_TooLongToGetLock( numTriesToGetLock ); + } + if( numTriesToGetLock > MASTERLOCK_RETRIES_BEFORE_YIELD ) + { numTriesToGetLock = 0; + pthread_yield(); + } + + //try to get the lock + gotLock = __sync_bool_compare_and_swap( addrOfMasterLock, UNLOCKED, LOCKED ); + } + MEAS__Capture_Post_Master_Lock_Point; + } + +/*Used by the backoff to pick a random amount of busy-wait. Can't use the + * system rand because it takes much too long. + *Note, are passing pointers to the seeds, which are then modified + */ +inline uint32_t +VMS_int__randomNumber() + { + _VMSMasterEnv->seed1 = 36969 * (_VMSMasterEnv->seed1 & 65535) + + (_VMSMasterEnv->seed1 >> 16); + _VMSMasterEnv->seed2 = 18000 * (_VMSMasterEnv->seed2 & 65535) + + (_VMSMasterEnv->seed2 >> 16); + return (_VMSMasterEnv->seed1 << 16) + _VMSMasterEnv->seed2; + } + + +/*Busy-waits for a random number of cycles -- chooses number of cycles + * differently than for the no-work backoff + */ +inline void +VMS_int__backoff_for_TooLongToGetLock( int32 numTriesToGetLock ) + { int32 i, waitIterations; + volatile double fakeWorkVar; //busy-wait fake work + + waitIterations = + VMS_int__randomNumber()% (numTriesToGetLock * GET_LOCK_BACKOFF_WEIGHT); + //addToHist( wait_iterations, coreLoopThdParams->wait_iterations_hist ); + for( i = 0; i < waitIterations; i++ ) + { fakeWorkVar += (fakeWorkVar + 32.0) / 2.0; //busy-wait + } + } + diff -r 5e315ce69d82 -r f1267bc7b342 VMS__startup_and_shutdown.c --- a/VMS__startup_and_shutdown.c Fri May 25 12:23:03 2012 +0200 +++ b/VMS__startup_and_shutdown.c Wed May 30 14:23:47 2012 -0700 @@ -311,6 +311,8 @@ } _VMSMasterEnv->masterVPs = masterVPs; _VMSMasterEnv->masterLock = UNLOCKED; + _VMSMasterEnv->seed1 = rand()%1000; // init random number generator + _VMSMasterEnv->seed2 = rand()%1000; // init random number generator _VMSMasterEnv->allAnimSlots = allAnimSlots; _VMSMasterEnv->measHistsInfo = NULL;