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.h"
13 #include <common/compat/poll.h>
14 #include <common/hashtable/hashtable.h>
15 #include <common/pipe.h>
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_thread_handle
{
52 * Queue of struct notification command.
53 * event_pipe must be WRITE(2) to signal that a new command
57 struct lttng_pipe
*event_pipe
;
58 struct cds_list_head list
;
62 * Read side of pipes used to receive channel status info collected
63 * by the various consumer daemons.
69 } channel_monitoring_pipes
;
70 /* Used to wait for the launch of the notification thread. */
75 * This thread maintains an internal state associating clients and triggers.
77 * In order to speed-up and simplify queries, hash tables providing the
78 * following associations are maintained:
80 * - client_socket_ht: associate a client's socket (fd) to its
81 * "struct notification_client".
82 * This hash table owns the "struct notification_client" which must
83 * thus be disposed-of on removal from the hash table.
85 * - client_id_ht: associate a client's id to its "struct notification_client"
86 * This hash table holds a _weak_ reference to the
87 * "struct notification_client".
89 * - channel_triggers_ht:
90 * associates a channel key to a list of
91 * struct lttng_trigger_list_nodes. The triggers in this list are
92 * those that have conditions that apply to a particular channel.
93 * A channel entry is only created when a channel is added; the
94 * list of triggers applying to such a channel is built at that
96 * This hash table owns the list, but not the triggers themselves.
98 * - session_triggers_ht:
99 * associates a session name 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 session.
102 * A session entry is only created when a session is created; the
103 * list of triggers applying to this new session is built at that
104 * moment. This happens at the time of creation of a session_info.
105 * Likewise, the list is destroyed at the time of the session_info's
108 * - channel_state_ht:
109 * associates a pair (channel key, channel domain) to its last
110 * sampled state received from the consumer daemon
111 * (struct channel_state).
112 * This previous sample is kept to implement edge-triggered
113 * conditions as we need to detect the state transitions.
114 * This hash table owns the channel state.
116 * - notification_trigger_clients_ht:
117 * associates notification-emitting triggers to clients
118 * (struct notification_client_list) subscribed to those
120 * The condition's hash and match functions are used directly since
121 * all triggers in this hash table have the "notify" action.
122 * This hash table holds no ownership.
125 * associates a channel_key to a struct channel_info. The hash table
126 * holds the ownership of the struct channel_info.
129 * associates a session_name (hash) to a struct session_info. The
130 * hash table holds no ownership of the struct session_info;
131 * the session_info structure is owned by the session's various
132 * channels through their struct channel_info (ref-counting is used).
135 * associates a trigger to a struct lttng_trigger_ht_element.
136 * The hash table holds the ownership of the
137 * lttng_trigger_ht_elements along with the triggers themselves.
138 * - triggers_by_name_uid_ht:
139 * associates a trigger (name, uid) tuple to
140 * a struct lttng_trigger_ht_element.
141 * The hash table does not hold any ownership and is used strictly
142 * for lookup on registration.
143 * - tracer_event_sources_list:
144 * A list of tracer event source (read side fd) of type
145 * struct notification_event_tracer_event_source_element.
148 * The thread reacts to the following internal events:
149 * 1) creation of a tracing channel,
150 * 2) destruction of a tracing channel,
151 * 3) registration of a trigger,
152 * 4) unregistration of a trigger,
153 * 5) reception of a channel monitor sample from the consumer daemon,
154 * 6) Session rotation ongoing,
155 * 7) Session rotation completed,
156 * 8) registration of a tracer event source,
157 * 9) unregistration of a tracer event source,
159 * Events specific to notification-emitting triggers:
160 * 9) connection of a notification client,
161 * 10) disconnection of a notification client,
162 * 11) subscription of a client to a conditions' notifications,
163 * 12) unsubscription of a client from a conditions' notifications,
166 * 1) Creation of a tracing channel
167 * - notification_trigger_clients_ht is traversed to identify
168 * triggers which apply to this new channel,
169 * - triggers identified are added to the channel_triggers_ht.
170 * - add channel to channels_ht
171 * - if it is the first channel of a session, a session_info is created and
172 * added to the sessions_ht. A list of the triggers associated with that
173 * session is built, and it is added to session_triggers_ht.
175 * 2) Destruction of a tracing channel
176 * - remove entry from channel_triggers_ht, releasing the list wrapper and
178 * - remove entry from the channel_state_ht.
179 * - remove channel from channels_ht
180 * - if it was the last known channel of a session, the session_info
181 * structure is torndown, which in return destroys the list of triggers
182 * applying to that session.
184 * 3) Registration of a trigger
185 * - if the trigger's action is of type "notify",
186 * - traverse the list of conditions of every client to build a list of
187 * clients which have to be notified when this trigger's condition is met,
188 * - add list of clients (even if it is empty) to the
189 * notification_trigger_clients_ht,
190 * - add trigger to channel_triggers_ht (if applicable),
191 * - add trigger to session_triggers_ht (if applicable),
192 * - add trigger to triggers_by_name_uid_ht
193 * - add trigger to triggers_ht
194 * - evaluate the trigger's condition right away to react if that condition
195 * is true from the beginning.
197 * 4) Unregistration of a trigger
198 * - if the trigger's action is of type "notify",
199 * - remove the trigger from the notification_trigger_clients_ht,
200 * - remove trigger from channel_triggers_ht (if applicable),
201 * - remove trigger from session_triggers_ht (if applicable),
202 * - remove trigger from triggers_by_name_uid_ht
203 * - remove trigger from triggers_ht
205 * 5) Reception of a channel monitor sample from the consumer daemon
206 * - evaluate the conditions associated with the triggers found in
207 * the channel_triggers_ht,
208 * - if a condition evaluates to "true" and the condition is of type
209 * "notify", query the notification_trigger_clients_ht and send
210 * a notification to the clients.
212 * 6) Session rotation ongoing
214 * 7) Session rotation completed
216 * 8) Registration of a tracer event source
217 * - Add the tracer event source of the application to
218 * tracer_event_sources_list,
219 * - Add the trace event source to the pollset.
221 * 8) Unregistration of a tracer event source
222 * - Remove the tracer event source of the application from
223 * tracer_event_sources_list,
224 * - Remove the trace event source from the pollset.
226 * 10) Connection of a client
227 * - add client socket to the client_socket_ht,
228 * - add client socket to the client_id_ht.
230 * 11) Disconnection of a client
231 * - remove client socket from the client_id_ht,
232 * - remove client socket from the client_socket_ht,
233 * - traverse all conditions to which the client is subscribed and remove
234 * the client from the notification_trigger_clients_ht.
236 * 12) Subscription of a client to a condition's notifications
237 * - Add the condition to the client's list of subscribed conditions,
238 * - Look-up notification_trigger_clients_ht and add the client to
240 * - Evaluate the condition for the client that subscribed if the trigger
241 * was already registered.
243 * 13) Unsubscription of a client to a condition's notifications
244 * - Remove the condition from the client's list of subscribed conditions,
245 * - Look-up notification_trigger_clients_ht and remove the client
246 * from the list of clients.
248 struct notification_thread_state
{
249 int notification_channel_socket
;
250 struct lttng_poll_event events
;
251 struct cds_lfht
*client_socket_ht
;
252 struct cds_lfht
*client_id_ht
;
253 struct cds_lfht
*channel_triggers_ht
;
254 struct cds_lfht
*session_triggers_ht
;
255 struct cds_lfht
*channel_state_ht
;
256 struct cds_lfht
*notification_trigger_clients_ht
;
257 struct cds_lfht
*channels_ht
;
258 struct cds_lfht
*sessions_ht
;
259 struct cds_lfht
*triggers_ht
;
260 struct cds_lfht
*triggers_by_name_uid_ht
;
262 uint64_t next_tracer_token
;
263 uint64_t name_offset
;
266 * Read side of the pipes used to receive tracer events. As their name
267 * implies, tracer event source activity originate from either
268 * registered applications (user space tracer) or from the kernel
271 * The list is not protected by a lock since add and remove operations
272 * are currently done only by the notification thread through in
273 * response to blocking commands.
275 struct cds_list_head tracer_event_sources_list
;
276 notification_client_id next_notification_client_id
;
277 struct action_executor
*executor
;
280 /* notification_thread_data takes ownership of the channel monitor pipes. */
281 struct notification_thread_handle
*notification_thread_handle_create(
282 struct lttng_pipe
*ust32_channel_monitor_pipe
,
283 struct lttng_pipe
*ust64_channel_monitor_pipe
,
284 struct lttng_pipe
*kernel_channel_monitor_pipe
);
285 void notification_thread_handle_destroy(
286 struct notification_thread_handle
*handle
);
287 struct lttng_thread
*launch_notification_thread(
288 struct notification_thread_handle
*handle
);
290 #endif /* NOTIFICATION_THREAD_H */