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> | |
ab0ee2ca | 17 | #include <pthread.h> |
90936dcf | 18 | #include <semaphore.h> |
f2b3ef9f JG |
19 | #include <urcu.h> |
20 | #include <urcu/list.h> | |
21 | #include <urcu/rculfhash.h> | |
ab0ee2ca | 22 | |
ac1889bf JG |
23 | |
24 | typedef uint64_t notification_client_id; | |
25 | ||
ab0ee2ca JG |
26 | struct notification_thread_handle { |
27 | /* | |
28 | * Queue of struct notification command. | |
814b4934 | 29 | * event_pipe must be WRITE(2) to signal that a new command |
ab0ee2ca JG |
30 | * has been enqueued. |
31 | */ | |
32 | struct { | |
814b4934 | 33 | struct lttng_pipe *event_pipe; |
ab0ee2ca JG |
34 | struct cds_list_head list; |
35 | pthread_mutex_t lock; | |
36 | } cmd_queue; | |
37 | /* | |
38 | * Read side of pipes used to receive channel status info collected | |
39 | * by the various consumer daemons. | |
40 | */ | |
41 | struct { | |
42 | int ust32_consumer; | |
43 | int ust64_consumer; | |
44 | int kernel_consumer; | |
45 | } channel_monitoring_pipes; | |
c8a9de5a JG |
46 | /* Used to wait for the launch of the notification thread. */ |
47 | sem_t ready; | |
ab0ee2ca JG |
48 | }; |
49 | ||
74df9916 JG |
50 | /** |
51 | * This thread maintains an internal state associating clients and triggers. | |
52 | * | |
53 | * In order to speed-up and simplify queries, hash tables providing the | |
54 | * following associations are maintained: | |
55 | * | |
ac1889bf JG |
56 | * - client_socket_ht: associate a client's socket (fd) to its |
57 | * "struct notification_client". | |
58 | * This hash table owns the "struct notification_client" which must | |
59 | * thus be disposed-of on removal from the hash table. | |
60 | * | |
61 | * - client_id_ht: associate a client's id to its "struct notification_client" | |
62 | * This hash table holds a _weak_ reference to the | |
63 | * "struct notification_client". | |
74df9916 JG |
64 | * |
65 | * - channel_triggers_ht: | |
66 | * associates a channel key to a list of | |
67 | * struct lttng_trigger_list_nodes. The triggers in this list are | |
ea9a44f0 | 68 | * those that have conditions that apply to a particular channel. |
74df9916 JG |
69 | * A channel entry is only created when a channel is added; the |
70 | * list of triggers applying to such a channel is built at that | |
71 | * moment. | |
72 | * This hash table owns the list, but not the triggers themselves. | |
73 | * | |
ea9a44f0 JG |
74 | * - session_triggers_ht: |
75 | * associates a session name to a list of | |
76 | * struct lttng_trigger_list_nodes. The triggers in this list are | |
77 | * those that have conditions that apply to a particular session. | |
78 | * A session entry is only created when a session is created; the | |
79 | * list of triggers applying to this new session is built at that | |
80 | * moment. This happens at the time of creation of a session_info. | |
81 | * Likewise, the list is destroyed at the time of the session_info's | |
82 | * destruction. | |
83 | * | |
74df9916 JG |
84 | * - channel_state_ht: |
85 | * associates a pair (channel key, channel domain) to its last | |
86 | * sampled state received from the consumer daemon | |
87 | * (struct channel_state). | |
88 | * This previous sample is kept to implement edge-triggered | |
89 | * conditions as we need to detect the state transitions. | |
90 | * This hash table owns the channel state. | |
91 | * | |
92 | * - notification_trigger_clients_ht: | |
93 | * associates notification-emitting triggers to clients | |
94 | * (struct notification_client_list) subscribed to those | |
95 | * conditions. | |
96 | * The condition's hash and match functions are used directly since | |
97 | * all triggers in this hash table have the "notify" action. | |
98 | * This hash table holds no ownership. | |
99 | * | |
100 | * - channels_ht: | |
101 | * associates a channel_key to a struct channel_info. The hash table | |
102 | * holds the ownership of the struct channel_info. | |
103 | * | |
8abe313a JG |
104 | * - sessions_ht: |
105 | * associates a session_name (hash) to a struct session_info. The | |
106 | * hash table holds no ownership of the struct session_info; | |
107 | * the session_info structure is owned by the session's various | |
108 | * channels through their struct channel_info (ref-counting is used). | |
109 | * | |
74df9916 | 110 | * - triggers_ht: |
242388e4 | 111 | * associates a trigger to a struct lttng_trigger_ht_element. |
74df9916 JG |
112 | * The hash table holds the ownership of the |
113 | * lttng_trigger_ht_elements along with the triggers themselves. | |
242388e4 JR |
114 | * - triggers_by_name_uid_ht: |
115 | * associates a trigger (name, uid) tuple to | |
116 | * a struct lttng_trigger_ht_element. | |
117 | * The hash table does not hold any ownership and is used strictly | |
118 | * for lookup on registration. | |
74df9916 JG |
119 | * |
120 | * The thread reacts to the following internal events: | |
121 | * 1) creation of a tracing channel, | |
122 | * 2) destruction of a tracing channel, | |
123 | * 3) registration of a trigger, | |
124 | * 4) unregistration of a trigger, | |
125 | * 5) reception of a channel monitor sample from the consumer daemon. | |
51eab943 JG |
126 | * 6) Session rotation ongoing |
127 | * 7) Session rotation completed | |
74df9916 JG |
128 | * |
129 | * Events specific to notification-emitting triggers: | |
51eab943 JG |
130 | * 8) connection of a notification client, |
131 | * 9) disconnection of a notification client, | |
132 | * 10) subscription of a client to a conditions' notifications, | |
133 | * 11) unsubscription of a client from a conditions' notifications, | |
74df9916 JG |
134 | * |
135 | * | |
136 | * 1) Creation of a tracing channel | |
137 | * - notification_trigger_clients_ht is traversed to identify | |
138 | * triggers which apply to this new channel, | |
8abe313a | 139 | * - triggers identified are added to the channel_triggers_ht. |
74df9916 | 140 | * - add channel to channels_ht |
ea9a44f0 JG |
141 | * - if it is the first channel of a session, a session_info is created and |
142 | * added to the sessions_ht. A list of the triggers associated with that | |
143 | * session is built, and it is added to session_triggers_ht. | |
74df9916 JG |
144 | * |
145 | * 2) Destruction of a tracing channel | |
146 | * - remove entry from channel_triggers_ht, releasing the list wrapper and | |
147 | * elements, | |
148 | * - remove entry from the channel_state_ht. | |
149 | * - remove channel from channels_ht | |
ea9a44f0 JG |
150 | * - if it was the last known channel of a session, the session_info |
151 | * structure is torndown, which in return destroys the list of triggers | |
152 | * applying to that session. | |
74df9916 JG |
153 | * |
154 | * 3) Registration of a trigger | |
155 | * - if the trigger's action is of type "notify", | |
156 | * - traverse the list of conditions of every client to build a list of | |
157 | * clients which have to be notified when this trigger's condition is met, | |
158 | * - add list of clients (even if it is empty) to the | |
159 | * notification_trigger_clients_ht, | |
160 | * - add trigger to channel_triggers_ht (if applicable), | |
ea9a44f0 | 161 | * - add trigger to session_triggers_ht (if applicable), |
242388e4 | 162 | * - add trigger to triggers_by_name_uid_ht |
74df9916 | 163 | * - add trigger to triggers_ht |
2ae99f0b JG |
164 | * - evaluate the trigger's condition right away to react if that condition |
165 | * is true from the beginning. | |
74df9916 JG |
166 | * |
167 | * 4) Unregistration of a trigger | |
168 | * - if the trigger's action is of type "notify", | |
169 | * - remove the trigger from the notification_trigger_clients_ht, | |
170 | * - remove trigger from channel_triggers_ht (if applicable), | |
ea9a44f0 | 171 | * - remove trigger from session_triggers_ht (if applicable), |
242388e4 | 172 | * - remove trigger from triggers_by_name_uid_ht |
74df9916 JG |
173 | * - remove trigger from triggers_ht |
174 | * | |
175 | * 5) Reception of a channel monitor sample from the consumer daemon | |
176 | * - evaluate the conditions associated with the triggers found in | |
177 | * the channel_triggers_ht, | |
178 | * - if a condition evaluates to "true" and the condition is of type | |
179 | * "notify", query the notification_trigger_clients_ht and send | |
180 | * a notification to the clients. | |
181 | * | |
51eab943 JG |
182 | * 6) Session rotation ongoing |
183 | * | |
184 | * 7) Session rotation completed | |
185 | * | |
186 | * 8) Connection of a client | |
ac1889bf JG |
187 | * - add client socket to the client_socket_ht, |
188 | * - add client socket to the client_id_ht. | |
74df9916 | 189 | * |
51eab943 | 190 | * 9) Disconnection of a client |
ac1889bf | 191 | * - remove client socket from the client_id_ht, |
74df9916 JG |
192 | * - remove client socket from the client_socket_ht, |
193 | * - traverse all conditions to which the client is subscribed and remove | |
194 | * the client from the notification_trigger_clients_ht. | |
195 | * | |
51eab943 | 196 | * 10) Subscription of a client to a condition's notifications |
74df9916 JG |
197 | * - Add the condition to the client's list of subscribed conditions, |
198 | * - Look-up notification_trigger_clients_ht and add the client to | |
199 | * list of clients. | |
2ae99f0b JG |
200 | * - Evaluate the condition for the client that subscribed if the trigger |
201 | * was already registered. | |
74df9916 | 202 | * |
51eab943 | 203 | * 11) Unsubscription of a client to a condition's notifications |
74df9916 JG |
204 | * - Remove the condition from the client's list of subscribed conditions, |
205 | * - Look-up notification_trigger_clients_ht and remove the client | |
206 | * from the list of clients. | |
207 | */ | |
ab0ee2ca JG |
208 | struct notification_thread_state { |
209 | int notification_channel_socket; | |
210 | struct lttng_poll_event events; | |
211 | struct cds_lfht *client_socket_ht; | |
ac1889bf | 212 | struct cds_lfht *client_id_ht; |
ab0ee2ca | 213 | struct cds_lfht *channel_triggers_ht; |
ea9a44f0 | 214 | struct cds_lfht *session_triggers_ht; |
ab0ee2ca JG |
215 | struct cds_lfht *channel_state_ht; |
216 | struct cds_lfht *notification_trigger_clients_ht; | |
217 | struct cds_lfht *channels_ht; | |
8abe313a | 218 | struct cds_lfht *sessions_ht; |
ab0ee2ca | 219 | struct cds_lfht *triggers_ht; |
242388e4 JR |
220 | struct cds_lfht *triggers_by_name_uid_ht; |
221 | struct { | |
e6887944 | 222 | uint64_t next_tracer_token; |
242388e4 JR |
223 | uint64_t name_offset; |
224 | } trigger_id; | |
ac1889bf | 225 | notification_client_id next_notification_client_id; |
f2b3ef9f | 226 | struct action_executor *executor; |
ab0ee2ca JG |
227 | }; |
228 | ||
229 | /* notification_thread_data takes ownership of the channel monitor pipes. */ | |
230 | struct notification_thread_handle *notification_thread_handle_create( | |
231 | struct lttng_pipe *ust32_channel_monitor_pipe, | |
232 | struct lttng_pipe *ust64_channel_monitor_pipe, | |
c8a9de5a | 233 | struct lttng_pipe *kernel_channel_monitor_pipe); |
ab0ee2ca JG |
234 | void notification_thread_handle_destroy( |
235 | struct notification_thread_handle *handle); | |
4a91420c JG |
236 | struct lttng_thread *launch_notification_thread( |
237 | struct notification_thread_handle *handle); | |
ab0ee2ca JG |
238 | |
239 | #endif /* NOTIFICATION_THREAD_H */ |