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>
30 struct notification_thread_handle
{
32 * Queue of struct notification command.
33 * event_pipe must be WRITE(2) to signal that a new command
37 struct lttng_pipe
*event_pipe
;
38 struct cds_list_head list
;
42 * Read side of pipes used to receive channel status info collected
43 * by the various consumer daemons.
49 } channel_monitoring_pipes
;
53 * This thread maintains an internal state associating clients and triggers.
55 * In order to speed-up and simplify queries, hash tables providing the
56 * following associations are maintained:
58 * - client_socket_ht: associate a client's socket (fd) to its "struct client"
59 * This hash table owns the "struct client" which must thus be
60 * disposed-of on removal from the hash table.
62 * - channel_triggers_ht:
63 * associates a channel key to a list of
64 * struct lttng_trigger_list_nodes. The triggers in this list are
65 * those that have conditions that apply to this channel.
66 * A channel entry is only created when a channel is added; the
67 * list of triggers applying to such a channel is built at that
69 * This hash table owns the list, but not the triggers themselves.
72 * associates a pair (channel key, channel domain) to its last
73 * sampled state received from the consumer daemon
74 * (struct channel_state).
75 * This previous sample is kept to implement edge-triggered
76 * conditions as we need to detect the state transitions.
77 * This hash table owns the channel state.
79 * - notification_trigger_clients_ht:
80 * associates notification-emitting triggers to clients
81 * (struct notification_client_list) subscribed to those
83 * The condition's hash and match functions are used directly since
84 * all triggers in this hash table have the "notify" action.
85 * This hash table holds no ownership.
88 * associates a channel_key to a struct channel_info. The hash table
89 * holds the ownership of the struct channel_info.
92 * associated a condition to a struct lttng_trigger_ht_element.
93 * The hash table holds the ownership of the
94 * lttng_trigger_ht_elements along with the triggers themselves.
96 * The thread reacts to the following internal events:
97 * 1) creation of a tracing channel,
98 * 2) destruction of a tracing channel,
99 * 3) registration of a trigger,
100 * 4) unregistration of a trigger,
101 * 5) reception of a channel monitor sample from the consumer daemon.
103 * Events specific to notification-emitting triggers:
104 * 6) connection of a notification client,
105 * 7) disconnection of a notification client,
106 * 8) subscription of a client to a conditions' notifications,
107 * 9) unsubscription of a client from a conditions' notifications,
110 * 1) Creation of a tracing channel
111 * - notification_trigger_clients_ht is traversed to identify
112 * triggers which apply to this new channel,
113 * - triggers identified are added to the channel_triggers_ht.
114 * - add channel to channels_ht
116 * 2) Destruction of a tracing channel
117 * - remove entry from channel_triggers_ht, releasing the list wrapper and
119 * - remove entry from the channel_state_ht.
120 * - remove channel from channels_ht
122 * 3) Registration of a trigger
123 * - if the trigger's action is of type "notify",
124 * - traverse the list of conditions of every client to build a list of
125 * clients which have to be notified when this trigger's condition is met,
126 * - add list of clients (even if it is empty) to the
127 * notification_trigger_clients_ht,
128 * - add trigger to channel_triggers_ht (if applicable),
129 * - add trigger to triggers_ht
131 * 4) Unregistration of a trigger
132 * - if the trigger's action is of type "notify",
133 * - remove the trigger from the notification_trigger_clients_ht,
134 * - remove trigger from channel_triggers_ht (if applicable),
135 * - remove trigger from triggers_ht
137 * 5) Reception of a channel monitor sample from the consumer daemon
138 * - evaluate the conditions associated with the triggers found in
139 * the channel_triggers_ht,
140 * - if a condition evaluates to "true" and the condition is of type
141 * "notify", query the notification_trigger_clients_ht and send
142 * a notification to the clients.
144 * 6) Connection of a client
145 * - add client socket to the client_socket_ht.
147 * 7) Disconnection of a client
148 * - remove client socket from the client_socket_ht,
149 * - traverse all conditions to which the client is subscribed and remove
150 * the client from the notification_trigger_clients_ht.
152 * 8) Subscription of a client to a condition's notifications
153 * - Add the condition to the client's list of subscribed conditions,
154 * - Look-up notification_trigger_clients_ht and add the client to
157 * 9) Unsubscription of a client to a condition's notifications
158 * - Remove the condition from the client's list of subscribed conditions,
159 * - Look-up notification_trigger_clients_ht and remove the client
160 * from the list of clients.
162 struct notification_thread_state
{
163 int notification_channel_socket
;
164 struct lttng_poll_event events
;
165 struct cds_lfht
*client_socket_ht
;
166 struct cds_lfht
*channel_triggers_ht
;
167 struct cds_lfht
*channel_state_ht
;
168 struct cds_lfht
*notification_trigger_clients_ht
;
169 struct cds_lfht
*channels_ht
;
170 struct cds_lfht
*triggers_ht
;
173 /* notification_thread_data takes ownership of the channel monitor pipes. */
174 struct notification_thread_handle
*notification_thread_handle_create(
175 struct lttng_pipe
*ust32_channel_monitor_pipe
,
176 struct lttng_pipe
*ust64_channel_monitor_pipe
,
177 struct lttng_pipe
*kernel_channel_monitor_pipe
);
178 void notification_thread_handle_destroy(
179 struct notification_thread_handle
*handle
);
181 void *thread_notification(void *data
);
183 #endif /* NOTIFICATION_THREAD_H */