2 * gcc -Wall -Werror -O3 -static -o ctx ctx.c
5 #define errx(x,y...) { fprintf(stderr, y); fprintf(stderr, "\n"); exit(x); }
13 #include <sys/ucontext.h>
17 #define ITERATIONS 8192
21 unsigned long elapsed_times
[ITERATIONS
];
22 unsigned long overhead
;
28 printf("ctx [-hl]\n");
38 if (write(fd1
[1], &ch
, 1) != 1)
39 errx(1, "child write failed");
41 if (read(fd0
[0], &ch
, 1) != 1)
42 errx(1, "child read failed");
43 if (write(fd1
[1], &ch
, 1) != 1)
44 errx(1, "child write failed");
51 unsigned long min_time
, max_time
, sum
;
54 min_time
= elapsed_times
[0];
55 max_time
= elapsed_times
[0];
57 for (i
=1; i
<ITERATIONS
; i
++) {
58 if (elapsed_times
[i
] < min_time
)
59 min_time
= elapsed_times
[i
];
60 if (elapsed_times
[i
] > max_time
)
61 max_time
= elapsed_times
[i
];
62 sum
+= elapsed_times
[i
] - overhead
;
67 printf("min latency: %f\n", (double)min_time
/ PASSES
);
68 printf("max latency: %f\n", (double)max_time
/ PASSES
);
69 printf("mean latency: %f\n", (double)sum
/ ITERATIONS
/ PASSES
);
73 main(int argc
, char *argv
[])
76 struct timeval before
, after
;
77 unsigned long elapsed
;
80 memset(elapsed_times
, 0, ITERATIONS
);
82 while ((ch
= getopt(argc
, argv
, "hl")) != -1) {
88 errx(1, "not supported");
101 errx(1, "Unable to create pipe");
103 errx(1, "Unable to create pipe");
108 for (count
=0; count
<2; count
++) {
109 gettimeofday(&before
, NULL
);
110 for (i
=0; i
<2*(PASSES
/2); i
++) {
112 write(fd0
[1], &ch
, 1);
113 read(fd0
[0], &ch
, 1);
115 gettimeofday(&after
, NULL
);
116 overhead
= 1000000 * (after
.tv_sec
- before
.tv_sec
);
117 overhead
+= after
.tv_usec
- before
.tv_usec
;
123 ucontext_t
*contextp
;
124 int stacksize
= 65536;
130 contextp
= (ucontext_t
*)malloc(sizeof(ucontext_t
));
131 stackbase
= malloc(stacksize
);
132 sigprocmask(SIG_SETMASK
, NULL
, &contextp
->uc_sigmask
);
133 _lwp_makecontext(contextp
, child
, NULL
, NULL
,
134 stackbase
, stacksize
);
135 error
= _lwp_create(contextp
, 0, &l
);
137 errx(1, "error _lwp_create");
140 switch (childpid
= fork()) {
144 errx(1, "error forking");
150 if (read(fd1
[0], &ch
, 1) != 1)
151 errx(1, "parent read failed");
152 for (count
=0; count
<ITERATIONS
; count
++) {
153 gettimeofday(&before
, NULL
);
154 for (i
=0; i
<PASSES
/2; i
++) {
156 if (write(fd0
[1], &ch
, 1) != 1)
157 errx(1, "parent write failed");
158 if (read(fd1
[0], &ch
, 1) != 1)
159 errx(1, "parent read failed");
161 gettimeofday(&after
, NULL
);
162 elapsed
= 1000000 * (after
.tv_sec
- before
.tv_sec
);
163 elapsed
+= after
.tv_usec
- before
.tv_usec
;
164 elapsed_times
[count
] = elapsed
;
168 kill(childpid
, SIGTERM
);
178 * min latency: 93.100000
179 * max latency: 150.700000
180 * mean latency: 100.857581
184 * min latency: 49.350000
185 * max latency: 76.750000
186 * mean latency: 54.141626
190 * min latency: 54.750000
191 * max latency: 76.050000
192 * mean latency: 60.088654
196 * min latency: 352.560000
197 * max latency: 402.960000
198 * mean latency: 367.836250
202 * min latency: 129.200000
203 * max latency: 187.040000
204 * mean latency: 142.528223
208 * min latency: 357.360000
209 * max latency: 414.400000
210 * mean latency: 372.436104