sched-rt-group.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. Real-Time group scheduling
  2. --------------------------
  3. CONTENTS
  4. ========
  5. 1. Overview
  6. 1.1 The problem
  7. 1.2 The solution
  8. 2. The interface
  9. 2.1 System-wide settings
  10. 2.2 Default behaviour
  11. 2.3 Basis for grouping tasks
  12. 3. Future plans
  13. 1. Overview
  14. ===========
  15. 1.1 The problem
  16. ---------------
  17. Realtime scheduling is all about determinism, a group has to be able to rely on
  18. the amount of bandwidth (eg. CPU time) being constant. In order to schedule
  19. multiple groups of realtime tasks, each group must be assigned a fixed portion
  20. of the CPU time available. Without a minimum guarantee a realtime group can
  21. obviously fall short. A fuzzy upper limit is of no use since it cannot be
  22. relied upon. Which leaves us with just the single fixed portion.
  23. 1.2 The solution
  24. ----------------
  25. CPU time is divided by means of specifying how much time can be spent running
  26. in a given period. We allocate this "run time" for each realtime group which
  27. the other realtime groups will not be permitted to use.
  28. Any time not allocated to a realtime group will be used to run normal priority
  29. tasks (SCHED_OTHER). Any allocated run time not used will also be picked up by
  30. SCHED_OTHER.
  31. Let's consider an example: a frame fixed realtime renderer must deliver 25
  32. frames a second, which yields a period of 0.04s per frame. Now say it will also
  33. have to play some music and respond to input, leaving it with around 80% CPU
  34. time dedicated for the graphics. We can then give this group a run time of 0.8
  35. * 0.04s = 0.032s.
  36. This way the graphics group will have a 0.04s period with a 0.032s run time
  37. limit. Now if the audio thread needs to refill the DMA buffer every 0.005s, but
  38. needs only about 3% CPU time to do so, it can do with a 0.03 * 0.005s =
  39. 0.00015s. So this group can be scheduled with a period of 0.005s and a run time
  40. of 0.00015s.
  41. The remaining CPU time will be used for user input and other tasks. Because
  42. realtime tasks have explicitly allocated the CPU time they need to perform
  43. their tasks, buffer underruns in the graphics or audio can be eliminated.
  44. NOTE: the above example is not fully implemented as of yet (2.6.25). We still
  45. lack an EDF scheduler to make non-uniform periods usable.
  46. 2. The Interface
  47. ================
  48. 2.1 System wide settings
  49. ------------------------
  50. The system wide settings are configured under the /proc virtual file system:
  51. /proc/sys/kernel/sched_rt_period_us:
  52. The scheduling period that is equivalent to 100% CPU bandwidth
  53. /proc/sys/kernel/sched_rt_runtime_us:
  54. A global limit on how much time realtime scheduling may use. Even without
  55. CONFIG_RT_GROUP_SCHED enabled, this will limit time reserved to realtime
  56. processes. With CONFIG_RT_GROUP_SCHED it signifies the total bandwidth
  57. available to all realtime groups.
  58. * Time is specified in us because the interface is s32. This gives an
  59. operating range from 1us to about 35 minutes.
  60. * sched_rt_period_us takes values from 1 to INT_MAX.
  61. * sched_rt_runtime_us takes values from -1 to (INT_MAX - 1).
  62. * A run time of -1 specifies runtime == period, ie. no limit.
  63. 2.2 Default behaviour
  64. ---------------------
  65. The default values for sched_rt_period_us (1000000 or 1s) and
  66. sched_rt_runtime_us (950000 or 0.95s). This gives 0.05s to be used by
  67. SCHED_OTHER (non-RT tasks). These defaults were chosen so that a run-away
  68. realtime tasks will not lock up the machine but leave a little time to recover
  69. it. By setting runtime to -1 you'd get the old behaviour back.
  70. By default all bandwidth is assigned to the root group and new groups get the
  71. period from /proc/sys/kernel/sched_rt_period_us and a run time of 0. If you
  72. want to assign bandwidth to another group, reduce the root group's bandwidth
  73. and assign some or all of the difference to another group.
  74. Realtime group scheduling means you have to assign a portion of total CPU
  75. bandwidth to the group before it will accept realtime tasks. Therefore you will
  76. not be able to run realtime tasks as any user other than root until you have
  77. done that, even if the user has the rights to run processes with realtime
  78. priority!
  79. 2.3 Basis for grouping tasks
  80. ----------------------------
  81. There are two compile-time settings for allocating CPU bandwidth. These are
  82. configured using the "Basis for grouping tasks" multiple choice menu under
  83. General setup > Group CPU Scheduler:
  84. a. CONFIG_USER_SCHED (aka "Basis for grouping tasks" = "user id")
  85. This lets you use the virtual files under
  86. "/sys/kernel/uids/<uid>/cpu_rt_runtime_us" to control he CPU time reserved for
  87. each user .
  88. The other option is:
  89. .o CONFIG_CGROUP_SCHED (aka "Basis for grouping tasks" = "Control groups")
  90. This uses the /cgroup virtual file system and "/cgroup/<cgroup>/cpu.rt_runtime_us"
  91. to control the CPU time reserved for each control group instead.
  92. For more information on working with control groups, you should read
  93. Documentation/cgroups/cgroups.txt as well.
  94. Group settings are checked against the following limits in order to keep the configuration
  95. schedulable:
  96. \Sum_{i} runtime_{i} / global_period <= global_runtime / global_period
  97. For now, this can be simplified to just the following (but see Future plans):
  98. \Sum_{i} runtime_{i} <= global_runtime
  99. 3. Future plans
  100. ===============
  101. There is work in progress to make the scheduling period for each group
  102. ("/sys/kernel/uids/<uid>/cpu_rt_period_us" or
  103. "/cgroup/<cgroup>/cpu.rt_period_us" respectively) configurable as well.
  104. The constraint on the period is that a subgroup must have a smaller or
  105. equal period to its parent. But realistically its not very useful _yet_
  106. as its prone to starvation without deadline scheduling.
  107. Consider two sibling groups A and B; both have 50% bandwidth, but A's
  108. period is twice the length of B's.
  109. * group A: period=100000us, runtime=10000us
  110. - this runs for 0.01s once every 0.1s
  111. * group B: period= 50000us, runtime=10000us
  112. - this runs for 0.01s twice every 0.1s (or once every 0.05 sec).
  113. This means that currently a while (1) loop in A will run for the full period of
  114. B and can starve B's tasks (assuming they are of lower priority) for a whole
  115. period.
  116. The next project will be SCHED_EDF (Earliest Deadline First scheduling) to bring
  117. full deadline scheduling to the linux kernel. Deadline scheduling the above
  118. groups and treating end of the period as a deadline will ensure that they both
  119. get their allocated time.
  120. Implementing SCHED_EDF might take a while to complete. Priority Inheritance is
  121. the biggest challenge as the current linux PI infrastructure is geared towards
  122. the limited static priority levels 0-139. With deadline scheduling you need to
  123. do deadline inheritance (since priority is inversely proportional to the
  124. deadline delta (deadline - now).
  125. This means the whole PI machinery will have to be reworked - and that is one of
  126. the most complex pieces of code we have.