sched.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* $OpenBSD: sched.h,v 1.38 2015/01/11 19:34:52 guenther Exp $ */
  2. /* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */
  3. /*-
  4. * Copyright (c) 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Ross Harvey.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*-
  32. * Copyright (c) 1982, 1986, 1991, 1993
  33. * The Regents of the University of California. All rights reserved.
  34. * (c) UNIX System Laboratories, Inc.
  35. * All or some portions of this file are derived from material licensed
  36. * to the University of California by American Telephone and Telegraph
  37. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  38. * the permission of UNIX System Laboratories, Inc.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. * 1. Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * 2. Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in the
  47. * documentation and/or other materials provided with the distribution.
  48. * 3. Neither the name of the University nor the names of its contributors
  49. * may be used to endorse or promote products derived from this software
  50. * without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  53. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  54. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  56. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  57. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  58. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  59. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  60. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  62. * SUCH DAMAGE.
  63. *
  64. * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
  65. */
  66. #ifndef _SYS_SCHED_H_
  67. #define _SYS_SCHED_H_
  68. #include <sys/queue.h>
  69. /*
  70. * Posix defines a <sched.h> which may want to include <sys/sched.h>
  71. */
  72. /*
  73. * CPU states.
  74. * XXX Not really scheduler state, but no other good place to put
  75. * it right now, and it really is per-CPU.
  76. */
  77. #define CP_USER 0
  78. #define CP_NICE 1
  79. #define CP_SYS 2
  80. #define CP_INTR 3
  81. #define CP_IDLE 4
  82. #define CPUSTATES 5
  83. #define SCHED_NQS 32 /* 32 run queues. */
  84. /*
  85. * Per-CPU scheduler state.
  86. * XXX - expose to userland for now.
  87. */
  88. struct schedstate_percpu {
  89. struct timespec spc_runtime; /* time curproc started running */
  90. volatile int spc_schedflags; /* flags; see below */
  91. u_int spc_schedticks; /* ticks for schedclock() */
  92. u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
  93. u_char spc_curpriority; /* usrpri of curproc */
  94. int spc_rrticks; /* ticks until roundrobin() */
  95. int spc_pscnt; /* prof/stat counter */
  96. int spc_psdiv; /* prof/stat divisor */
  97. struct proc *spc_idleproc; /* idle proc for this cpu */
  98. u_int spc_nrun; /* procs on the run queues */
  99. fixpt_t spc_ldavg; /* shortest load avg. for this cpu */
  100. TAILQ_HEAD(prochead, proc) spc_qs[SCHED_NQS];
  101. volatile uint32_t spc_whichqs;
  102. #ifdef notyet
  103. struct proc *spc_reaper; /* dead proc reaper */
  104. #endif
  105. LIST_HEAD(,proc) spc_deadproc;
  106. };
  107. #ifdef _KERNEL
  108. /* spc_flags */
  109. #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
  110. #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
  111. #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
  112. #define SPCF_SHOULDHALT 0x0004 /* CPU should be vacated */
  113. #define SPCF_HALTED 0x0008 /* CPU has been halted */
  114. #define SCHED_PPQ (128 / SCHED_NQS) /* priorities per queue */
  115. #define NICE_WEIGHT 2 /* priorities per nice level */
  116. #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - SCHED_PPQ)
  117. extern int schedhz; /* ideally: 16 */
  118. extern int rrticks_init; /* ticks per roundrobin() */
  119. struct proc;
  120. void schedclock(struct proc *);
  121. struct cpu_info;
  122. void roundrobin(struct cpu_info *);
  123. void scheduler_start(void);
  124. void updatepri(struct proc *);
  125. void userret(struct proc *p);
  126. void sched_init_cpu(struct cpu_info *);
  127. void sched_idle(void *);
  128. void sched_exit(struct proc *);
  129. void mi_switch(void);
  130. void cpu_switchto(struct proc *, struct proc *);
  131. struct proc *sched_chooseproc(void);
  132. struct cpu_info *sched_choosecpu(struct proc *);
  133. struct cpu_info *sched_choosecpu_fork(struct proc *parent, int);
  134. void cpu_idle_enter(void);
  135. void cpu_idle_cycle(void);
  136. void cpu_idle_leave(void);
  137. void sched_peg_curproc(struct cpu_info *ci);
  138. int sysctl_hwsetperf(void *, size_t *, void *, size_t);
  139. int sysctl_hwperfpolicy(void *, size_t *, void *, size_t);
  140. #ifdef MULTIPROCESSOR
  141. void sched_start_secondary_cpus(void);
  142. void sched_stop_secondary_cpus(void);
  143. #endif
  144. #define curcpu_is_idle() (curcpu()->ci_schedstate.spc_whichqs == 0)
  145. void sched_init_runqueues(void);
  146. void setrunqueue(struct proc *);
  147. void remrunqueue(struct proc *);
  148. /* Inherit the parent's scheduler history */
  149. #define scheduler_fork_hook(parent, child) do { \
  150. (child)->p_estcpu = (parent)->p_estcpu; \
  151. } while (0)
  152. /* Chargeback parents for the sins of their children. */
  153. #define scheduler_wait_hook(parent, child) do { \
  154. (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + (child)->p_estcpu);\
  155. } while (0)
  156. /* Allow other processes to progress */
  157. #define sched_pause() do { \
  158. if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) \
  159. yield(); \
  160. } while (0)
  161. #if defined(MULTIPROCESSOR)
  162. #include <sys/lock.h>
  163. /*
  164. * XXX Instead of using struct lock for the kernel lock and thus requiring us
  165. * XXX to implement simplelocks, causing all sorts of fine-grained locks all
  166. * XXX over our tree to be activated, the sched_lock is a different kind of
  167. * XXX lock to avoid introducing locking protocol bugs.
  168. */
  169. extern struct __mp_lock sched_lock;
  170. #define SCHED_ASSERT_LOCKED() \
  171. do { \
  172. splassert(IPL_SCHED); \
  173. KASSERT(__mp_lock_held(&sched_lock)); \
  174. } while (0)
  175. #define SCHED_ASSERT_UNLOCKED() KASSERT(__mp_lock_held(&sched_lock) == 0)
  176. #define SCHED_LOCK_INIT() __mp_lock_init(&sched_lock)
  177. #define SCHED_LOCK(s) \
  178. do { \
  179. s = splsched(); \
  180. __mp_lock(&sched_lock); \
  181. } while (/* CONSTCOND */ 0)
  182. #define SCHED_UNLOCK(s) \
  183. do { \
  184. __mp_unlock(&sched_lock); \
  185. splx(s); \
  186. } while (/* CONSTCOND */ 0)
  187. #else /* ! MULTIPROCESSOR */
  188. #define SCHED_ASSERT_LOCKED() splassert(IPL_SCHED);
  189. #define SCHED_ASSERT_UNLOCKED() /* nothing */
  190. #define SCHED_LOCK_INIT() /* nothing */
  191. #define SCHED_LOCK(s) s = splsched()
  192. #define SCHED_UNLOCK(s) splx(s)
  193. #endif /* MULTIPROCESSOR */
  194. #endif /* _KERNEL */
  195. #endif /* _SYS_SCHED_H_ */