sched.h revision 177428
1104964Sjeff/*-
2164185Strhodes * Copyright (c) 1996, 1997
3164185Strhodes *      HD Associates, Inc.  All rights reserved.
4164185Strhodes *
5164185Strhodes * Redistribution and use in source and binary forms, with or without
6164185Strhodes * modification, are permitted provided that the following conditions
7164185Strhodes * are met:
8164185Strhodes * 1. Redistributions of source code must retain the above copyright
9164185Strhodes *    notice, this list of conditions and the following disclaimer.
10164185Strhodes * 2. Redistributions in binary form must reproduce the above copyright
11164185Strhodes *    notice, this list of conditions and the following disclaimer in the
12164185Strhodes *    documentation and/or other materials provided with the distribution.
13164185Strhodes * 3. All advertising materials mentioning features or use of this software
14164185Strhodes *    must display the following acknowledgement:
15164185Strhodes *      This product includes software developed by HD Associates, Inc
16164185Strhodes *      and Jukka Antero Ukkonen.
17164185Strhodes * 4. Neither the name of the author nor the names of any co-contributors
18164185Strhodes *    may be used to endorse or promote products derived from this software
19164185Strhodes *    without specific prior written permission.
20164185Strhodes *
21164185Strhodes * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
22164185Strhodes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23164185Strhodes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24164185Strhodes * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
25164185Strhodes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26164185Strhodes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27164185Strhodes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28164185Strhodes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29164185Strhodes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30164185Strhodes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31164185Strhodes * SUCH DAMAGE.
32164185Strhodes */
33164185Strhodes
34164185Strhodes/*-
35176729Sjeff * Copyright (c) 2002-2008, Jeffrey Roberson <jeff@freebsd.org>
36104964Sjeff * All rights reserved.
37104964Sjeff *
38104964Sjeff * Redistribution and use in source and binary forms, with or without
39104964Sjeff * modification, are permitted provided that the following conditions
40104964Sjeff * are met:
41104964Sjeff * 1. Redistributions of source code must retain the above copyright
42104964Sjeff *    notice unmodified, this list of conditions, and the following
43104964Sjeff *    disclaimer.
44104964Sjeff * 2. Redistributions in binary form must reproduce the above copyright
45104964Sjeff *    notice, this list of conditions and the following disclaimer in the
46104964Sjeff *    documentation and/or other materials provided with the distribution.
47104964Sjeff *
48104964Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49104964Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50104964Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51104964Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52104964Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53104964Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54104964Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55104964Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56104964Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57104964Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58104964Sjeff *
59104964Sjeff * $FreeBSD: head/sys/sys/sched.h 177428 2008-03-20 03:09:15Z jeff $
60104964Sjeff */
61104964Sjeff
62164185Strhodes#ifndef _SCHED_H_
63164185Strhodes#define	_SCHED_H_
64104964Sjeff
65164185Strhodes#ifdef _KERNEL
66104964Sjeff/*
67104964Sjeff * General scheduling info.
68125287Sjeff *
69125287Sjeff * sched_load:
70125287Sjeff *	Total runnable non-ithread threads in the system.
71125287Sjeff *
72125287Sjeff * sched_runnable:
73125287Sjeff *	Runnable threads for this processor.
74104964Sjeff */
75125287Sjeffint	sched_load(void);
76104964Sjeffint	sched_rr_interval(void);
77104964Sjeffint	sched_runnable(void);
78104964Sjeff
79113355Sjeff/*
80113355Sjeff * Proc related scheduling hooks.
81113355Sjeff */
82132372Sjulianvoid	sched_exit(struct proc *p, struct thread *childtd);
83134791Sjulianvoid	sched_fork(struct thread *td, struct thread *childtd);
84170293Sjeffvoid	sched_fork_exit(struct thread *td);
85163709Sjbvoid	sched_class(struct thread *td, int class);
86130551Sjulianvoid	sched_nice(struct proc *p, int nice);
87104964Sjeff
88104964Sjeff/*
89122036Sjeff * Threads are switched in and out, block on resources, have temporary
90164936Sjulian * priorities inherited from their procs, and use up cpu time.
91104964Sjeff */
92113355Sjeffvoid	sched_exit_thread(struct thread *td, struct thread *child);
93113355Sjeffvoid	sched_fork_thread(struct thread *td, struct thread *child);
94161599Sdavidxuvoid	sched_lend_prio(struct thread *td, u_char prio);
95161599Sdavidxuvoid	sched_lend_user_prio(struct thread *td, u_char pri);
96122036Sjefffixpt_t	sched_pctcpu(struct thread *td);
97113355Sjeffvoid	sched_prio(struct thread *td, u_char prio);
98177085Sjeffvoid	sched_sleep(struct thread *td, int prio);
99135051Sjulianvoid	sched_switch(struct thread *td, struct thread *newtd, int flags);
100170293Sjeffvoid	sched_throw(struct thread *td);
101139453Sjhbvoid	sched_unlend_prio(struct thread *td, u_char prio);
102161599Sdavidxuvoid	sched_unlend_user_prio(struct thread *td, u_char pri);
103163709Sjbvoid	sched_user_prio(struct thread *td, u_char prio);
104113355Sjeffvoid	sched_userret(struct thread *td);
105104964Sjeffvoid	sched_wakeup(struct thread *td);
106177004Sjeffvoid	sched_preempt(struct thread *td);
107104964Sjeff
108104964Sjeff/*
109122036Sjeff * Threads are moved on and off of run queues
110104964Sjeff */
111134586Sjulianvoid	sched_add(struct thread *td, int flags);
112121127Sjeffvoid	sched_clock(struct thread *td);
113121127Sjeffvoid	sched_rem(struct thread *td);
114159570Sdavidxuvoid	sched_tick(void);
115159630Sdavidxuvoid	sched_relinquish(struct thread *td);
116166188Sjeffstruct thread *sched_choose(void);
117166188Sjeffvoid	sched_idletd(void *);
118104964Sjeff
119107126Sjeff/*
120122036Sjeff * Binding makes cpu affinity permanent while pinning is used to temporarily
121122036Sjeff * hold a thread on a particular CPU.
122107137Sjeff */
123122036Sjeffvoid	sched_bind(struct thread *td, int cpu);
124135076Sscottlstatic __inline void sched_pin(void);
125122036Sjeffvoid	sched_unbind(struct thread *td);
126135076Sscottlstatic __inline void sched_unpin(void);
127145256Sjkoshyint	sched_is_bound(struct thread *td);
128176729Sjeffvoid	sched_affinity(struct thread *td);
129107137Sjeff
130122036Sjeff/*
131107126Sjeff * These procedures tell the process data structure allocation code how
132107126Sjeff * many bytes to actually allocate.
133107126Sjeff */
134107126Sjeffint	sched_sizeof_proc(void);
135107126Sjeffint	sched_sizeof_thread(void);
136107126Sjeff
137135076Sscottlstatic __inline void
138135076Sscottlsched_pin(void)
139135076Sscottl{
140135076Sscottl	curthread->td_pinned++;
141135076Sscottl}
142135076Sscottl
143135076Sscottlstatic __inline void
144135076Sscottlsched_unpin(void)
145135076Sscottl{
146135076Sscottl	curthread->td_pinned--;
147135076Sscottl}
148135076Sscottl
149166188Sjeff/* sched_add arguments (formerly setrunqueue) */
150166188Sjeff#define	SRQ_BORING	0x0000		/* No special circumstances. */
151166188Sjeff#define	SRQ_YIELDING	0x0001		/* We are yielding (from mi_switch). */
152166188Sjeff#define	SRQ_OURSELF	0x0002		/* It is ourself (from mi_switch). */
153166188Sjeff#define	SRQ_INTR	0x0004		/* It is probably urgent. */
154166188Sjeff#define	SRQ_PREEMPTED	0x0008		/* has been preempted.. be kind */
155166188Sjeff#define	SRQ_BORROWING	0x0010		/* Priority updated due to prio_lend */
156166188Sjeff
157170293Sjeff/* Switch stats. */
158170293Sjeff#ifdef SCHED_STATS
159170293Sjeffextern long switch_preempt;
160170293Sjeffextern long switch_owepreempt;
161170293Sjeffextern long switch_turnstile;
162170293Sjeffextern long switch_sleepq;
163170293Sjeffextern long switch_sleepqtimo;
164170293Sjeffextern long switch_relinquish;
165170293Sjeffextern long switch_needresched;
166170293Sjeff#define SCHED_STAT_INC(var)     atomic_add_long(&(var), 1)
167170293Sjeff#else
168170293Sjeff#define SCHED_STAT_INC(var)
169170293Sjeff#endif
170166188Sjeff
171177428Sjeff/*
172177428Sjeff * Fixup scheduler state for proc0 and thread0
173177428Sjeff */
174134791Sjulianvoid schedinit(void);
175164185Strhodes#endif /* _KERNEL */
176135470Sdas
177164185Strhodes/* POSIX 1003.1b Process Scheduling */
178164185Strhodes
179164185Strhodes/*
180164185Strhodes * POSIX scheduling policies
181164185Strhodes */
182164185Strhodes#define SCHED_FIFO      1
183164185Strhodes#define SCHED_OTHER     2
184164185Strhodes#define SCHED_RR        3
185164185Strhodes
186164185Strhodesstruct sched_param {
187164185Strhodes        int     sched_priority;
188164185Strhodes};
189164185Strhodes
190164185Strhodes/*
191164185Strhodes * POSIX scheduling declarations for userland.
192164185Strhodes */
193164185Strhodes#ifndef _KERNEL
194164185Strhodes#include <sys/cdefs.h>
195164185Strhodes#include <sys/_types.h>
196164185Strhodes
197164185Strhodes#ifndef _PID_T_DECLARED
198164185Strhodestypedef __pid_t         pid_t;
199164185Strhodes#define _PID_T_DECLARED
200164185Strhodes#endif
201164185Strhodes
202164185Strhodesstruct timespec;
203164185Strhodes
204164185Strhodes__BEGIN_DECLS
205164185Strhodesint     sched_get_priority_max(int);
206164185Strhodesint     sched_get_priority_min(int);
207164185Strhodesint     sched_getparam(pid_t, struct sched_param *);
208164185Strhodesint     sched_getscheduler(pid_t);
209164185Strhodesint     sched_rr_get_interval(pid_t, struct timespec *);
210164185Strhodesint     sched_setparam(pid_t, const struct sched_param *);
211164185Strhodesint     sched_setscheduler(pid_t, int, const struct sched_param *);
212164185Strhodesint     sched_yield(void);
213164185Strhodes__END_DECLS
214164185Strhodes
215164185Strhodes#endif
216164185Strhodes#endif /* !_SCHED_H_ */
217