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 | ||
d3a684ee JR |
11 | #include "action-executor.h" |
12 | #include "thread.h" | |
ab0ee2ca JG |
13 | #include <common/compat/poll.h> |
14 | #include <common/hashtable/hashtable.h> | |
d3a684ee JR |
15 | #include <common/pipe.h> |
16 | #include <lttng/trigger/trigger.h> | |
ab0ee2ca | 17 | #include <pthread.h> |
90936dcf | 18 | #include <semaphore.h> |
d3a684ee 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; | |
d3a684ee 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; | |
d3a684ee JR |
70 | |
71 | pthread_mutex_t trigger_lock; | |
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 | * | |
d3a684ee JR |
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: |
d3a684ee | 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. | |
d3a684ee JR |
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. | |
74df9916 JG |
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. | |
51eab943 JG |
149 | * 6) Session rotation ongoing |
150 | * 7) Session rotation completed | |
74df9916 JG |
151 | * |
152 | * Events specific to notification-emitting triggers: | |
51eab943 JG |
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, | |
74df9916 JG |
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, | |
8abe313a | 162 | * - triggers identified are added to the channel_triggers_ht. |
74df9916 | 163 | * - add channel to channels_ht |
ea9a44f0 JG |
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. | |
74df9916 JG |
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 | |
ea9a44f0 JG |
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. | |
74df9916 JG |
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), | |
ea9a44f0 | 184 | * - add trigger to session_triggers_ht (if applicable), |
d3a684ee | 185 | * - add trigger to triggers_by_name_ht |
74df9916 | 186 | * - add trigger to triggers_ht |
2ae99f0b JG |
187 | * - evaluate the trigger's condition right away to react if that condition |
188 | * is true from the beginning. | |
74df9916 JG |
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), | |
ea9a44f0 | 194 | * - remove trigger from session_triggers_ht (if applicable), |
d3a684ee | 195 | * - remove trigger to triggers_by_name_ht |
74df9916 JG |
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 | * | |
51eab943 JG |
205 | * 6) Session rotation ongoing |
206 | * | |
207 | * 7) Session rotation completed | |
208 | * | |
209 | * 8) Connection of a client | |
d3a684ee JR |
210 | * - add client socket to the client_socket_ht, |
211 | * - add client socket to the client_id_ht. | |
74df9916 | 212 | * |
51eab943 | 213 | * 9) Disconnection of a client |
d3a684ee | 214 | * - remove client socket from the client_id_ht, |
74df9916 JG |
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 | * | |
51eab943 | 219 | * 10) Subscription of a client to a condition's notifications |
74df9916 JG |
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. | |
2ae99f0b JG |
223 | * - Evaluate the condition for the client that subscribed if the trigger |
224 | * was already registered. | |
74df9916 | 225 | * |
51eab943 | 226 | * 11) Unsubscription of a client to a condition's notifications |
74df9916 JG |
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 | */ | |
ab0ee2ca JG |
231 | struct notification_thread_state { |
232 | int notification_channel_socket; | |
233 | struct lttng_poll_event events; | |
234 | struct cds_lfht *client_socket_ht; | |
d3a684ee | 235 | struct cds_lfht *client_id_ht; |
ab0ee2ca | 236 | struct cds_lfht *channel_triggers_ht; |
ea9a44f0 | 237 | struct cds_lfht *session_triggers_ht; |
ab0ee2ca JG |
238 | struct cds_lfht *channel_state_ht; |
239 | struct cds_lfht *notification_trigger_clients_ht; | |
240 | struct cds_lfht *channels_ht; | |
8abe313a | 241 | struct cds_lfht *sessions_ht; |
ab0ee2ca | 242 | struct cds_lfht *triggers_ht; |
d3a684ee JR |
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; | |
ab0ee2ca JG |
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, | |
d3a684ee JR |
257 | struct lttng_pipe *kernel_channel_monitor_pipe, |
258 | int kernel_notification_monitor_fd); | |
ab0ee2ca JG |
259 | void notification_thread_handle_destroy( |
260 | struct notification_thread_handle *handle); | |
4a91420c JG |
261 | struct lttng_thread *launch_notification_thread( |
262 | struct notification_thread_handle *handle); | |
ab0ee2ca JG |
263 | |
264 | #endif /* NOTIFICATION_THREAD_H */ |