SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread.h
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef NOTIFICATION_THREAD_H
9 #define NOTIFICATION_THREAD_H
10
11 #include "action-executor.h"
12 #include "thread.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 <pthread.h>
18 #include <semaphore.h>
19 #include <urcu.h>
20 #include <urcu/list.h>
21 #include <urcu/rculfhash.h>
22
23 typedef uint64_t notification_client_id;
24
25 struct notification_event_trigger_source_element {
26 int fd;
27 struct cds_list_head node;
28 };
29
30 struct notification_trigger_tokens_ht_element {
31 uint64_t token;
32 struct lttng_trigger *trigger;
33 struct cds_lfht_node node;
34 /* call_rcu delayed reclaim. */
35 struct rcu_head rcu_node;
36 };
37
38 struct notification_thread_handle {
39 /*
40 * Queue of struct notification command.
41 * event_pipe must be WRITE(2) to signal that a new command
42 * has been enqueued.
43 */
44 struct {
45 struct lttng_pipe *event_pipe;
46 struct cds_list_head list;
47 pthread_mutex_t lock;
48 } cmd_queue;
49 /*
50 * Read side of pipes used to receive channel status info collected
51 * by the various consumer daemons.
52 */
53 struct {
54 int ust32_consumer;
55 int ust64_consumer;
56 int kernel_consumer;
57 } channel_monitoring_pipes;
58 /*
59 * Read side of pipes used to reveice event trigger generetated by
60 * registered applications
61 */
62 struct {
63 /* List of notification_event_trigger_source_element */
64 struct cds_list_head list;
65 pthread_mutex_t lock;
66 int kernel_tracer;
67 } event_trigger_sources;
68 /* Used to wait for the launch of the notification thread. */
69 sem_t ready;
70
71 pthread_mutex_t trigger_lock;
72 };
73
74 /**
75 * This thread maintains an internal state associating clients and triggers.
76 *
77 * In order to speed-up and simplify queries, hash tables providing the
78 * following associations are maintained:
79 *
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.
84 *
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".
88 *
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
95 * moment.
96 * This hash table owns the list, but not the triggers themselves.
97 *
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
106 * destruction.
107 *
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.
115 *
116 * - notification_trigger_clients_ht:
117 * associates notification-emitting triggers to clients
118 * (struct notification_client_list) subscribed to those
119 * conditions.
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.
123 *
124 * - channels_ht:
125 * associates a channel_key to a struct channel_info. The hash table
126 * holds the ownership of the struct channel_info.
127 *
128 * - sessions_ht:
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).
133 *
134 * - triggers_ht:
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_ht:
139 * associates a trigger name to a struct lttng_trigger_ht_element.
140 * The hash table does not hold any ownership and is used strictly
141 * for lookup on registration.
142 *
143 * The thread reacts to the following internal events:
144 * 1) creation of a tracing channel,
145 * 2) destruction of a tracing channel,
146 * 3) registration of a trigger,
147 * 4) unregistration of a trigger,
148 * 5) reception of a channel monitor sample from the consumer daemon.
149 * 6) Session rotation ongoing
150 * 7) Session rotation completed
151 *
152 * Events specific to notification-emitting triggers:
153 * 8) connection of a notification client,
154 * 9) disconnection of a notification client,
155 * 10) subscription of a client to a conditions' notifications,
156 * 11) unsubscription of a client from a conditions' notifications,
157 *
158 *
159 * 1) Creation of a tracing channel
160 * - notification_trigger_clients_ht is traversed to identify
161 * triggers which apply to this new channel,
162 * - triggers identified are added to the channel_triggers_ht.
163 * - add channel to channels_ht
164 * - if it is the first channel of a session, a session_info is created and
165 * added to the sessions_ht. A list of the triggers associated with that
166 * session is built, and it is added to session_triggers_ht.
167 *
168 * 2) Destruction of a tracing channel
169 * - remove entry from channel_triggers_ht, releasing the list wrapper and
170 * elements,
171 * - remove entry from the channel_state_ht.
172 * - remove channel from channels_ht
173 * - if it was the last known channel of a session, the session_info
174 * structure is torndown, which in return destroys the list of triggers
175 * applying to that session.
176 *
177 * 3) Registration of a trigger
178 * - if the trigger's action is of type "notify",
179 * - traverse the list of conditions of every client to build a list of
180 * clients which have to be notified when this trigger's condition is met,
181 * - add list of clients (even if it is empty) to the
182 * notification_trigger_clients_ht,
183 * - add trigger to channel_triggers_ht (if applicable),
184 * - add trigger to session_triggers_ht (if applicable),
185 * - add trigger to triggers_by_name_ht
186 * - add trigger to triggers_ht
187 * - evaluate the trigger's condition right away to react if that condition
188 * is true from the beginning.
189 *
190 * 4) Unregistration of a trigger
191 * - if the trigger's action is of type "notify",
192 * - remove the trigger from the notification_trigger_clients_ht,
193 * - remove trigger from channel_triggers_ht (if applicable),
194 * - remove trigger from session_triggers_ht (if applicable),
195 * - remove trigger to triggers_by_name_ht
196 * - remove trigger from triggers_ht
197 *
198 * 5) Reception of a channel monitor sample from the consumer daemon
199 * - evaluate the conditions associated with the triggers found in
200 * the channel_triggers_ht,
201 * - if a condition evaluates to "true" and the condition is of type
202 * "notify", query the notification_trigger_clients_ht and send
203 * a notification to the clients.
204 *
205 * 6) Session rotation ongoing
206 *
207 * 7) Session rotation completed
208 *
209 * 8) Connection of a client
210 * - add client socket to the client_socket_ht,
211 * - add client socket to the client_id_ht.
212 *
213 * 9) Disconnection of a client
214 * - remove client socket from the client_id_ht,
215 * - remove client socket from the client_socket_ht,
216 * - traverse all conditions to which the client is subscribed and remove
217 * the client from the notification_trigger_clients_ht.
218 *
219 * 10) Subscription of a client to a condition's notifications
220 * - Add the condition to the client's list of subscribed conditions,
221 * - Look-up notification_trigger_clients_ht and add the client to
222 * list of clients.
223 * - Evaluate the condition for the client that subscribed if the trigger
224 * was already registered.
225 *
226 * 11) Unsubscription of a client to a condition's notifications
227 * - Remove the condition from the client's list of subscribed conditions,
228 * - Look-up notification_trigger_clients_ht and remove the client
229 * from the list of clients.
230 */
231 struct notification_thread_state {
232 int notification_channel_socket;
233 struct lttng_poll_event events;
234 struct cds_lfht *client_socket_ht;
235 struct cds_lfht *client_id_ht;
236 struct cds_lfht *channel_triggers_ht;
237 struct cds_lfht *session_triggers_ht;
238 struct cds_lfht *channel_state_ht;
239 struct cds_lfht *notification_trigger_clients_ht;
240 struct cds_lfht *channels_ht;
241 struct cds_lfht *sessions_ht;
242 struct cds_lfht *triggers_ht;
243 struct cds_lfht *triggers_by_name_ht;
244 struct cds_lfht *trigger_tokens_ht;
245 struct {
246 uint64_t token_generator;
247 uint64_t name_offset;
248 } trigger_id;
249 notification_client_id next_notification_client_id;
250 struct action_executor *executor;
251 };
252
253 /* notification_thread_data takes ownership of the channel monitor pipes. */
254 struct notification_thread_handle *notification_thread_handle_create(
255 struct lttng_pipe *ust32_channel_monitor_pipe,
256 struct lttng_pipe *ust64_channel_monitor_pipe,
257 struct lttng_pipe *kernel_channel_monitor_pipe,
258 int kernel_notification_monitor_fd);
259 void notification_thread_handle_destroy(
260 struct notification_thread_handle *handle);
261 struct lttng_thread *launch_notification_thread(
262 struct notification_thread_handle *handle);
263
264 #endif /* NOTIFICATION_THREAD_H */
This page took 0.035119 seconds and 5 git commands to generate.