etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / librt / t_sched.c
blob879604305e4f997c9f5216d8d1b39e79033cfbaa
1 /* $NetBSD: t_sched.c,v 1.5 2012/03/25 04:11:42 christos Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sched.c,v 1.5 2012/03/25 04:11:42 christos Exp $");
34 #include <sched.h>
35 #include <limits.h>
36 #include <unistd.h>
38 #include <atf-c.h>
40 static void sched_priority_set(int, int);
42 ATF_TC(sched_getparam);
43 ATF_TC_HEAD(sched_getparam, tc)
45 atf_tc_set_md_var(tc, "descr", "A basic test of sched_getparam(3)");
48 ATF_TC_BODY(sched_getparam, tc)
50 struct sched_param s1, s2;
51 pid_t p = getpid();
54 * IEEE Std 1003.1-2008: if the supplied pid is zero,
55 * the parameters for the calling process are returned.
57 ATF_REQUIRE(sched_getparam(0, &s1) == 0);
58 ATF_REQUIRE(sched_getparam(p, &s2) == 0);
60 ATF_CHECK_EQ(s1.sched_priority, s2.sched_priority);
63 * The behavior is undefined but should error
64 * out in case the supplied PID is negative.
66 ATF_REQUIRE(sched_getparam(-1, &s1) != 0);
69 ATF_TC(sched_priority);
70 ATF_TC_HEAD(sched_priority, tc)
72 atf_tc_set_md_var(tc, "descr", "Test sched(3) priority ranges");
75 ATF_TC_BODY(sched_priority, tc)
77 static const int pol[3] = { SCHED_OTHER, SCHED_FIFO, SCHED_RR };
78 int pmax, pmin;
79 size_t i;
82 * Test that bogus values error out.
84 if (INT_MAX > SCHED_RR)
85 ATF_REQUIRE(sched_get_priority_max(INT_MAX) != 0);
87 if (-INT_MAX < SCHED_OTHER)
88 ATF_REQUIRE(sched_get_priority_max(-INT_MAX) != 0);
91 * Test that we have a valid range.
93 for (i = 0; i < __arraycount(pol); i++) {
95 pmax = sched_get_priority_max(pol[i]);
96 pmin = sched_get_priority_min(pol[i]);
98 ATF_REQUIRE(pmax != -1);
99 ATF_REQUIRE(pmin != -1);
100 ATF_REQUIRE(pmax > pmin);
104 static void
105 sched_priority_set(int pri, int pol)
107 struct sched_param sched;
109 sched.sched_priority = pri;
111 ATF_REQUIRE(pri >= 0);
112 ATF_REQUIRE(sched_setscheduler(0, pol, &sched) == 0);
115 * Test that the policy was changed.
117 ATF_CHECK_EQ(sched_getscheduler(0), pol);
120 * And that sched_getparam(3) returns the new priority.
122 sched.sched_priority = -1;
124 ATF_REQUIRE(sched_getparam(0, &sched) == 0);
125 ATF_CHECK_EQ(sched.sched_priority, pri);
128 ATF_TC(sched_setscheduler_1);
129 ATF_TC_HEAD(sched_setscheduler_1, tc)
131 atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), max, RR");
132 atf_tc_set_md_var(tc, "require.user", "root");
135 ATF_TC_BODY(sched_setscheduler_1, tc)
137 int pri;
139 pri = sched_get_priority_max(SCHED_RR);
140 sched_priority_set(pri, SCHED_RR);
143 ATF_TC(sched_setscheduler_2);
144 ATF_TC_HEAD(sched_setscheduler_2, tc)
146 atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), min, RR");
147 atf_tc_set_md_var(tc, "require.user", "root");
150 ATF_TC_BODY(sched_setscheduler_2, tc)
152 int pri;
154 pri = sched_get_priority_min(SCHED_RR);
155 sched_priority_set(pri, SCHED_RR);
158 ATF_TC(sched_setscheduler_3);
159 ATF_TC_HEAD(sched_setscheduler_3, tc)
161 atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), max, FIFO");
162 atf_tc_set_md_var(tc, "require.user", "root");
165 ATF_TC_BODY(sched_setscheduler_3, tc)
167 int pri;
169 pri = sched_get_priority_max(SCHED_FIFO);
170 sched_priority_set(pri, SCHED_FIFO);
173 ATF_TC(sched_setscheduler_4);
174 ATF_TC_HEAD(sched_setscheduler_4, tc)
176 atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), min, FIFO");
177 atf_tc_set_md_var(tc, "require.user", "root");
180 ATF_TC_BODY(sched_setscheduler_4, tc)
182 int pri;
184 pri = sched_get_priority_min(SCHED_FIFO);
185 sched_priority_set(pri, SCHED_FIFO);
188 ATF_TC(sched_rr_get_interval_1);
189 ATF_TC_HEAD(sched_rr_get_interval_1, tc)
191 atf_tc_set_md_var(tc, "descr", "Test sched_rr_get_interval(3), #1"
192 " (PR lib/44768)");
193 atf_tc_set_md_var(tc, "require.user", "root");
196 ATF_TC_BODY(sched_rr_get_interval_1, tc)
198 struct timespec tv;
199 int pri;
201 pri = sched_get_priority_min(SCHED_RR);
202 sched_priority_set(pri, SCHED_RR);
205 * This should fail with ESRCH for invalid PID.
207 ATF_REQUIRE(sched_rr_get_interval(-INT_MAX, &tv) != 0);
210 ATF_TC(sched_rr_get_interval_2);
211 ATF_TC_HEAD(sched_rr_get_interval_2, tc)
213 atf_tc_set_md_var(tc, "descr", "Test sched_rr_get_interval(3), #2");
214 atf_tc_set_md_var(tc, "require.user", "root");
217 ATF_TC_BODY(sched_rr_get_interval_2, tc)
219 struct timespec tv1, tv2;
220 int pri;
222 pri = sched_get_priority_min(SCHED_RR);
223 sched_priority_set(pri, SCHED_RR);
225 tv1.tv_sec = tv2.tv_sec = -1;
226 tv1.tv_nsec = tv2.tv_nsec = -1;
228 ATF_REQUIRE(sched_rr_get_interval(0, &tv1) == 0);
229 ATF_REQUIRE(sched_rr_get_interval(getpid(), &tv2) == 0);
231 ATF_REQUIRE(tv1.tv_sec != -1);
232 ATF_REQUIRE(tv2.tv_sec != -1);
234 ATF_REQUIRE(tv1.tv_nsec != -1);
235 ATF_REQUIRE(tv2.tv_nsec != -1);
237 ATF_REQUIRE(tv1.tv_sec == tv2.tv_sec);
238 ATF_REQUIRE(tv1.tv_nsec == tv2.tv_nsec);
241 ATF_TP_ADD_TCS(tp)
244 ATF_TP_ADD_TC(tp, sched_getparam);
245 ATF_TP_ADD_TC(tp, sched_priority);
247 ATF_TP_ADD_TC(tp, sched_setscheduler_1);
248 ATF_TP_ADD_TC(tp, sched_setscheduler_2);
249 ATF_TP_ADD_TC(tp, sched_setscheduler_3);
250 ATF_TP_ADD_TC(tp, sched_setscheduler_4);
252 ATF_TP_ADD_TC(tp, sched_rr_get_interval_1);
253 ATF_TP_ADD_TC(tp, sched_rr_get_interval_2);
255 return atf_no_error();