slow-work.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. THREAD-TO-CLASS ALLOCATION
  31. --------------------------
  32. Not all the threads in the pool are available to work on very slow work items.
  33. The number will be between one and one fewer than the number of active threads.
  34. This is configurable (see the "Pool Configuration" section).
  35. All the threads are available to work on ordinarily slow work items, but a
  36. percentage of the threads will prefer to work on very slow work items.
  37. The configuration ensures that at least one thread will be available to work on
  38. very slow work items, and at least one thread will be available that won't work
  39. on very slow work items at all.
  40. =====================
  41. USING SLOW WORK ITEMS
  42. =====================
  43. Firstly, a module or subsystem wanting to make use of slow work items must
  44. register its interest:
  45. int ret = slow_work_register_user();
  46. This will return 0 if successful, or a -ve error upon failure.
  47. Slow work items may then be set up by:
  48. (1) Declaring a slow_work struct type variable:
  49. #include <linux/slow-work.h>
  50. struct slow_work myitem;
  51. (2) Declaring the operations to be used for this item:
  52. struct slow_work_ops myitem_ops = {
  53. .get_ref = myitem_get_ref,
  54. .put_ref = myitem_put_ref,
  55. .execute = myitem_execute,
  56. };
  57. [*] For a description of the ops, see section "Item Operations".
  58. (3) Initialising the item:
  59. slow_work_init(&myitem, &myitem_ops);
  60. or:
  61. vslow_work_init(&myitem, &myitem_ops);
  62. depending on its class.
  63. A suitably set up work item can then be enqueued for processing:
  64. int ret = slow_work_enqueue(&myitem);
  65. This will return a -ve error if the thread pool is unable to gain a reference
  66. on the item, 0 otherwise.
  67. The items are reference counted, so there ought to be no need for a flush
  68. operation. When all a module's slow work items have been processed, and the
  69. module has no further interest in the facility, it should unregister its
  70. interest:
  71. slow_work_unregister_user();
  72. ===============
  73. ITEM OPERATIONS
  74. ===============
  75. Each work item requires a table of operations of type struct slow_work_ops.
  76. All members are required:
  77. (*) Get a reference on an item:
  78. int (*get_ref)(struct slow_work *work);
  79. This allows the thread pool to attempt to pin an item by getting a
  80. reference on it. This function should return 0 if the reference was
  81. granted, or a -ve error otherwise. If an error is returned,
  82. slow_work_enqueue() will fail.
  83. The reference is held whilst the item is queued and whilst it is being
  84. executed. The item may then be requeued with the same reference held, or
  85. the reference will be released.
  86. (*) Release a reference on an item:
  87. void (*put_ref)(struct slow_work *work);
  88. This allows the thread pool to unpin an item by releasing the reference on
  89. it. The thread pool will not touch the item again once this has been
  90. called.
  91. (*) Execute an item:
  92. void (*execute)(struct slow_work *work);
  93. This should perform the work required of the item. It may sleep, it may
  94. perform disk I/O and it may wait for locks.
  95. ==================
  96. POOL CONFIGURATION
  97. ==================
  98. The slow-work thread pool has a number of configurables:
  99. (*) /proc/sys/kernel/slow-work/min-threads
  100. The minimum number of threads that should be in the pool whilst it is in
  101. use. This may be anywhere between 2 and max-threads.
  102. (*) /proc/sys/kernel/slow-work/max-threads
  103. The maximum number of threads that should in the pool. This may be
  104. anywhere between min-threads and 255 or NR_CPUS * 2, whichever is greater.
  105. (*) /proc/sys/kernel/slow-work/vslow-percentage
  106. The percentage of active threads in the pool that may be used to execute
  107. very slow work items. This may be between 1 and 99. The resultant number
  108. is bounded to between 1 and one fewer than the number of active threads.
  109. This ensures there is always at least one thread that can process very
  110. slow work items, and always at least one thread that won't.