Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / Runtime / DSTM / interface / clocksyncserver.c
1 /** Server on dc-1 **/
2
3 //One clock tick =  (1 / CPU processor speed in Hz) secs
4 //compile:
5 //    gcc -Wall -o server clocksyncserver.c
6 //
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 #define PORT            8500
19 #define NUMITER     10000
20 #define DIRSIZE         1
21
22 static __inline__ unsigned long long rdtsc(void) {
23   unsigned hi, lo;
24   __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
25   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
26 }
27
28 int main() {
29   unsigned long long dir[DIRSIZE];  /* used for incomming dir name, and
30                                        outgoing data */
31   int sd, sd_current;
32   socklen_t addrlen;
33   struct   sockaddr_in sin;
34   struct   sockaddr_in pin;
35
36   FILE *f1;
37   f1=fopen("dc-1", "w");
38
39   unsigned long long array1[NUMITER];
40   unsigned long long array2[NUMITER];
41   /* get an internet domain socket */
42   if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
43     perror("socket");
44     exit(1);
45   }
46
47   /* complete the socket structure */
48   memset(&sin, 0, sizeof(sin));
49   sin.sin_family = AF_INET;
50   sin.sin_addr.s_addr = INADDR_ANY;
51   sin.sin_port = htons(PORT);
52
53   /* bind the socket to the port number */
54   if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
55     perror("bind");
56     exit(1);
57   }
58
59   /* show that we are willing to listen */
60   if (listen(sd, 5) == -1) {
61     perror("listen");
62     exit(1);
63   }
64   /* wait for a client to talk to us */
65   addrlen = sizeof(pin);
66   if ((sd_current = accept(sd, (struct sockaddr *)&pin, &addrlen)) == -1) {
67     perror("accept");
68     exit(1);
69   }
70   /* if you want to see the ip address and port of the client, uncomment the
71      next two lines */
72
73   /*
74      printf("Hi there, from  %s#\n",inet_ntoa(pin.sin_addr));
75      printf("Coming from port %d\n",ntohs(pin.sin_port));
76    */
77
78   int i;
79   char data[1];
80   data[0]='1';
81   long long norm = 0;
82   send(sd_current, data, sizeof(data), MSG_NOSIGNAL);
83   for(i=0; i<NUMITER; i++) {
84     /* get a message from the client */
85     if (recv(sd_current, dir, sizeof(unsigned long long), 0) == -1) {
86       perror("recv");
87       exit(1);
88     }
89     //printf("DEBUG: dir[0]= %lld\n", dir[0]);
90     array2[i] = rdtsc();
91     //printf("DEBUG: array2[i]= %lld\n", array2[i]);
92     //array1[i]=array2[i] - dir[0];
93     array1[i]= dir[0] - array2[i];
94     printf("%lld\n", array1[i]);
95
96     /* acknowledge the message, reply w/ the file names */
97     if (send(sd_current, &array2[i], sizeof(unsigned long long), MSG_NOSIGNAL) == -1) {
98       perror("send");
99       exit(1);
100     }
101     //array2[i]=rdtsc();
102   }
103
104   for(i=0; i<(NUMITER-1); i++) {
105     norm += array1[i];
106   }
107
108   /* spew-out the results */
109   //printf("DEBUG: Average offset= %lld\n", (norm/(NUMITER-1)));
110   long long average=(norm/(NUMITER-1));
111   printf("average= %lld",(norm/(NUMITER-1)));
112
113   long long stddev, avg1=0;
114   for(i=0; i<(NUMITER-1); i++) {
115     avg1 += ((array1[i] - average) * (array1[i] - average));
116   }
117   float ans = (avg1/(NUMITER-1));
118   float squareroot= sqrt(ans);
119   float squareroot2= sqrt(avg1);
120
121   printf("stddev= %f\n", squareroot);
122   printf("error= %f\n", squareroot2/(NUMITER-1));
123   fprintf(f1,"%lld\n",(norm/(NUMITER-1)));
124
125
126   /* give client a chance to properly shutdown */
127   sleep(1);
128
129   /* close up both sockets */
130   close(sd_current);
131   close(sd);
132
133
134   return 0;
135 }
136
137