2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
18 #ifndef NOTIFICATION_THREAD_H
19 #define NOTIFICATION_THREAD_H
21 #include <urcu/list.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>
29 #include <semaphore.h>
31 struct notification_thread_handle
{
33 * Queue of struct notification command.
34 * event_pipe must be WRITE(2) to signal that a new command
38 struct lttng_pipe
*event_pipe
;
39 struct cds_list_head list
;
43 * Read side of pipes used to receive channel status info collected
44 * by the various consumer daemons.
50 } channel_monitoring_pipes
;
52 * To inform the rotation thread we are ready.
54 sem_t
*notification_thread_ready
;
58 * This thread maintains an internal state associating clients and triggers.
60 * In order to speed-up and simplify queries, hash tables providing the
61 * following associations are maintained:
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.
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 this 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
74 * This hash table owns the list, but not the triggers themselves.
77 * associates a pair (channel key, channel domain) to its last
78 * sampled state received from the consumer daemon
79 * (struct channel_state).
80 * This previous sample is kept to implement edge-triggered
81 * conditions as we need to detect the state transitions.
82 * This hash table owns the channel state.
84 * - notification_trigger_clients_ht:
85 * associates notification-emitting triggers to clients
86 * (struct notification_client_list) subscribed to those
88 * The condition's hash and match functions are used directly since
89 * all triggers in this hash table have the "notify" action.
90 * This hash table holds no ownership.
93 * associates a channel_key to a struct channel_info. The hash table
94 * holds the ownership of the struct channel_info.
97 * associates a session_name (hash) to a struct session_info. The
98 * hash table holds no ownership of the struct session_info;
99 * the session_info structure is owned by the session's various
100 * channels through their struct channel_info (ref-counting is used).
103 * associates a condition to a struct lttng_trigger_ht_element.
104 * The hash table holds the ownership of the
105 * lttng_trigger_ht_elements along with the triggers themselves.
107 * The thread reacts to the following internal events:
108 * 1) creation of a tracing channel,
109 * 2) destruction of a tracing channel,
110 * 3) registration of a trigger,
111 * 4) unregistration of a trigger,
112 * 5) reception of a channel monitor sample from the consumer daemon.
114 * Events specific to notification-emitting triggers:
115 * 6) connection of a notification client,
116 * 7) disconnection of a notification client,
117 * 8) subscription of a client to a conditions' notifications,
118 * 9) unsubscription of a client from a conditions' notifications,
121 * 1) Creation of a tracing channel
122 * - notification_trigger_clients_ht is traversed to identify
123 * triggers which apply to this new channel,
124 * - triggers identified are added to the channel_triggers_ht.
125 * - add channel to channels_ht
127 * 2) Destruction of a tracing channel
128 * - remove entry from channel_triggers_ht, releasing the list wrapper and
130 * - remove entry from the channel_state_ht.
131 * - remove channel from channels_ht
133 * 3) Registration of a trigger
134 * - if the trigger's action is of type "notify",
135 * - traverse the list of conditions of every client to build a list of
136 * clients which have to be notified when this trigger's condition is met,
137 * - add list of clients (even if it is empty) to the
138 * notification_trigger_clients_ht,
139 * - add trigger to channel_triggers_ht (if applicable),
140 * - add trigger to triggers_ht
141 * - evaluate the trigger's condition right away to react if that condition
142 * is true from the beginning.
144 * 4) Unregistration of a trigger
145 * - if the trigger's action is of type "notify",
146 * - remove the trigger from the notification_trigger_clients_ht,
147 * - remove trigger from channel_triggers_ht (if applicable),
148 * - remove trigger from triggers_ht
150 * 5) Reception of a channel monitor sample from the consumer daemon
151 * - evaluate the conditions associated with the triggers found in
152 * the channel_triggers_ht,
153 * - if a condition evaluates to "true" and the condition is of type
154 * "notify", query the notification_trigger_clients_ht and send
155 * a notification to the clients.
157 * 6) Connection of a client
158 * - add client socket to the client_socket_ht.
160 * 7) Disconnection of a client
161 * - remove client socket from the client_socket_ht,
162 * - traverse all conditions to which the client is subscribed and remove
163 * the client from the notification_trigger_clients_ht.
165 * 8) Subscription of a client to a condition's notifications
166 * - Add the condition to the client's list of subscribed conditions,
167 * - Look-up notification_trigger_clients_ht and add the client to
169 * - Evaluate the condition for the client that subscribed if the trigger
170 * was already registered.
172 * 9) Unsubscription of a client to a condition's notifications
173 * - Remove the condition from the client's list of subscribed conditions,
174 * - Look-up notification_trigger_clients_ht and remove the client
175 * from the list of clients.
177 struct notification_thread_state
{
178 int notification_channel_socket
;
179 struct lttng_poll_event events
;
180 struct cds_lfht
*client_socket_ht
;
181 struct cds_lfht
*channel_triggers_ht
;
182 struct cds_lfht
*channel_state_ht
;
183 struct cds_lfht
*notification_trigger_clients_ht
;
184 struct cds_lfht
*channels_ht
;
185 struct cds_lfht
*sessions_ht
;
186 struct cds_lfht
*triggers_ht
;
189 /* notification_thread_data takes ownership of the channel monitor pipes. */
190 struct notification_thread_handle
*notification_thread_handle_create(
191 struct lttng_pipe
*ust32_channel_monitor_pipe
,
192 struct lttng_pipe
*ust64_channel_monitor_pipe
,
193 struct lttng_pipe
*kernel_channel_monitor_pipe
,
194 sem_t
*notification_thread_ready
);
195 void notification_thread_handle_destroy(
196 struct notification_thread_handle
*handle
);
198 void *thread_notification(void *data
);
200 #endif /* NOTIFICATION_THREAD_H */