xref: /spdk/include/spdk/thread.h (revision 407e88fd2ab020d753e33014cf759353a9901b51)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /** \file
35  * Thread
36  */
37 
38 #ifndef SPDK_THREAD_H_
39 #define SPDK_THREAD_H_
40 
41 #include "spdk/stdinc.h"
42 
43 #include "spdk/cpuset.h"
44 #include "spdk/queue.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 struct spdk_thread;
51 struct spdk_io_channel_iter;
52 struct spdk_poller;
53 
54 /**
55  * A function that is called each time a new thread is created.
56  * The implementor of this function should frequently call
57  * spdk_thread_poll() on the thread provided.
58  *
59  * \param thread The new spdk_thread.
60  */
61 typedef int (*spdk_new_thread_fn)(struct spdk_thread *thread);
62 
63 /**
64  * A function that will be called on the target thread.
65  *
66  * \param ctx Context passed as arg to spdk_thread_pass_msg().
67  */
68 typedef void (*spdk_msg_fn)(void *ctx);
69 
70 /**
71  * Function to be called to pass a message to a thread.
72  *
73  * \param fn Callback function for a thread.
74  * \param ctx Context passed to fn.
75  * \param thread_ctx Context for the thread.
76  */
77 typedef void (*spdk_thread_pass_msg)(spdk_msg_fn fn, void *ctx,
78 				     void *thread_ctx);
79 
80 /**
81  * Callback function for a poller.
82  *
83  * \param ctx Context passed as arg to spdk_poller_register().
84  * \return 0 to indicate that polling took place but no events were found;
85  * positive to indicate that polling took place and some events were processed;
86  * negative if the poller does not provide spin-wait information.
87  */
88 typedef int (*spdk_poller_fn)(void *ctx);
89 
90 /**
91  * Function to be called to start a poller for the thread.
92  *
93  * \param thread_ctx Context for the thread.
94  * \param fn Callback function for a poller.
95  * \param arg Argument passed to callback.
96  * \param period Polling period in microseconds.
97  *
98  * \return a pointer to the poller on success, or NULL on failure.
99  */
100 typedef struct spdk_poller *(*spdk_start_poller)(void *thread_ctx,
101 		spdk_poller_fn fn,
102 		void *arg,
103 		uint64_t period_microseconds);
104 
105 /**
106  * Function to be called to stop a poller.
107  *
108  * \param poller Poller to stop.
109  * \param thread_ctx Context for the thread.
110  */
111 typedef void (*spdk_stop_poller)(struct spdk_poller *poller, void *thread_ctx);
112 
113 /**
114  * I/O channel creation callback.
115  *
116  * \param io_device I/O device associated with this channel.
117  * \param ctx_buf Context for the I/O device.
118  */
119 typedef int (*spdk_io_channel_create_cb)(void *io_device, void *ctx_buf);
120 
121 /**
122  * I/O channel destruction callback.
123  *
124  * \param io_device I/O device associated with this channel.
125  * \param ctx_buf Context for the I/O device.
126  */
127 typedef void (*spdk_io_channel_destroy_cb)(void *io_device, void *ctx_buf);
128 
129 /**
130  * I/O device unregister callback.
131  *
132  * \param io_device Unregistered I/O device.
133  */
134 typedef void (*spdk_io_device_unregister_cb)(void *io_device);
135 
136 /**
137  * Called on the appropriate thread for each channel associated with io_device.
138  *
139  * \param i I/O channel iterator.
140  */
141 typedef void (*spdk_channel_msg)(struct spdk_io_channel_iter *i);
142 
143 /**
144  * spdk_for_each_channel() callback.
145  *
146  * \param i I/O channel iterator.
147  * \param status 0 if it completed successfully, or negative errno if it failed.
148  */
149 typedef void (*spdk_channel_for_each_cpl)(struct spdk_io_channel_iter *i, int status);
150 
151 /**
152  * \brief Represents a per-thread channel for accessing an I/O device.
153  *
154  * An I/O device may be a physical entity (i.e. NVMe controller) or a software
155  *  entity (i.e. a blobstore).
156  *
157  * This structure is not part of the API - all accesses should be done through
158  *  spdk_io_channel function calls.
159  */
160 struct spdk_io_channel {
161 	struct spdk_thread		*thread;
162 	struct io_device		*dev;
163 	uint32_t			ref;
164 	uint32_t			destroy_ref;
165 	TAILQ_ENTRY(spdk_io_channel)	tailq;
166 	spdk_io_channel_destroy_cb	destroy_cb;
167 
168 	/*
169 	 * Modules will allocate extra memory off the end of this structure
170 	 *  to store references to hardware-specific references (i.e. NVMe queue
171 	 *  pairs, or references to child device spdk_io_channels (i.e.
172 	 *  virtual bdevs).
173 	 */
174 };
175 
176 /**
177  * Initialize the threading library. Must be called once prior to allocating any threads.
178  *
179  * \param new_thread_fn Called each time a new SPDK thread is created. The implementor
180  * is expected to frequently call spdk_thread_poll() on the provided thread.
181  * \param ctx_sz For each thread allocated, an additional region of memory of
182  * size ctx_size will also be allocated, for use by the thread scheduler. A pointer
183  * to this region may be obtained by calling spdk_thread_get_ctx().
184  *
185  * \return 0 on success. Negated errno on failure.
186  */
187 int spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn, size_t ctx_sz);
188 
189 /**
190  * Release all resources associated with this library.
191  */
192 void spdk_thread_lib_fini(void);
193 
194 /**
195  * Creates a new SPDK thread object.
196  *
197  * \param name Human-readable name for the thread; can be retrieved with spdk_thread_get_name().
198  * The string is copied, so the pointed-to data only needs to be valid during the
199  * spdk_thread_create() call. May be NULL to specify no name.
200  * \param cpumask Optional mask of CPU cores on which to schedule this thread. This is only
201  * a suggestion to the scheduler. The value is copied, so cpumask may be released when
202  * this function returns. May be NULL if no mask is required.
203  *
204  * \return a pointer to the allocated thread on success or NULL on failure..
205  */
206 struct spdk_thread *spdk_thread_create(const char *name, struct spdk_cpuset *cpumask);
207 
208 /**
209  * Force the current system thread to act as if executing the given SPDK thread.
210  *
211  * \param thread The thread to set.
212  */
213 void spdk_set_thread(struct spdk_thread *thread);
214 
215 /**
216  * Mark the thread as exited, failing all future spdk_thread_poll() calls. May
217  * only be called within an spdk poller or message.
218  *
219  *
220  * \param thread The thread to destroy.
221  *
222  * All I/O channel references associated with the thread must be released using
223  * spdk_put_io_channel() prior to calling this function.
224  */
225 void spdk_thread_exit(struct spdk_thread *thread);
226 
227 /**
228  * Destroy a thread, releasing all of its resources. May only be called
229  * on a thread previously marked as exited.
230  *
231  * \param thread The thread to destroy.
232  *
233  */
234 void spdk_thread_destroy(struct spdk_thread *thread);
235 
236 /**
237  * Return a pointer to this thread's context.
238  *
239  * \param thread The thread on which to get the context.
240  *
241  * \return a pointer to the per-thread context, or NULL if there is
242  * no per-thread context.
243  */
244 void *spdk_thread_get_ctx(struct spdk_thread *thread);
245 
246 /**
247  * Get the thread's cpumask.
248  *
249  * \param thread The thread to get the cpumask for.
250  *
251  * \return cpuset pointer
252  */
253 struct spdk_cpuset *spdk_thread_get_cpumask(struct spdk_thread *thread);
254 
255 /**
256  * Return the thread object associated with the context handle previously
257  * obtained by calling spdk_thread_get_ctx().
258  *
259  * \param ctx A context previously obtained by calling spdk_thread_get_ctx()
260  *
261  * \return The associated thread.
262  */
263 struct spdk_thread *spdk_thread_get_from_ctx(void *ctx);
264 
265 /**
266  * Perform one iteration worth of processing on the thread. This includes
267  * both expired and continuous pollers as well as messages.
268  *
269  * \param thread The thread to process
270  * \param max_msgs The maximum number of messages that will be processed.
271  *                 Use 0 to process the default number of messages (8).
272  * \param now The current time, in ticks. Optional. If 0 is passed, this
273  *            function may call spdk_get_ticks() to get the current time.
274  *
275  * \return 1 if work was done. 0 if no work was done. -1 if thread has exited.
276  */
277 int spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now);
278 
279 /**
280  * Return the number of ticks until the next timed poller
281  * would expire. Timed pollers are pollers for which
282  * period_microseconds is greater than 0.
283  *
284  * \param thread The thread to check poller expiration times on
285  *
286  * \return Number of ticks. If no timed pollers, return 0.
287  */
288 uint64_t spdk_thread_next_poller_expiration(struct spdk_thread *thread);
289 
290 /**
291  * Returns whether there are any active pollers (pollers for which
292  * period_microseconds equals 0) registered to be run on the thread.
293  *
294  * \param thread The thread to check.
295  *
296  * \return 1 if there is at least one active poller, 0 otherwise.
297  */
298 int spdk_thread_has_active_pollers(struct spdk_thread *thread);
299 
300 /**
301  * Returns whether there are any pollers registered to be run
302  * on the thread.
303  *
304  * \param thread The thread to check.
305  *
306  * \return true if there is any active poller, false otherwise.
307  */
308 bool spdk_thread_has_pollers(struct spdk_thread *thread);
309 
310 /**
311  * Returns whether there are scheduled operations to be run on the thread.
312  *
313  * \param thread The thread to check.
314  *
315  * \return true if there are no scheduled operations, false otherwise.
316  */
317 bool spdk_thread_is_idle(struct spdk_thread *thread);
318 
319 /**
320  * Get count of allocated threads.
321  */
322 uint32_t spdk_thread_get_count(void);
323 
324 /**
325  * Get a handle to the current thread.
326  *
327  * This handle may be passed to other threads and used as the target of
328  * spdk_thread_send_msg().
329  *
330  * \sa spdk_io_channel_get_thread()
331  *
332  * \return a pointer to the current thread on success or NULL on failure.
333  */
334 struct spdk_thread *spdk_get_thread(void);
335 
336 /**
337  * Get a thread's name.
338  *
339  * \param thread Thread to query.
340  *
341  * \return the name of the thread.
342  */
343 const char *spdk_thread_get_name(const struct spdk_thread *thread);
344 
345 struct spdk_thread_stats {
346 	uint64_t busy_tsc;
347 	uint64_t idle_tsc;
348 };
349 
350 /**
351  * Get statistics about the current thread.
352  *
353  * Copy cumulative thread stats values to the provided thread stats structure.
354  *
355  * \param stats User's thread_stats structure.
356  */
357 int spdk_thread_get_stats(struct spdk_thread_stats *stats);
358 
359 /**
360  * Send a message to the given thread.
361  *
362  * The message may be sent asynchronously - i.e. spdk_thread_send_msg may return
363  * prior to `fn` being called.
364  *
365  * \param thread The target thread.
366  * \param fn This function will be called on the given thread.
367  * \param ctx This context will be passed to fn when called.
368  */
369 void spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx);
370 
371 /**
372  * Send a message to each thread, serially.
373  *
374  * The message is sent asynchronously - i.e. spdk_for_each_thread will return
375  * prior to `fn` being called on each thread.
376  *
377  * \param fn This is the function that will be called on each thread.
378  * \param ctx This context will be passed to fn when called.
379  * \param cpl This will be called on the originating thread after `fn` has been
380  * called on each thread.
381  */
382 void spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl);
383 
384 /**
385  * Register a poller on the current thread.
386  *
387  * The poller can be unregistered by calling spdk_poller_unregister().
388  *
389  * \param fn This function will be called every `period_microseconds`.
390  * \param arg Argument passed to fn.
391  * \param period_microseconds How often to call `fn`. If 0, call `fn` as often
392  *  as possible.
393  *
394  * \return a pointer to the poller registered on the current thread on success
395  * or NULL on failure.
396  */
397 struct spdk_poller *spdk_poller_register(spdk_poller_fn fn,
398 		void *arg,
399 		uint64_t period_microseconds);
400 
401 /**
402  * Unregister a poller on the current thread.
403  *
404  * \param ppoller The poller to unregister.
405  */
406 void spdk_poller_unregister(struct spdk_poller **ppoller);
407 
408 /**
409  * Register the opaque io_device context as an I/O device.
410  *
411  * After an I/O device is registered, it can return I/O channels using the
412  * spdk_get_io_channel() function.
413  *
414  * \param io_device The pointer to io_device context.
415  * \param create_cb Callback function invoked to allocate any resources required
416  * for a new I/O channel.
417  * \param destroy_cb Callback function invoked to release the resources for an
418  * I/O channel.
419  * \param ctx_size The size of the context buffer allocated to store references
420  * to allocated I/O channel resources.
421  * \param name A string name for the device used only for debugging. Optional -
422  * may be NULL.
423  */
424 void spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
425 			     spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size,
426 			     const char *name);
427 
428 /**
429  * Unregister the opaque io_device context as an I/O device.
430  *
431  * The actual unregistration might be deferred until all active I/O channels are
432  * destroyed.
433  *
434  * \param io_device The pointer to io_device context.
435  * \param unregister_cb An optional callback function invoked to release any
436  * references to this I/O device.
437  */
438 void spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb);
439 
440 /**
441  * Get an I/O channel for the specified io_device to be used by the calling thread.
442  *
443  * The io_device context pointer specified must have previously been registered
444  * using spdk_io_device_register(). If an existing I/O channel does not exist
445  * yet for the given io_device on the calling thread, it will allocate an I/O
446  * channel and invoke the create_cb function pointer specified in spdk_io_device_register().
447  * If an I/O channel already exists for the given io_device on the calling thread,
448  * its reference is returned rather than creating a new I/O channel.
449  *
450  * \param io_device The pointer to io_device context.
451  *
452  * \return a pointer to the I/O channel for this device on success or NULL on failure.
453  */
454 struct spdk_io_channel *spdk_get_io_channel(void *io_device);
455 
456 /**
457  * Release a reference to an I/O channel. This happens asynchronously.
458  *
459  * Actual release will happen on the same thread that called spdk_get_io_channel()
460  * for the specified I/O channel. If this releases the last reference to the
461  * I/O channel, The destroy_cb function specified in spdk_io_device_register()
462  * will be invoked to release any associated resources.
463  *
464  * \param ch I/O channel to release a reference.
465  */
466 void spdk_put_io_channel(struct spdk_io_channel *ch);
467 
468 /**
469  * Get the context buffer associated with an I/O channel.
470  *
471  * \param ch I/O channel.
472  *
473  * \return a pointer to the context buffer.
474  */
475 static inline void *
476 spdk_io_channel_get_ctx(struct spdk_io_channel *ch)
477 {
478 	return (uint8_t *)ch + sizeof(*ch);
479 }
480 
481 /**
482  * Get I/O channel from the context buffer. This is the inverse of
483  * spdk_io_channel_get_ctx().
484  *
485  * \param ctx The pointer to the context buffer.
486  *
487  * \return a pointer to the I/O channel associated with the context buffer.
488  */
489 struct spdk_io_channel *spdk_io_channel_from_ctx(void *ctx);
490 
491 /**
492  * Get the thread associated with an I/O channel.
493  *
494  * \param ch I/O channel.
495  *
496  * \return a pointer to the thread associated with the I/O channel
497  */
498 struct spdk_thread *spdk_io_channel_get_thread(struct spdk_io_channel *ch);
499 
500 /**
501  * Call 'fn' on each channel associated with io_device.
502  *
503  * This happens asynchronously, so fn may be called after spdk_for_each_channel
504  * returns. 'fn' will be called for each channel serially, such that two calls
505  * to 'fn' will not overlap in time. After 'fn' has been called, call
506  * spdk_for_each_channel_continue() to continue iterating.
507  *
508  * \param io_device 'fn' will be called on each channel associated with this io_device.
509  * \param fn Called on the appropriate thread for each channel associated with io_device.
510  * \param ctx Context buffer registered to spdk_io_channel_iter that can be obatined
511  * form the function spdk_io_channel_iter_get_ctx().
512  * \param cpl Called on the thread that spdk_for_each_channel was initially called
513  * from when 'fn' has been called on each channel.
514  */
515 void spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx,
516 			   spdk_channel_for_each_cpl cpl);
517 
518 /**
519  * Get io_device from the I/O channel iterator.
520  *
521  * \param i I/O channel iterator.
522  *
523  * \return a pointer to the io_device.
524  */
525 void *spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i);
526 
527 /**
528  * Get I/O channel from the I/O channel iterator.
529  *
530  * \param i I/O channel iterator.
531  *
532  * \return a pointer to the I/O channel.
533  */
534 struct spdk_io_channel *spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i);
535 
536 /**
537  * Get context buffer from the I/O channel iterator.
538  *
539  * \param i I/O channel iterator.
540  *
541  * \return a pointer to the context buffer.
542  */
543 void *spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i);
544 
545 /**
546  * Helper function to iterate all channels for spdk_for_each_channel().
547  *
548  * \param i I/O channel iterator.
549  * \param status Status for the I/O channel iterator.
550  */
551 void spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status);
552 
553 #ifdef __cplusplus
554 }
555 #endif
556 
557 #endif /* SPDK_THREAD_H_ */
558