2 #include <netinet/ip.h>
4 #include <sys/socket.h>
10 /* Subtract the `struct timespec' values X and Y,
11 storing the result in RESULT.
12 Return 1 if the difference is negative, otherwise 0. */
15 timespec_subtract (result, x, y)
16 struct timespec *result, *x, *y;
18 /* Perform the carry for the later subtraction by updating Y. */
19 if (x->tv_nsec < y->tv_nsec) {
20 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
21 y->tv_nsec -= 1000000000 * nsec;
24 if (x->tv_nsec - y->tv_nsec > 1000000000) {
25 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
26 y->tv_nsec += 1000000000 * nsec;
30 /* Compute the time remaining to wait.
31 `tv_nsec' is certainly positive. */
32 result->tv_sec = x->tv_sec - y->tv_sec;
33 result->tv_nsec = x->tv_nsec - y->tv_nsec;
35 /* Return 1 if result is negative. */
36 return x->tv_sec < y->tv_sec;
44 socklen_t alen = sizeof(a);
47 ret = recvfrom(srx, msgr, sizeof(msgr), 0, &a, &alen);
48 if (ret == -1) perror("slave recvfrom");
49 ret = sendto(srx, msgr, ret, 0, &a, alen);
50 if (ret == -1) perror("slave sendto");
55 #define HIST_SIZE 1000
56 #define HIST_MAX_US 100000
59 int usec2hist(int usec)
62 i = HIST_SIZE*usec/HIST_MAX_US;
72 struct timespec tstart, tend, t;
80 stx = socket(PF_INET, SOCK_DGRAM, 0);
81 a.sin_family = AF_INET;
82 a.sin_port = htons(1234);
83 a.sin_addr.s_addr = inet_addr("127.1.2.3");
84 srx = socket(PF_INET, SOCK_DGRAM, 0);
85 bind(srx, (struct sockaddr*)&a, sizeof(a));
96 for (i=0; i<10000; i++) {
97 clock_gettime(CLOCK_MONOTONIC, &tstart);
98 ret = sendto(stx, msg, sizeof(msg), 0, (struct sockaddr*)&a, sizeof(a));
99 if (ret == -1) perror("main sendto");
100 ret = recv(stx, msgr, sizeof(msgr), 0);
101 if (ret == -1) perror("main recv");
102 clock_gettime(CLOCK_MONOTONIC, &tend);
103 timespec_subtract(&t, &tend, &tstart);
105 usec = t.tv_sec*1000000 + t.tv_nsec/1000;
109 //printf("%d.%.03d\n", usec/1000, usec%1000);
116 printf("Duration: %d.%.03d ms\n", max/1000, max%1000);
124 perror("fork gnuplot");
130 /* int n = open("/dev/null", O_RDWR); */
133 execlp("gnuplot", "gnuplot", "-persist", NULL);
136 FILE *f = fdopen(fd[1], "a");
137 fprintf(f, "set xlabel 'ms'\n");
138 fprintf(f, "set logscale y\n");
139 fprintf(f, "plot '-' with steps\n");
141 for (j=0; j<HIST_SIZE; j++) {
143 fprintf(f, "%g %d\n", j/10.0, i);
147 fprintf(f, "e\n", i, hist[i]);