realtime 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. /dev/realtime/clone
  14. /dev/realtime/debug
  15. /dev/realtime/log
  16. /dev/realtime/nblog
  17. /dev/realtime/resources
  18. /dev/realtime/task/0
  19. /dev/realtime/task/1
  20. /dev/realtime/task/...
  21. /dev/realtime/time
  22. .sp
  23. #include "realtime.h"
  24. .ift .sp 0.5
  25. .ifn .sp
  26. enum SEvent {
  27. SAdmit, /* new proc arrives*/
  28. SRelease, /* released, but not yet scheduled */
  29. SRun, /* one of this task's procs started running */
  30. SPreempt, /* the running task was preempted */
  31. SBlock, /* the last of the runnable procs in a task blocked */
  32. SResume, /* task was scheduled after blocking */
  33. SDeadline, /* proc's deadline (reported ahead of time) */
  34. SYield, /* proc reached voluntary early deadline */
  35. SSlice, /* slice exhausted */
  36. SExpel, /* proc was expelled */
  37. SResack, /* acquire resource */
  38. SResrel, /* release resource */
  39. };
  40. typedef enum SEvent SEvent;
  41. typedef vlong Time;
  42. typedef struct Schedevent Schedevent;
  43. .ift .sp 0.5
  44. .ifn .sp
  45. struct Schedevent {
  46. ushort tid; /* Task ID */
  47. SEvent etype; /* Event type */
  48. Time ts; /* Event time */
  49. };
  50. .EE
  51. .SH DESCRIPTION
  52. The Plan 9 real-time scheduler allows mixing real-time processes and best-effort
  53. processes on a single system. The scheduler is intended for real-time loads well
  54. under 100% CPU utilization with minimum periods in the order of a hundred thousand
  55. instruction times. The precision of the scheduler depends on the precision of the
  56. machine's programmable real-time clock.
  57. .PP
  58. The unit of real-time scheduling is a
  59. .BR task .
  60. A task can contain zero or more processes. The processes in a task
  61. share a common period, deadline and CPU allocation. Tasks allow
  62. cooperating processes to collaborate to achieve a common deadline, for
  63. instance, processes receiving, decompressing and displaying video in a
  64. pipeline can share a single task.
  65. .PP
  66. Tasks are scheduled using Earliest-Deadline First (EDF). Each task
  67. has three primary scheduling parameters, a period $T$, a deadline $D$
  68. and a cost $C$, expressed in nanoseconds (but converted internally to
  69. a machine- and architecture-dependent unit called
  70. .IR tick ).
  71. Every $T$ nanoseconds, the task is
  72. .I released
  73. — i.e., it becomes schedulable — and it received a
  74. .I slice
  75. of $C$ nsec.
  76. When the task is released, its
  77. .I "release time"
  78. $r$ is set to the current time.
  79. The task's
  80. .I "absolute deadline"
  81. $d$ is set to $r + D$.
  82. (Note the use of capital letters to indicate intervals and lower-case
  83. letters to indicate `points in time'.)
  84. Between release and deadline the task must be scheduled often enough
  85. to be able to consume its slice of $C$ nsec of CPU time.
  86. So, to be schedulable, $C <= D <= T$ must hold.
  87. If $D < T$, tasks are not schedulable (runnable) between their
  88. deadline and the next release.
  89. Tasks are
  90. .I released
  91. from the moment of release until their deadline or their slice runs
  92. out, whichever happens first. Additionally, tasks can voluntarily
  93. declare the slice for the current period empty, allowing other
  94. real-time tasks or best-effort tasks the CPU time remaining in their
  95. slice.
  96. .PP
  97. Before they are released for the first time, tasks have to be
  98. .IR admitted
  99. into the system. Before admission, a test is conducted to determine
  100. whether the task, plus the already admitted tasks can all meet their
  101. deadlines, given their scheduling parameters. When a task changes its
  102. scheduling parameters, it is temporarily
  103. .I expelled
  104. and will be readmitted if the new scheduling parameters allow all
  105. tasks to continue to meet their deadlines.
  106. .PP
  107. The EDF scheduler schedules the released task with the earliest
  108. deadline. When a task is released it can, therefore, preempt a task
  109. that has a later deadline, but was released earlier.
  110. .PP
  111. Real-time tasks sharing resources, however, may need to be prevented
  112. from preempting each other. They can do this by declaring a shared
  113. resource. The scheduler will not preempt one task with another that
  114. shares a common resource. To do this, the scheduler needs to know the
  115. names of the sharable resources used by each of the tasks, the amount
  116. of time the resources are used, and the way in which resource usage is
  117. nested.
  118. .PP
  119. Resource usage must be strictly nested; i.e., it is legal for a task to
  120. first to acquire resource $A$, then to acquire resource $B$, to
  121. release $B$ again and finally to release $A$. It is also legal for it
  122. to acquire and release $A$ before acquiring and releasing $B$. But
  123. it is not legal to acquire $A$, acquire $B$, release $A$ and release $B$
  124. in that order.
  125. .PP
  126. During the admission test, information about resource sharing is used
  127. to calculate secondary deadlines for each task, called
  128. .IR "inherited deadlines" ,
  129. $Δ$, which is the minimum of the deadlines $D$ of each of the tasks
  130. that shares a resource currently held by the current task.
  131. .PP
  132. Acquiring a resource will lower the $Δ$ of the task (or keep it the
  133. same); releasing one will increase the $Δ$ (or keep it the same).
  134. .PP
  135. The scheduler allows a released task $T$ to preempt running task $T'$
  136. iff $d < d' ^ D < Δ'$; the first half of the condition says that the
  137. released task's deadline must be earlier, the second implies that the
  138. released task may not share resources with the running one (after all,
  139. if $T'$ had acquired a resource that $T$ uses, its $Δ'$ would, by the
  140. definition of inherited deadlines, be less than or equal to $D$).
  141. .PP
  142. The admission test takes these limitations into account. Note that,
  143. in practice, tasks rarely share resources.
  144. .PP
  145. Normally, tasks can block (when all their processes are blocked on
  146. system calls) during their release. The time spent blocking is not
  147. accounted against the current slice, but, since the scheduler has no
  148. control over the time spent blocking, there are no guarantees that the
  149. deadline will be made if a task blocks during its release. Whether or
  150. not tasks actually block, they will normally complete the work to be
  151. done in a period (slightly) before the slice runs out — and before
  152. the deadline. To tell the scheduler that they no longer require the
  153. CPU until the next release, tasks can
  154. .I yield
  155. (see below).
  156. .PP
  157. Tasks can also run in another mode where blocking automatically
  158. implies immediate yielding. This mode truly guarantees that deadlines
  159. will be made, but programmers must know more about the (blocking)
  160. nature of the system calls used in order to use this mode.
  161. .sp
  162. .LP
  163. The real-time scheduler is controlled through the
  164. .B #R
  165. file system, normally bound to
  166. .BR /dev .
  167. Opening
  168. .B /dev/realtime/clone
  169. for writing (and reading, if desired), causes a new task to be created in the
  170. non-admitted (expelled) state. The new task will be represented by a file in
  171. the
  172. .B /dev/realtime/task
  173. directory whose name $n$ is the task's number.
  174. The file descriptor resulting from opening
  175. .B /dev/realtime/clone
  176. provides the equivalent functionality as that resulting from opening
  177. .BI /dev/realtime/task/ n
  178. except for the initial creation of a new task when opening the
  179. .B clone
  180. file. The task file may also be opened read-only.
  181. .PP
  182. The task's parameters are set or modified by writing one of these files and they
  183. can be examined by reading it.
  184. A parameter is presented in the form
  185. \f2name\fP=\f2value\fP, \f2name\fP+=\f2value\fP, or \f2name\fP-=\f2value\fP.
  186. A command is presented in the form
  187. .IR commandname .
  188. Parameters and commands are separated by white space; quoting conventions apply
  189. (see
  190. .IR quote (2)).
  191. The settable parameters are
  192. .TP
  193. .B T
  194. Sets the period. The value must be given as a number with or without decimal point
  195. and directly followed (no white space) by one of
  196. .I s
  197. for seconds,
  198. .I ms
  199. for milliseconds,
  200. .I µs
  201. or
  202. .I us
  203. for microseconds,
  204. .I ns
  205. or
  206. nothing
  207. for nanoseconds. The
  208. .B +=
  209. operator adds to the period already set, the
  210. .B -=
  211. operator subtracts from it.
  212. .TP
  213. .B D
  214. Sets the deadline. Value syntax and operators as above.
  215. .TP
  216. .B C
  217. Sets the cost. Value syntax and operators as above.
  218. .TP
  219. .B resources
  220. Sets the list of resources; resource names must consist of letters, digits, or underscores
  221. and they must not start with a digit. A resource name may be followed by its cost
  222. (using the same notation of time as in setting the $T$ attribute) and by resources
  223. nested within. These nested resources are indicated by an open brace '{', a list of
  224. nested resources and a closing brace '}'. Quoting is usually necessary.
  225. An example of a resource specification is
  226. .ti +2m
  227. .B "resources='a 10ms { b 2ms c 3ms {d} }'
  228. .br
  229. Here, resource $a$ is held for at most 10 ms; during this time, resource $b$ is
  230. acquired and then released within 2 ms. After releasing $b$, resource $c$ can
  231. be acquired and then resource $d$. No time is specified for $d$, so its cost
  232. is inherited from its parent: 3 ms. Resources $d$, $c$ and $a$ must be released
  233. in that order.
  234. .br
  235. .ti +1m
  236. The
  237. .B +=
  238. and
  239. .B -=
  240. operators are not allowed for resources (they must be specified completely every time).
  241. .TP
  242. .B acquire
  243. is a run time command which tells the scheduler that the named resource is being acquired.
  244. The scheduler will adjust the task's $Δ$ accordingly. It is illegal to acquire a resource
  245. not specified or not to follow the nesting order. It is legal, however, not to acquire a resource
  246. (and all the ones nested inside) that may legally be acquired.
  247. The
  248. .B +=
  249. and
  250. .B -=
  251. operators do not apply.
  252. .TP
  253. .B procs
  254. Sets, adds to, or subtracts from the process membership of a task. Processes
  255. are identified by their
  256. .I pid
  257. or the special value
  258. .B self
  259. identifying the process writing the
  260. .I clone
  261. or
  262. .I task
  263. file.
  264. .TP
  265. .B yieldonblock
  266. When the (integer) value is non-zero, the task is placed in
  267. .I yield-on-block
  268. mode: when all processes in the task are blocked on system calls, the task automatically
  269. declares the current deadline reached and will be released again in the next period.
  270. When the value is zero, the task can block without forfeiting the current release.
  271. However, when the task blocks long, the deadline may be missed. The default value is zero.
  272. .LP
  273. In addition to these settable attributes, there are a number of attributes
  274. to be read:
  275. .TP
  276. .B n
  277. Number of releases of this task,
  278. .TP
  279. .B m
  280. Number of deadlines missed,
  281. .TP
  282. .B p
  283. Number of times the task was preempted by one with an earlier deadline,
  284. .TP
  285. .B t
  286. Total time used (divide this by
  287. .I n
  288. to get the average time used per period),
  289. .TP
  290. .B c
  291. Aged average time used,
  292. .LP
  293. The commands accepted are
  294. .TP
  295. .B verbose
  296. This is for debugging and causes the progression of the admission test to be displayed.
  297. .TP
  298. .B admit
  299. This initiates an admission test. If the test fails, the write containing the
  300. .B admit
  301. command will fail. If the test succeeds the task is admitted and, if it contains
  302. runnable processes, it will be released (almost) immediately.
  303. .TP
  304. .B expel
  305. Expel the task.
  306. .TP
  307. .B remove
  308. Remove the task, the file descriptor will become invalid.
  309. .TP
  310. .B release
  311. Release the last resource acquired. There is no attribute.
  312. .TP
  313. .B yield
  314. Gives up the processor until the next release.
  315. .LP
  316. .sp
  317. .B /dev/realtime/debug
  318. prints debugging information about the task queues maintained by the scheduler.
  319. This file will go away.
  320. .PP
  321. .B /dev/realtime/log
  322. and
  323. .B /dev/realtime/nblog
  324. are files used by real-time monitoring processes such as
  325. .B rtstats
  326. (see
  327. .IR rtstats (1)).
  328. Reading them produces a stream of
  329. .B Schedevent
  330. structures in the machine representation. These events are
  331. nearly ordered by Time (nanoseconds since boot); some events can
  332. be reported earlier or later than they occur.
  333. .I Tid
  334. corresponds to the file name in the directory
  335. .BR /dev/realtime/task ,
  336. .I etype
  337. is one of the events of
  338. .I SEvent
  339. as explained in
  340. .BR /sys/src/9/port/devrealtime.h ,
  341. and
  342. .I ts
  343. is the time at which the event occurred (or will occur).
  344. .B Nblog
  345. is a non-blocking version that returns zero bytes each time there is
  346. nothing left to read.
  347. .PP
  348. .B /dev/realtime/resources
  349. is a read-only file listing the resources declared by the current set of tasks.
  350. .PP
  351. .B /dev/realtime/time
  352. returns the number of nanoseconds since boot, the number of ticks since boot and
  353. .IR fasthz ,
  354. the number of clock ticks per second, a billion times the ratio between the other two numbers.
  355. Each number is returned as a binary
  356. .I vlong
  357. in architecture-dependent representation.
  358. .SH EXAMPLE
  359. .EX
  360. .ta 4n +4n +4n +4n +4n +4n +4n
  361. char *clonedev = "#R/realtime/clone";
  362. void
  363. processvideo(){
  364. int fd;
  365. if ((fd = open(clonedev, ORDWR)) < 0)
  366. sysfatal("%s: %r", clonedev);
  367. if (fprint(fd, "T=33ms D=20ms C=8ms procs=self admit") < 0)
  368. sysfatal("%s: admit: %r", clonedev);
  369. for (;;){
  370. processframe();
  371. if (fprint(fd, "yield") < 0)
  372. sysfatal("%s: yield: %r", clonedev);
  373. }
  374. if (fprint(fd, "remove") < 0)
  375. sysfatal("%s: remove: %r", clonedev);
  376. close(fd);
  377. }
  378. .EE
  379. .SH "SEE ALSO
  380. .IR rtstats (1)
  381. .SH FILES
  382. .B /sys/src/cmd/rtstats/edfproc.c
  383. as an example of using the scheduler for a trivial test run.
  384. .B /sys/src/cmd/rtstats/resproc.c
  385. as an example of using resources.
  386. .SH SOURCE
  387. .B /sys/src/9/port/devrealtime.c
  388. .br
  389. .B /sys/src/9/port/edf.c
  390. .SH BUGS
  391. The admission test does not take multiprocessors into account. The total
  392. real-time load cannot exceed a single-\s-2CPU\s0 load.