xref: /freebsd/tools/regression/p1003_1b/sched.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*
2  * Copyright (c) 1996-1999
3  *	HD Associates, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by HD Associates, Inc
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  * $FreeBSD$
32  *
33  */
34 
35 /* XXX: The spec says that if _POSIX_C_SOURCE is defined then
36  *      _POSIX_SOURCE is ignored.  However, this is similar to
37  *      the code in the O'Reilly "POSIX.4" book
38  */
39 
40 #define _POSIX_VERSION 199309L
41 #define _POSIX_SOURCE
42 #define _POSIX_C_SOURCE 199309L
43 
44 #include <sys/mman.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <limits.h>
48 #include <sched.h>
49 #include <stdio.h>
50 #define	__XSI_VISIBLE 1
51 #include <stdlib.h>
52 #undef __XSI_VISIBLE
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "prutil.h"
57 
58 static FILE *verbose;
59 
60 static void
61 checkpris(int sched)
62 {
63 	int smin;
64 	int smax;
65 
66 	errno = 0;
67 
68 	if ( (smin = sched_get_priority_min(sched)) == -1 && errno)
69 		quit("sched_get_priority_min");
70 
71 	if ( (smax = sched_get_priority_max(sched)) == -1 && errno)
72 		quit("sched_get_priority_max");
73 
74 	if (smax - smin + 1 < 32 || smax < smin) {
75 		fprintf(stderr, "Illegal priority range for %s: %d to %d\n",
76 		sched_text(sched), smin, smax);
77 		exit(-1);
78 	}
79 
80 	if (verbose)
81 		fprintf(verbose, "%12s: sched_min %2d sched_max %2d\n",
82 		sched_text(sched), smin, smax);
83 }
84 
85 /* Set "try_anyway" to quit if you don't want to go on when
86  * it doesn't look like something should work.
87  */
88 static void try_anyway(const char *s)
89 {
90 	fputs(s, stderr);
91 	fprintf(stderr, "(trying anyway)\n");
92 	errno = 0;
93 }
94 
95 static void q(int line, int code, const char *text)
96 {
97 	if (code == -1)
98 	{
99 		fprintf(stderr, "Error at line %d:\n", line);
100 		perror(text);
101 		exit(errno);
102 	}
103 }
104 
105 int sched(int ac, char *av[])
106 {
107 	int fifo_schedmin, fifo_schedmax;
108 	int i;
109 	struct sched_param rt_param;
110 	int n_instances = 10;
111 	int sched;
112 
113 	verbose = 0;
114 
115 #if _POSIX_VERSION < 199309
116 	try_anyway("The _POSIX_VERSION predates P1003.1B\n");
117 #endif
118 
119 #if !defined(_POSIX_PRIORITY_SCHEDULING)
120 	try_anyway(
121 	"The environment does not claim to support Posix scheduling.\n");
122 #endif
123 
124 	/* Is priority scheduling configured?
125 	 */
126 	errno = 0;
127 	if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) {
128 		if (errno != 0) {
129 			/* This isn't valid - may be a standard violation
130 			 */
131 			quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)");
132 		}
133 		else {
134 			try_anyway(
135 			"The environment does not have run-time "
136 			"support for Posix scheduling.\n");
137 		}
138 	}
139 
140 	/* Check that the priorities seem reasonable.
141 	 */
142 
143 	checkpris(SCHED_FIFO);
144 	checkpris(SCHED_RR);
145 	checkpris(SCHED_OTHER);
146 
147 /* BSD extensions?
148  */
149 #if defined(SCHED_IDLE)
150 	checkpris(SCHED_IDLE);
151 #endif
152 
153 	fifo_schedmin = sched_get_priority_min(SCHED_FIFO);
154 	fifo_schedmax = sched_get_priority_max(SCHED_FIFO);
155 
156 	/* Make sure we can do some basic schedule switching:
157 	 */
158 	{
159 		struct sched_param orig_param, shouldbe;
160 		int orig_scheduler = sched_is(__LINE__, &orig_param, -1);
161 
162 		if (verbose)
163 			fprintf(verbose,
164 			"The original scheduler is %s and the priority is %d.\n",
165 			sched_text(orig_scheduler), orig_param.sched_priority);
166 
167 		/* Basic check: Try to set current settings:
168 		 */
169 		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
170 			"sched_setscheduler: Can't set original scheduler");
171 
172 		rt_param.sched_priority = fifo_schedmin;
173 
174 		q(__LINE__, sched_setscheduler(0, SCHED_FIFO, &rt_param),
175 		"sched_setscheduler SCHED_FIFO");
176 
177 		(void)sched_is(__LINE__, 0, SCHED_FIFO);
178 
179 		q(__LINE__, sched_getparam(0, &shouldbe), "sched_getparam");
180 
181 		if (shouldbe.sched_priority != fifo_schedmin)
182 			quit("sched_setscheduler wrong priority (min)");
183 
184 		rt_param.sched_priority = fifo_schedmin;
185 
186 		q(__LINE__, sched_setparam(0, &rt_param),
187 			"sched_setparam to fifo_schedmin");
188 
189 		rt_param.sched_priority = fifo_schedmin + 1;
190 
191 		q(__LINE__, sched_setparam(0, &rt_param),
192 			"sched_setparam to fifo_schedmin + 1");
193 
194 		q(__LINE__, sched_getparam(0, &shouldbe),
195 			"sched_getparam");
196 
197 		if (shouldbe.sched_priority != fifo_schedmin + 1)
198 			quit("sched_setscheduler wrong priority (min + 1)");
199 
200 		q(__LINE__, sched_setscheduler(0, SCHED_RR, &rt_param),
201 			"sched_setscheduler SCHED_RR");
202 
203 		(void)sched_is(__LINE__, 0, SCHED_RR);
204 
205 		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
206 			"sched_setscheduler restoring original scheduler");
207 
208 		(void)sched_is(__LINE__, 0, orig_scheduler);
209 	}
210 
211 
212 	{
213 		char nam[] = "P1003_1b_schedXXXXXX";
214 		int fd;
215 		pid_t p;
216 		pid_t *lastrun;
217 
218 		fd = mkstemp(nam);
219 		if (fd == -1)
220 			q(__LINE__, errno, "mkstemp failed");
221 
222 		(void)unlink(nam);
223 
224 		p = (pid_t)0;
225 
226 		write(fd, &p, sizeof(p));
227 
228 		q(__LINE__,  (int)(lastrun = mmap(0, sizeof(*lastrun), PROT_READ|PROT_WRITE,
229 		MAP_SHARED, fd, 0)), "mmap");
230 
231 		/* Set our priority at the highest:
232 		 */
233 		sched = SCHED_FIFO;
234 		rt_param.sched_priority = fifo_schedmax;
235 		q(__LINE__, sched_setscheduler(0, sched, &rt_param),
236 		"sched_setscheduler sched");
237 
238 		for (i = 0; i < n_instances; i++)
239 		{
240 			pid_t me;
241 
242 			/* XXX This is completely bogus.  The children never run.
243 			 */
244 			if ((me = fork()) != 0)
245 			{
246 				/* Parent.
247 				 */
248 				(void)sched_is(__LINE__, 0, sched);
249 
250 				/* Lower our priority:
251 				 */
252 				rt_param.sched_priority--;
253 
254 				q(__LINE__, sched_setscheduler(0, sched, &rt_param),
255 				"sched_setscheduler sched");
256 
257 				while (1)
258 				{
259 					q(__LINE__, sched_getparam(0, &rt_param), "sched_getparam");
260 
261 					rt_param.sched_priority--;
262 
263 
264 					if (rt_param.sched_priority < fifo_schedmin)
265 						exit(0);
266 
267 					*lastrun = me;
268 					q(__LINE__, sched_setparam(0, &rt_param), "sched_setparam");
269 
270 					if (*lastrun == me)
271 					{
272 						/* The child will run twice
273 						 * at  the end:
274 						 */
275 						if (!me || rt_param.sched_priority != 0)
276 						{
277 							fprintf(stderr,
278 							"ran process %ld twice at priority %d\n",
279 							(long)me, rt_param.sched_priority + 1);
280 							exit(-1);
281 						}
282 					}
283 				}
284 			}
285 		}
286 	}
287 
288 	return 0;
289 }
290 #ifdef STANDALONE_TESTS
291 int main(int argc, char *argv[]) { return sched(argc, argv); }
292 #endif
293