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>
18 #include <semaphore.h>
20 #include <urcu/list.h>
21 #include <urcu/rculfhash.h>
23 typedef uint64_t notification_client_id
;
25 struct notification_event_trigger_source_element
{
27 struct cds_list_head node
;
30 struct notification_trigger_tokens_ht_element
{
32 struct lttng_trigger
*trigger
;
33 struct cds_lfht_node node
;
34 /* call_rcu delayed reclaim. */
35 struct rcu_head rcu_node
;
38 struct notification_thread_handle
{
40 * Queue of struct notification command.
41 * event_pipe must be WRITE(2) to signal that a new command
45 struct lttng_pipe
*event_pipe
;
46 struct cds_list_head list
;
50 * Read side of pipes used to receive channel status info collected
51 * by the various consumer daemons.
57 } channel_monitoring_pipes
;
59 * Read side of pipes used to reveice event trigger generetated by
60 * registered applications
63 /* List of notification_event_trigger_source_element */
64 struct cds_list_head list
;
67 } event_trigger_sources
;
68 /* Used to wait for the launch of the notification thread. */
73 * This thread maintains an internal state associating clients and triggers.
75 * In order to speed-up and simplify queries, hash tables providing the
76 * following associations are maintained:
78 * - client_socket_ht: associate a client's socket (fd) to its
79 * "struct notification_client".
80 * This hash table owns the "struct notification_client" which must
81 * thus be disposed-of on removal from the hash table.
83 * - client_id_ht: associate a client's id to its "struct notification_client"
84 * This hash table holds a _weak_ reference to the
85 * "struct notification_client".
87 * - channel_triggers_ht:
88 * associates a channel key to a list of
89 * struct lttng_trigger_list_nodes. The triggers in this list are
90 * those that have conditions that apply to a particular channel.
91 * A channel entry is only created when a channel is added; the
92 * list of triggers applying to such a channel is built at that
94 * This hash table owns the list, but not the triggers themselves.
96 * - session_triggers_ht:
97 * associates a session name to a list of
98 * struct lttng_trigger_list_nodes. The triggers in this list are
99 * those that have conditions that apply to a particular session.
100 * A session entry is only created when a session is created; the
101 * list of triggers applying to this new session is built at that
102 * moment. This happens at the time of creation of a session_info.
103 * Likewise, the list is destroyed at the time of the session_info's
106 * - channel_state_ht:
107 * associates a pair (channel key, channel domain) to its last
108 * sampled state received from the consumer daemon
109 * (struct channel_state).
110 * This previous sample is kept to implement edge-triggered
111 * conditions as we need to detect the state transitions.
112 * This hash table owns the channel state.
114 * - notification_trigger_clients_ht:
115 * associates notification-emitting triggers to clients
116 * (struct notification_client_list) subscribed to those
118 * The condition's hash and match functions are used directly since
119 * all triggers in this hash table have the "notify" action.
120 * This hash table holds no ownership.
123 * associates a channel_key to a struct channel_info. The hash table
124 * holds the ownership of the struct channel_info.
127 * associates a session_name (hash) to a struct session_info. The
128 * hash table holds no ownership of the struct session_info;
129 * the session_info structure is owned by the session's various
130 * channels through their struct channel_info (ref-counting is used).
133 * associates a trigger to a struct lttng_trigger_ht_element.
134 * The hash table holds the ownership of the
135 * lttng_trigger_ht_elements along with the triggers themselves.
136 * - triggers_by_name_ht:
137 * associates a trigger name to a struct lttng_trigger_ht_element.
138 * The hash table does not hold any ownership and is used strictly
139 * for lookup on registration.
141 * The thread reacts to the following internal events:
142 * 1) creation of a tracing channel,
143 * 2) destruction of a tracing channel,
144 * 3) registration of a trigger,
145 * 4) unregistration of a trigger,
146 * 5) reception of a channel monitor sample from the consumer daemon.
147 * 6) Session rotation ongoing
148 * 7) Session rotation completed
150 * Events specific to notification-emitting triggers:
151 * 8) connection of a notification client,
152 * 9) disconnection of a notification client,
153 * 10) subscription of a client to a conditions' notifications,
154 * 11) unsubscription of a client from a conditions' notifications,
157 * 1) Creation of a tracing channel
158 * - notification_trigger_clients_ht is traversed to identify
159 * triggers which apply to this new channel,
160 * - triggers identified are added to the channel_triggers_ht.
161 * - add channel to channels_ht
162 * - if it is the first channel of a session, a session_info is created and
163 * added to the sessions_ht. A list of the triggers associated with that
164 * session is built, and it is added to session_triggers_ht.
166 * 2) Destruction of a tracing channel
167 * - remove entry from channel_triggers_ht, releasing the list wrapper and
169 * - remove entry from the channel_state_ht.
170 * - remove channel from channels_ht
171 * - if it was the last known channel of a session, the session_info
172 * structure is torndown, which in return destroys the list of triggers
173 * applying to that session.
175 * 3) Registration of a trigger
176 * - if the trigger's action is of type "notify",
177 * - traverse the list of conditions of every client to build a list of
178 * clients which have to be notified when this trigger's condition is met,
179 * - add list of clients (even if it is empty) to the
180 * notification_trigger_clients_ht,
181 * - add trigger to channel_triggers_ht (if applicable),
182 * - add trigger to session_triggers_ht (if applicable),
183 * - add trigger to triggers_by_name_ht
184 * - add trigger to triggers_ht
185 * - evaluate the trigger's condition right away to react if that condition
186 * is true from the beginning.
188 * 4) Unregistration of a trigger
189 * - if the trigger's action is of type "notify",
190 * - remove the trigger from the notification_trigger_clients_ht,
191 * - remove trigger from channel_triggers_ht (if applicable),
192 * - remove trigger from session_triggers_ht (if applicable),
193 * - remove trigger to triggers_by_name_ht
194 * - remove trigger from triggers_ht
196 * 5) Reception of a channel monitor sample from the consumer daemon
197 * - evaluate the conditions associated with the triggers found in
198 * the channel_triggers_ht,
199 * - if a condition evaluates to "true" and the condition is of type
200 * "notify", query the notification_trigger_clients_ht and send
201 * a notification to the clients.
203 * 6) Session rotation ongoing
205 * 7) Session rotation completed
207 * 8) Connection of a client
208 * - add client socket to the client_socket_ht,
209 * - add client socket to the client_id_ht.
211 * 9) Disconnection of a client
212 * - remove client socket from the client_id_ht,
213 * - remove client socket from the client_socket_ht,
214 * - traverse all conditions to which the client is subscribed and remove
215 * the client from the notification_trigger_clients_ht.
217 * 10) Subscription of a client to a condition's notifications
218 * - Add the condition to the client's list of subscribed conditions,
219 * - Look-up notification_trigger_clients_ht and add the client to
221 * - Evaluate the condition for the client that subscribed if the trigger
222 * was already registered.
224 * 11) Unsubscription of a client to a condition's notifications
225 * - Remove the condition from the client's list of subscribed conditions,
226 * - Look-up notification_trigger_clients_ht and remove the client
227 * from the list of clients.
229 struct notification_thread_state
{
230 int notification_channel_socket
;
231 struct lttng_poll_event events
;
232 struct cds_lfht
*client_socket_ht
;
233 struct cds_lfht
*client_id_ht
;
234 struct cds_lfht
*channel_triggers_ht
;
235 struct cds_lfht
*session_triggers_ht
;
236 struct cds_lfht
*channel_state_ht
;
237 struct cds_lfht
*notification_trigger_clients_ht
;
238 struct cds_lfht
*channels_ht
;
239 struct cds_lfht
*sessions_ht
;
240 struct cds_lfht
*triggers_ht
;
241 struct cds_lfht
*triggers_by_name_ht
;
242 struct cds_lfht
*trigger_tokens_ht
;
244 uint64_t token_generator
;
245 uint64_t name_offset
;
247 notification_client_id next_notification_client_id
;
248 struct action_executor
*executor
;
251 /* notification_thread_data takes ownership of the channel monitor pipes. */
252 struct notification_thread_handle
*notification_thread_handle_create(
253 struct lttng_pipe
*ust32_channel_monitor_pipe
,
254 struct lttng_pipe
*ust64_channel_monitor_pipe
,
255 struct lttng_pipe
*kernel_channel_monitor_pipe
,
256 int kernel_notification_monitor_fd
);
257 void notification_thread_handle_destroy(
258 struct notification_thread_handle
*handle
);
259 struct lttng_thread
*launch_notification_thread(
260 struct notification_thread_handle
*handle
);
262 #endif /* NOTIFICATION_THREAD_H */