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