mtree/BSD.root.dist: Use spaces.
[dragonfly.git] / sys / kern / kern_sched.c
blob4b423c1f2ae39516e851e45bb0f17ec01a41790c
1 /*
2 * Copyright (c) 1996, 1997
3 * HD Associates, Inc. All rights reserved.
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.
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.
32 * $FreeBSD: src/sys/posix4/ksched.c,v 1.7.2.1 2000/05/16 06:58:13 dillon Exp $
36 * ksched: Soft real time scheduling based on "rtprio".
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/posix4.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/resource.h>
45 #include <machine/cpu.h> /* For need_user_resched */
48 /* ksched: Real-time extension to support POSIX priority scheduling.
51 struct ksched {
52 struct timespec rr_interval;
55 int
56 ksched_attach(struct ksched **p)
58 struct ksched *ksched= p31b_malloc(sizeof(*ksched));
60 ksched->rr_interval.tv_sec = 0;
61 ksched->rr_interval.tv_nsec = 1000000000L / 10; /* XXX */
63 *p = ksched;
64 return 0;
67 int
68 ksched_detach(struct ksched *p)
70 p31b_free(p);
72 return 0;
76 * XXX About priorities
78 * POSIX 1003.1b requires that numerically higher priorities be of
79 * higher priority. It also permits sched_setparam to be
80 * implementation defined for SCHED_OTHER. I don't like
81 * the notion of inverted priorites for normal processes when
82 * you can use "setpriority" for that.
84 * I'm rejecting sched_setparam for SCHED_OTHER with EINVAL.
87 /* Macros to convert between the unix (lower numerically is higher priority)
88 * and POSIX 1003.1b (higher numerically is higher priority)
91 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
92 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
95 * These improve readability a bit for me:
97 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
98 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
100 static __inline int
101 getscheduler(register_t *ret, struct ksched *ksched, struct lwp *lp)
103 int e = 0;
105 switch (lp->lwp_rtprio.type) {
106 case RTP_PRIO_FIFO:
107 *ret = SCHED_FIFO;
108 break;
109 case RTP_PRIO_REALTIME:
110 *ret = SCHED_RR;
111 break;
112 default:
113 *ret = SCHED_OTHER;
114 break;
117 return e;
121 ksched_setparam(register_t *ret, struct ksched *ksched,
122 struct lwp *lp, const struct sched_param *param)
124 register_t policy;
125 int e;
127 e = getscheduler(&policy, ksched, lp);
129 if (e == 0) {
130 if (policy == SCHED_OTHER)
131 e = EINVAL;
132 else
133 e = ksched_setscheduler(ret, ksched, lp, policy, param);
136 return e;
140 ksched_getparam(register_t *ret, struct ksched *ksched,
141 struct lwp *lp, struct sched_param *param)
143 if (RTP_PRIO_IS_REALTIME(lp->lwp_rtprio.type))
144 param->sched_priority = rtpprio_to_p4prio(lp->lwp_rtprio.prio);
146 return 0;
150 * XXX The priority and scheduler modifications should
151 * be moved into published interfaces in kern/kern_sync.
153 * The permissions to modify process p were checked in "p31b_proc()".
157 ksched_setscheduler(register_t *ret, struct ksched *ksched,
158 struct lwp *lp, int policy, const struct sched_param *param)
160 int e = 0;
161 struct rtprio rtp;
163 switch(policy) {
164 case SCHED_RR:
165 case SCHED_FIFO:
166 if (param->sched_priority >= P1B_PRIO_MIN &&
167 param->sched_priority <= P1B_PRIO_MAX) {
168 rtp.prio = p4prio_to_rtpprio(param->sched_priority);
169 rtp.type = (policy == SCHED_FIFO) ?
170 RTP_PRIO_FIFO : RTP_PRIO_REALTIME;
172 lp->lwp_rtprio = rtp;
173 need_user_resched();
174 } else {
175 e = EINVAL;
177 break;
178 case SCHED_OTHER:
179 rtp.type = RTP_PRIO_NORMAL;
180 rtp.prio = p4prio_to_rtpprio(param->sched_priority);
181 lp->lwp_rtprio = rtp;
184 * XXX Simply revert to whatever we had for last
185 * normal scheduler priorities.
186 * This puts a requirement
187 * on the scheduling code: You must leave the
188 * scheduling info alone.
190 need_user_resched();
191 break;
194 return e;
198 ksched_getscheduler(register_t *ret, struct ksched *ksched, struct lwp *lp)
200 return getscheduler(ret, ksched, lp);
204 * ksched_yield: Yield the CPU.
206 * MPSAFE
209 ksched_yield(register_t *ret, struct ksched *ksched)
211 struct lwp *lp;
213 if ((lp = curthread->td_lwp) != NULL)
214 lp->lwp_proc->p_usched->yield(lp);
215 return 0;
219 * MPSAFE
222 ksched_get_priority_max(register_t*ret, struct ksched *ksched, int policy)
224 int e = 0;
226 switch (policy) {
227 case SCHED_FIFO:
228 case SCHED_RR:
229 *ret = RTP_PRIO_MAX;
230 break;
231 case SCHED_OTHER:
232 *ret = PRIO_MAX;
233 break;
234 default:
235 e = EINVAL;
236 break;
239 return e;
243 * MPSAFE
246 ksched_get_priority_min(register_t *ret, struct ksched *ksched, int policy)
248 int e = 0;
250 switch (policy) {
251 case SCHED_FIFO:
252 case SCHED_RR:
253 *ret = P1B_PRIO_MIN;
254 break;
255 case SCHED_OTHER:
256 *ret = PRIO_MIN;
257 break;
258 default:
259 e = EINVAL;
260 break;
262 return e;
266 * MPSAFE
269 ksched_rr_get_interval(register_t *ret, struct ksched *ksched,
270 struct lwp *lp, struct timespec *timespec)
272 *timespec = ksched->rr_interval;
274 return 0;