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