2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <urcu/rculfhash.h>
12 #include <common/defaults.h>
13 #include <common/error.h>
14 #include <common/futex.h>
15 #include <common/unix.h>
16 #include <common/dynamic-buffer.h>
17 #include <common/hashtable/utils.h>
18 #include <common/sessiond-comm/sessiond-comm.h>
19 #include <common/macros.h>
20 #include <lttng/condition/condition.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/notification/notification-internal.h>
23 #include <lttng/condition/condition-internal.h>
24 #include <lttng/condition/buffer-usage-internal.h>
25 #include <lttng/condition/session-consumed-size-internal.h>
26 #include <lttng/condition/session-rotation-internal.h>
27 #include <lttng/notification/channel-internal.h>
35 #include "notification-thread.h"
36 #include "notification-thread-events.h"
37 #include "notification-thread-commands.h"
38 #include "lttng-sessiond.h"
41 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
42 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
44 enum lttng_object_type
{
45 LTTNG_OBJECT_TYPE_UNKNOWN
,
46 LTTNG_OBJECT_TYPE_NONE
,
47 LTTNG_OBJECT_TYPE_CHANNEL
,
48 LTTNG_OBJECT_TYPE_SESSION
,
51 struct lttng_trigger_list_element
{
52 /* No ownership of the trigger object is assumed. */
53 const struct lttng_trigger
*trigger
;
54 struct cds_list_head node
;
57 struct lttng_channel_trigger_list
{
58 struct channel_key channel_key
;
59 /* List of struct lttng_trigger_list_element. */
60 struct cds_list_head list
;
61 /* Node in the channel_triggers_ht */
62 struct cds_lfht_node channel_triggers_ht_node
;
63 /* call_rcu delayed reclaim. */
64 struct rcu_head rcu_node
;
68 * List of triggers applying to a given session.
71 * - lttng_session_trigger_list_create()
72 * - lttng_session_trigger_list_build()
73 * - lttng_session_trigger_list_destroy()
74 * - lttng_session_trigger_list_add()
76 struct lttng_session_trigger_list
{
78 * Not owned by this; points to the session_info structure's
81 const char *session_name
;
82 /* List of struct lttng_trigger_list_element. */
83 struct cds_list_head list
;
84 /* Node in the session_triggers_ht */
85 struct cds_lfht_node session_triggers_ht_node
;
87 * Weak reference to the notification system's session triggers
90 * The session trigger list structure structure is owned by
91 * the session's session_info.
93 * The session_info is kept alive the the channel_infos holding a
94 * reference to it (reference counting). When those channels are
95 * destroyed (at runtime or on teardown), the reference they hold
96 * to the session_info are released. On destruction of session_info,
97 * session_info_destroy() will remove the list of triggers applying
98 * to this session from the notification system's state.
100 * This implies that the session_triggers_ht must be destroyed
101 * after the channels.
103 struct cds_lfht
*session_triggers_ht
;
104 /* Used for delayed RCU reclaim. */
105 struct rcu_head rcu_node
;
108 struct lttng_trigger_ht_element
{
109 struct lttng_trigger
*trigger
;
110 struct cds_lfht_node node
;
111 /* call_rcu delayed reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_condition_list_element
{
116 struct lttng_condition
*condition
;
117 struct cds_list_head node
;
120 struct notification_client_list_element
{
121 struct notification_client
*client
;
122 struct cds_list_head node
;
125 struct notification_client_list
{
126 const struct lttng_trigger
*trigger
;
127 struct cds_list_head list
;
128 struct cds_lfht_node notification_trigger_ht_node
;
129 /* call_rcu delayed reclaim. */
130 struct rcu_head rcu_node
;
133 struct notification_client
{
135 /* Client protocol version. */
136 uint8_t major
, minor
;
140 * Indicates if the credentials and versions of the client have been
145 * Conditions to which the client's notification channel is subscribed.
146 * List of struct lttng_condition_list_node. The condition member is
147 * owned by the client.
149 struct cds_list_head condition_list
;
150 struct cds_lfht_node client_socket_ht_node
;
154 * During the reception of a message, the reception
155 * buffers' "size" is set to contain the current
156 * message's complete payload.
158 struct lttng_dynamic_buffer buffer
;
159 /* Bytes left to receive for the current message. */
160 size_t bytes_to_receive
;
161 /* Type of the message being received. */
162 enum lttng_notification_channel_message_type msg_type
;
164 * Indicates whether or not credentials are expected
169 * Indicates whether or not credentials were received
173 /* Only used during credentials reception. */
174 lttng_sock_cred creds
;
178 * Indicates whether or not a notification addressed to
179 * this client was dropped because a command reply was
182 * A notification is dropped whenever the buffer is not
185 bool dropped_notification
;
187 * Indicates whether or not a command reply is already
188 * buffered. In this case, it means that the client is
189 * not consuming command replies before emitting a new
190 * one. This could be caused by a protocol error or a
191 * misbehaving/malicious client.
193 bool queued_command_reply
;
194 struct lttng_dynamic_buffer buffer
;
197 /* call_rcu delayed reclaim. */
198 struct rcu_head rcu_node
;
201 struct channel_state_sample
{
202 struct channel_key key
;
203 struct cds_lfht_node channel_state_ht_node
;
204 uint64_t highest_usage
;
205 uint64_t lowest_usage
;
206 uint64_t channel_total_consumed
;
207 /* call_rcu delayed reclaim. */
208 struct rcu_head rcu_node
;
211 static unsigned long hash_channel_key(struct channel_key
*key
);
212 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
213 struct lttng_evaluation
**evaluation
,
214 const struct notification_thread_state
*state
,
215 const struct channel_state_sample
*previous_sample
,
216 const struct channel_state_sample
*latest_sample
,
217 uint64_t previous_session_consumed_total
,
218 uint64_t latest_session_consumed_total
,
219 struct channel_info
*channel_info
);
221 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
222 const struct lttng_evaluation
*evaluation
,
223 struct notification_client_list
*client_list
,
224 struct notification_thread_state
*state
,
225 uid_t channel_uid
, gid_t channel_gid
);
228 /* session_info API */
230 void session_info_destroy(void *_data
);
232 void session_info_get(struct session_info
*session_info
);
234 void session_info_put(struct session_info
*session_info
);
236 struct session_info
*session_info_create(const char *name
,
237 uid_t uid
, gid_t gid
,
238 struct lttng_session_trigger_list
*trigger_list
,
239 struct cds_lfht
*sessions_ht
);
241 void session_info_add_channel(struct session_info
*session_info
,
242 struct channel_info
*channel_info
);
244 void session_info_remove_channel(struct session_info
*session_info
,
245 struct channel_info
*channel_info
);
247 /* lttng_session_trigger_list API */
249 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
250 const char *session_name
,
251 struct cds_lfht
*session_triggers_ht
);
253 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
254 const struct notification_thread_state
*state
,
255 const char *session_name
);
257 void lttng_session_trigger_list_destroy(
258 struct lttng_session_trigger_list
*list
);
260 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
261 const struct lttng_trigger
*trigger
);
265 int match_client(struct cds_lfht_node
*node
, const void *key
)
267 /* This double-cast is intended to supress pointer-to-cast warning. */
268 int socket
= (int) (intptr_t) key
;
269 struct notification_client
*client
;
271 client
= caa_container_of(node
, struct notification_client
,
272 client_socket_ht_node
);
274 return !!(client
->socket
== socket
);
278 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
280 struct channel_key
*channel_key
= (struct channel_key
*) key
;
281 struct lttng_channel_trigger_list
*trigger_list
;
283 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
284 channel_triggers_ht_node
);
286 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
287 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
291 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
293 const char *session_name
= (const char *) key
;
294 struct lttng_session_trigger_list
*trigger_list
;
296 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
297 session_triggers_ht_node
);
299 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
303 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
305 struct channel_key
*channel_key
= (struct channel_key
*) key
;
306 struct channel_state_sample
*sample
;
308 sample
= caa_container_of(node
, struct channel_state_sample
,
309 channel_state_ht_node
);
311 return !!((channel_key
->key
== sample
->key
.key
) &&
312 (channel_key
->domain
== sample
->key
.domain
));
316 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
318 struct channel_key
*channel_key
= (struct channel_key
*) key
;
319 struct channel_info
*channel_info
;
321 channel_info
= caa_container_of(node
, struct channel_info
,
324 return !!((channel_key
->key
== channel_info
->key
.key
) &&
325 (channel_key
->domain
== channel_info
->key
.domain
));
329 int match_condition(struct cds_lfht_node
*node
, const void *key
)
331 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
332 struct lttng_trigger_ht_element
*trigger
;
333 struct lttng_condition
*condition
;
335 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
337 condition
= lttng_trigger_get_condition(trigger
->trigger
);
340 return !!lttng_condition_is_equal(condition_key
, condition
);
344 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
346 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
347 struct notification_client_list
*client_list
;
348 const struct lttng_condition
*condition
;
350 assert(condition_key
);
352 client_list
= caa_container_of(node
, struct notification_client_list
,
353 notification_trigger_ht_node
);
354 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
356 return !!lttng_condition_is_equal(condition_key
, condition
);
360 int match_session(struct cds_lfht_node
*node
, const void *key
)
362 const char *name
= key
;
363 struct session_info
*session_info
= caa_container_of(
364 node
, struct session_info
, sessions_ht_node
);
366 return !strcmp(session_info
->name
, name
);
370 unsigned long lttng_condition_buffer_usage_hash(
371 const struct lttng_condition
*_condition
)
374 unsigned long condition_type
;
375 struct lttng_condition_buffer_usage
*condition
;
377 condition
= container_of(_condition
,
378 struct lttng_condition_buffer_usage
, parent
);
380 condition_type
= (unsigned long) condition
->parent
.type
;
381 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
382 if (condition
->session_name
) {
383 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
385 if (condition
->channel_name
) {
386 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
388 if (condition
->domain
.set
) {
389 hash
^= hash_key_ulong(
390 (void *) condition
->domain
.type
,
393 if (condition
->threshold_ratio
.set
) {
396 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
397 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
398 } else if (condition
->threshold_bytes
.set
) {
401 val
= condition
->threshold_bytes
.value
;
402 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
408 unsigned long lttng_condition_session_consumed_size_hash(
409 const struct lttng_condition
*_condition
)
412 unsigned long condition_type
=
413 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
414 struct lttng_condition_session_consumed_size
*condition
;
417 condition
= container_of(_condition
,
418 struct lttng_condition_session_consumed_size
, parent
);
420 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
421 if (condition
->session_name
) {
422 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
424 val
= condition
->consumed_threshold_bytes
.value
;
425 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
430 unsigned long lttng_condition_session_rotation_hash(
431 const struct lttng_condition
*_condition
)
433 unsigned long hash
, condition_type
;
434 struct lttng_condition_session_rotation
*condition
;
436 condition
= container_of(_condition
,
437 struct lttng_condition_session_rotation
, parent
);
438 condition_type
= (unsigned long) condition
->parent
.type
;
439 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
440 assert(condition
->session_name
);
441 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
446 * The lttng_condition hashing code is kept in this file (rather than
447 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
448 * don't want to link in liblttng-ctl.
451 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
453 switch (condition
->type
) {
454 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
455 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
456 return lttng_condition_buffer_usage_hash(condition
);
457 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
458 return lttng_condition_session_consumed_size_hash(condition
);
459 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
460 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
461 return lttng_condition_session_rotation_hash(condition
);
463 ERR("[notification-thread] Unexpected condition type caught");
469 unsigned long hash_channel_key(struct channel_key
*key
)
471 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
472 unsigned long domain_hash
= hash_key_ulong(
473 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
475 return key_hash
^ domain_hash
;
479 * Get the type of object to which a given condition applies. Bindings let
480 * the notification system evaluate a trigger's condition when a given
481 * object's state is updated.
483 * For instance, a condition bound to a channel will be evaluated everytime
484 * the channel's state is changed by a channel monitoring sample.
487 enum lttng_object_type
get_condition_binding_object(
488 const struct lttng_condition
*condition
)
490 switch (lttng_condition_get_type(condition
)) {
491 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
492 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
493 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
494 return LTTNG_OBJECT_TYPE_CHANNEL
;
495 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
496 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
497 return LTTNG_OBJECT_TYPE_SESSION
;
499 return LTTNG_OBJECT_TYPE_UNKNOWN
;
504 void free_channel_info_rcu(struct rcu_head
*node
)
506 free(caa_container_of(node
, struct channel_info
, rcu_node
));
510 void channel_info_destroy(struct channel_info
*channel_info
)
516 if (channel_info
->session_info
) {
517 session_info_remove_channel(channel_info
->session_info
,
519 session_info_put(channel_info
->session_info
);
521 if (channel_info
->name
) {
522 free(channel_info
->name
);
524 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
528 void free_session_info_rcu(struct rcu_head
*node
)
530 free(caa_container_of(node
, struct session_info
, rcu_node
));
533 /* Don't call directly, use the ref-counting mechanism. */
535 void session_info_destroy(void *_data
)
537 struct session_info
*session_info
= _data
;
540 assert(session_info
);
541 if (session_info
->channel_infos_ht
) {
542 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
544 ERR("[notification-thread] Failed to destroy channel information hash table");
547 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
550 cds_lfht_del(session_info
->sessions_ht
,
551 &session_info
->sessions_ht_node
);
553 free(session_info
->name
);
554 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
558 void session_info_get(struct session_info
*session_info
)
563 lttng_ref_get(&session_info
->ref
);
567 void session_info_put(struct session_info
*session_info
)
572 lttng_ref_put(&session_info
->ref
);
576 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
577 struct lttng_session_trigger_list
*trigger_list
,
578 struct cds_lfht
*sessions_ht
)
580 struct session_info
*session_info
;
584 session_info
= zmalloc(sizeof(*session_info
));
588 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
590 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
591 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
592 if (!session_info
->channel_infos_ht
) {
596 cds_lfht_node_init(&session_info
->sessions_ht_node
);
597 session_info
->name
= strdup(name
);
598 if (!session_info
->name
) {
601 session_info
->uid
= uid
;
602 session_info
->gid
= gid
;
603 session_info
->trigger_list
= trigger_list
;
604 session_info
->sessions_ht
= sessions_ht
;
608 session_info_put(session_info
);
613 void session_info_add_channel(struct session_info
*session_info
,
614 struct channel_info
*channel_info
)
617 cds_lfht_add(session_info
->channel_infos_ht
,
618 hash_channel_key(&channel_info
->key
),
619 &channel_info
->session_info_channels_ht_node
);
624 void session_info_remove_channel(struct session_info
*session_info
,
625 struct channel_info
*channel_info
)
628 cds_lfht_del(session_info
->channel_infos_ht
,
629 &channel_info
->session_info_channels_ht_node
);
634 struct channel_info
*channel_info_create(const char *channel_name
,
635 struct channel_key
*channel_key
, uint64_t channel_capacity
,
636 struct session_info
*session_info
)
638 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
644 cds_lfht_node_init(&channel_info
->channels_ht_node
);
645 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
646 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
647 channel_info
->capacity
= channel_capacity
;
649 channel_info
->name
= strdup(channel_name
);
650 if (!channel_info
->name
) {
655 * Set the references between session and channel infos:
656 * - channel_info holds a strong reference to session_info
657 * - session_info holds a weak reference to channel_info
659 session_info_get(session_info
);
660 session_info_add_channel(session_info
, channel_info
);
661 channel_info
->session_info
= session_info
;
665 channel_info_destroy(channel_info
);
669 /* RCU read lock must be held by the caller. */
671 struct notification_client_list
*get_client_list_from_condition(
672 struct notification_thread_state
*state
,
673 const struct lttng_condition
*condition
)
675 struct cds_lfht_node
*node
;
676 struct cds_lfht_iter iter
;
678 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
679 lttng_condition_hash(condition
),
680 match_client_list_condition
,
683 node
= cds_lfht_iter_get_node(&iter
);
685 return node
? caa_container_of(node
,
686 struct notification_client_list
,
687 notification_trigger_ht_node
) : NULL
;
690 /* This function must be called with the RCU read lock held. */
692 int evaluate_channel_condition_for_client(
693 const struct lttng_condition
*condition
,
694 struct notification_thread_state
*state
,
695 struct lttng_evaluation
**evaluation
,
696 uid_t
*session_uid
, gid_t
*session_gid
)
699 struct cds_lfht_iter iter
;
700 struct cds_lfht_node
*node
;
701 struct channel_info
*channel_info
= NULL
;
702 struct channel_key
*channel_key
= NULL
;
703 struct channel_state_sample
*last_sample
= NULL
;
704 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
706 /* Find the channel associated with the condition. */
707 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
708 channel_trigger_list
, channel_triggers_ht_node
) {
709 struct lttng_trigger_list_element
*element
;
711 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
712 const struct lttng_condition
*current_condition
=
713 lttng_trigger_get_const_condition(
716 assert(current_condition
);
717 if (!lttng_condition_is_equal(condition
,
718 current_condition
)) {
722 /* Found the trigger, save the channel key. */
723 channel_key
= &channel_trigger_list
->channel_key
;
727 /* The channel key was found stop iteration. */
733 /* No channel found; normal exit. */
734 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
739 /* Fetch channel info for the matching channel. */
740 cds_lfht_lookup(state
->channels_ht
,
741 hash_channel_key(channel_key
),
745 node
= cds_lfht_iter_get_node(&iter
);
747 channel_info
= caa_container_of(node
, struct channel_info
,
750 /* Retrieve the channel's last sample, if it exists. */
751 cds_lfht_lookup(state
->channel_state_ht
,
752 hash_channel_key(channel_key
),
753 match_channel_state_sample
,
756 node
= cds_lfht_iter_get_node(&iter
);
758 last_sample
= caa_container_of(node
,
759 struct channel_state_sample
,
760 channel_state_ht_node
);
762 /* Nothing to evaluate, no sample was ever taken. Normal exit */
763 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
768 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
770 0, channel_info
->session_info
->consumed_data_size
,
773 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
777 *session_uid
= channel_info
->session_info
->uid
;
778 *session_gid
= channel_info
->session_info
->gid
;
784 const char *get_condition_session_name(const struct lttng_condition
*condition
)
786 const char *session_name
= NULL
;
787 enum lttng_condition_status status
;
789 switch (lttng_condition_get_type(condition
)) {
790 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
791 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
792 status
= lttng_condition_buffer_usage_get_session_name(
793 condition
, &session_name
);
795 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
796 status
= lttng_condition_session_consumed_size_get_session_name(
797 condition
, &session_name
);
799 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
800 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
801 status
= lttng_condition_session_rotation_get_session_name(
802 condition
, &session_name
);
807 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
808 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
815 /* This function must be called with the RCU read lock held. */
817 int evaluate_session_condition_for_client(
818 const struct lttng_condition
*condition
,
819 struct notification_thread_state
*state
,
820 struct lttng_evaluation
**evaluation
,
821 uid_t
*session_uid
, gid_t
*session_gid
)
824 struct cds_lfht_iter iter
;
825 struct cds_lfht_node
*node
;
826 const char *session_name
;
827 struct session_info
*session_info
= NULL
;
829 session_name
= get_condition_session_name(condition
);
831 /* Find the session associated with the trigger. */
832 cds_lfht_lookup(state
->sessions_ht
,
833 hash_key_str(session_name
, lttng_ht_seed
),
837 node
= cds_lfht_iter_get_node(&iter
);
839 DBG("[notification-thread] No known session matching name \"%s\"",
845 session_info
= caa_container_of(node
, struct session_info
,
847 session_info_get(session_info
);
850 * Evaluation is performed in-line here since only one type of
851 * session-bound condition is handled for the moment.
853 switch (lttng_condition_get_type(condition
)) {
854 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
855 if (!session_info
->rotation
.ongoing
) {
857 goto end_session_put
;
860 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
861 session_info
->rotation
.id
);
864 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
867 goto end_session_put
;
873 goto end_session_put
;
876 *session_uid
= session_info
->uid
;
877 *session_gid
= session_info
->gid
;
880 session_info_put(session_info
);
885 /* This function must be called with the RCU read lock held. */
887 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
888 const struct lttng_condition
*condition
,
889 struct notification_client
*client
,
890 struct notification_thread_state
*state
)
893 struct lttng_evaluation
*evaluation
= NULL
;
894 struct notification_client_list client_list
= { 0 };
895 struct notification_client_list_element client_list_element
= { 0 };
896 uid_t object_uid
= 0;
897 gid_t object_gid
= 0;
904 switch (get_condition_binding_object(condition
)) {
905 case LTTNG_OBJECT_TYPE_SESSION
:
906 ret
= evaluate_session_condition_for_client(condition
, state
,
907 &evaluation
, &object_uid
, &object_gid
);
909 case LTTNG_OBJECT_TYPE_CHANNEL
:
910 ret
= evaluate_channel_condition_for_client(condition
, state
,
911 &evaluation
, &object_uid
, &object_gid
);
913 case LTTNG_OBJECT_TYPE_NONE
:
916 case LTTNG_OBJECT_TYPE_UNKNOWN
:
926 /* Evaluation yielded nothing. Normal exit. */
927 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
933 * Create a temporary client list with the client currently
936 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
937 CDS_INIT_LIST_HEAD(&client_list
.list
);
938 client_list
.trigger
= trigger
;
940 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
941 client_list_element
.client
= client
;
942 cds_list_add(&client_list_element
.node
, &client_list
.list
);
944 /* Send evaluation result to the newly-subscribed client. */
945 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
946 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
947 state
, object_uid
, object_gid
);
954 int notification_thread_client_subscribe(struct notification_client
*client
,
955 struct lttng_condition
*condition
,
956 struct notification_thread_state
*state
,
957 enum lttng_notification_channel_status
*_status
)
960 struct notification_client_list
*client_list
;
961 struct lttng_condition_list_element
*condition_list_element
= NULL
;
962 struct notification_client_list_element
*client_list_element
= NULL
;
963 enum lttng_notification_channel_status status
=
964 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
967 * Ensure that the client has not already subscribed to this condition
970 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
971 if (lttng_condition_is_equal(condition_list_element
->condition
,
973 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
978 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
979 if (!condition_list_element
) {
983 client_list_element
= zmalloc(sizeof(*client_list_element
));
984 if (!client_list_element
) {
992 * Add the newly-subscribed condition to the client's subscription list.
994 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
995 condition_list_element
->condition
= condition
;
996 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
998 client_list
= get_client_list_from_condition(state
, condition
);
1001 * No notification-emiting trigger registered with this
1002 * condition. We don't evaluate the condition right away
1003 * since this trigger is not registered yet.
1005 free(client_list_element
);
1010 * The condition to which the client just subscribed is evaluated
1011 * at this point so that conditions that are already TRUE result
1012 * in a notification being sent out.
1014 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1016 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1018 free(client_list_element
);
1023 * Add the client to the list of clients interested in a given trigger
1024 * if a "notification" trigger with a corresponding condition was
1027 client_list_element
->client
= client
;
1028 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1029 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1038 free(condition_list_element
);
1039 free(client_list_element
);
1044 int notification_thread_client_unsubscribe(
1045 struct notification_client
*client
,
1046 struct lttng_condition
*condition
,
1047 struct notification_thread_state
*state
,
1048 enum lttng_notification_channel_status
*_status
)
1050 struct notification_client_list
*client_list
;
1051 struct lttng_condition_list_element
*condition_list_element
,
1053 struct notification_client_list_element
*client_list_element
,
1055 bool condition_found
= false;
1056 enum lttng_notification_channel_status status
=
1057 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1059 /* Remove the condition from the client's condition list. */
1060 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1061 &client
->condition_list
, node
) {
1062 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1067 cds_list_del(&condition_list_element
->node
);
1069 * The caller may be iterating on the client's conditions to
1070 * tear down a client's connection. In this case, the condition
1071 * will be destroyed at the end.
1073 if (condition
!= condition_list_element
->condition
) {
1074 lttng_condition_destroy(
1075 condition_list_element
->condition
);
1077 free(condition_list_element
);
1078 condition_found
= true;
1082 if (!condition_found
) {
1083 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1088 * Remove the client from the list of clients interested the trigger
1089 * matching the condition.
1092 client_list
= get_client_list_from_condition(state
, condition
);
1097 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1098 &client_list
->list
, node
) {
1099 if (client_list_element
->client
->socket
!= client
->socket
) {
1102 cds_list_del(&client_list_element
->node
);
1103 free(client_list_element
);
1109 lttng_condition_destroy(condition
);
1117 void free_notification_client_rcu(struct rcu_head
*node
)
1119 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1123 void notification_client_destroy(struct notification_client
*client
,
1124 struct notification_thread_state
*state
)
1126 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
1132 /* Release all conditions to which the client was subscribed. */
1133 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
1134 &client
->condition_list
, node
) {
1135 (void) notification_thread_client_unsubscribe(client
,
1136 condition_list_element
->condition
, state
, NULL
);
1139 if (client
->socket
>= 0) {
1140 (void) lttcomm_close_unix_sock(client
->socket
);
1142 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1143 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1144 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1148 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1152 struct notification_client
*get_client_from_socket(int socket
,
1153 struct notification_thread_state
*state
)
1155 struct cds_lfht_iter iter
;
1156 struct cds_lfht_node
*node
;
1157 struct notification_client
*client
= NULL
;
1159 cds_lfht_lookup(state
->client_socket_ht
,
1160 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
1162 (void *) (unsigned long) socket
,
1164 node
= cds_lfht_iter_get_node(&iter
);
1169 client
= caa_container_of(node
, struct notification_client
,
1170 client_socket_ht_node
);
1176 bool buffer_usage_condition_applies_to_channel(
1177 const struct lttng_condition
*condition
,
1178 const struct channel_info
*channel_info
)
1180 enum lttng_condition_status status
;
1181 enum lttng_domain_type condition_domain
;
1182 const char *condition_session_name
= NULL
;
1183 const char *condition_channel_name
= NULL
;
1185 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1187 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1188 if (channel_info
->key
.domain
!= condition_domain
) {
1192 status
= lttng_condition_buffer_usage_get_session_name(
1193 condition
, &condition_session_name
);
1194 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1196 status
= lttng_condition_buffer_usage_get_channel_name(
1197 condition
, &condition_channel_name
);
1198 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1200 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1203 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1213 bool session_consumed_size_condition_applies_to_channel(
1214 const struct lttng_condition
*condition
,
1215 const struct channel_info
*channel_info
)
1217 enum lttng_condition_status status
;
1218 const char *condition_session_name
= NULL
;
1220 status
= lttng_condition_session_consumed_size_get_session_name(
1221 condition
, &condition_session_name
);
1222 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1224 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1234 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1235 const struct channel_info
*channel_info
)
1237 const struct lttng_condition
*condition
;
1238 bool trigger_applies
;
1240 condition
= lttng_trigger_get_const_condition(trigger
);
1245 switch (lttng_condition_get_type(condition
)) {
1246 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1247 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1248 trigger_applies
= buffer_usage_condition_applies_to_channel(
1249 condition
, channel_info
);
1251 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1252 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1253 condition
, channel_info
);
1259 return trigger_applies
;
1265 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1266 struct notification_client
*client
)
1268 bool applies
= false;
1269 struct lttng_condition_list_element
*condition_list_element
;
1271 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1273 applies
= lttng_condition_is_equal(
1274 condition_list_element
->condition
,
1275 lttng_trigger_get_condition(trigger
));
1283 /* Must be called with RCU read lock held. */
1285 struct lttng_session_trigger_list
*get_session_trigger_list(
1286 struct notification_thread_state
*state
,
1287 const char *session_name
)
1289 struct lttng_session_trigger_list
*list
= NULL
;
1290 struct cds_lfht_node
*node
;
1291 struct cds_lfht_iter iter
;
1293 cds_lfht_lookup(state
->session_triggers_ht
,
1294 hash_key_str(session_name
, lttng_ht_seed
),
1295 match_session_trigger_list
,
1298 node
= cds_lfht_iter_get_node(&iter
);
1301 * Not an error, the list of triggers applying to that session
1302 * will be initialized when the session is created.
1304 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1309 list
= caa_container_of(node
,
1310 struct lttng_session_trigger_list
,
1311 session_triggers_ht_node
);
1317 * Allocate an empty lttng_session_trigger_list for the session named
1320 * No ownership of 'session_name' is assumed by the session trigger list.
1321 * It is the caller's responsability to ensure the session name is alive
1322 * for as long as this list is.
1325 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1326 const char *session_name
,
1327 struct cds_lfht
*session_triggers_ht
)
1329 struct lttng_session_trigger_list
*list
;
1331 list
= zmalloc(sizeof(*list
));
1335 list
->session_name
= session_name
;
1336 CDS_INIT_LIST_HEAD(&list
->list
);
1337 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1338 list
->session_triggers_ht
= session_triggers_ht
;
1341 /* Publish the list through the session_triggers_ht. */
1342 cds_lfht_add(session_triggers_ht
,
1343 hash_key_str(session_name
, lttng_ht_seed
),
1344 &list
->session_triggers_ht_node
);
1351 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1353 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1358 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1360 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1362 /* Empty the list element by element, and then free the list itself. */
1363 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1364 &list
->list
, node
) {
1365 cds_list_del(&trigger_list_element
->node
);
1366 free(trigger_list_element
);
1369 /* Unpublish the list from the session_triggers_ht. */
1370 cds_lfht_del(list
->session_triggers_ht
,
1371 &list
->session_triggers_ht_node
);
1373 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1377 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1378 const struct lttng_trigger
*trigger
)
1381 struct lttng_trigger_list_element
*new_element
=
1382 zmalloc(sizeof(*new_element
));
1388 CDS_INIT_LIST_HEAD(&new_element
->node
);
1389 new_element
->trigger
= trigger
;
1390 cds_list_add(&new_element
->node
, &list
->list
);
1396 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1397 const char *session_name
)
1399 bool applies
= false;
1400 const struct lttng_condition
*condition
;
1402 condition
= lttng_trigger_get_const_condition(trigger
);
1403 switch (lttng_condition_get_type(condition
)) {
1404 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1405 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1407 enum lttng_condition_status condition_status
;
1408 const char *condition_session_name
;
1410 condition_status
= lttng_condition_session_rotation_get_session_name(
1411 condition
, &condition_session_name
);
1412 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1413 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1417 assert(condition_session_name
);
1418 applies
= !strcmp(condition_session_name
, session_name
);
1429 * Allocate and initialize an lttng_session_trigger_list which contains
1430 * all triggers that apply to the session named 'session_name'.
1432 * No ownership of 'session_name' is assumed by the session trigger list.
1433 * It is the caller's responsability to ensure the session name is alive
1434 * for as long as this list is.
1437 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1438 const struct notification_thread_state
*state
,
1439 const char *session_name
)
1441 int trigger_count
= 0;
1442 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1443 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1444 struct cds_lfht_iter iter
;
1446 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1447 state
->session_triggers_ht
);
1449 /* Add all triggers applying to the session named 'session_name'. */
1450 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1454 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1459 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1460 trigger_ht_element
->trigger
);
1468 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1470 return session_trigger_list
;
1472 lttng_session_trigger_list_destroy(session_trigger_list
);
1477 struct session_info
*find_or_create_session_info(
1478 struct notification_thread_state
*state
,
1479 const char *name
, uid_t uid
, gid_t gid
)
1481 struct session_info
*session
= NULL
;
1482 struct cds_lfht_node
*node
;
1483 struct cds_lfht_iter iter
;
1484 struct lttng_session_trigger_list
*trigger_list
;
1487 cds_lfht_lookup(state
->sessions_ht
,
1488 hash_key_str(name
, lttng_ht_seed
),
1492 node
= cds_lfht_iter_get_node(&iter
);
1494 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1496 session
= caa_container_of(node
, struct session_info
,
1498 assert(session
->uid
== uid
);
1499 assert(session
->gid
== gid
);
1500 session_info_get(session
);
1504 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1505 if (!trigger_list
) {
1509 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1510 state
->sessions_ht
);
1512 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1514 lttng_session_trigger_list_destroy(trigger_list
);
1517 trigger_list
= NULL
;
1519 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1520 &session
->sessions_ht_node
);
1526 session_info_put(session
);
1531 int handle_notification_thread_command_add_channel(
1532 struct notification_thread_state
*state
,
1533 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1534 const char *channel_name
, enum lttng_domain_type channel_domain
,
1535 uint64_t channel_key_int
, uint64_t channel_capacity
,
1536 enum lttng_error_code
*cmd_result
)
1538 struct cds_list_head trigger_list
;
1539 struct channel_info
*new_channel_info
= NULL
;
1540 struct channel_key channel_key
= {
1541 .key
= channel_key_int
,
1542 .domain
= channel_domain
,
1544 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1545 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1546 int trigger_count
= 0;
1547 struct cds_lfht_iter iter
;
1548 struct session_info
*session_info
= NULL
;
1550 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1551 channel_name
, session_name
, channel_key_int
,
1552 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1554 CDS_INIT_LIST_HEAD(&trigger_list
);
1556 session_info
= find_or_create_session_info(state
, session_name
,
1557 session_uid
, session_gid
);
1558 if (!session_info
) {
1559 /* Allocation error or an internal error occurred. */
1563 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1564 channel_capacity
, session_info
);
1565 if (!new_channel_info
) {
1570 /* Build a list of all triggers applying to the new channel. */
1571 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1573 struct lttng_trigger_list_element
*new_element
;
1575 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1576 new_channel_info
)) {
1580 new_element
= zmalloc(sizeof(*new_element
));
1585 CDS_INIT_LIST_HEAD(&new_element
->node
);
1586 new_element
->trigger
= trigger_ht_element
->trigger
;
1587 cds_list_add(&new_element
->node
, &trigger_list
);
1592 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1594 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1595 if (!channel_trigger_list
) {
1598 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1599 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1600 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1601 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1604 /* Add channel to the channel_ht which owns the channel_infos. */
1605 cds_lfht_add(state
->channels_ht
,
1606 hash_channel_key(&new_channel_info
->key
),
1607 &new_channel_info
->channels_ht_node
);
1609 * Add the list of triggers associated with this channel to the
1610 * channel_triggers_ht.
1612 cds_lfht_add(state
->channel_triggers_ht
,
1613 hash_channel_key(&new_channel_info
->key
),
1614 &channel_trigger_list
->channel_triggers_ht_node
);
1616 session_info_put(session_info
);
1617 *cmd_result
= LTTNG_OK
;
1620 channel_info_destroy(new_channel_info
);
1621 session_info_put(session_info
);
1626 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1628 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1633 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1635 free(caa_container_of(node
, struct channel_state_sample
,
1640 int handle_notification_thread_command_remove_channel(
1641 struct notification_thread_state
*state
,
1642 uint64_t channel_key
, enum lttng_domain_type domain
,
1643 enum lttng_error_code
*cmd_result
)
1645 struct cds_lfht_node
*node
;
1646 struct cds_lfht_iter iter
;
1647 struct lttng_channel_trigger_list
*trigger_list
;
1648 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1649 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1650 struct channel_info
*channel_info
;
1652 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1653 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1657 cds_lfht_lookup(state
->channel_triggers_ht
,
1658 hash_channel_key(&key
),
1659 match_channel_trigger_list
,
1662 node
= cds_lfht_iter_get_node(&iter
);
1664 * There is a severe internal error if we are being asked to remove a
1665 * channel that doesn't exist.
1668 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1672 /* Free the list of triggers associated with this channel. */
1673 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1674 channel_triggers_ht_node
);
1675 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1676 &trigger_list
->list
, node
) {
1677 cds_list_del(&trigger_list_element
->node
);
1678 free(trigger_list_element
);
1680 cds_lfht_del(state
->channel_triggers_ht
, node
);
1681 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1683 /* Free sampled channel state. */
1684 cds_lfht_lookup(state
->channel_state_ht
,
1685 hash_channel_key(&key
),
1686 match_channel_state_sample
,
1689 node
= cds_lfht_iter_get_node(&iter
);
1691 * This is expected to be NULL if the channel is destroyed before we
1692 * received a sample.
1695 struct channel_state_sample
*sample
= caa_container_of(node
,
1696 struct channel_state_sample
,
1697 channel_state_ht_node
);
1699 cds_lfht_del(state
->channel_state_ht
, node
);
1700 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1703 /* Remove the channel from the channels_ht and free it. */
1704 cds_lfht_lookup(state
->channels_ht
,
1705 hash_channel_key(&key
),
1709 node
= cds_lfht_iter_get_node(&iter
);
1711 channel_info
= caa_container_of(node
, struct channel_info
,
1713 cds_lfht_del(state
->channels_ht
, node
);
1714 channel_info_destroy(channel_info
);
1717 *cmd_result
= LTTNG_OK
;
1722 int handle_notification_thread_command_session_rotation(
1723 struct notification_thread_state
*state
,
1724 enum notification_thread_command_type cmd_type
,
1725 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1726 uint64_t trace_archive_chunk_id
,
1727 struct lttng_trace_archive_location
*location
,
1728 enum lttng_error_code
*_cmd_result
)
1731 enum lttng_error_code cmd_result
= LTTNG_OK
;
1732 struct lttng_session_trigger_list
*trigger_list
;
1733 struct lttng_trigger_list_element
*trigger_list_element
;
1734 struct session_info
*session_info
;
1738 session_info
= find_or_create_session_info(state
, session_name
,
1739 session_uid
, session_gid
);
1740 if (!session_info
) {
1741 /* Allocation error or an internal error occurred. */
1743 cmd_result
= LTTNG_ERR_NOMEM
;
1747 session_info
->rotation
.ongoing
=
1748 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1749 session_info
->rotation
.id
= trace_archive_chunk_id
;
1750 trigger_list
= get_session_trigger_list(state
, session_name
);
1751 if (!trigger_list
) {
1752 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1757 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1759 const struct lttng_condition
*condition
;
1760 const struct lttng_action
*action
;
1761 const struct lttng_trigger
*trigger
;
1762 struct notification_client_list
*client_list
;
1763 struct lttng_evaluation
*evaluation
= NULL
;
1764 enum lttng_condition_type condition_type
;
1766 trigger
= trigger_list_element
->trigger
;
1767 condition
= lttng_trigger_get_const_condition(trigger
);
1769 condition_type
= lttng_condition_get_type(condition
);
1771 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1772 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1774 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1775 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1779 action
= lttng_trigger_get_const_action(trigger
);
1781 /* Notify actions are the only type currently supported. */
1782 assert(lttng_action_get_type_const(action
) ==
1783 LTTNG_ACTION_TYPE_NOTIFY
);
1785 client_list
= get_client_list_from_condition(state
, condition
);
1786 assert(client_list
);
1788 if (cds_list_empty(&client_list
->list
)) {
1790 * No clients interested in the evaluation's result,
1796 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1797 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1798 trace_archive_chunk_id
);
1800 evaluation
= lttng_evaluation_session_rotation_completed_create(
1801 trace_archive_chunk_id
, location
);
1805 /* Internal error */
1807 cmd_result
= LTTNG_ERR_UNK
;
1811 /* Dispatch evaluation result to all clients. */
1812 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1813 evaluation
, client_list
, state
,
1816 lttng_evaluation_destroy(evaluation
);
1817 if (caa_unlikely(ret
)) {
1822 session_info_put(session_info
);
1823 *_cmd_result
= cmd_result
;
1829 int condition_is_supported(struct lttng_condition
*condition
)
1833 switch (lttng_condition_get_type(condition
)) {
1834 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1835 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1837 enum lttng_domain_type domain
;
1839 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1846 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1852 * Older kernel tracers don't expose the API to monitor their
1853 * buffers. Therefore, we reject triggers that require that
1854 * mechanism to be available to be evaluated.
1856 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1866 /* Must be called with RCU read lock held. */
1868 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1869 struct notification_thread_state
*state
)
1872 const struct lttng_condition
*condition
;
1873 const char *session_name
;
1874 struct lttng_session_trigger_list
*trigger_list
;
1876 condition
= lttng_trigger_get_const_condition(trigger
);
1877 switch (lttng_condition_get_type(condition
)) {
1878 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1879 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1881 enum lttng_condition_status status
;
1883 status
= lttng_condition_session_rotation_get_session_name(
1884 condition
, &session_name
);
1885 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1886 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1897 trigger_list
= get_session_trigger_list(state
, session_name
);
1898 if (!trigger_list
) {
1899 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1905 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1907 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1912 /* Must be called with RCU read lock held. */
1914 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1915 struct notification_thread_state
*state
)
1918 struct cds_lfht_node
*node
;
1919 struct cds_lfht_iter iter
;
1920 struct channel_info
*channel
;
1922 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1924 struct lttng_trigger_list_element
*trigger_list_element
;
1925 struct lttng_channel_trigger_list
*trigger_list
;
1926 struct cds_lfht_iter lookup_iter
;
1928 if (!trigger_applies_to_channel(trigger
, channel
)) {
1932 cds_lfht_lookup(state
->channel_triggers_ht
,
1933 hash_channel_key(&channel
->key
),
1934 match_channel_trigger_list
,
1937 node
= cds_lfht_iter_get_node(&lookup_iter
);
1939 trigger_list
= caa_container_of(node
,
1940 struct lttng_channel_trigger_list
,
1941 channel_triggers_ht_node
);
1943 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1944 if (!trigger_list_element
) {
1948 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1949 trigger_list_element
->trigger
= trigger
;
1950 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1951 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
1959 * FIXME A client's credentials are not checked when registering a trigger, nor
1960 * are they stored alongside with the trigger.
1962 * The effects of this are benign since:
1963 * - The client will succeed in registering the trigger, as it is valid,
1964 * - The trigger will, internally, be bound to the channel/session,
1965 * - The notifications will not be sent since the client's credentials
1966 * are checked against the channel at that moment.
1968 * If this function returns a non-zero value, it means something is
1969 * fundamentally broken and the whole subsystem/thread will be torn down.
1971 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1975 int handle_notification_thread_command_register_trigger(
1976 struct notification_thread_state
*state
,
1977 struct lttng_trigger
*trigger
,
1978 enum lttng_error_code
*cmd_result
)
1981 struct lttng_condition
*condition
;
1982 struct notification_client
*client
;
1983 struct notification_client_list
*client_list
= NULL
;
1984 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1985 struct notification_client_list_element
*client_list_element
, *tmp
;
1986 struct cds_lfht_node
*node
;
1987 struct cds_lfht_iter iter
;
1988 bool free_trigger
= true;
1992 condition
= lttng_trigger_get_condition(trigger
);
1995 ret
= condition_is_supported(condition
);
1998 } else if (ret
== 0) {
1999 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2002 /* Feature is supported, continue. */
2006 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2007 if (!trigger_ht_element
) {
2012 /* Add trigger to the trigger_ht. */
2013 cds_lfht_node_init(&trigger_ht_element
->node
);
2014 trigger_ht_element
->trigger
= trigger
;
2016 node
= cds_lfht_add_unique(state
->triggers_ht
,
2017 lttng_condition_hash(condition
),
2020 &trigger_ht_element
->node
);
2021 if (node
!= &trigger_ht_element
->node
) {
2022 /* Not a fatal error, simply report it to the client. */
2023 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2024 goto error_free_ht_element
;
2028 * Ownership of the trigger and of its wrapper was transfered to
2031 trigger_ht_element
= NULL
;
2032 free_trigger
= false;
2035 * The rest only applies to triggers that have a "notify" action.
2036 * It is not skipped as this is the only action type currently
2039 client_list
= zmalloc(sizeof(*client_list
));
2042 goto error_free_ht_element
;
2044 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
2045 CDS_INIT_LIST_HEAD(&client_list
->list
);
2046 client_list
->trigger
= trigger
;
2048 /* Build a list of clients to which this new trigger applies. */
2049 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2050 client_socket_ht_node
) {
2051 if (!trigger_applies_to_client(trigger
, client
)) {
2055 client_list_element
= zmalloc(sizeof(*client_list_element
));
2056 if (!client_list_element
) {
2058 goto error_free_client_list
;
2060 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2061 client_list_element
->client
= client
;
2062 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2065 cds_lfht_add(state
->notification_trigger_clients_ht
,
2066 lttng_condition_hash(condition
),
2067 &client_list
->notification_trigger_ht_node
);
2069 switch (get_condition_binding_object(condition
)) {
2070 case LTTNG_OBJECT_TYPE_SESSION
:
2071 /* Add the trigger to the list if it matches a known session. */
2072 ret
= bind_trigger_to_matching_session(trigger
, state
);
2074 goto error_free_client_list
;
2077 case LTTNG_OBJECT_TYPE_CHANNEL
:
2079 * Add the trigger to list of triggers bound to the channels
2082 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2084 goto error_free_client_list
;
2087 case LTTNG_OBJECT_TYPE_NONE
:
2090 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2092 goto error_free_client_list
;
2096 * Since there is nothing preventing clients from subscribing to a
2097 * condition before the corresponding trigger is registered, we have
2098 * to evaluate this new condition right away.
2100 * At some point, we were waiting for the next "evaluation" (e.g. on
2101 * reception of a channel sample) to evaluate this new condition, but
2104 * The reason it was broken is that waiting for the next sample
2105 * does not allow us to properly handle transitions for edge-triggered
2108 * Consider this example: when we handle a new channel sample, we
2109 * evaluate each conditions twice: once with the previous state, and
2110 * again with the newest state. We then use those two results to
2111 * determine whether a state change happened: a condition was false and
2112 * became true. If a state change happened, we have to notify clients.
2114 * Now, if a client subscribes to a given notification and registers
2115 * a trigger *after* that subscription, we have to make sure the
2116 * condition is evaluated at this point while considering only the
2117 * current state. Otherwise, the next evaluation cycle may only see
2118 * that the evaluations remain the same (true for samples n-1 and n) and
2119 * the client will never know that the condition has been met.
2121 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2122 &client_list
->list
, node
) {
2123 ret
= evaluate_condition_for_client(trigger
, condition
,
2124 client_list_element
->client
, state
);
2126 goto error_free_client_list
;
2131 * Client list ownership transferred to the
2132 * notification_trigger_clients_ht.
2136 *cmd_result
= LTTNG_OK
;
2137 error_free_client_list
:
2139 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2140 &client_list
->list
, node
) {
2141 free(client_list_element
);
2145 error_free_ht_element
:
2146 free(trigger_ht_element
);
2149 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
2151 lttng_condition_destroy(condition
);
2152 lttng_action_destroy(action
);
2153 lttng_trigger_destroy(trigger
);
2160 void free_notification_client_list_rcu(struct rcu_head
*node
)
2162 free(caa_container_of(node
, struct notification_client_list
,
2167 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2169 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2174 int handle_notification_thread_command_unregister_trigger(
2175 struct notification_thread_state
*state
,
2176 struct lttng_trigger
*trigger
,
2177 enum lttng_error_code
*_cmd_reply
)
2179 struct cds_lfht_iter iter
;
2180 struct cds_lfht_node
*triggers_ht_node
;
2181 struct lttng_channel_trigger_list
*trigger_list
;
2182 struct notification_client_list
*client_list
;
2183 struct notification_client_list_element
*client_list_element
, *tmp
;
2184 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2185 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2187 struct lttng_action
*action
;
2188 enum lttng_error_code cmd_reply
;
2192 cds_lfht_lookup(state
->triggers_ht
,
2193 lttng_condition_hash(condition
),
2197 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2198 if (!triggers_ht_node
) {
2199 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2202 cmd_reply
= LTTNG_OK
;
2205 /* Remove trigger from channel_triggers_ht. */
2206 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2207 channel_triggers_ht_node
) {
2208 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2210 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2211 &trigger_list
->list
, node
) {
2212 const struct lttng_condition
*current_condition
=
2213 lttng_trigger_get_const_condition(
2214 trigger_element
->trigger
);
2216 assert(current_condition
);
2217 if (!lttng_condition_is_equal(condition
,
2218 current_condition
)) {
2222 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2223 cds_list_del(&trigger_element
->node
);
2224 /* A trigger can only appear once per channel */
2230 * Remove and release the client list from
2231 * notification_trigger_clients_ht.
2233 client_list
= get_client_list_from_condition(state
, condition
);
2234 assert(client_list
);
2236 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2237 &client_list
->list
, node
) {
2238 free(client_list_element
);
2240 cds_lfht_del(state
->notification_trigger_clients_ht
,
2241 &client_list
->notification_trigger_ht_node
);
2242 call_rcu(&client_list
->rcu_node
, free_notification_client_list_rcu
);
2244 /* Remove trigger from triggers_ht. */
2245 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2246 struct lttng_trigger_ht_element
, node
);
2247 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2249 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
2250 lttng_condition_destroy(condition
);
2251 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
2252 lttng_action_destroy(action
);
2253 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2254 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2258 *_cmd_reply
= cmd_reply
;
2263 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2264 int handle_notification_thread_command(
2265 struct notification_thread_handle
*handle
,
2266 struct notification_thread_state
*state
)
2270 struct notification_thread_command
*cmd
;
2272 /* Read the event pipe to put it back into a quiescent state. */
2273 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2275 if (ret
!= sizeof(counter
)) {
2279 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2280 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2281 struct notification_thread_command
, cmd_list_node
);
2282 switch (cmd
->type
) {
2283 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2284 DBG("[notification-thread] Received register trigger command");
2285 ret
= handle_notification_thread_command_register_trigger(
2286 state
, cmd
->parameters
.trigger
,
2289 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2290 DBG("[notification-thread] Received unregister trigger command");
2291 ret
= handle_notification_thread_command_unregister_trigger(
2292 state
, cmd
->parameters
.trigger
,
2295 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2296 DBG("[notification-thread] Received add channel command");
2297 ret
= handle_notification_thread_command_add_channel(
2299 cmd
->parameters
.add_channel
.session
.name
,
2300 cmd
->parameters
.add_channel
.session
.uid
,
2301 cmd
->parameters
.add_channel
.session
.gid
,
2302 cmd
->parameters
.add_channel
.channel
.name
,
2303 cmd
->parameters
.add_channel
.channel
.domain
,
2304 cmd
->parameters
.add_channel
.channel
.key
,
2305 cmd
->parameters
.add_channel
.channel
.capacity
,
2308 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2309 DBG("[notification-thread] Received remove channel command");
2310 ret
= handle_notification_thread_command_remove_channel(
2311 state
, cmd
->parameters
.remove_channel
.key
,
2312 cmd
->parameters
.remove_channel
.domain
,
2315 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2316 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2317 DBG("[notification-thread] Received session rotation %s command",
2318 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2319 "ongoing" : "completed");
2320 ret
= handle_notification_thread_command_session_rotation(
2323 cmd
->parameters
.session_rotation
.session_name
,
2324 cmd
->parameters
.session_rotation
.uid
,
2325 cmd
->parameters
.session_rotation
.gid
,
2326 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2327 cmd
->parameters
.session_rotation
.location
,
2330 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2331 DBG("[notification-thread] Received quit command");
2332 cmd
->reply_code
= LTTNG_OK
;
2336 ERR("[notification-thread] Unknown internal command received");
2344 cds_list_del(&cmd
->cmd_list_node
);
2345 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2346 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2349 /* Wake-up and return a fatal error to the calling thread. */
2350 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2351 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2352 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2354 /* Indicate a fatal error to the caller. */
2359 unsigned long hash_client_socket(int socket
)
2361 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
2365 int socket_set_non_blocking(int socket
)
2369 /* Set the pipe as non-blocking. */
2370 ret
= fcntl(socket
, F_GETFL
, 0);
2372 PERROR("fcntl get socket flags");
2377 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2379 PERROR("fcntl set O_NONBLOCK socket flag");
2382 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2388 int client_reset_inbound_state(struct notification_client
*client
)
2392 ret
= lttng_dynamic_buffer_set_size(
2393 &client
->communication
.inbound
.buffer
, 0);
2396 client
->communication
.inbound
.bytes_to_receive
=
2397 sizeof(struct lttng_notification_channel_message
);
2398 client
->communication
.inbound
.msg_type
=
2399 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2400 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2401 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2402 ret
= lttng_dynamic_buffer_set_size(
2403 &client
->communication
.inbound
.buffer
,
2404 client
->communication
.inbound
.bytes_to_receive
);
2408 int handle_notification_thread_client_connect(
2409 struct notification_thread_state
*state
)
2412 struct notification_client
*client
;
2414 DBG("[notification-thread] Handling new notification channel client connection");
2416 client
= zmalloc(sizeof(*client
));
2422 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2423 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2424 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2425 client
->communication
.inbound
.expect_creds
= true;
2426 ret
= client_reset_inbound_state(client
);
2428 ERR("[notification-thread] Failed to reset client communication's inbound state");
2433 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2435 ERR("[notification-thread] Failed to accept new notification channel client connection");
2440 client
->socket
= ret
;
2442 ret
= socket_set_non_blocking(client
->socket
);
2444 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2448 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2450 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2455 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2456 LPOLLIN
| LPOLLERR
|
2457 LPOLLHUP
| LPOLLRDHUP
);
2459 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2463 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2467 cds_lfht_add(state
->client_socket_ht
,
2468 hash_client_socket(client
->socket
),
2469 &client
->client_socket_ht_node
);
2474 notification_client_destroy(client
, state
);
2478 int handle_notification_thread_client_disconnect(
2480 struct notification_thread_state
*state
)
2483 struct notification_client
*client
;
2486 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2488 client
= get_client_from_socket(client_socket
, state
);
2490 /* Internal state corruption, fatal error. */
2491 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2497 ret
= lttng_poll_del(&state
->events
, client_socket
);
2499 ERR("[notification-thread] Failed to remove client socket from poll set");
2501 cds_lfht_del(state
->client_socket_ht
,
2502 &client
->client_socket_ht_node
);
2503 notification_client_destroy(client
, state
);
2509 int handle_notification_thread_client_disconnect_all(
2510 struct notification_thread_state
*state
)
2512 struct cds_lfht_iter iter
;
2513 struct notification_client
*client
;
2514 bool error_encoutered
= false;
2517 DBG("[notification-thread] Closing all client connections");
2518 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2519 client_socket_ht_node
) {
2522 ret
= handle_notification_thread_client_disconnect(
2523 client
->socket
, state
);
2525 error_encoutered
= true;
2529 return error_encoutered
? 1 : 0;
2532 int handle_notification_thread_trigger_unregister_all(
2533 struct notification_thread_state
*state
)
2535 bool error_occurred
= false;
2536 struct cds_lfht_iter iter
;
2537 struct lttng_trigger_ht_element
*trigger_ht_element
;
2540 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2542 int ret
= handle_notification_thread_command_unregister_trigger(
2543 state
, trigger_ht_element
->trigger
, NULL
);
2545 error_occurred
= true;
2549 return error_occurred
? -1 : 0;
2553 int client_flush_outgoing_queue(struct notification_client
*client
,
2554 struct notification_thread_state
*state
)
2557 size_t to_send_count
;
2559 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2560 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2561 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2564 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2565 client
->communication
.outbound
.buffer
.data
,
2567 if ((ret
>= 0 && ret
< to_send_count
)) {
2568 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2570 to_send_count
-= max(ret
, 0);
2572 memcpy(client
->communication
.outbound
.buffer
.data
,
2573 client
->communication
.outbound
.buffer
.data
+
2574 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2576 ret
= lttng_dynamic_buffer_set_size(
2577 &client
->communication
.outbound
.buffer
,
2584 * We want to be notified whenever there is buffer space
2585 * available to send the rest of the payload.
2587 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2588 CLIENT_POLL_MASK_IN_OUT
);
2592 } else if (ret
< 0) {
2593 /* Generic error, disconnect the client. */
2594 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2596 ret
= handle_notification_thread_client_disconnect(
2597 client
->socket
, state
);
2602 /* No error and flushed the queue completely. */
2603 ret
= lttng_dynamic_buffer_set_size(
2604 &client
->communication
.outbound
.buffer
, 0);
2608 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2609 CLIENT_POLL_MASK_IN
);
2614 client
->communication
.outbound
.queued_command_reply
= false;
2615 client
->communication
.outbound
.dropped_notification
= false;
2624 int client_send_command_reply(struct notification_client
*client
,
2625 struct notification_thread_state
*state
,
2626 enum lttng_notification_channel_status status
)
2629 struct lttng_notification_channel_command_reply reply
= {
2630 .status
= (int8_t) status
,
2632 struct lttng_notification_channel_message msg
= {
2633 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2634 .size
= sizeof(reply
),
2636 char buffer
[sizeof(msg
) + sizeof(reply
)];
2638 if (client
->communication
.outbound
.queued_command_reply
) {
2639 /* Protocol error. */
2643 memcpy(buffer
, &msg
, sizeof(msg
));
2644 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2645 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2647 /* Enqueue buffer to outgoing queue and flush it. */
2648 ret
= lttng_dynamic_buffer_append(
2649 &client
->communication
.outbound
.buffer
,
2650 buffer
, sizeof(buffer
));
2655 ret
= client_flush_outgoing_queue(client
, state
);
2660 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2661 /* Queue could not be emptied. */
2662 client
->communication
.outbound
.queued_command_reply
= true;
2671 int client_dispatch_message(struct notification_client
*client
,
2672 struct notification_thread_state
*state
)
2676 if (client
->communication
.inbound
.msg_type
!=
2677 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2678 client
->communication
.inbound
.msg_type
!=
2679 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2680 !client
->validated
) {
2681 WARN("[notification-thread] client attempted a command before handshake");
2686 switch (client
->communication
.inbound
.msg_type
) {
2687 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2690 * Receiving message header. The function will be called again
2691 * once the rest of the message as been received and can be
2694 const struct lttng_notification_channel_message
*msg
;
2696 assert(sizeof(*msg
) ==
2697 client
->communication
.inbound
.buffer
.size
);
2698 msg
= (const struct lttng_notification_channel_message
*)
2699 client
->communication
.inbound
.buffer
.data
;
2701 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2702 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2707 switch (msg
->type
) {
2708 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2709 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2710 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2714 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2718 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2719 client
->communication
.inbound
.msg_type
=
2720 (enum lttng_notification_channel_message_type
) msg
->type
;
2721 ret
= lttng_dynamic_buffer_set_size(
2722 &client
->communication
.inbound
.buffer
, msg
->size
);
2728 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2730 struct lttng_notification_channel_command_handshake
*handshake_client
;
2731 struct lttng_notification_channel_command_handshake handshake_reply
= {
2732 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2733 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2735 struct lttng_notification_channel_message msg_header
= {
2736 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2737 .size
= sizeof(handshake_reply
),
2739 enum lttng_notification_channel_status status
=
2740 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2741 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2743 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2744 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2745 sizeof(handshake_reply
));
2748 (struct lttng_notification_channel_command_handshake
*)
2749 client
->communication
.inbound
.buffer
.data
;
2750 client
->major
= handshake_client
->major
;
2751 client
->minor
= handshake_client
->minor
;
2752 if (!client
->communication
.inbound
.creds_received
) {
2753 ERR("[notification-thread] No credentials received from client");
2758 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2759 &client
->communication
.inbound
.creds
);
2760 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2761 &client
->communication
.inbound
.creds
);
2762 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2763 client
->uid
, client
->gid
, (int) client
->major
,
2764 (int) client
->minor
);
2766 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2767 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2770 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2771 send_buffer
, sizeof(send_buffer
));
2773 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2777 ret
= client_flush_outgoing_queue(client
, state
);
2782 ret
= client_send_command_reply(client
, state
, status
);
2784 ERR("[notification-thread] Failed to send reply to notification channel client");
2788 /* Set reception state to receive the next message header. */
2789 ret
= client_reset_inbound_state(client
);
2791 ERR("[notification-thread] Failed to reset client communication's inbound state");
2794 client
->validated
= true;
2797 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2798 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2800 struct lttng_condition
*condition
;
2801 enum lttng_notification_channel_status status
=
2802 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2803 const struct lttng_buffer_view condition_view
=
2804 lttng_buffer_view_from_dynamic_buffer(
2805 &client
->communication
.inbound
.buffer
,
2807 size_t expected_condition_size
=
2808 client
->communication
.inbound
.buffer
.size
;
2810 ret
= lttng_condition_create_from_buffer(&condition_view
,
2812 if (ret
!= expected_condition_size
) {
2813 ERR("[notification-thread] Malformed condition received from client");
2817 if (client
->communication
.inbound
.msg_type
==
2818 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2819 ret
= notification_thread_client_subscribe(client
,
2820 condition
, state
, &status
);
2822 ret
= notification_thread_client_unsubscribe(client
,
2823 condition
, state
, &status
);
2829 ret
= client_send_command_reply(client
, state
, status
);
2831 ERR("[notification-thread] Failed to send reply to notification channel client");
2835 /* Set reception state to receive the next message header. */
2836 ret
= client_reset_inbound_state(client
);
2838 ERR("[notification-thread] Failed to reset client communication's inbound state");
2850 /* Incoming data from client. */
2851 int handle_notification_thread_client_in(
2852 struct notification_thread_state
*state
, int socket
)
2855 struct notification_client
*client
;
2859 client
= get_client_from_socket(socket
, state
);
2861 /* Internal error, abort. */
2866 offset
= client
->communication
.inbound
.buffer
.size
-
2867 client
->communication
.inbound
.bytes_to_receive
;
2868 if (client
->communication
.inbound
.expect_creds
) {
2869 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2870 client
->communication
.inbound
.buffer
.data
+ offset
,
2871 client
->communication
.inbound
.bytes_to_receive
,
2872 &client
->communication
.inbound
.creds
);
2874 client
->communication
.inbound
.expect_creds
= false;
2875 client
->communication
.inbound
.creds_received
= true;
2878 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2879 client
->communication
.inbound
.buffer
.data
+ offset
,
2880 client
->communication
.inbound
.bytes_to_receive
);
2883 goto error_disconnect_client
;
2886 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2887 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2888 ret
= client_dispatch_message(client
, state
);
2891 * Only returns an error if this client must be
2894 goto error_disconnect_client
;
2901 error_disconnect_client
:
2902 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2906 /* Client ready to receive outgoing data. */
2907 int handle_notification_thread_client_out(
2908 struct notification_thread_state
*state
, int socket
)
2911 struct notification_client
*client
;
2913 client
= get_client_from_socket(socket
, state
);
2915 /* Internal error, abort. */
2920 ret
= client_flush_outgoing_queue(client
, state
);
2929 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2930 const struct channel_state_sample
*sample
,
2931 uint64_t buffer_capacity
)
2933 bool result
= false;
2935 enum lttng_condition_type condition_type
;
2936 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2937 condition
, struct lttng_condition_buffer_usage
,
2940 if (use_condition
->threshold_bytes
.set
) {
2941 threshold
= use_condition
->threshold_bytes
.value
;
2944 * Threshold was expressed as a ratio.
2946 * TODO the threshold (in bytes) of conditions expressed
2947 * as a ratio of total buffer size could be cached to
2948 * forego this double-multiplication or it could be performed
2949 * as fixed-point math.
2951 * Note that caching should accommodates the case where the
2952 * condition applies to multiple channels (i.e. don't assume
2953 * that all channels matching my_chann* have the same size...)
2955 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2956 (double) buffer_capacity
);
2959 condition_type
= lttng_condition_get_type(condition
);
2960 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2961 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2962 threshold
, sample
->highest_usage
);
2965 * The low condition should only be triggered once _all_ of the
2966 * streams in a channel have gone below the "low" threshold.
2968 if (sample
->highest_usage
<= threshold
) {
2972 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2973 threshold
, sample
->highest_usage
);
2976 * For high buffer usage scenarios, we want to trigger whenever
2977 * _any_ of the streams has reached the "high" threshold.
2979 if (sample
->highest_usage
>= threshold
) {
2988 bool evaluate_session_consumed_size_condition(
2989 const struct lttng_condition
*condition
,
2990 uint64_t session_consumed_size
)
2993 const struct lttng_condition_session_consumed_size
*size_condition
=
2994 container_of(condition
,
2995 struct lttng_condition_session_consumed_size
,
2998 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2999 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3000 threshold
, session_consumed_size
);
3001 return session_consumed_size
>= threshold
;
3005 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3006 struct lttng_evaluation
**evaluation
,
3007 const struct notification_thread_state
*state
,
3008 const struct channel_state_sample
*previous_sample
,
3009 const struct channel_state_sample
*latest_sample
,
3010 uint64_t previous_session_consumed_total
,
3011 uint64_t latest_session_consumed_total
,
3012 struct channel_info
*channel_info
)
3015 enum lttng_condition_type condition_type
;
3016 const bool previous_sample_available
= !!previous_sample
;
3017 bool previous_sample_result
= false;
3018 bool latest_sample_result
;
3020 condition_type
= lttng_condition_get_type(condition
);
3022 switch (condition_type
) {
3023 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3024 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3025 if (caa_likely(previous_sample_available
)) {
3026 previous_sample_result
=
3027 evaluate_buffer_usage_condition(condition
,
3028 previous_sample
, channel_info
->capacity
);
3030 latest_sample_result
= evaluate_buffer_usage_condition(
3031 condition
, latest_sample
,
3032 channel_info
->capacity
);
3034 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3035 if (caa_likely(previous_sample_available
)) {
3036 previous_sample_result
=
3037 evaluate_session_consumed_size_condition(
3039 previous_session_consumed_total
);
3041 latest_sample_result
=
3042 evaluate_session_consumed_size_condition(
3044 latest_session_consumed_total
);
3047 /* Unknown condition type; internal error. */
3051 if (!latest_sample_result
||
3052 (previous_sample_result
== latest_sample_result
)) {
3054 * Only trigger on a condition evaluation transition.
3056 * NOTE: This edge-triggered logic may not be appropriate for
3057 * future condition types.
3062 if (!evaluation
|| !latest_sample_result
) {
3066 switch (condition_type
) {
3067 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3068 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3069 *evaluation
= lttng_evaluation_buffer_usage_create(
3071 latest_sample
->highest_usage
,
3072 channel_info
->capacity
);
3074 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3075 *evaluation
= lttng_evaluation_session_consumed_size_create(
3076 latest_session_consumed_total
);
3091 int client_enqueue_dropped_notification(struct notification_client
*client
,
3092 struct notification_thread_state
*state
)
3095 struct lttng_notification_channel_message msg
= {
3096 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3100 ret
= lttng_dynamic_buffer_append(
3101 &client
->communication
.outbound
.buffer
, &msg
,
3107 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3108 const struct lttng_evaluation
*evaluation
,
3109 struct notification_client_list
* client_list
,
3110 struct notification_thread_state
*state
,
3111 uid_t channel_uid
, gid_t channel_gid
)
3114 struct lttng_dynamic_buffer msg_buffer
;
3115 struct notification_client_list_element
*client_list_element
, *tmp
;
3116 const struct lttng_notification notification
= {
3117 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
3118 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3120 struct lttng_notification_channel_message msg_header
= {
3121 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3124 lttng_dynamic_buffer_init(&msg_buffer
);
3126 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
3127 sizeof(msg_header
));
3132 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
3134 ERR("[notification-thread] Failed to serialize notification");
3139 /* Update payload size. */
3140 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
3141 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
3143 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3144 &client_list
->list
, node
) {
3145 struct notification_client
*client
=
3146 client_list_element
->client
;
3148 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
3150 /* Client is not allowed to monitor this channel. */
3151 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
3155 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3156 client
->socket
, msg_buffer
.size
);
3157 if (client
->communication
.outbound
.buffer
.size
) {
3159 * Outgoing data is already buffered for this client;
3160 * drop the notification and enqueue a "dropped
3161 * notification" message if this is the first dropped
3162 * notification since the socket spilled-over to the
3165 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
3167 if (!client
->communication
.outbound
.dropped_notification
) {
3168 client
->communication
.outbound
.dropped_notification
= true;
3169 ret
= client_enqueue_dropped_notification(
3178 ret
= lttng_dynamic_buffer_append_buffer(
3179 &client
->communication
.outbound
.buffer
,
3185 ret
= client_flush_outgoing_queue(client
, state
);
3192 lttng_dynamic_buffer_reset(&msg_buffer
);
3196 int handle_notification_thread_channel_sample(
3197 struct notification_thread_state
*state
, int pipe
,
3198 enum lttng_domain_type domain
)
3201 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3202 struct channel_info
*channel_info
;
3203 struct cds_lfht_node
*node
;
3204 struct cds_lfht_iter iter
;
3205 struct lttng_channel_trigger_list
*trigger_list
;
3206 struct lttng_trigger_list_element
*trigger_list_element
;
3207 bool previous_sample_available
= false;
3208 struct channel_state_sample previous_sample
, latest_sample
;
3209 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3212 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3213 * ensuring that read/write of sampling messages are atomic.
3215 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3216 if (ret
!= sizeof(sample_msg
)) {
3217 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3224 latest_sample
.key
.key
= sample_msg
.key
;
3225 latest_sample
.key
.domain
= domain
;
3226 latest_sample
.highest_usage
= sample_msg
.highest
;
3227 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3228 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3232 /* Retrieve the channel's informations */
3233 cds_lfht_lookup(state
->channels_ht
,
3234 hash_channel_key(&latest_sample
.key
),
3238 node
= cds_lfht_iter_get_node(&iter
);
3239 if (caa_unlikely(!node
)) {
3241 * Not an error since the consumer can push a sample to the pipe
3242 * and the rest of the session daemon could notify us of the
3243 * channel's destruction before we get a chance to process that
3246 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3247 latest_sample
.key
.key
,
3248 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3252 channel_info
= caa_container_of(node
, struct channel_info
,
3254 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3256 latest_sample
.key
.key
,
3257 channel_info
->session_info
->name
,
3258 latest_sample
.highest_usage
,
3259 latest_sample
.lowest_usage
,
3260 latest_sample
.channel_total_consumed
);
3262 previous_session_consumed_total
=
3263 channel_info
->session_info
->consumed_data_size
;
3265 /* Retrieve the channel's last sample, if it exists, and update it. */
3266 cds_lfht_lookup(state
->channel_state_ht
,
3267 hash_channel_key(&latest_sample
.key
),
3268 match_channel_state_sample
,
3271 node
= cds_lfht_iter_get_node(&iter
);
3272 if (caa_likely(node
)) {
3273 struct channel_state_sample
*stored_sample
;
3275 /* Update the sample stored. */
3276 stored_sample
= caa_container_of(node
,
3277 struct channel_state_sample
,
3278 channel_state_ht_node
);
3280 memcpy(&previous_sample
, stored_sample
,
3281 sizeof(previous_sample
));
3282 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3283 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3284 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3285 previous_sample_available
= true;
3287 latest_session_consumed_total
=
3288 previous_session_consumed_total
+
3289 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3292 * This is the channel's first sample, allocate space for and
3293 * store the new sample.
3295 struct channel_state_sample
*stored_sample
;
3297 stored_sample
= zmalloc(sizeof(*stored_sample
));
3298 if (!stored_sample
) {
3303 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3304 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3305 cds_lfht_add(state
->channel_state_ht
,
3306 hash_channel_key(&stored_sample
->key
),
3307 &stored_sample
->channel_state_ht_node
);
3309 latest_session_consumed_total
=
3310 previous_session_consumed_total
+
3311 latest_sample
.channel_total_consumed
;
3314 channel_info
->session_info
->consumed_data_size
=
3315 latest_session_consumed_total
;
3317 /* Find triggers associated with this channel. */
3318 cds_lfht_lookup(state
->channel_triggers_ht
,
3319 hash_channel_key(&latest_sample
.key
),
3320 match_channel_trigger_list
,
3323 node
= cds_lfht_iter_get_node(&iter
);
3324 if (caa_likely(!node
)) {
3328 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3329 channel_triggers_ht_node
);
3330 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3332 const struct lttng_condition
*condition
;
3333 const struct lttng_action
*action
;
3334 const struct lttng_trigger
*trigger
;
3335 struct notification_client_list
*client_list
;
3336 struct lttng_evaluation
*evaluation
= NULL
;
3338 trigger
= trigger_list_element
->trigger
;
3339 condition
= lttng_trigger_get_const_condition(trigger
);
3341 action
= lttng_trigger_get_const_action(trigger
);
3343 /* Notify actions are the only type currently supported. */
3344 assert(lttng_action_get_type_const(action
) ==
3345 LTTNG_ACTION_TYPE_NOTIFY
);
3348 * Check if any client is subscribed to the result of this
3351 client_list
= get_client_list_from_condition(state
, condition
);
3352 assert(client_list
);
3353 if (cds_list_empty(&client_list
->list
)) {
3355 * No clients interested in the evaluation's result,
3361 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3362 previous_sample_available
? &previous_sample
: NULL
,
3364 previous_session_consumed_total
,
3365 latest_session_consumed_total
,
3367 if (caa_unlikely(ret
)) {
3371 if (caa_likely(!evaluation
)) {
3375 /* Dispatch evaluation result to all clients. */
3376 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3377 evaluation
, client_list
, state
,
3378 channel_info
->session_info
->uid
,
3379 channel_info
->session_info
->gid
);
3380 lttng_evaluation_destroy(evaluation
);
3381 if (caa_unlikely(ret
)) {