2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/condition/session-consumed-size-internal.h>
36 #include <lttng/notification/channel-internal.h>
44 #include "notification-thread.h"
45 #include "notification-thread-events.h"
46 #include "notification-thread-commands.h"
47 #include "lttng-sessiond.h"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 struct lttng_trigger_list_element
{
54 struct lttng_trigger
*trigger
;
55 struct cds_list_head node
;
58 struct lttng_channel_trigger_list
{
59 struct channel_key channel_key
;
60 struct cds_list_head list
;
61 struct cds_lfht_node channel_triggers_ht_node
;
64 struct lttng_trigger_ht_element
{
65 struct lttng_trigger
*trigger
;
66 struct cds_lfht_node node
;
69 struct lttng_condition_list_element
{
70 struct lttng_condition
*condition
;
71 struct cds_list_head node
;
74 struct notification_client_list_element
{
75 struct notification_client
*client
;
76 struct cds_list_head node
;
79 struct notification_client_list
{
80 struct lttng_trigger
*trigger
;
81 struct cds_list_head list
;
82 struct cds_lfht_node notification_trigger_ht_node
;
85 struct notification_client
{
87 /* Client protocol version. */
92 * Indicates if the credentials and versions of the client have been
97 * Conditions to which the client's notification channel is subscribed.
98 * List of struct lttng_condition_list_node. The condition member is
99 * owned by the client.
101 struct cds_list_head condition_list
;
102 struct cds_lfht_node client_socket_ht_node
;
106 * During the reception of a message, the reception
107 * buffers' "size" is set to contain the current
108 * message's complete payload.
110 struct lttng_dynamic_buffer buffer
;
111 /* Bytes left to receive for the current message. */
112 size_t bytes_to_receive
;
113 /* Type of the message being received. */
114 enum lttng_notification_channel_message_type msg_type
;
116 * Indicates whether or not credentials are expected
121 * Indicates whether or not credentials were received
125 /* Only used during credentials reception. */
126 lttng_sock_cred creds
;
130 * Indicates whether or not a notification addressed to
131 * this client was dropped because a command reply was
134 * A notification is dropped whenever the buffer is not
137 bool dropped_notification
;
139 * Indicates whether or not a command reply is already
140 * buffered. In this case, it means that the client is
141 * not consuming command replies before emitting a new
142 * one. This could be caused by a protocol error or a
143 * misbehaving/malicious client.
145 bool queued_command_reply
;
146 struct lttng_dynamic_buffer buffer
;
151 struct channel_state_sample
{
152 struct channel_key key
;
153 struct cds_lfht_node channel_state_ht_node
;
154 uint64_t highest_usage
;
155 uint64_t lowest_usage
;
156 uint64_t channel_total_consumed
;
159 static unsigned long hash_channel_key(struct channel_key
*key
);
160 static int evaluate_condition(const struct lttng_condition
*condition
,
161 struct lttng_evaluation
**evaluation
,
162 const struct notification_thread_state
*state
,
163 const struct channel_state_sample
*previous_sample
,
164 const struct channel_state_sample
*latest_sample
,
165 uint64_t previous_session_consumed_total
,
166 uint64_t latest_session_consumed_total
,
167 struct channel_info
*channel_info
);
169 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
170 const struct lttng_evaluation
*evaluation
,
171 struct notification_client_list
*client_list
,
172 struct notification_thread_state
*state
,
173 uid_t channel_uid
, gid_t channel_gid
);
177 void session_info_destroy(void *_data
);
179 void session_info_get(struct session_info
*session_info
);
181 void session_info_put(struct session_info
*session_info
);
183 struct session_info
*session_info_create(const char *name
,
184 uid_t uid
, gid_t gid
);
186 void session_info_add_channel(struct session_info
*session_info
,
187 struct channel_info
*channel_info
);
189 void session_info_remove_channel(struct session_info
*session_info
,
190 struct channel_info
*channel_info
);
193 int match_client(struct cds_lfht_node
*node
, const void *key
)
195 /* This double-cast is intended to supress pointer-to-cast warning. */
196 int socket
= (int) (intptr_t) key
;
197 struct notification_client
*client
;
199 client
= caa_container_of(node
, struct notification_client
,
200 client_socket_ht_node
);
202 return !!(client
->socket
== socket
);
206 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
208 struct channel_key
*channel_key
= (struct channel_key
*) key
;
209 struct lttng_channel_trigger_list
*trigger_list
;
211 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
212 channel_triggers_ht_node
);
214 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
215 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
219 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
221 struct channel_key
*channel_key
= (struct channel_key
*) key
;
222 struct channel_state_sample
*sample
;
224 sample
= caa_container_of(node
, struct channel_state_sample
,
225 channel_state_ht_node
);
227 return !!((channel_key
->key
== sample
->key
.key
) &&
228 (channel_key
->domain
== sample
->key
.domain
));
232 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
234 struct channel_key
*channel_key
= (struct channel_key
*) key
;
235 struct channel_info
*channel_info
;
237 channel_info
= caa_container_of(node
, struct channel_info
,
240 return !!((channel_key
->key
== channel_info
->key
.key
) &&
241 (channel_key
->domain
== channel_info
->key
.domain
));
245 int match_condition(struct cds_lfht_node
*node
, const void *key
)
247 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
248 struct lttng_trigger_ht_element
*trigger
;
249 struct lttng_condition
*condition
;
251 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
253 condition
= lttng_trigger_get_condition(trigger
->trigger
);
256 return !!lttng_condition_is_equal(condition_key
, condition
);
260 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
262 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
263 struct notification_client_list
*client_list
;
264 struct lttng_condition
*condition
;
265 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
268 assert(condition_key
);
270 client_list
= caa_container_of(node
, struct notification_client_list
,
271 notification_trigger_ht_node
);
272 condition
= lttng_trigger_get_condition(client_list
->trigger
);
274 return !!lttng_condition_is_equal(condition_key
, condition
);
278 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
280 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
281 struct notification_client_list
*client_list
;
282 struct lttng_condition
*condition
;
284 assert(condition_key
);
286 client_list
= caa_container_of(node
, struct notification_client_list
,
287 notification_trigger_ht_node
);
288 condition
= lttng_trigger_get_condition(client_list
->trigger
);
290 return !!lttng_condition_is_equal(condition_key
, condition
);
294 unsigned long lttng_condition_buffer_usage_hash(
295 const struct lttng_condition
*_condition
)
298 unsigned long condition_type
;
299 struct lttng_condition_buffer_usage
*condition
;
301 condition
= container_of(_condition
,
302 struct lttng_condition_buffer_usage
, parent
);
304 condition_type
= (unsigned long) condition
->parent
.type
;
305 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
306 if (condition
->session_name
) {
307 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
309 if (condition
->channel_name
) {
310 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
312 if (condition
->domain
.set
) {
313 hash
^= hash_key_ulong(
314 (void *) condition
->domain
.type
,
317 if (condition
->threshold_ratio
.set
) {
320 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
321 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
322 } else if (condition
->threshold_bytes
.set
) {
325 val
= condition
->threshold_bytes
.value
;
326 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
332 unsigned long lttng_condition_session_consumed_size_hash(
333 const struct lttng_condition
*_condition
)
336 unsigned long condition_type
=
337 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
338 struct lttng_condition_session_consumed_size
*condition
;
341 condition
= container_of(_condition
,
342 struct lttng_condition_session_consumed_size
, parent
);
344 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
345 if (condition
->session_name
) {
346 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
348 val
= condition
->consumed_threshold_bytes
.value
;
349 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
355 * The lttng_condition hashing code is kept in this file (rather than
356 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
357 * don't want to link in liblttng-ctl.
360 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
362 switch (condition
->type
) {
363 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
364 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
365 return lttng_condition_buffer_usage_hash(condition
);
366 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
367 return lttng_condition_session_consumed_size_hash(condition
);
369 ERR("[notification-thread] Unexpected condition type caught");
375 unsigned long hash_channel_key(struct channel_key
*key
)
377 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
378 unsigned long domain_hash
= hash_key_ulong(
379 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
381 return key_hash
^ domain_hash
;
385 void channel_info_destroy(struct channel_info
*channel_info
)
391 if (channel_info
->session_info
) {
392 session_info_remove_channel(channel_info
->session_info
,
394 session_info_put(channel_info
->session_info
);
396 if (channel_info
->name
) {
397 free(channel_info
->name
);
402 /* Don't call directly, use the ref-counting mechanism. */
404 void session_info_destroy(void *_data
)
406 struct session_info
*session_info
= _data
;
409 assert(session_info
);
410 if (session_info
->channel_infos_ht
) {
411 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
413 ERR("[notification-thread] Failed to destroy channel information hash table");
416 free(session_info
->name
);
421 void session_info_get(struct session_info
*session_info
)
426 lttng_ref_get(&session_info
->ref
);
430 void session_info_put(struct session_info
*session_info
)
435 lttng_ref_put(&session_info
->ref
);
439 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
)
441 struct session_info
*session_info
;
445 session_info
= zmalloc(sizeof(*session_info
));
449 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
451 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
452 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
453 if (!session_info
->channel_infos_ht
) {
457 cds_lfht_node_init(&session_info
->sessions_ht_node
);
458 session_info
->name
= strdup(name
);
459 if (!session_info
->name
) {
462 session_info
->uid
= uid
;
463 session_info
->gid
= gid
;
467 session_info_put(session_info
);
472 void session_info_add_channel(struct session_info
*session_info
,
473 struct channel_info
*channel_info
)
476 cds_lfht_add(session_info
->channel_infos_ht
,
477 hash_channel_key(&channel_info
->key
),
478 &channel_info
->session_info_channels_ht_node
);
483 void session_info_remove_channel(struct session_info
*session_info
,
484 struct channel_info
*channel_info
)
487 cds_lfht_del(session_info
->channel_infos_ht
,
488 &channel_info
->session_info_channels_ht_node
);
493 struct channel_info
*channel_info_create(const char *channel_name
,
494 struct channel_key
*channel_key
, uint64_t channel_capacity
,
495 struct session_info
*session_info
)
497 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
503 cds_lfht_node_init(&channel_info
->channels_ht_node
);
504 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
505 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
506 channel_info
->capacity
= channel_capacity
;
508 channel_info
->name
= strdup(channel_name
);
509 if (!channel_info
->name
) {
514 * Set the references between session and channel infos:
515 * - channel_info holds a strong reference to session_info
516 * - session_info holds a weak reference to channel_info
518 session_info_get(session_info
);
519 session_info_add_channel(session_info
, channel_info
);
520 channel_info
->session_info
= session_info
;
524 channel_info_destroy(channel_info
);
528 /* This function must be called with the RCU read lock held. */
530 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
531 struct lttng_condition
*condition
,
532 struct notification_client
*client
,
533 struct notification_thread_state
*state
)
536 struct cds_lfht_iter iter
;
537 struct cds_lfht_node
*node
;
538 struct channel_info
*channel_info
= NULL
;
539 struct channel_key
*channel_key
= NULL
;
540 struct channel_state_sample
*last_sample
= NULL
;
541 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
542 struct lttng_evaluation
*evaluation
= NULL
;
543 struct notification_client_list client_list
= { 0 };
544 struct notification_client_list_element client_list_element
= { 0 };
551 /* Find the channel associated with the trigger. */
552 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
553 channel_trigger_list
, channel_triggers_ht_node
) {
554 struct lttng_trigger_list_element
*element
;
556 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
557 const struct lttng_condition
*current_condition
=
558 lttng_trigger_get_const_condition(
561 assert(current_condition
);
562 if (!lttng_condition_is_equal(condition
,
563 current_condition
)) {
567 /* Found the trigger, save the channel key. */
568 channel_key
= &channel_trigger_list
->channel_key
;
572 /* The channel key was found stop iteration. */
578 /* No channel found; normal exit. */
579 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
584 /* Fetch channel info for the matching channel. */
585 cds_lfht_lookup(state
->channels_ht
,
586 hash_channel_key(channel_key
),
590 node
= cds_lfht_iter_get_node(&iter
);
592 channel_info
= caa_container_of(node
, struct channel_info
,
595 /* Retrieve the channel's last sample, if it exists. */
596 cds_lfht_lookup(state
->channel_state_ht
,
597 hash_channel_key(channel_key
),
598 match_channel_state_sample
,
601 node
= cds_lfht_iter_get_node(&iter
);
603 last_sample
= caa_container_of(node
,
604 struct channel_state_sample
,
605 channel_state_ht_node
);
607 /* Nothing to evaluate, no sample was ever taken. Normal exit */
608 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
613 ret
= evaluate_condition(condition
, &evaluation
, state
,
615 0, channel_info
->session_info
->consumed_data_size
,
618 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
623 /* Evaluation yielded nothing. Normal exit. */
624 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
630 * Create a temporary client list with the client currently
633 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
634 CDS_INIT_LIST_HEAD(&client_list
.list
);
635 client_list
.trigger
= trigger
;
637 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
638 client_list_element
.client
= client
;
639 cds_list_add(&client_list_element
.node
, &client_list
.list
);
641 /* Send evaluation result to the newly-subscribed client. */
642 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
643 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
644 state
, channel_info
->session_info
->uid
,
645 channel_info
->session_info
->gid
);
652 int notification_thread_client_subscribe(struct notification_client
*client
,
653 struct lttng_condition
*condition
,
654 struct notification_thread_state
*state
,
655 enum lttng_notification_channel_status
*_status
)
658 struct cds_lfht_iter iter
;
659 struct cds_lfht_node
*node
;
660 struct notification_client_list
*client_list
;
661 struct lttng_condition_list_element
*condition_list_element
= NULL
;
662 struct notification_client_list_element
*client_list_element
= NULL
;
663 enum lttng_notification_channel_status status
=
664 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
667 * Ensure that the client has not already subscribed to this condition
670 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
671 if (lttng_condition_is_equal(condition_list_element
->condition
,
673 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
678 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
679 if (!condition_list_element
) {
683 client_list_element
= zmalloc(sizeof(*client_list_element
));
684 if (!client_list_element
) {
692 * Add the newly-subscribed condition to the client's subscription list.
694 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
695 condition_list_element
->condition
= condition
;
696 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
698 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
699 lttng_condition_hash(condition
),
700 match_client_list_condition
,
703 node
= cds_lfht_iter_get_node(&iter
);
706 * No notification-emiting trigger registered with this
707 * condition. We don't evaluate the condition right away
708 * since this trigger is not registered yet.
710 free(client_list_element
);
714 client_list
= caa_container_of(node
, struct notification_client_list
,
715 notification_trigger_ht_node
);
717 * The condition to which the client just subscribed is evaluated
718 * at this point so that conditions that are already TRUE result
719 * in a notification being sent out.
721 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
723 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
725 free(client_list_element
);
730 * Add the client to the list of clients interested in a given trigger
731 * if a "notification" trigger with a corresponding condition was
734 client_list_element
->client
= client
;
735 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
736 cds_list_add(&client_list_element
->node
, &client_list
->list
);
745 free(condition_list_element
);
746 free(client_list_element
);
751 int notification_thread_client_unsubscribe(
752 struct notification_client
*client
,
753 struct lttng_condition
*condition
,
754 struct notification_thread_state
*state
,
755 enum lttng_notification_channel_status
*_status
)
757 struct cds_lfht_iter iter
;
758 struct cds_lfht_node
*node
;
759 struct notification_client_list
*client_list
;
760 struct lttng_condition_list_element
*condition_list_element
,
762 struct notification_client_list_element
*client_list_element
,
764 bool condition_found
= false;
765 enum lttng_notification_channel_status status
=
766 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
768 /* Remove the condition from the client's condition list. */
769 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
770 &client
->condition_list
, node
) {
771 if (!lttng_condition_is_equal(condition_list_element
->condition
,
776 cds_list_del(&condition_list_element
->node
);
778 * The caller may be iterating on the client's conditions to
779 * tear down a client's connection. In this case, the condition
780 * will be destroyed at the end.
782 if (condition
!= condition_list_element
->condition
) {
783 lttng_condition_destroy(
784 condition_list_element
->condition
);
786 free(condition_list_element
);
787 condition_found
= true;
791 if (!condition_found
) {
792 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
797 * Remove the client from the list of clients interested the trigger
798 * matching the condition.
801 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
802 lttng_condition_hash(condition
),
803 match_client_list_condition
,
806 node
= cds_lfht_iter_get_node(&iter
);
811 client_list
= caa_container_of(node
, struct notification_client_list
,
812 notification_trigger_ht_node
);
813 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
814 &client_list
->list
, node
) {
815 if (client_list_element
->client
->socket
!= client
->socket
) {
818 cds_list_del(&client_list_element
->node
);
819 free(client_list_element
);
825 lttng_condition_destroy(condition
);
833 void notification_client_destroy(struct notification_client
*client
,
834 struct notification_thread_state
*state
)
836 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
842 /* Release all conditions to which the client was subscribed. */
843 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
844 &client
->condition_list
, node
) {
845 (void) notification_thread_client_unsubscribe(client
,
846 condition_list_element
->condition
, state
, NULL
);
849 if (client
->socket
>= 0) {
850 (void) lttcomm_close_unix_sock(client
->socket
);
852 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
853 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
858 * Call with rcu_read_lock held (and hold for the lifetime of the returned
862 struct notification_client
*get_client_from_socket(int socket
,
863 struct notification_thread_state
*state
)
865 struct cds_lfht_iter iter
;
866 struct cds_lfht_node
*node
;
867 struct notification_client
*client
= NULL
;
869 cds_lfht_lookup(state
->client_socket_ht
,
870 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
872 (void *) (unsigned long) socket
,
874 node
= cds_lfht_iter_get_node(&iter
);
879 client
= caa_container_of(node
, struct notification_client
,
880 client_socket_ht_node
);
886 bool buffer_usage_condition_applies_to_channel(
887 struct lttng_condition
*condition
,
888 struct channel_info
*channel_info
)
890 enum lttng_condition_status status
;
891 enum lttng_domain_type condition_domain
;
892 const char *condition_session_name
= NULL
;
893 const char *condition_channel_name
= NULL
;
895 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
897 assert(status
== LTTNG_CONDITION_STATUS_OK
);
898 if (channel_info
->key
.domain
!= condition_domain
) {
902 status
= lttng_condition_buffer_usage_get_session_name(
903 condition
, &condition_session_name
);
904 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
906 status
= lttng_condition_buffer_usage_get_channel_name(
907 condition
, &condition_channel_name
);
908 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
910 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
913 if (strcmp(channel_info
->name
, condition_channel_name
)) {
923 bool session_consumed_size_condition_applies_to_channel(
924 struct lttng_condition
*condition
,
925 struct channel_info
*channel_info
)
927 enum lttng_condition_status status
;
928 const char *condition_session_name
= NULL
;
930 status
= lttng_condition_session_consumed_size_get_session_name(
931 condition
, &condition_session_name
);
932 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
934 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
944 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
945 struct channel_info
*channel_info
)
947 struct lttng_condition
*condition
;
948 bool trigger_applies
;
950 condition
= lttng_trigger_get_condition(trigger
);
955 switch (lttng_condition_get_type(condition
)) {
956 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
957 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
958 trigger_applies
= buffer_usage_condition_applies_to_channel(
959 condition
, channel_info
);
961 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
962 trigger_applies
= session_consumed_size_condition_applies_to_channel(
963 condition
, channel_info
);
969 return trigger_applies
;
975 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
976 struct notification_client
*client
)
978 bool applies
= false;
979 struct lttng_condition_list_element
*condition_list_element
;
981 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
983 applies
= lttng_condition_is_equal(
984 condition_list_element
->condition
,
985 lttng_trigger_get_condition(trigger
));
994 int match_session(struct cds_lfht_node
*node
, const void *key
)
996 const char *name
= key
;
997 struct session_info
*session_info
= caa_container_of(
998 node
, struct session_info
, sessions_ht_node
);
1000 return !strcmp(session_info
->name
, name
);
1004 struct session_info
*find_or_create_session_info(
1005 struct notification_thread_state
*state
,
1006 const char *name
, uid_t uid
, gid_t gid
)
1008 struct session_info
*session
= NULL
;
1009 struct cds_lfht_node
*node
;
1010 struct cds_lfht_iter iter
;
1013 cds_lfht_lookup(state
->sessions_ht
,
1014 hash_key_str(name
, lttng_ht_seed
),
1018 node
= cds_lfht_iter_get_node(&iter
);
1020 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1022 session
= caa_container_of(node
, struct session_info
,
1024 assert(session
->uid
== uid
);
1025 assert(session
->gid
== gid
);
1029 session
= session_info_create(name
, uid
, gid
);
1031 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1036 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1037 &session
->sessions_ht_node
);
1044 int handle_notification_thread_command_add_channel(
1045 struct notification_thread_state
*state
,
1046 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1047 const char *channel_name
, enum lttng_domain_type channel_domain
,
1048 uint64_t channel_key_int
, uint64_t channel_capacity
,
1049 enum lttng_error_code
*cmd_result
)
1051 struct cds_list_head trigger_list
;
1052 struct channel_info
*new_channel_info
= NULL
;
1053 struct channel_key channel_key
= {
1054 .key
= channel_key_int
,
1055 .domain
= channel_domain
,
1057 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1058 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1059 int trigger_count
= 0;
1060 struct cds_lfht_iter iter
;
1061 struct session_info
*session_info
= NULL
;
1063 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1064 channel_name
, session_name
, channel_key_int
,
1065 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1067 CDS_INIT_LIST_HEAD(&trigger_list
);
1069 session_info
= find_or_create_session_info(state
, session_name
,
1070 session_uid
, session_gid
);
1071 if (!session_info
) {
1072 /* Allocation error or an internal error occured. */
1076 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1077 channel_capacity
, session_info
);
1078 if (!new_channel_info
) {
1082 /* Build a list of all triggers applying to the new channel. */
1083 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1085 struct lttng_trigger_list_element
*new_element
;
1087 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1088 new_channel_info
)) {
1092 new_element
= zmalloc(sizeof(*new_element
));
1096 CDS_INIT_LIST_HEAD(&new_element
->node
);
1097 new_element
->trigger
= trigger_ht_element
->trigger
;
1098 cds_list_add(&new_element
->node
, &trigger_list
);
1102 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1104 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1105 if (!channel_trigger_list
) {
1108 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1109 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1110 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1111 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1114 /* Add channel to the channel_ht which owns the channel_infos. */
1115 cds_lfht_add(state
->channels_ht
,
1116 hash_channel_key(&new_channel_info
->key
),
1117 &new_channel_info
->channels_ht_node
);
1119 * Add the list of triggers associated with this channel to the
1120 * channel_triggers_ht.
1122 cds_lfht_add(state
->channel_triggers_ht
,
1123 hash_channel_key(&new_channel_info
->key
),
1124 &channel_trigger_list
->channel_triggers_ht_node
);
1126 *cmd_result
= LTTNG_OK
;
1129 channel_info_destroy(new_channel_info
);
1130 session_info_put(session_info
);
1135 int handle_notification_thread_command_remove_channel(
1136 struct notification_thread_state
*state
,
1137 uint64_t channel_key
, enum lttng_domain_type domain
,
1138 enum lttng_error_code
*cmd_result
)
1140 struct cds_lfht_node
*node
;
1141 struct cds_lfht_iter iter
;
1142 struct lttng_channel_trigger_list
*trigger_list
;
1143 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1144 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1145 struct channel_info
*channel_info
;
1147 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1148 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1152 cds_lfht_lookup(state
->channel_triggers_ht
,
1153 hash_channel_key(&key
),
1154 match_channel_trigger_list
,
1157 node
= cds_lfht_iter_get_node(&iter
);
1159 * There is a severe internal error if we are being asked to remove a
1160 * channel that doesn't exist.
1163 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1167 /* Free the list of triggers associated with this channel. */
1168 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1169 channel_triggers_ht_node
);
1170 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1171 &trigger_list
->list
, node
) {
1172 cds_list_del(&trigger_list_element
->node
);
1173 free(trigger_list_element
);
1175 cds_lfht_del(state
->channel_triggers_ht
, node
);
1178 /* Free sampled channel state. */
1179 cds_lfht_lookup(state
->channel_state_ht
,
1180 hash_channel_key(&key
),
1181 match_channel_state_sample
,
1184 node
= cds_lfht_iter_get_node(&iter
);
1186 * This is expected to be NULL if the channel is destroyed before we
1187 * received a sample.
1190 struct channel_state_sample
*sample
= caa_container_of(node
,
1191 struct channel_state_sample
,
1192 channel_state_ht_node
);
1194 cds_lfht_del(state
->channel_state_ht
, node
);
1198 /* Remove the channel from the channels_ht and free it. */
1199 cds_lfht_lookup(state
->channels_ht
,
1200 hash_channel_key(&key
),
1204 node
= cds_lfht_iter_get_node(&iter
);
1206 channel_info
= caa_container_of(node
, struct channel_info
,
1208 cds_lfht_del(state
->channels_ht
, node
);
1209 channel_info_destroy(channel_info
);
1212 *cmd_result
= LTTNG_OK
;
1217 int condition_is_supported(struct lttng_condition
*condition
)
1221 switch (lttng_condition_get_type(condition
)) {
1222 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1223 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1225 enum lttng_domain_type domain
;
1227 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1234 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1240 * Older kernel tracers don't expose the API to monitor their
1241 * buffers. Therefore, we reject triggers that require that
1242 * mechanism to be available to be evaluated.
1244 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1256 * FIXME A client's credentials are not checked when registering a trigger, nor
1257 * are they stored alongside with the trigger.
1259 * The effects of this are benign since:
1260 * - The client will succeed in registering the trigger, as it is valid,
1261 * - The trigger will, internally, be bound to the channel,
1262 * - The notifications will not be sent since the client's credentials
1263 * are checked against the channel at that moment.
1265 * If this function returns a non-zero value, it means something is
1266 * fundamentally broken and the whole subsystem/thread will be torn down.
1268 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1272 int handle_notification_thread_command_register_trigger(
1273 struct notification_thread_state
*state
,
1274 struct lttng_trigger
*trigger
,
1275 enum lttng_error_code
*cmd_result
)
1278 struct lttng_condition
*condition
;
1279 struct notification_client
*client
;
1280 struct notification_client_list
*client_list
= NULL
;
1281 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1282 struct notification_client_list_element
*client_list_element
, *tmp
;
1283 struct cds_lfht_node
*node
;
1284 struct cds_lfht_iter iter
;
1285 struct channel_info
*channel
;
1286 bool free_trigger
= true;
1290 condition
= lttng_trigger_get_condition(trigger
);
1293 ret
= condition_is_supported(condition
);
1296 } else if (ret
== 0) {
1297 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1300 /* Feature is supported, continue. */
1304 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1305 if (!trigger_ht_element
) {
1310 /* Add trigger to the trigger_ht. */
1311 cds_lfht_node_init(&trigger_ht_element
->node
);
1312 trigger_ht_element
->trigger
= trigger
;
1314 node
= cds_lfht_add_unique(state
->triggers_ht
,
1315 lttng_condition_hash(condition
),
1318 &trigger_ht_element
->node
);
1319 if (node
!= &trigger_ht_element
->node
) {
1320 /* Not a fatal error, simply report it to the client. */
1321 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1322 goto error_free_ht_element
;
1326 * Ownership of the trigger and of its wrapper was transfered to
1329 trigger_ht_element
= NULL
;
1330 free_trigger
= false;
1333 * The rest only applies to triggers that have a "notify" action.
1334 * It is not skipped as this is the only action type currently
1337 client_list
= zmalloc(sizeof(*client_list
));
1340 goto error_free_ht_element
;
1342 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1343 CDS_INIT_LIST_HEAD(&client_list
->list
);
1344 client_list
->trigger
= trigger
;
1346 /* Build a list of clients to which this new trigger applies. */
1347 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1348 client_socket_ht_node
) {
1349 if (!trigger_applies_to_client(trigger
, client
)) {
1353 client_list_element
= zmalloc(sizeof(*client_list_element
));
1354 if (!client_list_element
) {
1356 goto error_free_client_list
;
1358 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1359 client_list_element
->client
= client
;
1360 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1363 cds_lfht_add(state
->notification_trigger_clients_ht
,
1364 lttng_condition_hash(condition
),
1365 &client_list
->notification_trigger_ht_node
);
1368 * Add the trigger to list of triggers bound to the channels currently
1371 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1373 struct lttng_trigger_list_element
*trigger_list_element
;
1374 struct lttng_channel_trigger_list
*trigger_list
;
1376 if (!trigger_applies_to_channel(trigger
, channel
)) {
1380 cds_lfht_lookup(state
->channel_triggers_ht
,
1381 hash_channel_key(&channel
->key
),
1382 match_channel_trigger_list
,
1385 node
= cds_lfht_iter_get_node(&iter
);
1387 trigger_list
= caa_container_of(node
,
1388 struct lttng_channel_trigger_list
,
1389 channel_triggers_ht_node
);
1391 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1392 if (!trigger_list_element
) {
1394 goto error_free_client_list
;
1396 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1397 trigger_list_element
->trigger
= trigger
;
1398 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1402 * Since there is nothing preventing clients from subscribing to a
1403 * condition before the corresponding trigger is registered, we have
1404 * to evaluate this new condition right away.
1406 * At some point, we were waiting for the next "evaluation" (e.g. on
1407 * reception of a channel sample) to evaluate this new condition, but
1410 * The reason it was broken is that waiting for the next sample
1411 * does not allow us to properly handle transitions for edge-triggered
1414 * Consider this example: when we handle a new channel sample, we
1415 * evaluate each conditions twice: once with the previous state, and
1416 * again with the newest state. We then use those two results to
1417 * determine whether a state change happened: a condition was false and
1418 * became true. If a state change happened, we have to notify clients.
1420 * Now, if a client subscribes to a given notification and registers
1421 * a trigger *after* that subscription, we have to make sure the
1422 * condition is evaluated at this point while considering only the
1423 * current state. Otherwise, the next evaluation cycle may only see
1424 * that the evaluations remain the same (true for samples n-1 and n) and
1425 * the client will never know that the condition has been met.
1427 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1428 &client_list
->list
, node
) {
1429 ret
= evaluate_condition_for_client(trigger
, condition
,
1430 client_list_element
->client
, state
);
1432 goto error_free_client_list
;
1437 * Client list ownership transferred to the
1438 * notification_trigger_clients_ht.
1442 *cmd_result
= LTTNG_OK
;
1443 error_free_client_list
:
1445 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1446 &client_list
->list
, node
) {
1447 free(client_list_element
);
1451 error_free_ht_element
:
1452 free(trigger_ht_element
);
1455 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1457 lttng_condition_destroy(condition
);
1458 lttng_action_destroy(action
);
1459 lttng_trigger_destroy(trigger
);
1466 int handle_notification_thread_command_unregister_trigger(
1467 struct notification_thread_state
*state
,
1468 struct lttng_trigger
*trigger
,
1469 enum lttng_error_code
*_cmd_reply
)
1471 struct cds_lfht_iter iter
;
1472 struct cds_lfht_node
*node
, *triggers_ht_node
;
1473 struct lttng_channel_trigger_list
*trigger_list
;
1474 struct notification_client_list
*client_list
;
1475 struct notification_client_list_element
*client_list_element
, *tmp
;
1476 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1477 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1479 struct lttng_action
*action
;
1480 enum lttng_error_code cmd_reply
;
1484 cds_lfht_lookup(state
->triggers_ht
,
1485 lttng_condition_hash(condition
),
1489 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1490 if (!triggers_ht_node
) {
1491 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1494 cmd_reply
= LTTNG_OK
;
1497 /* Remove trigger from channel_triggers_ht. */
1498 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1499 channel_triggers_ht_node
) {
1500 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1502 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1503 &trigger_list
->list
, node
) {
1504 const struct lttng_condition
*current_condition
=
1505 lttng_trigger_get_const_condition(
1506 trigger_element
->trigger
);
1508 assert(current_condition
);
1509 if (!lttng_condition_is_equal(condition
,
1510 current_condition
)) {
1514 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1515 cds_list_del(&trigger_element
->node
);
1516 /* A trigger can only appear once per channel */
1522 * Remove and release the client list from
1523 * notification_trigger_clients_ht.
1525 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1526 lttng_condition_hash(condition
),
1530 node
= cds_lfht_iter_get_node(&iter
);
1532 client_list
= caa_container_of(node
, struct notification_client_list
,
1533 notification_trigger_ht_node
);
1534 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1535 &client_list
->list
, node
) {
1536 free(client_list_element
);
1538 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1541 /* Remove trigger from triggers_ht. */
1542 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1543 struct lttng_trigger_ht_element
, node
);
1544 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1546 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1547 lttng_condition_destroy(condition
);
1548 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1549 lttng_action_destroy(action
);
1550 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1551 free(trigger_ht_element
);
1555 *_cmd_reply
= cmd_reply
;
1560 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1561 int handle_notification_thread_command(
1562 struct notification_thread_handle
*handle
,
1563 struct notification_thread_state
*state
)
1567 struct notification_thread_command
*cmd
;
1569 /* Read the event pipe to put it back into a quiescent state. */
1570 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1576 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1577 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1578 struct notification_thread_command
, cmd_list_node
);
1579 switch (cmd
->type
) {
1580 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1581 DBG("[notification-thread] Received register trigger command");
1582 ret
= handle_notification_thread_command_register_trigger(
1583 state
, cmd
->parameters
.trigger
,
1586 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1587 DBG("[notification-thread] Received unregister trigger command");
1588 ret
= handle_notification_thread_command_unregister_trigger(
1589 state
, cmd
->parameters
.trigger
,
1592 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1593 DBG("[notification-thread] Received add channel command");
1594 ret
= handle_notification_thread_command_add_channel(
1596 cmd
->parameters
.add_channel
.session
.name
,
1597 cmd
->parameters
.add_channel
.session
.uid
,
1598 cmd
->parameters
.add_channel
.session
.gid
,
1599 cmd
->parameters
.add_channel
.channel
.name
,
1600 cmd
->parameters
.add_channel
.channel
.domain
,
1601 cmd
->parameters
.add_channel
.channel
.key
,
1602 cmd
->parameters
.add_channel
.channel
.capacity
,
1605 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1606 DBG("[notification-thread] Received remove channel command");
1607 ret
= handle_notification_thread_command_remove_channel(
1608 state
, cmd
->parameters
.remove_channel
.key
,
1609 cmd
->parameters
.remove_channel
.domain
,
1612 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1613 DBG("[notification-thread] Received quit command");
1614 cmd
->reply_code
= LTTNG_OK
;
1618 ERR("[notification-thread] Unknown internal command received");
1626 cds_list_del(&cmd
->cmd_list_node
);
1627 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1628 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1631 /* Wake-up and return a fatal error to the calling thread. */
1632 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1633 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1634 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1636 /* Indicate a fatal error to the caller. */
1641 unsigned long hash_client_socket(int socket
)
1643 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1647 int socket_set_non_blocking(int socket
)
1651 /* Set the pipe as non-blocking. */
1652 ret
= fcntl(socket
, F_GETFL
, 0);
1654 PERROR("fcntl get socket flags");
1659 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1661 PERROR("fcntl set O_NONBLOCK socket flag");
1664 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1670 int client_reset_inbound_state(struct notification_client
*client
)
1674 ret
= lttng_dynamic_buffer_set_size(
1675 &client
->communication
.inbound
.buffer
, 0);
1678 client
->communication
.inbound
.bytes_to_receive
=
1679 sizeof(struct lttng_notification_channel_message
);
1680 client
->communication
.inbound
.msg_type
=
1681 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1682 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1683 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1684 ret
= lttng_dynamic_buffer_set_size(
1685 &client
->communication
.inbound
.buffer
,
1686 client
->communication
.inbound
.bytes_to_receive
);
1690 int handle_notification_thread_client_connect(
1691 struct notification_thread_state
*state
)
1694 struct notification_client
*client
;
1696 DBG("[notification-thread] Handling new notification channel client connection");
1698 client
= zmalloc(sizeof(*client
));
1704 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1705 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1706 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1707 client
->communication
.inbound
.expect_creds
= true;
1708 ret
= client_reset_inbound_state(client
);
1710 ERR("[notification-thread] Failed to reset client communication's inbound state");
1715 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1717 ERR("[notification-thread] Failed to accept new notification channel client connection");
1722 client
->socket
= ret
;
1724 ret
= socket_set_non_blocking(client
->socket
);
1726 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1730 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1732 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1737 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1738 LPOLLIN
| LPOLLERR
|
1739 LPOLLHUP
| LPOLLRDHUP
);
1741 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1745 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1749 cds_lfht_add(state
->client_socket_ht
,
1750 hash_client_socket(client
->socket
),
1751 &client
->client_socket_ht_node
);
1756 notification_client_destroy(client
, state
);
1760 int handle_notification_thread_client_disconnect(
1762 struct notification_thread_state
*state
)
1765 struct notification_client
*client
;
1768 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1770 client
= get_client_from_socket(client_socket
, state
);
1772 /* Internal state corruption, fatal error. */
1773 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1779 ret
= lttng_poll_del(&state
->events
, client_socket
);
1781 ERR("[notification-thread] Failed to remove client socket from poll set");
1783 cds_lfht_del(state
->client_socket_ht
,
1784 &client
->client_socket_ht_node
);
1785 notification_client_destroy(client
, state
);
1791 int handle_notification_thread_client_disconnect_all(
1792 struct notification_thread_state
*state
)
1794 struct cds_lfht_iter iter
;
1795 struct notification_client
*client
;
1796 bool error_encoutered
= false;
1799 DBG("[notification-thread] Closing all client connections");
1800 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1801 client_socket_ht_node
) {
1804 ret
= handle_notification_thread_client_disconnect(
1805 client
->socket
, state
);
1807 error_encoutered
= true;
1811 return error_encoutered
? 1 : 0;
1814 int handle_notification_thread_trigger_unregister_all(
1815 struct notification_thread_state
*state
)
1817 bool error_occurred
= false;
1818 struct cds_lfht_iter iter
;
1819 struct lttng_trigger_ht_element
*trigger_ht_element
;
1821 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1823 int ret
= handle_notification_thread_command_unregister_trigger(
1824 state
, trigger_ht_element
->trigger
, NULL
);
1826 error_occurred
= true;
1829 return error_occurred
? -1 : 0;
1833 int client_flush_outgoing_queue(struct notification_client
*client
,
1834 struct notification_thread_state
*state
)
1837 size_t to_send_count
;
1839 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1840 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1841 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1844 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1845 client
->communication
.outbound
.buffer
.data
,
1847 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1848 (ret
> 0 && ret
< to_send_count
)) {
1849 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1851 to_send_count
-= max(ret
, 0);
1853 memcpy(client
->communication
.outbound
.buffer
.data
,
1854 client
->communication
.outbound
.buffer
.data
+
1855 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1857 ret
= lttng_dynamic_buffer_set_size(
1858 &client
->communication
.outbound
.buffer
,
1865 * We want to be notified whenever there is buffer space
1866 * available to send the rest of the payload.
1868 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1869 CLIENT_POLL_MASK_IN_OUT
);
1873 } else if (ret
< 0) {
1874 /* Generic error, disconnect the client. */
1875 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1877 ret
= handle_notification_thread_client_disconnect(
1878 client
->socket
, state
);
1883 /* No error and flushed the queue completely. */
1884 ret
= lttng_dynamic_buffer_set_size(
1885 &client
->communication
.outbound
.buffer
, 0);
1889 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1890 CLIENT_POLL_MASK_IN
);
1895 client
->communication
.outbound
.queued_command_reply
= false;
1896 client
->communication
.outbound
.dropped_notification
= false;
1905 int client_send_command_reply(struct notification_client
*client
,
1906 struct notification_thread_state
*state
,
1907 enum lttng_notification_channel_status status
)
1910 struct lttng_notification_channel_command_reply reply
= {
1911 .status
= (int8_t) status
,
1913 struct lttng_notification_channel_message msg
= {
1914 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1915 .size
= sizeof(reply
),
1917 char buffer
[sizeof(msg
) + sizeof(reply
)];
1919 if (client
->communication
.outbound
.queued_command_reply
) {
1920 /* Protocol error. */
1924 memcpy(buffer
, &msg
, sizeof(msg
));
1925 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1926 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1928 /* Enqueue buffer to outgoing queue and flush it. */
1929 ret
= lttng_dynamic_buffer_append(
1930 &client
->communication
.outbound
.buffer
,
1931 buffer
, sizeof(buffer
));
1936 ret
= client_flush_outgoing_queue(client
, state
);
1941 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1942 /* Queue could not be emptied. */
1943 client
->communication
.outbound
.queued_command_reply
= true;
1952 int client_dispatch_message(struct notification_client
*client
,
1953 struct notification_thread_state
*state
)
1957 if (client
->communication
.inbound
.msg_type
!=
1958 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1959 client
->communication
.inbound
.msg_type
!=
1960 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1961 !client
->validated
) {
1962 WARN("[notification-thread] client attempted a command before handshake");
1967 switch (client
->communication
.inbound
.msg_type
) {
1968 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1971 * Receiving message header. The function will be called again
1972 * once the rest of the message as been received and can be
1975 const struct lttng_notification_channel_message
*msg
;
1977 assert(sizeof(*msg
) ==
1978 client
->communication
.inbound
.buffer
.size
);
1979 msg
= (const struct lttng_notification_channel_message
*)
1980 client
->communication
.inbound
.buffer
.data
;
1982 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1983 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1988 switch (msg
->type
) {
1989 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1990 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1991 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1995 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1999 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2000 client
->communication
.inbound
.msg_type
=
2001 (enum lttng_notification_channel_message_type
) msg
->type
;
2002 ret
= lttng_dynamic_buffer_set_size(
2003 &client
->communication
.inbound
.buffer
, msg
->size
);
2009 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2011 struct lttng_notification_channel_command_handshake
*handshake_client
;
2012 struct lttng_notification_channel_command_handshake handshake_reply
= {
2013 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2014 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2016 struct lttng_notification_channel_message msg_header
= {
2017 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2018 .size
= sizeof(handshake_reply
),
2020 enum lttng_notification_channel_status status
=
2021 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2022 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2024 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2025 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2026 sizeof(handshake_reply
));
2029 (struct lttng_notification_channel_command_handshake
*)
2030 client
->communication
.inbound
.buffer
.data
;
2031 client
->major
= handshake_client
->major
;
2032 client
->minor
= handshake_client
->minor
;
2033 if (!client
->communication
.inbound
.creds_received
) {
2034 ERR("[notification-thread] No credentials received from client");
2039 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2040 &client
->communication
.inbound
.creds
);
2041 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2042 &client
->communication
.inbound
.creds
);
2043 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2044 client
->uid
, client
->gid
, (int) client
->major
,
2045 (int) client
->minor
);
2047 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2048 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2051 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2052 send_buffer
, sizeof(send_buffer
));
2054 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2058 ret
= client_flush_outgoing_queue(client
, state
);
2063 ret
= client_send_command_reply(client
, state
, status
);
2065 ERR("[notification-thread] Failed to send reply to notification channel client");
2069 /* Set reception state to receive the next message header. */
2070 ret
= client_reset_inbound_state(client
);
2072 ERR("[notification-thread] Failed to reset client communication's inbound state");
2075 client
->validated
= true;
2078 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2079 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2081 struct lttng_condition
*condition
;
2082 enum lttng_notification_channel_status status
=
2083 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2084 const struct lttng_buffer_view condition_view
=
2085 lttng_buffer_view_from_dynamic_buffer(
2086 &client
->communication
.inbound
.buffer
,
2088 size_t expected_condition_size
=
2089 client
->communication
.inbound
.buffer
.size
;
2091 ret
= lttng_condition_create_from_buffer(&condition_view
,
2093 if (ret
!= expected_condition_size
) {
2094 ERR("[notification-thread] Malformed condition received from client");
2098 if (client
->communication
.inbound
.msg_type
==
2099 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2100 ret
= notification_thread_client_subscribe(client
,
2101 condition
, state
, &status
);
2103 ret
= notification_thread_client_unsubscribe(client
,
2104 condition
, state
, &status
);
2110 ret
= client_send_command_reply(client
, state
, status
);
2112 ERR("[notification-thread] Failed to send reply to notification channel client");
2116 /* Set reception state to receive the next message header. */
2117 ret
= client_reset_inbound_state(client
);
2119 ERR("[notification-thread] Failed to reset client communication's inbound state");
2131 /* Incoming data from client. */
2132 int handle_notification_thread_client_in(
2133 struct notification_thread_state
*state
, int socket
)
2136 struct notification_client
*client
;
2140 client
= get_client_from_socket(socket
, state
);
2142 /* Internal error, abort. */
2147 offset
= client
->communication
.inbound
.buffer
.size
-
2148 client
->communication
.inbound
.bytes_to_receive
;
2149 if (client
->communication
.inbound
.expect_creds
) {
2150 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2151 client
->communication
.inbound
.buffer
.data
+ offset
,
2152 client
->communication
.inbound
.bytes_to_receive
,
2153 &client
->communication
.inbound
.creds
);
2155 client
->communication
.inbound
.expect_creds
= false;
2156 client
->communication
.inbound
.creds_received
= true;
2159 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2160 client
->communication
.inbound
.buffer
.data
+ offset
,
2161 client
->communication
.inbound
.bytes_to_receive
);
2164 goto error_disconnect_client
;
2167 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2168 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2169 ret
= client_dispatch_message(client
, state
);
2172 * Only returns an error if this client must be
2175 goto error_disconnect_client
;
2182 error_disconnect_client
:
2183 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2187 /* Client ready to receive outgoing data. */
2188 int handle_notification_thread_client_out(
2189 struct notification_thread_state
*state
, int socket
)
2192 struct notification_client
*client
;
2194 client
= get_client_from_socket(socket
, state
);
2196 /* Internal error, abort. */
2201 ret
= client_flush_outgoing_queue(client
, state
);
2210 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2211 const struct channel_state_sample
*sample
,
2212 uint64_t buffer_capacity
)
2214 bool result
= false;
2216 enum lttng_condition_type condition_type
;
2217 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2218 condition
, struct lttng_condition_buffer_usage
,
2221 if (use_condition
->threshold_bytes
.set
) {
2222 threshold
= use_condition
->threshold_bytes
.value
;
2225 * Threshold was expressed as a ratio.
2227 * TODO the threshold (in bytes) of conditions expressed
2228 * as a ratio of total buffer size could be cached to
2229 * forego this double-multiplication or it could be performed
2230 * as fixed-point math.
2232 * Note that caching should accomodate the case where the
2233 * condition applies to multiple channels (i.e. don't assume
2234 * that all channels matching my_chann* have the same size...)
2236 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2237 (double) buffer_capacity
);
2240 condition_type
= lttng_condition_get_type(condition
);
2241 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2242 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2243 threshold
, sample
->highest_usage
);
2246 * The low condition should only be triggered once _all_ of the
2247 * streams in a channel have gone below the "low" threshold.
2249 if (sample
->highest_usage
<= threshold
) {
2253 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2254 threshold
, sample
->highest_usage
);
2257 * For high buffer usage scenarios, we want to trigger whenever
2258 * _any_ of the streams has reached the "high" threshold.
2260 if (sample
->highest_usage
>= threshold
) {
2269 bool evaluate_session_consumed_size_condition(
2270 const struct lttng_condition
*condition
,
2271 uint64_t session_consumed_size
)
2274 const struct lttng_condition_session_consumed_size
*size_condition
=
2275 container_of(condition
,
2276 struct lttng_condition_session_consumed_size
,
2279 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2280 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2281 threshold
, session_consumed_size
);
2282 return session_consumed_size
>= threshold
;
2286 int evaluate_condition(const struct lttng_condition
*condition
,
2287 struct lttng_evaluation
**evaluation
,
2288 const struct notification_thread_state
*state
,
2289 const struct channel_state_sample
*previous_sample
,
2290 const struct channel_state_sample
*latest_sample
,
2291 uint64_t previous_session_consumed_total
,
2292 uint64_t latest_session_consumed_total
,
2293 struct channel_info
*channel_info
)
2296 enum lttng_condition_type condition_type
;
2297 const bool previous_sample_available
= !!previous_sample
;
2298 bool previous_sample_result
= false;
2299 bool latest_sample_result
;
2301 condition_type
= lttng_condition_get_type(condition
);
2303 switch (condition_type
) {
2304 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2305 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2306 if (caa_likely(previous_sample_available
)) {
2307 previous_sample_result
=
2308 evaluate_buffer_usage_condition(condition
,
2309 previous_sample
, channel_info
->capacity
);
2311 latest_sample_result
= evaluate_buffer_usage_condition(
2312 condition
, latest_sample
,
2313 channel_info
->capacity
);
2315 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2316 if (caa_likely(previous_sample_available
)) {
2317 previous_sample_result
=
2318 evaluate_session_consumed_size_condition(
2320 previous_session_consumed_total
);
2322 latest_sample_result
=
2323 evaluate_session_consumed_size_condition(
2325 latest_session_consumed_total
);
2328 /* Unknown condition type; internal error. */
2332 if (!latest_sample_result
||
2333 (previous_sample_result
== latest_sample_result
)) {
2335 * Only trigger on a condition evaluation transition.
2337 * NOTE: This edge-triggered logic may not be appropriate for
2338 * future condition types.
2343 if (!evaluation
|| !latest_sample_result
) {
2347 switch (condition_type
) {
2348 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2349 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2350 *evaluation
= lttng_evaluation_buffer_usage_create(
2352 latest_sample
->highest_usage
,
2353 channel_info
->capacity
);
2355 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2356 *evaluation
= lttng_evaluation_session_consumed_size_create(
2357 latest_session_consumed_total
);
2372 int client_enqueue_dropped_notification(struct notification_client
*client
,
2373 struct notification_thread_state
*state
)
2376 struct lttng_notification_channel_message msg
= {
2377 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2381 ret
= lttng_dynamic_buffer_append(
2382 &client
->communication
.outbound
.buffer
, &msg
,
2388 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
2389 const struct lttng_evaluation
*evaluation
,
2390 struct notification_client_list
* client_list
,
2391 struct notification_thread_state
*state
,
2392 uid_t channel_uid
, gid_t channel_gid
)
2395 struct lttng_dynamic_buffer msg_buffer
;
2396 struct notification_client_list_element
*client_list_element
, *tmp
;
2397 const struct lttng_notification notification
= {
2398 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
2399 .evaluation
= (struct lttng_evaluation
*) evaluation
,
2401 struct lttng_notification_channel_message msg_header
= {
2402 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
2405 lttng_dynamic_buffer_init(&msg_buffer
);
2407 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
2408 sizeof(msg_header
));
2413 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
2415 ERR("[notification-thread] Failed to serialize notification");
2420 /* Update payload size. */
2421 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
2422 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
2424 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2425 &client_list
->list
, node
) {
2426 struct notification_client
*client
=
2427 client_list_element
->client
;
2429 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2431 /* Client is not allowed to monitor this channel. */
2432 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2436 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2437 client
->socket
, msg_buffer
.size
);
2438 if (client
->communication
.outbound
.buffer
.size
) {
2440 * Outgoing data is already buffered for this client;
2441 * drop the notification and enqueue a "dropped
2442 * notification" message if this is the first dropped
2443 * notification since the socket spilled-over to the
2446 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2448 if (!client
->communication
.outbound
.dropped_notification
) {
2449 client
->communication
.outbound
.dropped_notification
= true;
2450 ret
= client_enqueue_dropped_notification(
2459 ret
= lttng_dynamic_buffer_append_buffer(
2460 &client
->communication
.outbound
.buffer
,
2466 ret
= client_flush_outgoing_queue(client
, state
);
2473 lttng_dynamic_buffer_reset(&msg_buffer
);
2477 int handle_notification_thread_channel_sample(
2478 struct notification_thread_state
*state
, int pipe
,
2479 enum lttng_domain_type domain
)
2482 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2483 struct channel_info
*channel_info
;
2484 struct cds_lfht_node
*node
;
2485 struct cds_lfht_iter iter
;
2486 struct lttng_channel_trigger_list
*trigger_list
;
2487 struct lttng_trigger_list_element
*trigger_list_element
;
2488 bool previous_sample_available
= false;
2489 struct channel_state_sample previous_sample
, latest_sample
;
2490 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2493 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2494 * ensuring that read/write of sampling messages are atomic.
2496 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2497 if (ret
!= sizeof(sample_msg
)) {
2498 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2505 latest_sample
.key
.key
= sample_msg
.key
;
2506 latest_sample
.key
.domain
= domain
;
2507 latest_sample
.highest_usage
= sample_msg
.highest
;
2508 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2509 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2513 /* Retrieve the channel's informations */
2514 cds_lfht_lookup(state
->channels_ht
,
2515 hash_channel_key(&latest_sample
.key
),
2519 node
= cds_lfht_iter_get_node(&iter
);
2520 if (caa_unlikely(!node
)) {
2522 * Not an error since the consumer can push a sample to the pipe
2523 * and the rest of the session daemon could notify us of the
2524 * channel's destruction before we get a chance to process that
2527 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2528 latest_sample
.key
.key
,
2529 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2533 channel_info
= caa_container_of(node
, struct channel_info
,
2535 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2537 latest_sample
.key
.key
,
2538 channel_info
->session_info
->name
,
2539 latest_sample
.highest_usage
,
2540 latest_sample
.lowest_usage
,
2541 latest_sample
.channel_total_consumed
);
2543 previous_session_consumed_total
=
2544 channel_info
->session_info
->consumed_data_size
;
2546 /* Retrieve the channel's last sample, if it exists, and update it. */
2547 cds_lfht_lookup(state
->channel_state_ht
,
2548 hash_channel_key(&latest_sample
.key
),
2549 match_channel_state_sample
,
2552 node
= cds_lfht_iter_get_node(&iter
);
2553 if (caa_likely(node
)) {
2554 struct channel_state_sample
*stored_sample
;
2556 /* Update the sample stored. */
2557 stored_sample
= caa_container_of(node
,
2558 struct channel_state_sample
,
2559 channel_state_ht_node
);
2561 memcpy(&previous_sample
, stored_sample
,
2562 sizeof(previous_sample
));
2563 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2564 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2565 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2566 previous_sample_available
= true;
2568 latest_session_consumed_total
=
2569 previous_session_consumed_total
+
2570 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2573 * This is the channel's first sample, allocate space for and
2574 * store the new sample.
2576 struct channel_state_sample
*stored_sample
;
2578 stored_sample
= zmalloc(sizeof(*stored_sample
));
2579 if (!stored_sample
) {
2584 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2585 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2586 cds_lfht_add(state
->channel_state_ht
,
2587 hash_channel_key(&stored_sample
->key
),
2588 &stored_sample
->channel_state_ht_node
);
2590 latest_session_consumed_total
=
2591 previous_session_consumed_total
+
2592 latest_sample
.channel_total_consumed
;
2595 channel_info
->session_info
->consumed_data_size
=
2596 latest_session_consumed_total
;
2598 /* Find triggers associated with this channel. */
2599 cds_lfht_lookup(state
->channel_triggers_ht
,
2600 hash_channel_key(&latest_sample
.key
),
2601 match_channel_trigger_list
,
2604 node
= cds_lfht_iter_get_node(&iter
);
2605 if (caa_likely(!node
)) {
2609 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2610 channel_triggers_ht_node
);
2611 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2613 const struct lttng_condition
*condition
;
2614 const struct lttng_action
*action
;
2615 const struct lttng_trigger
*trigger
;
2616 struct notification_client_list
*client_list
;
2617 struct lttng_evaluation
*evaluation
= NULL
;
2619 trigger
= trigger_list_element
->trigger
;
2620 condition
= lttng_trigger_get_const_condition(trigger
);
2622 action
= lttng_trigger_get_const_action(trigger
);
2624 /* Notify actions are the only type currently supported. */
2625 assert(lttng_action_get_type_const(action
) ==
2626 LTTNG_ACTION_TYPE_NOTIFY
);
2629 * Check if any client is subscribed to the result of this
2632 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2633 lttng_condition_hash(condition
),
2637 node
= cds_lfht_iter_get_node(&iter
);
2640 client_list
= caa_container_of(node
,
2641 struct notification_client_list
,
2642 notification_trigger_ht_node
);
2643 if (cds_list_empty(&client_list
->list
)) {
2645 * No clients interested in the evaluation's result,
2651 ret
= evaluate_condition(condition
, &evaluation
, state
,
2652 previous_sample_available
? &previous_sample
: NULL
,
2654 previous_session_consumed_total
,
2655 latest_session_consumed_total
,
2657 if (caa_unlikely(ret
)) {
2661 if (caa_likely(!evaluation
)) {
2665 /* Dispatch evaluation result to all clients. */
2666 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2667 evaluation
, client_list
, state
,
2668 channel_info
->session_info
->uid
,
2669 channel_info
->session_info
->gid
);
2670 lttng_evaluation_destroy(evaluation
);
2671 if (caa_unlikely(ret
)) {