Fix: notification: deadlock on cmd_queue.lock and client->lock
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread.h
CommitLineData
ab0ee2ca 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
ab0ee2ca 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
ab0ee2ca 5 *
ab0ee2ca
JG
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>
90936dcf 19#include <semaphore.h>
4a91420c 20#include "thread.h"
ab0ee2ca 21
ac1889bf
JG
22
23typedef uint64_t notification_client_id;
24
ab0ee2ca
JG
25struct notification_thread_handle {
26 /*
27 * Queue of struct notification command.
814b4934 28 * event_pipe must be WRITE(2) to signal that a new command
ab0ee2ca
JG
29 * has been enqueued.
30 */
31 struct {
814b4934 32 struct lttng_pipe *event_pipe;
ab0ee2ca
JG
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;
c8a9de5a
JG
45 /* Used to wait for the launch of the notification thread. */
46 sem_t ready;
ab0ee2ca
JG
47};
48
74df9916
JG
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 *
ac1889bf
JG
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".
74df9916
JG
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
ea9a44f0 67 * those that have conditions that apply to a particular channel.
74df9916
JG
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 *
ea9a44f0
JG
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 *
74df9916
JG
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 *
8abe313a
JG
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 *
74df9916 109 * - triggers_ht:
50ca7858 110 * associates a condition to a struct lttng_trigger_ht_element.
74df9916
JG
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.
51eab943
JG
120 * 6) Session rotation ongoing
121 * 7) Session rotation completed
74df9916
JG
122 *
123 * Events specific to notification-emitting triggers:
51eab943
JG
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,
74df9916
JG
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,
8abe313a 133 * - triggers identified are added to the channel_triggers_ht.
74df9916 134 * - add channel to channels_ht
ea9a44f0
JG
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.
74df9916
JG
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
ea9a44f0
JG
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.
74df9916
JG
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),
ea9a44f0 155 * - add trigger to session_triggers_ht (if applicable),
74df9916 156 * - add trigger to triggers_ht
2ae99f0b
JG
157 * - evaluate the trigger's condition right away to react if that condition
158 * is true from the beginning.
74df9916
JG
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),
ea9a44f0 164 * - remove trigger from session_triggers_ht (if applicable),
74df9916
JG
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 *
51eab943
JG
174 * 6) Session rotation ongoing
175 *
176 * 7) Session rotation completed
177 *
178 * 8) Connection of a client
ac1889bf
JG
179 * - add client socket to the client_socket_ht,
180 * - add client socket to the client_id_ht.
74df9916 181 *
51eab943 182 * 9) Disconnection of a client
ac1889bf 183 * - remove client socket from the client_id_ht,
74df9916
JG
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 *
51eab943 188 * 10) Subscription of a client to a condition's notifications
74df9916
JG
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.
2ae99f0b
JG
192 * - Evaluate the condition for the client that subscribed if the trigger
193 * was already registered.
74df9916 194 *
51eab943 195 * 11) Unsubscription of a client to a condition's notifications
74df9916
JG
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 */
ab0ee2ca
JG
200struct notification_thread_state {
201 int notification_channel_socket;
202 struct lttng_poll_event events;
203 struct cds_lfht *client_socket_ht;
ac1889bf 204 struct cds_lfht *client_id_ht;
ab0ee2ca 205 struct cds_lfht *channel_triggers_ht;
ea9a44f0 206 struct cds_lfht *session_triggers_ht;
ab0ee2ca
JG
207 struct cds_lfht *channel_state_ht;
208 struct cds_lfht *notification_trigger_clients_ht;
209 struct cds_lfht *channels_ht;
8abe313a 210 struct cds_lfht *sessions_ht;
ab0ee2ca 211 struct cds_lfht *triggers_ht;
ac1889bf 212 notification_client_id next_notification_client_id;
ab0ee2ca
JG
213};
214
215/* notification_thread_data takes ownership of the channel monitor pipes. */
216struct notification_thread_handle *notification_thread_handle_create(
217 struct lttng_pipe *ust32_channel_monitor_pipe,
218 struct lttng_pipe *ust64_channel_monitor_pipe,
c8a9de5a 219 struct lttng_pipe *kernel_channel_monitor_pipe);
ab0ee2ca
JG
220void notification_thread_handle_destroy(
221 struct notification_thread_handle *handle);
4a91420c
JG
222struct lttng_thread *launch_notification_thread(
223 struct notification_thread_handle *handle);
ab0ee2ca
JG
224
225#endif /* NOTIFICATION_THREAD_H */
This page took 0.055167 seconds and 5 git commands to generate.