sched-coding.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Reference for various scheduler-related methods in the O(1) scheduler
  2. Robert Love <rml@tech9.net>, MontaVista Software
  3. Note most of these methods are local to kernel/sched.c - this is by design.
  4. The scheduler is meant to be self-contained and abstracted away. This document
  5. is primarily for understanding the scheduler, not interfacing to it. Some of
  6. the discussed interfaces, however, are general process/scheduling methods.
  7. They are typically defined in include/linux/sched.h.
  8. Main Scheduling Methods
  9. -----------------------
  10. void load_balance(runqueue_t *this_rq, int idle)
  11. Attempts to pull tasks from one cpu to another to balance cpu usage,
  12. if needed. This method is called explicitly if the runqueues are
  13. imbalanced or periodically by the timer tick. Prior to calling,
  14. the current runqueue must be locked and interrupts disabled.
  15. void schedule()
  16. The main scheduling function. Upon return, the highest priority
  17. process will be active.
  18. Locking
  19. -------
  20. Each runqueue has its own lock, rq->lock. When multiple runqueues need
  21. to be locked, lock acquires must be ordered by ascending &runqueue value.
  22. A specific runqueue is locked via
  23. task_rq_lock(task_t pid, unsigned long *flags)
  24. which disables preemption, disables interrupts, and locks the runqueue pid is
  25. running on. Likewise,
  26. task_rq_unlock(task_t pid, unsigned long *flags)
  27. unlocks the runqueue pid is running on, restores interrupts to their previous
  28. state, and reenables preemption.
  29. The routines
  30. double_rq_lock(runqueue_t *rq1, runqueue_t *rq2)
  31. and
  32. double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2)
  33. safely lock and unlock, respectively, the two specified runqueues. They do
  34. not, however, disable and restore interrupts. Users are required to do so
  35. manually before and after calls.
  36. Values
  37. ------
  38. MAX_PRIO
  39. The maximum priority of the system, stored in the task as task->prio.
  40. Lower priorities are higher. Normal (non-RT) priorities range from
  41. MAX_RT_PRIO to (MAX_PRIO - 1).
  42. MAX_RT_PRIO
  43. The maximum real-time priority of the system. Valid RT priorities
  44. range from 0 to (MAX_RT_PRIO - 1).
  45. MAX_USER_RT_PRIO
  46. The maximum real-time priority that is exported to user-space. Should
  47. always be equal to or less than MAX_RT_PRIO. Setting it less allows
  48. kernel threads to have higher priorities than any user-space task.
  49. MIN_TIMESLICE
  50. MAX_TIMESLICE
  51. Respectively, the minimum and maximum timeslices (quanta) of a process.
  52. Data
  53. ----
  54. struct runqueue
  55. The main per-CPU runqueue data structure.
  56. struct task_struct
  57. The main per-process data structure.
  58. General Methods
  59. ---------------
  60. cpu_rq(cpu)
  61. Returns the runqueue of the specified cpu.
  62. this_rq()
  63. Returns the runqueue of the current cpu.
  64. task_rq(pid)
  65. Returns the runqueue which holds the specified pid.
  66. cpu_curr(cpu)
  67. Returns the task currently running on the given cpu.
  68. rt_task(pid)
  69. Returns true if pid is real-time, false if not.
  70. Process Control Methods
  71. -----------------------
  72. void set_user_nice(task_t *p, long nice)
  73. Sets the "nice" value of task p to the given value.
  74. int setscheduler(pid_t pid, int policy, struct sched_param *param)
  75. Sets the scheduling policy and parameters for the given pid.
  76. int set_cpus_allowed(task_t *p, unsigned long new_mask)
  77. Sets a given task's CPU affinity and migrates it to a proper cpu.
  78. Callers must have a valid reference to the task and assure the
  79. task not exit prematurely. No locks can be held during the call.
  80. set_task_state(tsk, state_value)
  81. Sets the given task's state to the given value.
  82. set_current_state(state_value)
  83. Sets the current task's state to the given value.
  84. void set_tsk_need_resched(struct task_struct *tsk)
  85. Sets need_resched in the given task.
  86. void clear_tsk_need_resched(struct task_struct *tsk)
  87. Clears need_resched in the given task.
  88. void set_need_resched()
  89. Sets need_resched in the current task.
  90. void clear_need_resched()
  91. Clears need_resched in the current task.
  92. int need_resched()
  93. Returns true if need_resched is set in the current task, false
  94. otherwise.
  95. yield()
  96. Place the current process at the end of the runqueue and call schedule.