diff src/Application/main.h @ 0:9cf9b2091eeb

working condition variable version
author Sean Halle <seanhalle@yahoo.com>
date Wed, 10 Jul 2013 14:13:46 -0700
parents
children 88db7b62b961
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Application/main.h	Wed Jul 10 14:13:46 2013 -0700
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* 
     1.5 + * 
     1.6 + */
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string.h>
    1.10 +#include <math.h>
    1.11 +#include <ctype.h>
    1.12 +#include <errno.h>
    1.13 +#include <pthread.h>
    1.14 +#include <sched.h>
    1.15 +#include <unistd.h>
    1.16 +
    1.17 +#include <linux/perf_event.h>
    1.18 +#include <sys/syscall.h>
    1.19 +
    1.20 +//==========================
    1.21 +//#define TURN_ON_DEBUG
    1.22 +
    1.23 +//==========================
    1.24 +#define NUM_CORES 4
    1.25 +
    1.26 +//==========================
    1.27 +
    1.28 +//SELECT how the measurement is done
    1.29 +//only one must be enabled
    1.30 +#define MEASURE_TSC
    1.31 +//#define MEASURE_PERF
    1.32 +
    1.33 +
    1.34 +#if !defined(unix) && !defined(__unix__)
    1.35 +#ifdef __MACH__
    1.36 +#define unix		1
    1.37 +#define __unix__	1
    1.38 +#endif	/* __MACH__ */
    1.39 +#endif	/* unix */
    1.40 +
    1.41 +/* find the appropriate way to define explicitly sized types */
    1.42 +/* for C99 or GNU libc (also mach's libc) we can use stdint.h */
    1.43 +#if (__STDC_VERSION__ >= 199900) || defined(__GLIBC__) || defined(__MACH__)
    1.44 +#include <stdint.h>
    1.45 +#elif defined(unix) || defined(__unix__)	/* some UNIX systems have them in sys/types.h */
    1.46 +#include <sys/types.h>
    1.47 +#elif defined(__WIN32__) || defined(WIN32)	/* the nameless one */
    1.48 +typedef unsigned __int8 uint8_t;
    1.49 +typedef unsigned __int32 uint32_t;
    1.50 +#endif	/* sized type detection */
    1.51 +
    1.52 +
    1.53 +//==================
    1.54 +#ifdef TURN_ON_DEBUG
    1.55 + #define DEBUG__printf(msg) printf(msg)
    1.56 + #define DEBUG__printf1(msg, arg1) printf(msg, arg1)
    1.57 + #define DEBUG__printf2(msg, arg1, arg2) printf(msg, arg1, arg2)
    1.58 +#else
    1.59 + #define DEBUG__printf(msg)
    1.60 + #define DEBUG__printf1(msg, arg1)
    1.61 + #define DEBUG__printf2(msg, arg1, arg2)
    1.62 +#endif
    1.63 +//===== RDTSC wrapper ===== //Does work for x86_64 compile
    1.64 +
    1.65 +#define saveTimeStampCountInto(low, high) \
    1.66 +   asm volatile("RDTSC;                   \
    1.67 +                 movl %%eax, %0;          \
    1.68 +                 movl %%edx, %1;"         \
    1.69 +   /* outputs */ : "=m" (low), "=m" (high)\
    1.70 +   /* inputs  */ :                        \
    1.71 +   /* clobber */ : "%eax", "%edx"         \
    1.72 +                );
    1.73 +
    1.74 +#define saveLowTimeStampCountInto(low)    \
    1.75 +   asm volatile("RDTSC;                   \
    1.76 +                 movl %%eax, %0;"         \
    1.77 +   /* outputs */ : "=m" (low)             \
    1.78 +   /* inputs  */ :                        \
    1.79 +   /* clobber */ : "%eax", "%edx"         \
    1.80 +                );
    1.81 +
    1.82 +//====================
    1.83 +
    1.84 +union timeStamp
    1.85 + {
    1.86 +    uint32_t lowHigh[2]; //lowHigh[0] is low, lowHigh[1] is high
    1.87 +    uint64_t total;
    1.88 + };
    1.89 +
    1.90 +struct perfData
    1.91 + {
    1.92 +    uint64_t cycles;
    1.93 +    uint64_t instructions;
    1.94 + };
    1.95 +
    1.96 +//MEASURE_TSC should be mutually exclusive with MEASURE_PERF
    1.97 +#ifdef MEASURE_TSC
    1.98 + typedef union timeStamp MeasStruct;
    1.99 +#else
   1.100 + #ifdef MEASURE_PERF
   1.101 + typedef struct perfData MeasStruct;
   1.102 + #endif
   1.103 +#endif
   1.104 +
   1.105 + //fast way to collect time intervals, by putting into hist right away
   1.106 +#define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
   1.107 +   makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
   1.108 +   _VMSMasterEnv->measHists[idx] =  \
   1.109 +                       makeFixedBinHist( numBins, startVal, binWidth, name );
   1.110 +
   1.111 +//read and save current perf-counter readings for cycles and instrs
   1.112 +#ifdef MEASURE_PERF
   1.113 + #define takeAMeas(core, perfDataStruct) do{     \
   1.114 +   int cycles_fd = cycles_counter_fd[core];             \
   1.115 +   int nread;                                           \
   1.116 +                                                        \
   1.117 +   nread = read(cycles_fd,&(perfDataStruct.cycles),sizeof(perfDataStruct.cycles));    \
   1.118 +   if(nread<0){                                         \
   1.119 +       perror("Error reading cycles counter");          \
   1.120 +       cycles = 0;                                      \
   1.121 +   }                                                    \
   1.122 + } while (0) //macro magic for scoping
   1.123 +#else
   1.124 + #define takeAMeas(core, timeStampStruct) do{     \
   1.125 +   saveTimeStampCountInto(timeStampStruct.lowHigh[0], timeStampStruct.lowHigh[1]);\
   1.126 + } while (0) //macro magic for scoping
   1.127 +#endif
   1.128 +
   1.129 + 
   1.130 +typedef struct
   1.131 + {
   1.132 +   int coreID;
   1.133 +   int numTuplesToCreate;
   1.134 +   int producerID;
   1.135 +   
   1.136 + }
   1.137 +ProducerParams;
   1.138 +
   1.139 +typedef struct
   1.140 + {
   1.141 +   int coreID;
   1.142 +   int numTuplesToCreate;
   1.143 +   int numProducers;
   1.144 +   
   1.145 + }
   1.146 +ConsumerParams;
   1.147 + 
   1.148 +//=========== Global Vars =============
   1.149 +pthread_mutex_t tupleIterLock;
   1.150 +pthread_cond_t  tupleIterCond;
   1.151 +int tupleIter;
   1.152 +
   1.153 +pthread_mutex_t producerAccessMutex;
   1.154 +pthread_mutex_t productionReadyLock;
   1.155 +pthread_cond_t  productionReadyCond;
   1.156 +int currProductionNum;
   1.157 +int producerMessage;
   1.158 +
   1.159 +pthread_mutex_t consumerReceivedAckLock;
   1.160 +pthread_cond_t  consumerReceivedAckCond;
   1.161 +int currConsumerReceivedACKNum;
   1.162 +
   1.163 +//=======
   1.164 +void* 
   1.165 +producer_birthFn( void* _params );
   1.166 +void* 
   1.167 +consumer_birthFn( void* _params );
   1.168 +
   1.169 +