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$ 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); 93298145Skibu_int sched_estcpu(struct thread *td); 94113355Sjeffvoid sched_fork_thread(struct thread *td, struct thread *child); 95161599Sdavidxuvoid sched_lend_prio(struct thread *td, u_char prio); 96161599Sdavidxuvoid sched_lend_user_prio(struct thread *td, u_char pri); 97122036Sjefffixpt_t sched_pctcpu(struct thread *td); 98113355Sjeffvoid sched_prio(struct thread *td, u_char prio); 99177085Sjeffvoid sched_sleep(struct thread *td, int prio); 100135051Sjulianvoid sched_switch(struct thread *td, struct thread *newtd, int flags); 101170293Sjeffvoid sched_throw(struct thread *td); 102139453Sjhbvoid sched_unlend_prio(struct thread *td, u_char prio); 103163709Sjbvoid sched_user_prio(struct thread *td, u_char prio); 104113355Sjeffvoid sched_userret(struct thread *td); 105104964Sjeffvoid sched_wakeup(struct thread *td); 106242139Strasz#ifdef RACCT 107242139Strasz#ifdef SCHED_4BSD 108242139Straszfixpt_t sched_pctcpu_delta(struct thread *td); 109242139Strasz#endif 110242139Strasz#endif 111104964Sjeff 112104964Sjeff/* 113122036Sjeff * Threads are moved on and off of run queues 114104964Sjeff */ 115134586Sjulianvoid sched_add(struct thread *td, int flags); 116121127Sjeffvoid sched_clock(struct thread *td); 117298145Skibvoid sched_preempt(struct thread *td); 118121127Sjeffvoid sched_rem(struct thread *td); 119159630Sdavidxuvoid sched_relinquish(struct thread *td); 120166188Sjeffstruct thread *sched_choose(void); 121166188Sjeffvoid sched_idletd(void *); 122104964Sjeff 123107126Sjeff/* 124122036Sjeff * Binding makes cpu affinity permanent while pinning is used to temporarily 125122036Sjeff * hold a thread on a particular CPU. 126107137Sjeff */ 127122036Sjeffvoid sched_bind(struct thread *td, int cpu); 128135076Sscottlstatic __inline void sched_pin(void); 129122036Sjeffvoid sched_unbind(struct thread *td); 130135076Sscottlstatic __inline void sched_unpin(void); 131145256Sjkoshyint sched_is_bound(struct thread *td); 132176729Sjeffvoid sched_affinity(struct thread *td); 133107137Sjeff 134122036Sjeff/* 135107126Sjeff * These procedures tell the process data structure allocation code how 136107126Sjeff * many bytes to actually allocate. 137107126Sjeff */ 138107126Sjeffint sched_sizeof_proc(void); 139107126Sjeffint sched_sizeof_thread(void); 140107126Sjeff 141187357Sjeff/* 142187357Sjeff * This routine provides a consistent thread name for use with KTR graphing 143187357Sjeff * functions. 144187357Sjeff */ 145187357Sjeffchar *sched_tdname(struct thread *td); 146232700Sjhb#ifdef KTR 147232700Sjhbvoid sched_clear_tdname(struct thread *td); 148232700Sjhb#endif 149187357Sjeff 150135076Sscottlstatic __inline void 151135076Sscottlsched_pin(void) 152135076Sscottl{ 153135076Sscottl curthread->td_pinned++; 154242274Sattilio __compiler_membar(); 155135076Sscottl} 156135076Sscottl 157135076Sscottlstatic __inline void 158135076Sscottlsched_unpin(void) 159135076Sscottl{ 160242274Sattilio __compiler_membar(); 161135076Sscottl curthread->td_pinned--; 162135076Sscottl} 163135076Sscottl 164166188Sjeff/* sched_add arguments (formerly setrunqueue) */ 165166188Sjeff#define SRQ_BORING 0x0000 /* No special circumstances. */ 166166188Sjeff#define SRQ_YIELDING 0x0001 /* We are yielding (from mi_switch). */ 167166188Sjeff#define SRQ_OURSELF 0x0002 /* It is ourself (from mi_switch). */ 168166188Sjeff#define SRQ_INTR 0x0004 /* It is probably urgent. */ 169166188Sjeff#define SRQ_PREEMPTED 0x0008 /* has been preempted.. be kind */ 170166188Sjeff#define SRQ_BORROWING 0x0010 /* Priority updated due to prio_lend */ 171166188Sjeff 172178272Sjeff/* Scheduler stats. */ 173170293Sjeff#ifdef SCHED_STATS 174194936SjeffDPCPU_DECLARE(long, sched_switch_stats[SWT_COUNT]); 175178272Sjeff 176178272Sjeff#define SCHED_STAT_DEFINE_VAR(name, ptr, descr) \ 177194936Sjeffstatic void name ## _add_proc(void *dummy __unused) \ 178194936Sjeff{ \ 179194936Sjeff \ 180194936Sjeff SYSCTL_ADD_PROC(NULL, \ 181194936Sjeff SYSCTL_STATIC_CHILDREN(_kern_sched_stats), OID_AUTO, \ 182194936Sjeff #name, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE, \ 183194936Sjeff ptr, 0, sysctl_dpcpu_long, "LU", descr); \ 184194936Sjeff} \ 185253604SavgSYSINIT(name, SI_SUB_LAST, SI_ORDER_MIDDLE, name ## _add_proc, NULL); 186194936Sjeff 187178272Sjeff#define SCHED_STAT_DEFINE(name, descr) \ 188194936Sjeff DPCPU_DEFINE(unsigned long, name); \ 189194936Sjeff SCHED_STAT_DEFINE_VAR(name, &DPCPU_NAME(name), descr) 190194936Sjeff/* 191194936Sjeff * Sched stats are always incremented in critical sections so no atomic 192194936Sjeff * is necesssary to increment them. 193194936Sjeff */ 194194936Sjeff#define SCHED_STAT_INC(var) DPCPU_GET(var)++; 195170293Sjeff#else 196178272Sjeff#define SCHED_STAT_DEFINE_VAR(name, descr, ptr) 197178272Sjeff#define SCHED_STAT_DEFINE(name, descr) 198194578Srdivacky#define SCHED_STAT_INC(var) (void)0 199170293Sjeff#endif 200166188Sjeff 201177428Sjeff/* 202177428Sjeff * Fixup scheduler state for proc0 and thread0 203177428Sjeff */ 204134791Sjulianvoid schedinit(void); 205164185Strhodes#endif /* _KERNEL */ 206135470Sdas 207164185Strhodes/* POSIX 1003.1b Process Scheduling */ 208164185Strhodes 209164185Strhodes/* 210164185Strhodes * POSIX scheduling policies 211164185Strhodes */ 212164185Strhodes#define SCHED_FIFO 1 213164185Strhodes#define SCHED_OTHER 2 214164185Strhodes#define SCHED_RR 3 215164185Strhodes 216164185Strhodesstruct sched_param { 217164185Strhodes int sched_priority; 218164185Strhodes}; 219164185Strhodes 220164185Strhodes/* 221164185Strhodes * POSIX scheduling declarations for userland. 222164185Strhodes */ 223164185Strhodes#ifndef _KERNEL 224164185Strhodes#include <sys/cdefs.h> 225301026Sed#include <sys/_timespec.h> 226164185Strhodes#include <sys/_types.h> 227164185Strhodes 228164185Strhodes#ifndef _PID_T_DECLARED 229164185Strhodestypedef __pid_t pid_t; 230164185Strhodes#define _PID_T_DECLARED 231164185Strhodes#endif 232164185Strhodes 233164185Strhodes__BEGIN_DECLS 234164185Strhodesint sched_get_priority_max(int); 235164185Strhodesint sched_get_priority_min(int); 236164185Strhodesint sched_getparam(pid_t, struct sched_param *); 237164185Strhodesint sched_getscheduler(pid_t); 238164185Strhodesint sched_rr_get_interval(pid_t, struct timespec *); 239164185Strhodesint sched_setparam(pid_t, const struct sched_param *); 240164185Strhodesint sched_setscheduler(pid_t, int, const struct sched_param *); 241164185Strhodesint sched_yield(void); 242164185Strhodes__END_DECLS 243164185Strhodes 244164185Strhodes#endif 245164185Strhodes#endif /* !_SCHED_H_ */ 246