realtime 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 receives 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 must do this by declaring their shared
  113. resources. 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
  128. .IR "inherited deadlines" ,
  129. for each task. A resource's
  130. .IR "inherited deadline" ,
  131. $Δ$, is the minimum of the deadlines $D$ of each of the tasks
  132. that shares that resource. A task's $Δ$ is the minimum of the
  133. resource $Δ$s of all resources held by the task.
  134. .PP
  135. Acquiring a resource may lower the $Δ$ of the task; releasing one
  136. may increase it.
  137. .PP
  138. The scheduler allows a released task $T$ to preempt running task $T'$
  139. iff $d < d' ^ D < Δ'$; the first half of the condition says that the
  140. released task's deadline must be earlier, the second implies that the
  141. released task may not share resources with the running one (after all,
  142. if $T'$ had acquired a resource that $T$ uses, its $Δ'$ would, by the
  143. definition of inherited deadlines, be less than or equal to $D$).
  144. .PP
  145. The admission test takes these limitations into account. Note that,
  146. in practice, tasks rarely share resources.
  147. .PP
  148. Normally, tasks can block (when all their processes are blocked on
  149. system calls) during their release. The time spent blocking is not
  150. accounted against the current slice, but, since the scheduler has no
  151. control over the time spent blocking, there are no guarantees that the
  152. deadline will be made if a task blocks during its release. Whether or
  153. not tasks actually block, they will normally complete the work to be
  154. done in a period (slightly) before the slice runs out — and before
  155. the deadline. To tell the scheduler that they no longer require the
  156. CPU until the next release, tasks can
  157. .I yield
  158. (see below).
  159. .PP
  160. Tasks can also run in another mode where blocking automatically
  161. implies immediate yielding. This mode truly guarantees that deadlines
  162. will be made, but programmers must know more about the (blocking)
  163. nature of the system calls used in order to use this mode.
  164. .sp
  165. .LP
  166. The real-time scheduler is controlled through the
  167. .B #R
  168. file system, normally bound to
  169. .BR /dev .
  170. Opening
  171. .B /dev/realtime/clone
  172. for writing (and reading, if desired), causes a new task to be created in the
  173. non-admitted (expelled) state. The new task will be represented by a file in
  174. the
  175. .B /dev/realtime/task
  176. directory whose name $n$ is the task's number.
  177. The file descriptor resulting from opening
  178. .B /dev/realtime/clone
  179. provides the equivalent functionality as that resulting from opening
  180. .BI /dev/realtime/task/ n
  181. except for the initial creation of a new task when opening the
  182. .B clone
  183. file. The task file may also be opened read-only.
  184. .PP
  185. The task's parameters are set or modified by writing one of these files and they
  186. can be examined by reading it.
  187. A parameter is presented in the form
  188. \f2name\fP=\f2value\fP, \f2name\fP+=\f2value\fP, or \f2name\fP-=\f2value\fP.
  189. A command is presented in the form
  190. .IR commandname .
  191. Parameters and commands are separated by white space; quoting conventions apply
  192. (see
  193. .IR quote (2)).
  194. The settable parameters are
  195. .TP
  196. .B T
  197. Sets the period. The value must be given as a number with or without decimal point
  198. and directly followed (no white space) by one of
  199. .I s
  200. for seconds,
  201. .I ms
  202. for milliseconds,
  203. .I µs
  204. or
  205. .I us
  206. for microseconds,
  207. .I ns
  208. or
  209. nothing
  210. for nanoseconds. The
  211. .B +=
  212. operator adds to the period already set, the
  213. .B -=
  214. operator subtracts from it.
  215. .TP
  216. .B D
  217. Sets the deadline. Value syntax and operators as above.
  218. .TP
  219. .B C
  220. Sets the cost. Value syntax and operators as above.
  221. .TP
  222. .B resources
  223. Sets the list of resources; resource names must consist of letters, digits, or underscores
  224. and they must not start with a digit. A resource name may be followed by its cost
  225. (using the same notation of time as in setting the $T$ attribute) and by resources
  226. nested within. These nested resources are indicated by an open brace '{', a list of
  227. nested resources and a closing brace '}'. Quoting is usually necessary.
  228. An example of a resource specification is
  229. .ti +2m
  230. .B "resources='a 10ms { b 2ms c 3ms {d} }'
  231. .br
  232. Here, resource $a$ is held for at most 10 ms; during this time, resource $b$ is
  233. acquired and then released within 2 ms. After releasing $b$, resource $c$ can
  234. be acquired and then resource $d$. No time is specified for $d$, so its cost
  235. is inherited from its parent: 3 ms. Resources $d$, $c$ and $a$ must be released
  236. in that order.
  237. .br
  238. .ti +1m
  239. The
  240. .B +=
  241. and
  242. .B -=
  243. operators are not allowed for resources (they must be specified completely every time).
  244. .TP
  245. .B acquire
  246. is a run time command which tells the scheduler that the named resource is being acquired.
  247. The scheduler will adjust the task's $Δ$ accordingly. It is illegal to acquire a resource
  248. not specified or not to follow the nesting order. It is legal, however, not to acquire a resource
  249. (and all the ones nested inside) that may legally be acquired.
  250. The
  251. .B +=
  252. and
  253. .B -=
  254. operators do not apply.
  255. .TP
  256. .B procs
  257. Sets, adds to, or subtracts from the process membership of a task. Processes
  258. are identified by their
  259. .I pid
  260. or the special value
  261. .B self
  262. identifying the process writing the
  263. .I clone
  264. or
  265. .I task
  266. file.
  267. .TP
  268. .B yieldonblock
  269. When the (integer) value is non-zero, the task is placed in
  270. .I yield-on-block
  271. mode: when all processes in the task are blocked on system calls, the task automatically
  272. declares the current deadline reached and will be released again in the next period.
  273. When the value is zero, the task can block without forfeiting the current release.
  274. However, when the task blocks long, the deadline may be missed. The default value is zero.
  275. .LP
  276. In addition to these settable attributes, there are a number of attributes
  277. to be read:
  278. .TP
  279. .B n
  280. Number of releases of this task,
  281. .TP
  282. .B m
  283. Number of deadlines missed,
  284. .TP
  285. .B p
  286. Number of times the task was preempted by one with an earlier deadline,
  287. .TP
  288. .B t
  289. Total time used (divide this by
  290. .I n
  291. to get the average time used per period),
  292. .TP
  293. .B c
  294. Aged average time used,
  295. .LP
  296. The commands accepted are
  297. .TP
  298. .B verbose
  299. This is for debugging and causes the progression of the admission test to be displayed.
  300. .TP
  301. .B admit
  302. This initiates an admission test. If the test fails, the write containing the
  303. .B admit
  304. command will fail. If the test succeeds the task is admitted and, if it contains
  305. runnable processes, it will be released (almost) immediately.
  306. .TP
  307. .B expel
  308. Expel the task.
  309. .TP
  310. .B remove
  311. Remove the task, the file descriptor will become invalid.
  312. .TP
  313. .B release
  314. Release the last resource acquired. There is no attribute.
  315. .TP
  316. .B yield
  317. Gives up the processor until the next release.
  318. .LP
  319. .sp
  320. .B /dev/realtime/debug
  321. prints debugging information about the task queues maintained by the scheduler.
  322. This file will go away.
  323. .PP
  324. .B /dev/realtime/log
  325. and
  326. .B /dev/realtime/nblog
  327. are files used by real-time monitoring processes such as
  328. .B rtstats
  329. (see
  330. .IR rtstats (1)).
  331. Reading them produces a stream of
  332. .B Schedevent
  333. structures in the machine representation. These events are
  334. nearly ordered by Time (nanoseconds since boot); some events can
  335. be reported earlier or later than they occur.
  336. .I Tid
  337. corresponds to the file name in the directory
  338. .BR /dev/realtime/task ,
  339. .I etype
  340. is one of the events of
  341. .I SEvent
  342. as explained in
  343. .BR /sys/src/9/port/devrealtime.h ,
  344. and
  345. .I ts
  346. is the time at which the event occurred (or will occur).
  347. .B Nblog
  348. is a non-blocking version that returns zero bytes each time there is
  349. nothing left to read.
  350. .PP
  351. .B /dev/realtime/resources
  352. is a read-only file listing the resources declared by the current set of tasks.
  353. .PP
  354. .B /dev/realtime/time
  355. returns the number of nanoseconds since boot, the number of ticks since boot and
  356. .IR fasthz ,
  357. the number of clock ticks per second, a billion times the ratio between the other two numbers.
  358. Each number is returned as a binary
  359. .I vlong
  360. in architecture-dependent representation.
  361. .SH EXAMPLE
  362. .EX
  363. .ta 4n +4n +4n +4n +4n +4n +4n
  364. char *clonedev = "#R/realtime/clone";
  365. void
  366. processvideo(){
  367. int fd;
  368. if ((fd = open(clonedev, ORDWR)) < 0)
  369. sysfatal("%s: %r", clonedev);
  370. if (fprint(fd, "T=33ms D=20ms C=8ms procs=self admit") < 0)
  371. sysfatal("%s: admit: %r", clonedev);
  372. for (;;){
  373. processframe();
  374. if (fprint(fd, "yield") < 0)
  375. sysfatal("%s: yield: %r", clonedev);
  376. }
  377. if (fprint(fd, "remove") < 0)
  378. sysfatal("%s: remove: %r", clonedev);
  379. close(fd);
  380. }
  381. .EE
  382. .SH "SEE ALSO
  383. .IR rtstats (1)
  384. .SH FILES
  385. .B /sys/src/cmd/rtstats/edfproc.c
  386. as an example of using the scheduler for a trivial test run.
  387. .B /sys/src/cmd/rtstats/resproc.c
  388. as an example of using resources.
  389. .SH SOURCE
  390. .B /sys/src/9/port/devrealtime.c
  391. .br
  392. .B /sys/src/9/port/edf.c
  393. .SH BUGS
  394. The admission test does not take multiprocessors into account. The total
  395. real-time load cannot exceed a single-\s-2CPU\s0 load.