Commit | Line | Data |
---|---|---|
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 | ||
f2b3ef9f JG |
11 | #include "action-executor.h" |
12 | #include "thread.h" | |
ab0ee2ca JG |
13 | #include <common/compat/poll.h> |
14 | #include <common/hashtable/hashtable.h> | |
f2b3ef9f JG |
15 | #include <common/pipe.h> |
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 | |
ac1889bf JG |
24 | typedef uint64_t notification_client_id; |
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 | */ | |
32 | struct 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 | ||
ab0ee2ca JG |
50 | struct notification_thread_handle { |
51 | /* | |
52 | * Queue of struct notification command. | |
814b4934 | 53 | * event_pipe must be WRITE(2) to signal that a new command |
ab0ee2ca JG |
54 | * has been enqueued. |
55 | */ | |
56 | struct { | |
814b4934 | 57 | struct lttng_pipe *event_pipe; |
ab0ee2ca JG |
58 | struct cds_list_head list; |
59 | pthread_mutex_t lock; | |
60 | } cmd_queue; | |
61 | /* | |
62 | * Read side of pipes used to receive channel status info collected | |
63 | * by the various consumer daemons. | |
64 | */ | |
65 | struct { | |
66 | int ust32_consumer; | |
67 | int ust64_consumer; | |
68 | int kernel_consumer; | |
69 | } channel_monitoring_pipes; | |
c8a9de5a JG |
70 | /* Used to wait for the launch of the notification thread. */ |
71 | sem_t ready; | |
ab0ee2ca JG |
72 | }; |
73 | ||
74df9916 JG |
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 | * | |
ac1889bf JG |
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". | |
74df9916 JG |
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 | |
ea9a44f0 | 92 | * those that have conditions that apply to a particular channel. |
74df9916 JG |
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 | * | |
ea9a44f0 JG |
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 | * | |
74df9916 JG |
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 | * | |
8abe313a JG |
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 | * | |
74df9916 | 134 | * - triggers_ht: |
242388e4 | 135 | * associates a trigger to a struct lttng_trigger_ht_element. |
74df9916 JG |
136 | * The hash table holds the ownership of the |
137 | * lttng_trigger_ht_elements along with the triggers themselves. | |
242388e4 JR |
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. | |
d02d7404 JR |
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. | |
146 | * | |
74df9916 JG |
147 | * |
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, | |
d02d7404 JR |
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, | |
74df9916 JG |
158 | * |
159 | * Events specific to notification-emitting triggers: | |
d02d7404 JR |
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, | |
74df9916 JG |
164 | * |
165 | * | |
166 | * 1) Creation of a tracing channel | |
167 | * - notification_trigger_clients_ht is traversed to identify | |
168 | * triggers which apply to this new channel, | |
8abe313a | 169 | * - triggers identified are added to the channel_triggers_ht. |
74df9916 | 170 | * - add channel to channels_ht |
ea9a44f0 JG |
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. | |
74df9916 JG |
174 | * |
175 | * 2) Destruction of a tracing channel | |
176 | * - remove entry from channel_triggers_ht, releasing the list wrapper and | |
177 | * elements, | |
178 | * - remove entry from the channel_state_ht. | |
179 | * - remove channel from channels_ht | |
ea9a44f0 JG |
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. | |
74df9916 JG |
183 | * |
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), | |
ea9a44f0 | 191 | * - add trigger to session_triggers_ht (if applicable), |
242388e4 | 192 | * - add trigger to triggers_by_name_uid_ht |
74df9916 | 193 | * - add trigger to triggers_ht |
2ae99f0b JG |
194 | * - evaluate the trigger's condition right away to react if that condition |
195 | * is true from the beginning. | |
74df9916 JG |
196 | * |
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), | |
ea9a44f0 | 201 | * - remove trigger from session_triggers_ht (if applicable), |
242388e4 | 202 | * - remove trigger from triggers_by_name_uid_ht |
74df9916 JG |
203 | * - remove trigger from triggers_ht |
204 | * | |
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. | |
211 | * | |
51eab943 JG |
212 | * 6) Session rotation ongoing |
213 | * | |
214 | * 7) Session rotation completed | |
215 | * | |
d02d7404 JR |
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. | |
220 | * | |
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. | |
225 | * | |
226 | * 10) Connection of a client | |
ac1889bf JG |
227 | * - add client socket to the client_socket_ht, |
228 | * - add client socket to the client_id_ht. | |
74df9916 | 229 | * |
d02d7404 | 230 | * 11) Disconnection of a client |
ac1889bf | 231 | * - remove client socket from the client_id_ht, |
74df9916 JG |
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. | |
235 | * | |
d02d7404 | 236 | * 12) Subscription of a client to a condition's notifications |
74df9916 JG |
237 | * - Add the condition to the client's list of subscribed conditions, |
238 | * - Look-up notification_trigger_clients_ht and add the client to | |
239 | * list of clients. | |
2ae99f0b JG |
240 | * - Evaluate the condition for the client that subscribed if the trigger |
241 | * was already registered. | |
74df9916 | 242 | * |
d02d7404 | 243 | * 13) Unsubscription of a client to a condition's notifications |
74df9916 JG |
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. | |
247 | */ | |
ab0ee2ca JG |
248 | struct notification_thread_state { |
249 | int notification_channel_socket; | |
250 | struct lttng_poll_event events; | |
251 | struct cds_lfht *client_socket_ht; | |
ac1889bf | 252 | struct cds_lfht *client_id_ht; |
ab0ee2ca | 253 | struct cds_lfht *channel_triggers_ht; |
ea9a44f0 | 254 | struct cds_lfht *session_triggers_ht; |
ab0ee2ca JG |
255 | struct cds_lfht *channel_state_ht; |
256 | struct cds_lfht *notification_trigger_clients_ht; | |
257 | struct cds_lfht *channels_ht; | |
8abe313a | 258 | struct cds_lfht *sessions_ht; |
ab0ee2ca | 259 | struct cds_lfht *triggers_ht; |
242388e4 JR |
260 | struct cds_lfht *triggers_by_name_uid_ht; |
261 | struct { | |
e6887944 | 262 | uint64_t next_tracer_token; |
242388e4 JR |
263 | uint64_t name_offset; |
264 | } trigger_id; | |
d02d7404 JR |
265 | /* |
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 | |
269 | * tracer. | |
270 | * | |
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. | |
274 | */ | |
275 | struct cds_list_head tracer_event_sources_list; | |
ac1889bf | 276 | notification_client_id next_notification_client_id; |
f2b3ef9f | 277 | struct action_executor *executor; |
ab0ee2ca JG |
278 | }; |
279 | ||
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, | |
c8a9de5a | 284 | struct lttng_pipe *kernel_channel_monitor_pipe); |
ab0ee2ca JG |
285 | void notification_thread_handle_destroy( |
286 | struct notification_thread_handle *handle); | |
4a91420c JG |
287 | struct lttng_thread *launch_notification_thread( |
288 | struct notification_thread_handle *handle); | |
ab0ee2ca JG |
289 | |
290 | #endif /* NOTIFICATION_THREAD_H */ |