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