realtime 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. .TH REALTIME 3
  2. .EQ
  3. delim $$
  4. .EN
  5. .SH NAME
  6. realtime \- real time scheduling
  7. .SH SYNOPSIS
  8. .EX
  9. .ta 4n +11n +7
  10. .B bind -a #R /dev
  11. .ift .sp 0.5
  12. .ifn .sp
  13. /proc/\fIn\fP/ctl
  14. .EE
  15. .SH DESCRIPTION
  16. The Plan 9 real-time scheduler allows mixing real-time processes and best-effort
  17. processes on a single system. The scheduler is intended for real-time loads well
  18. under 100% CPU utilization with minimum periods in the order of a hundred thousand
  19. instruction times. The precision of the scheduler depends on the precision of the
  20. machine's programmable real-time clock.
  21. .PP
  22. Real-time processes are scheduled using Earliest-Deadline First (EDF). Each process
  23. has three primary scheduling parameters, a period $T$, a deadline $D$
  24. and a cost $C$, expressed in nanoseconds.
  25. Every $T$ nanoseconds, the process is
  26. .I released
  27. — i.e., it becomes schedulable — and it receives a
  28. .I slice
  29. of $C$ nsec.
  30. When the process is released, its
  31. .I "release time"
  32. $r$ is set to the current time.
  33. The process's
  34. .I "absolute deadline"
  35. $d$ is set to $r + D$.
  36. (Note the use of capital letters to indicate intervals and lower-case
  37. letters to indicate `points in time'.)
  38. Between release and deadline, the process must be scheduled often enough
  39. to be able to consume its slice of $C$ nsec of CPU time.
  40. So, to be schedulable, $C <= D <= T$ must hold.
  41. If $D < T$, processes are not real-time schedulable (runnable) between their
  42. deadline and the next release, but they may run as best-effort processes and compete with
  43. other processes for the CPU.
  44. Processes are scheduled according to the EDF rule
  45. from the moment of release until their deadline or their slice runs
  46. out, whichever happens first. Additionally, processes can voluntarily
  47. declare the slice for the current period empty, allowing other
  48. real-time processes or best-effort processes to use the CPU time remaining in their
  49. slice.
  50. .PP
  51. Before they are released for the first time, processes have to be
  52. .IR admitted
  53. into the system. Before admission, a test is conducted to determine
  54. whether the process, plus the already admitted processes, given their
  55. scheduling parameters, can all meet their deadlines. When a process
  56. changes its scheduling parameters, it is temporarily
  57. .I expelled
  58. and will be readmitted only if the new scheduling parameters allow all
  59. processes to continue to meet their deadlines.
  60. .PP
  61. The EDF scheduler schedules the released process with the earliest
  62. deadline. When a process is released it can, therefore, preempt a process
  63. that has a later deadline, but was released earlier.
  64. .\" .PP
  65. .\" Real-time processes sharing resources, however, may need to be prevented
  66. .\" from preempting each other. They must do this by declaring their shared
  67. .\" resources. The scheduler will not preempt one process with another that
  68. .\" shares a common resource. To do this, processes can name resources they use
  69. .\" and the scheduler will not allow preemption of a process by another that
  70. .\" has named the same resource.
  71. .\" .PP
  72. .\" Note that, if all processes share a common resource, the scheduler reduces to
  73. .\" non-preemptive EDF scheduling. Note also that resources in this context
  74. .\" are just named placeholders for the actual resources shared by the processes.
  75. .\" It is up to the programmer to define the relationship between actual
  76. .\" shared resources (data structures, for example) and the named resources
  77. .\" used by the scheduler.
  78. .\" .PP
  79. .\" During the admission test, information about resource sharing is used
  80. .\" to calculate
  81. .\" .IR "inherited deadlines" ,
  82. .\" for each process. A resource's
  83. .\" .IR "inherited deadline" ,
  84. .\" $Δ$, is the minimum of the deadlines $D$ of each of the processes
  85. .\" that shares that resource. A process's $Δ$ is the minimum of the
  86. .\" resource $Δ$s of all resources held by the process.
  87. .\" .PP
  88. .\" The scheduler allows a released process $T$ to preempt running process $T'$
  89. .\" iff $d < d' ^ D < Δ'$; the first half of the condition says that the
  90. .\" released process's deadline must be earlier, the second implies that the
  91. .\" released process may not share resources with the running one (after all,
  92. .\" if $T'$ had acquired a resource that $T$ uses, its $Δ'$ would, by the
  93. .\" definition of inherited deadlines, be less than or equal to $D$).
  94. .\" .PP
  95. .\" The admission test takes these limitations into account. Note that,
  96. .\" in practice, processes rarely share resources.
  97. .PP
  98. Normally, processes can block (when all their processes are blocked on
  99. system calls) during their release. The time spent blocking may be
  100. accounted against the current slice, because blocked processes may stop
  101. other processes from getting the processor (due to shared resources).
  102. Processes can tell the scheduler that they no longer require the CPU until the next
  103. release, by
  104. .I yielding
  105. (see below).
  106. .PP
  107. Processes can also run in another mode where blocking automatically
  108. implies immediate yielding. This mode truly guarantees that deadlines
  109. will be made, but programmers must know more about the (blocking)
  110. nature of system calls used in order to use this mode.
  111. .sp
  112. .LP
  113. The real-time scheduler is controlled through
  114. .BR /proc/\fPpid\fP/ctl .
  115. See
  116. .IR proc (3).
  117. .PP
  118. The process' parameters are set or modified by writing one of these files and they
  119. can be examined by reading it.
  120. A parameter is presented in the form
  121. \f2name\fP \f2value\fP, \f2name\fP +\f2value\fP, or \f2name\fP -\f2value\fP.
  122. A command is presented in the form
  123. .IR commandname .
  124. Parameters and commands are separated by semicolons.
  125. The settable parameters are
  126. .TP
  127. .B period
  128. Sets the period. The value must be given as a number with or without decimal point
  129. and directly followed (no white space) by one of
  130. .I s
  131. for seconds,
  132. .I ms
  133. for milliseconds,
  134. .I µs
  135. or
  136. .I us
  137. for microseconds,
  138. .I ns
  139. or
  140. nothing
  141. for nanoseconds.
  142. If the value is signed, it is taken as an increment (decrement) of the value
  143. perviously set.
  144. .TP
  145. .B deadline
  146. Sets the deadline. Value specified as above.
  147. .TP
  148. .B cost
  149. Sets the cost. Value specified as above.
  150. .TP
  151. .B yieldonblock
  152. When the (integer) value is non-zero or absent, the process is placed in
  153. .I yield-on-block
  154. mode: when the process is blocked on a system call, the process automatically
  155. declares the current deadline reached and will be released again in the next period or when the process
  156. becomes runnable again, whichever occurs last.
  157. When the value is zero, the process can block without forfeiting the current release.
  158. The default value is zero.
  159. .TP
  160. .B sporadic
  161. When the (integer) value is non-zero or absent, the process is placed in
  162. .I sporadic
  163. mode. This mode resembles
  164. .IR yield-on-block .
  165. When the process unblocks and the time elapsed since last release is more than the period, it
  166. is released immediately, rather than at the next period boundary. Sporadic scheduling is
  167. useful for processes that are interrupt driven. The period merely specifies the maximum
  168. invocation rate. The default value is zero.
  169. .TP
  170. .B admit
  171. This initiates an admission test. If the test fails, the write containing the
  172. .B admit
  173. command will fail. If the test succeeds the process is admitted and will be released.
  174. .TP
  175. .B expel
  176. Expels the process by turning it back into a regular best-effort process.
  177. .LP
  178. .BR Sleep(0)
  179. will yield (i.e., giveup the processor until the next release time). In non-real-time
  180. processes,
  181. .B sleep(0)
  182. will invoke the scheduler, allowing higher priority processes to run.
  183. .TP
  184. .SH EXAMPLE
  185. .EX
  186. .ta 4n +4n +4n +4n +4n +4n +4n
  187. char *ctl[128];
  188. void
  189. processvideo(){
  190. int fd;
  191. snprint(ctl, sizeof ctl, "/proc/%ld/ctl", getpid());
  192. if ((fd = open(ctl, ORDWR)) < 0)
  193. sysfatal("%s: %r", ctl);
  194. if (fprint(fd, "period 33ms;deadline 20ms;cost 8ms;admit") < 0)
  195. sysfatal("%s: admit: %r", ctl);
  196. close(fd);
  197. for (;;){
  198. processframe();
  199. sleep(0);
  200. }
  201. }
  202. .EE
  203. .SH "SEE ALSO
  204. .IR trace (1)
  205. .LP
  206. .SH SOURCE
  207. .B /sys/src/9/port/edf.c
  208. .SH BUGS
  209. The admission test does not take multiprocessors into account. The total
  210. real-time load cannot exceed a single-\s-2CPU\s0 load.