Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2016 Intel Corporation.
3 : : * All rights reserved.
4 : : * Copyright (c) 2022, 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #include "spdk/env.h"
10 : : #include "spdk/likely.h"
11 : : #include "spdk/queue.h"
12 : : #include "spdk/string.h"
13 : : #include "spdk/thread.h"
14 : : #include "spdk/trace.h"
15 : : #include "spdk/util.h"
16 : : #include "spdk/fd_group.h"
17 : :
18 : : #include "spdk/log.h"
19 : : #include "spdk_internal/thread.h"
20 : : #include "spdk_internal/usdt.h"
21 : : #include "thread_internal.h"
22 : :
23 : : #include "spdk_internal/trace_defs.h"
24 : :
25 : : #ifdef __linux__
26 : : #include <sys/timerfd.h>
27 : : #include <sys/eventfd.h>
28 : : #endif
29 : :
30 : : #ifdef SPDK_HAVE_EXECINFO_H
31 : : #include <execinfo.h>
32 : : #endif
33 : :
34 : : #define SPDK_MSG_BATCH_SIZE 8
35 : : #define SPDK_MAX_DEVICE_NAME_LEN 256
36 : : #define SPDK_THREAD_EXIT_TIMEOUT_SEC 5
37 : : #define SPDK_MAX_POLLER_NAME_LEN 256
38 : : #define SPDK_MAX_THREAD_NAME_LEN 256
39 : :
40 : : static struct spdk_thread *g_app_thread;
41 : :
42 : : struct spdk_interrupt {
43 : : int efd;
44 : : struct spdk_fd_group *fgrp;
45 : : struct spdk_thread *thread;
46 : : spdk_interrupt_fn fn;
47 : : void *arg;
48 : : char name[SPDK_MAX_POLLER_NAME_LEN + 1];
49 : : };
50 : :
51 : : enum spdk_poller_state {
52 : : /* The poller is registered with a thread but not currently executing its fn. */
53 : : SPDK_POLLER_STATE_WAITING,
54 : :
55 : : /* The poller is currently running its fn. */
56 : : SPDK_POLLER_STATE_RUNNING,
57 : :
58 : : /* The poller was unregistered during the execution of its fn. */
59 : : SPDK_POLLER_STATE_UNREGISTERED,
60 : :
61 : : /* The poller is in the process of being paused. It will be paused
62 : : * during the next time it's supposed to be executed.
63 : : */
64 : : SPDK_POLLER_STATE_PAUSING,
65 : :
66 : : /* The poller is registered but currently paused. It's on the
67 : : * paused_pollers list.
68 : : */
69 : : SPDK_POLLER_STATE_PAUSED,
70 : : };
71 : :
72 : : struct spdk_poller {
73 : : TAILQ_ENTRY(spdk_poller) tailq;
74 : : RB_ENTRY(spdk_poller) node;
75 : :
76 : : /* Current state of the poller; should only be accessed from the poller's thread. */
77 : : enum spdk_poller_state state;
78 : :
79 : : uint64_t period_ticks;
80 : : uint64_t next_run_tick;
81 : : uint64_t run_count;
82 : : uint64_t busy_count;
83 : : uint64_t id;
84 : : spdk_poller_fn fn;
85 : : void *arg;
86 : : struct spdk_thread *thread;
87 : : struct spdk_interrupt *intr;
88 : : spdk_poller_set_interrupt_mode_cb set_intr_cb_fn;
89 : : void *set_intr_cb_arg;
90 : :
91 : : char name[SPDK_MAX_POLLER_NAME_LEN + 1];
92 : : };
93 : :
94 : : enum spdk_thread_state {
95 : : /* The thread is processing poller and message by spdk_thread_poll(). */
96 : : SPDK_THREAD_STATE_RUNNING,
97 : :
98 : : /* The thread is in the process of termination. It reaps unregistering
99 : : * poller are releasing I/O channel.
100 : : */
101 : : SPDK_THREAD_STATE_EXITING,
102 : :
103 : : /* The thread is exited. It is ready to call spdk_thread_destroy(). */
104 : : SPDK_THREAD_STATE_EXITED,
105 : : };
106 : :
107 : : struct spdk_thread_post_poller_handler {
108 : : spdk_post_poller_fn fn;
109 : : void *fn_arg;
110 : : };
111 : :
112 : : #define SPDK_THREAD_MAX_POST_POLLER_HANDLERS (4)
113 : :
114 : : struct spdk_thread {
115 : : uint64_t tsc_last;
116 : : struct spdk_thread_stats stats;
117 : : /*
118 : : * Contains pollers actively running on this thread. Pollers
119 : : * are run round-robin. The thread takes one poller from the head
120 : : * of the ring, executes it, then puts it back at the tail of
121 : : * the ring.
122 : : */
123 : : TAILQ_HEAD(active_pollers_head, spdk_poller) active_pollers;
124 : : /**
125 : : * Contains pollers running on this thread with a periodic timer.
126 : : */
127 : : RB_HEAD(timed_pollers_tree, spdk_poller) timed_pollers;
128 : : struct spdk_poller *first_timed_poller;
129 : : /*
130 : : * Contains paused pollers. Pollers on this queue are waiting until
131 : : * they are resumed (in which case they're put onto the active/timer
132 : : * queues) or unregistered.
133 : : */
134 : : TAILQ_HEAD(paused_pollers_head, spdk_poller) paused_pollers;
135 : : struct spdk_thread_post_poller_handler pp_handlers[SPDK_THREAD_MAX_POST_POLLER_HANDLERS];
136 : : struct spdk_ring *messages;
137 : : uint8_t num_pp_handlers;
138 : : int msg_fd;
139 : : SLIST_HEAD(, spdk_msg) msg_cache;
140 : : size_t msg_cache_count;
141 : : spdk_msg_fn critical_msg;
142 : : uint64_t id;
143 : : uint64_t next_poller_id;
144 : : enum spdk_thread_state state;
145 : : int pending_unregister_count;
146 : : uint32_t for_each_count;
147 : :
148 : : RB_HEAD(io_channel_tree, spdk_io_channel) io_channels;
149 : : TAILQ_ENTRY(spdk_thread) tailq;
150 : :
151 : : char name[SPDK_MAX_THREAD_NAME_LEN + 1];
152 : : struct spdk_cpuset cpumask;
153 : : uint64_t exit_timeout_tsc;
154 : :
155 : : int32_t lock_count;
156 : :
157 : : /* spdk_thread is bound to current CPU core. */
158 : : bool is_bound;
159 : :
160 : : /* Indicates whether this spdk_thread currently runs in interrupt. */
161 : : bool in_interrupt;
162 : : bool poller_unregistered;
163 : : struct spdk_fd_group *fgrp;
164 : :
165 : : uint16_t trace_id;
166 : :
167 : : uint8_t reserved[6];
168 : :
169 : : /* User context allocated at the end */
170 : : uint8_t ctx[0];
171 : : };
172 : :
173 : : /*
174 : : * Assert that spdk_thread struct is 8 byte aligned to ensure
175 : : * the user ctx is also 8-byte aligned.
176 : : */
177 : : SPDK_STATIC_ASSERT((sizeof(struct spdk_thread)) % 8 == 0, "Incorrect size");
178 : :
179 : : static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER;
180 : :
181 : : static spdk_new_thread_fn g_new_thread_fn = NULL;
182 : : static spdk_thread_op_fn g_thread_op_fn = NULL;
183 : : static spdk_thread_op_supported_fn g_thread_op_supported_fn;
184 : : static size_t g_ctx_sz = 0;
185 : : /* Monotonic increasing ID is set to each created thread beginning at 1. Once the
186 : : * ID exceeds UINT64_MAX, further thread creation is not allowed and restarting
187 : : * SPDK application is required.
188 : : */
189 : : static uint64_t g_thread_id = 1;
190 : :
191 : : enum spin_error {
192 : : SPIN_ERR_NONE,
193 : : /* Trying to use an SPDK lock while not on an SPDK thread */
194 : : SPIN_ERR_NOT_SPDK_THREAD,
195 : : /* Trying to lock a lock already held by this SPDK thread */
196 : : SPIN_ERR_DEADLOCK,
197 : : /* Trying to unlock a lock not held by this SPDK thread */
198 : : SPIN_ERR_WRONG_THREAD,
199 : : /* pthread_spin_*() returned an error */
200 : : SPIN_ERR_PTHREAD,
201 : : /* Trying to destroy a lock that is held */
202 : : SPIN_ERR_LOCK_HELD,
203 : : /* lock_count is invalid */
204 : : SPIN_ERR_LOCK_COUNT,
205 : : /*
206 : : * An spdk_thread may migrate to another pthread. A spinlock held across migration leads to
207 : : * undefined behavior. A spinlock held when an SPDK thread goes off CPU would lead to
208 : : * deadlock when another SPDK thread on the same pthread tries to take that lock.
209 : : */
210 : : SPIN_ERR_HOLD_DURING_SWITCH,
211 : : /* Trying to use a lock that was destroyed (but not re-initialized) */
212 : : SPIN_ERR_DESTROYED,
213 : : /* Trying to use a lock that is not initialized */
214 : : SPIN_ERR_NOT_INITIALIZED,
215 : :
216 : : /* Must be last, not an actual error code */
217 : : SPIN_ERR_LAST
218 : : };
219 : :
220 : : static const char *spin_error_strings[] = {
221 : : [SPIN_ERR_NONE] = "No error",
222 : : [SPIN_ERR_NOT_SPDK_THREAD] = "Not an SPDK thread",
223 : : [SPIN_ERR_DEADLOCK] = "Deadlock detected",
224 : : [SPIN_ERR_WRONG_THREAD] = "Unlock on wrong SPDK thread",
225 : : [SPIN_ERR_PTHREAD] = "Error from pthread_spinlock",
226 : : [SPIN_ERR_LOCK_HELD] = "Destroying a held spinlock",
227 : : [SPIN_ERR_LOCK_COUNT] = "Lock count is invalid",
228 : : [SPIN_ERR_HOLD_DURING_SWITCH] = "Lock(s) held while SPDK thread going off CPU",
229 : : [SPIN_ERR_DESTROYED] = "Lock has been destroyed",
230 : : [SPIN_ERR_NOT_INITIALIZED] = "Lock has not been initialized",
231 : : };
232 : :
233 : : #define SPIN_ERROR_STRING(err) (err < 0 || err >= SPDK_COUNTOF(spin_error_strings)) \
234 : : ? "Unknown error" : spin_error_strings[err]
235 : :
236 : : static void
237 : 0 : __posix_abort(enum spin_error err)
238 : : {
239 [ # # ]: 0 : abort();
240 : : }
241 : :
242 : : typedef void (*spin_abort)(enum spin_error err);
243 : : spin_abort g_spin_abort_fn = __posix_abort;
244 : :
245 : : #define SPIN_ASSERT_IMPL(cond, err, extra_log, ret) \
246 : : do { \
247 : : if (spdk_unlikely(!(cond))) { \
248 : : SPDK_ERRLOG("unrecoverable spinlock error %d: %s (%s)\n", err, \
249 : : SPIN_ERROR_STRING(err), #cond); \
250 : : extra_log; \
251 : : g_spin_abort_fn(err); \
252 : : ret; \
253 : : } \
254 : : } while (0)
255 : : #define SPIN_ASSERT_LOG_STACKS(cond, err, lock) \
256 : : SPIN_ASSERT_IMPL(cond, err, sspin_stacks_print(sspin), return)
257 : : #define SPIN_ASSERT_RETURN(cond, err, ret) SPIN_ASSERT_IMPL(cond, err, , return ret)
258 : : #define SPIN_ASSERT(cond, err) SPIN_ASSERT_IMPL(cond, err, ,)
259 : :
260 : : struct io_device {
261 : : void *io_device;
262 : : char name[SPDK_MAX_DEVICE_NAME_LEN + 1];
263 : : spdk_io_channel_create_cb create_cb;
264 : : spdk_io_channel_destroy_cb destroy_cb;
265 : : spdk_io_device_unregister_cb unregister_cb;
266 : : struct spdk_thread *unregister_thread;
267 : : uint32_t ctx_size;
268 : : uint32_t for_each_count;
269 : : RB_ENTRY(io_device) node;
270 : :
271 : : uint32_t refcnt;
272 : :
273 : : bool pending_unregister;
274 : : bool unregistered;
275 : : };
276 : :
277 : : static RB_HEAD(io_device_tree, io_device) g_io_devices = RB_INITIALIZER(g_io_devices);
278 : :
279 : : static int
280 : 4646703 : io_device_cmp(struct io_device *dev1, struct io_device *dev2)
281 : : {
282 [ + + + - : 4646703 : return (dev1->io_device < dev2->io_device ? -1 : dev1->io_device > dev2->io_device);
+ - + - +
+ + - + -
+ - + - ]
283 : : }
284 : :
285 [ + + + + : 4929799 : RB_GENERATE_STATIC(io_device_tree, io_device, node, io_device_cmp);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ + + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + + -
+ - + - +
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + + + +
+ - + - +
- + - + -
+ - - + -
+ - + - +
- + - + -
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ + + - +
- + - + +
+ - + - +
- + + + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # +
- + - + -
+ - + - +
- - + - +
- + - + -
+ - + - +
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - + # #
# # # # +
- + - + -
+ - # # #
# # # + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # +
- + - + -
+ - + - +
- + - # #
# # # # #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - + # #
# # # # +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + +
+ - + - +
- + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
+ + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- # # # #
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - +
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + ]
286 : :
287 : : static int
288 : 11493096 : io_channel_cmp(struct spdk_io_channel *ch1, struct spdk_io_channel *ch2)
289 : : {
290 [ + + + - : 11493096 : return (ch1->dev < ch2->dev ? -1 : ch1->dev > ch2->dev);
+ - + - +
+ + - + -
+ - + - ]
291 : : }
292 : :
293 [ + + + + : 14516305 : RB_GENERATE_STATIC(io_channel_tree, spdk_io_channel, node, io_channel_cmp);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ + + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + + -
+ - + - +
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + + + +
+ - + - +
- + - + -
+ - + + -
+ - + - +
- + - + -
+ + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ + + - +
- + + + +
+ - + - +
- + + + -
+ - + - +
- + - + -
+ + + - +
- + - + +
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# + - + -
+ - + - +
- + - - +
- + - + -
+ - + - +
- + # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - +
# # # # #
# + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
+ + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - -
+ # # # #
# # + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- # # # #
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - +
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
- + # # #
# # # + -
+ - + - +
- + - + -
+ + + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - + - #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + ]
294 : :
295 : : struct spdk_msg {
296 : : spdk_msg_fn fn;
297 : : void *arg;
298 : :
299 : : SLIST_ENTRY(spdk_msg) link;
300 : : };
301 : :
302 : : static struct spdk_mempool *g_spdk_msg_mempool = NULL;
303 : :
304 : : static TAILQ_HEAD(, spdk_thread) g_threads = TAILQ_HEAD_INITIALIZER(g_threads);
305 : : static uint32_t g_thread_count = 0;
306 : :
307 : : static __thread struct spdk_thread *tls_thread = NULL;
308 : :
309 : : static void
310 : 2324 : thread_trace(void)
311 : : {
312 : 2324 : struct spdk_trace_tpoint_opts opts[] = {
313 : : {
314 : : "THREAD_IOCH_GET", TRACE_THREAD_IOCH_GET,
315 : : OWNER_TYPE_NONE, OBJECT_NONE, 0,
316 : : {{ "refcnt", SPDK_TRACE_ARG_TYPE_INT, 4 }}
317 : : },
318 : : {
319 : : "THREAD_IOCH_PUT", TRACE_THREAD_IOCH_PUT,
320 : : OWNER_TYPE_NONE, OBJECT_NONE, 0,
321 : : {{ "refcnt", SPDK_TRACE_ARG_TYPE_INT, 4 }}
322 : : }
323 : : };
324 : :
325 : 2324 : spdk_trace_register_owner_type(OWNER_TYPE_THREAD, 't');
326 : 2324 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
327 : 2324 : }
328 : 3022 : SPDK_TRACE_REGISTER_FN(thread_trace, "thread", TRACE_GROUP_THREAD)
329 : :
330 : : /*
331 : : * If this compare function returns zero when two next_run_ticks are equal,
332 : : * the macro RB_INSERT() returns a pointer to the element with the same
333 : : * next_run_tick.
334 : : *
335 : : * Fortunately, the macro RB_REMOVE() takes not a key but a pointer to the element
336 : : * to remove as a parameter.
337 : : *
338 : : * Hence we allow RB_INSERT() to insert elements with the same keys on the right
339 : : * side by returning 1 when two next_run_ticks are equal.
340 : : */
341 : : static inline int
342 : 166479691 : timed_poller_compare(struct spdk_poller *poller1, struct spdk_poller *poller2)
343 : : {
344 [ + + + - : 166479691 : if (poller1->next_run_tick < poller2->next_run_tick) {
+ - + - +
+ ]
345 : 23460236 : return -1;
346 : : } else {
347 : 143019455 : return 1;
348 : : }
349 : 8884108 : }
350 : :
351 [ + + + + : 372732225 : RB_GENERATE_STATIC(timed_pollers_tree, spdk_poller, node, timed_poller_compare);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + - + -
+ + + - +
- + + + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- - + + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # + + +
- + - + -
+ - + - +
- + + + +
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ - + - +
- - + + -
+ - + - +
+ + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - - +
# # # # #
# + - + -
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # +
- + - + -
+ - + - +
- + - # #
# # # # #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - # #
# # # # +
- + - + -
+ - # # #
# # # + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
- + + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
+ - + - +
- + + + -
+ - + + +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + - +
+ - + - +
- + + - +
- + - - +
+ - + - +
- + + + -
+ - + - +
+ + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - # #
# # + - +
- + - + -
+ - + - -
+ - + - +
- + - + -
+ - + # #
# # # # #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ # # # #
# # + - +
- + - + -
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - # #
# # + - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ # # # #
# # + - +
- + - + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
352 : :
353 : : static inline struct spdk_thread *
354 :10899156042 : _get_thread(void)
355 : : {
356 :10899156042 : return tls_thread;
357 : : }
358 : :
359 : : static int
360 : 2661 : _thread_lib_init(size_t ctx_sz, size_t msg_mempool_sz)
361 : : {
362 : 1246 : char mempool_name[SPDK_MAX_MEMZONE_NAME_LEN];
363 : :
364 : 2661 : g_ctx_sz = ctx_sz;
365 : :
366 [ - + ]: 2661 : snprintf(mempool_name, sizeof(mempool_name), "msgpool_%d", getpid());
367 : 2661 : g_spdk_msg_mempool = spdk_mempool_create(mempool_name, msg_mempool_sz,
368 : : sizeof(struct spdk_msg),
369 : : 0, /* No cache. We do our own. */
370 : : SPDK_ENV_NUMA_ID_ANY);
371 : :
372 [ + + + + : 2661 : SPDK_DEBUGLOG(thread, "spdk_msg_mempool was created with size: %zu\n",
+ - ]
373 : : msg_mempool_sz);
374 : :
375 [ + + ]: 2661 : if (!g_spdk_msg_mempool) {
376 : 0 : SPDK_ERRLOG("spdk_msg_mempool creation failed\n");
377 : 0 : return -ENOMEM;
378 : : }
379 : :
380 : 2661 : return 0;
381 : 65 : }
382 : :
383 : : static void thread_interrupt_destroy(struct spdk_thread *thread);
384 : : static int thread_interrupt_create(struct spdk_thread *thread);
385 : :
386 : : static void
387 : 7306 : _free_thread(struct spdk_thread *thread)
388 : : {
389 : : struct spdk_io_channel *ch;
390 : : struct spdk_msg *msg;
391 : : struct spdk_poller *poller, *ptmp;
392 : :
393 [ + + - + ]: 7306 : RB_FOREACH(ch, io_channel_tree, &thread->io_channels) {
394 [ # # # # : 0 : SPDK_ERRLOG("thread %s still has channel for io_device %s\n",
# # # # ]
395 : : thread->name, ch->dev->name);
396 : 0 : }
397 : :
398 [ + + + - : 7306 : TAILQ_FOREACH_SAFE(poller, &thread->active_pollers, tailq, ptmp) {
+ - + - #
# # # # #
- + ]
399 [ # # # # : 0 : if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) {
# # ]
400 [ # # ]: 0 : SPDK_WARNLOG("active_poller %s still registered at thread exit\n",
401 : : poller->name);
402 : 0 : }
403 [ # # # # : 0 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
404 : 0 : free(poller);
405 : 0 : }
406 : :
407 [ + + + + : 15724 : RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, ptmp) {
+ + ]
408 [ + + + - : 8418 : if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) {
+ - ]
409 [ # # ]: 0 : SPDK_WARNLOG("timed_poller %s still registered at thread exit\n",
410 : : poller->name);
411 : 0 : }
412 [ + - ]: 8418 : RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller);
413 : 8418 : free(poller);
414 : 2296 : }
415 : :
416 [ + + + - : 7306 : TAILQ_FOREACH_SAFE(poller, &thread->paused_pollers, tailq, ptmp) {
+ - + - #
# # # # #
- + ]
417 [ # # ]: 0 : SPDK_WARNLOG("paused_poller %s still registered at thread exit\n", poller->name);
418 [ # # # # : 0 : TAILQ_REMOVE(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
419 : 0 : free(poller);
420 : 0 : }
421 : :
422 [ + + ]: 7306 : pthread_mutex_lock(&g_devlist_mutex);
423 [ + + # # ]: 7306 : assert(g_thread_count > 0);
424 : 7306 : g_thread_count--;
425 [ + + + - : 7306 : TAILQ_REMOVE(&g_threads, thread, tailq);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
426 [ + + ]: 7306 : pthread_mutex_unlock(&g_devlist_mutex);
427 : :
428 [ + - + - : 7306 : msg = SLIST_FIRST(&thread->msg_cache);
+ - ]
429 [ + + ]: 7441385 : while (msg != NULL) {
430 [ + - + - : 7434079 : SLIST_REMOVE_HEAD(&thread->msg_cache, link);
+ - + - +
- + - + -
+ - + - ]
431 : :
432 [ + + + - : 7434079 : assert(thread->msg_cache_count > 0);
+ - # # ]
433 [ + - ]: 7434079 : thread->msg_cache_count--;
434 : 7434079 : spdk_mempool_put(g_spdk_msg_mempool, msg);
435 : :
436 [ + - + - : 7434079 : msg = SLIST_FIRST(&thread->msg_cache);
+ - ]
437 : : }
438 : :
439 [ + + + - : 7306 : assert(thread->msg_cache_count == 0);
+ - # # ]
440 : :
441 [ + + ]: 7306 : if (spdk_interrupt_mode_is_enabled()) {
442 : 122 : thread_interrupt_destroy(thread);
443 : 0 : }
444 : :
445 [ + - + - ]: 7306 : spdk_ring_free(thread->messages);
446 : 7306 : free(thread);
447 : 7306 : }
448 : :
449 : : int
450 : 298 : spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn, size_t ctx_sz)
451 : : {
452 [ - + # # ]: 298 : assert(g_new_thread_fn == NULL);
453 [ - + # # ]: 298 : assert(g_thread_op_fn == NULL);
454 : :
455 [ + + ]: 298 : if (new_thread_fn == NULL) {
456 [ - + - + : 235 : SPDK_INFOLOG(thread, "new_thread_fn was not specified at spdk_thread_lib_init\n");
# # ]
457 : 0 : } else {
458 : 63 : g_new_thread_fn = new_thread_fn;
459 : : }
460 : :
461 : 298 : return _thread_lib_init(ctx_sz, SPDK_DEFAULT_MSG_MEMPOOL_SIZE);
462 : : }
463 : :
464 : : int
465 : 2363 : spdk_thread_lib_init_ext(spdk_thread_op_fn thread_op_fn,
466 : : spdk_thread_op_supported_fn thread_op_supported_fn,
467 : : size_t ctx_sz, size_t msg_mempool_sz)
468 : : {
469 [ + + # # ]: 2363 : assert(g_new_thread_fn == NULL);
470 [ + + # # ]: 2363 : assert(g_thread_op_fn == NULL);
471 [ + + # # ]: 2363 : assert(g_thread_op_supported_fn == NULL);
472 : :
473 [ - + ]: 2363 : if ((thread_op_fn != NULL) != (thread_op_supported_fn != NULL)) {
474 : 0 : SPDK_ERRLOG("Both must be defined or undefined together.\n");
475 : 0 : return -EINVAL;
476 : : }
477 : :
478 [ - + - - ]: 2363 : if (thread_op_fn == NULL && thread_op_supported_fn == NULL) {
479 [ # # # # : 0 : SPDK_INFOLOG(thread, "thread_op_fn and thread_op_supported_fn were not specified\n");
# # ]
480 : 0 : } else {
481 : 2363 : g_thread_op_fn = thread_op_fn;
482 : 2363 : g_thread_op_supported_fn = thread_op_supported_fn;
483 : : }
484 : :
485 : 2363 : return _thread_lib_init(ctx_sz, msg_mempool_sz);
486 : 65 : }
487 : :
488 : : void
489 : 2655 : spdk_thread_lib_fini(void)
490 : : {
491 : : struct io_device *dev;
492 : :
493 [ + + ]: 2658 : RB_FOREACH(dev, io_device_tree, &g_io_devices) {
494 [ # # ]: 3 : SPDK_ERRLOG("io_device %s not unregistered\n", dev->name);
495 : 0 : }
496 : :
497 : 2655 : g_new_thread_fn = NULL;
498 : 2655 : g_thread_op_fn = NULL;
499 : 2655 : g_thread_op_supported_fn = NULL;
500 : 2655 : g_ctx_sz = 0;
501 [ + + ]: 2655 : if (g_app_thread != NULL) {
502 : 2643 : _free_thread(g_app_thread);
503 : 2643 : g_app_thread = NULL;
504 : 65 : }
505 : :
506 [ + + ]: 2655 : if (g_spdk_msg_mempool) {
507 : 2655 : spdk_mempool_free(g_spdk_msg_mempool);
508 : 2655 : g_spdk_msg_mempool = NULL;
509 : 65 : }
510 : 2655 : }
511 : :
512 : : struct spdk_thread *
513 : 7417 : spdk_thread_create(const char *name, const struct spdk_cpuset *cpumask)
514 : : {
515 : 3345 : struct spdk_thread *thread, *null_thread;
516 : 7417 : size_t size = SPDK_ALIGN_CEIL(sizeof(*thread) + g_ctx_sz, SPDK_CACHE_LINE_SIZE);
517 : 3345 : struct spdk_msg *msgs[SPDK_MSG_MEMPOOL_CACHE_SIZE];
518 : 7417 : int rc = 0, i;
519 : :
520 : : /* Since this spdk_thread object will be used by another core, ensure that it won't share a
521 : : * cache line with any other object allocated on this core */
522 [ + + ]: 7417 : rc = posix_memalign((void **)&thread, SPDK_CACHE_LINE_SIZE, size);
523 [ - + ]: 7417 : if (rc != 0) {
524 : 0 : SPDK_ERRLOG("Unable to allocate memory for thread\n");
525 : 0 : return NULL;
526 : : }
527 [ + + ]: 7417 : memset(thread, 0, size);
528 : :
529 [ + + ]: 7417 : if (cpumask) {
530 [ + - ]: 4823 : spdk_cpuset_copy(&thread->cpumask, cpumask);
531 : 105 : } else {
532 [ + - ]: 2594 : spdk_cpuset_negate(&thread->cpumask);
533 : : }
534 : :
535 [ + - + - : 7417 : RB_INIT(&thread->io_channels);
+ - ]
536 [ + - + - : 7417 : TAILQ_INIT(&thread->active_pollers);
+ - + - +
- + - + -
+ - ]
537 [ + - + - : 7417 : RB_INIT(&thread->timed_pollers);
+ - ]
538 [ + - + - : 7417 : TAILQ_INIT(&thread->paused_pollers);
+ - + - +
- + - + -
+ - ]
539 [ + - + - : 7417 : SLIST_INIT(&thread->msg_cache);
+ - ]
540 [ + - + - ]: 7417 : thread->msg_cache_count = 0;
541 : :
542 [ + - + - ]: 7417 : thread->tsc_last = spdk_get_ticks();
543 : :
544 : : /* Monotonic increasing ID is set to each created poller beginning at 1. Once the
545 : : * ID exceeds UINT64_MAX a warning message is logged
546 : : */
547 [ + - + - ]: 7417 : thread->next_poller_id = 1;
548 : :
549 [ + - + - ]: 7417 : thread->messages = spdk_ring_create(SPDK_RING_TYPE_MP_SC, 65536, SPDK_ENV_NUMA_ID_ANY);
550 [ + + + - : 7417 : if (!thread->messages) {
- + ]
551 : 0 : SPDK_ERRLOG("Unable to allocate memory for message ring\n");
552 : 0 : free(thread);
553 : 0 : return NULL;
554 : : }
555 : :
556 : : /* Fill the local message pool cache. */
557 : 7417 : rc = spdk_mempool_get_bulk(g_spdk_msg_mempool, (void **)msgs, SPDK_MSG_MEMPOOL_CACHE_SIZE);
558 [ + - ]: 7417 : if (rc == 0) {
559 : : /* If we can't populate the cache it's ok. The cache will get filled
560 : : * up organically as messages are passed to the thread. */
561 [ + + + - ]: 7602425 : for (i = 0; i < SPDK_MSG_MEMPOOL_CACHE_SIZE; i++) {
562 [ + - + - : 7595008 : SLIST_INSERT_HEAD(&thread->msg_cache, msgs[i], link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
563 [ + - ]: 7595008 : thread->msg_cache_count++;
564 : 179200 : }
565 : 175 : }
566 : :
567 [ + + ]: 7417 : if (name) {
568 [ + + ]: 6894 : snprintf(thread->name, sizeof(thread->name), "%s", name);
569 : 175 : } else {
570 [ - + ]: 523 : snprintf(thread->name, sizeof(thread->name), "%p", thread);
571 : : }
572 : :
573 [ + - + - : 7417 : thread->trace_id = spdk_trace_register_owner(OWNER_TYPE_THREAD, thread->name);
+ - ]
574 : :
575 [ + + ]: 7417 : pthread_mutex_lock(&g_devlist_mutex);
576 [ - + ]: 7417 : if (g_thread_id == 0) {
577 : 0 : SPDK_ERRLOG("Thread ID rolled over. Further thread creation is not allowed.\n");
578 [ # # ]: 0 : pthread_mutex_unlock(&g_devlist_mutex);
579 : 0 : _free_thread(thread);
580 : 0 : return NULL;
581 : : }
582 [ + - + - ]: 7417 : thread->id = g_thread_id++;
583 [ + - + - : 7417 : TAILQ_INSERT_TAIL(&g_threads, thread, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
584 : 7417 : g_thread_count++;
585 [ + + ]: 7417 : pthread_mutex_unlock(&g_devlist_mutex);
586 : :
587 [ + + + + : 7417 : SPDK_DEBUGLOG(thread, "Allocating new thread (%" PRIu64 ", %s)\n",
+ - # # #
# # # ]
588 : : thread->id, thread->name);
589 : :
590 [ + + ]: 7417 : if (spdk_interrupt_mode_is_enabled()) {
591 [ # # # # ]: 122 : thread->in_interrupt = true;
592 : 122 : rc = thread_interrupt_create(thread);
593 [ - + ]: 122 : if (rc != 0) {
594 : 0 : _free_thread(thread);
595 : 0 : return NULL;
596 : : }
597 : 0 : }
598 : :
599 [ + + ]: 7417 : if (g_new_thread_fn) {
600 [ # # # # ]: 364 : rc = g_new_thread_fn(thread);
601 [ + + + + : 7053 : } else if (g_thread_op_supported_fn && g_thread_op_supported_fn(SPDK_THREAD_OP_NEW)) {
+ - - + ]
602 [ - + - + ]: 6478 : rc = g_thread_op_fn(thread, SPDK_THREAD_OP_NEW);
603 : 175 : }
604 : :
605 [ + + ]: 7417 : if (rc != 0) {
606 : 6 : _free_thread(thread);
607 : 6 : return NULL;
608 : : }
609 : :
610 [ + - + - ]: 7411 : thread->state = SPDK_THREAD_STATE_RUNNING;
611 : :
612 : : /* If this is the first thread, save it as the app thread. Use an atomic
613 : : * compare + exchange to guard against crazy users who might try to
614 : : * call spdk_thread_create() simultaneously on multiple threads.
615 : : */
616 : 7411 : null_thread = NULL;
617 [ + + + - ]: 7411 : __atomic_compare_exchange_n(&g_app_thread, &null_thread, thread, false,
618 : : __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
619 : :
620 : 7411 : return thread;
621 : 175 : }
622 : :
623 : : struct spdk_thread *
624 : 21214 : spdk_thread_get_app_thread(void)
625 : : {
626 : 21214 : return g_app_thread;
627 : : }
628 : :
629 : : bool
630 : 37126 : spdk_thread_is_app_thread(struct spdk_thread *thread)
631 : : {
632 [ + + ]: 37126 : if (thread == NULL) {
633 : 34791 : thread = _get_thread();
634 : 919 : }
635 : :
636 : 37126 : return g_app_thread == thread;
637 : : }
638 : :
639 : : void
640 : 19 : spdk_thread_bind(struct spdk_thread *thread, bool bind)
641 : : {
642 [ # # # # : 19 : thread->is_bound = bind;
# # ]
643 : 19 : }
644 : :
645 : : bool
646 : 120 : spdk_thread_is_bound(struct spdk_thread *thread)
647 : : {
648 [ + + + - : 120 : return thread->is_bound;
+ - ]
649 : : }
650 : :
651 : : void
652 : 469025206 : spdk_set_thread(struct spdk_thread *thread)
653 : : {
654 : 469025206 : tls_thread = thread;
655 : 469025206 : }
656 : :
657 : : static void
658 : 446434 : thread_exit(struct spdk_thread *thread, uint64_t now)
659 : : {
660 : : struct spdk_poller *poller;
661 : : struct spdk_io_channel *ch;
662 : :
663 [ + + + - : 446434 : if (now >= thread->exit_timeout_tsc) {
- + ]
664 [ # # ]: 3 : SPDK_ERRLOG("thread %s got timeout, and move it to the exited state forcefully\n",
665 : : thread->name);
666 : 3 : goto exited;
667 : : }
668 : :
669 [ + + + - : 446431 : if (spdk_ring_count(thread->messages) > 0) {
+ + ]
670 [ + + + + : 5073 : SPDK_INFOLOG(thread, "thread %s still has messages\n", thread->name);
+ - # # ]
671 : 5073 : return;
672 : : }
673 : :
674 [ + + + - : 441358 : if (thread->for_each_count > 0) {
- + ]
675 [ - + - + : 8120 : SPDK_INFOLOG(thread, "thread %s is still executing %u for_each_channels/threads\n",
# # # # #
# # # ]
676 : : thread->name, thread->for_each_count);
677 : 8120 : return;
678 : : }
679 : :
680 [ + + + - : 433239 : TAILQ_FOREACH(poller, &thread->active_pollers, tailq) {
+ - - + #
# # # #
# ]
681 [ + + # # : 425848 : if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) {
# # ]
682 [ - + - + : 425847 : SPDK_INFOLOG(thread,
# # # # #
# ]
683 : : "thread %s still has active poller %s\n",
684 : : thread->name, poller->name);
685 : 425847 : return;
686 : : }
687 : 0 : }
688 : :
689 [ + + + + ]: 16195 : RB_FOREACH(poller, timed_pollers_tree, &thread->timed_pollers) {
690 [ + + + - : 8804 : if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) {
+ - ]
691 [ # # # # : 0 : SPDK_INFOLOG(thread,
# # # # #
# ]
692 : : "thread %s still has active timed poller %s\n",
693 : : thread->name, poller->name);
694 : 0 : return;
695 : : }
696 : 2304 : }
697 : :
698 [ + + + - : 7391 : TAILQ_FOREACH(poller, &thread->paused_pollers, tailq) {
+ - - + #
# # # #
# ]
699 [ # # # # : 0 : SPDK_INFOLOG(thread,
# # # # #
# ]
700 : : "thread %s still has paused poller %s\n",
701 : : thread->name, poller->name);
702 : 0 : return;
703 : : }
704 : :
705 [ + + - + ]: 7391 : RB_FOREACH(ch, io_channel_tree, &thread->io_channels) {
706 [ - + - + : 18 : SPDK_INFOLOG(thread,
# # # # #
# # # #
# ]
707 : : "thread %s still has channel for io_device %s\n",
708 : : thread->name, ch->dev->name);
709 : 18 : return;
710 : : }
711 : :
712 [ + + + - : 7373 : if (thread->pending_unregister_count > 0) {
- + ]
713 [ - + - + : 6 : SPDK_INFOLOG(thread,
# # # # ]
714 : : "thread %s is still unregistering io_devices\n",
715 : : thread->name);
716 : 6 : return;
717 : : }
718 : :
719 : 7193 : exited:
720 [ + - + - ]: 7370 : thread->state = SPDK_THREAD_STATE_EXITED;
721 [ + + + + : 7370 : if (spdk_unlikely(thread->in_interrupt)) {
+ - + - ]
722 [ # # # # ]: 122 : g_thread_op_fn(thread, SPDK_THREAD_OP_RESCHED);
723 : 0 : }
724 : 242 : }
725 : :
726 : : static void _thread_exit(void *ctx);
727 : :
728 : : int
729 : 7420 : spdk_thread_exit(struct spdk_thread *thread)
730 : : {
731 [ + + + + : 7420 : SPDK_DEBUGLOG(thread, "Exit thread %s\n", thread->name);
+ - # # ]
732 : :
733 [ + + # # ]: 7420 : assert(tls_thread == thread);
734 : :
735 [ + + + - : 7420 : if (thread->state >= SPDK_THREAD_STATE_EXITING) {
- + ]
736 [ - + - + : 43 : SPDK_INFOLOG(thread,
# # # # ]
737 : : "thread %s is already exiting\n",
738 : : thread->name);
739 : 43 : return 0;
740 : : }
741 : :
742 [ + - + - ]: 7377 : thread->exit_timeout_tsc = spdk_get_ticks() + (spdk_get_ticks_hz() *
743 : : SPDK_THREAD_EXIT_TIMEOUT_SEC);
744 [ + - + - ]: 7377 : thread->state = SPDK_THREAD_STATE_EXITING;
745 : :
746 [ + + ]: 7377 : if (spdk_interrupt_mode_is_enabled()) {
747 : 122 : spdk_thread_send_msg(thread, _thread_exit, thread);
748 : 0 : }
749 : :
750 : 7377 : return 0;
751 : 175 : }
752 : :
753 : : bool
754 : 3512 : spdk_thread_is_running(struct spdk_thread *thread)
755 : : {
756 [ + - + - ]: 3512 : return thread->state == SPDK_THREAD_STATE_RUNNING;
757 : : }
758 : :
759 : : bool
760 : 9847771850 : spdk_thread_is_exited(struct spdk_thread *thread)
761 : : {
762 [ + - + - ]: 9847771850 : return thread->state == SPDK_THREAD_STATE_EXITED;
763 : : }
764 : :
765 : : void
766 : 7313 : spdk_thread_destroy(struct spdk_thread *thread)
767 : : {
768 [ + + # # ]: 7313 : assert(thread != NULL);
769 [ + + + + : 7313 : SPDK_DEBUGLOG(thread, "Destroy thread %s\n", thread->name);
+ - # # ]
770 : :
771 [ + + + - : 7313 : assert(thread->state == SPDK_THREAD_STATE_EXITED);
+ - # # ]
772 : :
773 [ + + ]: 7313 : if (tls_thread == thread) {
774 : 4030 : tls_thread = NULL;
775 : 118 : }
776 : :
777 : : /* To be safe, do not free the app thread until spdk_thread_lib_fini(). */
778 [ + + ]: 7313 : if (thread != g_app_thread) {
779 : 4656 : _free_thread(thread);
780 : 109 : }
781 : 7313 : }
782 : :
783 : : void *
784 : 37554 : spdk_thread_get_ctx(struct spdk_thread *thread)
785 : : {
786 [ + - ]: 37554 : if (g_ctx_sz > 0) {
787 [ - + ]: 37554 : return thread->ctx;
788 : : }
789 : :
790 : 0 : return NULL;
791 : 352 : }
792 : :
793 : : struct spdk_cpuset *
794 : 28917 : spdk_thread_get_cpumask(struct spdk_thread *thread)
795 : : {
796 [ + - ]: 28917 : return &thread->cpumask;
797 : : }
798 : :
799 : : int
800 : 11962 : spdk_thread_set_cpumask(struct spdk_cpuset *cpumask)
801 : : {
802 : : struct spdk_thread *thread;
803 : :
804 [ + - - + : 11962 : if (!g_thread_op_supported_fn || !g_thread_op_supported_fn(SPDK_THREAD_OP_RESCHED)) {
# # ]
805 : 0 : SPDK_ERRLOG("Framework does not support reschedule operation.\n");
806 [ # # ]: 0 : assert(false);
807 : : return -ENOTSUP;
808 : : }
809 : :
810 : 11962 : thread = spdk_get_thread();
811 [ - + ]: 11962 : if (!thread) {
812 : 0 : SPDK_ERRLOG("Called from non-SPDK thread\n");
813 [ # # ]: 0 : assert(false);
814 : : return -EINVAL;
815 : : }
816 : :
817 [ # # ]: 11962 : spdk_cpuset_copy(&thread->cpumask, cpumask);
818 : :
819 : : /* Invoke framework's reschedule operation. If this function is called multiple times
820 : : * in a single spdk_thread_poll() context, the last cpumask will be used in the
821 : : * reschedule operation.
822 : : */
823 [ # # # # ]: 11962 : g_thread_op_fn(thread, SPDK_THREAD_OP_RESCHED);
824 : :
825 : 11962 : return 0;
826 : : }
827 : :
828 : : struct spdk_thread *
829 :19644254188 : spdk_thread_get_from_ctx(void *ctx)
830 : : {
831 [ + + ]:19644254188 : if (ctx == NULL) {
832 [ # # ]: 0 : assert(false);
833 : : return NULL;
834 : : }
835 : :
836 [ + + # # ]:19644254188 : assert(g_ctx_sz > 0);
837 : :
838 :19644254188 : return SPDK_CONTAINEROF(ctx, struct spdk_thread, ctx);
839 : : }
840 : :
841 : : static inline uint32_t
842 :10059473586 : msg_queue_run_batch(struct spdk_thread *thread, uint32_t max_msgs)
843 : : {
844 : : unsigned count, i;
845 : 2973660594 : void *messages[SPDK_MSG_BATCH_SIZE];
846 :10059473586 : uint64_t notify = 1;
847 : : int rc;
848 : :
849 : : #ifdef DEBUG
850 : : /*
851 : : * spdk_ring_dequeue() fills messages and returns how many entries it wrote,
852 : : * so we will never actually read uninitialized data from events, but just to be sure
853 : : * (and to silence a static analyzer false positive), initialize the array to NULL pointers.
854 : : */
855 [ + - ]:10059473586 : memset(messages, 0, sizeof(messages));
856 : : #endif
857 : :
858 [ + + ]:10059473586 : if (max_msgs > 0) {
859 [ # # ]: 1095 : max_msgs = spdk_min(max_msgs, SPDK_MSG_BATCH_SIZE);
860 : 0 : } else {
861 :10059472491 : max_msgs = SPDK_MSG_BATCH_SIZE;
862 : : }
863 : :
864 [ + - + - ]:10059473586 : count = spdk_ring_dequeue(thread->messages, messages, max_msgs);
865 [ + + + + :10060232171 : if (spdk_unlikely(thread->in_interrupt) &&
+ + - + #
# ]
866 [ # # # # ]: 758585 : spdk_ring_count(thread->messages) != 0) {
867 [ # # # # ]: 8382 : rc = write(thread->msg_fd, ¬ify, sizeof(notify));
868 [ - + ]: 8382 : if (rc < 0) {
869 [ # # ]: 0 : SPDK_ERRLOG("failed to notify msg_queue: %s.\n", spdk_strerror(errno));
870 : 0 : }
871 : 0 : }
872 [ + + ]:10059473586 : if (count == 0) {
873 :10050405924 : return 0;
874 : : }
875 : :
876 [ + + ]: 28300705 : for (i = 0; i < count; i++) {
877 [ + - + - : 19233043 : struct spdk_msg *msg = messages[i];
+ - ]
878 : :
879 [ + + # # ]: 19233043 : assert(msg != NULL);
880 : :
881 : 1699363 : SPDK_DTRACE_PROBE2(msg_exec, msg->fn, msg->arg);
882 : :
883 [ + - + - : 19233043 : msg->fn(msg->arg);
- + + - +
- + - ]
884 : :
885 [ + + + - : 19233043 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
+ + + - +
- + - - +
+ - ]
886 : :
887 [ + + + - : 19233043 : if (thread->msg_cache_count < SPDK_MSG_MEMPOOL_CACHE_SIZE) {
+ + ]
888 : : /* Insert the messages at the head. We want to re-use the hot
889 : : * ones. */
890 [ + - + - : 14060414 : SLIST_INSERT_HEAD(&thread->msg_cache, msg, link);
+ - + - +
- + - + -
+ - + - ]
891 [ + - ]: 14060414 : thread->msg_cache_count++;
892 : 14677 : } else {
893 : 5172629 : spdk_mempool_put(g_spdk_msg_mempool, msg);
894 : : }
895 : 16347 : }
896 : :
897 : 9067662 : return count;
898 : 122369340 : }
899 : :
900 : : static void
901 : 26035705 : poller_insert_timer(struct spdk_thread *thread, struct spdk_poller *poller, uint64_t now)
902 : : {
903 : : struct spdk_poller *tmp __attribute__((unused));
904 : :
905 [ + - + - : 26035705 : poller->next_run_tick = now + poller->period_ticks;
+ - + - ]
906 : :
907 : : /*
908 : : * Insert poller in the thread's timed_pollers tree by next scheduled run time
909 : : * as its key.
910 : : */
911 [ + - ]: 26035705 : tmp = RB_INSERT(timed_pollers_tree, &thread->timed_pollers, poller);
912 [ + + # # ]: 26035705 : assert(tmp == NULL);
913 : :
914 : : /* Update the cache only if it is empty or the inserted poller is earlier than it.
915 : : * RB_MIN() is not necessary here because all pollers, which has exactly the same
916 : : * next_run_tick as the existing poller, are inserted on the right side.
917 : : */
918 [ + + + - : 26919104 : if (thread->first_timed_poller == NULL ||
+ + + + ]
919 [ + + + - : 24326649 : poller->next_run_tick < thread->first_timed_poller->next_run_tick) {
+ - + - +
- + - ]
920 [ + - + - ]: 6434630 : thread->first_timed_poller = poller;
921 : 50030 : }
922 : 26035705 : }
923 : :
924 : : static inline void
925 : 244 : poller_remove_timer(struct spdk_thread *thread, struct spdk_poller *poller)
926 : : {
927 : : struct spdk_poller *tmp __attribute__((unused));
928 : :
929 [ # # ]: 244 : tmp = RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller);
930 [ - + # # ]: 244 : assert(tmp != NULL);
931 : :
932 : : /* This function is not used in any case that is performance critical.
933 : : * Update the cache simply by RB_MIN() if it needs to be changed.
934 : : */
935 [ + + # # : 244 : if (thread->first_timed_poller == poller) {
# # ]
936 [ # # # # : 230 : thread->first_timed_poller = RB_MIN(timed_pollers_tree, &thread->timed_pollers);
# # ]
937 : 0 : }
938 : 244 : }
939 : :
940 : : static void
941 : 169217 : thread_insert_poller(struct spdk_thread *thread, struct spdk_poller *poller)
942 : : {
943 [ + + + - : 169217 : if (poller->period_ticks) {
+ + ]
944 : 107193 : poller_insert_timer(thread, poller, spdk_get_ticks());
945 : 3784 : } else {
946 [ + - + - : 62024 : TAILQ_INSERT_TAIL(&thread->active_pollers, poller, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
947 : : }
948 : 169217 : }
949 : :
950 : : static inline void
951 :10058604613 : thread_update_stats(struct spdk_thread *thread, uint64_t end,
952 : : uint64_t start, int rc)
953 : : {
954 [ + + ]:10058604613 : if (rc == 0) {
955 : : /* Poller status idle */
956 [ - + - + : 9854130945 : thread->stats.idle_tsc += end - start;
- + ]
957 [ + + ]: 325883406 : } else if (rc > 0) {
958 : : /* Poller status busy */
959 [ + - + - : 204473668 : thread->stats.busy_tsc += end - start;
+ - ]
960 : 849210 : }
961 : : /* Store end time to use it as start time of the next spdk_thread_poll(). */
962 [ + - + - ]:10058604613 : thread->tsc_last = end;
963 :10058604613 : }
964 : :
965 : : static inline int
966 : 8710306992 : thread_execute_poller(struct spdk_thread *thread, struct spdk_poller *poller)
967 : : {
968 : : int rc;
969 : :
970 [ + + + - : 8710306992 : switch (poller->state) {
- + - + ]
971 : 40551 : case SPDK_POLLER_STATE_UNREGISTERED:
972 [ + + + - : 41753 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
973 : 41753 : free(poller);
974 : 41753 : return 0;
975 : 15 : case SPDK_POLLER_STATE_PAUSING:
976 [ - + # # : 15 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
977 [ # # # # : 15 : TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
978 [ # # # # ]: 15 : poller->state = SPDK_POLLER_STATE_PAUSED;
979 : 15 : return 0;
980 : 8632002391 : case SPDK_POLLER_STATE_WAITING:
981 : 8710265224 : break;
982 : 0 : default:
983 [ # # ]: 0 : assert(false);
984 : : break;
985 : : }
986 : :
987 [ + - + - ]: 8710265224 : poller->state = SPDK_POLLER_STATE_RUNNING;
988 [ + - + - : 8710265224 : rc = poller->fn(poller->arg);
+ + + - +
- + - ]
989 : :
990 [ + + + - : 8710265224 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
+ + + - +
- + - - +
+ - ]
991 : :
992 [ + - ]: 8710265224 : poller->run_count++;
993 [ + + ]: 8710265224 : if (rc > 0) {
994 [ + - ]: 364298431 : poller->busy_count++;
995 : 12570556 : }
996 : :
997 : : #ifdef DEBUG
998 [ + + ]: 8710265224 : if (rc == -1) {
999 [ + + + + : 63 : SPDK_DEBUGLOG(thread, "Poller %s returned -1\n", poller->name);
+ - # # ]
1000 : 1 : }
1001 : : #endif
1002 : :
1003 [ + + + + : 8710265224 : switch (poller->state) {
- - + -
+ ]
1004 : 18542 : case SPDK_POLLER_STATE_UNREGISTERED:
1005 [ + + + - : 18714 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
1006 : 18714 : free(poller);
1007 : 18714 : break;
1008 : 21 : case SPDK_POLLER_STATE_PAUSING:
1009 [ - + # # : 21 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1010 [ # # # # : 21 : TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1011 [ # # # # ]: 21 : poller->state = SPDK_POLLER_STATE_PAUSED;
1012 : 21 : break;
1013 : 0 : case SPDK_POLLER_STATE_PAUSED:
1014 : : case SPDK_POLLER_STATE_WAITING:
1015 : 0 : break;
1016 : 8631983828 : case SPDK_POLLER_STATE_RUNNING:
1017 [ + - + - ]: 8710246489 : poller->state = SPDK_POLLER_STATE_WAITING;
1018 : 8710246489 : break;
1019 : 0 : default:
1020 [ # # ]: 0 : assert(false);
1021 : : break;
1022 : : }
1023 : :
1024 : 8710265224 : return rc;
1025 : 78264035 : }
1026 : :
1027 : : static inline int
1028 : 26026982 : thread_execute_timed_poller(struct spdk_thread *thread, struct spdk_poller *poller,
1029 : : uint64_t now)
1030 : : {
1031 : : int rc;
1032 : :
1033 [ + + + - : 26026982 : switch (poller->state) {
- - + + ]
1034 : 37364 : case SPDK_POLLER_STATE_UNREGISTERED:
1035 : 38575 : free(poller);
1036 : 38575 : return 0;
1037 : 84 : case SPDK_POLLER_STATE_PAUSING:
1038 [ # # # # : 84 : TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1039 [ # # # # ]: 84 : poller->state = SPDK_POLLER_STATE_PAUSED;
1040 : 84 : return 0;
1041 : 25084470 : case SPDK_POLLER_STATE_WAITING:
1042 : 25988323 : break;
1043 : 0 : default:
1044 [ # # ]: 0 : assert(false);
1045 : : break;
1046 : : }
1047 : :
1048 [ + - + - ]: 25988323 : poller->state = SPDK_POLLER_STATE_RUNNING;
1049 [ + - + - : 25988323 : rc = poller->fn(poller->arg);
- + + - +
- + - ]
1050 : :
1051 [ + + + - : 25988323 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
+ + + - +
- + - - +
+ - ]
1052 : :
1053 [ + - ]: 25988323 : poller->run_count++;
1054 [ + + ]: 25988323 : if (rc > 0) {
1055 [ + - ]: 20238995 : poller->busy_count++;
1056 : 872718 : }
1057 : :
1058 : : #ifdef DEBUG
1059 [ + + ]: 25988323 : if (rc == -1) {
1060 [ + + + + : 2010 : SPDK_DEBUGLOG(thread, "Timed poller %s returned -1\n", poller->name);
+ - # # ]
1061 : 15 : }
1062 : : #endif
1063 : :
1064 [ + + + + : 25988323 : switch (poller->state) {
- - + - -
+ ]
1065 : 59527 : case SPDK_POLLER_STATE_UNREGISTERED:
1066 : 59803 : free(poller);
1067 : 59803 : break;
1068 : 12 : case SPDK_POLLER_STATE_PAUSING:
1069 [ # # # # : 12 : TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1070 [ # # # # ]: 12 : poller->state = SPDK_POLLER_STATE_PAUSED;
1071 : 12 : break;
1072 : 0 : case SPDK_POLLER_STATE_PAUSED:
1073 : 0 : break;
1074 : 25024931 : case SPDK_POLLER_STATE_RUNNING:
1075 [ + - + - ]: 25928508 : poller->state = SPDK_POLLER_STATE_WAITING;
1076 : : /* fallthrough */
1077 : 25024931 : case SPDK_POLLER_STATE_WAITING:
1078 : 25928508 : poller_insert_timer(thread, poller, now);
1079 : 25928508 : break;
1080 : 0 : default:
1081 [ # # ]: 0 : assert(false);
1082 : : break;
1083 : : }
1084 : :
1085 : 25988323 : return rc;
1086 : 905064 : }
1087 : :
1088 : : static inline void
1089 : 0 : thread_run_pp_handlers(struct spdk_thread *thread)
1090 : : {
1091 [ # # # # ]: 0 : uint8_t i, count = thread->num_pp_handlers;
1092 : :
1093 : : /* Set to max value to prevent new handlers registration within the callback */
1094 [ # # # # ]: 0 : thread->num_pp_handlers = SPDK_THREAD_MAX_POST_POLLER_HANDLERS;
1095 : :
1096 [ # # ]: 0 : for (i = 0; i < count; i++) {
1097 [ # # # # : 0 : thread->pp_handlers[i].fn(thread->pp_handlers[i].fn_arg);
# # # # #
# # # # #
# # # # #
# # # #
# ]
1098 [ # # # # : 0 : thread->pp_handlers[i].fn = NULL;
# # # # #
# ]
1099 : 0 : }
1100 : :
1101 [ # # # # ]: 0 : thread->num_pp_handlers = 0;
1102 : 0 : }
1103 : :
1104 : : static int
1105 :10058523373 : thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now)
1106 : : {
1107 : : uint32_t msg_count;
1108 : : struct spdk_poller *poller, *tmp;
1109 : : spdk_msg_fn critical_msg;
1110 :10058523373 : int rc = 0;
1111 : :
1112 [ + - + - ]:10058523373 : thread->tsc_last = now;
1113 : :
1114 [ + - + - ]:10058523373 : critical_msg = thread->critical_msg;
1115 [ + + ]:10058523373 : if (spdk_unlikely(critical_msg != NULL)) {
1116 [ - + + - ]: 1327 : critical_msg(NULL);
1117 [ + - + - ]: 1327 : thread->critical_msg = NULL;
1118 : 1327 : rc = 1;
1119 : 26 : }
1120 : :
1121 :10058523373 : msg_count = msg_queue_run_batch(thread, max_msgs);
1122 [ + + ]:10058523373 : if (msg_count) {
1123 : 8069442 : rc = 1;
1124 : 15961 : }
1125 : :
1126 [ + + + - :18768819742 : TAILQ_FOREACH_REVERSE_SAFE(poller, &thread->active_pollers,
+ - + - +
- + - + +
+ - + - +
- + - + -
+ - + + ]
1127 : : active_pollers_head, tailq, tmp) {
1128 : : int poller_rc;
1129 : :
1130 : 8710296369 : poller_rc = thread_execute_poller(thread, poller);
1131 [ + + ]: 8710296369 : if (poller_rc > rc) {
1132 : 190623647 : rc = poller_rc;
1133 : 791230 : }
1134 [ + + + - : 8710296369 : if (thread->num_pp_handlers) {
- + ]
1135 : 0 : thread_run_pp_handlers(thread);
1136 : 0 : }
1137 : 78253412 : }
1138 : :
1139 [ + - + - ]:10058523373 : poller = thread->first_timed_poller;
1140 [ + + ]:10084550355 : while (poller != NULL) {
1141 : 7681614787 : int timer_rc = 0;
1142 : :
1143 [ + + + - : 7681614787 : if (now < poller->next_run_tick) {
+ + ]
1144 : 7655587805 : break;
1145 : : }
1146 : :
1147 : 26026982 : tmp = RB_NEXT(timed_pollers_tree, &thread->timed_pollers, poller);
1148 [ + - ]: 26026982 : RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller);
1149 : :
1150 : : /* Update the cache to the next timed poller in the list
1151 : : * only if the current poller is still the closest, otherwise,
1152 : : * do nothing because the cache has been already updated.
1153 : : */
1154 [ + - + - : 26026982 : if (thread->first_timed_poller == poller) {
- + ]
1155 [ + - + - ]: 26026982 : thread->first_timed_poller = tmp;
1156 : 905064 : }
1157 : :
1158 : 26026982 : timer_rc = thread_execute_timed_poller(thread, poller, now);
1159 [ + + ]: 26026982 : if (timer_rc > rc) {
1160 : 5792590 : rc = timer_rc;
1161 : 42019 : }
1162 : :
1163 : 26026982 : poller = tmp;
1164 : : }
1165 : :
1166 :10058523373 : return rc;
1167 : : }
1168 : :
1169 : : static void
1170 : 1663 : _thread_remove_pollers(void *ctx)
1171 : : {
1172 : 1663 : struct spdk_thread *thread = ctx;
1173 : : struct spdk_poller *poller, *tmp;
1174 : :
1175 [ + + # # : 7044 : TAILQ_FOREACH_REVERSE_SAFE(poller, &thread->active_pollers,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1176 : : active_pollers_head, tailq, tmp) {
1177 [ + + # # : 5381 : if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) {
# # ]
1178 [ + + # # : 1542 : TAILQ_REMOVE(&thread->active_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1179 : 1542 : free(poller);
1180 : 0 : }
1181 : 0 : }
1182 : :
1183 [ + + + - : 2920 : RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, tmp) {
# # ]
1184 [ + + # # : 1257 : if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) {
# # ]
1185 : 240 : poller_remove_timer(thread, poller);
1186 : 240 : free(poller);
1187 : 0 : }
1188 : 0 : }
1189 : :
1190 [ # # # # ]: 1663 : thread->poller_unregistered = false;
1191 : 1663 : }
1192 : :
1193 : : static void
1194 : 245 : _thread_exit(void *ctx)
1195 : : {
1196 : 245 : struct spdk_thread *thread = ctx;
1197 : :
1198 [ - + # # : 245 : assert(thread->state == SPDK_THREAD_STATE_EXITING);
# # # # ]
1199 : :
1200 : 245 : thread_exit(thread, spdk_get_ticks());
1201 : :
1202 [ + + # # : 245 : if (thread->state != SPDK_THREAD_STATE_EXITED) {
# # ]
1203 : 123 : spdk_thread_send_msg(thread, _thread_exit, thread);
1204 : 0 : }
1205 : 245 : }
1206 : :
1207 : : int
1208 :10058507533 : spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now)
1209 : : {
1210 : : struct spdk_thread *orig_thread;
1211 : : int rc;
1212 : :
1213 :10058507533 : orig_thread = _get_thread();
1214 :10058507533 : tls_thread = thread;
1215 : :
1216 [ + + ]:10058507533 : if (now == 0) {
1217 : 244527359 : now = spdk_get_ticks();
1218 : 65 : }
1219 : :
1220 [ + + + + :10058507533 : if (spdk_likely(!thread->in_interrupt)) {
+ - - + ]
1221 :10058507529 : rc = thread_poll(thread, max_msgs, now);
1222 [ + + + + :10058507529 : if (spdk_unlikely(thread->in_interrupt)) {
+ - + - ]
1223 : : /* The thread transitioned to interrupt mode during the above poll.
1224 : : * Poll it one more time in case that during the transition time
1225 : : * there is msg received without notification.
1226 : : */
1227 : 4 : rc = thread_poll(thread, max_msgs, now);
1228 : 0 : }
1229 : :
1230 [ + + + - :10058507529 : if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITING)) {
+ + ]
1231 : 446190 : thread_exit(thread, now);
1232 : 243 : }
1233 : 122161868 : } else {
1234 : : /* Non-block wait on thread's fd_group */
1235 [ # # # # ]: 4 : rc = spdk_fd_group_wait(thread->fgrp, 0);
1236 : : }
1237 : :
1238 :10058507533 : thread_update_stats(thread, spdk_get_ticks(), now, rc);
1239 : :
1240 :10058507533 : tls_thread = orig_thread;
1241 : :
1242 :10058507533 : return rc;
1243 : : }
1244 : :
1245 : : uint64_t
1246 : 24224 : spdk_thread_next_poller_expiration(struct spdk_thread *thread)
1247 : : {
1248 : : struct spdk_poller *poller;
1249 : :
1250 [ # # # # ]: 24224 : poller = thread->first_timed_poller;
1251 [ + + ]: 24224 : if (poller) {
1252 [ # # # # ]: 24031 : return poller->next_run_tick;
1253 : : }
1254 : :
1255 : 193 : return 0;
1256 : 0 : }
1257 : :
1258 : : int
1259 : 1816685 : spdk_thread_has_active_pollers(struct spdk_thread *thread)
1260 : : {
1261 [ # # # # : 1816685 : return !TAILQ_EMPTY(&thread->active_pollers);
# # ]
1262 : : }
1263 : :
1264 : : static bool
1265 : 58586581 : thread_has_unpaused_pollers(struct spdk_thread *thread)
1266 : : {
1267 [ + - + - : 60213264 : if (TAILQ_EMPTY(&thread->active_pollers) &&
+ - + + +
+ ]
1268 [ + + + - : 58579438 : RB_EMPTY(&thread->timed_pollers)) {
+ - ]
1269 : 2976 : return false;
1270 : : }
1271 : :
1272 : 58583605 : return true;
1273 : 1633826 : }
1274 : :
1275 : : bool
1276 : 6 : spdk_thread_has_pollers(struct spdk_thread *thread)
1277 : : {
1278 [ + - # # ]: 6 : if (!thread_has_unpaused_pollers(thread) &&
1279 [ + - # # : 6 : TAILQ_EMPTY(&thread->paused_pollers)) {
# # ]
1280 : 6 : return false;
1281 : : }
1282 : :
1283 : 0 : return true;
1284 : 0 : }
1285 : :
1286 : : bool
1287 : 58590447 : spdk_thread_is_idle(struct spdk_thread *thread)
1288 : : {
1289 [ + - + + : 115543252 : if (spdk_ring_count(thread->messages) ||
+ + - + ]
1290 [ + + ]: 58588467 : thread_has_unpaused_pollers(thread) ||
1291 [ + + + - ]: 2970 : thread->critical_msg != NULL) {
1292 : 58587477 : return false;
1293 : : }
1294 : :
1295 : 2970 : return true;
1296 : 1633738 : }
1297 : :
1298 : : uint32_t
1299 : 384 : spdk_thread_get_count(void)
1300 : : {
1301 : : /*
1302 : : * Return cached value of the current thread count. We could acquire the
1303 : : * lock and iterate through the TAILQ of threads to count them, but that
1304 : : * count could still be invalidated after we release the lock.
1305 : : */
1306 : 384 : return g_thread_count;
1307 : : }
1308 : :
1309 : : struct spdk_thread *
1310 : 820640781 : spdk_get_thread(void)
1311 : : {
1312 : 820640781 : return _get_thread();
1313 : : }
1314 : :
1315 : : const char *
1316 : 1232 : spdk_thread_get_name(const struct spdk_thread *thread)
1317 : : {
1318 [ + - ]: 1232 : return thread->name;
1319 : : }
1320 : :
1321 : : uint64_t
1322 : 89168 : spdk_thread_get_id(const struct spdk_thread *thread)
1323 : : {
1324 [ + - + - ]: 89168 : return thread->id;
1325 : : }
1326 : :
1327 : : struct spdk_thread *
1328 : 672 : spdk_thread_get_by_id(uint64_t id)
1329 : : {
1330 : : struct spdk_thread *thread;
1331 : :
1332 [ + - - + ]: 672 : if (id == 0 || id >= g_thread_id) {
1333 : 0 : SPDK_ERRLOG("invalid thread id: %" PRIu64 ".\n", id);
1334 : 0 : return NULL;
1335 : : }
1336 [ + + ]: 672 : pthread_mutex_lock(&g_devlist_mutex);
1337 [ + + + - : 3245 : TAILQ_FOREACH(thread, &g_threads, tailq) {
+ - + - ]
1338 [ + + + - : 3171 : if (thread->id == id) {
+ + ]
1339 : 598 : break;
1340 : : }
1341 : 84 : }
1342 [ + + ]: 672 : pthread_mutex_unlock(&g_devlist_mutex);
1343 : 672 : return thread;
1344 : 19 : }
1345 : :
1346 : : int
1347 : 8461 : spdk_thread_get_stats(struct spdk_thread_stats *stats)
1348 : : {
1349 : : struct spdk_thread *thread;
1350 : :
1351 : 8461 : thread = _get_thread();
1352 [ + + ]: 8461 : if (!thread) {
1353 : 0 : SPDK_ERRLOG("No thread allocated\n");
1354 : 0 : return -EINVAL;
1355 : : }
1356 : :
1357 [ + + ]: 8461 : if (stats == NULL) {
1358 : 0 : return -EINVAL;
1359 : : }
1360 : :
1361 [ + - ]: 8461 : *stats = thread->stats;
1362 : :
1363 : 8461 : return 0;
1364 : 203 : }
1365 : :
1366 : : uint64_t
1367 : 9897795632 : spdk_thread_get_last_tsc(struct spdk_thread *thread)
1368 : : {
1369 [ + + ]: 9897795632 : if (thread == NULL) {
1370 : 0 : thread = _get_thread();
1371 : 0 : }
1372 : :
1373 [ + - + - ]: 9897795632 : return thread->tsc_last;
1374 : : }
1375 : :
1376 : : static inline int
1377 : 19234433 : thread_send_msg_notification(const struct spdk_thread *target_thread)
1378 : : {
1379 : 19234433 : uint64_t notify = 1;
1380 : : int rc;
1381 : :
1382 : : /* Not necessary to do notification if interrupt facility is not enabled */
1383 [ + + ]: 19234433 : if (spdk_likely(!spdk_interrupt_mode_is_enabled())) {
1384 : 18002152 : return 0;
1385 : : }
1386 : :
1387 : : /* When each spdk_thread can switch between poll and interrupt mode dynamically,
1388 : : * after sending thread msg, it is necessary to check whether target thread runs in
1389 : : * interrupt mode and then decide whether do event notification.
1390 : : */
1391 [ + + + + : 1232281 : if (spdk_unlikely(target_thread->in_interrupt)) {
# # # # ]
1392 [ # # # # ]: 1232265 : rc = write(target_thread->msg_fd, ¬ify, sizeof(notify));
1393 [ - + ]: 1232265 : if (rc < 0) {
1394 [ # # ]: 0 : SPDK_ERRLOG("failed to notify msg_queue: %s.\n", spdk_strerror(errno));
1395 : 0 : return -EIO;
1396 : : }
1397 : 0 : }
1398 : :
1399 : 1232281 : return 0;
1400 : 16368 : }
1401 : :
1402 : : int
1403 : 19233066 : spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx)
1404 : : {
1405 : : struct spdk_thread *local_thread;
1406 : 7025131 : struct spdk_msg *msg;
1407 : : int rc;
1408 : :
1409 [ + + # # ]: 19233066 : assert(thread != NULL);
1410 : :
1411 [ + + + - : 19233066 : if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) {
- + ]
1412 [ # # ]: 0 : SPDK_ERRLOG("Thread %s is marked as exited.\n", thread->name);
1413 : 0 : return -EIO;
1414 : : }
1415 : :
1416 : 19233066 : local_thread = _get_thread();
1417 : :
1418 : 19233066 : msg = NULL;
1419 [ + + ]: 19233066 : if (local_thread != NULL) {
1420 [ + + + - : 14152127 : if (local_thread->msg_cache_count > 0) {
- + ]
1421 [ + - + - : 14106550 : msg = SLIST_FIRST(&local_thread->msg_cache);
+ - ]
1422 [ + + # # ]: 14106550 : assert(msg != NULL);
1423 [ + - + - : 14106550 : SLIST_REMOVE_HEAD(&local_thread->msg_cache, link);
+ - + - +
- + - + -
+ - + - ]
1424 [ + - ]: 14106550 : local_thread->msg_cache_count--;
1425 : 16211 : }
1426 : 16211 : }
1427 : :
1428 [ + + ]: 19233066 : if (msg == NULL) {
1429 : 5126516 : msg = spdk_mempool_get(g_spdk_msg_mempool);
1430 [ + + ]: 5126516 : if (!msg) {
1431 : 0 : SPDK_ERRLOG("msg could not be allocated\n");
1432 : 0 : return -ENOMEM;
1433 : : }
1434 : 132 : }
1435 : :
1436 [ + - + - ]: 19233066 : msg->fn = fn;
1437 [ + - + - ]: 19233066 : msg->arg = ctx;
1438 : :
1439 [ + - + - ]: 19233066 : rc = spdk_ring_enqueue(thread->messages, (void **)&msg, 1, NULL);
1440 [ - + ]: 19233066 : if (rc != 1) {
1441 : 0 : SPDK_ERRLOG("msg could not be enqueued\n");
1442 : 0 : spdk_mempool_put(g_spdk_msg_mempool, msg);
1443 : 0 : return -EIO;
1444 : : }
1445 : :
1446 : 19233066 : return thread_send_msg_notification(thread);
1447 : 16343 : }
1448 : :
1449 : : int
1450 : 1368 : spdk_thread_send_critical_msg(struct spdk_thread *thread, spdk_msg_fn fn)
1451 : : {
1452 : 1368 : spdk_msg_fn expected = NULL;
1453 : :
1454 [ + + + - : 1368 : if (!__atomic_compare_exchange_n(&thread->critical_msg, &expected, fn, false, __ATOMIC_SEQ_CST,
+ - + - ]
1455 : : __ATOMIC_SEQ_CST)) {
1456 : 0 : return -EIO;
1457 : : }
1458 : :
1459 : 1368 : return thread_send_msg_notification(thread);
1460 : 26 : }
1461 : :
1462 : : #ifdef __linux__
1463 : : static int
1464 : 94662 : interrupt_timerfd_process(void *arg)
1465 : : {
1466 : 94662 : struct spdk_poller *poller = arg;
1467 : 6180 : uint64_t exp;
1468 : : int rc;
1469 : :
1470 : : /* clear the level of interval timer */
1471 [ # # # # : 94662 : rc = read(poller->intr->efd, &exp, sizeof(exp));
# # # # ]
1472 [ - + ]: 94662 : if (rc < 0) {
1473 [ # # ]: 0 : if (rc == -EAGAIN) {
1474 : 0 : return 0;
1475 : : }
1476 : :
1477 : 0 : return rc;
1478 : : }
1479 : :
1480 : 40588 : SPDK_DTRACE_PROBE2(timerfd_exec, poller->fn, poller->arg);
1481 : :
1482 [ # # # # : 94662 : return poller->fn(poller->arg);
# # # # #
# # # ]
1483 : 0 : }
1484 : :
1485 : : static int
1486 : 240 : period_poller_interrupt_init(struct spdk_poller *poller)
1487 : : {
1488 : : int timerfd;
1489 : :
1490 [ - + - + : 240 : SPDK_DEBUGLOG(thread, "timerfd init for periodic poller %s\n", poller->name);
# # # # ]
1491 : 240 : timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
1492 [ - + ]: 240 : if (timerfd < 0) {
1493 [ # # # # ]: 0 : return -errno;
1494 : : }
1495 : :
1496 [ # # # # : 240 : poller->intr = spdk_interrupt_register(timerfd, interrupt_timerfd_process, poller, poller->name);
# # ]
1497 [ - + # # : 240 : if (poller->intr == NULL) {
# # ]
1498 : 0 : close(timerfd);
1499 : 0 : return -1;
1500 : : }
1501 : :
1502 : 240 : return 0;
1503 : 0 : }
1504 : :
1505 : : static void
1506 : 248 : period_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
1507 : : {
1508 : : int timerfd;
1509 : 248 : uint64_t now_tick = spdk_get_ticks();
1510 : 248 : uint64_t ticks = spdk_get_ticks_hz();
1511 : : int ret;
1512 : 248 : struct itimerspec new_tv = {};
1513 : 248 : struct itimerspec old_tv = {};
1514 : :
1515 [ - + # # : 248 : assert(poller->intr != NULL);
# # # # ]
1516 [ - + # # : 248 : assert(poller->period_ticks != 0);
# # # # ]
1517 : :
1518 [ # # # # : 248 : timerfd = poller->intr->efd;
# # # # ]
1519 : :
1520 [ - + # # ]: 248 : assert(timerfd >= 0);
1521 : :
1522 [ - + - + : 248 : SPDK_DEBUGLOG(thread, "timerfd set poller %s into %s mode\n", poller->name,
- - # # #
# ]
1523 : : interrupt_mode ? "interrupt" : "poll");
1524 : :
1525 [ + + # # ]: 248 : if (interrupt_mode) {
1526 : : /* Set repeated timer expiration */
1527 [ - + # # : 244 : new_tv.it_interval.tv_sec = poller->period_ticks / ticks;
# # ]
1528 [ - + - + : 244 : new_tv.it_interval.tv_nsec = poller->period_ticks % ticks * SPDK_SEC_TO_NSEC / ticks;
# # # # #
# ]
1529 : :
1530 : : /* Update next timer expiration */
1531 [ + + # # : 244 : if (poller->next_run_tick == 0) {
# # ]
1532 [ # # # # : 240 : poller->next_run_tick = now_tick + poller->period_ticks;
# # # # ]
1533 [ - + # # : 4 : } else if (poller->next_run_tick < now_tick) {
# # ]
1534 [ # # # # ]: 0 : poller->next_run_tick = now_tick;
1535 : 0 : }
1536 : :
1537 [ - + # # : 244 : new_tv.it_value.tv_sec = (poller->next_run_tick - now_tick) / ticks;
# # # # #
# ]
1538 [ - + - + : 244 : new_tv.it_value.tv_nsec = (poller->next_run_tick - now_tick) % ticks * SPDK_SEC_TO_NSEC / ticks;
# # # # #
# # # ]
1539 : :
1540 : 244 : ret = timerfd_settime(timerfd, 0, &new_tv, NULL);
1541 [ - + ]: 244 : if (ret < 0) {
1542 [ # # ]: 0 : SPDK_ERRLOG("Failed to arm timerfd: error(%d)\n", errno);
1543 [ # # ]: 0 : assert(false);
1544 : : }
1545 : 0 : } else {
1546 : : /* Disarm the timer */
1547 : 4 : ret = timerfd_settime(timerfd, 0, &new_tv, &old_tv);
1548 [ - + ]: 4 : if (ret < 0) {
1549 : : /* timerfd_settime's failure indicates that the timerfd is in error */
1550 [ # # ]: 0 : SPDK_ERRLOG("Failed to disarm timerfd: error(%d)\n", errno);
1551 [ # # ]: 0 : assert(false);
1552 : : }
1553 : :
1554 : : /* In order to reuse poller_insert_timer, fix now_tick, so next_run_tick would be
1555 : : * now_tick + ticks * old_tv.it_value.tv_sec + (ticks * old_tv.it_value.tv_nsec) / SPDK_SEC_TO_NSEC
1556 : : */
1557 [ # # # # : 4 : now_tick = now_tick - poller->period_ticks + ticks * old_tv.it_value.tv_sec + \
# # # # ]
1558 [ # # # # : 4 : (ticks * old_tv.it_value.tv_nsec) / SPDK_SEC_TO_NSEC;
# # ]
1559 [ # # # # ]: 4 : poller_remove_timer(poller->thread, poller);
1560 [ # # # # ]: 4 : poller_insert_timer(poller->thread, poller, now_tick);
1561 : : }
1562 : 248 : }
1563 : :
1564 : : static void
1565 : 1782 : poller_interrupt_fini(struct spdk_poller *poller)
1566 : : {
1567 : : int fd;
1568 : :
1569 [ - + - + : 1782 : SPDK_DEBUGLOG(thread, "interrupt fini for poller %s\n", poller->name);
# # # # ]
1570 [ - + # # : 1782 : assert(poller->intr != NULL);
# # # # ]
1571 [ # # # # : 1782 : fd = poller->intr->efd;
# # # # ]
1572 [ # # ]: 1782 : spdk_interrupt_unregister(&poller->intr);
1573 : 1782 : close(fd);
1574 : 1782 : }
1575 : :
1576 : : static int
1577 : 1542 : busy_poller_interrupt_init(struct spdk_poller *poller)
1578 : : {
1579 : : int busy_efd;
1580 : :
1581 [ - + - + : 1542 : SPDK_DEBUGLOG(thread, "busy_efd init for busy poller %s\n", poller->name);
# # # # ]
1582 : 1542 : busy_efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1583 [ - + ]: 1542 : if (busy_efd < 0) {
1584 [ # # ]: 0 : SPDK_ERRLOG("Failed to create eventfd for Poller(%s).\n", poller->name);
1585 [ # # # # ]: 0 : return -errno;
1586 : : }
1587 : :
1588 [ # # # # : 1542 : poller->intr = spdk_interrupt_register(busy_efd, poller->fn, poller->arg, poller->name);
# # # # #
# # # #
# ]
1589 [ - + # # : 1542 : if (poller->intr == NULL) {
# # ]
1590 : 0 : close(busy_efd);
1591 : 0 : return -1;
1592 : : }
1593 : :
1594 : 1542 : return 0;
1595 : 0 : }
1596 : :
1597 : : static void
1598 : 1542 : busy_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
1599 : : {
1600 [ # # # # : 1542 : int busy_efd = poller->intr->efd;
# # # # ]
1601 : 1542 : uint64_t notify = 1;
1602 : : int rc __attribute__((unused));
1603 : :
1604 [ - + # # ]: 1542 : assert(busy_efd >= 0);
1605 : :
1606 [ + - # # ]: 1542 : if (interrupt_mode) {
1607 : : /* Write without read on eventfd will get it repeatedly triggered. */
1608 [ - + ]: 1542 : if (write(busy_efd, ¬ify, sizeof(notify)) < 0) {
1609 [ # # ]: 0 : SPDK_ERRLOG("Failed to set busy wait for Poller(%s).\n", poller->name);
1610 : 0 : }
1611 : 0 : } else {
1612 : : /* Read on eventfd will clear its level triggering. */
1613 : 0 : rc = read(busy_efd, ¬ify, sizeof(notify));
1614 : : }
1615 : 1542 : }
1616 : :
1617 : : #else
1618 : :
1619 : : static int
1620 : : period_poller_interrupt_init(struct spdk_poller *poller)
1621 : : {
1622 : : return -ENOTSUP;
1623 : : }
1624 : :
1625 : : static void
1626 : : period_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
1627 : : {
1628 : : }
1629 : :
1630 : : static void
1631 : : poller_interrupt_fini(struct spdk_poller *poller)
1632 : : {
1633 : : }
1634 : :
1635 : : static int
1636 : : busy_poller_interrupt_init(struct spdk_poller *poller)
1637 : : {
1638 : : return -ENOTSUP;
1639 : : }
1640 : :
1641 : : static void
1642 : : busy_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
1643 : : {
1644 : : }
1645 : :
1646 : : #endif
1647 : :
1648 : : void
1649 : 1827 : spdk_poller_register_interrupt(struct spdk_poller *poller,
1650 : : spdk_poller_set_interrupt_mode_cb cb_fn,
1651 : : void *cb_arg)
1652 : : {
1653 [ + + # # ]: 1827 : assert(poller != NULL);
1654 [ + + + - : 1827 : assert(spdk_get_thread() == poller->thread);
+ - # # ]
1655 : :
1656 [ + + ]: 1827 : if (!spdk_interrupt_mode_is_enabled()) {
1657 : 1697 : return;
1658 : : }
1659 : :
1660 : : /* If this poller already had an interrupt, clean the old one up. */
1661 [ + - # # : 130 : if (poller->intr != NULL) {
# # ]
1662 : 130 : poller_interrupt_fini(poller);
1663 : 0 : }
1664 : :
1665 [ # # # # ]: 130 : poller->set_intr_cb_fn = cb_fn;
1666 [ # # # # ]: 130 : poller->set_intr_cb_arg = cb_arg;
1667 : :
1668 : : /* Set poller into interrupt mode if thread is in interrupt. */
1669 [ + + + + : 130 : if (poller->thread->in_interrupt && poller->set_intr_cb_fn) {
- + # # #
# # # # #
# # # # ]
1670 [ # # # # : 0 : poller->set_intr_cb_fn(poller, poller->set_intr_cb_arg, true);
# # # # #
# # # ]
1671 : 0 : }
1672 : 67 : }
1673 : :
1674 : : static uint64_t
1675 : 169112 : convert_us_to_ticks(uint64_t us)
1676 : : {
1677 : : uint64_t quotient, remainder, ticks;
1678 : :
1679 [ + + ]: 169112 : if (us) {
1680 [ - + ]: 107106 : quotient = us / SPDK_SEC_TO_USEC;
1681 [ - + ]: 107106 : remainder = us % SPDK_SEC_TO_USEC;
1682 : 107106 : ticks = spdk_get_ticks_hz();
1683 : :
1684 [ - + ]: 107106 : return ticks * quotient + (ticks * remainder) / SPDK_SEC_TO_USEC;
1685 : : } else {
1686 : 62006 : return 0;
1687 : : }
1688 : 5158 : }
1689 : :
1690 : : static struct spdk_poller *
1691 : 169112 : poller_register(spdk_poller_fn fn,
1692 : : void *arg,
1693 : : uint64_t period_microseconds,
1694 : : const char *name)
1695 : : {
1696 : : struct spdk_thread *thread;
1697 : : struct spdk_poller *poller;
1698 : :
1699 : 169112 : thread = spdk_get_thread();
1700 [ + + ]: 169112 : if (!thread) {
1701 [ # # ]: 0 : assert(false);
1702 : : return NULL;
1703 : : }
1704 : :
1705 [ + + + - : 169112 : if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) {
- + ]
1706 [ # # ]: 0 : SPDK_ERRLOG("thread %s is marked as exited\n", thread->name);
1707 : 0 : return NULL;
1708 : : }
1709 : :
1710 : 169112 : poller = calloc(1, sizeof(*poller));
1711 [ + + ]: 169112 : if (poller == NULL) {
1712 : 0 : SPDK_ERRLOG("Poller memory allocation failed\n");
1713 : 0 : return NULL;
1714 : : }
1715 : :
1716 [ + + ]: 169112 : if (name) {
1717 [ + - ]: 168925 : snprintf(poller->name, sizeof(poller->name), "%s", name);
1718 : 5157 : } else {
1719 [ + - ]: 187 : snprintf(poller->name, sizeof(poller->name), "%p", fn);
1720 : : }
1721 : :
1722 [ + - + - ]: 169112 : poller->state = SPDK_POLLER_STATE_WAITING;
1723 [ + - + - ]: 169112 : poller->fn = fn;
1724 [ + - + - ]: 169112 : poller->arg = arg;
1725 [ + - + - ]: 169112 : poller->thread = thread;
1726 [ + - + - ]: 169112 : poller->intr = NULL;
1727 [ + + + - : 169112 : if (thread->next_poller_id == 0) {
+ - ]
1728 : 0 : SPDK_WARNLOG("Poller ID rolled over. Poller ID is duplicated.\n");
1729 [ # # # # ]: 0 : thread->next_poller_id = 1;
1730 : 0 : }
1731 [ + - + - : 169112 : poller->id = thread->next_poller_id++;
+ - ]
1732 : :
1733 [ + - + - ]: 169112 : poller->period_ticks = convert_us_to_ticks(period_microseconds);
1734 : :
1735 [ + + ]: 169112 : if (spdk_interrupt_mode_is_enabled()) {
1736 : : int rc;
1737 : :
1738 [ + + ]: 1782 : if (period_microseconds) {
1739 : 240 : rc = period_poller_interrupt_init(poller);
1740 [ - + ]: 240 : if (rc < 0) {
1741 [ # # ]: 0 : SPDK_ERRLOG("Failed to register interruptfd for periodic poller: %s\n", spdk_strerror(-rc));
1742 : 0 : free(poller);
1743 : 0 : return NULL;
1744 : : }
1745 : :
1746 [ # # # # ]: 240 : poller->set_intr_cb_fn = period_poller_set_interrupt_mode;
1747 [ # # # # ]: 240 : poller->set_intr_cb_arg = NULL;
1748 : :
1749 : 0 : } else {
1750 : : /* If the poller doesn't have a period, create interruptfd that's always
1751 : : * busy automatically when running in interrupt mode.
1752 : : */
1753 : 1542 : rc = busy_poller_interrupt_init(poller);
1754 [ - + ]: 1542 : if (rc > 0) {
1755 [ # # ]: 0 : SPDK_ERRLOG("Failed to register interruptfd for busy poller: %s\n", spdk_strerror(-rc));
1756 : 0 : free(poller);
1757 : 0 : return NULL;
1758 : : }
1759 : :
1760 [ # # # # ]: 1542 : poller->set_intr_cb_fn = busy_poller_set_interrupt_mode;
1761 [ # # # # ]: 1542 : poller->set_intr_cb_arg = NULL;
1762 : : }
1763 : :
1764 : : /* Set poller into interrupt mode if thread is in interrupt. */
1765 [ + + + - : 1782 : if (poller->thread->in_interrupt) {
# # # # #
# # # ]
1766 [ # # # # : 1782 : poller->set_intr_cb_fn(poller, poller->set_intr_cb_arg, true);
# # # # #
# # # ]
1767 : 0 : }
1768 : 0 : }
1769 : :
1770 : 169112 : thread_insert_poller(thread, poller);
1771 : :
1772 : 169112 : return poller;
1773 : 5158 : }
1774 : :
1775 : : struct spdk_poller *
1776 : 187 : spdk_poller_register(spdk_poller_fn fn,
1777 : : void *arg,
1778 : : uint64_t period_microseconds)
1779 : : {
1780 : 187 : return poller_register(fn, arg, period_microseconds, NULL);
1781 : : }
1782 : :
1783 : : struct spdk_poller *
1784 : 168925 : spdk_poller_register_named(spdk_poller_fn fn,
1785 : : void *arg,
1786 : : uint64_t period_microseconds,
1787 : : const char *name)
1788 : : {
1789 : 168925 : return poller_register(fn, arg, period_microseconds, name);
1790 : : }
1791 : :
1792 : : static void
1793 : 0 : wrong_thread(const char *func, const char *name, struct spdk_thread *thread,
1794 : : struct spdk_thread *curthread)
1795 : : {
1796 [ # # ]: 0 : if (thread == NULL) {
1797 : 0 : SPDK_ERRLOG("%s(%s) called with NULL thread\n", func, name);
1798 [ # # ]: 0 : abort();
1799 : : }
1800 [ # # # # : 0 : SPDK_ERRLOG("%s(%s) called from wrong thread %s:%" PRIu64 " (should be "
# # # # #
# # # ]
1801 : : "%s:%" PRIu64 ")\n", func, name, curthread->name, curthread->id,
1802 : : thread->name, thread->id);
1803 [ # # ]: 0 : assert(false);
1804 : : }
1805 : :
1806 : : void
1807 : 361989 : spdk_poller_unregister(struct spdk_poller **ppoller)
1808 : : {
1809 : : struct spdk_thread *thread;
1810 : : struct spdk_poller *poller;
1811 : :
1812 [ + - ]: 361989 : poller = *ppoller;
1813 [ + + ]: 361989 : if (poller == NULL) {
1814 : 192877 : return;
1815 : : }
1816 : :
1817 [ + - ]: 169112 : *ppoller = NULL;
1818 : :
1819 : 169112 : thread = spdk_get_thread();
1820 [ + + ]: 169112 : if (!thread) {
1821 [ # # ]: 0 : assert(false);
1822 : : return;
1823 : : }
1824 : :
1825 [ + + + - : 169112 : if (poller->thread != thread) {
- + ]
1826 [ # # # # : 0 : wrong_thread(__func__, poller->name, poller->thread, thread);
# # ]
1827 : 0 : return;
1828 : : }
1829 : :
1830 [ + + ]: 169112 : if (spdk_interrupt_mode_is_enabled()) {
1831 : : /* Release the interrupt resource for period or busy poller */
1832 [ + + # # : 1782 : if (poller->intr != NULL) {
# # ]
1833 : 1652 : poller_interrupt_fini(poller);
1834 : 0 : }
1835 : :
1836 : : /* If there is not already a pending poller removal, generate
1837 : : * a message to go process removals. */
1838 [ + + + + : 1782 : if (!thread->poller_unregistered) {
# # # # ]
1839 [ # # # # ]: 1663 : thread->poller_unregistered = true;
1840 : 1663 : spdk_thread_send_msg(thread, _thread_remove_pollers, thread);
1841 : 0 : }
1842 : 0 : }
1843 : :
1844 : : /* If the poller was paused, put it on the active_pollers list so that
1845 : : * its unregistration can be processed by spdk_thread_poll().
1846 : : */
1847 [ + + + - : 169112 : if (poller->state == SPDK_POLLER_STATE_PAUSED) {
+ - ]
1848 [ - + # # : 27 : TAILQ_REMOVE(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1849 [ # # # # : 27 : TAILQ_INSERT_TAIL(&thread->active_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1850 [ # # # # ]: 27 : poller->period_ticks = 0;
1851 : 0 : }
1852 : :
1853 : : /* Simply set the state to unregistered. The poller will get cleaned up
1854 : : * in a subsequent call to spdk_thread_poll().
1855 : : */
1856 [ + - + - ]: 169112 : poller->state = SPDK_POLLER_STATE_UNREGISTERED;
1857 : 8744 : }
1858 : :
1859 : : void
1860 : 168 : spdk_poller_pause(struct spdk_poller *poller)
1861 : : {
1862 : : struct spdk_thread *thread;
1863 : :
1864 : 168 : thread = spdk_get_thread();
1865 [ - + ]: 168 : if (!thread) {
1866 [ # # ]: 0 : assert(false);
1867 : : return;
1868 : : }
1869 : :
1870 [ - + # # : 168 : if (poller->thread != thread) {
# # ]
1871 [ # # # # : 0 : wrong_thread(__func__, poller->name, poller->thread, thread);
# # ]
1872 : 0 : return;
1873 : : }
1874 : :
1875 : : /* We just set its state to SPDK_POLLER_STATE_PAUSING and let
1876 : : * spdk_thread_poll() move it. It allows a poller to be paused from
1877 : : * another one's context without breaking the TAILQ_FOREACH_REVERSE_SAFE
1878 : : * iteration, or from within itself without breaking the logic to always
1879 : : * remove the closest timed poller in the TAILQ_FOREACH_SAFE iteration.
1880 : : */
1881 [ + + - # : 168 : switch (poller->state) {
# # # ]
1882 : 6 : case SPDK_POLLER_STATE_PAUSED:
1883 : : case SPDK_POLLER_STATE_PAUSING:
1884 : 6 : break;
1885 : 162 : case SPDK_POLLER_STATE_RUNNING:
1886 : : case SPDK_POLLER_STATE_WAITING:
1887 [ # # # # ]: 162 : poller->state = SPDK_POLLER_STATE_PAUSING;
1888 : 162 : break;
1889 : 0 : default:
1890 [ # # ]: 0 : assert(false);
1891 : : break;
1892 : : }
1893 : 0 : }
1894 : :
1895 : : void
1896 : 165 : spdk_poller_resume(struct spdk_poller *poller)
1897 : : {
1898 : : struct spdk_thread *thread;
1899 : :
1900 : 165 : thread = spdk_get_thread();
1901 [ - + ]: 165 : if (!thread) {
1902 [ # # ]: 0 : assert(false);
1903 : : return;
1904 : : }
1905 : :
1906 [ - + # # : 165 : if (poller->thread != thread) {
# # ]
1907 [ # # # # : 0 : wrong_thread(__func__, poller->name, poller->thread, thread);
# # ]
1908 : 0 : return;
1909 : : }
1910 : :
1911 : : /* If a poller is paused it has to be removed from the paused pollers
1912 : : * list and put on the active list or timer tree depending on its
1913 : : * period_ticks. If a poller is still in the process of being paused,
1914 : : * we just need to flip its state back to waiting, as it's already on
1915 : : * the appropriate list or tree.
1916 : : */
1917 [ + + + - : 165 : switch (poller->state) {
# # # # ]
1918 : 105 : case SPDK_POLLER_STATE_PAUSED:
1919 [ - + # # : 105 : TAILQ_REMOVE(&thread->paused_pollers, poller, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1920 : 105 : thread_insert_poller(thread, poller);
1921 : : /* fallthrough */
1922 : 132 : case SPDK_POLLER_STATE_PAUSING:
1923 [ # # # # ]: 132 : poller->state = SPDK_POLLER_STATE_WAITING;
1924 : 132 : break;
1925 : 33 : case SPDK_POLLER_STATE_RUNNING:
1926 : : case SPDK_POLLER_STATE_WAITING:
1927 : 33 : break;
1928 : 0 : default:
1929 [ # # ]: 0 : assert(false);
1930 : : break;
1931 : : }
1932 : 0 : }
1933 : :
1934 : : const char *
1935 : 14 : spdk_poller_get_name(struct spdk_poller *poller)
1936 : : {
1937 [ # # ]: 14 : return poller->name;
1938 : : }
1939 : :
1940 : : uint64_t
1941 : 17 : spdk_poller_get_id(struct spdk_poller *poller)
1942 : : {
1943 [ # # # # ]: 17 : return poller->id;
1944 : : }
1945 : :
1946 : : const char *
1947 : 35 : spdk_poller_get_state_str(struct spdk_poller *poller)
1948 : : {
1949 [ + + - + : 35 : switch (poller->state) {
+ - # # #
# ]
1950 : 20 : case SPDK_POLLER_STATE_WAITING:
1951 : 20 : return "waiting";
1952 : 6 : case SPDK_POLLER_STATE_RUNNING:
1953 : 6 : return "running";
1954 : 0 : case SPDK_POLLER_STATE_UNREGISTERED:
1955 : 0 : return "unregistered";
1956 : 3 : case SPDK_POLLER_STATE_PAUSING:
1957 : 3 : return "pausing";
1958 : 6 : case SPDK_POLLER_STATE_PAUSED:
1959 : 6 : return "paused";
1960 : 0 : default:
1961 : 0 : return NULL;
1962 : : }
1963 : 0 : }
1964 : :
1965 : : uint64_t
1966 : 14 : spdk_poller_get_period_ticks(struct spdk_poller *poller)
1967 : : {
1968 [ # # # # ]: 14 : return poller->period_ticks;
1969 : : }
1970 : :
1971 : : void
1972 : 14 : spdk_poller_get_stats(struct spdk_poller *poller, struct spdk_poller_stats *stats)
1973 : : {
1974 [ # # # # : 14 : stats->run_count = poller->run_count;
# # # # ]
1975 [ # # # # : 14 : stats->busy_count = poller->busy_count;
# # # # ]
1976 : 14 : }
1977 : :
1978 : : struct spdk_poller *
1979 : 126 : spdk_thread_get_first_active_poller(struct spdk_thread *thread)
1980 : : {
1981 [ # # # # : 126 : return TAILQ_FIRST(&thread->active_pollers);
# # ]
1982 : : }
1983 : :
1984 : : struct spdk_poller *
1985 : 48 : spdk_thread_get_next_active_poller(struct spdk_poller *prev)
1986 : : {
1987 [ # # # # : 48 : return TAILQ_NEXT(prev, tailq);
# # ]
1988 : : }
1989 : :
1990 : : struct spdk_poller *
1991 : 126 : spdk_thread_get_first_timed_poller(struct spdk_thread *thread)
1992 : : {
1993 [ # # ]: 126 : return RB_MIN(timed_pollers_tree, &thread->timed_pollers);
1994 : : }
1995 : :
1996 : : struct spdk_poller *
1997 : 78 : spdk_thread_get_next_timed_poller(struct spdk_poller *prev)
1998 : : {
1999 : 78 : return RB_NEXT(timed_pollers_tree, &thread->timed_pollers, prev);
2000 : : }
2001 : :
2002 : : struct spdk_poller *
2003 : 126 : spdk_thread_get_first_paused_poller(struct spdk_thread *thread)
2004 : : {
2005 [ # # # # : 126 : return TAILQ_FIRST(&thread->paused_pollers);
# # ]
2006 : : }
2007 : :
2008 : : struct spdk_poller *
2009 : 0 : spdk_thread_get_next_paused_poller(struct spdk_poller *prev)
2010 : : {
2011 [ # # # # : 0 : return TAILQ_NEXT(prev, tailq);
# # ]
2012 : : }
2013 : :
2014 : : struct spdk_io_channel *
2015 : 0 : spdk_thread_get_first_io_channel(struct spdk_thread *thread)
2016 : : {
2017 [ # # ]: 0 : return RB_MIN(io_channel_tree, &thread->io_channels);
2018 : : }
2019 : :
2020 : : struct spdk_io_channel *
2021 : 0 : spdk_thread_get_next_io_channel(struct spdk_io_channel *prev)
2022 : : {
2023 : 0 : return RB_NEXT(io_channel_tree, &thread->io_channels, prev);
2024 : : }
2025 : :
2026 : : uint16_t
2027 : 6 : spdk_thread_get_trace_id(struct spdk_thread *thread)
2028 : : {
2029 [ # # # # ]: 6 : return thread->trace_id;
2030 : : }
2031 : :
2032 : : struct call_thread {
2033 : : struct spdk_thread *cur_thread;
2034 : : spdk_msg_fn fn;
2035 : : void *ctx;
2036 : :
2037 : : struct spdk_thread *orig_thread;
2038 : : spdk_msg_fn cpl;
2039 : : };
2040 : :
2041 : : static void
2042 : 584 : _back_to_orig_thread(void *ctx)
2043 : : {
2044 : 584 : struct call_thread *ct = ctx;
2045 : :
2046 [ + + + - : 584 : assert(ct->orig_thread->for_each_count > 0);
+ - + - +
- # # ]
2047 [ + - + - : 584 : ct->orig_thread->for_each_count--;
+ - ]
2048 : :
2049 [ + - + - : 584 : ct->cpl(ct->ctx);
- + + - +
- + - ]
2050 : 584 : free(ctx);
2051 : 584 : }
2052 : :
2053 : : static void
2054 : 791 : _on_thread(void *ctx)
2055 : : {
2056 : 791 : struct call_thread *ct = ctx;
2057 : : int rc __attribute__((unused));
2058 : :
2059 [ + - + - : 791 : ct->fn(ct->ctx);
- + + - +
- + - ]
2060 : :
2061 [ + + ]: 791 : pthread_mutex_lock(&g_devlist_mutex);
2062 [ + - + - : 791 : ct->cur_thread = TAILQ_NEXT(ct->cur_thread, tailq);
+ - + - +
- + - +
- ]
2063 [ + + + + : 1473 : while (ct->cur_thread && ct->cur_thread->state != SPDK_THREAD_STATE_RUNNING) {
+ + + - +
- + - + -
+ + ]
2064 [ + + + + : 682 : SPDK_DEBUGLOG(thread, "thread %s is not running but still not destroyed.\n",
+ - # # #
# # # ]
2065 : : ct->cur_thread->name);
2066 [ + - + - : 682 : ct->cur_thread = TAILQ_NEXT(ct->cur_thread, tailq);
+ - + - +
- + - +
- ]
2067 : : }
2068 [ + + ]: 791 : pthread_mutex_unlock(&g_devlist_mutex);
2069 : :
2070 [ + + + - : 791 : if (!ct->cur_thread) {
+ + ]
2071 [ + + + + : 584 : SPDK_DEBUGLOG(thread, "Completed thread iteration\n");
+ - ]
2072 : :
2073 [ + - + - ]: 584 : rc = spdk_thread_send_msg(ct->orig_thread, _back_to_orig_thread, ctx);
2074 : 21 : } else {
2075 [ + + + + : 207 : SPDK_DEBUGLOG(thread, "Continuing thread iteration to %s\n",
+ - # # #
# # # ]
2076 : : ct->cur_thread->name);
2077 : :
2078 [ + - + - ]: 207 : rc = spdk_thread_send_msg(ct->cur_thread, _on_thread, ctx);
2079 : : }
2080 [ + + # # ]: 791 : assert(rc == 0);
2081 : 791 : }
2082 : :
2083 : : void
2084 : 584 : spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl)
2085 : : {
2086 : : struct call_thread *ct;
2087 : : struct spdk_thread *thread;
2088 : : int rc __attribute__((unused));
2089 : :
2090 : 584 : ct = calloc(1, sizeof(*ct));
2091 [ + + ]: 584 : if (!ct) {
2092 : 0 : SPDK_ERRLOG("Unable to perform thread iteration\n");
2093 [ # # # # ]: 0 : cpl(ctx);
2094 : 0 : return;
2095 : : }
2096 : :
2097 [ + - + - ]: 584 : ct->fn = fn;
2098 [ + - + - ]: 584 : ct->ctx = ctx;
2099 [ + - + - ]: 584 : ct->cpl = cpl;
2100 : :
2101 : 584 : thread = _get_thread();
2102 [ + + ]: 584 : if (!thread) {
2103 : 0 : SPDK_ERRLOG("No thread allocated\n");
2104 : 0 : free(ct);
2105 [ # # # # ]: 0 : cpl(ctx);
2106 : 0 : return;
2107 : : }
2108 [ + - + - ]: 584 : ct->orig_thread = thread;
2109 : :
2110 [ + - + - : 584 : ct->orig_thread->for_each_count++;
+ - ]
2111 : :
2112 [ + + ]: 584 : pthread_mutex_lock(&g_devlist_mutex);
2113 [ + - + - ]: 584 : ct->cur_thread = TAILQ_FIRST(&g_threads);
2114 [ + + ]: 584 : pthread_mutex_unlock(&g_devlist_mutex);
2115 : :
2116 [ + + + + : 584 : SPDK_DEBUGLOG(thread, "Starting thread iteration from %s\n",
+ - # # #
# # # ]
2117 : : ct->orig_thread->name);
2118 : :
2119 [ + - + - ]: 584 : rc = spdk_thread_send_msg(ct->cur_thread, _on_thread, ct);
2120 [ + + # # ]: 584 : assert(rc == 0);
2121 : 21 : }
2122 : :
2123 : : static inline void
2124 : 8 : poller_set_interrupt_mode(struct spdk_poller *poller, bool interrupt_mode)
2125 : : {
2126 [ - + # # : 8 : if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) {
# # ]
2127 : 0 : return;
2128 : : }
2129 : :
2130 [ + - # # : 8 : if (poller->set_intr_cb_fn) {
# # ]
2131 [ # # # # : 8 : poller->set_intr_cb_fn(poller, poller->set_intr_cb_arg, interrupt_mode);
# # # # #
# # # #
# ]
2132 : 0 : }
2133 : 0 : }
2134 : :
2135 : : void
2136 : 138 : spdk_thread_set_interrupt_mode(bool enable_interrupt)
2137 : : {
2138 : 138 : struct spdk_thread *thread = _get_thread();
2139 : : struct spdk_poller *poller, *tmp;
2140 : :
2141 [ - + # # ]: 138 : assert(thread);
2142 [ - + # # ]: 138 : assert(spdk_interrupt_mode_is_enabled());
2143 : :
2144 [ + + + + : 138 : SPDK_NOTICELOG("Set spdk_thread (%s) to %s mode from %s mode.\n",
+ + # # #
# ]
2145 : : thread->name, enable_interrupt ? "intr" : "poll",
2146 : : thread->in_interrupt ? "intr" : "poll");
2147 : :
2148 [ + + + + : 138 : if (thread->in_interrupt == enable_interrupt) {
# # # # #
# ]
2149 : 130 : return;
2150 : : }
2151 : :
2152 : : /* Set pollers to expected mode */
2153 [ + + + - : 16 : RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, tmp) {
# # ]
2154 [ # # ]: 8 : poller_set_interrupt_mode(poller, enable_interrupt);
2155 : 0 : }
2156 [ - + # # : 8 : TAILQ_FOREACH_SAFE(poller, &thread->active_pollers, tailq, tmp) {
# # # # #
# # # # #
# # ]
2157 [ # # ]: 0 : poller_set_interrupt_mode(poller, enable_interrupt);
2158 : 0 : }
2159 : : /* All paused pollers will go to work in interrupt mode */
2160 [ - + # # : 8 : TAILQ_FOREACH_SAFE(poller, &thread->paused_pollers, tailq, tmp) {
# # # # #
# # # # #
# # ]
2161 [ # # ]: 0 : poller_set_interrupt_mode(poller, enable_interrupt);
2162 : 0 : }
2163 : :
2164 [ # # # # : 8 : thread->in_interrupt = enable_interrupt;
# # ]
2165 : 8 : return;
2166 : 0 : }
2167 : :
2168 : : static struct io_device *
2169 : 1069845 : io_device_get(void *io_device)
2170 : : {
2171 : 1069845 : struct io_device find = {};
2172 : :
2173 : 1069845 : find.io_device = io_device;
2174 : 1069845 : return RB_FIND(io_device_tree, &g_io_devices, &find);
2175 : : }
2176 : :
2177 : : void
2178 : 44077 : spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
2179 : : spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size,
2180 : : const char *name)
2181 : : {
2182 : : struct io_device *dev, *tmp;
2183 : : struct spdk_thread *thread;
2184 : :
2185 [ + + # # ]: 44077 : assert(io_device != NULL);
2186 [ + + # # ]: 44077 : assert(create_cb != NULL);
2187 [ + + # # ]: 44077 : assert(destroy_cb != NULL);
2188 : :
2189 : 44077 : thread = spdk_get_thread();
2190 [ + + ]: 44077 : if (!thread) {
2191 : 0 : SPDK_ERRLOG("called from non-SPDK thread\n");
2192 [ # # ]: 0 : assert(false);
2193 : : return;
2194 : : }
2195 : :
2196 : 44077 : dev = calloc(1, sizeof(struct io_device));
2197 [ + + ]: 44077 : if (dev == NULL) {
2198 : 0 : SPDK_ERRLOG("could not allocate io_device\n");
2199 : 0 : return;
2200 : : }
2201 : :
2202 [ + - + - ]: 44077 : dev->io_device = io_device;
2203 [ + + ]: 44077 : if (name) {
2204 [ + + ]: 43654 : snprintf(dev->name, sizeof(dev->name), "%s", name);
2205 : 732 : } else {
2206 [ - + ]: 423 : snprintf(dev->name, sizeof(dev->name), "%p", dev);
2207 : : }
2208 [ + - + - ]: 44077 : dev->create_cb = create_cb;
2209 [ + - + - ]: 44077 : dev->destroy_cb = destroy_cb;
2210 [ + - + - ]: 44077 : dev->unregister_cb = NULL;
2211 [ + - + - ]: 44077 : dev->ctx_size = ctx_size;
2212 [ + - + - ]: 44077 : dev->for_each_count = 0;
2213 [ + - + - ]: 44077 : dev->unregistered = false;
2214 [ + - + - ]: 44077 : dev->refcnt = 0;
2215 : :
2216 [ + + + + : 44077 : SPDK_DEBUGLOG(thread, "Registering io_device %s (%p) on thread %s\n",
+ - # # #
# # # #
# ]
2217 : : dev->name, dev->io_device, thread->name);
2218 : :
2219 [ + + ]: 44077 : pthread_mutex_lock(&g_devlist_mutex);
2220 : 44077 : tmp = RB_INSERT(io_device_tree, &g_io_devices, dev);
2221 [ + + ]: 44077 : if (tmp != NULL) {
2222 [ # # # # ]: 6 : SPDK_ERRLOG("io_device %p already registered (old:%s new:%s)\n",
2223 : : io_device, tmp->name, dev->name);
2224 : 6 : free(dev);
2225 : 0 : }
2226 : :
2227 [ + + ]: 44077 : pthread_mutex_unlock(&g_devlist_mutex);
2228 : 732 : }
2229 : :
2230 : : static void
2231 : 34637 : _finish_unregister(void *arg)
2232 : : {
2233 : 34637 : struct io_device *dev = arg;
2234 : : struct spdk_thread *thread;
2235 : :
2236 : 34637 : thread = spdk_get_thread();
2237 [ + + + - : 34637 : assert(thread == dev->unregister_thread);
+ - # # ]
2238 : :
2239 [ + + + + : 34637 : SPDK_DEBUGLOG(thread, "Finishing unregistration of io_device %s (%p) on thread %s\n",
+ - # # #
# # # #
# ]
2240 : : dev->name, dev->io_device, thread->name);
2241 : :
2242 [ + + + - : 34637 : assert(thread->pending_unregister_count > 0);
+ - # # ]
2243 [ + - + - ]: 34637 : thread->pending_unregister_count--;
2244 : :
2245 [ + - + - : 34637 : dev->unregister_cb(dev->io_device);
- + + - +
- + - ]
2246 : 34637 : free(dev);
2247 : 34637 : }
2248 : :
2249 : : static void
2250 : 44068 : io_device_free(struct io_device *dev)
2251 : : {
2252 : : int rc __attribute__((unused));
2253 : :
2254 [ + + + - : 44068 : if (dev->unregister_cb == NULL) {
+ + ]
2255 : 9431 : free(dev);
2256 : 228 : } else {
2257 [ + + + - : 34637 : assert(dev->unregister_thread != NULL);
+ - # # ]
2258 [ + + + + : 34637 : SPDK_DEBUGLOG(thread, "io_device %s (%p) needs to unregister from thread %s\n",
+ - # # #
# # # # #
# # # # ]
2259 : : dev->name, dev->io_device, dev->unregister_thread->name);
2260 [ + - + - ]: 34637 : rc = spdk_thread_send_msg(dev->unregister_thread, _finish_unregister, dev);
2261 [ + + # # ]: 34637 : assert(rc == 0);
2262 : : }
2263 : 44068 : }
2264 : :
2265 : : void
2266 : 44074 : spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb)
2267 : : {
2268 : : struct io_device *dev;
2269 : : uint32_t refcnt;
2270 : : struct spdk_thread *thread;
2271 : :
2272 : 44074 : thread = spdk_get_thread();
2273 [ + + ]: 44074 : if (!thread) {
2274 : 0 : SPDK_ERRLOG("called from non-SPDK thread\n");
2275 [ # # ]: 0 : assert(false);
2276 : : return;
2277 : : }
2278 : :
2279 [ + + ]: 44074 : pthread_mutex_lock(&g_devlist_mutex);
2280 : 44074 : dev = io_device_get(io_device);
2281 [ + + ]: 44074 : if (!dev) {
2282 : 0 : SPDK_ERRLOG("io_device %p not found\n", io_device);
2283 [ # # ]: 0 : assert(false);
2284 : : pthread_mutex_unlock(&g_devlist_mutex);
2285 : : return;
2286 : : }
2287 : :
2288 : : /* The for_each_count check differentiates the user attempting to unregister the
2289 : : * device a second time, from the internal call to this function that occurs
2290 : : * after the for_each_count reaches 0.
2291 : : */
2292 [ + + + + : 44074 : if (dev->pending_unregister && dev->for_each_count > 0) {
+ + - + #
# # # #
# ]
2293 : 0 : SPDK_ERRLOG("io_device %p already has a pending unregister\n", io_device);
2294 [ # # ]: 0 : assert(false);
2295 : : pthread_mutex_unlock(&g_devlist_mutex);
2296 : : return;
2297 : : }
2298 : :
2299 [ + - + - ]: 44074 : dev->unregister_cb = unregister_cb;
2300 [ + - + - ]: 44074 : dev->unregister_thread = thread;
2301 : :
2302 [ + + + - : 44074 : if (dev->for_each_count > 0) {
- + ]
2303 [ # # # # : 3 : SPDK_WARNLOG("io_device %s (%p) has %u for_each calls outstanding\n",
# # ]
2304 : : dev->name, io_device, dev->for_each_count);
2305 [ # # # # ]: 3 : dev->pending_unregister = true;
2306 [ - + ]: 3 : pthread_mutex_unlock(&g_devlist_mutex);
2307 : 3 : return;
2308 : : }
2309 : :
2310 [ + - + - ]: 44071 : dev->unregistered = true;
2311 : 44071 : RB_REMOVE(io_device_tree, &g_io_devices, dev);
2312 [ + - + - ]: 44071 : refcnt = dev->refcnt;
2313 [ + + ]: 44071 : pthread_mutex_unlock(&g_devlist_mutex);
2314 : :
2315 [ + + + + : 44071 : SPDK_DEBUGLOG(thread, "Unregistering io_device %s (%p) from thread %s\n",
+ - # # #
# # # #
# ]
2316 : : dev->name, dev->io_device, thread->name);
2317 : :
2318 [ + + ]: 44071 : if (unregister_cb) {
2319 [ + - + - ]: 34637 : thread->pending_unregister_count++;
2320 : 504 : }
2321 : :
2322 [ + + ]: 44071 : if (refcnt > 0) {
2323 : : /* defer deletion */
2324 : 9516 : return;
2325 : : }
2326 : :
2327 : 34555 : io_device_free(dev);
2328 : 732 : }
2329 : :
2330 : : const char *
2331 : 0 : spdk_io_device_get_name(struct io_device *dev)
2332 : : {
2333 [ # # ]: 0 : return dev->name;
2334 : : }
2335 : :
2336 : : static struct spdk_io_channel *
2337 : 4237713 : thread_get_io_channel(struct spdk_thread *thread, struct io_device *dev)
2338 : : {
2339 : 4237713 : struct spdk_io_channel find = {};
2340 : :
2341 [ + - ]: 4237713 : find.dev = dev;
2342 [ + - ]: 4237713 : return RB_FIND(io_channel_tree, &thread->io_channels, &find);
2343 : : }
2344 : :
2345 : : struct spdk_io_channel *
2346 : 310030 : spdk_get_io_channel(void *io_device)
2347 : : {
2348 : : struct spdk_io_channel *ch;
2349 : : struct spdk_thread *thread;
2350 : : struct io_device *dev;
2351 : : int rc;
2352 : :
2353 [ + - ]: 310030 : pthread_mutex_lock(&g_devlist_mutex);
2354 : 310030 : dev = io_device_get(io_device);
2355 [ + + ]: 310030 : if (dev == NULL) {
2356 : 3 : SPDK_ERRLOG("could not find io_device %p\n", io_device);
2357 [ # # ]: 3 : pthread_mutex_unlock(&g_devlist_mutex);
2358 : 3 : return NULL;
2359 : : }
2360 : :
2361 : 310027 : thread = _get_thread();
2362 [ + + ]: 310027 : if (!thread) {
2363 : 0 : SPDK_ERRLOG("No thread allocated\n");
2364 [ # # ]: 0 : pthread_mutex_unlock(&g_devlist_mutex);
2365 : 0 : return NULL;
2366 : : }
2367 : :
2368 [ + + + - : 310027 : if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) {
- + ]
2369 [ # # ]: 0 : SPDK_ERRLOG("Thread %s is marked as exited\n", thread->name);
2370 [ # # ]: 0 : pthread_mutex_unlock(&g_devlist_mutex);
2371 : 0 : return NULL;
2372 : : }
2373 : :
2374 : 310027 : ch = thread_get_io_channel(thread, dev);
2375 [ + + ]: 310027 : if (ch != NULL) {
2376 [ + - ]: 212958 : ch->ref++;
2377 : :
2378 [ + + + + : 212958 : SPDK_DEBUGLOG(thread, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
+ - # # #
# # # # #
# # # # ]
2379 : : ch, dev->name, dev->io_device, thread->name, ch->ref);
2380 : :
2381 : : /*
2382 : : * An I/O channel already exists for this device on this
2383 : : * thread, so return it.
2384 : : */
2385 [ + - ]: 212958 : pthread_mutex_unlock(&g_devlist_mutex);
2386 [ + + + + : 212958 : spdk_trace_record(TRACE_THREAD_IOCH_GET, 0, 0,
+ - + - +
- + - + -
+ - # # #
# ]
2387 : : (uint64_t)spdk_io_channel_get_ctx(ch), ch->ref);
2388 : 212958 : return ch;
2389 : : }
2390 : :
2391 [ + - + - ]: 97069 : ch = calloc(1, sizeof(*ch) + dev->ctx_size);
2392 [ + + ]: 97069 : if (ch == NULL) {
2393 : 0 : SPDK_ERRLOG("could not calloc spdk_io_channel\n");
2394 [ # # ]: 0 : pthread_mutex_unlock(&g_devlist_mutex);
2395 : 0 : return NULL;
2396 : : }
2397 : :
2398 [ + - + - ]: 97069 : ch->dev = dev;
2399 [ + - + - : 97069 : ch->destroy_cb = dev->destroy_cb;
+ - + - ]
2400 [ + - + - ]: 97069 : ch->thread = thread;
2401 [ + - + - ]: 97069 : ch->ref = 1;
2402 [ + - + - ]: 97069 : ch->destroy_ref = 0;
2403 [ + - ]: 97069 : RB_INSERT(io_channel_tree, &thread->io_channels, ch);
2404 : :
2405 [ + + + + : 97069 : SPDK_DEBUGLOG(thread, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
+ - # # #
# # # # #
# # # # ]
2406 : : ch, dev->name, dev->io_device, thread->name, ch->ref);
2407 : :
2408 [ + - ]: 97069 : dev->refcnt++;
2409 : :
2410 [ + - ]: 97069 : pthread_mutex_unlock(&g_devlist_mutex);
2411 : :
2412 [ + - + - : 97069 : rc = dev->create_cb(io_device, (uint8_t *)ch + sizeof(*ch));
- + + - +
- ]
2413 [ + + ]: 97069 : if (rc != 0) {
2414 [ # # ]: 9 : pthread_mutex_lock(&g_devlist_mutex);
2415 [ # # # # : 9 : RB_REMOVE(io_channel_tree, &ch->thread->io_channels, ch);
# # ]
2416 [ # # ]: 9 : dev->refcnt--;
2417 : 9 : free(ch);
2418 [ # # # # ]: 9 : SPDK_ERRLOG("could not create io_channel for io_device %s (%p): %s (rc=%d)\n",
2419 : : dev->name, io_device, spdk_strerror(-rc), rc);
2420 [ # # ]: 9 : pthread_mutex_unlock(&g_devlist_mutex);
2421 : 9 : return NULL;
2422 : : }
2423 : :
2424 [ + + + + : 97060 : spdk_trace_record(TRACE_THREAD_IOCH_GET, 0, 0, (uint64_t)spdk_io_channel_get_ctx(ch), 1);
- + - + -
+ - + - +
- + ]
2425 : 97060 : return ch;
2426 : 2339 : }
2427 : :
2428 : : static void
2429 : 97135 : put_io_channel(void *arg)
2430 : : {
2431 : 97135 : struct spdk_io_channel *ch = arg;
2432 : 97135 : bool do_remove_dev = true;
2433 : : struct spdk_thread *thread;
2434 : :
2435 : 97135 : thread = spdk_get_thread();
2436 [ + + ]: 97135 : if (!thread) {
2437 : 0 : SPDK_ERRLOG("called from non-SPDK thread\n");
2438 [ # # ]: 0 : assert(false);
2439 : : return;
2440 : : }
2441 : :
2442 [ + + + + : 97135 : SPDK_DEBUGLOG(thread,
+ - # # #
# # # # #
# # # # #
# # # ]
2443 : : "Releasing io_channel %p for io_device %s (%p) on thread %s\n",
2444 : : ch, ch->dev->name, ch->dev->io_device, thread->name);
2445 : :
2446 [ + + + - : 97135 : assert(ch->thread == thread);
+ - # # ]
2447 : :
2448 [ + - ]: 97135 : ch->destroy_ref--;
2449 : :
2450 [ + + + + : 97135 : if (ch->ref > 0 || ch->destroy_ref > 0) {
+ - + - +
- - + ]
2451 : : /*
2452 : : * Another reference to the associated io_device was requested
2453 : : * after this message was sent but before it had a chance to
2454 : : * execute.
2455 : : */
2456 : 81 : return;
2457 : : }
2458 : :
2459 [ + + ]: 97054 : pthread_mutex_lock(&g_devlist_mutex);
2460 [ + - + - : 97054 : RB_REMOVE(io_channel_tree, &ch->thread->io_channels, ch);
+ - ]
2461 [ + + ]: 97054 : pthread_mutex_unlock(&g_devlist_mutex);
2462 : :
2463 : : /* Don't hold the devlist mutex while the destroy_cb is called. */
2464 [ + - + - : 97054 : ch->destroy_cb(ch->dev->io_device, spdk_io_channel_get_ctx(ch));
- + + - +
- + - + -
+ - ]
2465 : :
2466 [ + + ]: 97054 : pthread_mutex_lock(&g_devlist_mutex);
2467 [ + - + - : 97054 : ch->dev->refcnt--;
+ - ]
2468 : :
2469 [ + + + + : 97054 : if (!ch->dev->unregistered) {
+ - + - +
- + + ]
2470 : 87398 : do_remove_dev = false;
2471 : 585 : }
2472 : :
2473 [ + + + - : 97054 : if (ch->dev->refcnt > 0) {
+ - + - +
+ ]
2474 : 32140 : do_remove_dev = false;
2475 : 14 : }
2476 : :
2477 [ + + ]: 97054 : pthread_mutex_unlock(&g_devlist_mutex);
2478 : :
2479 [ + + + + ]: 97054 : if (do_remove_dev) {
2480 [ + - + - ]: 9513 : io_device_free(ch->dev);
2481 : 62 : }
2482 : 97054 : free(ch);
2483 : 647 : }
2484 : :
2485 : : void
2486 : 310013 : spdk_put_io_channel(struct spdk_io_channel *ch)
2487 : : {
2488 : : struct spdk_thread *thread;
2489 : : int rc __attribute__((unused));
2490 : :
2491 [ + + + + : 310013 : spdk_trace_record(TRACE_THREAD_IOCH_PUT, 0, 0,
+ - + - +
- + - + -
+ + + - +
- ]
2492 : : (uint64_t)spdk_io_channel_get_ctx(ch), ch->ref);
2493 : :
2494 : 310013 : thread = spdk_get_thread();
2495 [ + + ]: 310013 : if (!thread) {
2496 : 0 : SPDK_ERRLOG("called from non-SPDK thread\n");
2497 [ # # ]: 0 : assert(false);
2498 : : return;
2499 : : }
2500 : :
2501 [ + + + - : 310009 : if (ch->thread != thread) {
- + ]
2502 [ # # # # ]: 0 : wrong_thread(__func__, "ch", ch->thread, thread);
2503 : 0 : return;
2504 : : }
2505 : :
2506 [ + + + + : 310009 : SPDK_DEBUGLOG(thread,
+ - # # #
# # # # #
# # # # #
# # # # #
# # ]
2507 : : "Putting io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
2508 : : ch, ch->dev->name, ch->dev->io_device, thread->name, ch->ref);
2509 : :
2510 [ + - ]: 310009 : ch->ref--;
2511 : :
2512 [ + + + - : 310009 : if (ch->ref == 0) {
+ + ]
2513 [ + - ]: 97131 : ch->destroy_ref++;
2514 : 97131 : rc = spdk_thread_send_msg(thread, put_io_channel, ch);
2515 [ + + # # ]: 97131 : assert(rc == 0);
2516 : 643 : }
2517 : 2333 : }
2518 : :
2519 : : struct spdk_io_channel *
2520 : 24935206 : spdk_io_channel_from_ctx(void *ctx)
2521 : : {
2522 [ + - ]: 24935206 : return (struct spdk_io_channel *)((uint8_t *)ctx - sizeof(struct spdk_io_channel));
2523 : : }
2524 : :
2525 : : struct spdk_thread *
2526 : 416676848 : spdk_io_channel_get_thread(struct spdk_io_channel *ch)
2527 : : {
2528 [ + - + - ]: 416676848 : return ch->thread;
2529 : : }
2530 : :
2531 : : void *
2532 : 16889679 : spdk_io_channel_get_io_device(struct spdk_io_channel *ch)
2533 : : {
2534 [ + - + - : 16889679 : return ch->dev->io_device;
+ - + - ]
2535 : : }
2536 : :
2537 : : const char *
2538 : 0 : spdk_io_channel_get_io_device_name(struct spdk_io_channel *ch)
2539 : : {
2540 [ # # # # ]: 0 : return spdk_io_device_get_name(ch->dev);
2541 : : }
2542 : :
2543 : : int
2544 : 0 : spdk_io_channel_get_ref_count(struct spdk_io_channel *ch)
2545 : : {
2546 [ # # # # ]: 0 : return ch->ref;
2547 : : }
2548 : :
2549 : : struct spdk_io_channel_iter {
2550 : : void *io_device;
2551 : : struct io_device *dev;
2552 : : spdk_channel_msg fn;
2553 : : int status;
2554 : : void *ctx;
2555 : : struct spdk_io_channel *ch;
2556 : :
2557 : : struct spdk_thread *cur_thread;
2558 : :
2559 : : struct spdk_thread *orig_thread;
2560 : : spdk_channel_for_each_cpl cpl;
2561 : : };
2562 : :
2563 : : void *
2564 : 675104 : spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i)
2565 : : {
2566 [ + - + - ]: 675104 : return i->io_device;
2567 : : }
2568 : :
2569 : : struct spdk_io_channel *
2570 : 743014 : spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i)
2571 : : {
2572 [ + - + - ]: 743014 : return i->ch;
2573 : : }
2574 : :
2575 : : void *
2576 : 1458773 : spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i)
2577 : : {
2578 [ + - + - ]: 1458773 : return i->ctx;
2579 : : }
2580 : :
2581 : : static void
2582 : 715732 : _call_completion(void *ctx)
2583 : : {
2584 : 715732 : struct spdk_io_channel_iter *i = ctx;
2585 : :
2586 [ + + + - : 715732 : assert(i->orig_thread->for_each_count > 0);
+ - + - +
- # # ]
2587 [ + - + - : 715732 : i->orig_thread->for_each_count--;
+ - ]
2588 : :
2589 [ + + + - : 715732 : if (i->cpl != NULL) {
- + ]
2590 [ + - + - : 715708 : i->cpl(i, i->status);
- + + - +
- + - ]
2591 : 1254 : }
2592 : 715732 : free(i);
2593 : 715732 : }
2594 : :
2595 : : static void
2596 : 812776 : _call_channel(void *ctx)
2597 : : {
2598 : 812776 : struct spdk_io_channel_iter *i = ctx;
2599 : : struct spdk_io_channel *ch;
2600 : :
2601 : : /*
2602 : : * It is possible that the channel was deleted before this
2603 : : * message had a chance to execute. If so, skip calling
2604 : : * the fn() on this thread.
2605 : : */
2606 [ + + ]: 812776 : pthread_mutex_lock(&g_devlist_mutex);
2607 [ + - + - : 812776 : ch = thread_get_io_channel(i->cur_thread, i->dev);
+ - + - ]
2608 [ + + ]: 812776 : pthread_mutex_unlock(&g_devlist_mutex);
2609 : :
2610 [ + + ]: 812776 : if (ch) {
2611 [ + - + - : 812551 : i->fn(i);
- + - + ]
2612 : 1222 : } else {
2613 : 225 : spdk_for_each_channel_continue(i, 0);
2614 : : }
2615 : 812776 : }
2616 : :
2617 : : void
2618 : 715732 : spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx,
2619 : : spdk_channel_for_each_cpl cpl)
2620 : : {
2621 : : struct spdk_thread *thread;
2622 : : struct spdk_io_channel *ch;
2623 : : struct spdk_io_channel_iter *i;
2624 : : int rc __attribute__((unused));
2625 : :
2626 : 715732 : i = calloc(1, sizeof(*i));
2627 [ + + ]: 715732 : if (!i) {
2628 : 0 : SPDK_ERRLOG("Unable to allocate iterator\n");
2629 [ # # ]: 0 : assert(false);
2630 : : return;
2631 : : }
2632 : :
2633 [ + - + - ]: 715732 : i->io_device = io_device;
2634 [ + - + - ]: 715732 : i->fn = fn;
2635 [ + - + - ]: 715732 : i->ctx = ctx;
2636 [ + - + - ]: 715732 : i->cpl = cpl;
2637 [ + - + - ]: 715732 : i->orig_thread = _get_thread();
2638 : :
2639 [ + - + - : 715732 : i->orig_thread->for_each_count++;
+ - ]
2640 : :
2641 [ + + ]: 715732 : pthread_mutex_lock(&g_devlist_mutex);
2642 [ + - + - ]: 715732 : i->dev = io_device_get(io_device);
2643 [ + + + - : 715732 : if (i->dev == NULL) {
+ - ]
2644 : 0 : SPDK_ERRLOG("could not find io_device %p\n", io_device);
2645 [ # # ]: 0 : assert(false);
2646 : : i->status = -ENODEV;
2647 : : goto end;
2648 : : }
2649 : :
2650 : : /* Do not allow new for_each operations if we are already waiting to unregister
2651 : : * the device for other for_each operations to complete.
2652 : : */
2653 [ + + + + : 715732 : if (i->dev->pending_unregister) {
+ - + - +
- - + ]
2654 : 0 : SPDK_ERRLOG("io_device %p has a pending unregister\n", io_device);
2655 [ # # # # ]: 0 : i->status = -ENODEV;
2656 : 0 : goto end;
2657 : : }
2658 : :
2659 [ + + + - : 1689179 : TAILQ_FOREACH(thread, &g_threads, tailq) {
+ - + - ]
2660 [ + - + - ]: 1680407 : ch = thread_get_io_channel(thread, i->dev);
2661 [ + + ]: 1680407 : if (ch != NULL) {
2662 [ + - + - : 706960 : ch->dev->for_each_count++;
+ - ]
2663 [ + - + - ]: 706960 : i->cur_thread = thread;
2664 [ + - + - ]: 706960 : i->ch = ch;
2665 [ + + ]: 706960 : pthread_mutex_unlock(&g_devlist_mutex);
2666 : 706960 : rc = spdk_thread_send_msg(thread, _call_channel, i);
2667 [ + + # # ]: 706960 : assert(rc == 0);
2668 : 706960 : return;
2669 : : }
2670 : 1387 : }
2671 : :
2672 : 8719 : end:
2673 [ + + ]: 8772 : pthread_mutex_unlock(&g_devlist_mutex);
2674 : :
2675 [ + - + - ]: 8772 : rc = spdk_thread_send_msg(i->orig_thread, _call_completion, i);
2676 [ + + # # ]: 8772 : assert(rc == 0);
2677 : 1254 : }
2678 : :
2679 : : static void
2680 : 3 : __pending_unregister(void *arg)
2681 : : {
2682 : 3 : struct io_device *dev = arg;
2683 : :
2684 [ - + - + : 3 : assert(dev->pending_unregister);
# # # # #
# ]
2685 [ - + # # : 3 : assert(dev->for_each_count == 0);
# # # # ]
2686 [ # # # # : 3 : spdk_io_device_unregister(dev->io_device, dev->unregister_cb);
# # # # ]
2687 : 3 : }
2688 : :
2689 : : void
2690 : 812776 : spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status)
2691 : : {
2692 : : struct spdk_thread *thread;
2693 : : struct spdk_io_channel *ch;
2694 : : struct io_device *dev;
2695 : : int rc __attribute__((unused));
2696 : :
2697 [ + + + - : 812776 : assert(i->cur_thread == spdk_get_thread());
+ - # # ]
2698 : :
2699 [ + - + - ]: 812776 : i->status = status;
2700 : :
2701 [ + + ]: 812776 : pthread_mutex_lock(&g_devlist_mutex);
2702 [ + - + - ]: 812776 : dev = i->dev;
2703 [ + + ]: 812776 : if (status) {
2704 : 181698 : goto end;
2705 : : }
2706 : :
2707 [ + - + - : 631078 : thread = TAILQ_NEXT(i->cur_thread, tailq);
+ - + - +
- ]
2708 [ + + ]: 1959765 : while (thread) {
2709 : 1434503 : ch = thread_get_io_channel(thread, dev);
2710 [ + + ]: 1434503 : if (ch != NULL) {
2711 [ + - + - ]: 105816 : i->cur_thread = thread;
2712 [ + - + - ]: 105816 : i->ch = ch;
2713 [ + + ]: 105816 : pthread_mutex_unlock(&g_devlist_mutex);
2714 : 105816 : rc = spdk_thread_send_msg(thread, _call_channel, i);
2715 [ + + # # ]: 105816 : assert(rc == 0);
2716 : 105816 : return;
2717 : : }
2718 [ + - + - : 1328687 : thread = TAILQ_NEXT(thread, tailq);
+ - ]
2719 : : }
2720 : :
2721 : 524063 : end:
2722 [ + - ]: 706960 : dev->for_each_count--;
2723 [ + - + - ]: 706960 : i->ch = NULL;
2724 [ + + ]: 706960 : pthread_mutex_unlock(&g_devlist_mutex);
2725 : :
2726 [ + - + - ]: 706960 : rc = spdk_thread_send_msg(i->orig_thread, _call_completion, i);
2727 [ + + # # ]: 706960 : assert(rc == 0);
2728 : :
2729 [ + + ]: 706960 : pthread_mutex_lock(&g_devlist_mutex);
2730 [ + + + + : 706960 : if (dev->pending_unregister && dev->for_each_count == 0) {
+ - - + #
# # # #
# ]
2731 [ # # # # ]: 3 : rc = spdk_thread_send_msg(dev->unregister_thread, __pending_unregister, dev);
2732 [ - + # # ]: 3 : assert(rc == 0);
2733 : 0 : }
2734 [ + + ]: 706960 : pthread_mutex_unlock(&g_devlist_mutex);
2735 : 1222 : }
2736 : :
2737 : : static void
2738 : 122 : thread_interrupt_destroy(struct spdk_thread *thread)
2739 : : {
2740 [ # # # # ]: 122 : struct spdk_fd_group *fgrp = thread->fgrp;
2741 : :
2742 [ - + - + : 122 : SPDK_INFOLOG(thread, "destroy fgrp for thread (%s)\n", thread->name);
# # # # ]
2743 : :
2744 [ - + # # : 122 : if (thread->msg_fd < 0) {
# # ]
2745 : 0 : return;
2746 : : }
2747 : :
2748 [ # # # # ]: 122 : spdk_fd_group_remove(fgrp, thread->msg_fd);
2749 [ # # # # ]: 122 : close(thread->msg_fd);
2750 [ # # # # ]: 122 : thread->msg_fd = -1;
2751 : :
2752 : 122 : spdk_fd_group_destroy(fgrp);
2753 [ # # # # ]: 122 : thread->fgrp = NULL;
2754 : 0 : }
2755 : :
2756 : : #ifdef __linux__
2757 : : static int
2758 : 758581 : thread_interrupt_msg_process(void *arg)
2759 : : {
2760 : 758581 : struct spdk_thread *thread = arg;
2761 : : struct spdk_thread *orig_thread;
2762 : : uint32_t msg_count;
2763 : : spdk_msg_fn critical_msg;
2764 : 758581 : int rc = 0;
2765 : 758581 : uint64_t notify = 1;
2766 : :
2767 [ - + # # ]: 758581 : assert(spdk_interrupt_mode_is_enabled());
2768 : :
2769 : 758581 : orig_thread = spdk_get_thread();
2770 : 758581 : spdk_set_thread(thread);
2771 : :
2772 [ # # # # ]: 758581 : critical_msg = thread->critical_msg;
2773 [ + + ]: 758581 : if (spdk_unlikely(critical_msg != NULL)) {
2774 [ # # # # ]: 41 : critical_msg(NULL);
2775 [ # # # # ]: 41 : thread->critical_msg = NULL;
2776 : 41 : rc = 1;
2777 : 0 : }
2778 : :
2779 : 758581 : msg_count = msg_queue_run_batch(thread, 0);
2780 [ + + ]: 758581 : if (msg_count) {
2781 : 758112 : rc = 1;
2782 : 0 : }
2783 : :
2784 [ - + # # : 758581 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
# # # # #
# # # # #
# # ]
2785 [ + + + + : 758581 : if (spdk_unlikely(!thread->in_interrupt)) {
# # # # ]
2786 : : /* The thread transitioned to poll mode in a msg during the above processing.
2787 : : * Clear msg_fd since thread messages will be polled directly in poll mode.
2788 : : */
2789 [ # # # # ]: 4 : rc = read(thread->msg_fd, ¬ify, sizeof(notify));
2790 [ + - - + : 4 : if (rc < 0 && errno != EAGAIN) {
# # ]
2791 [ # # ]: 0 : SPDK_ERRLOG("failed to acknowledge msg queue: %s.\n", spdk_strerror(errno));
2792 : 0 : }
2793 : 0 : }
2794 : :
2795 : 758581 : spdk_set_thread(orig_thread);
2796 : 758581 : return rc;
2797 : : }
2798 : :
2799 : : static int
2800 : 122 : thread_interrupt_create(struct spdk_thread *thread)
2801 : : {
2802 : 122 : struct spdk_event_handler_opts opts = {};
2803 : : int rc;
2804 : :
2805 [ - + - + : 122 : SPDK_INFOLOG(thread, "Create fgrp for thread (%s)\n", thread->name);
# # # # ]
2806 : :
2807 [ # # ]: 122 : rc = spdk_fd_group_create(&thread->fgrp);
2808 [ - + ]: 122 : if (rc) {
2809 [ # # # # ]: 0 : thread->msg_fd = -1;
2810 : 0 : return rc;
2811 : : }
2812 : :
2813 [ # # # # ]: 122 : thread->msg_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
2814 [ - + # # : 122 : if (thread->msg_fd < 0) {
# # ]
2815 [ # # # # ]: 0 : rc = -errno;
2816 [ # # # # ]: 0 : spdk_fd_group_destroy(thread->fgrp);
2817 [ # # # # ]: 0 : thread->fgrp = NULL;
2818 : :
2819 : 0 : return rc;
2820 : : }
2821 : :
2822 : 122 : spdk_fd_group_get_default_event_handler_opts(&opts, sizeof(opts));
2823 [ # # ]: 122 : opts.fd_type = SPDK_FD_TYPE_EVENTFD;
2824 : :
2825 [ # # # # : 122 : return SPDK_FD_GROUP_ADD_EXT(thread->fgrp, thread->msg_fd,
# # # # ]
2826 : : thread_interrupt_msg_process, thread, &opts);
2827 : 0 : }
2828 : : #else
2829 : : static int
2830 : : thread_interrupt_create(struct spdk_thread *thread)
2831 : : {
2832 : : return -ENOTSUP;
2833 : : }
2834 : : #endif
2835 : :
2836 : : static int
2837 : 232940320 : _interrupt_wrapper(void *ctx)
2838 : : {
2839 : 232940320 : struct spdk_interrupt *intr = ctx;
2840 : : struct spdk_thread *orig_thread, *thread;
2841 : : int rc;
2842 : :
2843 : 232940320 : orig_thread = spdk_get_thread();
2844 [ # # # # ]: 232940320 : thread = intr->thread;
2845 : :
2846 : 232940320 : spdk_set_thread(thread);
2847 : :
2848 : 117271720 : SPDK_DTRACE_PROBE4(interrupt_fd_process, intr->name, intr->efd,
2849 : : intr->fn, intr->arg);
2850 : :
2851 [ # # # # : 232940320 : rc = intr->fn(intr->arg);
# # # # #
# # # ]
2852 : :
2853 [ - + # # : 232940320 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
# # # # #
# # # # #
# # ]
2854 : :
2855 : 232940320 : spdk_set_thread(orig_thread);
2856 : :
2857 : 232940320 : return rc;
2858 : : }
2859 : :
2860 : : struct spdk_interrupt *
2861 : 1810 : spdk_interrupt_register(int efd, spdk_interrupt_fn fn,
2862 : : void *arg, const char *name)
2863 : : {
2864 : 1810 : return spdk_interrupt_register_for_events(efd, SPDK_INTERRUPT_EVENT_IN, fn, arg, name);
2865 : : }
2866 : :
2867 : : struct spdk_interrupt *
2868 : 2008 : spdk_interrupt_register_for_events(int efd, uint32_t events, spdk_interrupt_fn fn, void *arg,
2869 : : const char *name)
2870 : : {
2871 : 2008 : struct spdk_event_handler_opts opts = {};
2872 : :
2873 : 2008 : spdk_fd_group_get_default_event_handler_opts(&opts, sizeof(opts));
2874 [ # # ]: 2008 : opts.events = events;
2875 [ # # ]: 2008 : opts.fd_type = SPDK_FD_TYPE_DEFAULT;
2876 : :
2877 : 2008 : return spdk_interrupt_register_ext(efd, fn, arg, name, &opts);
2878 : : }
2879 : :
2880 : : static struct spdk_interrupt *
2881 : 2011 : alloc_interrupt(int efd, struct spdk_fd_group *fgrp, spdk_interrupt_fn fn, void *arg,
2882 : : const char *name)
2883 : : {
2884 : : struct spdk_thread *thread;
2885 : : struct spdk_interrupt *intr;
2886 : :
2887 : 2011 : thread = spdk_get_thread();
2888 [ - + ]: 2011 : if (!thread) {
2889 [ # # ]: 0 : assert(false);
2890 : : return NULL;
2891 : : }
2892 : :
2893 [ - + # # : 2011 : if (spdk_unlikely(thread->state != SPDK_THREAD_STATE_RUNNING)) {
# # ]
2894 [ # # ]: 0 : SPDK_ERRLOG("thread %s is marked as exited\n", thread->name);
2895 : 0 : return NULL;
2896 : : }
2897 : :
2898 : 2011 : intr = calloc(1, sizeof(*intr));
2899 [ - + ]: 2011 : if (intr == NULL) {
2900 : 0 : SPDK_ERRLOG("Interrupt handler allocation failed\n");
2901 : 0 : return NULL;
2902 : : }
2903 : :
2904 [ + - ]: 2011 : if (name) {
2905 [ # # ]: 2011 : snprintf(intr->name, sizeof(intr->name), "%s", name);
2906 : 0 : } else {
2907 [ # # ]: 0 : snprintf(intr->name, sizeof(intr->name), "%p", fn);
2908 : : }
2909 : :
2910 [ + + - + : 2011 : assert(efd < 0 || fgrp == NULL);
# # ]
2911 [ # # # # ]: 2011 : intr->efd = efd;
2912 [ # # # # ]: 2011 : intr->fgrp = fgrp;
2913 [ # # # # ]: 2011 : intr->thread = thread;
2914 [ # # # # ]: 2011 : intr->fn = fn;
2915 [ # # # # ]: 2011 : intr->arg = arg;
2916 : :
2917 : 2011 : return intr;
2918 : 0 : }
2919 : :
2920 : : struct spdk_interrupt *
2921 : 2009 : spdk_interrupt_register_ext(int efd, spdk_interrupt_fn fn, void *arg, const char *name,
2922 : : struct spdk_event_handler_opts *opts)
2923 : : {
2924 : : struct spdk_interrupt *intr;
2925 : : int ret;
2926 : :
2927 : 2009 : intr = alloc_interrupt(efd, NULL, fn, arg, name);
2928 [ - + ]: 2009 : if (intr == NULL) {
2929 : 0 : return NULL;
2930 : : }
2931 : :
2932 [ # # # # : 2009 : ret = spdk_fd_group_add_ext(intr->thread->fgrp, efd,
# # # # ]
2933 [ # # ]: 2009 : _interrupt_wrapper, intr, intr->name, opts);
2934 [ - + ]: 2009 : if (ret != 0) {
2935 [ # # # # : 0 : SPDK_ERRLOG("thread %s: failed to add fd %d: %s\n",
# # # # ]
2936 : : intr->thread->name, efd, spdk_strerror(-ret));
2937 : 0 : free(intr);
2938 : 0 : return NULL;
2939 : : }
2940 : :
2941 : 2009 : return intr;
2942 : 0 : }
2943 : :
2944 : : static int
2945 : 158699 : interrupt_fd_group_wrapper(void *wrap_ctx, spdk_fd_fn cb_fn, void *cb_ctx)
2946 : : {
2947 : 158699 : struct spdk_interrupt *intr = wrap_ctx;
2948 : : struct spdk_thread *orig_thread, *thread;
2949 : : int rc;
2950 : :
2951 : 158699 : orig_thread = spdk_get_thread();
2952 [ # # # # ]: 158699 : thread = intr->thread;
2953 : :
2954 : 158699 : spdk_set_thread(thread);
2955 [ # # # # ]: 158699 : rc = cb_fn(cb_ctx);
2956 [ - + # # : 158699 : SPIN_ASSERT(thread->lock_count == 0, SPIN_ERR_HOLD_DURING_SWITCH);
# # # # #
# # # # #
# # ]
2957 : 158699 : spdk_set_thread(orig_thread);
2958 : :
2959 : 158699 : return rc;
2960 : : }
2961 : :
2962 : : struct spdk_interrupt *
2963 : 2 : spdk_interrupt_register_fd_group(struct spdk_fd_group *fgrp, const char *name)
2964 : : {
2965 : : struct spdk_interrupt *intr;
2966 : : int rc;
2967 : :
2968 : 2 : intr = alloc_interrupt(-1, fgrp, NULL, NULL, name);
2969 [ - + ]: 2 : if (intr == NULL) {
2970 : 0 : return NULL;
2971 : : }
2972 : :
2973 : 2 : rc = spdk_fd_group_set_wrapper(fgrp, interrupt_fd_group_wrapper, intr);
2974 [ - + ]: 2 : if (rc != 0) {
2975 [ # # # # : 0 : SPDK_ERRLOG("thread %s: failed to set wrapper for fd_group %d: %s\n",
# # # # ]
2976 : : intr->thread->name, spdk_fd_group_get_fd(fgrp), spdk_strerror(-rc));
2977 : 0 : free(intr);
2978 : 0 : return NULL;
2979 : : }
2980 : :
2981 [ # # # # : 2 : rc = spdk_fd_group_nest(intr->thread->fgrp, fgrp);
# # # # ]
2982 [ - + ]: 2 : if (rc != 0) {
2983 [ # # # # : 0 : SPDK_ERRLOG("thread %s: failed to nest fd_group %d: %s\n",
# # # # ]
2984 : : intr->thread->name, spdk_fd_group_get_fd(fgrp), spdk_strerror(-rc));
2985 : 0 : spdk_fd_group_set_wrapper(fgrp, NULL, NULL);
2986 : 0 : free(intr);
2987 : 0 : return NULL;
2988 : : }
2989 : :
2990 : 2 : return intr;
2991 : 0 : }
2992 : :
2993 : : void
2994 : 3934 : spdk_interrupt_unregister(struct spdk_interrupt **pintr)
2995 : : {
2996 : : struct spdk_thread *thread;
2997 : : struct spdk_interrupt *intr;
2998 : :
2999 [ + - ]: 3934 : intr = *pintr;
3000 [ + + ]: 3934 : if (intr == NULL) {
3001 : 1923 : return;
3002 : : }
3003 : :
3004 [ # # ]: 2011 : *pintr = NULL;
3005 : :
3006 : 2011 : thread = spdk_get_thread();
3007 [ - + ]: 2011 : if (!thread) {
3008 [ # # ]: 0 : assert(false);
3009 : : return;
3010 : : }
3011 : :
3012 [ - + # # : 2011 : if (intr->thread != thread) {
# # ]
3013 [ # # # # : 0 : wrong_thread(__func__, intr->name, intr->thread, thread);
# # ]
3014 : 0 : return;
3015 : : }
3016 : :
3017 [ + + # # : 2011 : if (intr->fgrp != NULL) {
# # ]
3018 [ - + # # : 2 : assert(intr->efd < 0);
# # # # ]
3019 [ # # # # : 2 : spdk_fd_group_unnest(thread->fgrp, intr->fgrp);
# # # # ]
3020 [ # # # # ]: 2 : spdk_fd_group_set_wrapper(thread->fgrp, NULL, NULL);
3021 : 0 : } else {
3022 [ # # # # : 2009 : spdk_fd_group_remove(thread->fgrp, intr->efd);
# # # # ]
3023 : : }
3024 : :
3025 : 2011 : free(intr);
3026 : 341 : }
3027 : :
3028 : : int
3029 : 0 : spdk_interrupt_set_event_types(struct spdk_interrupt *intr,
3030 : : enum spdk_interrupt_event_types event_types)
3031 : : {
3032 : : struct spdk_thread *thread;
3033 : :
3034 : 0 : thread = spdk_get_thread();
3035 [ # # ]: 0 : if (!thread) {
3036 [ # # ]: 0 : assert(false);
3037 : : return -EINVAL;
3038 : : }
3039 : :
3040 [ # # # # : 0 : if (intr->thread != thread) {
# # ]
3041 [ # # # # : 0 : wrong_thread(__func__, intr->name, intr->thread, thread);
# # ]
3042 : 0 : return -EINVAL;
3043 : : }
3044 : :
3045 [ # # # # : 0 : if (intr->efd < 0) {
# # ]
3046 [ # # ]: 0 : assert(false);
3047 : : return -EINVAL;
3048 : : }
3049 : :
3050 [ # # # # : 0 : return spdk_fd_group_event_modify(thread->fgrp, intr->efd, event_types);
# # # # ]
3051 : 0 : }
3052 : :
3053 : : int
3054 : 0 : spdk_thread_get_interrupt_fd(struct spdk_thread *thread)
3055 : : {
3056 [ # # # # ]: 0 : return spdk_fd_group_get_fd(thread->fgrp);
3057 : : }
3058 : :
3059 : : struct spdk_fd_group *
3060 : 268 : spdk_thread_get_interrupt_fd_group(struct spdk_thread *thread)
3061 : : {
3062 [ # # # # ]: 268 : return thread->fgrp;
3063 : : }
3064 : :
3065 : : static bool g_interrupt_mode = false;
3066 : :
3067 : : int
3068 : 41 : spdk_interrupt_mode_enable(void)
3069 : : {
3070 : : /* It must be called once prior to initializing the threading library.
3071 : : * g_spdk_msg_mempool will be valid if thread library is initialized.
3072 : : */
3073 [ - + ]: 41 : if (g_spdk_msg_mempool) {
3074 : 0 : SPDK_ERRLOG("Failed due to threading library is already initialized.\n");
3075 : 0 : return -1;
3076 : : }
3077 : :
3078 : : #ifdef __linux__
3079 : 41 : SPDK_NOTICELOG("Set SPDK running in interrupt mode.\n");
3080 : 41 : g_interrupt_mode = true;
3081 : 41 : return 0;
3082 : : #else
3083 : : SPDK_ERRLOG("SPDK interrupt mode supports only Linux platform now.\n");
3084 : : g_interrupt_mode = false;
3085 : : return -ENOTSUP;
3086 : : #endif
3087 : 0 : }
3088 : :
3089 : : bool
3090 : 511111878 : spdk_interrupt_mode_is_enabled(void)
3091 : : {
3092 [ + + ]: 511111878 : return g_interrupt_mode;
3093 : : }
3094 : :
3095 : : #define SSPIN_DEBUG_STACK_FRAMES 16
3096 : :
3097 : : struct sspin_stack {
3098 : : void *addrs[SSPIN_DEBUG_STACK_FRAMES];
3099 : : uint32_t depth;
3100 : : };
3101 : :
3102 : : struct spdk_spinlock_internal {
3103 : : struct sspin_stack init_stack;
3104 : : struct sspin_stack lock_stack;
3105 : : struct sspin_stack unlock_stack;
3106 : : };
3107 : :
3108 : : static void
3109 : 132794 : sspin_init_internal(struct spdk_spinlock *sspin)
3110 : : {
3111 : : #ifdef DEBUG
3112 [ + - + - ]: 132794 : sspin->internal = calloc(1, sizeof(*sspin->internal));
3113 : : #endif
3114 : 132794 : }
3115 : :
3116 : : static void
3117 : 90572 : sspin_fini_internal(struct spdk_spinlock *sspin)
3118 : : {
3119 : : #ifdef DEBUG
3120 [ + - + - ]: 90572 : free(sspin->internal);
3121 [ + - + - ]: 90572 : sspin->internal = NULL;
3122 : : #endif
3123 : 90572 : }
3124 : :
3125 : : #if defined(DEBUG) && defined(SPDK_HAVE_EXECINFO_H)
3126 : : #define SSPIN_GET_STACK(sspin, which) \
3127 : : do { \
3128 : : if (sspin->internal != NULL) { \
3129 : : struct sspin_stack *stack = &sspin->internal->which ## _stack; \
3130 : : stack->depth = backtrace(stack->addrs, SPDK_COUNTOF(stack->addrs)); \
3131 : : } \
3132 : : } while (0)
3133 : : #else
3134 : : #define SSPIN_GET_STACK(sspin, which) do { } while (0)
3135 : : #endif
3136 : :
3137 : : static void
3138 : 57 : sspin_stack_print(const char *title, const struct sspin_stack *sspin_stack)
3139 : : {
3140 : : #ifdef SPDK_HAVE_EXECINFO_H
3141 : : char **stack;
3142 : : size_t i;
3143 : :
3144 : : stack = backtrace_symbols(sspin_stack->addrs, sspin_stack->depth);
3145 : : if (stack == NULL) {
3146 : : SPDK_ERRLOG("Out of memory while allocate stack for %s\n", title);
3147 : : return;
3148 : : }
3149 : : SPDK_ERRLOG(" %s:\n", title);
3150 : : for (i = 0; i < sspin_stack->depth; i++) {
3151 : : /*
3152 : : * This does not print line numbers. In gdb, use something like "list *0x444b6b" or
3153 : : * "list *sspin_stack->addrs[0]". Or more conveniently, load the spdk gdb macros
3154 : : * and use use "print *sspin" or "print sspin->internal.lock_stack". See
3155 : : * gdb_macros.md in the docs directory for details.
3156 : : */
3157 : : SPDK_ERRLOG(" #%" PRIu64 ": %s\n", i, stack[i]);
3158 : : }
3159 : : free(stack);
3160 : : #endif /* SPDK_HAVE_EXECINFO_H */
3161 : 57 : }
3162 : :
3163 : : static void
3164 : 19 : sspin_stacks_print(const struct spdk_spinlock *sspin)
3165 : : {
3166 [ + + + - : 19 : if (sspin->internal == NULL) {
+ - ]
3167 : 0 : return;
3168 : : }
3169 : 19 : SPDK_ERRLOG("spinlock %p\n", sspin);
3170 [ + - + - : 19 : sspin_stack_print("Lock initialized at", &sspin->internal->init_stack);
+ - ]
3171 [ + - + - : 19 : sspin_stack_print("Last locked at", &sspin->internal->lock_stack);
+ - ]
3172 [ + - + - : 19 : sspin_stack_print("Last unlocked at", &sspin->internal->unlock_stack);
+ - ]
3173 : 1 : }
3174 : :
3175 : : void
3176 : 132794 : spdk_spin_init(struct spdk_spinlock *sspin)
3177 : : {
3178 : : int rc;
3179 : :
3180 [ + + ]: 132794 : memset(sspin, 0, sizeof(*sspin));
3181 [ + + + - ]: 132794 : rc = pthread_spin_init(&sspin->spinlock, PTHREAD_PROCESS_PRIVATE);
3182 [ - + # # : 132794 : SPIN_ASSERT_LOG_STACKS(rc == 0, SPIN_ERR_PTHREAD, sspin);
# # # # #
# # # ]
3183 : 132794 : sspin_init_internal(sspin);
3184 : 1623 : SSPIN_GET_STACK(sspin, init);
3185 [ + - + - ]: 132794 : sspin->initialized = true;
3186 : 1623 : }
3187 : :
3188 : : void
3189 : 90575 : spdk_spin_destroy(struct spdk_spinlock *sspin)
3190 : : {
3191 : : int rc;
3192 : :
3193 [ + + + + : 90575 : SPIN_ASSERT_LOG_STACKS(!sspin->destroyed, SPIN_ERR_DESTROYED, sspin);
+ - - + #
# # # # #
# # # # ]
3194 [ + + + + : 90575 : SPIN_ASSERT_LOG_STACKS(sspin->initialized, SPIN_ERR_NOT_INITIALIZED, sspin);
+ - - + #
# # # # #
# # # # ]
3195 [ + + + - : 90575 : SPIN_ASSERT_LOG_STACKS(sspin->thread == NULL, SPIN_ERR_LOCK_HELD, sspin);
- + # # #
# # # # #
# # ]
3196 : :
3197 [ + + + - ]: 90572 : rc = pthread_spin_destroy(&sspin->spinlock);
3198 [ - + # # : 90572 : SPIN_ASSERT_LOG_STACKS(rc == 0, SPIN_ERR_PTHREAD, sspin);
# # # # #
# # # ]
3199 : :
3200 : 90572 : sspin_fini_internal(sspin);
3201 [ + - + - ]: 90572 : sspin->initialized = false;
3202 [ + - + - ]: 90572 : sspin->destroyed = true;
3203 : 514 : }
3204 : :
3205 : : void
3206 : 2423056 : spdk_spin_lock(struct spdk_spinlock *sspin)
3207 : : {
3208 : 2423056 : struct spdk_thread *thread = spdk_get_thread();
3209 : : int rc;
3210 : :
3211 [ + + + + : 2423056 : SPIN_ASSERT_LOG_STACKS(!sspin->destroyed, SPIN_ERR_DESTROYED, sspin);
+ - - + #
# # # # #
# # # # ]
3212 [ + + + + : 2423056 : SPIN_ASSERT_LOG_STACKS(sspin->initialized, SPIN_ERR_NOT_INITIALIZED, sspin);
+ - - + #
# # # # #
# # # # ]
3213 [ + + # # : 2423056 : SPIN_ASSERT_LOG_STACKS(thread != NULL, SPIN_ERR_NOT_SPDK_THREAD, sspin);
# # # # #
# # # ]
3214 [ + + + - : 2423053 : SPIN_ASSERT_LOG_STACKS(thread != sspin->thread, SPIN_ERR_DEADLOCK, sspin);
+ + + - +
- + - - +
+ - ]
3215 : :
3216 [ + + + - ]: 2423046 : rc = pthread_spin_lock(&sspin->spinlock);
3217 [ - + # # : 2423046 : SPIN_ASSERT_LOG_STACKS(rc == 0, SPIN_ERR_PTHREAD, sspin);
# # # # #
# # # ]
3218 : :
3219 [ + - + - ]: 2423046 : sspin->thread = thread;
3220 [ + - + - : 2423046 : sspin->thread->lock_count++;
+ - + - ]
3221 : :
3222 : 103475 : SSPIN_GET_STACK(sspin, lock);
3223 : 103476 : }
3224 : :
3225 : : void
3226 : 2423047 : spdk_spin_unlock(struct spdk_spinlock *sspin)
3227 : : {
3228 : 2423047 : struct spdk_thread *thread = spdk_get_thread();
3229 : : int rc;
3230 : :
3231 [ + + + + : 2423047 : SPIN_ASSERT_LOG_STACKS(!sspin->destroyed, SPIN_ERR_DESTROYED, sspin);
+ - - + #
# # # # #
# # # # ]
3232 [ + + + + : 2423047 : SPIN_ASSERT_LOG_STACKS(sspin->initialized, SPIN_ERR_NOT_INITIALIZED, sspin);
+ - - + #
# # # # #
# # # # ]
3233 [ - + # # : 2423047 : SPIN_ASSERT_LOG_STACKS(thread != NULL, SPIN_ERR_NOT_SPDK_THREAD, sspin);
# # # # #
# # # ]
3234 [ + + + - : 2423047 : SPIN_ASSERT_LOG_STACKS(thread == sspin->thread, SPIN_ERR_WRONG_THREAD, sspin);
- + # # #
# # # # #
# # ]
3235 : :
3236 [ + + + - : 2423041 : SPIN_ASSERT_LOG_STACKS(thread->lock_count > 0, SPIN_ERR_LOCK_COUNT, sspin);
- + # # #
# # # # #
# # ]
3237 [ + - + - ]: 2423041 : thread->lock_count--;
3238 [ + - + - ]: 2423041 : sspin->thread = NULL;
3239 : :
3240 : 103473 : SSPIN_GET_STACK(sspin, unlock);
3241 : :
3242 [ + + + - ]: 2423041 : rc = pthread_spin_unlock(&sspin->spinlock);
3243 [ - + # # : 2423041 : SPIN_ASSERT_LOG_STACKS(rc == 0, SPIN_ERR_PTHREAD, sspin);
# # # # #
# # # ]
3244 : 103473 : }
3245 : :
3246 : : bool
3247 : 7647040 : spdk_spin_held(struct spdk_spinlock *sspin)
3248 : : {
3249 : 7647040 : struct spdk_thread *thread = spdk_get_thread();
3250 : :
3251 [ + + # # : 7647040 : SPIN_ASSERT_RETURN(thread != NULL, SPIN_ERR_NOT_SPDK_THREAD, false);
# # # # #
# # # ]
3252 : :
3253 [ + - + - ]: 7647037 : return sspin->thread == thread;
3254 : 200 : }
3255 : :
3256 : : void
3257 : 0 : spdk_thread_register_post_poller_handler(spdk_post_poller_fn fn, void *fn_arg)
3258 : : {
3259 : : struct spdk_thread *thr;
3260 : :
3261 : 0 : thr = _get_thread();
3262 [ # # # # ]: 0 : assert(thr);
3263 [ # # # # : 0 : if (spdk_unlikely(thr->num_pp_handlers == SPDK_THREAD_MAX_POST_POLLER_HANDLERS)) {
# # ]
3264 : 0 : SPDK_ERRLOG("Too many handlers registered");
3265 : 0 : return;
3266 : : }
3267 : :
3268 [ # # # # : 0 : thr->pp_handlers[thr->num_pp_handlers].fn = fn;
# # # # #
# # # #
# ]
3269 [ # # # # : 0 : thr->pp_handlers[thr->num_pp_handlers].fn_arg = fn_arg;
# # # # #
# # # #
# ]
3270 [ # # ]: 0 : thr->num_pp_handlers++;
3271 : 0 : }
3272 : :
3273 : 3022 : SPDK_LOG_REGISTER_COMPONENT(thread)
|