slow-work.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ====================================
  2. SLOW WORK ITEM EXECUTION THREAD POOL
  3. ====================================
  4. By: David Howells <dhowells@redhat.com>
  5. The slow work item execution thread pool is a pool of threads for performing
  6. things that take a relatively long time, such as making mkdir calls.
  7. Typically, when processing something, these items will spend a lot of time
  8. blocking a thread on I/O, thus making that thread unavailable for doing other
  9. work.
  10. The standard workqueue model is unsuitable for this class of work item as that
  11. limits the owner to a single thread or a single thread per CPU. For some
  12. tasks, however, more threads - or fewer - are required.
  13. There is just one pool per system. It contains no threads unless something
  14. wants to use it - and that something must register its interest first. When
  15. the pool is active, the number of threads it contains is dynamic, varying
  16. between a maximum and minimum setting, depending on the load.
  17. ====================
  18. CLASSES OF WORK ITEM
  19. ====================
  20. This pool support two classes of work items:
  21. (*) Slow work items.
  22. (*) Very slow work items.
  23. The former are expected to finish much quicker than the latter.
  24. An operation of the very slow class may do a batch combination of several
  25. lookups, mkdirs, and a create for instance.
  26. An operation of the ordinarily slow class may, for example, write stuff or
  27. expand files, provided the time taken to do so isn't too long.
  28. Operations of both types may sleep during execution, thus tying up the thread
  29. loaned to it.
  30. A further class of work item is available, based on the slow work item class:
  31. (*) Delayed slow work items.
  32. These are slow work items that have a timer to defer queueing of the item for
  33. a while.
  34. THREAD-TO-CLASS ALLOCATION
  35. --------------------------
  36. Not all the threads in the pool are available to work on very slow work items.
  37. The number will be between one and one fewer than the number of active threads.
  38. This is configurable (see the "Pool Configuration" section).
  39. All the threads are available to work on ordinarily slow work items, but a
  40. percentage of the threads will prefer to work on very slow work items.
  41. The configuration ensures that at least one thread will be available to work on
  42. very slow work items, and at least one thread will be available that won't work
  43. on very slow work items at all.
  44. =====================
  45. USING SLOW WORK ITEMS
  46. =====================
  47. Firstly, a module or subsystem wanting to make use of slow work items must
  48. register its interest:
  49. int ret = slow_work_register_user(struct module *module);
  50. This will return 0 if successful, or a -ve error upon failure. The module
  51. pointer should be the module interested in using this facility (almost
  52. certainly THIS_MODULE).
  53. Slow work items may then be set up by:
  54. (1) Declaring a slow_work struct type variable:
  55. #include <linux/slow-work.h>
  56. struct slow_work myitem;
  57. (2) Declaring the operations to be used for this item:
  58. struct slow_work_ops myitem_ops = {
  59. .get_ref = myitem_get_ref,
  60. .put_ref = myitem_put_ref,
  61. .execute = myitem_execute,
  62. };
  63. [*] For a description of the ops, see section "Item Operations".
  64. (3) Initialising the item:
  65. slow_work_init(&myitem, &myitem_ops);
  66. or:
  67. delayed_slow_work_init(&myitem, &myitem_ops);
  68. or:
  69. vslow_work_init(&myitem, &myitem_ops);
  70. depending on its class.
  71. A suitably set up work item can then be enqueued for processing:
  72. int ret = slow_work_enqueue(&myitem);
  73. This will return a -ve error if the thread pool is unable to gain a reference
  74. on the item, 0 otherwise, or (for delayed work):
  75. int ret = delayed_slow_work_enqueue(&myitem, my_jiffy_delay);
  76. The items are reference counted, so there ought to be no need for a flush
  77. operation. But as the reference counting is optional, means to cancel
  78. existing work items are also included:
  79. cancel_slow_work(&myitem);
  80. cancel_delayed_slow_work(&myitem);
  81. can be used to cancel pending work. The above cancel function waits for
  82. existing work to have been executed (or prevent execution of them, depending
  83. on timing).
  84. When all a module's slow work items have been processed, and the
  85. module has no further interest in the facility, it should unregister its
  86. interest:
  87. slow_work_unregister_user(struct module *module);
  88. The module pointer is used to wait for all outstanding work items for that
  89. module before completing the unregistration. This prevents the put_ref() code
  90. from being taken away before it completes. module should almost certainly be
  91. THIS_MODULE.
  92. ================
  93. HELPER FUNCTIONS
  94. ================
  95. The slow-work facility provides a function by which it can be determined
  96. whether or not an item is queued for later execution:
  97. bool queued = slow_work_is_queued(struct slow_work *work);
  98. If it returns false, then the item is not on the queue (it may be executing
  99. with a requeue pending). This can be used to work out whether an item on which
  100. another depends is on the queue, thus allowing a dependent item to be queued
  101. after it.
  102. ===============
  103. ITEM OPERATIONS
  104. ===============
  105. Each work item requires a table of operations of type struct slow_work_ops.
  106. Only ->execute() is required; the getting and putting of a reference and the
  107. describing of an item are all optional.
  108. (*) Get a reference on an item:
  109. int (*get_ref)(struct slow_work *work);
  110. This allows the thread pool to attempt to pin an item by getting a
  111. reference on it. This function should return 0 if the reference was
  112. granted, or a -ve error otherwise. If an error is returned,
  113. slow_work_enqueue() will fail.
  114. The reference is held whilst the item is queued and whilst it is being
  115. executed. The item may then be requeued with the same reference held, or
  116. the reference will be released.
  117. (*) Release a reference on an item:
  118. void (*put_ref)(struct slow_work *work);
  119. This allows the thread pool to unpin an item by releasing the reference on
  120. it. The thread pool will not touch the item again once this has been
  121. called.
  122. (*) Execute an item:
  123. void (*execute)(struct slow_work *work);
  124. This should perform the work required of the item. It may sleep, it may
  125. perform disk I/O and it may wait for locks.
  126. (*) View an item through /proc:
  127. void (*desc)(struct slow_work *work, struct seq_file *m);
  128. If supplied, this should print to 'm' a small string describing the work
  129. the item is to do. This should be no more than about 40 characters, and
  130. shouldn't include a newline character.
  131. See the 'Viewing executing and queued items' section below.
  132. ==================
  133. POOL CONFIGURATION
  134. ==================
  135. The slow-work thread pool has a number of configurables:
  136. (*) /proc/sys/kernel/slow-work/min-threads
  137. The minimum number of threads that should be in the pool whilst it is in
  138. use. This may be anywhere between 2 and max-threads.
  139. (*) /proc/sys/kernel/slow-work/max-threads
  140. The maximum number of threads that should in the pool. This may be
  141. anywhere between min-threads and 255 or NR_CPUS * 2, whichever is greater.
  142. (*) /proc/sys/kernel/slow-work/vslow-percentage
  143. The percentage of active threads in the pool that may be used to execute
  144. very slow work items. This may be between 1 and 99. The resultant number
  145. is bounded to between 1 and one fewer than the number of active threads.
  146. This ensures there is always at least one thread that can process very
  147. slow work items, and always at least one thread that won't.
  148. ==================================
  149. VIEWING EXECUTING AND QUEUED ITEMS
  150. ==================================
  151. If CONFIG_SLOW_WORK_PROC is enabled, a proc file is made available:
  152. /proc/slow_work_rq
  153. through which the list of work items being executed and the queues of items to
  154. be executed may be viewed. The owner of a work item is given the chance to
  155. add some information of its own.
  156. The contents look something like the following:
  157. THR PID ITEM ADDR FL MARK DESC
  158. === ===== ================ == ===== ==========
  159. 0 3005 ffff880023f52348 a 952ms FSC: OBJ17d3: LOOK
  160. 1 3006 ffff880024e33668 2 160ms FSC: OBJ17e5 OP60d3b: Write1/Store fl=2
  161. 2 3165 ffff8800296dd180 a 424ms FSC: OBJ17e4: LOOK
  162. 3 4089 ffff8800262c8d78 a 212ms FSC: OBJ17ea: CRTN
  163. 4 4090 ffff88002792bed8 2 388ms FSC: OBJ17e8 OP60d36: Write1/Store fl=2
  164. 5 4092 ffff88002a0ef308 2 388ms FSC: OBJ17e7 OP60d2e: Write1/Store fl=2
  165. 6 4094 ffff88002abaf4b8 2 132ms FSC: OBJ17e2 OP60d4e: Write1/Store fl=2
  166. 7 4095 ffff88002bb188e0 a 388ms FSC: OBJ17e9: CRTN
  167. vsq - ffff880023d99668 1 308ms FSC: OBJ17e0 OP60f91: Write1/EnQ fl=2
  168. vsq - ffff8800295d1740 1 212ms FSC: OBJ16be OP4d4b6: Write1/EnQ fl=2
  169. vsq - ffff880025ba3308 1 160ms FSC: OBJ179a OP58dec: Write1/EnQ fl=2
  170. vsq - ffff880024ec83e0 1 160ms FSC: OBJ17ae OP599f2: Write1/EnQ fl=2
  171. vsq - ffff880026618e00 1 160ms FSC: OBJ17e6 OP60d33: Write1/EnQ fl=2
  172. vsq - ffff880025a2a4b8 1 132ms FSC: OBJ16a2 OP4d583: Write1/EnQ fl=2
  173. vsq - ffff880023cbe6d8 9 212ms FSC: OBJ17eb: LOOK
  174. vsq - ffff880024d37590 9 212ms FSC: OBJ17ec: LOOK
  175. vsq - ffff880027746cb0 9 212ms FSC: OBJ17ed: LOOK
  176. vsq - ffff880024d37ae8 9 212ms FSC: OBJ17ee: LOOK
  177. vsq - ffff880024d37cb0 9 212ms FSC: OBJ17ef: LOOK
  178. vsq - ffff880025036550 9 212ms FSC: OBJ17f0: LOOK
  179. vsq - ffff8800250368e0 9 212ms FSC: OBJ17f1: LOOK
  180. vsq - ffff880025036aa8 9 212ms FSC: OBJ17f2: LOOK
  181. In the 'THR' column, executing items show the thread they're occupying and
  182. queued threads indicate which queue they're on. 'PID' shows the process ID of
  183. a slow-work thread that's executing something. 'FL' shows the work item flags.
  184. 'MARK' indicates how long since an item was queued or began executing. Lastly,
  185. the 'DESC' column permits the owner of an item to give some information.