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