SLOW_WORK: Add delayed_slow_work support
[deliverable/linux.git] / Documentation / slow-work.txt
CommitLineData
8f0aa2f2
DH
1 ====================================
2 SLOW WORK ITEM EXECUTION THREAD POOL
3 ====================================
4
5By: David Howells <dhowells@redhat.com>
6
7The slow work item execution thread pool is a pool of threads for performing
8things that take a relatively long time, such as making mkdir calls.
9Typically, when processing something, these items will spend a lot of time
10blocking a thread on I/O, thus making that thread unavailable for doing other
11work.
12
13The standard workqueue model is unsuitable for this class of work item as that
14limits the owner to a single thread or a single thread per CPU. For some
15tasks, however, more threads - or fewer - are required.
16
17There is just one pool per system. It contains no threads unless something
18wants to use it - and that something must register its interest first. When
19the pool is active, the number of threads it contains is dynamic, varying
20between a maximum and minimum setting, depending on the load.
21
22
23====================
24CLASSES OF WORK ITEM
25====================
26
27This pool support two classes of work items:
28
29 (*) Slow work items.
30
31 (*) Very slow work items.
32
33The former are expected to finish much quicker than the latter.
34
35An operation of the very slow class may do a batch combination of several
36lookups, mkdirs, and a create for instance.
37
38An operation of the ordinarily slow class may, for example, write stuff or
39expand files, provided the time taken to do so isn't too long.
40
41Operations of both types may sleep during execution, thus tying up the thread
42loaned to it.
43
6b8268b1
JA
44A further class of work item is available, based on the slow work item class:
45
46 (*) Delayed slow work items.
47
48These are slow work items that have a timer to defer queueing of the item for
49a while.
50
8f0aa2f2
DH
51
52THREAD-TO-CLASS ALLOCATION
53--------------------------
54
55Not all the threads in the pool are available to work on very slow work items.
56The number will be between one and one fewer than the number of active threads.
57This is configurable (see the "Pool Configuration" section).
58
59All the threads are available to work on ordinarily slow work items, but a
60percentage of the threads will prefer to work on very slow work items.
61
62The configuration ensures that at least one thread will be available to work on
63very slow work items, and at least one thread will be available that won't work
64on very slow work items at all.
65
66
67=====================
68USING SLOW WORK ITEMS
69=====================
70
71Firstly, a module or subsystem wanting to make use of slow work items must
72register its interest:
73
3d7a641e 74 int ret = slow_work_register_user(struct module *module);
8f0aa2f2 75
3d7a641e
DH
76This will return 0 if successful, or a -ve error upon failure. The module
77pointer should be the module interested in using this facility (almost
78certainly THIS_MODULE).
8f0aa2f2
DH
79
80
81Slow work items may then be set up by:
82
83 (1) Declaring a slow_work struct type variable:
84
85 #include <linux/slow-work.h>
86
87 struct slow_work myitem;
88
89 (2) Declaring the operations to be used for this item:
90
91 struct slow_work_ops myitem_ops = {
92 .get_ref = myitem_get_ref,
93 .put_ref = myitem_put_ref,
94 .execute = myitem_execute,
95 };
96
97 [*] For a description of the ops, see section "Item Operations".
98
99 (3) Initialising the item:
100
101 slow_work_init(&myitem, &myitem_ops);
102
6b8268b1
JA
103 or:
104
105 delayed_slow_work_init(&myitem, &myitem_ops);
106
8f0aa2f2
DH
107 or:
108
109 vslow_work_init(&myitem, &myitem_ops);
110
111 depending on its class.
112
113A suitably set up work item can then be enqueued for processing:
114
115 int ret = slow_work_enqueue(&myitem);
116
117This will return a -ve error if the thread pool is unable to gain a reference
6b8268b1
JA
118on the item, 0 otherwise, or (for delayed work):
119
120 int ret = delayed_slow_work_enqueue(&myitem, my_jiffy_delay);
8f0aa2f2
DH
121
122
123The items are reference counted, so there ought to be no need for a flush
01609502
JA
124operation. But as the reference counting is optional, means to cancel
125existing work items are also included:
126
127 cancel_slow_work(&myitem);
6b8268b1 128 cancel_delayed_slow_work(&myitem);
01609502
JA
129
130can be used to cancel pending work. The above cancel function waits for
131existing work to have been executed (or prevent execution of them, depending
132on timing).
133
134
135When all a module's slow work items have been processed, and the
8f0aa2f2
DH
136module has no further interest in the facility, it should unregister its
137interest:
138
3d7a641e
DH
139 slow_work_unregister_user(struct module *module);
140
141The module pointer is used to wait for all outstanding work items for that
142module before completing the unregistration. This prevents the put_ref() code
143from being taken away before it completes. module should almost certainly be
144THIS_MODULE.
8f0aa2f2
DH
145
146
147===============
148ITEM OPERATIONS
149===============
150
151Each work item requires a table of operations of type struct slow_work_ops.
4d8bb2cb 152Only ->execute() is required, getting and putting of a reference are optional.
8f0aa2f2
DH
153
154 (*) Get a reference on an item:
155
156 int (*get_ref)(struct slow_work *work);
157
158 This allows the thread pool to attempt to pin an item by getting a
159 reference on it. This function should return 0 if the reference was
160 granted, or a -ve error otherwise. If an error is returned,
161 slow_work_enqueue() will fail.
162
163 The reference is held whilst the item is queued and whilst it is being
164 executed. The item may then be requeued with the same reference held, or
165 the reference will be released.
166
167 (*) Release a reference on an item:
168
169 void (*put_ref)(struct slow_work *work);
170
171 This allows the thread pool to unpin an item by releasing the reference on
172 it. The thread pool will not touch the item again once this has been
173 called.
174
175 (*) Execute an item:
176
177 void (*execute)(struct slow_work *work);
178
179 This should perform the work required of the item. It may sleep, it may
180 perform disk I/O and it may wait for locks.
181
182
183==================
184POOL CONFIGURATION
185==================
186
187The slow-work thread pool has a number of configurables:
188
189 (*) /proc/sys/kernel/slow-work/min-threads
190
191 The minimum number of threads that should be in the pool whilst it is in
192 use. This may be anywhere between 2 and max-threads.
193
194 (*) /proc/sys/kernel/slow-work/max-threads
195
196 The maximum number of threads that should in the pool. This may be
197 anywhere between min-threads and 255 or NR_CPUS * 2, whichever is greater.
198
199 (*) /proc/sys/kernel/slow-work/vslow-percentage
200
201 The percentage of active threads in the pool that may be used to execute
202 very slow work items. This may be between 1 and 99. The resultant number
203 is bounded to between 1 and one fewer than the number of active threads.
204 This ensures there is always at least one thread that can process very
205 slow work items, and always at least one thread that won't.
This page took 0.072437 seconds and 5 git commands to generate.