xref: /freebsd/tools/regression/p1003_1b/fifo.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1996 - 2000
5  *	HD Associates, Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by HD Associates, Inc
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36 #include <sys/types.h>
37 #include <sys/mman.h>
38 #include <sys/time.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sched.h>
43 #include <signal.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 
48 volatile int ticked;
49 #define CAN_USE_ALARMS
50 
51 #ifdef CAN_USE_ALARMS
52 void tick(int arg)
53 {
54 	ticked = 1;
55 }
56 #endif
57 
58 /* Fifo: Verify that fifo and round-robin scheduling seem to work.
59  *
60  * This tests:
61  * 1. That sched_rr_get_interval seems to work;
62  * 2. That FIFO scheduling doesn't seeem to be round-robin;
63  * 3. That round-robin scheduling seems to work.
64  *
65  */
66 static pid_t child;
67 static void tidyup(void)
68 {
69 	if (child)
70 		kill(child, SIGHUP);
71 }
72 
73 static double
74 tvsub(const struct timeval *a, const struct timeval *b)
75 {
76 	long sdiff;
77 	long udiff;
78 
79 	sdiff = a->tv_sec - b->tv_sec;
80 	udiff = a->tv_usec - b->tv_usec;
81 
82 	return (double)(sdiff * 1000000 + udiff) / 1e6;
83 }
84 
85 int fifo(int argc, char *argv[])
86 {
87 	int e = 0;
88 	volatile long *p, pid;
89 	int i;
90 	struct sched_param fifo_param;
91 	struct timespec interval;
92 #define MAX_RANAT 32
93 	struct timeval ranat[MAX_RANAT];
94 
95 #ifdef CAN_USE_ALARMS
96 	static struct itimerval itimerval;
97 #endif
98 
99 	/* What is the round robin interval?
100 	 */
101 
102 	if (sched_rr_get_interval(0, &interval) == -1) {
103 		perror("sched_rr_get_interval");
104 		exit(errno);
105 	}
106 
107 #ifdef CAN_USE_ALARMS
108 	signal(SIGALRM, tick);
109 #endif
110 
111 	fifo_param.sched_priority = 1;
112 
113 	p = (long *)mmap(0, sizeof(*p),
114 	PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
115 
116 	if (p == (long *)-1)
117 		err(errno, "mmap");
118 
119 	*p = 0;
120 
121 	if (sched_setscheduler(0, SCHED_FIFO, &fifo_param) == -1)
122 	{
123 		perror("sched_setscheduler");
124 		return -1;
125 	}
126 
127 	pid = getpid();
128 
129 	if ((child = fork()) == 0)
130 	{
131 		/* Child process.  Just keep setting the pointer to our
132 		 * PID.  The parent will kill us when it wants to.
133 		 */
134 
135 		pid = getpid();
136 		while (1)
137 			*p = pid;
138 	}
139 	else
140 	{
141 		atexit(tidyup);
142 		*p = pid;
143 
144 
145 		ticked = 0;
146 
147 #ifdef CAN_USE_ALARMS
148 		/* Set an alarm for 250 times the round-robin interval.
149 		 * Then we will verify that a similar priority process
150 		 * will not run when we are using the FIFO scheduler.
151 		 */
152 		itimerval.it_value.tv_usec = interval.tv_nsec / (1000 / 250);
153 
154 		itimerval.it_value.tv_sec = itimerval.it_value.tv_usec / 1000000;
155 		itimerval.it_value.tv_usec %= 1000000;
156 
157 
158 		if (setitimer(ITIMER_REAL, &itimerval, 0) == -1) {
159 			perror("setitimer");
160 			exit(errno);
161 		}
162 #endif
163 
164 
165 		gettimeofday(ranat, 0);
166 		i = 1;
167 		while (!ticked && i < MAX_RANAT)
168 			if (*p == child) {
169 				gettimeofday(ranat + i, 0);
170 				*p = 0;
171 				e = -1;
172 				i++;
173 			}
174 
175 		if (e) {
176 			int j;
177 
178 			fprintf(stderr,
179 			"SCHED_FIFO had erroneous context switches:\n");
180 			for (j = 1; j < i; j++) {
181 				fprintf(stderr, "%d %g\n", j,
182 					tvsub(ranat + j, ranat + j - 1));
183 			}
184 			return e;
185 		}
186 
187 		/* Switch to the round robin scheduler and the child
188 		 * should run within twice the interval.
189 		 */
190 		if (sched_setscheduler(child, SCHED_RR, &fifo_param) == -1 ||
191 		sched_setscheduler(0, SCHED_RR, &fifo_param) == -1)
192 		{
193 			perror("sched_setscheduler");
194 			return -1;
195 		}
196 
197 		e = -1;
198 
199 		ticked = 0;
200 
201 #ifdef CAN_USE_ALARMS
202 
203 		/* Now we do want to see it run.  But only set
204 		 * the alarm for twice the interval:
205 		 */
206 		itimerval.it_value.tv_usec = interval.tv_nsec / 500;
207 
208 		if (setitimer(ITIMER_REAL, &itimerval, 0) == -1) {
209 			perror("setitimer");
210 			exit(errno);
211 		}
212 #endif
213 
214 		for (i = 0; !ticked; i++)
215 			if (*p == child) {
216 				e = 0;
217 				break;
218 			}
219 
220 		if (e)
221 			fprintf(stderr,"Child never ran when it should have.\n");
222 	}
223 
224 	exit(e);
225 }
226 
227 #ifdef STANDALONE_TESTS
228 int main(int argc, char *argv[]) { return fifo(argc, argv); }
229 #endif
230