| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #ifndef NOTIFICATION_THREAD_H |
| 19 | #define NOTIFICATION_THREAD_H |
| 20 | |
| 21 | #include <urcu/list.h> |
| 22 | #include <urcu.h> |
| 23 | #include <urcu/rculfhash.h> |
| 24 | #include <lttng/trigger/trigger.h> |
| 25 | #include <common/pipe.h> |
| 26 | #include <common/compat/poll.h> |
| 27 | #include <common/hashtable/hashtable.h> |
| 28 | #include <pthread.h> |
| 29 | #include <semaphore.h> |
| 30 | #include "thread.h" |
| 31 | |
| 32 | struct notification_thread_handle { |
| 33 | /* |
| 34 | * Queue of struct notification command. |
| 35 | * event_pipe must be WRITE(2) to signal that a new command |
| 36 | * has been enqueued. |
| 37 | */ |
| 38 | struct { |
| 39 | struct lttng_pipe *event_pipe; |
| 40 | struct cds_list_head list; |
| 41 | pthread_mutex_t lock; |
| 42 | } cmd_queue; |
| 43 | /* |
| 44 | * Read side of pipes used to receive channel status info collected |
| 45 | * by the various consumer daemons. |
| 46 | */ |
| 47 | struct { |
| 48 | int ust32_consumer; |
| 49 | int ust64_consumer; |
| 50 | int kernel_consumer; |
| 51 | } channel_monitoring_pipes; |
| 52 | /* Used to wait for the launch of the notification thread. */ |
| 53 | sem_t ready; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * This thread maintains an internal state associating clients and triggers. |
| 58 | * |
| 59 | * In order to speed-up and simplify queries, hash tables providing the |
| 60 | * following associations are maintained: |
| 61 | * |
| 62 | * - client_socket_ht: associate a client's socket (fd) to its "struct client" |
| 63 | * This hash table owns the "struct client" which must thus be |
| 64 | * disposed-of on removal from the hash table. |
| 65 | * |
| 66 | * - channel_triggers_ht: |
| 67 | * associates a channel key to a list of |
| 68 | * struct lttng_trigger_list_nodes. The triggers in this list are |
| 69 | * those that have conditions that apply to a particular channel. |
| 70 | * A channel entry is only created when a channel is added; the |
| 71 | * list of triggers applying to such a channel is built at that |
| 72 | * moment. |
| 73 | * This hash table owns the list, but not the triggers themselves. |
| 74 | * |
| 75 | * - session_triggers_ht: |
| 76 | * associates a session name to a list of |
| 77 | * struct lttng_trigger_list_nodes. The triggers in this list are |
| 78 | * those that have conditions that apply to a particular session. |
| 79 | * A session entry is only created when a session is created; the |
| 80 | * list of triggers applying to this new session is built at that |
| 81 | * moment. This happens at the time of creation of a session_info. |
| 82 | * Likewise, the list is destroyed at the time of the session_info's |
| 83 | * destruction. |
| 84 | * |
| 85 | * - channel_state_ht: |
| 86 | * associates a pair (channel key, channel domain) to its last |
| 87 | * sampled state received from the consumer daemon |
| 88 | * (struct channel_state). |
| 89 | * This previous sample is kept to implement edge-triggered |
| 90 | * conditions as we need to detect the state transitions. |
| 91 | * This hash table owns the channel state. |
| 92 | * |
| 93 | * - notification_trigger_clients_ht: |
| 94 | * associates notification-emitting triggers to clients |
| 95 | * (struct notification_client_list) subscribed to those |
| 96 | * conditions. |
| 97 | * The condition's hash and match functions are used directly since |
| 98 | * all triggers in this hash table have the "notify" action. |
| 99 | * This hash table holds no ownership. |
| 100 | * |
| 101 | * - channels_ht: |
| 102 | * associates a channel_key to a struct channel_info. The hash table |
| 103 | * holds the ownership of the struct channel_info. |
| 104 | * |
| 105 | * - sessions_ht: |
| 106 | * associates a session_name (hash) to a struct session_info. The |
| 107 | * hash table holds no ownership of the struct session_info; |
| 108 | * the session_info structure is owned by the session's various |
| 109 | * channels through their struct channel_info (ref-counting is used). |
| 110 | * |
| 111 | * - triggers_ht: |
| 112 | * associates a condition to a struct lttng_trigger_ht_element. |
| 113 | * The hash table holds the ownership of the |
| 114 | * lttng_trigger_ht_elements along with the triggers themselves. |
| 115 | * |
| 116 | * The thread reacts to the following internal events: |
| 117 | * 1) creation of a tracing channel, |
| 118 | * 2) destruction of a tracing channel, |
| 119 | * 3) registration of a trigger, |
| 120 | * 4) unregistration of a trigger, |
| 121 | * 5) reception of a channel monitor sample from the consumer daemon. |
| 122 | * 6) Session rotation ongoing |
| 123 | * 7) Session rotation completed |
| 124 | * |
| 125 | * Events specific to notification-emitting triggers: |
| 126 | * 8) connection of a notification client, |
| 127 | * 9) disconnection of a notification client, |
| 128 | * 10) subscription of a client to a conditions' notifications, |
| 129 | * 11) unsubscription of a client from a conditions' notifications, |
| 130 | * |
| 131 | * |
| 132 | * 1) Creation of a tracing channel |
| 133 | * - notification_trigger_clients_ht is traversed to identify |
| 134 | * triggers which apply to this new channel, |
| 135 | * - triggers identified are added to the channel_triggers_ht. |
| 136 | * - add channel to channels_ht |
| 137 | * - if it is the first channel of a session, a session_info is created and |
| 138 | * added to the sessions_ht. A list of the triggers associated with that |
| 139 | * session is built, and it is added to session_triggers_ht. |
| 140 | * |
| 141 | * 2) Destruction of a tracing channel |
| 142 | * - remove entry from channel_triggers_ht, releasing the list wrapper and |
| 143 | * elements, |
| 144 | * - remove entry from the channel_state_ht. |
| 145 | * - remove channel from channels_ht |
| 146 | * - if it was the last known channel of a session, the session_info |
| 147 | * structure is torndown, which in return destroys the list of triggers |
| 148 | * applying to that session. |
| 149 | * |
| 150 | * 3) Registration of a trigger |
| 151 | * - if the trigger's action is of type "notify", |
| 152 | * - traverse the list of conditions of every client to build a list of |
| 153 | * clients which have to be notified when this trigger's condition is met, |
| 154 | * - add list of clients (even if it is empty) to the |
| 155 | * notification_trigger_clients_ht, |
| 156 | * - add trigger to channel_triggers_ht (if applicable), |
| 157 | * - add trigger to session_triggers_ht (if applicable), |
| 158 | * - add trigger to triggers_ht |
| 159 | * - evaluate the trigger's condition right away to react if that condition |
| 160 | * is true from the beginning. |
| 161 | * |
| 162 | * 4) Unregistration of a trigger |
| 163 | * - if the trigger's action is of type "notify", |
| 164 | * - remove the trigger from the notification_trigger_clients_ht, |
| 165 | * - remove trigger from channel_triggers_ht (if applicable), |
| 166 | * - remove trigger from session_triggers_ht (if applicable), |
| 167 | * - remove trigger from triggers_ht |
| 168 | * |
| 169 | * 5) Reception of a channel monitor sample from the consumer daemon |
| 170 | * - evaluate the conditions associated with the triggers found in |
| 171 | * the channel_triggers_ht, |
| 172 | * - if a condition evaluates to "true" and the condition is of type |
| 173 | * "notify", query the notification_trigger_clients_ht and send |
| 174 | * a notification to the clients. |
| 175 | * |
| 176 | * 6) Session rotation ongoing |
| 177 | * |
| 178 | * 7) Session rotation completed |
| 179 | * |
| 180 | * 8) Connection of a client |
| 181 | * - add client socket to the client_socket_ht. |
| 182 | * |
| 183 | * 9) Disconnection of a client |
| 184 | * - remove client socket from the client_socket_ht, |
| 185 | * - traverse all conditions to which the client is subscribed and remove |
| 186 | * the client from the notification_trigger_clients_ht. |
| 187 | * |
| 188 | * 10) Subscription of a client to a condition's notifications |
| 189 | * - Add the condition to the client's list of subscribed conditions, |
| 190 | * - Look-up notification_trigger_clients_ht and add the client to |
| 191 | * list of clients. |
| 192 | * - Evaluate the condition for the client that subscribed if the trigger |
| 193 | * was already registered. |
| 194 | * |
| 195 | * 11) Unsubscription of a client to a condition's notifications |
| 196 | * - Remove the condition from the client's list of subscribed conditions, |
| 197 | * - Look-up notification_trigger_clients_ht and remove the client |
| 198 | * from the list of clients. |
| 199 | */ |
| 200 | struct notification_thread_state { |
| 201 | int notification_channel_socket; |
| 202 | struct lttng_poll_event events; |
| 203 | struct cds_lfht *client_socket_ht; |
| 204 | struct cds_lfht *channel_triggers_ht; |
| 205 | struct cds_lfht *session_triggers_ht; |
| 206 | struct cds_lfht *channel_state_ht; |
| 207 | struct cds_lfht *notification_trigger_clients_ht; |
| 208 | struct cds_lfht *channels_ht; |
| 209 | struct cds_lfht *sessions_ht; |
| 210 | struct cds_lfht *triggers_ht; |
| 211 | }; |
| 212 | |
| 213 | /* notification_thread_data takes ownership of the channel monitor pipes. */ |
| 214 | struct notification_thread_handle *notification_thread_handle_create( |
| 215 | struct lttng_pipe *ust32_channel_monitor_pipe, |
| 216 | struct lttng_pipe *ust64_channel_monitor_pipe, |
| 217 | struct lttng_pipe *kernel_channel_monitor_pipe); |
| 218 | void notification_thread_handle_destroy( |
| 219 | struct notification_thread_handle *handle); |
| 220 | struct lttng_thread *launch_notification_thread( |
| 221 | struct notification_thread_handle *handle); |
| 222 | |
| 223 | #endif /* NOTIFICATION_THREAD_H */ |