changeset 183:c66137e4dc2f false_sharing

random wait time between tries to aquire master lock, because of performance penalties due to lock contention
author Merten Sach <msach@mailbox.tu-berlin.de>
date Fri, 13 Jan 2012 11:49:05 +0100
parents cf5faa15cb4d
children f89bf9e368f5 91d0d2e06719
files CoreLoop.c
diffstat 1 files changed, 30 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/CoreLoop.c	Tue Dec 20 19:42:42 2011 +0100
     1.2 +++ b/CoreLoop.c	Fri Jan 13 11:49:05 2012 +0100
     1.3 @@ -18,6 +18,9 @@
     1.4  
     1.5  void *terminateCoreLoop(VirtProcr *currVP);
     1.6  
     1.7 +inline uint32_t
     1.8 +randomNumber(uint32_t* seed1, uint32_t* seed2);
     1.9 +
    1.10  /*This is the loop that runs in the OS Thread pinned to each core
    1.11   *Get virt procr from queue,
    1.12   * save state of current animator, then load in state of virt procr, using
    1.13 @@ -45,6 +48,10 @@
    1.14     volatile int32 *masterLock;
    1.15     VirtProcr      *masterVP;
    1.16  
    1.17 +   // init random number generator for retries
    1.18 +   uint32_t seed1 = rand()%1000;
    1.19 +   uint32_t seed2 = rand()%1000;
    1.20 +
    1.21        //work-stealing struc on stack to prevent false-sharing in cache-line
    1.22     volatile GateStruc gate;
    1.23     //preGateProgress, waitProgress, exitProgress, gateClosed;
    1.24 @@ -67,6 +74,8 @@
    1.25                           &suspendLock );
    1.26      }
    1.27     pthread_mutex_unlock( &suspendLock );
    1.28 +   
    1.29 +  //coreLoopThdParams->wait_iterations_hist = makeHistogram( 100, 0, 10000 );
    1.30  
    1.31        //printf( "\nCore unsuspended: %d\n", coreLoopThdParams->coreNum );
    1.32  
    1.33 @@ -128,9 +137,15 @@
    1.34        #endif
    1.35        //=====================================================================
    1.36        int tries = 0; int gotLock = 0;
    1.37 +      unsigned int wait_iterations=0;
    1.38        while( currVP == NULL ) //if queue was empty, enter get masterLock loop
    1.39         {    //queue was empty, so get master lock
    1.40  
    1.41 +		 if(numMasterInARow > 2){
    1.42 +		 	wait_iterations=(randomNumber(&seed1, &seed2) % (numMasterInARow*numMasterInARow))*100;
    1.43 +	   		int i;
    1.44 +	  	    for(i=0; i<wait_iterations; i++)/*busy wait*/;
    1.45 +		 }
    1.46           gotLock = __sync_bool_compare_and_swap(masterLock,
    1.47                                                  UNLOCKED, LOCKED );
    1.48           if( gotLock )
    1.49 @@ -142,11 +157,17 @@
    1.50               }
    1.51              numMasterInARow += 1;
    1.52              break;  //end while -- have a VP to animate now
    1.53 -          }
    1.54 +          }else{
    1.55 +	   wait_iterations=(randomNumber(&seed1, &seed2) % (tries+1))*100;
    1.56 +	   //addToHist( wait_iterations, coreLoopThdParams->wait_iterations_hist );
    1.57 +	   int i;
    1.58 +	   for(i=0; i<wait_iterations; i++)/*busy wait*/;
    1.59 +	}
    1.60  
    1.61           tries++;      //if too many, means master on other core taking too long
    1.62           if( tries > MASTERLOCK_RETRIES ) { tries = 0; pthread_yield(); }
    1.63         }
    1.64 +
    1.65        //============================= MEASUREMENT STUFF =====================
    1.66        #ifdef MEAS__TIME_MASTER_LOCK
    1.67        saveLowTimeStampCountInto( endStamp );
    1.68 @@ -181,11 +202,19 @@
    1.69     }//CoreLoop      
    1.70   }
    1.71  
    1.72 +inline uint32_t
    1.73 +randomNumber(uint32_t* seed1, uint32_t* seed2){
    1.74 +	*seed1 = 36969 * (*seed1 & 65535) + (*seed1 >> 16);
    1.75 +	*seed2 = 18000 * (*seed2 & 65535) + (*seed2 >> 16);
    1.76 +	return (*seed1 << 16) + *seed2;
    1.77 +}
    1.78 +
    1.79  
    1.80  void *
    1.81  terminateCoreLoop(VirtProcr *currVP){
    1.82     //first free shutdown VP that jumped here -- it first restores the
    1.83     // coreloop's stack, so addr of currVP in stack frame is still correct
    1.84 +   //printHist(coreLoopThdParams[currVP->coreAnimatedBy]->wait_iterations_hist);
    1.85     VMS__dissipate_procr( currVP );
    1.86     pthread_exit( NULL );
    1.87  }