Fix: file-descriptor: missing include guards
[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"
13#include <common/compat/poll.hpp>
14#include <common/hashtable/hashtable.hpp>
15#include <common/pipe.hpp>
f2b3ef9f 16#include <lttng/trigger/trigger.h>
d02d7404 17#include <lttng/domain.h>
ab0ee2ca 18#include <pthread.h>
90936dcf 19#include <semaphore.h>
f2b3ef9f
JG
20#include <urcu.h>
21#include <urcu/list.h>
22#include <urcu/rculfhash.h>
ab0ee2ca 23
e665dfbc 24using notification_client_id = uint64_t;
ac1889bf 25
d02d7404
JR
26/*
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.
31 */
32struct notification_event_tracer_event_source_element {
33 int fd;
34 /*
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
39 * file descriptors.
40 *
41 * Under such circumstances, the notification thread still expects
42 * the normal clean-up to occur through the 'REMOVE_TRACER_EVENT_SOURCE'
43 * command.
44 */
45 bool is_fd_in_poll_set;
46 enum lttng_domain_type domain;
47 struct cds_list_head node;
48};
49
e7c93cf9
JR
50struct notification_trigger_tokens_ht_element {
51 uint64_t token;
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;
57};
58
ab0ee2ca
JG
59struct notification_thread_handle {
60 /*
61 * Queue of struct notification command.
814b4934 62 * event_pipe must be WRITE(2) to signal that a new command
ab0ee2ca
JG
63 * has been enqueued.
64 */
65 struct {
f370852f 66 int event_fd;
ab0ee2ca
JG
67 struct cds_list_head list;
68 pthread_mutex_t lock;
69 } cmd_queue;
70 /*
71 * Read side of pipes used to receive channel status info collected
72 * by the various consumer daemons.
73 */
74 struct {
75 int ust32_consumer;
76 int ust64_consumer;
77 int kernel_consumer;
78 } channel_monitoring_pipes;
c8a9de5a
JG
79 /* Used to wait for the launch of the notification thread. */
80 sem_t ready;
ab0ee2ca
JG
81};
82
74df9916
JG
83/**
84 * This thread maintains an internal state associating clients and triggers.
85 *
86 * In order to speed-up and simplify queries, hash tables providing the
87 * following associations are maintained:
88 *
ac1889bf
JG
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.
93 *
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".
74df9916
JG
97 *
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
ea9a44f0 101 * those that have conditions that apply to a particular channel.
74df9916
JG
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
104 * moment.
105 * This hash table owns the list, but not the triggers themselves.
106 *
ea9a44f0
JG
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
115 * destruction.
116 *
74df9916
JG
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.
124 *
125 * - notification_trigger_clients_ht:
126 * associates notification-emitting triggers to clients
127 * (struct notification_client_list) subscribed to those
128 * conditions.
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.
132 *
133 * - channels_ht:
134 * associates a channel_key to a struct channel_info. The hash table
135 * holds the ownership of the struct channel_info.
136 *
8abe313a
JG
137 * - sessions_ht:
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).
142 *
74df9916 143 * - triggers_ht:
242388e4 144 * associates a trigger to a struct lttng_trigger_ht_element.
74df9916
JG
145 * The hash table holds the ownership of the
146 * lttng_trigger_ht_elements along with the triggers themselves.
242388e4
JR
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.
d02d7404
JR
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.
155*
74df9916
JG
156 *
157 * The thread reacts to the following internal events:
158 * 1) creation of a tracing channel,
159 * 2) destruction of a tracing channel,
139a8d25
JG
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,
74df9916
JG
169 *
170 * Events specific to notification-emitting triggers:
d02d7404
JR
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,
74df9916
JG
175 *
176 *
177 * 1) Creation of a tracing channel
178 * - notification_trigger_clients_ht is traversed to identify
179 * triggers which apply to this new channel,
8abe313a 180 * - triggers identified are added to the channel_triggers_ht.
74df9916 181 * - add channel to channels_ht
ea9a44f0
JG
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.
74df9916
JG
185 *
186 * 2) Destruction of a tracing channel
187 * - remove entry from channel_triggers_ht, releasing the list wrapper and
188 * elements,
189 * - remove entry from the channel_state_ht.
190 * - remove channel from channels_ht
ea9a44f0
JG
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.
74df9916
JG
194 *
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),
ea9a44f0 202 * - add trigger to session_triggers_ht (if applicable),
242388e4 203 * - add trigger to triggers_by_name_uid_ht
74df9916 204 * - add trigger to triggers_ht
2ae99f0b
JG
205 * - evaluate the trigger's condition right away to react if that condition
206 * is true from the beginning.
74df9916
JG
207 *
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),
ea9a44f0 212 * - remove trigger from session_triggers_ht (if applicable),
242388e4 213 * - remove trigger from triggers_by_name_uid_ht
74df9916
JG
214 * - remove trigger from triggers_ht
215 *
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.
222 *
51eab943
JG
223 * 6) Session rotation ongoing
224 *
225 * 7) Session rotation completed
226 *
d02d7404
JR
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.
231 *
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.
236 *
237 * 10) Connection of a client
ac1889bf
JG
238 * - add client socket to the client_socket_ht,
239 * - add client socket to the client_id_ht.
74df9916 240 *
d02d7404 241 * 11) Disconnection of a client
ac1889bf 242 * - remove client socket from the client_id_ht,
74df9916
JG
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.
246 *
d02d7404 247 * 12) Subscription of a client to a condition's notifications
74df9916
JG
248 * - Add the condition to the client's list of subscribed conditions,
249 * - Look-up notification_trigger_clients_ht and add the client to
250 * list of clients.
2ae99f0b
JG
251 * - Evaluate the condition for the client that subscribed if the trigger
252 * was already registered.
74df9916 253 *
d02d7404 254 * 13) Unsubscription of a client to a condition's notifications
74df9916
JG
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.
258 */
ab0ee2ca
JG
259struct notification_thread_state {
260 int notification_channel_socket;
261 struct lttng_poll_event events;
262 struct cds_lfht *client_socket_ht;
ac1889bf 263 struct cds_lfht *client_id_ht;
ab0ee2ca 264 struct cds_lfht *channel_triggers_ht;
ea9a44f0 265 struct cds_lfht *session_triggers_ht;
ab0ee2ca
JG
266 struct cds_lfht *channel_state_ht;
267 struct cds_lfht *notification_trigger_clients_ht;
268 struct cds_lfht *channels_ht;
8abe313a 269 struct cds_lfht *sessions_ht;
ab0ee2ca 270 struct cds_lfht *triggers_ht;
242388e4 271 struct cds_lfht *triggers_by_name_uid_ht;
e7c93cf9 272 struct cds_lfht *trigger_tokens_ht;
242388e4 273 struct {
e6887944 274 uint64_t next_tracer_token;
242388e4
JR
275 uint64_t name_offset;
276 } trigger_id;
d02d7404
JR
277 /*
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
281 * tracer.
282 *
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.
286 */
287 struct cds_list_head tracer_event_sources_list;
ac1889bf 288 notification_client_id next_notification_client_id;
f2b3ef9f 289 struct action_executor *executor;
8b524060
FD
290
291 /*
292 * Indicates the thread to break for the poll event processing loop and
293 * call _poll_wait() again.
294 *
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
299 * notifier).
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.
303 *
304 * The _REMOVE_TRACER_EVENT_SOURCE will read all the data of the
305 * removed tracer event source.
306 *
307 * The second event is now invalid has we consumed all the data for
308 * which we received the POLLIN.
309 *
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
312 * fds.
313 */
314 bool restart_poll;
ab0ee2ca
JG
315};
316
317/* notification_thread_data takes ownership of the channel monitor pipes. */
318struct notification_thread_handle *notification_thread_handle_create(
319 struct lttng_pipe *ust32_channel_monitor_pipe,
320 struct lttng_pipe *ust64_channel_monitor_pipe,
c8a9de5a 321 struct lttng_pipe *kernel_channel_monitor_pipe);
ab0ee2ca
JG
322void notification_thread_handle_destroy(
323 struct notification_thread_handle *handle);
4a91420c
JG
324struct lttng_thread *launch_notification_thread(
325 struct notification_thread_handle *handle);
ab0ee2ca
JG
326
327#endif /* NOTIFICATION_THREAD_H */
This page took 0.073656 seconds and 4 git commands to generate.