xref: /spdk/include/spdk_internal/thread.h (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
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				timerfd;
78 
79 	char				name[SPDK_MAX_POLLER_NAME_LEN + 1];
80 };
81 
82 enum spdk_thread_state {
83 	/* The thread is pocessing poller and message by spdk_thread_poll(). */
84 	SPDK_THREAD_STATE_RUNNING,
85 
86 	/* The thread is in the process of termination. It reaps unregistering
87 	 * poller are releasing I/O channel.
88 	 */
89 	SPDK_THREAD_STATE_EXITING,
90 
91 	/* The thread is exited. It is ready to call spdk_thread_destroy(). */
92 	SPDK_THREAD_STATE_EXITED,
93 };
94 
95 struct spdk_thread {
96 	uint64_t			tsc_last;
97 	struct spdk_thread_stats	stats;
98 	/*
99 	 * Contains pollers actively running on this thread.  Pollers
100 	 *  are run round-robin. The thread takes one poller from the head
101 	 *  of the ring, executes it, then puts it back at the tail of
102 	 *  the ring.
103 	 */
104 	TAILQ_HEAD(active_pollers_head, spdk_poller)	active_pollers;
105 	/**
106 	 * Contains pollers running on this thread with a periodic timer.
107 	 */
108 	TAILQ_HEAD(timed_pollers_head, spdk_poller)	timed_pollers;
109 	/*
110 	 * Contains paused pollers.  Pollers on this queue are waiting until
111 	 * they are resumed (in which case they're put onto the active/timer
112 	 * queues) or unregistered.
113 	 */
114 	TAILQ_HEAD(paused_pollers_head, spdk_poller)	paused_pollers;
115 	struct spdk_ring		*messages;
116 	int				msg_fd;
117 	SLIST_HEAD(, spdk_msg)		msg_cache;
118 	size_t				msg_cache_count;
119 	spdk_msg_fn			critical_msg;
120 	uint64_t			id;
121 	enum spdk_thread_state		state;
122 	int				pending_unregister_count;
123 
124 	TAILQ_HEAD(, spdk_io_channel)	io_channels;
125 	TAILQ_ENTRY(spdk_thread)	tailq;
126 
127 	char				name[SPDK_MAX_THREAD_NAME_LEN + 1];
128 	struct spdk_cpuset		cpumask;
129 	uint64_t			exit_timeout_tsc;
130 
131 	bool				interrupt_mode;
132 	struct spdk_fd_group		*fgrp;
133 
134 	/* User context allocated at the end */
135 	uint8_t				ctx[0];
136 };
137 
138 const char *spdk_poller_state_str(enum spdk_poller_state state);
139 
140 const char *spdk_io_device_get_name(struct io_device *dev);
141 
142 #endif /* SPDK_THREAD_INTERNAL_H_ */
143