xref: /freebsd/tools/regression/p1003_1b/sched.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1996-1999
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  */
35 
36 /* XXX: The spec says that if _POSIX_C_SOURCE is defined then
37  *      _POSIX_SOURCE is ignored.  However, this is similar to
38  *      the code in the O'Reilly "POSIX.4" book
39  */
40 
41 #define _POSIX_VERSION 199309L
42 #define _POSIX_SOURCE
43 #define _POSIX_C_SOURCE 199309L
44 
45 #include <sys/mman.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <sched.h>
50 #include <stdio.h>
51 #define	__XSI_VISIBLE 1
52 #include <stdlib.h>
53 #undef __XSI_VISIBLE
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "prutil.h"
58 
59 static FILE *verbose;
60 
61 static void
62 checkpris(int sched)
63 {
64 	int smin;
65 	int smax;
66 
67 	errno = 0;
68 
69 	if ( (smin = sched_get_priority_min(sched)) == -1 && errno)
70 		quit("sched_get_priority_min");
71 
72 	if ( (smax = sched_get_priority_max(sched)) == -1 && errno)
73 		quit("sched_get_priority_max");
74 
75 	if (smax - smin + 1 < 32 || smax < smin) {
76 		fprintf(stderr, "Illegal priority range for %s: %d to %d\n",
77 		sched_text(sched), smin, smax);
78 		exit(-1);
79 	}
80 
81 	if (verbose)
82 		fprintf(verbose, "%12s: sched_min %2d sched_max %2d\n",
83 		sched_text(sched), smin, smax);
84 }
85 
86 /* Set "try_anyway" to quit if you don't want to go on when
87  * it doesn't look like something should work.
88  */
89 static void try_anyway(const char *s)
90 {
91 	fputs(s, stderr);
92 	fprintf(stderr, "(trying anyway)\n");
93 	errno = 0;
94 }
95 
96 static void q(int line, int code, const char *text)
97 {
98 	if (code == -1)
99 	{
100 		fprintf(stderr, "Error at line %d:\n", line);
101 		perror(text);
102 		exit(errno);
103 	}
104 }
105 
106 int sched(int ac, char *av[])
107 {
108 	int fifo_schedmin, fifo_schedmax;
109 	int i;
110 	struct sched_param rt_param;
111 	int n_instances = 10;
112 	int sched;
113 
114 	verbose = 0;
115 
116 #if _POSIX_VERSION < 199309
117 	try_anyway("The _POSIX_VERSION predates P1003.1B\n");
118 #endif
119 
120 #if !defined(_POSIX_PRIORITY_SCHEDULING)
121 	try_anyway(
122 	"The environment does not claim to support Posix scheduling.\n");
123 #endif
124 
125 	/* Is priority scheduling configured?
126 	 */
127 	errno = 0;
128 	if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) {
129 		if (errno != 0) {
130 			/* This isn't valid - may be a standard violation
131 			 */
132 			quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)");
133 		}
134 		else {
135 			try_anyway(
136 			"The environment does not have run-time "
137 			"support for Posix scheduling.\n");
138 		}
139 	}
140 
141 	/* Check that the priorities seem reasonable.
142 	 */
143 
144 	checkpris(SCHED_FIFO);
145 	checkpris(SCHED_RR);
146 	checkpris(SCHED_OTHER);
147 
148 /* BSD extensions?
149  */
150 #if defined(SCHED_IDLE)
151 	checkpris(SCHED_IDLE);
152 #endif
153 
154 	fifo_schedmin = sched_get_priority_min(SCHED_FIFO);
155 	fifo_schedmax = sched_get_priority_max(SCHED_FIFO);
156 
157 	/* Make sure we can do some basic schedule switching:
158 	 */
159 	{
160 		struct sched_param orig_param, shouldbe;
161 		int orig_scheduler = sched_is(__LINE__, &orig_param, -1);
162 
163 		if (verbose)
164 			fprintf(verbose,
165 			"The original scheduler is %s and the priority is %d.\n",
166 			sched_text(orig_scheduler), orig_param.sched_priority);
167 
168 		/* Basic check: Try to set current settings:
169 		 */
170 		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
171 			"sched_setscheduler: Can't set original scheduler");
172 
173 		rt_param.sched_priority = fifo_schedmin;
174 
175 		q(__LINE__, sched_setscheduler(0, SCHED_FIFO, &rt_param),
176 		"sched_setscheduler SCHED_FIFO");
177 
178 		(void)sched_is(__LINE__, 0, SCHED_FIFO);
179 
180 		q(__LINE__, sched_getparam(0, &shouldbe), "sched_getparam");
181 
182 		if (shouldbe.sched_priority != fifo_schedmin)
183 			quit("sched_setscheduler wrong priority (min)");
184 
185 		rt_param.sched_priority = fifo_schedmin;
186 
187 		q(__LINE__, sched_setparam(0, &rt_param),
188 			"sched_setparam to fifo_schedmin");
189 
190 		rt_param.sched_priority = fifo_schedmin + 1;
191 
192 		q(__LINE__, sched_setparam(0, &rt_param),
193 			"sched_setparam to fifo_schedmin + 1");
194 
195 		q(__LINE__, sched_getparam(0, &shouldbe),
196 			"sched_getparam");
197 
198 		if (shouldbe.sched_priority != fifo_schedmin + 1)
199 			quit("sched_setscheduler wrong priority (min + 1)");
200 
201 		q(__LINE__, sched_setscheduler(0, SCHED_RR, &rt_param),
202 			"sched_setscheduler SCHED_RR");
203 
204 		(void)sched_is(__LINE__, 0, SCHED_RR);
205 
206 		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
207 			"sched_setscheduler restoring original scheduler");
208 
209 		(void)sched_is(__LINE__, 0, orig_scheduler);
210 	}
211 
212 
213 	{
214 		char nam[] = "P1003_1b_schedXXXXXX";
215 		int fd;
216 		pid_t p;
217 		pid_t *lastrun;
218 
219 		fd = mkstemp(nam);
220 		if (fd == -1)
221 			q(__LINE__, errno, "mkstemp failed");
222 
223 		(void)unlink(nam);
224 
225 		p = (pid_t)0;
226 
227 		write(fd, &p, sizeof(p));
228 
229 		q(__LINE__,  (int)(lastrun = mmap(0, sizeof(*lastrun), PROT_READ|PROT_WRITE,
230 		MAP_SHARED, fd, 0)), "mmap");
231 
232 		/* Set our priority at the highest:
233 		 */
234 		sched = SCHED_FIFO;
235 		rt_param.sched_priority = fifo_schedmax;
236 		q(__LINE__, sched_setscheduler(0, sched, &rt_param),
237 		"sched_setscheduler sched");
238 
239 		for (i = 0; i < n_instances; i++)
240 		{
241 			pid_t me;
242 
243 			/* XXX This is completely bogus.  The children never run.
244 			 */
245 			if ((me = fork()) != 0)
246 			{
247 				/* Parent.
248 				 */
249 				(void)sched_is(__LINE__, 0, sched);
250 
251 				/* Lower our priority:
252 				 */
253 				rt_param.sched_priority--;
254 
255 				q(__LINE__, sched_setscheduler(0, sched, &rt_param),
256 				"sched_setscheduler sched");
257 
258 				while (1)
259 				{
260 					q(__LINE__, sched_getparam(0, &rt_param), "sched_getparam");
261 
262 					rt_param.sched_priority--;
263 
264 
265 					if (rt_param.sched_priority < fifo_schedmin)
266 						exit(0);
267 
268 					*lastrun = me;
269 					q(__LINE__, sched_setparam(0, &rt_param), "sched_setparam");
270 
271 					if (*lastrun == me)
272 					{
273 						/* The child will run twice
274 						 * at  the end:
275 						 */
276 						if (!me || rt_param.sched_priority != 0)
277 						{
278 							fprintf(stderr,
279 							"ran process %ld twice at priority %d\n",
280 							(long)me, rt_param.sched_priority + 1);
281 							exit(-1);
282 						}
283 					}
284 				}
285 			}
286 		}
287 	}
288 
289 	return 0;
290 }
291 #ifdef STANDALONE_TESTS
292 int main(int argc, char *argv[]) { return sched(argc, argv); }
293 #endif
294