files used for all paper numbers
[IRC.git] / Robust / src / Benchmarks / SingleTM / MicroBenchmarks / checkClockSync.c
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <pthread.h>
5 #include <sys/time.h>
6
7 #define THREADS 8
8 #define ITERATIONS 100000
9
10 pthread_barrier_t barr;
11
12 static __inline__ unsigned long long rdtsc(void)
13 {
14   unsigned hi, lo;
15   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
16   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
17 }
18
19 static __inline__ int64_t timeInMS () //time in microsec
20 {
21   struct timeval t;
22
23   gettimeofday(&t, NULL);
24   return (
25       (int64_t)t.tv_sec * 1000000 +
26       (int64_t)t.tv_usec
27       );
28 }
29
30 void * thd (
31     void * unused
32     ) {
33   int count = 0;
34   unsigned long long tottime, time;
35   while(count<ITERATIONS) {
36     // Wait till we may fire away
37     int rc=pthread_barrier_wait(&barr);
38     if(rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
39     {
40       printf("Could not wait on barrier\n");
41       exit(-1);
42     }
43     //printf("t= %lld\n", rdtsc());
44     //time = timeInMS();
45     time = rdtsc();
46     tottime += time;
47     count++;
48   }
49   printf("time= %lld micro secs\n", tottime/ITERATIONS);
50 }
51
52 int main (
53     int argc,
54     char ** argv
55     ) {
56   int64_t start;
57   pthread_t t1, t2, t3, t4, t5, t6, t7, t8;
58   int64_t myTime;
59
60   // Barrier initialization
61   if(pthread_barrier_init(&barr, NULL, THREADS))
62   {
63     printf("Could not create a barrier\n");
64     return -1;
65   }
66
67   pthread_create(&t1, NULL, thd, NULL);
68   pthread_create(&t2, NULL, thd, NULL);
69   pthread_create(&t3, NULL, thd, NULL);
70   pthread_create(&t4, NULL, thd, NULL);
71   pthread_create(&t5, NULL, thd, NULL);
72   pthread_create(&t6, NULL, thd, NULL);
73   pthread_create(&t7, NULL, thd, NULL);
74   pthread_create(&t8, NULL, thd, NULL);
75
76   pthread_join(t1, NULL);
77   pthread_join(t2, NULL);
78   pthread_join(t3, NULL);
79   pthread_join(t4, NULL);
80   pthread_join(t5, NULL);
81   pthread_join(t6, NULL);
82   pthread_join(t7, NULL);
83   pthread_join(t8, NULL);
84
85   return 0;
86 }