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.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/condition/session-consumed-size-internal.h>
36 #include <lttng/condition/session-rotation-internal.h>
37 #include <lttng/notification/channel-internal.h>
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 enum lttng_object_type
{
55 LTTNG_OBJECT_TYPE_UNKNOWN
,
56 LTTNG_OBJECT_TYPE_NONE
,
57 LTTNG_OBJECT_TYPE_CHANNEL
,
58 LTTNG_OBJECT_TYPE_SESSION
,
61 struct lttng_trigger_list_element
{
62 /* No ownership of the trigger object is assumed. */
63 const struct lttng_trigger
*trigger
;
64 struct cds_list_head node
;
67 struct lttng_channel_trigger_list
{
68 struct channel_key channel_key
;
69 /* List of struct lttng_trigger_list_element. */
70 struct cds_list_head list
;
71 /* Node in the channel_triggers_ht */
72 struct cds_lfht_node channel_triggers_ht_node
;
73 /* call_rcu delayed reclaim. */
74 struct rcu_head rcu_node
;
78 * List of triggers applying to a given session.
81 * - lttng_session_trigger_list_create()
82 * - lttng_session_trigger_list_build()
83 * - lttng_session_trigger_list_destroy()
84 * - lttng_session_trigger_list_add()
86 struct lttng_session_trigger_list
{
88 * Not owned by this; points to the session_info structure's
91 const char *session_name
;
92 /* List of struct lttng_trigger_list_element. */
93 struct cds_list_head list
;
94 /* Node in the session_triggers_ht */
95 struct cds_lfht_node session_triggers_ht_node
;
97 * Weak reference to the notification system's session triggers
100 * The session trigger list structure structure is owned by
101 * the session's session_info.
103 * The session_info is kept alive the the channel_infos holding a
104 * reference to it (reference counting). When those channels are
105 * destroyed (at runtime or on teardown), the reference they hold
106 * to the session_info are released. On destruction of session_info,
107 * session_info_destroy() will remove the list of triggers applying
108 * to this session from the notification system's state.
110 * This implies that the session_triggers_ht must be destroyed
111 * after the channels.
113 struct cds_lfht
*session_triggers_ht
;
114 /* Used for delayed RCU reclaim. */
115 struct rcu_head rcu_node
;
118 struct lttng_trigger_ht_element
{
119 struct lttng_trigger
*trigger
;
120 struct cds_lfht_node node
;
121 /* call_rcu delayed reclaim. */
122 struct rcu_head rcu_node
;
125 struct lttng_condition_list_element
{
126 struct lttng_condition
*condition
;
127 struct cds_list_head node
;
130 struct notification_client_list_element
{
131 struct notification_client
*client
;
132 struct cds_list_head node
;
135 struct notification_client_list
{
136 const struct lttng_trigger
*trigger
;
137 struct cds_list_head list
;
138 struct cds_lfht_node notification_trigger_ht_node
;
139 /* call_rcu delayed reclaim. */
140 struct rcu_head rcu_node
;
143 struct notification_client
{
145 /* Client protocol version. */
146 uint8_t major
, minor
;
150 * Indicates if the credentials and versions of the client have been
155 * Conditions to which the client's notification channel is subscribed.
156 * List of struct lttng_condition_list_node. The condition member is
157 * owned by the client.
159 struct cds_list_head condition_list
;
160 struct cds_lfht_node client_socket_ht_node
;
164 * During the reception of a message, the reception
165 * buffers' "size" is set to contain the current
166 * message's complete payload.
168 struct lttng_dynamic_buffer buffer
;
169 /* Bytes left to receive for the current message. */
170 size_t bytes_to_receive
;
171 /* Type of the message being received. */
172 enum lttng_notification_channel_message_type msg_type
;
174 * Indicates whether or not credentials are expected
179 * Indicates whether or not credentials were received
183 /* Only used during credentials reception. */
184 lttng_sock_cred creds
;
188 * Indicates whether or not a notification addressed to
189 * this client was dropped because a command reply was
192 * A notification is dropped whenever the buffer is not
195 bool dropped_notification
;
197 * Indicates whether or not a command reply is already
198 * buffered. In this case, it means that the client is
199 * not consuming command replies before emitting a new
200 * one. This could be caused by a protocol error or a
201 * misbehaving/malicious client.
203 bool queued_command_reply
;
204 struct lttng_dynamic_buffer buffer
;
207 /* call_rcu delayed reclaim. */
208 struct rcu_head rcu_node
;
211 struct channel_state_sample
{
212 struct channel_key key
;
213 struct cds_lfht_node channel_state_ht_node
;
214 uint64_t highest_usage
;
215 uint64_t lowest_usage
;
216 uint64_t channel_total_consumed
;
217 /* call_rcu delayed reclaim. */
218 struct rcu_head rcu_node
;
221 static unsigned long hash_channel_key(struct channel_key
*key
);
222 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
223 struct lttng_evaluation
**evaluation
,
224 const struct notification_thread_state
*state
,
225 const struct channel_state_sample
*previous_sample
,
226 const struct channel_state_sample
*latest_sample
,
227 uint64_t previous_session_consumed_total
,
228 uint64_t latest_session_consumed_total
,
229 struct channel_info
*channel_info
);
231 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
232 const struct lttng_evaluation
*evaluation
,
233 struct notification_client_list
*client_list
,
234 struct notification_thread_state
*state
,
235 uid_t channel_uid
, gid_t channel_gid
);
238 /* session_info API */
240 void session_info_destroy(void *_data
);
242 void session_info_get(struct session_info
*session_info
);
244 void session_info_put(struct session_info
*session_info
);
246 struct session_info
*session_info_create(const char *name
,
247 uid_t uid
, gid_t gid
,
248 struct lttng_session_trigger_list
*trigger_list
,
249 struct cds_lfht
*sessions_ht
);
251 void session_info_add_channel(struct session_info
*session_info
,
252 struct channel_info
*channel_info
);
254 void session_info_remove_channel(struct session_info
*session_info
,
255 struct channel_info
*channel_info
);
257 /* lttng_session_trigger_list API */
259 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
260 const char *session_name
,
261 struct cds_lfht
*session_triggers_ht
);
263 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
264 const struct notification_thread_state
*state
,
265 const char *session_name
);
267 void lttng_session_trigger_list_destroy(
268 struct lttng_session_trigger_list
*list
);
270 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
271 const struct lttng_trigger
*trigger
);
275 int match_client(struct cds_lfht_node
*node
, const void *key
)
277 /* This double-cast is intended to supress pointer-to-cast warning. */
278 int socket
= (int) (intptr_t) key
;
279 struct notification_client
*client
;
281 client
= caa_container_of(node
, struct notification_client
,
282 client_socket_ht_node
);
284 return !!(client
->socket
== socket
);
288 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
290 struct channel_key
*channel_key
= (struct channel_key
*) key
;
291 struct lttng_channel_trigger_list
*trigger_list
;
293 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
294 channel_triggers_ht_node
);
296 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
297 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
301 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
303 const char *session_name
= (const char *) key
;
304 struct lttng_session_trigger_list
*trigger_list
;
306 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
307 session_triggers_ht_node
);
309 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
313 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
315 struct channel_key
*channel_key
= (struct channel_key
*) key
;
316 struct channel_state_sample
*sample
;
318 sample
= caa_container_of(node
, struct channel_state_sample
,
319 channel_state_ht_node
);
321 return !!((channel_key
->key
== sample
->key
.key
) &&
322 (channel_key
->domain
== sample
->key
.domain
));
326 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
328 struct channel_key
*channel_key
= (struct channel_key
*) key
;
329 struct channel_info
*channel_info
;
331 channel_info
= caa_container_of(node
, struct channel_info
,
334 return !!((channel_key
->key
== channel_info
->key
.key
) &&
335 (channel_key
->domain
== channel_info
->key
.domain
));
339 int match_condition(struct cds_lfht_node
*node
, const void *key
)
341 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
342 struct lttng_trigger_ht_element
*trigger
;
343 struct lttng_condition
*condition
;
345 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
347 condition
= lttng_trigger_get_condition(trigger
->trigger
);
350 return !!lttng_condition_is_equal(condition_key
, condition
);
354 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
356 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
357 struct notification_client_list
*client_list
;
358 const struct lttng_condition
*condition
;
360 assert(condition_key
);
362 client_list
= caa_container_of(node
, struct notification_client_list
,
363 notification_trigger_ht_node
);
364 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
366 return !!lttng_condition_is_equal(condition_key
, condition
);
370 int match_session(struct cds_lfht_node
*node
, const void *key
)
372 const char *name
= key
;
373 struct session_info
*session_info
= caa_container_of(
374 node
, struct session_info
, sessions_ht_node
);
376 return !strcmp(session_info
->name
, name
);
380 unsigned long lttng_condition_buffer_usage_hash(
381 const struct lttng_condition
*_condition
)
384 unsigned long condition_type
;
385 struct lttng_condition_buffer_usage
*condition
;
387 condition
= container_of(_condition
,
388 struct lttng_condition_buffer_usage
, parent
);
390 condition_type
= (unsigned long) condition
->parent
.type
;
391 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
392 if (condition
->session_name
) {
393 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
395 if (condition
->channel_name
) {
396 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
398 if (condition
->domain
.set
) {
399 hash
^= hash_key_ulong(
400 (void *) condition
->domain
.type
,
403 if (condition
->threshold_ratio
.set
) {
406 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
407 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
408 } else if (condition
->threshold_bytes
.set
) {
411 val
= condition
->threshold_bytes
.value
;
412 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
418 unsigned long lttng_condition_session_consumed_size_hash(
419 const struct lttng_condition
*_condition
)
422 unsigned long condition_type
=
423 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
424 struct lttng_condition_session_consumed_size
*condition
;
427 condition
= container_of(_condition
,
428 struct lttng_condition_session_consumed_size
, parent
);
430 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
431 if (condition
->session_name
) {
432 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
434 val
= condition
->consumed_threshold_bytes
.value
;
435 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
440 unsigned long lttng_condition_session_rotation_hash(
441 const struct lttng_condition
*_condition
)
443 unsigned long hash
, condition_type
;
444 struct lttng_condition_session_rotation
*condition
;
446 condition
= container_of(_condition
,
447 struct lttng_condition_session_rotation
, parent
);
448 condition_type
= (unsigned long) condition
->parent
.type
;
449 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
450 assert(condition
->session_name
);
451 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
456 * The lttng_condition hashing code is kept in this file (rather than
457 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
458 * don't want to link in liblttng-ctl.
461 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
463 switch (condition
->type
) {
464 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
465 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
466 return lttng_condition_buffer_usage_hash(condition
);
467 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
468 return lttng_condition_session_consumed_size_hash(condition
);
469 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
470 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
471 return lttng_condition_session_rotation_hash(condition
);
473 ERR("[notification-thread] Unexpected condition type caught");
479 unsigned long hash_channel_key(struct channel_key
*key
)
481 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
482 unsigned long domain_hash
= hash_key_ulong(
483 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
485 return key_hash
^ domain_hash
;
489 * Get the type of object to which a given condition applies. Bindings let
490 * the notification system evaluate a trigger's condition when a given
491 * object's state is updated.
493 * For instance, a condition bound to a channel will be evaluated everytime
494 * the channel's state is changed by a channel monitoring sample.
497 enum lttng_object_type
get_condition_binding_object(
498 const struct lttng_condition
*condition
)
500 switch (lttng_condition_get_type(condition
)) {
501 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
502 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
503 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
504 return LTTNG_OBJECT_TYPE_CHANNEL
;
505 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
506 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
507 return LTTNG_OBJECT_TYPE_SESSION
;
509 return LTTNG_OBJECT_TYPE_UNKNOWN
;
514 void free_channel_info_rcu(struct rcu_head
*node
)
516 free(caa_container_of(node
, struct channel_info
, rcu_node
));
520 void channel_info_destroy(struct channel_info
*channel_info
)
526 if (channel_info
->session_info
) {
527 session_info_remove_channel(channel_info
->session_info
,
529 session_info_put(channel_info
->session_info
);
531 if (channel_info
->name
) {
532 free(channel_info
->name
);
534 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
538 void free_session_info_rcu(struct rcu_head
*node
)
540 free(caa_container_of(node
, struct session_info
, rcu_node
));
543 /* Don't call directly, use the ref-counting mechanism. */
545 void session_info_destroy(void *_data
)
547 struct session_info
*session_info
= _data
;
550 assert(session_info
);
551 if (session_info
->channel_infos_ht
) {
552 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
554 ERR("[notification-thread] Failed to destroy channel information hash table");
557 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
560 cds_lfht_del(session_info
->sessions_ht
,
561 &session_info
->sessions_ht_node
);
563 free(session_info
->name
);
564 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
568 void session_info_get(struct session_info
*session_info
)
573 lttng_ref_get(&session_info
->ref
);
577 void session_info_put(struct session_info
*session_info
)
582 lttng_ref_put(&session_info
->ref
);
586 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
587 struct lttng_session_trigger_list
*trigger_list
,
588 struct cds_lfht
*sessions_ht
)
590 struct session_info
*session_info
;
594 session_info
= zmalloc(sizeof(*session_info
));
598 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
600 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
601 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
602 if (!session_info
->channel_infos_ht
) {
606 cds_lfht_node_init(&session_info
->sessions_ht_node
);
607 session_info
->name
= strdup(name
);
608 if (!session_info
->name
) {
611 session_info
->uid
= uid
;
612 session_info
->gid
= gid
;
613 session_info
->trigger_list
= trigger_list
;
614 session_info
->sessions_ht
= sessions_ht
;
618 session_info_put(session_info
);
623 void session_info_add_channel(struct session_info
*session_info
,
624 struct channel_info
*channel_info
)
627 cds_lfht_add(session_info
->channel_infos_ht
,
628 hash_channel_key(&channel_info
->key
),
629 &channel_info
->session_info_channels_ht_node
);
634 void session_info_remove_channel(struct session_info
*session_info
,
635 struct channel_info
*channel_info
)
638 cds_lfht_del(session_info
->channel_infos_ht
,
639 &channel_info
->session_info_channels_ht_node
);
644 struct channel_info
*channel_info_create(const char *channel_name
,
645 struct channel_key
*channel_key
, uint64_t channel_capacity
,
646 struct session_info
*session_info
)
648 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
654 cds_lfht_node_init(&channel_info
->channels_ht_node
);
655 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
656 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
657 channel_info
->capacity
= channel_capacity
;
659 channel_info
->name
= strdup(channel_name
);
660 if (!channel_info
->name
) {
665 * Set the references between session and channel infos:
666 * - channel_info holds a strong reference to session_info
667 * - session_info holds a weak reference to channel_info
669 session_info_get(session_info
);
670 session_info_add_channel(session_info
, channel_info
);
671 channel_info
->session_info
= session_info
;
675 channel_info_destroy(channel_info
);
679 /* RCU read lock must be held by the caller. */
681 struct notification_client_list
*get_client_list_from_condition(
682 struct notification_thread_state
*state
,
683 const struct lttng_condition
*condition
)
685 struct cds_lfht_node
*node
;
686 struct cds_lfht_iter iter
;
688 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
689 lttng_condition_hash(condition
),
690 match_client_list_condition
,
693 node
= cds_lfht_iter_get_node(&iter
);
695 return node
? caa_container_of(node
,
696 struct notification_client_list
,
697 notification_trigger_ht_node
) : NULL
;
700 /* This function must be called with the RCU read lock held. */
702 int evaluate_channel_condition_for_client(
703 const struct lttng_condition
*condition
,
704 struct notification_thread_state
*state
,
705 struct lttng_evaluation
**evaluation
,
706 uid_t
*session_uid
, gid_t
*session_gid
)
709 struct cds_lfht_iter iter
;
710 struct cds_lfht_node
*node
;
711 struct channel_info
*channel_info
= NULL
;
712 struct channel_key
*channel_key
= NULL
;
713 struct channel_state_sample
*last_sample
= NULL
;
714 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
716 /* Find the channel associated with the condition. */
717 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
718 channel_trigger_list
, channel_triggers_ht_node
) {
719 struct lttng_trigger_list_element
*element
;
721 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
722 const struct lttng_condition
*current_condition
=
723 lttng_trigger_get_const_condition(
726 assert(current_condition
);
727 if (!lttng_condition_is_equal(condition
,
728 current_condition
)) {
732 /* Found the trigger, save the channel key. */
733 channel_key
= &channel_trigger_list
->channel_key
;
737 /* The channel key was found stop iteration. */
743 /* No channel found; normal exit. */
744 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
749 /* Fetch channel info for the matching channel. */
750 cds_lfht_lookup(state
->channels_ht
,
751 hash_channel_key(channel_key
),
755 node
= cds_lfht_iter_get_node(&iter
);
757 channel_info
= caa_container_of(node
, struct channel_info
,
760 /* Retrieve the channel's last sample, if it exists. */
761 cds_lfht_lookup(state
->channel_state_ht
,
762 hash_channel_key(channel_key
),
763 match_channel_state_sample
,
766 node
= cds_lfht_iter_get_node(&iter
);
768 last_sample
= caa_container_of(node
,
769 struct channel_state_sample
,
770 channel_state_ht_node
);
772 /* Nothing to evaluate, no sample was ever taken. Normal exit */
773 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
778 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
780 0, channel_info
->session_info
->consumed_data_size
,
783 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
787 *session_uid
= channel_info
->session_info
->uid
;
788 *session_gid
= channel_info
->session_info
->gid
;
794 const char *get_condition_session_name(const struct lttng_condition
*condition
)
796 const char *session_name
= NULL
;
797 enum lttng_condition_status status
;
799 switch (lttng_condition_get_type(condition
)) {
800 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
801 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
802 status
= lttng_condition_buffer_usage_get_session_name(
803 condition
, &session_name
);
805 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
806 status
= lttng_condition_session_consumed_size_get_session_name(
807 condition
, &session_name
);
809 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
810 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
811 status
= lttng_condition_session_rotation_get_session_name(
812 condition
, &session_name
);
817 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
818 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
825 /* This function must be called with the RCU read lock held. */
827 int evaluate_session_condition_for_client(
828 const struct lttng_condition
*condition
,
829 struct notification_thread_state
*state
,
830 struct lttng_evaluation
**evaluation
,
831 uid_t
*session_uid
, gid_t
*session_gid
)
834 struct cds_lfht_iter iter
;
835 struct cds_lfht_node
*node
;
836 const char *session_name
;
837 struct session_info
*session_info
= NULL
;
839 session_name
= get_condition_session_name(condition
);
841 /* Find the session associated with the trigger. */
842 cds_lfht_lookup(state
->sessions_ht
,
843 hash_key_str(session_name
, lttng_ht_seed
),
847 node
= cds_lfht_iter_get_node(&iter
);
849 DBG("[notification-thread] No known session matching name \"%s\"",
855 session_info
= caa_container_of(node
, struct session_info
,
857 session_info_get(session_info
);
860 * Evaluation is performed in-line here since only one type of
861 * session-bound condition is handled for the moment.
863 switch (lttng_condition_get_type(condition
)) {
864 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
865 if (!session_info
->rotation
.ongoing
) {
867 goto end_session_put
;
870 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
871 session_info
->rotation
.id
);
874 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
877 goto end_session_put
;
883 goto end_session_put
;
886 *session_uid
= session_info
->uid
;
887 *session_gid
= session_info
->gid
;
890 session_info_put(session_info
);
895 /* This function must be called with the RCU read lock held. */
897 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
898 const struct lttng_condition
*condition
,
899 struct notification_client
*client
,
900 struct notification_thread_state
*state
)
903 struct lttng_evaluation
*evaluation
= NULL
;
904 struct notification_client_list client_list
= { 0 };
905 struct notification_client_list_element client_list_element
= { 0 };
906 uid_t object_uid
= 0;
907 gid_t object_gid
= 0;
914 switch (get_condition_binding_object(condition
)) {
915 case LTTNG_OBJECT_TYPE_SESSION
:
916 ret
= evaluate_session_condition_for_client(condition
, state
,
917 &evaluation
, &object_uid
, &object_gid
);
919 case LTTNG_OBJECT_TYPE_CHANNEL
:
920 ret
= evaluate_channel_condition_for_client(condition
, state
,
921 &evaluation
, &object_uid
, &object_gid
);
923 case LTTNG_OBJECT_TYPE_NONE
:
926 case LTTNG_OBJECT_TYPE_UNKNOWN
:
933 /* Evaluation yielded nothing. Normal exit. */
934 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
940 * Create a temporary client list with the client currently
943 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
944 CDS_INIT_LIST_HEAD(&client_list
.list
);
945 client_list
.trigger
= trigger
;
947 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
948 client_list_element
.client
= client
;
949 cds_list_add(&client_list_element
.node
, &client_list
.list
);
951 /* Send evaluation result to the newly-subscribed client. */
952 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
953 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
954 state
, object_uid
, object_gid
);
961 int notification_thread_client_subscribe(struct notification_client
*client
,
962 struct lttng_condition
*condition
,
963 struct notification_thread_state
*state
,
964 enum lttng_notification_channel_status
*_status
)
967 struct notification_client_list
*client_list
;
968 struct lttng_condition_list_element
*condition_list_element
= NULL
;
969 struct notification_client_list_element
*client_list_element
= NULL
;
970 enum lttng_notification_channel_status status
=
971 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
974 * Ensure that the client has not already subscribed to this condition
977 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
978 if (lttng_condition_is_equal(condition_list_element
->condition
,
980 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
985 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
986 if (!condition_list_element
) {
990 client_list_element
= zmalloc(sizeof(*client_list_element
));
991 if (!client_list_element
) {
999 * Add the newly-subscribed condition to the client's subscription list.
1001 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1002 condition_list_element
->condition
= condition
;
1003 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1005 client_list
= get_client_list_from_condition(state
, condition
);
1008 * No notification-emiting trigger registered with this
1009 * condition. We don't evaluate the condition right away
1010 * since this trigger is not registered yet.
1012 free(client_list_element
);
1017 * The condition to which the client just subscribed is evaluated
1018 * at this point so that conditions that are already TRUE result
1019 * in a notification being sent out.
1021 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1023 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1025 free(client_list_element
);
1030 * Add the client to the list of clients interested in a given trigger
1031 * if a "notification" trigger with a corresponding condition was
1034 client_list_element
->client
= client
;
1035 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1036 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1045 free(condition_list_element
);
1046 free(client_list_element
);
1051 int notification_thread_client_unsubscribe(
1052 struct notification_client
*client
,
1053 struct lttng_condition
*condition
,
1054 struct notification_thread_state
*state
,
1055 enum lttng_notification_channel_status
*_status
)
1057 struct notification_client_list
*client_list
;
1058 struct lttng_condition_list_element
*condition_list_element
,
1060 struct notification_client_list_element
*client_list_element
,
1062 bool condition_found
= false;
1063 enum lttng_notification_channel_status status
=
1064 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1066 /* Remove the condition from the client's condition list. */
1067 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1068 &client
->condition_list
, node
) {
1069 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1074 cds_list_del(&condition_list_element
->node
);
1076 * The caller may be iterating on the client's conditions to
1077 * tear down a client's connection. In this case, the condition
1078 * will be destroyed at the end.
1080 if (condition
!= condition_list_element
->condition
) {
1081 lttng_condition_destroy(
1082 condition_list_element
->condition
);
1084 free(condition_list_element
);
1085 condition_found
= true;
1089 if (!condition_found
) {
1090 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1095 * Remove the client from the list of clients interested the trigger
1096 * matching the condition.
1099 client_list
= get_client_list_from_condition(state
, condition
);
1104 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1105 &client_list
->list
, node
) {
1106 if (client_list_element
->client
->socket
!= client
->socket
) {
1109 cds_list_del(&client_list_element
->node
);
1110 free(client_list_element
);
1116 lttng_condition_destroy(condition
);
1124 void free_notification_client_rcu(struct rcu_head
*node
)
1126 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1130 void notification_client_destroy(struct notification_client
*client
,
1131 struct notification_thread_state
*state
)
1133 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
1139 /* Release all conditions to which the client was subscribed. */
1140 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
1141 &client
->condition_list
, node
) {
1142 (void) notification_thread_client_unsubscribe(client
,
1143 condition_list_element
->condition
, state
, NULL
);
1146 if (client
->socket
>= 0) {
1147 (void) lttcomm_close_unix_sock(client
->socket
);
1149 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1150 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1151 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1155 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1159 struct notification_client
*get_client_from_socket(int socket
,
1160 struct notification_thread_state
*state
)
1162 struct cds_lfht_iter iter
;
1163 struct cds_lfht_node
*node
;
1164 struct notification_client
*client
= NULL
;
1166 cds_lfht_lookup(state
->client_socket_ht
,
1167 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
1169 (void *) (unsigned long) socket
,
1171 node
= cds_lfht_iter_get_node(&iter
);
1176 client
= caa_container_of(node
, struct notification_client
,
1177 client_socket_ht_node
);
1183 bool buffer_usage_condition_applies_to_channel(
1184 const struct lttng_condition
*condition
,
1185 const struct channel_info
*channel_info
)
1187 enum lttng_condition_status status
;
1188 enum lttng_domain_type condition_domain
;
1189 const char *condition_session_name
= NULL
;
1190 const char *condition_channel_name
= NULL
;
1192 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1194 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1195 if (channel_info
->key
.domain
!= condition_domain
) {
1199 status
= lttng_condition_buffer_usage_get_session_name(
1200 condition
, &condition_session_name
);
1201 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1203 status
= lttng_condition_buffer_usage_get_channel_name(
1204 condition
, &condition_channel_name
);
1205 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1207 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1210 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1220 bool session_consumed_size_condition_applies_to_channel(
1221 const struct lttng_condition
*condition
,
1222 const struct channel_info
*channel_info
)
1224 enum lttng_condition_status status
;
1225 const char *condition_session_name
= NULL
;
1227 status
= lttng_condition_session_consumed_size_get_session_name(
1228 condition
, &condition_session_name
);
1229 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1231 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1241 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1242 const struct channel_info
*channel_info
)
1244 const struct lttng_condition
*condition
;
1245 bool trigger_applies
;
1247 condition
= lttng_trigger_get_const_condition(trigger
);
1252 switch (lttng_condition_get_type(condition
)) {
1253 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1254 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1255 trigger_applies
= buffer_usage_condition_applies_to_channel(
1256 condition
, channel_info
);
1258 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1259 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1260 condition
, channel_info
);
1266 return trigger_applies
;
1272 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1273 struct notification_client
*client
)
1275 bool applies
= false;
1276 struct lttng_condition_list_element
*condition_list_element
;
1278 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1280 applies
= lttng_condition_is_equal(
1281 condition_list_element
->condition
,
1282 lttng_trigger_get_condition(trigger
));
1290 /* Must be called with RCU read lock held. */
1292 struct lttng_session_trigger_list
*get_session_trigger_list(
1293 struct notification_thread_state
*state
,
1294 const char *session_name
)
1296 struct lttng_session_trigger_list
*list
= NULL
;
1297 struct cds_lfht_node
*node
;
1298 struct cds_lfht_iter iter
;
1300 cds_lfht_lookup(state
->session_triggers_ht
,
1301 hash_key_str(session_name
, lttng_ht_seed
),
1302 match_session_trigger_list
,
1305 node
= cds_lfht_iter_get_node(&iter
);
1308 * Not an error, the list of triggers applying to that session
1309 * will be initialized when the session is created.
1311 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1316 list
= caa_container_of(node
,
1317 struct lttng_session_trigger_list
,
1318 session_triggers_ht_node
);
1324 * Allocate an empty lttng_session_trigger_list for the session named
1327 * No ownership of 'session_name' is assumed by the session trigger list.
1328 * It is the caller's responsability to ensure the session name is alive
1329 * for as long as this list is.
1332 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1333 const char *session_name
,
1334 struct cds_lfht
*session_triggers_ht
)
1336 struct lttng_session_trigger_list
*list
;
1338 list
= zmalloc(sizeof(*list
));
1342 list
->session_name
= session_name
;
1343 CDS_INIT_LIST_HEAD(&list
->list
);
1344 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1345 list
->session_triggers_ht
= session_triggers_ht
;
1348 /* Publish the list through the session_triggers_ht. */
1349 cds_lfht_add(session_triggers_ht
,
1350 hash_key_str(session_name
, lttng_ht_seed
),
1351 &list
->session_triggers_ht_node
);
1358 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1360 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1365 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1367 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1369 /* Empty the list element by element, and then free the list itself. */
1370 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1371 &list
->list
, node
) {
1372 cds_list_del(&trigger_list_element
->node
);
1373 free(trigger_list_element
);
1376 /* Unpublish the list from the session_triggers_ht. */
1377 cds_lfht_del(list
->session_triggers_ht
,
1378 &list
->session_triggers_ht_node
);
1380 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1384 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1385 const struct lttng_trigger
*trigger
)
1388 struct lttng_trigger_list_element
*new_element
=
1389 zmalloc(sizeof(*new_element
));
1395 CDS_INIT_LIST_HEAD(&new_element
->node
);
1396 new_element
->trigger
= trigger
;
1397 cds_list_add(&new_element
->node
, &list
->list
);
1403 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1404 const char *session_name
)
1406 bool applies
= false;
1407 const struct lttng_condition
*condition
;
1409 condition
= lttng_trigger_get_const_condition(trigger
);
1410 switch (lttng_condition_get_type(condition
)) {
1411 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1412 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1414 enum lttng_condition_status condition_status
;
1415 const char *condition_session_name
;
1417 condition_status
= lttng_condition_session_rotation_get_session_name(
1418 condition
, &condition_session_name
);
1419 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1420 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1424 assert(condition_session_name
);
1425 applies
= !strcmp(condition_session_name
, session_name
);
1436 * Allocate and initialize an lttng_session_trigger_list which contains
1437 * all triggers that apply to the session named 'session_name'.
1439 * No ownership of 'session_name' is assumed by the session trigger list.
1440 * It is the caller's responsability to ensure the session name is alive
1441 * for as long as this list is.
1444 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1445 const struct notification_thread_state
*state
,
1446 const char *session_name
)
1448 int trigger_count
= 0;
1449 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1450 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1451 struct cds_lfht_iter iter
;
1453 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1454 state
->session_triggers_ht
);
1456 /* Add all triggers applying to the session named 'session_name'. */
1457 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1461 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1466 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1467 trigger_ht_element
->trigger
);
1475 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1477 return session_trigger_list
;
1479 lttng_session_trigger_list_destroy(session_trigger_list
);
1484 struct session_info
*find_or_create_session_info(
1485 struct notification_thread_state
*state
,
1486 const char *name
, uid_t uid
, gid_t gid
)
1488 struct session_info
*session
= NULL
;
1489 struct cds_lfht_node
*node
;
1490 struct cds_lfht_iter iter
;
1491 struct lttng_session_trigger_list
*trigger_list
;
1494 cds_lfht_lookup(state
->sessions_ht
,
1495 hash_key_str(name
, lttng_ht_seed
),
1499 node
= cds_lfht_iter_get_node(&iter
);
1501 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1503 session
= caa_container_of(node
, struct session_info
,
1505 assert(session
->uid
== uid
);
1506 assert(session
->gid
== gid
);
1507 session_info_get(session
);
1511 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1512 if (!trigger_list
) {
1516 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1517 state
->sessions_ht
);
1519 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1521 lttng_session_trigger_list_destroy(trigger_list
);
1524 trigger_list
= NULL
;
1526 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1527 &session
->sessions_ht_node
);
1533 session_info_put(session
);
1538 int handle_notification_thread_command_add_channel(
1539 struct notification_thread_state
*state
,
1540 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1541 const char *channel_name
, enum lttng_domain_type channel_domain
,
1542 uint64_t channel_key_int
, uint64_t channel_capacity
,
1543 enum lttng_error_code
*cmd_result
)
1545 struct cds_list_head trigger_list
;
1546 struct channel_info
*new_channel_info
= NULL
;
1547 struct channel_key channel_key
= {
1548 .key
= channel_key_int
,
1549 .domain
= channel_domain
,
1551 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1552 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1553 int trigger_count
= 0;
1554 struct cds_lfht_iter iter
;
1555 struct session_info
*session_info
= NULL
;
1557 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1558 channel_name
, session_name
, channel_key_int
,
1559 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1561 CDS_INIT_LIST_HEAD(&trigger_list
);
1563 session_info
= find_or_create_session_info(state
, session_name
,
1564 session_uid
, session_gid
);
1565 if (!session_info
) {
1566 /* Allocation error or an internal error occurred. */
1570 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1571 channel_capacity
, session_info
);
1572 if (!new_channel_info
) {
1577 /* Build a list of all triggers applying to the new channel. */
1578 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1580 struct lttng_trigger_list_element
*new_element
;
1582 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1583 new_channel_info
)) {
1587 new_element
= zmalloc(sizeof(*new_element
));
1592 CDS_INIT_LIST_HEAD(&new_element
->node
);
1593 new_element
->trigger
= trigger_ht_element
->trigger
;
1594 cds_list_add(&new_element
->node
, &trigger_list
);
1599 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1601 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1602 if (!channel_trigger_list
) {
1605 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1606 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1607 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1608 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1611 /* Add channel to the channel_ht which owns the channel_infos. */
1612 cds_lfht_add(state
->channels_ht
,
1613 hash_channel_key(&new_channel_info
->key
),
1614 &new_channel_info
->channels_ht_node
);
1616 * Add the list of triggers associated with this channel to the
1617 * channel_triggers_ht.
1619 cds_lfht_add(state
->channel_triggers_ht
,
1620 hash_channel_key(&new_channel_info
->key
),
1621 &channel_trigger_list
->channel_triggers_ht_node
);
1623 session_info_put(session_info
);
1624 *cmd_result
= LTTNG_OK
;
1627 channel_info_destroy(new_channel_info
);
1628 session_info_put(session_info
);
1633 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1635 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1640 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1642 free(caa_container_of(node
, struct channel_state_sample
,
1647 int handle_notification_thread_command_remove_channel(
1648 struct notification_thread_state
*state
,
1649 uint64_t channel_key
, enum lttng_domain_type domain
,
1650 enum lttng_error_code
*cmd_result
)
1652 struct cds_lfht_node
*node
;
1653 struct cds_lfht_iter iter
;
1654 struct lttng_channel_trigger_list
*trigger_list
;
1655 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1656 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1657 struct channel_info
*channel_info
;
1659 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1660 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1664 cds_lfht_lookup(state
->channel_triggers_ht
,
1665 hash_channel_key(&key
),
1666 match_channel_trigger_list
,
1669 node
= cds_lfht_iter_get_node(&iter
);
1671 * There is a severe internal error if we are being asked to remove a
1672 * channel that doesn't exist.
1675 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1679 /* Free the list of triggers associated with this channel. */
1680 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1681 channel_triggers_ht_node
);
1682 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1683 &trigger_list
->list
, node
) {
1684 cds_list_del(&trigger_list_element
->node
);
1685 free(trigger_list_element
);
1687 cds_lfht_del(state
->channel_triggers_ht
, node
);
1688 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1690 /* Free sampled channel state. */
1691 cds_lfht_lookup(state
->channel_state_ht
,
1692 hash_channel_key(&key
),
1693 match_channel_state_sample
,
1696 node
= cds_lfht_iter_get_node(&iter
);
1698 * This is expected to be NULL if the channel is destroyed before we
1699 * received a sample.
1702 struct channel_state_sample
*sample
= caa_container_of(node
,
1703 struct channel_state_sample
,
1704 channel_state_ht_node
);
1706 cds_lfht_del(state
->channel_state_ht
, node
);
1707 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1710 /* Remove the channel from the channels_ht and free it. */
1711 cds_lfht_lookup(state
->channels_ht
,
1712 hash_channel_key(&key
),
1716 node
= cds_lfht_iter_get_node(&iter
);
1718 channel_info
= caa_container_of(node
, struct channel_info
,
1720 cds_lfht_del(state
->channels_ht
, node
);
1721 channel_info_destroy(channel_info
);
1724 *cmd_result
= LTTNG_OK
;
1729 int handle_notification_thread_command_session_rotation(
1730 struct notification_thread_state
*state
,
1731 enum notification_thread_command_type cmd_type
,
1732 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1733 uint64_t trace_archive_chunk_id
,
1734 struct lttng_trace_archive_location
*location
,
1735 enum lttng_error_code
*_cmd_result
)
1738 enum lttng_error_code cmd_result
= LTTNG_OK
;
1739 struct lttng_session_trigger_list
*trigger_list
;
1740 struct lttng_trigger_list_element
*trigger_list_element
;
1741 struct session_info
*session_info
;
1745 session_info
= find_or_create_session_info(state
, session_name
,
1746 session_uid
, session_gid
);
1747 if (!session_info
) {
1748 /* Allocation error or an internal error occurred. */
1750 cmd_result
= LTTNG_ERR_NOMEM
;
1754 session_info
->rotation
.ongoing
=
1755 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1756 session_info
->rotation
.id
= trace_archive_chunk_id
;
1757 trigger_list
= get_session_trigger_list(state
, session_name
);
1758 if (!trigger_list
) {
1759 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1764 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1766 const struct lttng_condition
*condition
;
1767 const struct lttng_action
*action
;
1768 const struct lttng_trigger
*trigger
;
1769 struct notification_client_list
*client_list
;
1770 struct lttng_evaluation
*evaluation
= NULL
;
1771 enum lttng_condition_type condition_type
;
1773 trigger
= trigger_list_element
->trigger
;
1774 condition
= lttng_trigger_get_const_condition(trigger
);
1776 condition_type
= lttng_condition_get_type(condition
);
1778 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1779 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1781 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1782 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1786 action
= lttng_trigger_get_const_action(trigger
);
1788 /* Notify actions are the only type currently supported. */
1789 assert(lttng_action_get_type_const(action
) ==
1790 LTTNG_ACTION_TYPE_NOTIFY
);
1792 client_list
= get_client_list_from_condition(state
, condition
);
1793 assert(client_list
);
1795 if (cds_list_empty(&client_list
->list
)) {
1797 * No clients interested in the evaluation's result,
1803 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1804 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1805 trace_archive_chunk_id
);
1807 evaluation
= lttng_evaluation_session_rotation_completed_create(
1808 trace_archive_chunk_id
, location
);
1812 /* Internal error */
1814 cmd_result
= LTTNG_ERR_UNK
;
1818 /* Dispatch evaluation result to all clients. */
1819 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1820 evaluation
, client_list
, state
,
1823 lttng_evaluation_destroy(evaluation
);
1824 if (caa_unlikely(ret
)) {
1829 session_info_put(session_info
);
1830 *_cmd_result
= cmd_result
;
1836 int condition_is_supported(struct lttng_condition
*condition
)
1840 switch (lttng_condition_get_type(condition
)) {
1841 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1842 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1844 enum lttng_domain_type domain
;
1846 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1853 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1859 * Older kernel tracers don't expose the API to monitor their
1860 * buffers. Therefore, we reject triggers that require that
1861 * mechanism to be available to be evaluated.
1863 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1874 /* Must be called with RCU read lock held. */
1876 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1877 struct notification_thread_state
*state
)
1880 const struct lttng_condition
*condition
;
1881 const char *session_name
;
1882 struct lttng_session_trigger_list
*trigger_list
;
1884 condition
= lttng_trigger_get_const_condition(trigger
);
1885 switch (lttng_condition_get_type(condition
)) {
1886 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1887 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1889 enum lttng_condition_status status
;
1891 status
= lttng_condition_session_rotation_get_session_name(
1892 condition
, &session_name
);
1893 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1894 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1905 trigger_list
= get_session_trigger_list(state
, session_name
);
1906 if (!trigger_list
) {
1907 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1913 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1915 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1920 /* Must be called with RCU read lock held. */
1922 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1923 struct notification_thread_state
*state
)
1926 struct cds_lfht_node
*node
;
1927 struct cds_lfht_iter iter
;
1928 struct channel_info
*channel
;
1930 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1932 struct lttng_trigger_list_element
*trigger_list_element
;
1933 struct lttng_channel_trigger_list
*trigger_list
;
1934 struct cds_lfht_iter lookup_iter
;
1936 if (!trigger_applies_to_channel(trigger
, channel
)) {
1940 cds_lfht_lookup(state
->channel_triggers_ht
,
1941 hash_channel_key(&channel
->key
),
1942 match_channel_trigger_list
,
1945 node
= cds_lfht_iter_get_node(&lookup_iter
);
1947 trigger_list
= caa_container_of(node
,
1948 struct lttng_channel_trigger_list
,
1949 channel_triggers_ht_node
);
1951 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1952 if (!trigger_list_element
) {
1956 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1957 trigger_list_element
->trigger
= trigger
;
1958 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1959 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
1967 * FIXME A client's credentials are not checked when registering a trigger, nor
1968 * are they stored alongside with the trigger.
1970 * The effects of this are benign since:
1971 * - The client will succeed in registering the trigger, as it is valid,
1972 * - The trigger will, internally, be bound to the channel/session,
1973 * - The notifications will not be sent since the client's credentials
1974 * are checked against the channel at that moment.
1976 * If this function returns a non-zero value, it means something is
1977 * fundamentally broken and the whole subsystem/thread will be torn down.
1979 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1983 int handle_notification_thread_command_register_trigger(
1984 struct notification_thread_state
*state
,
1985 struct lttng_trigger
*trigger
,
1986 enum lttng_error_code
*cmd_result
)
1989 struct lttng_condition
*condition
;
1990 struct notification_client
*client
;
1991 struct notification_client_list
*client_list
= NULL
;
1992 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1993 struct notification_client_list_element
*client_list_element
, *tmp
;
1994 struct cds_lfht_node
*node
;
1995 struct cds_lfht_iter iter
;
1996 bool free_trigger
= true;
2000 condition
= lttng_trigger_get_condition(trigger
);
2003 ret
= condition_is_supported(condition
);
2006 } else if (ret
== 0) {
2007 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2010 /* Feature is supported, continue. */
2014 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2015 if (!trigger_ht_element
) {
2020 /* Add trigger to the trigger_ht. */
2021 cds_lfht_node_init(&trigger_ht_element
->node
);
2022 trigger_ht_element
->trigger
= trigger
;
2024 node
= cds_lfht_add_unique(state
->triggers_ht
,
2025 lttng_condition_hash(condition
),
2028 &trigger_ht_element
->node
);
2029 if (node
!= &trigger_ht_element
->node
) {
2030 /* Not a fatal error, simply report it to the client. */
2031 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2032 goto error_free_ht_element
;
2036 * Ownership of the trigger and of its wrapper was transfered to
2039 trigger_ht_element
= NULL
;
2040 free_trigger
= false;
2043 * The rest only applies to triggers that have a "notify" action.
2044 * It is not skipped as this is the only action type currently
2047 client_list
= zmalloc(sizeof(*client_list
));
2050 goto error_free_ht_element
;
2052 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
2053 CDS_INIT_LIST_HEAD(&client_list
->list
);
2054 client_list
->trigger
= trigger
;
2056 /* Build a list of clients to which this new trigger applies. */
2057 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2058 client_socket_ht_node
) {
2059 if (!trigger_applies_to_client(trigger
, client
)) {
2063 client_list_element
= zmalloc(sizeof(*client_list_element
));
2064 if (!client_list_element
) {
2066 goto error_free_client_list
;
2068 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2069 client_list_element
->client
= client
;
2070 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2073 cds_lfht_add(state
->notification_trigger_clients_ht
,
2074 lttng_condition_hash(condition
),
2075 &client_list
->notification_trigger_ht_node
);
2077 switch (get_condition_binding_object(condition
)) {
2078 case LTTNG_OBJECT_TYPE_SESSION
:
2079 /* Add the trigger to the list if it matches a known session. */
2080 ret
= bind_trigger_to_matching_session(trigger
, state
);
2082 goto error_free_client_list
;
2085 case LTTNG_OBJECT_TYPE_CHANNEL
:
2087 * Add the trigger to list of triggers bound to the channels
2090 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2092 goto error_free_client_list
;
2095 case LTTNG_OBJECT_TYPE_NONE
:
2098 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2100 goto error_free_client_list
;
2104 * Since there is nothing preventing clients from subscribing to a
2105 * condition before the corresponding trigger is registered, we have
2106 * to evaluate this new condition right away.
2108 * At some point, we were waiting for the next "evaluation" (e.g. on
2109 * reception of a channel sample) to evaluate this new condition, but
2112 * The reason it was broken is that waiting for the next sample
2113 * does not allow us to properly handle transitions for edge-triggered
2116 * Consider this example: when we handle a new channel sample, we
2117 * evaluate each conditions twice: once with the previous state, and
2118 * again with the newest state. We then use those two results to
2119 * determine whether a state change happened: a condition was false and
2120 * became true. If a state change happened, we have to notify clients.
2122 * Now, if a client subscribes to a given notification and registers
2123 * a trigger *after* that subscription, we have to make sure the
2124 * condition is evaluated at this point while considering only the
2125 * current state. Otherwise, the next evaluation cycle may only see
2126 * that the evaluations remain the same (true for samples n-1 and n) and
2127 * the client will never know that the condition has been met.
2129 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2130 &client_list
->list
, node
) {
2131 ret
= evaluate_condition_for_client(trigger
, condition
,
2132 client_list_element
->client
, state
);
2134 goto error_free_client_list
;
2139 * Client list ownership transferred to the
2140 * notification_trigger_clients_ht.
2144 *cmd_result
= LTTNG_OK
;
2145 error_free_client_list
:
2147 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2148 &client_list
->list
, node
) {
2149 free(client_list_element
);
2153 error_free_ht_element
:
2154 free(trigger_ht_element
);
2157 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
2159 lttng_condition_destroy(condition
);
2160 lttng_action_destroy(action
);
2161 lttng_trigger_destroy(trigger
);
2168 void free_notification_client_list_rcu(struct rcu_head
*node
)
2170 free(caa_container_of(node
, struct notification_client_list
,
2175 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2177 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2182 int handle_notification_thread_command_unregister_trigger(
2183 struct notification_thread_state
*state
,
2184 struct lttng_trigger
*trigger
,
2185 enum lttng_error_code
*_cmd_reply
)
2187 struct cds_lfht_iter iter
;
2188 struct cds_lfht_node
*triggers_ht_node
;
2189 struct lttng_channel_trigger_list
*trigger_list
;
2190 struct notification_client_list
*client_list
;
2191 struct notification_client_list_element
*client_list_element
, *tmp
;
2192 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2193 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2195 struct lttng_action
*action
;
2196 enum lttng_error_code cmd_reply
;
2200 cds_lfht_lookup(state
->triggers_ht
,
2201 lttng_condition_hash(condition
),
2205 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2206 if (!triggers_ht_node
) {
2207 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2210 cmd_reply
= LTTNG_OK
;
2213 /* Remove trigger from channel_triggers_ht. */
2214 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2215 channel_triggers_ht_node
) {
2216 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2218 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2219 &trigger_list
->list
, node
) {
2220 const struct lttng_condition
*current_condition
=
2221 lttng_trigger_get_const_condition(
2222 trigger_element
->trigger
);
2224 assert(current_condition
);
2225 if (!lttng_condition_is_equal(condition
,
2226 current_condition
)) {
2230 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2231 cds_list_del(&trigger_element
->node
);
2232 /* A trigger can only appear once per channel */
2238 * Remove and release the client list from
2239 * notification_trigger_clients_ht.
2241 client_list
= get_client_list_from_condition(state
, condition
);
2242 assert(client_list
);
2244 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2245 &client_list
->list
, node
) {
2246 free(client_list_element
);
2248 cds_lfht_del(state
->notification_trigger_clients_ht
,
2249 &client_list
->notification_trigger_ht_node
);
2250 call_rcu(&client_list
->rcu_node
, free_notification_client_list_rcu
);
2252 /* Remove trigger from triggers_ht. */
2253 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2254 struct lttng_trigger_ht_element
, node
);
2255 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2257 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
2258 lttng_condition_destroy(condition
);
2259 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
2260 lttng_action_destroy(action
);
2261 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2262 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2266 *_cmd_reply
= cmd_reply
;
2271 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2272 int handle_notification_thread_command(
2273 struct notification_thread_handle
*handle
,
2274 struct notification_thread_state
*state
)
2278 struct notification_thread_command
*cmd
;
2280 /* Read the event pipe to put it back into a quiescent state. */
2281 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2283 if (ret
!= sizeof(counter
)) {
2287 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2288 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2289 struct notification_thread_command
, cmd_list_node
);
2290 switch (cmd
->type
) {
2291 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2292 DBG("[notification-thread] Received register trigger command");
2293 ret
= handle_notification_thread_command_register_trigger(
2294 state
, cmd
->parameters
.trigger
,
2297 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2298 DBG("[notification-thread] Received unregister trigger command");
2299 ret
= handle_notification_thread_command_unregister_trigger(
2300 state
, cmd
->parameters
.trigger
,
2303 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2304 DBG("[notification-thread] Received add channel command");
2305 ret
= handle_notification_thread_command_add_channel(
2307 cmd
->parameters
.add_channel
.session
.name
,
2308 cmd
->parameters
.add_channel
.session
.uid
,
2309 cmd
->parameters
.add_channel
.session
.gid
,
2310 cmd
->parameters
.add_channel
.channel
.name
,
2311 cmd
->parameters
.add_channel
.channel
.domain
,
2312 cmd
->parameters
.add_channel
.channel
.key
,
2313 cmd
->parameters
.add_channel
.channel
.capacity
,
2316 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2317 DBG("[notification-thread] Received remove channel command");
2318 ret
= handle_notification_thread_command_remove_channel(
2319 state
, cmd
->parameters
.remove_channel
.key
,
2320 cmd
->parameters
.remove_channel
.domain
,
2323 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2324 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2325 DBG("[notification-thread] Received session rotation %s command",
2326 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2327 "ongoing" : "completed");
2328 ret
= handle_notification_thread_command_session_rotation(
2331 cmd
->parameters
.session_rotation
.session_name
,
2332 cmd
->parameters
.session_rotation
.uid
,
2333 cmd
->parameters
.session_rotation
.gid
,
2334 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2335 cmd
->parameters
.session_rotation
.location
,
2338 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2339 DBG("[notification-thread] Received quit command");
2340 cmd
->reply_code
= LTTNG_OK
;
2344 ERR("[notification-thread] Unknown internal command received");
2352 cds_list_del(&cmd
->cmd_list_node
);
2353 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2354 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2357 /* Wake-up and return a fatal error to the calling thread. */
2358 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2359 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2360 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2362 /* Indicate a fatal error to the caller. */
2367 unsigned long hash_client_socket(int socket
)
2369 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
2373 int socket_set_non_blocking(int socket
)
2377 /* Set the pipe as non-blocking. */
2378 ret
= fcntl(socket
, F_GETFL
, 0);
2380 PERROR("fcntl get socket flags");
2385 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2387 PERROR("fcntl set O_NONBLOCK socket flag");
2390 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2396 int client_reset_inbound_state(struct notification_client
*client
)
2400 ret
= lttng_dynamic_buffer_set_size(
2401 &client
->communication
.inbound
.buffer
, 0);
2404 client
->communication
.inbound
.bytes_to_receive
=
2405 sizeof(struct lttng_notification_channel_message
);
2406 client
->communication
.inbound
.msg_type
=
2407 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2408 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2409 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2410 ret
= lttng_dynamic_buffer_set_size(
2411 &client
->communication
.inbound
.buffer
,
2412 client
->communication
.inbound
.bytes_to_receive
);
2416 int handle_notification_thread_client_connect(
2417 struct notification_thread_state
*state
)
2420 struct notification_client
*client
;
2422 DBG("[notification-thread] Handling new notification channel client connection");
2424 client
= zmalloc(sizeof(*client
));
2430 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2431 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2432 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2433 client
->communication
.inbound
.expect_creds
= true;
2434 ret
= client_reset_inbound_state(client
);
2436 ERR("[notification-thread] Failed to reset client communication's inbound state");
2441 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2443 ERR("[notification-thread] Failed to accept new notification channel client connection");
2448 client
->socket
= ret
;
2450 ret
= socket_set_non_blocking(client
->socket
);
2452 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2456 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2458 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2463 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2464 LPOLLIN
| LPOLLERR
|
2465 LPOLLHUP
| LPOLLRDHUP
);
2467 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2471 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2475 cds_lfht_add(state
->client_socket_ht
,
2476 hash_client_socket(client
->socket
),
2477 &client
->client_socket_ht_node
);
2482 notification_client_destroy(client
, state
);
2486 int handle_notification_thread_client_disconnect(
2488 struct notification_thread_state
*state
)
2491 struct notification_client
*client
;
2494 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2496 client
= get_client_from_socket(client_socket
, state
);
2498 /* Internal state corruption, fatal error. */
2499 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2505 ret
= lttng_poll_del(&state
->events
, client_socket
);
2507 ERR("[notification-thread] Failed to remove client socket from poll set");
2509 cds_lfht_del(state
->client_socket_ht
,
2510 &client
->client_socket_ht_node
);
2511 notification_client_destroy(client
, state
);
2517 int handle_notification_thread_client_disconnect_all(
2518 struct notification_thread_state
*state
)
2520 struct cds_lfht_iter iter
;
2521 struct notification_client
*client
;
2522 bool error_encoutered
= false;
2525 DBG("[notification-thread] Closing all client connections");
2526 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2527 client_socket_ht_node
) {
2530 ret
= handle_notification_thread_client_disconnect(
2531 client
->socket
, state
);
2533 error_encoutered
= true;
2537 return error_encoutered
? 1 : 0;
2540 int handle_notification_thread_trigger_unregister_all(
2541 struct notification_thread_state
*state
)
2543 bool error_occurred
= false;
2544 struct cds_lfht_iter iter
;
2545 struct lttng_trigger_ht_element
*trigger_ht_element
;
2548 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2550 int ret
= handle_notification_thread_command_unregister_trigger(
2551 state
, trigger_ht_element
->trigger
, NULL
);
2553 error_occurred
= true;
2557 return error_occurred
? -1 : 0;
2561 int client_flush_outgoing_queue(struct notification_client
*client
,
2562 struct notification_thread_state
*state
)
2565 size_t to_send_count
;
2567 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2568 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2569 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2572 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2573 client
->communication
.outbound
.buffer
.data
,
2575 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
2576 (ret
> 0 && ret
< to_send_count
)) {
2577 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2579 to_send_count
-= max(ret
, 0);
2581 memcpy(client
->communication
.outbound
.buffer
.data
,
2582 client
->communication
.outbound
.buffer
.data
+
2583 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2585 ret
= lttng_dynamic_buffer_set_size(
2586 &client
->communication
.outbound
.buffer
,
2593 * We want to be notified whenever there is buffer space
2594 * available to send the rest of the payload.
2596 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2597 CLIENT_POLL_MASK_IN_OUT
);
2601 } else if (ret
< 0) {
2602 /* Generic error, disconnect the client. */
2603 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2605 ret
= handle_notification_thread_client_disconnect(
2606 client
->socket
, state
);
2611 /* No error and flushed the queue completely. */
2612 ret
= lttng_dynamic_buffer_set_size(
2613 &client
->communication
.outbound
.buffer
, 0);
2617 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2618 CLIENT_POLL_MASK_IN
);
2623 client
->communication
.outbound
.queued_command_reply
= false;
2624 client
->communication
.outbound
.dropped_notification
= false;
2633 int client_send_command_reply(struct notification_client
*client
,
2634 struct notification_thread_state
*state
,
2635 enum lttng_notification_channel_status status
)
2638 struct lttng_notification_channel_command_reply reply
= {
2639 .status
= (int8_t) status
,
2641 struct lttng_notification_channel_message msg
= {
2642 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2643 .size
= sizeof(reply
),
2645 char buffer
[sizeof(msg
) + sizeof(reply
)];
2647 if (client
->communication
.outbound
.queued_command_reply
) {
2648 /* Protocol error. */
2652 memcpy(buffer
, &msg
, sizeof(msg
));
2653 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2654 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2656 /* Enqueue buffer to outgoing queue and flush it. */
2657 ret
= lttng_dynamic_buffer_append(
2658 &client
->communication
.outbound
.buffer
,
2659 buffer
, sizeof(buffer
));
2664 ret
= client_flush_outgoing_queue(client
, state
);
2669 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2670 /* Queue could not be emptied. */
2671 client
->communication
.outbound
.queued_command_reply
= true;
2680 int client_dispatch_message(struct notification_client
*client
,
2681 struct notification_thread_state
*state
)
2685 if (client
->communication
.inbound
.msg_type
!=
2686 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2687 client
->communication
.inbound
.msg_type
!=
2688 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2689 !client
->validated
) {
2690 WARN("[notification-thread] client attempted a command before handshake");
2695 switch (client
->communication
.inbound
.msg_type
) {
2696 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2699 * Receiving message header. The function will be called again
2700 * once the rest of the message as been received and can be
2703 const struct lttng_notification_channel_message
*msg
;
2705 assert(sizeof(*msg
) ==
2706 client
->communication
.inbound
.buffer
.size
);
2707 msg
= (const struct lttng_notification_channel_message
*)
2708 client
->communication
.inbound
.buffer
.data
;
2710 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2711 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2716 switch (msg
->type
) {
2717 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2718 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2719 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2723 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2727 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2728 client
->communication
.inbound
.msg_type
=
2729 (enum lttng_notification_channel_message_type
) msg
->type
;
2730 ret
= lttng_dynamic_buffer_set_size(
2731 &client
->communication
.inbound
.buffer
, msg
->size
);
2737 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2739 struct lttng_notification_channel_command_handshake
*handshake_client
;
2740 struct lttng_notification_channel_command_handshake handshake_reply
= {
2741 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2742 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2744 struct lttng_notification_channel_message msg_header
= {
2745 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2746 .size
= sizeof(handshake_reply
),
2748 enum lttng_notification_channel_status status
=
2749 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2750 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2752 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2753 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2754 sizeof(handshake_reply
));
2757 (struct lttng_notification_channel_command_handshake
*)
2758 client
->communication
.inbound
.buffer
.data
;
2759 client
->major
= handshake_client
->major
;
2760 client
->minor
= handshake_client
->minor
;
2761 if (!client
->communication
.inbound
.creds_received
) {
2762 ERR("[notification-thread] No credentials received from client");
2767 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2768 &client
->communication
.inbound
.creds
);
2769 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2770 &client
->communication
.inbound
.creds
);
2771 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2772 client
->uid
, client
->gid
, (int) client
->major
,
2773 (int) client
->minor
);
2775 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2776 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2779 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2780 send_buffer
, sizeof(send_buffer
));
2782 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2786 ret
= client_flush_outgoing_queue(client
, state
);
2791 ret
= client_send_command_reply(client
, state
, status
);
2793 ERR("[notification-thread] Failed to send reply to notification channel client");
2797 /* Set reception state to receive the next message header. */
2798 ret
= client_reset_inbound_state(client
);
2800 ERR("[notification-thread] Failed to reset client communication's inbound state");
2803 client
->validated
= true;
2806 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2807 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2809 struct lttng_condition
*condition
;
2810 enum lttng_notification_channel_status status
=
2811 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2812 const struct lttng_buffer_view condition_view
=
2813 lttng_buffer_view_from_dynamic_buffer(
2814 &client
->communication
.inbound
.buffer
,
2816 size_t expected_condition_size
=
2817 client
->communication
.inbound
.buffer
.size
;
2819 ret
= lttng_condition_create_from_buffer(&condition_view
,
2821 if (ret
!= expected_condition_size
) {
2822 ERR("[notification-thread] Malformed condition received from client");
2826 if (client
->communication
.inbound
.msg_type
==
2827 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2828 ret
= notification_thread_client_subscribe(client
,
2829 condition
, state
, &status
);
2831 ret
= notification_thread_client_unsubscribe(client
,
2832 condition
, state
, &status
);
2838 ret
= client_send_command_reply(client
, state
, status
);
2840 ERR("[notification-thread] Failed to send reply to notification channel client");
2844 /* Set reception state to receive the next message header. */
2845 ret
= client_reset_inbound_state(client
);
2847 ERR("[notification-thread] Failed to reset client communication's inbound state");
2859 /* Incoming data from client. */
2860 int handle_notification_thread_client_in(
2861 struct notification_thread_state
*state
, int socket
)
2864 struct notification_client
*client
;
2868 client
= get_client_from_socket(socket
, state
);
2870 /* Internal error, abort. */
2875 offset
= client
->communication
.inbound
.buffer
.size
-
2876 client
->communication
.inbound
.bytes_to_receive
;
2877 if (client
->communication
.inbound
.expect_creds
) {
2878 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2879 client
->communication
.inbound
.buffer
.data
+ offset
,
2880 client
->communication
.inbound
.bytes_to_receive
,
2881 &client
->communication
.inbound
.creds
);
2883 client
->communication
.inbound
.expect_creds
= false;
2884 client
->communication
.inbound
.creds_received
= true;
2887 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2888 client
->communication
.inbound
.buffer
.data
+ offset
,
2889 client
->communication
.inbound
.bytes_to_receive
);
2892 goto error_disconnect_client
;
2895 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2896 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2897 ret
= client_dispatch_message(client
, state
);
2900 * Only returns an error if this client must be
2903 goto error_disconnect_client
;
2910 error_disconnect_client
:
2911 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2915 /* Client ready to receive outgoing data. */
2916 int handle_notification_thread_client_out(
2917 struct notification_thread_state
*state
, int socket
)
2920 struct notification_client
*client
;
2922 client
= get_client_from_socket(socket
, state
);
2924 /* Internal error, abort. */
2929 ret
= client_flush_outgoing_queue(client
, state
);
2938 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2939 const struct channel_state_sample
*sample
,
2940 uint64_t buffer_capacity
)
2942 bool result
= false;
2944 enum lttng_condition_type condition_type
;
2945 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2946 condition
, struct lttng_condition_buffer_usage
,
2949 if (use_condition
->threshold_bytes
.set
) {
2950 threshold
= use_condition
->threshold_bytes
.value
;
2953 * Threshold was expressed as a ratio.
2955 * TODO the threshold (in bytes) of conditions expressed
2956 * as a ratio of total buffer size could be cached to
2957 * forego this double-multiplication or it could be performed
2958 * as fixed-point math.
2960 * Note that caching should accomodate the case where the
2961 * condition applies to multiple channels (i.e. don't assume
2962 * that all channels matching my_chann* have the same size...)
2964 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2965 (double) buffer_capacity
);
2968 condition_type
= lttng_condition_get_type(condition
);
2969 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2970 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2971 threshold
, sample
->highest_usage
);
2974 * The low condition should only be triggered once _all_ of the
2975 * streams in a channel have gone below the "low" threshold.
2977 if (sample
->highest_usage
<= threshold
) {
2981 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2982 threshold
, sample
->highest_usage
);
2985 * For high buffer usage scenarios, we want to trigger whenever
2986 * _any_ of the streams has reached the "high" threshold.
2988 if (sample
->highest_usage
>= threshold
) {
2997 bool evaluate_session_consumed_size_condition(
2998 const struct lttng_condition
*condition
,
2999 uint64_t session_consumed_size
)
3002 const struct lttng_condition_session_consumed_size
*size_condition
=
3003 container_of(condition
,
3004 struct lttng_condition_session_consumed_size
,
3007 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3008 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3009 threshold
, session_consumed_size
);
3010 return session_consumed_size
>= threshold
;
3014 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3015 struct lttng_evaluation
**evaluation
,
3016 const struct notification_thread_state
*state
,
3017 const struct channel_state_sample
*previous_sample
,
3018 const struct channel_state_sample
*latest_sample
,
3019 uint64_t previous_session_consumed_total
,
3020 uint64_t latest_session_consumed_total
,
3021 struct channel_info
*channel_info
)
3024 enum lttng_condition_type condition_type
;
3025 const bool previous_sample_available
= !!previous_sample
;
3026 bool previous_sample_result
= false;
3027 bool latest_sample_result
;
3029 condition_type
= lttng_condition_get_type(condition
);
3031 switch (condition_type
) {
3032 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3033 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3034 if (caa_likely(previous_sample_available
)) {
3035 previous_sample_result
=
3036 evaluate_buffer_usage_condition(condition
,
3037 previous_sample
, channel_info
->capacity
);
3039 latest_sample_result
= evaluate_buffer_usage_condition(
3040 condition
, latest_sample
,
3041 channel_info
->capacity
);
3043 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3044 if (caa_likely(previous_sample_available
)) {
3045 previous_sample_result
=
3046 evaluate_session_consumed_size_condition(
3048 previous_session_consumed_total
);
3050 latest_sample_result
=
3051 evaluate_session_consumed_size_condition(
3053 latest_session_consumed_total
);
3056 /* Unknown condition type; internal error. */
3060 if (!latest_sample_result
||
3061 (previous_sample_result
== latest_sample_result
)) {
3063 * Only trigger on a condition evaluation transition.
3065 * NOTE: This edge-triggered logic may not be appropriate for
3066 * future condition types.
3071 if (!evaluation
|| !latest_sample_result
) {
3075 switch (condition_type
) {
3076 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3077 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3078 *evaluation
= lttng_evaluation_buffer_usage_create(
3080 latest_sample
->highest_usage
,
3081 channel_info
->capacity
);
3083 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3084 *evaluation
= lttng_evaluation_session_consumed_size_create(
3085 latest_session_consumed_total
);
3100 int client_enqueue_dropped_notification(struct notification_client
*client
,
3101 struct notification_thread_state
*state
)
3104 struct lttng_notification_channel_message msg
= {
3105 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3109 ret
= lttng_dynamic_buffer_append(
3110 &client
->communication
.outbound
.buffer
, &msg
,
3116 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3117 const struct lttng_evaluation
*evaluation
,
3118 struct notification_client_list
* client_list
,
3119 struct notification_thread_state
*state
,
3120 uid_t channel_uid
, gid_t channel_gid
)
3123 struct lttng_dynamic_buffer msg_buffer
;
3124 struct notification_client_list_element
*client_list_element
, *tmp
;
3125 const struct lttng_notification notification
= {
3126 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
3127 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3129 struct lttng_notification_channel_message msg_header
= {
3130 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3133 lttng_dynamic_buffer_init(&msg_buffer
);
3135 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
3136 sizeof(msg_header
));
3141 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
3143 ERR("[notification-thread] Failed to serialize notification");
3148 /* Update payload size. */
3149 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
3150 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
3152 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3153 &client_list
->list
, node
) {
3154 struct notification_client
*client
=
3155 client_list_element
->client
;
3157 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
3159 /* Client is not allowed to monitor this channel. */
3160 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
3164 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3165 client
->socket
, msg_buffer
.size
);
3166 if (client
->communication
.outbound
.buffer
.size
) {
3168 * Outgoing data is already buffered for this client;
3169 * drop the notification and enqueue a "dropped
3170 * notification" message if this is the first dropped
3171 * notification since the socket spilled-over to the
3174 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
3176 if (!client
->communication
.outbound
.dropped_notification
) {
3177 client
->communication
.outbound
.dropped_notification
= true;
3178 ret
= client_enqueue_dropped_notification(
3187 ret
= lttng_dynamic_buffer_append_buffer(
3188 &client
->communication
.outbound
.buffer
,
3194 ret
= client_flush_outgoing_queue(client
, state
);
3201 lttng_dynamic_buffer_reset(&msg_buffer
);
3205 int handle_notification_thread_channel_sample(
3206 struct notification_thread_state
*state
, int pipe
,
3207 enum lttng_domain_type domain
)
3210 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3211 struct channel_info
*channel_info
;
3212 struct cds_lfht_node
*node
;
3213 struct cds_lfht_iter iter
;
3214 struct lttng_channel_trigger_list
*trigger_list
;
3215 struct lttng_trigger_list_element
*trigger_list_element
;
3216 bool previous_sample_available
= false;
3217 struct channel_state_sample previous_sample
, latest_sample
;
3218 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3221 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3222 * ensuring that read/write of sampling messages are atomic.
3224 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3225 if (ret
!= sizeof(sample_msg
)) {
3226 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3233 latest_sample
.key
.key
= sample_msg
.key
;
3234 latest_sample
.key
.domain
= domain
;
3235 latest_sample
.highest_usage
= sample_msg
.highest
;
3236 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3237 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3241 /* Retrieve the channel's informations */
3242 cds_lfht_lookup(state
->channels_ht
,
3243 hash_channel_key(&latest_sample
.key
),
3247 node
= cds_lfht_iter_get_node(&iter
);
3248 if (caa_unlikely(!node
)) {
3250 * Not an error since the consumer can push a sample to the pipe
3251 * and the rest of the session daemon could notify us of the
3252 * channel's destruction before we get a chance to process that
3255 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3256 latest_sample
.key
.key
,
3257 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3261 channel_info
= caa_container_of(node
, struct channel_info
,
3263 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3265 latest_sample
.key
.key
,
3266 channel_info
->session_info
->name
,
3267 latest_sample
.highest_usage
,
3268 latest_sample
.lowest_usage
,
3269 latest_sample
.channel_total_consumed
);
3271 previous_session_consumed_total
=
3272 channel_info
->session_info
->consumed_data_size
;
3274 /* Retrieve the channel's last sample, if it exists, and update it. */
3275 cds_lfht_lookup(state
->channel_state_ht
,
3276 hash_channel_key(&latest_sample
.key
),
3277 match_channel_state_sample
,
3280 node
= cds_lfht_iter_get_node(&iter
);
3281 if (caa_likely(node
)) {
3282 struct channel_state_sample
*stored_sample
;
3284 /* Update the sample stored. */
3285 stored_sample
= caa_container_of(node
,
3286 struct channel_state_sample
,
3287 channel_state_ht_node
);
3289 memcpy(&previous_sample
, stored_sample
,
3290 sizeof(previous_sample
));
3291 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3292 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3293 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3294 previous_sample_available
= true;
3296 latest_session_consumed_total
=
3297 previous_session_consumed_total
+
3298 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3301 * This is the channel's first sample, allocate space for and
3302 * store the new sample.
3304 struct channel_state_sample
*stored_sample
;
3306 stored_sample
= zmalloc(sizeof(*stored_sample
));
3307 if (!stored_sample
) {
3312 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3313 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3314 cds_lfht_add(state
->channel_state_ht
,
3315 hash_channel_key(&stored_sample
->key
),
3316 &stored_sample
->channel_state_ht_node
);
3318 latest_session_consumed_total
=
3319 previous_session_consumed_total
+
3320 latest_sample
.channel_total_consumed
;
3323 channel_info
->session_info
->consumed_data_size
=
3324 latest_session_consumed_total
;
3326 /* Find triggers associated with this channel. */
3327 cds_lfht_lookup(state
->channel_triggers_ht
,
3328 hash_channel_key(&latest_sample
.key
),
3329 match_channel_trigger_list
,
3332 node
= cds_lfht_iter_get_node(&iter
);
3333 if (caa_likely(!node
)) {
3337 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3338 channel_triggers_ht_node
);
3339 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3341 const struct lttng_condition
*condition
;
3342 const struct lttng_action
*action
;
3343 const struct lttng_trigger
*trigger
;
3344 struct notification_client_list
*client_list
;
3345 struct lttng_evaluation
*evaluation
= NULL
;
3347 trigger
= trigger_list_element
->trigger
;
3348 condition
= lttng_trigger_get_const_condition(trigger
);
3350 action
= lttng_trigger_get_const_action(trigger
);
3352 /* Notify actions are the only type currently supported. */
3353 assert(lttng_action_get_type_const(action
) ==
3354 LTTNG_ACTION_TYPE_NOTIFY
);
3357 * Check if any client is subscribed to the result of this
3360 client_list
= get_client_list_from_condition(state
, condition
);
3361 assert(client_list
);
3362 if (cds_list_empty(&client_list
->list
)) {
3364 * No clients interested in the evaluation's result,
3370 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3371 previous_sample_available
? &previous_sample
: NULL
,
3373 previous_session_consumed_total
,
3374 latest_session_consumed_total
,
3376 if (caa_unlikely(ret
)) {
3380 if (caa_likely(!evaluation
)) {
3384 /* Dispatch evaluation result to all clients. */
3385 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3386 evaluation
, client_list
, state
,
3387 channel_info
->session_info
->uid
,
3388 channel_info
->session_info
->gid
);
3389 lttng_evaluation_destroy(evaluation
);
3390 if (caa_unlikely(ret
)) {