| 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 <lttng/domain.h> |
| 18 | #include <pthread.h> |
| 19 | #include <semaphore.h> |
| 20 | #include <urcu.h> |
| 21 | #include <urcu/list.h> |
| 22 | #include <urcu/rculfhash.h> |
| 23 | |
| 24 | typedef uint64_t notification_client_id; |
| 25 | |
| 26 | struct notification_event_trigger_source_element { |
| 27 | int fd; |
| 28 | enum lttng_domain_type domain; |
| 29 | struct cds_list_head node; |
| 30 | }; |
| 31 | |
| 32 | struct notification_trigger_tokens_ht_element { |
| 33 | uint64_t token; |
| 34 | struct lttng_trigger *trigger; |
| 35 | struct cds_lfht_node node; |
| 36 | /* call_rcu delayed reclaim. */ |
| 37 | struct rcu_head rcu_node; |
| 38 | }; |
| 39 | |
| 40 | struct notification_thread_handle { |
| 41 | /* |
| 42 | * Queue of struct notification command. |
| 43 | * event_pipe must be WRITE(2) to signal that a new command |
| 44 | * has been enqueued. |
| 45 | */ |
| 46 | struct { |
| 47 | struct lttng_pipe *event_pipe; |
| 48 | struct cds_list_head list; |
| 49 | pthread_mutex_t lock; |
| 50 | } cmd_queue; |
| 51 | /* |
| 52 | * Read side of pipes used to receive channel status info collected |
| 53 | * by the various consumer daemons. |
| 54 | */ |
| 55 | struct { |
| 56 | int ust32_consumer; |
| 57 | int ust64_consumer; |
| 58 | int kernel_consumer; |
| 59 | } channel_monitoring_pipes; |
| 60 | /* |
| 61 | * Read side of pipes used to reveice event trigger generetated by |
| 62 | * registered applications |
| 63 | */ |
| 64 | struct { |
| 65 | /* List of notification_event_trigger_source_element */ |
| 66 | struct cds_list_head list; |
| 67 | pthread_mutex_t lock; |
| 68 | } event_trigger_sources; |
| 69 | /* Used to wait for the launch of the notification thread. */ |
| 70 | sem_t ready; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * This thread maintains an internal state associating clients and triggers. |
| 75 | * |
| 76 | * In order to speed-up and simplify queries, hash tables providing the |
| 77 | * following associations are maintained: |
| 78 | * |
| 79 | * - client_socket_ht: associate a client's socket (fd) to its |
| 80 | * "struct notification_client". |
| 81 | * This hash table owns the "struct notification_client" which must |
| 82 | * thus be disposed-of on removal from the hash table. |
| 83 | * |
| 84 | * - client_id_ht: associate a client's id to its "struct notification_client" |
| 85 | * This hash table holds a _weak_ reference to the |
| 86 | * "struct notification_client". |
| 87 | * |
| 88 | * - channel_triggers_ht: |
| 89 | * associates a channel key to a list of |
| 90 | * struct lttng_trigger_list_nodes. The triggers in this list are |
| 91 | * those that have conditions that apply to a particular channel. |
| 92 | * A channel entry is only created when a channel is added; the |
| 93 | * list of triggers applying to such a channel is built at that |
| 94 | * moment. |
| 95 | * This hash table owns the list, but not the triggers themselves. |
| 96 | * |
| 97 | * - session_triggers_ht: |
| 98 | * associates a session name to a list of |
| 99 | * struct lttng_trigger_list_nodes. The triggers in this list are |
| 100 | * those that have conditions that apply to a particular session. |
| 101 | * A session entry is only created when a session is created; the |
| 102 | * list of triggers applying to this new session is built at that |
| 103 | * moment. This happens at the time of creation of a session_info. |
| 104 | * Likewise, the list is destroyed at the time of the session_info's |
| 105 | * destruction. |
| 106 | * |
| 107 | * - channel_state_ht: |
| 108 | * associates a pair (channel key, channel domain) to its last |
| 109 | * sampled state received from the consumer daemon |
| 110 | * (struct channel_state). |
| 111 | * This previous sample is kept to implement edge-triggered |
| 112 | * conditions as we need to detect the state transitions. |
| 113 | * This hash table owns the channel state. |
| 114 | * |
| 115 | * - notification_trigger_clients_ht: |
| 116 | * associates notification-emitting triggers to clients |
| 117 | * (struct notification_client_list) subscribed to those |
| 118 | * conditions. |
| 119 | * The condition's hash and match functions are used directly since |
| 120 | * all triggers in this hash table have the "notify" action. |
| 121 | * This hash table holds no ownership. |
| 122 | * |
| 123 | * - channels_ht: |
| 124 | * associates a channel_key to a struct channel_info. The hash table |
| 125 | * holds the ownership of the struct channel_info. |
| 126 | * |
| 127 | * - sessions_ht: |
| 128 | * associates a session_name (hash) to a struct session_info. The |
| 129 | * hash table holds no ownership of the struct session_info; |
| 130 | * the session_info structure is owned by the session's various |
| 131 | * channels through their struct channel_info (ref-counting is used). |
| 132 | * |
| 133 | * - triggers_ht: |
| 134 | * associates a trigger to a struct lttng_trigger_ht_element. |
| 135 | * The hash table holds the ownership of the |
| 136 | * lttng_trigger_ht_elements along with the triggers themselves. |
| 137 | * - triggers_by_name_uid_ht: |
| 138 | * associates a trigger (name, uid) tuple to |
| 139 | * 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_uid_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 from triggers_by_name_uid_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_uid_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 */ |