xref: /spdk/include/spdk_internal/thread.h (revision 47c4304d83bea9b122aa659aede1407492bd8564)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2020 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef SPDK_THREAD_INTERNAL_H_
35 #define SPDK_THREAD_INTERNAL_H_
36 
37 #include "spdk/stdinc.h"
38 #include "spdk/thread.h"
39 
40 #define SPDK_MAX_POLLER_NAME_LEN	256
41 #define SPDK_MAX_THREAD_NAME_LEN	256
42 
43 enum spdk_poller_state {
44 	/* The poller is registered with a thread but not currently executing its fn. */
45 	SPDK_POLLER_STATE_WAITING,
46 
47 	/* The poller is currently running its fn. */
48 	SPDK_POLLER_STATE_RUNNING,
49 
50 	/* The poller was unregistered during the execution of its fn. */
51 	SPDK_POLLER_STATE_UNREGISTERED,
52 
53 	/* The poller is in the process of being paused.  It will be paused
54 	 * during the next time it's supposed to be executed.
55 	 */
56 	SPDK_POLLER_STATE_PAUSING,
57 
58 	/* The poller is registered but currently paused.  It's on the
59 	 * paused_pollers list.
60 	 */
61 	SPDK_POLLER_STATE_PAUSED,
62 };
63 
64 struct spdk_poller {
65 	TAILQ_ENTRY(spdk_poller)	tailq;
66 
67 	/* Current state of the poller; should only be accessed from the poller's thread. */
68 	enum spdk_poller_state		state;
69 
70 	uint64_t			period_ticks;
71 	uint64_t			next_run_tick;
72 	uint64_t			run_count;
73 	uint64_t			busy_count;
74 	spdk_poller_fn			fn;
75 	void				*arg;
76 	struct spdk_thread		*thread;
77 	int				interruptfd;
78 	spdk_poller_set_interrupt_mode_cb set_intr_cb_fn;
79 	void				*set_intr_cb_arg;
80 
81 	char				name[SPDK_MAX_POLLER_NAME_LEN + 1];
82 };
83 
84 enum spdk_thread_state {
85 	/* The thread is pocessing poller and message by spdk_thread_poll(). */
86 	SPDK_THREAD_STATE_RUNNING,
87 
88 	/* The thread is in the process of termination. It reaps unregistering
89 	 * poller are releasing I/O channel.
90 	 */
91 	SPDK_THREAD_STATE_EXITING,
92 
93 	/* The thread is exited. It is ready to call spdk_thread_destroy(). */
94 	SPDK_THREAD_STATE_EXITED,
95 };
96 
97 struct spdk_thread {
98 	uint64_t			tsc_last;
99 	struct spdk_thread_stats	stats;
100 	/*
101 	 * Contains pollers actively running on this thread.  Pollers
102 	 *  are run round-robin. The thread takes one poller from the head
103 	 *  of the ring, executes it, then puts it back at the tail of
104 	 *  the ring.
105 	 */
106 	TAILQ_HEAD(active_pollers_head, spdk_poller)	active_pollers;
107 	/**
108 	 * Contains pollers running on this thread with a periodic timer.
109 	 */
110 	TAILQ_HEAD(timed_pollers_head, spdk_poller)	timed_pollers;
111 	/*
112 	 * Contains paused pollers.  Pollers on this queue are waiting until
113 	 * they are resumed (in which case they're put onto the active/timer
114 	 * queues) or unregistered.
115 	 */
116 	TAILQ_HEAD(paused_pollers_head, spdk_poller)	paused_pollers;
117 	struct spdk_ring		*messages;
118 	int				msg_fd;
119 	SLIST_HEAD(, spdk_msg)		msg_cache;
120 	size_t				msg_cache_count;
121 	spdk_msg_fn			critical_msg;
122 	uint64_t			id;
123 	enum spdk_thread_state		state;
124 	int				pending_unregister_count;
125 
126 	TAILQ_HEAD(, spdk_io_channel)	io_channels;
127 	TAILQ_ENTRY(spdk_thread)	tailq;
128 
129 	char				name[SPDK_MAX_THREAD_NAME_LEN + 1];
130 	struct spdk_cpuset		cpumask;
131 	uint64_t			exit_timeout_tsc;
132 
133 	/* Indicates whether this spdk_thread currently runs in interrupt. */
134 	bool				in_interrupt;
135 	struct spdk_fd_group		*fgrp;
136 
137 	/* User context allocated at the end */
138 	uint8_t				ctx[0];
139 };
140 
141 const char *spdk_poller_state_str(enum spdk_poller_state state);
142 
143 const char *spdk_io_device_get_name(struct io_device *dev);
144 
145 #endif /* SPDK_THREAD_INTERNAL_H_ */
146