Skip to content
Newer
Older
100644 104 lines (81 sloc) 1.8 KB
844967a @rigtorp Initial commit
rigtorp authored Jan 7, 2010
1 /* Measure latency of IPC using shm */
2
1618c9e @avsm various compile fixes to work on MacOS X. Disable shm test for now.
authored Oct 30, 2011
3 #include <unistd.h>
844967a @rigtorp Initial commit
rigtorp authored Jan 7, 2010
4 #include <stdio.h>
5 #include <stdlib.h>
1618c9e @avsm various compile fixes to work on MacOS X. Disable shm test for now.
authored Oct 30, 2011
6 #include <sys/time.h>
844967a @rigtorp Initial commit
rigtorp authored Jan 7, 2010
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <sys/ipc.h>
10 #include <sys/shm.h>
11
12 int main(void)
13 {
14 int pfds[2];
15
16 char c;
17 int shmid;
18 key_t key;
19 struct timespec *shm;
20
21
22
23 struct timespec start, stop;
24
25 int64_t delta;
26
27 int64_t max = 0;
28 int64_t min = INT64_MAX;
29 int64_t sum = 0;
30 int64_t count = 0;
31
32 /*
33 * We'll name our shared memory segment
34 * "5678".
35 */
36 key = 5678;
37
38 if (!fork()) {
39
40 /*
41 * Create the segment.
42 */
43 if ((shmid = shmget(key, 100, IPC_CREAT | 0666)) < 0) {
44 perror("shmget");
45 exit(1);
46 }
47
48 /*
49 * Now we attach the segment to our data space.
50 */
51 if ((shm = shmat(shmid, NULL, 0)) == (struct timespec*) -1) {
52 perror("shmat");
53 exit(1);
54 }
55
56 while (1) {
57 clock_gettime(CLOCK_MONOTONIC, shm);
58
59 usleep(10000);
60 }
61 } else {
62 sleep(1);
63
64 /*
65 * Locate the segment.
66 */
67 if ((shmid = shmget(key, 100, 0666)) < 0) {
68 perror("shmget");
69 exit(1);
70 }
71
72 /*
73 * Now we attach the segment to our data space.
74 */
75 if ((shm = shmat(shmid, NULL, 0)) == (struct timespec *) -1) {
76 perror("shmat");
77 exit(1);
78 }
79
80 while (1) {
81 while ((shm->tv_sec == start.tv_sec) && (shm->tv_nsec == start.tv_nsec)) {}
82
83 clock_gettime(CLOCK_MONOTONIC, &stop);
84 start = *shm;
85 delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1000000000 +
86 stop.tv_nsec - start.tv_nsec);
87
88 if (delta > max)
89 max = delta;
90 else if (delta < min)
91 min = delta;
92
93 sum += delta;
94 count++;
95
96 if (!(count % 100)) {
97 printf("%lli %lli %lli\n", max, min, sum / count);
98 }
99 }
100 }
101
102 return 0;
103 }
Something went wrong with that request. Please try again.