aboutsummaryrefslogtreecommitdiff
path: root/tools/regression/p1003_1b/sched.c
blob: 1814c79194e4a176f9a5a1f180f8f792f36ca5b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (c) 1996-1999
 *	HD Associates, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by HD Associates, Inc
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * $FreeBSD$
 *
 */

/* XXX: The spec says that if _POSIX_C_SOURCE is defined then
 *      _POSIX_SOURCE is ignored.  However, this is similar to
 *      the code in the O'Reilly "POSIX.4" book
 */

#define _POSIX_VERSION 199309L
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L

#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <stdio.h>
#define	__XSI_VISIBLE 1
#include <stdlib.h>
#undef __XSI_VISIBLE
#include <string.h>
#include <unistd.h>

#include "prutil.h"

static FILE *verbose;

static void
checkpris(int sched)
{
	int smin;
	int smax;

	errno = 0;

	if ( (smin = sched_get_priority_min(sched)) == -1 && errno)
		quit("sched_get_priority_min");

	if ( (smax = sched_get_priority_max(sched)) == -1 && errno)
		quit("sched_get_priority_max");

	if (smax - smin + 1 < 32 || smax < smin) {
		fprintf(stderr, "Illegal priority range for %s: %d to %d\n",
		sched_text(sched), smin, smax);
		exit(-1);
	}

	if (verbose)
		fprintf(verbose, "%12s: sched_min %2d sched_max %2d\n",
		sched_text(sched), smin, smax);
}

/* Set "try_anyway" to quit if you don't want to go on when
 * it doesn't look like something should work.
 */
static void try_anyway(const char *s)
{
	fputs(s, stderr);
	fprintf(stderr, "(trying anyway)\n");
	errno = 0;
}

static void q(int line, int code, const char *text)
{
	if (code == -1)
	{
		fprintf(stderr, "Error at line %d:\n", line);
		perror(text);
		exit(errno);
	}
}

int sched(int ac, char *av[])
{
	int fifo_schedmin, fifo_schedmax;
	int i;
	struct sched_param rt_param;
	int n_instances = 10;
	int sched;

	verbose = 0;

#if _POSIX_VERSION < 199309
	try_anyway("The _POSIX_VERSION predates P1003.1B\n");
#endif

#if !defined(_POSIX_PRIORITY_SCHEDULING)
	try_anyway(
	"The environment does not claim to support Posix scheduling.\n");
#endif

	/* Is priority scheduling configured?
	 */
	errno = 0;
	if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) {
		if (errno != 0) {
			/* This isn't valid - may be a standard violation
			 */
			quit("(should not happen) sysconf(_SC_PRIORITY_SCHEDULING)");
		}
		else {
			try_anyway(
			"The environment does not have run-time "
			"support for Posix scheduling.\n");
		}
	}

	/* Check that the priorities seem reasonable.
	 */

	checkpris(SCHED_FIFO);
	checkpris(SCHED_RR);
	checkpris(SCHED_OTHER);

/* BSD extensions?
 */
#if defined(SCHED_IDLE)
	checkpris(SCHED_IDLE);
#endif

	fifo_schedmin = sched_get_priority_min(SCHED_FIFO);
	fifo_schedmax = sched_get_priority_max(SCHED_FIFO);

	/* Make sure we can do some basic schedule switching:
	 */
	{
		struct sched_param orig_param, shouldbe;
		int orig_scheduler = sched_is(__LINE__, &orig_param, -1);

		if (verbose)
			fprintf(verbose,
			"The original scheduler is %s and the priority is %d.\n",
			sched_text(orig_scheduler), orig_param.sched_priority);

		/* Basic check: Try to set current settings:
		 */
		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
			"sched_setscheduler: Can't set original scheduler");

		rt_param.sched_priority = fifo_schedmin;

		q(__LINE__, sched_setscheduler(0, SCHED_FIFO, &rt_param),
		"sched_setscheduler SCHED_FIFO");

		(void)sched_is(__LINE__, 0, SCHED_FIFO);

		q(__LINE__, sched_getparam(0, &shouldbe), "sched_getparam");

		if (shouldbe.sched_priority != fifo_schedmin)
			quit("sched_setscheduler wrong priority (min)");

		rt_param.sched_priority = fifo_schedmin;

		q(__LINE__, sched_setparam(0, &rt_param),
			"sched_setparam to fifo_schedmin");

		rt_param.sched_priority = fifo_schedmin + 1;

		q(__LINE__, sched_setparam(0, &rt_param),
			"sched_setparam to fifo_schedmin + 1");

		q(__LINE__, sched_getparam(0, &shouldbe),
			"sched_getparam");

		if (shouldbe.sched_priority != fifo_schedmin + 1)
			quit("sched_setscheduler wrong priority (min + 1)");

		q(__LINE__, sched_setscheduler(0, SCHED_RR, &rt_param),
			"sched_setscheduler SCHED_RR");

		(void)sched_is(__LINE__, 0, SCHED_RR);

		q(__LINE__, sched_setscheduler(0, orig_scheduler, &orig_param),
			"sched_setscheduler restoring original scheduler");

		(void)sched_is(__LINE__, 0, orig_scheduler);
	}


	{
		char nam[] = "P1003_1b_schedXXXXXX";
		int fd;
		pid_t p;
		pid_t *lastrun;

		fd = mkstemp(nam);
		if (fd == -1)
			q(__LINE__, errno, "mkstemp failed");

		(void)unlink(nam);

		p = (pid_t)0;

		write(fd, &p, sizeof(p));

		q(__LINE__,  (int)(lastrun = mmap(0, sizeof(*lastrun), PROT_READ|PROT_WRITE,
		MAP_SHARED, fd, 0)), "mmap");

		/* Set our priority at the highest:
		 */
		sched = SCHED_FIFO;
		rt_param.sched_priority = fifo_schedmax;
		q(__LINE__, sched_setscheduler(0, sched, &rt_param),
		"sched_setscheduler sched");

		for (i = 0; i < n_instances; i++)
		{
			pid_t me;

			/* XXX This is completely bogus.  The children never run.
			 */
			if ((me = fork()) != 0)
			{
				/* Parent.
				 */
				(void)sched_is(__LINE__, 0, sched);

				/* Lower our priority:
				 */
				rt_param.sched_priority--;

				q(__LINE__, sched_setscheduler(0, sched, &rt_param),
				"sched_setscheduler sched");

				while (1)
				{
					q(__LINE__, sched_getparam(0, &rt_param), "sched_getparam");

					rt_param.sched_priority--;


					if (rt_param.sched_priority < fifo_schedmin)
						exit(0);

					*lastrun = me;
					q(__LINE__, sched_setparam(0, &rt_param), "sched_setparam");

					if (*lastrun == me)
					{
						/* The child will run twice
						 * at  the end:
						 */
						if (!me || rt_param.sched_priority != 0)
						{
							fprintf(stderr,
							"ran process %ld twice at priority %d\n",
							(long)me, rt_param.sched_priority + 1);
							exit(-1);
						}
					}
				}
			}
		}
	}

	return 0;
}
#ifdef STANDALONE_TESTS
int main(int argc, char *argv[]) { return sched(argc, argv); }
#endif