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