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
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
33 /* ksched: Soft real time scheduling based on "rtprio".
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_posix.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/mutex.h>
46 #include <sys/posix4.h>
47 #include <sys/resource.h>
48 #include <sys/sched.h>
50 /* ksched: Real-time extension to support POSIX priority scheduling.
54 struct timespec rr_interval;
58 ksched_attach(struct ksched **p)
60 struct ksched *ksched= p31b_malloc(sizeof(*ksched));
62 ksched->rr_interval.tv_sec = 0;
63 ksched->rr_interval.tv_nsec = 1000000000L / sched_rr_interval();
70 ksched_detach(struct ksched *ks)
78 * XXX About priorities
80 * POSIX 1003.1b requires that numerically higher priorities be of
81 * higher priority. It also permits sched_setparam to be
82 * implementation defined for SCHED_OTHER. I don't like
83 * the notion of inverted priorites for normal processes when
84 * you can use "setpriority" for that.
86 * I'm rejecting sched_setparam for SCHED_OTHER with EINVAL.
89 /* Macros to convert between the unix (lower numerically is higher priority)
90 * and POSIX 1003.1b (higher numerically is higher priority)
93 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
94 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
96 /* These improve readability a bit for me:
98 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
99 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
102 getscheduler(struct ksched *ksched, struct thread *td, int *policy)
107 mtx_lock_spin(&sched_lock);
108 pri_to_rtp(td, &rtp);
109 mtx_unlock_spin(&sched_lock);
113 *policy = SCHED_FIFO;
116 case RTP_PRIO_REALTIME:
121 *policy = SCHED_OTHER;
129 ksched_setparam(struct ksched *ksched,
130 struct thread *td, const struct sched_param *param)
135 e = getscheduler(ksched, td, &policy);
139 if (policy == SCHED_OTHER)
142 e = ksched_setscheduler(ksched, td, policy, param);
149 ksched_getparam(struct ksched *ksched,
150 struct thread *td, struct sched_param *param)
154 mtx_lock_spin(&sched_lock);
155 pri_to_rtp(td, &rtp);
156 mtx_unlock_spin(&sched_lock);
157 if (RTP_PRIO_IS_REALTIME(rtp.type))
158 param->sched_priority = rtpprio_to_p4prio(rtp.prio);
164 * XXX The priority and scheduler modifications should
165 * be moved into published interfaces in kern/kern_sync.
167 * The permissions to modify process p were checked in "p31b_proc()".
171 ksched_setscheduler(struct ksched *ksched,
172 struct thread *td, int policy, const struct sched_param *param)
182 if (param->sched_priority >= P1B_PRIO_MIN &&
183 param->sched_priority <= P1B_PRIO_MAX)
185 rtp.prio = p4prio_to_rtpprio(param->sched_priority);
186 rtp.type = (policy == SCHED_FIFO)
187 ? RTP_PRIO_FIFO : RTP_PRIO_REALTIME;
189 mtx_lock_spin(&sched_lock);
190 rtp_to_pri(&rtp, td);
191 mtx_unlock_spin(&sched_lock);
201 rtp.type = RTP_PRIO_NORMAL;
202 rtp.prio = p4prio_to_rtpprio(param->sched_priority);
203 mtx_lock_spin(&sched_lock);
204 rtp_to_pri(&rtp, td);
205 mtx_unlock_spin(&sched_lock);
218 ksched_getscheduler(struct ksched *ksched, struct thread *td, int *policy)
220 return getscheduler(ksched, td, policy);
223 /* ksched_yield: Yield the CPU.
226 ksched_yield(struct ksched *ksched)
228 sched_relinquish(curthread);
233 ksched_get_priority_max(struct ksched *ksched, int policy, int *prio)
241 *prio = RTP_PRIO_MAX;
245 *prio = PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
256 ksched_get_priority_min(struct ksched *ksched, int policy, int *prio)
264 *prio = P1B_PRIO_MIN;
279 ksched_rr_get_interval(struct ksched *ksched,
280 struct thread *td, struct timespec *timespec)
282 *timespec = ksched->rr_interval;