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