sched.h revision 187357
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 187357 2009-01-17 07:17:57Z 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 137187357Sjeff/* 138187357Sjeff * This routine provides a consistent thread name for use with KTR graphing 139187357Sjeff * functions. 140187357Sjeff */ 141187357Sjeffchar *sched_tdname(struct thread *td); 142187357Sjeff 143135076Sscottlstatic __inline void 144135076Sscottlsched_pin(void) 145135076Sscottl{ 146135076Sscottl curthread->td_pinned++; 147135076Sscottl} 148135076Sscottl 149135076Sscottlstatic __inline void 150135076Sscottlsched_unpin(void) 151135076Sscottl{ 152135076Sscottl curthread->td_pinned--; 153135076Sscottl} 154135076Sscottl 155166188Sjeff/* sched_add arguments (formerly setrunqueue) */ 156166188Sjeff#define SRQ_BORING 0x0000 /* No special circumstances. */ 157166188Sjeff#define SRQ_YIELDING 0x0001 /* We are yielding (from mi_switch). */ 158166188Sjeff#define SRQ_OURSELF 0x0002 /* It is ourself (from mi_switch). */ 159166188Sjeff#define SRQ_INTR 0x0004 /* It is probably urgent. */ 160166188Sjeff#define SRQ_PREEMPTED 0x0008 /* has been preempted.. be kind */ 161166188Sjeff#define SRQ_BORROWING 0x0010 /* Priority updated due to prio_lend */ 162166188Sjeff 163178272Sjeff/* Scheduler stats. */ 164170293Sjeff#ifdef SCHED_STATS 165178272Sjeffextern long sched_switch_stats[SWT_COUNT]; 166178272Sjeff 167178272Sjeff#define SCHED_STAT_DEFINE_VAR(name, ptr, descr) \ 168178272Sjeff SYSCTL_LONG(_kern_sched_stats, OID_AUTO, name, CTLFLAG_RD, ptr, 0, descr) 169178272Sjeff#define SCHED_STAT_DEFINE(name, descr) \ 170178272Sjeff unsigned long name; \ 171178272Sjeff SCHED_STAT_DEFINE_VAR(name, &name, descr) 172170293Sjeff#define SCHED_STAT_INC(var) atomic_add_long(&(var), 1) 173170293Sjeff#else 174178272Sjeff#define SCHED_STAT_DEFINE_VAR(name, descr, ptr) 175178272Sjeff#define SCHED_STAT_DEFINE(name, descr) 176170293Sjeff#define SCHED_STAT_INC(var) 177170293Sjeff#endif 178166188Sjeff 179177428Sjeff/* 180177428Sjeff * Fixup scheduler state for proc0 and thread0 181177428Sjeff */ 182134791Sjulianvoid schedinit(void); 183164185Strhodes#endif /* _KERNEL */ 184135470Sdas 185164185Strhodes/* POSIX 1003.1b Process Scheduling */ 186164185Strhodes 187164185Strhodes/* 188164185Strhodes * POSIX scheduling policies 189164185Strhodes */ 190164185Strhodes#define SCHED_FIFO 1 191164185Strhodes#define SCHED_OTHER 2 192164185Strhodes#define SCHED_RR 3 193164185Strhodes 194164185Strhodesstruct sched_param { 195164185Strhodes int sched_priority; 196164185Strhodes}; 197164185Strhodes 198164185Strhodes/* 199164185Strhodes * POSIX scheduling declarations for userland. 200164185Strhodes */ 201164185Strhodes#ifndef _KERNEL 202164185Strhodes#include <sys/cdefs.h> 203164185Strhodes#include <sys/_types.h> 204164185Strhodes 205164185Strhodes#ifndef _PID_T_DECLARED 206164185Strhodestypedef __pid_t pid_t; 207164185Strhodes#define _PID_T_DECLARED 208164185Strhodes#endif 209164185Strhodes 210164185Strhodesstruct timespec; 211164185Strhodes 212164185Strhodes__BEGIN_DECLS 213164185Strhodesint sched_get_priority_max(int); 214164185Strhodesint sched_get_priority_min(int); 215164185Strhodesint sched_getparam(pid_t, struct sched_param *); 216164185Strhodesint sched_getscheduler(pid_t); 217164185Strhodesint sched_rr_get_interval(pid_t, struct timespec *); 218164185Strhodesint sched_setparam(pid_t, const struct sched_param *); 219164185Strhodesint sched_setscheduler(pid_t, int, const struct sched_param *); 220164185Strhodesint sched_yield(void); 221164185Strhodes__END_DECLS 222164185Strhodes 223164185Strhodes#endif 224164185Strhodes#endif /* !_SCHED_H_ */ 225