1 /* $NetBSD: select.c,v 1.2 2008/03/21 16:03:33 ad Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * THIS IS NOT A GOOD TEST OF REALTIME DISPATCH LATENCY AND SHOULD NOT
33 * Two threads are involved in the test. Every 100ms the first thread
34 * writes a byte down a pipe. The second thread waits to read it. The
35 * time taken for the byte to move between threads is measured. With
36 * no arguments, it uses timeshared threads. When given any argument
37 * (junk will do) it uses realtime threads. Ctrl-C ends the test.
41 #include <sys/resource.h>
52 pthread_barrier_t barrier
;
53 struct timespec start
;
74 (void)pthread_barrier_wait(&barrier
);
75 if (read(fds
[0], buf
, sizeof(buf
)) == -1) {
78 clock_gettime(CLOCK_MONOTONIC
, &end
);
79 timespecsub(&end
, &start
, &end
);
80 val
= (long)end
.tv_sec
* 1000000000 + (long)end
.tv_nsec
;
90 main(int argc
, char *argv
[])
92 static struct timespec delay
= { 0, 100000000 }; /* 100ms */
93 struct sched_param sp
;
98 errx(1, "run me as root");
103 pthread_barrier_init(&barrier
, NULL
, 2);
104 if (pthread_create(&pt
, NULL
, reader
, NULL
)) {
105 errx(1, "pthread_create failed");
107 if (mlockall(MCL_CURRENT
| MCL_FUTURE
)) {
110 if (signal(SIGINT
, sigint
)) {
114 /* Have an argument, use realtime priorities. */
115 sp
.sched_priority
= sched_get_priority_max(SCHED_FIFO
) - 2;
116 if (pthread_setschedparam(pthread_self(), SCHED_FIFO
, &sp
)) {
117 errx(1, "pthread_setschedparam");
119 sp
.sched_priority
= sched_get_priority_max(SCHED_FIFO
) - 1;
120 if (pthread_setschedparam(pt
, SCHED_FIFO
, &sp
)) {
121 errx(1, "pthread_setschedparam");
124 /* Not realtime, set the maximum timeshared priority. */
125 if (setpriority(PRIO_PROCESS
, 0, -20)) {
126 err(1, "setpriority");
131 (void)pthread_barrier_wait(&barrier
);
132 while (nanosleep(&delay
, NULL
) != 0) {
135 clock_gettime(CLOCK_MONOTONIC
, &start
);
136 if (write(fds
[1], buf
, sizeof(buf
)) == -1) {
141 printf("\nmin %ldns, max=%ldns\n", min
, max
);