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