xref: /freebsd/contrib/netbsd-tests/lib/librt/t_sched.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /* $NetBSD: t_sched.c,v 1.5 2012/03/25 04:11:42 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sched.c,v 1.5 2012/03/25 04:11:42 christos Exp $");
33 
34 #include <sched.h>
35 #include <limits.h>
36 #include <unistd.h>
37 
38 #include <atf-c.h>
39 
40 static void	 sched_priority_set(int, int);
41 
42 ATF_TC(sched_getparam);
43 ATF_TC_HEAD(sched_getparam, tc)
44 {
45 	atf_tc_set_md_var(tc, "descr", "A basic test of sched_getparam(3)");
46 }
47 
48 ATF_TC_BODY(sched_getparam, tc)
49 {
50 	struct sched_param s1, s2;
51 	pid_t p = getpid();
52 
53 	/*
54 	 * IEEE Std 1003.1-2008: if the supplied pid is zero,
55 	 * the parameters for the calling process are returned.
56 	 */
57 	ATF_REQUIRE(sched_getparam(0, &s1) == 0);
58 	ATF_REQUIRE(sched_getparam(p, &s2) == 0);
59 
60 	ATF_CHECK_EQ(s1.sched_priority, s2.sched_priority);
61 
62 	/*
63 	 * The behavior is undefined but should error
64 	 * out in case the supplied PID is negative.
65 	 */
66 	ATF_REQUIRE(sched_getparam(-1, &s1) != 0);
67 }
68 
69 ATF_TC(sched_priority);
70 ATF_TC_HEAD(sched_priority, tc)
71 {
72 	atf_tc_set_md_var(tc, "descr", "Test sched(3) priority ranges");
73 }
74 
75 ATF_TC_BODY(sched_priority, tc)
76 {
77 	static const int pol[3] = { SCHED_OTHER, SCHED_FIFO, SCHED_RR };
78 	int pmax, pmin;
79 	size_t i;
80 
81 	/*
82 	 * Test that bogus values error out.
83 	 */
84 	if (INT_MAX > SCHED_RR)
85 		ATF_REQUIRE(sched_get_priority_max(INT_MAX) != 0);
86 
87 	if (-INT_MAX < SCHED_OTHER)
88 		ATF_REQUIRE(sched_get_priority_max(-INT_MAX) != 0);
89 
90 	/*
91 	 * Test that we have a valid range.
92 	 */
93 	for (i = 0; i < __arraycount(pol); i++) {
94 
95 		pmax = sched_get_priority_max(pol[i]);
96 		pmin = sched_get_priority_min(pol[i]);
97 
98 		ATF_REQUIRE(pmax != -1);
99 		ATF_REQUIRE(pmin != -1);
100 		ATF_REQUIRE(pmax > pmin);
101 	}
102 }
103 
104 static void
105 sched_priority_set(int pri, int pol)
106 {
107 	struct sched_param sched;
108 
109 	sched.sched_priority = pri;
110 
111 	ATF_REQUIRE(pri >= 0);
112 	ATF_REQUIRE(sched_setscheduler(0, pol, &sched) == 0);
113 
114 	/*
115 	 * Test that the policy was changed.
116 	 */
117 	ATF_CHECK_EQ(sched_getscheduler(0), pol);
118 
119 	/*
120 	 * And that sched_getparam(3) returns the new priority.
121 	 */
122 	sched.sched_priority = -1;
123 
124 	ATF_REQUIRE(sched_getparam(0, &sched) == 0);
125 	ATF_CHECK_EQ(sched.sched_priority, pri);
126 }
127 
128 ATF_TC(sched_setscheduler_1);
129 ATF_TC_HEAD(sched_setscheduler_1, tc)
130 {
131 	atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), max, RR");
132 	atf_tc_set_md_var(tc, "require.user", "root");
133 }
134 
135 ATF_TC_BODY(sched_setscheduler_1, tc)
136 {
137 	int pri;
138 
139 	pri = sched_get_priority_max(SCHED_RR);
140 	sched_priority_set(pri, SCHED_RR);
141 }
142 
143 ATF_TC(sched_setscheduler_2);
144 ATF_TC_HEAD(sched_setscheduler_2, tc)
145 {
146 	atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), min, RR");
147 	atf_tc_set_md_var(tc, "require.user", "root");
148 }
149 
150 ATF_TC_BODY(sched_setscheduler_2, tc)
151 {
152 	int pri;
153 
154 	pri = sched_get_priority_min(SCHED_RR);
155 	sched_priority_set(pri, SCHED_RR);
156 }
157 
158 ATF_TC(sched_setscheduler_3);
159 ATF_TC_HEAD(sched_setscheduler_3, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), max, FIFO");
162 	atf_tc_set_md_var(tc, "require.user", "root");
163 }
164 
165 ATF_TC_BODY(sched_setscheduler_3, tc)
166 {
167 	int pri;
168 
169 	pri = sched_get_priority_max(SCHED_FIFO);
170 	sched_priority_set(pri, SCHED_FIFO);
171 }
172 
173 ATF_TC(sched_setscheduler_4);
174 ATF_TC_HEAD(sched_setscheduler_4, tc)
175 {
176 	atf_tc_set_md_var(tc, "descr", "sched_setscheduler(3), min, FIFO");
177 	atf_tc_set_md_var(tc, "require.user", "root");
178 }
179 
180 ATF_TC_BODY(sched_setscheduler_4, tc)
181 {
182 	int pri;
183 
184 	pri = sched_get_priority_min(SCHED_FIFO);
185 	sched_priority_set(pri, SCHED_FIFO);
186 }
187 
188 ATF_TC(sched_rr_get_interval_1);
189 ATF_TC_HEAD(sched_rr_get_interval_1, tc)
190 {
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");
194 }
195 
196 ATF_TC_BODY(sched_rr_get_interval_1, tc)
197 {
198 	struct timespec tv;
199 	int pri;
200 
201 	pri = sched_get_priority_min(SCHED_RR);
202 	sched_priority_set(pri, SCHED_RR);
203 
204 	/*
205 	 * This should fail with ESRCH for invalid PID.
206 	 */
207 	ATF_REQUIRE(sched_rr_get_interval(-INT_MAX, &tv) != 0);
208 }
209 
210 ATF_TC(sched_rr_get_interval_2);
211 ATF_TC_HEAD(sched_rr_get_interval_2, tc)
212 {
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");
215 }
216 
217 ATF_TC_BODY(sched_rr_get_interval_2, tc)
218 {
219 	struct timespec tv1, tv2;
220 	int pri;
221 
222 	pri = sched_get_priority_min(SCHED_RR);
223 	sched_priority_set(pri, SCHED_RR);
224 
225 	tv1.tv_sec = tv2.tv_sec = -1;
226 	tv1.tv_nsec = tv2.tv_nsec = -1;
227 
228 	ATF_REQUIRE(sched_rr_get_interval(0, &tv1) == 0);
229 	ATF_REQUIRE(sched_rr_get_interval(getpid(), &tv2) == 0);
230 
231 	ATF_REQUIRE(tv1.tv_sec != -1);
232 	ATF_REQUIRE(tv2.tv_sec != -1);
233 
234 	ATF_REQUIRE(tv1.tv_nsec != -1);
235 	ATF_REQUIRE(tv2.tv_nsec != -1);
236 
237 	ATF_REQUIRE(tv1.tv_sec == tv2.tv_sec);
238 	ATF_REQUIRE(tv1.tv_nsec == tv2.tv_nsec);
239 }
240 
241 ATF_TP_ADD_TCS(tp)
242 {
243 
244 	ATF_TP_ADD_TC(tp, sched_getparam);
245 	ATF_TP_ADD_TC(tp, sched_priority);
246 
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);
251 
252 	ATF_TP_ADD_TC(tp, sched_rr_get_interval_1);
253 	ATF_TP_ADD_TC(tp, sched_rr_get_interval_2);
254 
255 	return atf_no_error();
256 }
257