2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef NOTIFICATION_THREAD_H
9 #define NOTIFICATION_THREAD_H
11 #include "action-executor.hpp"
13 #include <common/compat/poll.hpp>
14 #include <common/hashtable/hashtable.hpp>
15 #include <common/pipe.hpp>
16 #include <lttng/trigger/trigger.h>
17 #include <lttng/domain.h>
19 #include <semaphore.h>
21 #include <urcu/list.h>
22 #include <urcu/rculfhash.h>
24 typedef uint64_t notification_client_id;
27 * The notification thread holds no ownership of the tracer event source pipe
28 * file descriptor. The tracer management logic must remove the event source
29 * from the notification thread (see external commands) before releasing
30 * this file descriptor.
32 struct notification_event_tracer_event_source_element {
35 * A tracer event source can be removed from the notification thread's
36 * poll set before the end of its lifetime (for instance, when an error
37 * or hang-up is detected on its file descriptor). This is done to
38 * allow the notification thread to ignore follow-up events on this
41 * Under such circumstances, the notification thread still expects
42 * the normal clean-up to occur through the 'REMOVE_TRACER_EVENT_SOURCE'
45 bool is_fd_in_poll_set;
46 enum lttng_domain_type domain;
47 struct cds_list_head node;
50 struct notification_trigger_tokens_ht_element {
52 /* Weak reference to the trigger. */
53 struct lttng_trigger *trigger;
54 struct cds_lfht_node node;
55 /* call_rcu delayed reclaim. */
56 struct rcu_head rcu_node;
59 struct notification_thread_handle {
61 * Queue of struct notification command.
62 * event_pipe must be WRITE(2) to signal that a new command
67 struct cds_list_head list;
71 * Read side of pipes used to receive channel status info collected
72 * by the various consumer daemons.
78 } channel_monitoring_pipes;
79 /* Used to wait for the launch of the notification thread. */
84 * This thread maintains an internal state associating clients and triggers.
86 * In order to speed-up and simplify queries, hash tables providing the
87 * following associations are maintained:
89 * - client_socket_ht: associate a client's socket (fd) to its
90 * "struct notification_client".
91 * This hash table owns the "struct notification_client" which must
92 * thus be disposed-of on removal from the hash table.
94 * - client_id_ht: associate a client's id to its "struct notification_client"
95 * This hash table holds a _weak_ reference to the
96 * "struct notification_client".
98 * - channel_triggers_ht:
99 * associates a channel key to a list of
100 * struct lttng_trigger_list_nodes. The triggers in this list are
101 * those that have conditions that apply to a particular channel.
102 * A channel entry is only created when a channel is added; the
103 * list of triggers applying to such a channel is built at that
105 * This hash table owns the list, but not the triggers themselves.
107 * - session_triggers_ht:
108 * associates a session name to a list of
109 * struct lttng_trigger_list_nodes. The triggers in this list are
110 * those that have conditions that apply to a particular session.
111 * A session entry is only created when a session is created; the
112 * list of triggers applying to this new session is built at that
113 * moment. This happens at the time of creation of a session_info.
114 * Likewise, the list is destroyed at the time of the session_info's
117 * - channel_state_ht:
118 * associates a pair (channel key, channel domain) to its last
119 * sampled state received from the consumer daemon
120 * (struct channel_state).
121 * This previous sample is kept to implement edge-triggered
122 * conditions as we need to detect the state transitions.
123 * This hash table owns the channel state.
125 * - notification_trigger_clients_ht:
126 * associates notification-emitting triggers to clients
127 * (struct notification_client_list) subscribed to those
129 * The condition's hash and match functions are used directly since
130 * all triggers in this hash table have the "notify" action.
131 * This hash table holds no ownership.
134 * associates a channel_key to a struct channel_info. The hash table
135 * holds the ownership of the struct channel_info.
138 * associates a session_name (hash) to a struct session_info. The
139 * hash table holds no ownership of the struct session_info;
140 * the session_info structure is owned by the session's various
141 * channels through their struct channel_info (ref-counting is used).
144 * associates a trigger to a struct lttng_trigger_ht_element.
145 * The hash table holds the ownership of the
146 * lttng_trigger_ht_elements along with the triggers themselves.
147 * - triggers_by_name_uid_ht:
148 * associates a trigger (name, uid) tuple to
149 * a struct lttng_trigger_ht_element.
150 * The hash table does not hold any ownership and is used strictly
151 * for lookup on registration.
152 * - tracer_event_sources_list:
153 * A list of tracer event source (read side fd) of type
154 * struct notification_event_tracer_event_source_element.
157 * The thread reacts to the following internal events:
158 * 1) creation of a tracing channel,
159 * 2) destruction of a tracing channel,
160 * 3) creation of a tracing session,
161 * 4) destruction of a tracing session,
162 * 5) registration of a trigger,
163 * 6) unregistration of a trigger,
164 * 7) reception of a channel monitor sample from the consumer daemon,
165 * 8) Session rotation ongoing,
166 * 9) Session rotation completed,
167 * 10) registration of a tracer event source,
168 * 11) unregistration of a tracer event source,
170 * Events specific to notification-emitting triggers:
171 * 9) connection of a notification client,
172 * 10) disconnection of a notification client,
173 * 11) subscription of a client to a conditions' notifications,
174 * 12) unsubscription of a client from a conditions' notifications,
177 * 1) Creation of a tracing channel
178 * - notification_trigger_clients_ht is traversed to identify
179 * triggers which apply to this new channel,
180 * - triggers identified are added to the channel_triggers_ht.
181 * - add channel to channels_ht
182 * - if it is the first channel of a session, a session_info is created and
183 * added to the sessions_ht. A list of the triggers associated with that
184 * session is built, and it is added to session_triggers_ht.
186 * 2) Destruction of a tracing channel
187 * - remove entry from channel_triggers_ht, releasing the list wrapper and
189 * - remove entry from the channel_state_ht.
190 * - remove channel from channels_ht
191 * - if it was the last known channel of a session, the session_info
192 * structure is torndown, which in return destroys the list of triggers
193 * applying to that session.
195 * 3) Registration of a trigger
196 * - if the trigger's action is of type "notify",
197 * - traverse the list of conditions of every client to build a list of
198 * clients which have to be notified when this trigger's condition is met,
199 * - add list of clients (even if it is empty) to the
200 * notification_trigger_clients_ht,
201 * - add trigger to channel_triggers_ht (if applicable),
202 * - add trigger to session_triggers_ht (if applicable),
203 * - add trigger to triggers_by_name_uid_ht
204 * - add trigger to triggers_ht
205 * - evaluate the trigger's condition right away to react if that condition
206 * is true from the beginning.
208 * 4) Unregistration of a trigger
209 * - if the trigger's action is of type "notify",
210 * - remove the trigger from the notification_trigger_clients_ht,
211 * - remove trigger from channel_triggers_ht (if applicable),
212 * - remove trigger from session_triggers_ht (if applicable),
213 * - remove trigger from triggers_by_name_uid_ht
214 * - remove trigger from triggers_ht
216 * 5) Reception of a channel monitor sample from the consumer daemon
217 * - evaluate the conditions associated with the triggers found in
218 * the channel_triggers_ht,
219 * - if a condition evaluates to "true" and the condition is of type
220 * "notify", query the notification_trigger_clients_ht and send
221 * a notification to the clients.
223 * 6) Session rotation ongoing
225 * 7) Session rotation completed
227 * 8) Registration of a tracer event source
228 * - Add the tracer event source of the application to
229 * tracer_event_sources_list,
230 * - Add the trace event source to the pollset.
232 * 8) Unregistration of a tracer event source
233 * - Remove the tracer event source of the application from
234 * tracer_event_sources_list,
235 * - Remove the trace event source from the pollset.
237 * 10) Connection of a client
238 * - add client socket to the client_socket_ht,
239 * - add client socket to the client_id_ht.
241 * 11) Disconnection of a client
242 * - remove client socket from the client_id_ht,
243 * - remove client socket from the client_socket_ht,
244 * - traverse all conditions to which the client is subscribed and remove
245 * the client from the notification_trigger_clients_ht.
247 * 12) Subscription of a client to a condition's notifications
248 * - Add the condition to the client's list of subscribed conditions,
249 * - Look-up notification_trigger_clients_ht and add the client to
251 * - Evaluate the condition for the client that subscribed if the trigger
252 * was already registered.
254 * 13) Unsubscription of a client to a condition's notifications
255 * - Remove the condition from the client's list of subscribed conditions,
256 * - Look-up notification_trigger_clients_ht and remove the client
257 * from the list of clients.
259 struct notification_thread_state {
260 int notification_channel_socket;
261 struct lttng_poll_event events;
262 struct cds_lfht *client_socket_ht;
263 struct cds_lfht *client_id_ht;
264 struct cds_lfht *channel_triggers_ht;
265 struct cds_lfht *session_triggers_ht;
266 struct cds_lfht *channel_state_ht;
267 struct cds_lfht *notification_trigger_clients_ht;
268 struct cds_lfht *channels_ht;
269 struct cds_lfht *sessions_ht;
270 struct cds_lfht *triggers_ht;
271 struct cds_lfht *triggers_by_name_uid_ht;
272 struct cds_lfht *trigger_tokens_ht;
274 uint64_t next_tracer_token;
275 uint64_t name_offset;
278 * Read side of the pipes used to receive tracer events. As their name
279 * implies, tracer event source activity originate from either
280 * registered applications (user space tracer) or from the kernel
283 * The list is not protected by a lock since add and remove operations
284 * are currently done only by the notification thread through in
285 * response to blocking commands.
287 struct cds_list_head tracer_event_sources_list;
288 notification_client_id next_notification_client_id;
289 struct action_executor *executor;
292 * Indicates the thread to break for the poll event processing loop and
293 * call _poll_wait() again.
295 * This is necessary because some events on one fd might trigger the
296 * consumption of another fd.
297 * For example, a single _poll_wait() call can return notification
298 * thread commands and events from the tracer event source (event
300 * Picture a scenario where we receive two events:
301 * the first one is a _REMOVE_TRACER_EVENT_SOURCE command, and
302 * the second is an POLLIN on the tracer event source fd.
304 * The _REMOVE_TRACER_EVENT_SOURCE will read all the data of the
305 * removed tracer event source.
307 * The second event is now invalid has we consumed all the data for
308 * which we received the POLLIN.
310 * For this reason, we need to break for the event processing loop and
311 * call _poll_wait() again to get a clean view of the activity on the
317 /* notification_thread_data takes ownership of the channel monitor pipes. */
318 struct notification_thread_handle *notification_thread_handle_create(
319 struct lttng_pipe *ust32_channel_monitor_pipe,
320 struct lttng_pipe *ust64_channel_monitor_pipe,
321 struct lttng_pipe *kernel_channel_monitor_pipe);
322 void notification_thread_handle_destroy(
323 struct notification_thread_handle *handle);
324 struct lttng_thread *launch_notification_thread(
325 struct notification_thread_handle *handle);
327 #endif /* NOTIFICATION_THREAD_H */