sched.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * File: sched.h
  28. * Author: Avadis Tevanian, Jr.
  29. * Date: 1985
  30. *
  31. * Header file for scheduler.
  32. *
  33. */
  34. #ifndef _KERN_SCHED_H_
  35. #define _KERN_SCHED_H_
  36. #include <kern/queue.h>
  37. #include <kern/lock.h>
  38. #include <kern/kern_types.h>
  39. #include <kern/macros.h>
  40. #if MACH_FIXPRI
  41. #include <mach/policy.h>
  42. #endif /* MACH_FIXPRI */
  43. #if STAT_TIME
  44. /*
  45. * Statistical timing uses microseconds as timer units. 18 bit shift
  46. * yields priorities. PRI_SHIFT_2 isn't needed.
  47. */
  48. #define PRI_SHIFT 18
  49. #else /* STAT_TIME */
  50. /*
  51. * Otherwise machine provides shift(s) based on time units it uses.
  52. */
  53. #include <machine/sched_param.h>
  54. #endif /* STAT_TIME */
  55. #define NRQS 50 /* 50 run queues per cpu */
  56. struct run_queue {
  57. queue_head_t runq[NRQS]; /* one for each priority */
  58. decl_simple_lock_data(, lock) /* one lock for all queues */
  59. int low; /* low queue value */
  60. int count; /* count of threads runable */
  61. };
  62. typedef struct run_queue *run_queue_t;
  63. #define RUN_QUEUE_NULL ((run_queue_t) 0)
  64. #if MACH_FIXPRI
  65. /*
  66. * NOTE: For fixed priority threads, first_quantum indicates
  67. * whether context switch at same priority is ok. For timeshareing
  68. * it indicates whether preempt is ok.
  69. */
  70. #define csw_needed(thread, processor) ((thread)->state & TH_SUSP || \
  71. ((processor)->runq.count > 0) || \
  72. ((thread)->policy == POLICY_TIMESHARE && \
  73. (processor)->first_quantum == FALSE && \
  74. (processor)->processor_set->runq.count > 0 && \
  75. (processor)->processor_set->runq.low <= \
  76. (thread)->sched_pri) || \
  77. ((thread)->policy == POLICY_FIXEDPRI && \
  78. (processor)->processor_set->runq.count > 0 && \
  79. ((((processor)->first_quantum == FALSE) && \
  80. ((processor)->processor_set->runq.low <= \
  81. (thread)->sched_pri)) || \
  82. ((processor)->processor_set->runq.low < \
  83. (thread)->sched_pri))))
  84. #else /* MACH_FIXPRI */
  85. #define csw_needed(thread, processor) ((thread)->state & TH_SUSP || \
  86. ((processor)->runq.count > 0) || \
  87. ((processor)->first_quantum == FALSE && \
  88. ((processor)->processor_set->runq.count > 0 && \
  89. (processor)->processor_set->runq.low <= \
  90. ((thread)->sched_pri))))
  91. #endif /* MACH_FIXPRI */
  92. /*
  93. * Scheduler routines.
  94. */
  95. extern struct run_queue *rem_runq(thread_t);
  96. extern struct thread *choose_thread(processor_t);
  97. extern queue_head_t action_queue; /* assign/shutdown queue */
  98. decl_simple_lock_data(extern,action_lock);
  99. extern int min_quantum; /* defines max context switch rate */
  100. /*
  101. * Default base priorities for threads.
  102. */
  103. #define BASEPRI_SYSTEM 6
  104. #define BASEPRI_USER 25
  105. /*
  106. * Macro to check for invalid priorities.
  107. */
  108. #define invalid_pri(pri) (((pri) < 0) || ((pri) >= NRQS))
  109. /*
  110. * Shift structures for holding update shifts. Actual computation
  111. * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
  112. * +/- is determined by the sign of shift 2.
  113. */
  114. struct shift {
  115. int shift1;
  116. int shift2;
  117. };
  118. typedef struct shift *shift_t, shift_data_t;
  119. /*
  120. * sched_tick increments once a second. Used to age priorities.
  121. */
  122. extern unsigned sched_tick;
  123. #define SCHED_SCALE 128
  124. #define SCHED_SHIFT 7
  125. /*
  126. * thread_timer_delta macro takes care of both thread timers.
  127. */
  128. #define thread_timer_delta(thread) \
  129. MACRO_BEGIN \
  130. unsigned delta; \
  131. \
  132. delta = 0; \
  133. TIMER_DELTA((thread)->system_timer, \
  134. (thread)->system_timer_save, delta); \
  135. TIMER_DELTA((thread)->user_timer, \
  136. (thread)->user_timer_save, delta); \
  137. (thread)->cpu_delta += delta; \
  138. (thread)->sched_delta += delta * \
  139. (thread)->processor_set->sched_load; \
  140. MACRO_END
  141. #if SIMPLE_CLOCK
  142. /*
  143. * sched_usec is an exponential average of number of microseconds
  144. * in a second for clock drift compensation.
  145. */
  146. extern int sched_usec;
  147. #endif /* SIMPLE_CLOCK */
  148. #endif /* _KERN_SCHED_H_ */