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