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 | ||
5024c2ac JR |
11 | #include "action-executor.h" |
12 | #include "thread.h" | |
ab0ee2ca JG |
13 | #include <common/compat/poll.h> |
14 | #include <common/hashtable/hashtable.h> | |
5024c2ac JR |
15 | #include <common/pipe.h> |
16 | #include <lttng/trigger/trigger.h> | |
ab0ee2ca | 17 | #include <pthread.h> |
90936dcf | 18 | #include <semaphore.h> |
5024c2ac JR |
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 | }; | |
ab0ee2ca JG |
37 | |
38 | struct notification_thread_handle { | |
39 | /* | |
40 | * Queue of struct notification command. | |
814b4934 | 41 | * event_pipe must be WRITE(2) to signal that a new command |
ab0ee2ca JG |
42 | * has been enqueued. |
43 | */ | |
44 | struct { | |
814b4934 | 45 | struct lttng_pipe *event_pipe; |
ab0ee2ca JG |
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; | |
5024c2ac JR |
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; | |
c8a9de5a JG |
68 | /* Used to wait for the launch of the notification thread. */ |
69 | sem_t ready; | |
ab0ee2ca JG |
70 | }; |
71 | ||
74df9916 JG |
72 | /** |
73 | * This thread maintains an internal state associating clients and triggers. | |
74 | * | |
75 | * In order to speed-up and simplify queries, hash tables providing the | |
76 | * following associations are maintained: | |
77 | * | |
5024c2ac JR |
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. | |
82 | * | |
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". | |
74df9916 JG |
86 | * |
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 | |
ea9a44f0 | 90 | * those that have conditions that apply to a particular channel. |
74df9916 JG |
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 | |
93 | * moment. | |
94 | * This hash table owns the list, but not the triggers themselves. | |
95 | * | |
ea9a44f0 JG |
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 | |
104 | * destruction. | |
105 | * | |
74df9916 JG |
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. | |
113 | * | |
114 | * - notification_trigger_clients_ht: | |
115 | * associates notification-emitting triggers to clients | |
116 | * (struct notification_client_list) subscribed to those | |
117 | * conditions. | |
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. | |
121 | * | |
122 | * - channels_ht: | |
123 | * associates a channel_key to a struct channel_info. The hash table | |
124 | * holds the ownership of the struct channel_info. | |
125 | * | |
8abe313a JG |
126 | * - sessions_ht: |
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). | |
131 | * | |
74df9916 | 132 | * - triggers_ht: |
5024c2ac | 133 | * associates a trigger to a struct lttng_trigger_ht_element. |
74df9916 JG |
134 | * The hash table holds the ownership of the |
135 | * lttng_trigger_ht_elements along with the triggers themselves. | |
5024c2ac JR |
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. | |
74df9916 JG |
140 | * |
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. | |
51eab943 JG |
147 | * 6) Session rotation ongoing |
148 | * 7) Session rotation completed | |
74df9916 JG |
149 | * |
150 | * Events specific to notification-emitting triggers: | |
51eab943 JG |
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, | |
74df9916 JG |
155 | * |
156 | * | |
157 | * 1) Creation of a tracing channel | |
158 | * - notification_trigger_clients_ht is traversed to identify | |
159 | * triggers which apply to this new channel, | |
8abe313a | 160 | * - triggers identified are added to the channel_triggers_ht. |
74df9916 | 161 | * - add channel to channels_ht |
ea9a44f0 JG |
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. | |
74df9916 JG |
165 | * |
166 | * 2) Destruction of a tracing channel | |
167 | * - remove entry from channel_triggers_ht, releasing the list wrapper and | |
168 | * elements, | |
169 | * - remove entry from the channel_state_ht. | |
170 | * - remove channel from channels_ht | |
ea9a44f0 JG |
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. | |
74df9916 JG |
174 | * |
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), | |
ea9a44f0 | 182 | * - add trigger to session_triggers_ht (if applicable), |
5024c2ac | 183 | * - add trigger to triggers_by_name_ht |
74df9916 | 184 | * - add trigger to triggers_ht |
2ae99f0b JG |
185 | * - evaluate the trigger's condition right away to react if that condition |
186 | * is true from the beginning. | |
74df9916 JG |
187 | * |
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), | |
ea9a44f0 | 192 | * - remove trigger from session_triggers_ht (if applicable), |
5024c2ac | 193 | * - remove trigger to triggers_by_name_ht |
74df9916 JG |
194 | * - remove trigger from triggers_ht |
195 | * | |
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. | |
202 | * | |
51eab943 JG |
203 | * 6) Session rotation ongoing |
204 | * | |
205 | * 7) Session rotation completed | |
206 | * | |
207 | * 8) Connection of a client | |
5024c2ac JR |
208 | * - add client socket to the client_socket_ht, |
209 | * - add client socket to the client_id_ht. | |
74df9916 | 210 | * |
51eab943 | 211 | * 9) Disconnection of a client |
5024c2ac | 212 | * - remove client socket from the client_id_ht, |
74df9916 JG |
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. | |
216 | * | |
51eab943 | 217 | * 10) Subscription of a client to a condition's notifications |
74df9916 JG |
218 | * - Add the condition to the client's list of subscribed conditions, |
219 | * - Look-up notification_trigger_clients_ht and add the client to | |
220 | * list of clients. | |
2ae99f0b JG |
221 | * - Evaluate the condition for the client that subscribed if the trigger |
222 | * was already registered. | |
74df9916 | 223 | * |
51eab943 | 224 | * 11) Unsubscription of a client to a condition's notifications |
74df9916 JG |
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. | |
228 | */ | |
ab0ee2ca JG |
229 | struct notification_thread_state { |
230 | int notification_channel_socket; | |
231 | struct lttng_poll_event events; | |
232 | struct cds_lfht *client_socket_ht; | |
5024c2ac | 233 | struct cds_lfht *client_id_ht; |
ab0ee2ca | 234 | struct cds_lfht *channel_triggers_ht; |
ea9a44f0 | 235 | struct cds_lfht *session_triggers_ht; |
ab0ee2ca JG |
236 | struct cds_lfht *channel_state_ht; |
237 | struct cds_lfht *notification_trigger_clients_ht; | |
238 | struct cds_lfht *channels_ht; | |
8abe313a | 239 | struct cds_lfht *sessions_ht; |
ab0ee2ca | 240 | struct cds_lfht *triggers_ht; |
5024c2ac JR |
241 | struct cds_lfht *triggers_by_name_ht; |
242 | struct cds_lfht *trigger_tokens_ht; | |
243 | struct { | |
244 | uint64_t token_generator; | |
245 | uint64_t name_offset; | |
246 | } trigger_id; | |
247 | notification_client_id next_notification_client_id; | |
248 | struct action_executor *executor; | |
ab0ee2ca JG |
249 | }; |
250 | ||
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, | |
5024c2ac JR |
255 | struct lttng_pipe *kernel_channel_monitor_pipe, |
256 | int kernel_notification_monitor_fd); | |
ab0ee2ca JG |
257 | void notification_thread_handle_destroy( |
258 | struct notification_thread_handle *handle); | |
4a91420c JG |
259 | struct lttng_thread *launch_notification_thread( |
260 | struct notification_thread_handle *handle); | |
ab0ee2ca JG |
261 | |
262 | #endif /* NOTIFICATION_THREAD_H */ |