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 /* No ownership of the trigger object is assumed. */
55 const struct lttng_trigger
*trigger
;
56 struct cds_list_head node
;
59 struct lttng_channel_trigger_list
{
60 struct channel_key channel_key
;
61 struct cds_list_head list
;
62 struct cds_lfht_node channel_triggers_ht_node
;
65 struct lttng_trigger_ht_element
{
66 struct lttng_trigger
*trigger
;
67 struct cds_lfht_node node
;
70 struct lttng_condition_list_element
{
71 struct lttng_condition
*condition
;
72 struct cds_list_head node
;
75 struct notification_client_list_element
{
76 struct notification_client
*client
;
77 struct cds_list_head node
;
80 struct notification_client_list
{
81 struct lttng_trigger
*trigger
;
82 struct cds_list_head list
;
83 struct cds_lfht_node notification_trigger_ht_node
;
86 struct notification_client
{
88 /* Client protocol version. */
93 * Indicates if the credentials and versions of the client have been
98 * Conditions to which the client's notification channel is subscribed.
99 * List of struct lttng_condition_list_node. The condition member is
100 * owned by the client.
102 struct cds_list_head condition_list
;
103 struct cds_lfht_node client_socket_ht_node
;
107 * During the reception of a message, the reception
108 * buffers' "size" is set to contain the current
109 * message's complete payload.
111 struct lttng_dynamic_buffer buffer
;
112 /* Bytes left to receive for the current message. */
113 size_t bytes_to_receive
;
114 /* Type of the message being received. */
115 enum lttng_notification_channel_message_type msg_type
;
117 * Indicates whether or not credentials are expected
122 * Indicates whether or not credentials were received
126 /* Only used during credentials reception. */
127 lttng_sock_cred creds
;
131 * Indicates whether or not a notification addressed to
132 * this client was dropped because a command reply was
135 * A notification is dropped whenever the buffer is not
138 bool dropped_notification
;
140 * Indicates whether or not a command reply is already
141 * buffered. In this case, it means that the client is
142 * not consuming command replies before emitting a new
143 * one. This could be caused by a protocol error or a
144 * misbehaving/malicious client.
146 bool queued_command_reply
;
147 struct lttng_dynamic_buffer buffer
;
152 struct channel_state_sample
{
153 struct channel_key key
;
154 struct cds_lfht_node channel_state_ht_node
;
155 uint64_t highest_usage
;
156 uint64_t lowest_usage
;
157 uint64_t channel_total_consumed
;
160 static unsigned long hash_channel_key(struct channel_key
*key
);
161 static int evaluate_condition(const struct lttng_condition
*condition
,
162 struct lttng_evaluation
**evaluation
,
163 const struct notification_thread_state
*state
,
164 const struct channel_state_sample
*previous_sample
,
165 const struct channel_state_sample
*latest_sample
,
166 uint64_t previous_session_consumed_total
,
167 uint64_t latest_session_consumed_total
,
168 struct channel_info
*channel_info
);
170 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
171 const struct lttng_evaluation
*evaluation
,
172 struct notification_client_list
*client_list
,
173 struct notification_thread_state
*state
,
174 uid_t channel_uid
, gid_t channel_gid
);
178 void session_info_destroy(void *_data
);
180 void session_info_get(struct session_info
*session_info
);
182 void session_info_put(struct session_info
*session_info
);
184 struct session_info
*session_info_create(const char *name
,
185 uid_t uid
, gid_t gid
);
187 void session_info_add_channel(struct session_info
*session_info
,
188 struct channel_info
*channel_info
);
190 void session_info_remove_channel(struct session_info
*session_info
,
191 struct channel_info
*channel_info
);
194 int match_client(struct cds_lfht_node
*node
, const void *key
)
196 /* This double-cast is intended to supress pointer-to-cast warning. */
197 int socket
= (int) (intptr_t) key
;
198 struct notification_client
*client
;
200 client
= caa_container_of(node
, struct notification_client
,
201 client_socket_ht_node
);
203 return !!(client
->socket
== socket
);
207 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
209 struct channel_key
*channel_key
= (struct channel_key
*) key
;
210 struct lttng_channel_trigger_list
*trigger_list
;
212 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
213 channel_triggers_ht_node
);
215 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
216 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
220 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
222 struct channel_key
*channel_key
= (struct channel_key
*) key
;
223 struct channel_state_sample
*sample
;
225 sample
= caa_container_of(node
, struct channel_state_sample
,
226 channel_state_ht_node
);
228 return !!((channel_key
->key
== sample
->key
.key
) &&
229 (channel_key
->domain
== sample
->key
.domain
));
233 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
235 struct channel_key
*channel_key
= (struct channel_key
*) key
;
236 struct channel_info
*channel_info
;
238 channel_info
= caa_container_of(node
, struct channel_info
,
241 return !!((channel_key
->key
== channel_info
->key
.key
) &&
242 (channel_key
->domain
== channel_info
->key
.domain
));
246 int match_condition(struct cds_lfht_node
*node
, const void *key
)
248 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
249 struct lttng_trigger_ht_element
*trigger
;
250 struct lttng_condition
*condition
;
252 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
254 condition
= lttng_trigger_get_condition(trigger
->trigger
);
257 return !!lttng_condition_is_equal(condition_key
, condition
);
261 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
263 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
264 struct notification_client_list
*client_list
;
265 struct lttng_condition
*condition
;
266 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
269 assert(condition_key
);
271 client_list
= caa_container_of(node
, struct notification_client_list
,
272 notification_trigger_ht_node
);
273 condition
= lttng_trigger_get_condition(client_list
->trigger
);
275 return !!lttng_condition_is_equal(condition_key
, condition
);
279 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
281 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
282 struct notification_client_list
*client_list
;
283 struct lttng_condition
*condition
;
285 assert(condition_key
);
287 client_list
= caa_container_of(node
, struct notification_client_list
,
288 notification_trigger_ht_node
);
289 condition
= lttng_trigger_get_condition(client_list
->trigger
);
291 return !!lttng_condition_is_equal(condition_key
, condition
);
295 unsigned long lttng_condition_buffer_usage_hash(
296 const struct lttng_condition
*_condition
)
299 unsigned long condition_type
;
300 struct lttng_condition_buffer_usage
*condition
;
302 condition
= container_of(_condition
,
303 struct lttng_condition_buffer_usage
, parent
);
305 condition_type
= (unsigned long) condition
->parent
.type
;
306 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
307 if (condition
->session_name
) {
308 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
310 if (condition
->channel_name
) {
311 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
313 if (condition
->domain
.set
) {
314 hash
^= hash_key_ulong(
315 (void *) condition
->domain
.type
,
318 if (condition
->threshold_ratio
.set
) {
321 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
322 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
323 } else if (condition
->threshold_bytes
.set
) {
326 val
= condition
->threshold_bytes
.value
;
327 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
333 unsigned long lttng_condition_session_consumed_size_hash(
334 const struct lttng_condition
*_condition
)
337 unsigned long condition_type
=
338 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
339 struct lttng_condition_session_consumed_size
*condition
;
342 condition
= container_of(_condition
,
343 struct lttng_condition_session_consumed_size
, parent
);
345 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
346 if (condition
->session_name
) {
347 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
349 val
= condition
->consumed_threshold_bytes
.value
;
350 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
356 * The lttng_condition hashing code is kept in this file (rather than
357 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
358 * don't want to link in liblttng-ctl.
361 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
363 switch (condition
->type
) {
364 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
365 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
366 return lttng_condition_buffer_usage_hash(condition
);
367 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
368 return lttng_condition_session_consumed_size_hash(condition
);
370 ERR("[notification-thread] Unexpected condition type caught");
376 unsigned long hash_channel_key(struct channel_key
*key
)
378 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
379 unsigned long domain_hash
= hash_key_ulong(
380 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
382 return key_hash
^ domain_hash
;
386 void channel_info_destroy(struct channel_info
*channel_info
)
392 if (channel_info
->session_info
) {
393 session_info_remove_channel(channel_info
->session_info
,
395 session_info_put(channel_info
->session_info
);
397 if (channel_info
->name
) {
398 free(channel_info
->name
);
403 /* Don't call directly, use the ref-counting mechanism. */
405 void session_info_destroy(void *_data
)
407 struct session_info
*session_info
= _data
;
410 assert(session_info
);
411 if (session_info
->channel_infos_ht
) {
412 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
414 ERR("[notification-thread] Failed to destroy channel information hash table");
417 free(session_info
->name
);
422 void session_info_get(struct session_info
*session_info
)
427 lttng_ref_get(&session_info
->ref
);
431 void session_info_put(struct session_info
*session_info
)
436 lttng_ref_put(&session_info
->ref
);
440 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
)
442 struct session_info
*session_info
;
446 session_info
= zmalloc(sizeof(*session_info
));
450 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
452 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
453 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
454 if (!session_info
->channel_infos_ht
) {
458 cds_lfht_node_init(&session_info
->sessions_ht_node
);
459 session_info
->name
= strdup(name
);
460 if (!session_info
->name
) {
463 session_info
->uid
= uid
;
464 session_info
->gid
= gid
;
468 session_info_put(session_info
);
473 void session_info_add_channel(struct session_info
*session_info
,
474 struct channel_info
*channel_info
)
477 cds_lfht_add(session_info
->channel_infos_ht
,
478 hash_channel_key(&channel_info
->key
),
479 &channel_info
->session_info_channels_ht_node
);
484 void session_info_remove_channel(struct session_info
*session_info
,
485 struct channel_info
*channel_info
)
488 cds_lfht_del(session_info
->channel_infos_ht
,
489 &channel_info
->session_info_channels_ht_node
);
494 struct channel_info
*channel_info_create(const char *channel_name
,
495 struct channel_key
*channel_key
, uint64_t channel_capacity
,
496 struct session_info
*session_info
)
498 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
504 cds_lfht_node_init(&channel_info
->channels_ht_node
);
505 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
506 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
507 channel_info
->capacity
= channel_capacity
;
509 channel_info
->name
= strdup(channel_name
);
510 if (!channel_info
->name
) {
515 * Set the references between session and channel infos:
516 * - channel_info holds a strong reference to session_info
517 * - session_info holds a weak reference to channel_info
519 session_info_get(session_info
);
520 session_info_add_channel(session_info
, channel_info
);
521 channel_info
->session_info
= session_info
;
525 channel_info_destroy(channel_info
);
529 /* This function must be called with the RCU read lock held. */
531 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
532 struct lttng_condition
*condition
,
533 struct notification_client
*client
,
534 struct notification_thread_state
*state
)
537 struct cds_lfht_iter iter
;
538 struct cds_lfht_node
*node
;
539 struct channel_info
*channel_info
= NULL
;
540 struct channel_key
*channel_key
= NULL
;
541 struct channel_state_sample
*last_sample
= NULL
;
542 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
543 struct lttng_evaluation
*evaluation
= NULL
;
544 struct notification_client_list client_list
= { 0 };
545 struct notification_client_list_element client_list_element
= { 0 };
552 /* Find the channel associated with the trigger. */
553 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
554 channel_trigger_list
, channel_triggers_ht_node
) {
555 struct lttng_trigger_list_element
*element
;
557 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
558 const struct lttng_condition
*current_condition
=
559 lttng_trigger_get_const_condition(
562 assert(current_condition
);
563 if (!lttng_condition_is_equal(condition
,
564 current_condition
)) {
568 /* Found the trigger, save the channel key. */
569 channel_key
= &channel_trigger_list
->channel_key
;
573 /* The channel key was found stop iteration. */
579 /* No channel found; normal exit. */
580 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
585 /* Fetch channel info for the matching channel. */
586 cds_lfht_lookup(state
->channels_ht
,
587 hash_channel_key(channel_key
),
591 node
= cds_lfht_iter_get_node(&iter
);
593 channel_info
= caa_container_of(node
, struct channel_info
,
596 /* Retrieve the channel's last sample, if it exists. */
597 cds_lfht_lookup(state
->channel_state_ht
,
598 hash_channel_key(channel_key
),
599 match_channel_state_sample
,
602 node
= cds_lfht_iter_get_node(&iter
);
604 last_sample
= caa_container_of(node
,
605 struct channel_state_sample
,
606 channel_state_ht_node
);
608 /* Nothing to evaluate, no sample was ever taken. Normal exit */
609 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
614 ret
= evaluate_condition(condition
, &evaluation
, state
,
616 0, channel_info
->session_info
->consumed_data_size
,
619 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
624 /* Evaluation yielded nothing. Normal exit. */
625 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
631 * Create a temporary client list with the client currently
634 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
635 CDS_INIT_LIST_HEAD(&client_list
.list
);
636 client_list
.trigger
= trigger
;
638 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
639 client_list_element
.client
= client
;
640 cds_list_add(&client_list_element
.node
, &client_list
.list
);
642 /* Send evaluation result to the newly-subscribed client. */
643 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
644 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
645 state
, channel_info
->session_info
->uid
,
646 channel_info
->session_info
->gid
);
653 int notification_thread_client_subscribe(struct notification_client
*client
,
654 struct lttng_condition
*condition
,
655 struct notification_thread_state
*state
,
656 enum lttng_notification_channel_status
*_status
)
659 struct cds_lfht_iter iter
;
660 struct cds_lfht_node
*node
;
661 struct notification_client_list
*client_list
;
662 struct lttng_condition_list_element
*condition_list_element
= NULL
;
663 struct notification_client_list_element
*client_list_element
= NULL
;
664 enum lttng_notification_channel_status status
=
665 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
668 * Ensure that the client has not already subscribed to this condition
671 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
672 if (lttng_condition_is_equal(condition_list_element
->condition
,
674 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
679 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
680 if (!condition_list_element
) {
684 client_list_element
= zmalloc(sizeof(*client_list_element
));
685 if (!client_list_element
) {
693 * Add the newly-subscribed condition to the client's subscription list.
695 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
696 condition_list_element
->condition
= condition
;
697 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
699 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
700 lttng_condition_hash(condition
),
701 match_client_list_condition
,
704 node
= cds_lfht_iter_get_node(&iter
);
707 * No notification-emiting trigger registered with this
708 * condition. We don't evaluate the condition right away
709 * since this trigger is not registered yet.
711 free(client_list_element
);
715 client_list
= caa_container_of(node
, struct notification_client_list
,
716 notification_trigger_ht_node
);
718 * The condition to which the client just subscribed is evaluated
719 * at this point so that conditions that are already TRUE result
720 * in a notification being sent out.
722 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
724 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
726 free(client_list_element
);
731 * Add the client to the list of clients interested in a given trigger
732 * if a "notification" trigger with a corresponding condition was
735 client_list_element
->client
= client
;
736 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
737 cds_list_add(&client_list_element
->node
, &client_list
->list
);
746 free(condition_list_element
);
747 free(client_list_element
);
752 int notification_thread_client_unsubscribe(
753 struct notification_client
*client
,
754 struct lttng_condition
*condition
,
755 struct notification_thread_state
*state
,
756 enum lttng_notification_channel_status
*_status
)
758 struct cds_lfht_iter iter
;
759 struct cds_lfht_node
*node
;
760 struct notification_client_list
*client_list
;
761 struct lttng_condition_list_element
*condition_list_element
,
763 struct notification_client_list_element
*client_list_element
,
765 bool condition_found
= false;
766 enum lttng_notification_channel_status status
=
767 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
769 /* Remove the condition from the client's condition list. */
770 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
771 &client
->condition_list
, node
) {
772 if (!lttng_condition_is_equal(condition_list_element
->condition
,
777 cds_list_del(&condition_list_element
->node
);
779 * The caller may be iterating on the client's conditions to
780 * tear down a client's connection. In this case, the condition
781 * will be destroyed at the end.
783 if (condition
!= condition_list_element
->condition
) {
784 lttng_condition_destroy(
785 condition_list_element
->condition
);
787 free(condition_list_element
);
788 condition_found
= true;
792 if (!condition_found
) {
793 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
798 * Remove the client from the list of clients interested the trigger
799 * matching the condition.
802 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
803 lttng_condition_hash(condition
),
804 match_client_list_condition
,
807 node
= cds_lfht_iter_get_node(&iter
);
812 client_list
= caa_container_of(node
, struct notification_client_list
,
813 notification_trigger_ht_node
);
814 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
815 &client_list
->list
, node
) {
816 if (client_list_element
->client
->socket
!= client
->socket
) {
819 cds_list_del(&client_list_element
->node
);
820 free(client_list_element
);
826 lttng_condition_destroy(condition
);
834 void notification_client_destroy(struct notification_client
*client
,
835 struct notification_thread_state
*state
)
837 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
843 /* Release all conditions to which the client was subscribed. */
844 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
845 &client
->condition_list
, node
) {
846 (void) notification_thread_client_unsubscribe(client
,
847 condition_list_element
->condition
, state
, NULL
);
850 if (client
->socket
>= 0) {
851 (void) lttcomm_close_unix_sock(client
->socket
);
853 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
854 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
859 * Call with rcu_read_lock held (and hold for the lifetime of the returned
863 struct notification_client
*get_client_from_socket(int socket
,
864 struct notification_thread_state
*state
)
866 struct cds_lfht_iter iter
;
867 struct cds_lfht_node
*node
;
868 struct notification_client
*client
= NULL
;
870 cds_lfht_lookup(state
->client_socket_ht
,
871 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
873 (void *) (unsigned long) socket
,
875 node
= cds_lfht_iter_get_node(&iter
);
880 client
= caa_container_of(node
, struct notification_client
,
881 client_socket_ht_node
);
887 bool buffer_usage_condition_applies_to_channel(
888 struct lttng_condition
*condition
,
889 struct channel_info
*channel_info
)
891 enum lttng_condition_status status
;
892 enum lttng_domain_type condition_domain
;
893 const char *condition_session_name
= NULL
;
894 const char *condition_channel_name
= NULL
;
896 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
898 assert(status
== LTTNG_CONDITION_STATUS_OK
);
899 if (channel_info
->key
.domain
!= condition_domain
) {
903 status
= lttng_condition_buffer_usage_get_session_name(
904 condition
, &condition_session_name
);
905 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
907 status
= lttng_condition_buffer_usage_get_channel_name(
908 condition
, &condition_channel_name
);
909 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
911 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
914 if (strcmp(channel_info
->name
, condition_channel_name
)) {
924 bool session_consumed_size_condition_applies_to_channel(
925 struct lttng_condition
*condition
,
926 struct channel_info
*channel_info
)
928 enum lttng_condition_status status
;
929 const char *condition_session_name
= NULL
;
931 status
= lttng_condition_session_consumed_size_get_session_name(
932 condition
, &condition_session_name
);
933 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
935 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
945 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
946 struct channel_info
*channel_info
)
948 struct lttng_condition
*condition
;
949 bool trigger_applies
;
951 condition
= lttng_trigger_get_condition(trigger
);
956 switch (lttng_condition_get_type(condition
)) {
957 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
958 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
959 trigger_applies
= buffer_usage_condition_applies_to_channel(
960 condition
, channel_info
);
962 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
963 trigger_applies
= session_consumed_size_condition_applies_to_channel(
964 condition
, channel_info
);
970 return trigger_applies
;
976 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
977 struct notification_client
*client
)
979 bool applies
= false;
980 struct lttng_condition_list_element
*condition_list_element
;
982 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
984 applies
= lttng_condition_is_equal(
985 condition_list_element
->condition
,
986 lttng_trigger_get_condition(trigger
));
995 int match_session(struct cds_lfht_node
*node
, const void *key
)
997 const char *name
= key
;
998 struct session_info
*session_info
= caa_container_of(
999 node
, struct session_info
, sessions_ht_node
);
1001 return !strcmp(session_info
->name
, name
);
1005 struct session_info
*find_or_create_session_info(
1006 struct notification_thread_state
*state
,
1007 const char *name
, uid_t uid
, gid_t gid
)
1009 struct session_info
*session
= NULL
;
1010 struct cds_lfht_node
*node
;
1011 struct cds_lfht_iter iter
;
1014 cds_lfht_lookup(state
->sessions_ht
,
1015 hash_key_str(name
, lttng_ht_seed
),
1019 node
= cds_lfht_iter_get_node(&iter
);
1021 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1023 session
= caa_container_of(node
, struct session_info
,
1025 assert(session
->uid
== uid
);
1026 assert(session
->gid
== gid
);
1030 session
= session_info_create(name
, uid
, gid
);
1032 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1037 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1038 &session
->sessions_ht_node
);
1045 int handle_notification_thread_command_add_channel(
1046 struct notification_thread_state
*state
,
1047 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1048 const char *channel_name
, enum lttng_domain_type channel_domain
,
1049 uint64_t channel_key_int
, uint64_t channel_capacity
,
1050 enum lttng_error_code
*cmd_result
)
1052 struct cds_list_head trigger_list
;
1053 struct channel_info
*new_channel_info
= NULL
;
1054 struct channel_key channel_key
= {
1055 .key
= channel_key_int
,
1056 .domain
= channel_domain
,
1058 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1059 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1060 int trigger_count
= 0;
1061 struct cds_lfht_iter iter
;
1062 struct session_info
*session_info
= NULL
;
1064 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1065 channel_name
, session_name
, channel_key_int
,
1066 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1068 CDS_INIT_LIST_HEAD(&trigger_list
);
1070 session_info
= find_or_create_session_info(state
, session_name
,
1071 session_uid
, session_gid
);
1072 if (!session_info
) {
1073 /* Allocation error or an internal error occured. */
1077 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1078 channel_capacity
, session_info
);
1079 if (!new_channel_info
) {
1083 /* Build a list of all triggers applying to the new channel. */
1084 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1086 struct lttng_trigger_list_element
*new_element
;
1088 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1089 new_channel_info
)) {
1093 new_element
= zmalloc(sizeof(*new_element
));
1097 CDS_INIT_LIST_HEAD(&new_element
->node
);
1098 new_element
->trigger
= trigger_ht_element
->trigger
;
1099 cds_list_add(&new_element
->node
, &trigger_list
);
1103 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1105 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1106 if (!channel_trigger_list
) {
1109 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1110 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1111 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1112 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1115 /* Add channel to the channel_ht which owns the channel_infos. */
1116 cds_lfht_add(state
->channels_ht
,
1117 hash_channel_key(&new_channel_info
->key
),
1118 &new_channel_info
->channels_ht_node
);
1120 * Add the list of triggers associated with this channel to the
1121 * channel_triggers_ht.
1123 cds_lfht_add(state
->channel_triggers_ht
,
1124 hash_channel_key(&new_channel_info
->key
),
1125 &channel_trigger_list
->channel_triggers_ht_node
);
1127 *cmd_result
= LTTNG_OK
;
1130 channel_info_destroy(new_channel_info
);
1131 session_info_put(session_info
);
1136 int handle_notification_thread_command_remove_channel(
1137 struct notification_thread_state
*state
,
1138 uint64_t channel_key
, enum lttng_domain_type domain
,
1139 enum lttng_error_code
*cmd_result
)
1141 struct cds_lfht_node
*node
;
1142 struct cds_lfht_iter iter
;
1143 struct lttng_channel_trigger_list
*trigger_list
;
1144 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1145 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1146 struct channel_info
*channel_info
;
1148 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1149 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1153 cds_lfht_lookup(state
->channel_triggers_ht
,
1154 hash_channel_key(&key
),
1155 match_channel_trigger_list
,
1158 node
= cds_lfht_iter_get_node(&iter
);
1160 * There is a severe internal error if we are being asked to remove a
1161 * channel that doesn't exist.
1164 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1168 /* Free the list of triggers associated with this channel. */
1169 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1170 channel_triggers_ht_node
);
1171 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1172 &trigger_list
->list
, node
) {
1173 cds_list_del(&trigger_list_element
->node
);
1174 free(trigger_list_element
);
1176 cds_lfht_del(state
->channel_triggers_ht
, node
);
1179 /* Free sampled channel state. */
1180 cds_lfht_lookup(state
->channel_state_ht
,
1181 hash_channel_key(&key
),
1182 match_channel_state_sample
,
1185 node
= cds_lfht_iter_get_node(&iter
);
1187 * This is expected to be NULL if the channel is destroyed before we
1188 * received a sample.
1191 struct channel_state_sample
*sample
= caa_container_of(node
,
1192 struct channel_state_sample
,
1193 channel_state_ht_node
);
1195 cds_lfht_del(state
->channel_state_ht
, node
);
1199 /* Remove the channel from the channels_ht and free it. */
1200 cds_lfht_lookup(state
->channels_ht
,
1201 hash_channel_key(&key
),
1205 node
= cds_lfht_iter_get_node(&iter
);
1207 channel_info
= caa_container_of(node
, struct channel_info
,
1209 cds_lfht_del(state
->channels_ht
, node
);
1210 channel_info_destroy(channel_info
);
1213 *cmd_result
= LTTNG_OK
;
1218 int condition_is_supported(struct lttng_condition
*condition
)
1222 switch (lttng_condition_get_type(condition
)) {
1223 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1224 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1226 enum lttng_domain_type domain
;
1228 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1235 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1241 * Older kernel tracers don't expose the API to monitor their
1242 * buffers. Therefore, we reject triggers that require that
1243 * mechanism to be available to be evaluated.
1245 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1257 * FIXME A client's credentials are not checked when registering a trigger, nor
1258 * are they stored alongside with the trigger.
1260 * The effects of this are benign since:
1261 * - The client will succeed in registering the trigger, as it is valid,
1262 * - The trigger will, internally, be bound to the channel,
1263 * - The notifications will not be sent since the client's credentials
1264 * are checked against the channel at that moment.
1266 * If this function returns a non-zero value, it means something is
1267 * fundamentally broken and the whole subsystem/thread will be torn down.
1269 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1273 int handle_notification_thread_command_register_trigger(
1274 struct notification_thread_state
*state
,
1275 struct lttng_trigger
*trigger
,
1276 enum lttng_error_code
*cmd_result
)
1279 struct lttng_condition
*condition
;
1280 struct notification_client
*client
;
1281 struct notification_client_list
*client_list
= NULL
;
1282 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1283 struct notification_client_list_element
*client_list_element
, *tmp
;
1284 struct cds_lfht_node
*node
;
1285 struct cds_lfht_iter iter
;
1286 struct channel_info
*channel
;
1287 bool free_trigger
= true;
1291 condition
= lttng_trigger_get_condition(trigger
);
1294 ret
= condition_is_supported(condition
);
1297 } else if (ret
== 0) {
1298 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1301 /* Feature is supported, continue. */
1305 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1306 if (!trigger_ht_element
) {
1311 /* Add trigger to the trigger_ht. */
1312 cds_lfht_node_init(&trigger_ht_element
->node
);
1313 trigger_ht_element
->trigger
= trigger
;
1315 node
= cds_lfht_add_unique(state
->triggers_ht
,
1316 lttng_condition_hash(condition
),
1319 &trigger_ht_element
->node
);
1320 if (node
!= &trigger_ht_element
->node
) {
1321 /* Not a fatal error, simply report it to the client. */
1322 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1323 goto error_free_ht_element
;
1327 * Ownership of the trigger and of its wrapper was transfered to
1330 trigger_ht_element
= NULL
;
1331 free_trigger
= false;
1334 * The rest only applies to triggers that have a "notify" action.
1335 * It is not skipped as this is the only action type currently
1338 client_list
= zmalloc(sizeof(*client_list
));
1341 goto error_free_ht_element
;
1343 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1344 CDS_INIT_LIST_HEAD(&client_list
->list
);
1345 client_list
->trigger
= trigger
;
1347 /* Build a list of clients to which this new trigger applies. */
1348 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1349 client_socket_ht_node
) {
1350 if (!trigger_applies_to_client(trigger
, client
)) {
1354 client_list_element
= zmalloc(sizeof(*client_list_element
));
1355 if (!client_list_element
) {
1357 goto error_free_client_list
;
1359 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1360 client_list_element
->client
= client
;
1361 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1364 cds_lfht_add(state
->notification_trigger_clients_ht
,
1365 lttng_condition_hash(condition
),
1366 &client_list
->notification_trigger_ht_node
);
1369 * Add the trigger to list of triggers bound to the channels currently
1372 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1374 struct lttng_trigger_list_element
*trigger_list_element
;
1375 struct lttng_channel_trigger_list
*trigger_list
;
1377 if (!trigger_applies_to_channel(trigger
, channel
)) {
1381 cds_lfht_lookup(state
->channel_triggers_ht
,
1382 hash_channel_key(&channel
->key
),
1383 match_channel_trigger_list
,
1386 node
= cds_lfht_iter_get_node(&iter
);
1388 trigger_list
= caa_container_of(node
,
1389 struct lttng_channel_trigger_list
,
1390 channel_triggers_ht_node
);
1392 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1393 if (!trigger_list_element
) {
1395 goto error_free_client_list
;
1397 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1398 trigger_list_element
->trigger
= trigger
;
1399 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1403 * Since there is nothing preventing clients from subscribing to a
1404 * condition before the corresponding trigger is registered, we have
1405 * to evaluate this new condition right away.
1407 * At some point, we were waiting for the next "evaluation" (e.g. on
1408 * reception of a channel sample) to evaluate this new condition, but
1411 * The reason it was broken is that waiting for the next sample
1412 * does not allow us to properly handle transitions for edge-triggered
1415 * Consider this example: when we handle a new channel sample, we
1416 * evaluate each conditions twice: once with the previous state, and
1417 * again with the newest state. We then use those two results to
1418 * determine whether a state change happened: a condition was false and
1419 * became true. If a state change happened, we have to notify clients.
1421 * Now, if a client subscribes to a given notification and registers
1422 * a trigger *after* that subscription, we have to make sure the
1423 * condition is evaluated at this point while considering only the
1424 * current state. Otherwise, the next evaluation cycle may only see
1425 * that the evaluations remain the same (true for samples n-1 and n) and
1426 * the client will never know that the condition has been met.
1428 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1429 &client_list
->list
, node
) {
1430 ret
= evaluate_condition_for_client(trigger
, condition
,
1431 client_list_element
->client
, state
);
1433 goto error_free_client_list
;
1438 * Client list ownership transferred to the
1439 * notification_trigger_clients_ht.
1443 *cmd_result
= LTTNG_OK
;
1444 error_free_client_list
:
1446 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1447 &client_list
->list
, node
) {
1448 free(client_list_element
);
1452 error_free_ht_element
:
1453 free(trigger_ht_element
);
1456 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1458 lttng_condition_destroy(condition
);
1459 lttng_action_destroy(action
);
1460 lttng_trigger_destroy(trigger
);
1467 int handle_notification_thread_command_unregister_trigger(
1468 struct notification_thread_state
*state
,
1469 struct lttng_trigger
*trigger
,
1470 enum lttng_error_code
*_cmd_reply
)
1472 struct cds_lfht_iter iter
;
1473 struct cds_lfht_node
*node
, *triggers_ht_node
;
1474 struct lttng_channel_trigger_list
*trigger_list
;
1475 struct notification_client_list
*client_list
;
1476 struct notification_client_list_element
*client_list_element
, *tmp
;
1477 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1478 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1480 struct lttng_action
*action
;
1481 enum lttng_error_code cmd_reply
;
1485 cds_lfht_lookup(state
->triggers_ht
,
1486 lttng_condition_hash(condition
),
1490 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1491 if (!triggers_ht_node
) {
1492 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1495 cmd_reply
= LTTNG_OK
;
1498 /* Remove trigger from channel_triggers_ht. */
1499 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1500 channel_triggers_ht_node
) {
1501 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1503 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1504 &trigger_list
->list
, node
) {
1505 const struct lttng_condition
*current_condition
=
1506 lttng_trigger_get_const_condition(
1507 trigger_element
->trigger
);
1509 assert(current_condition
);
1510 if (!lttng_condition_is_equal(condition
,
1511 current_condition
)) {
1515 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1516 cds_list_del(&trigger_element
->node
);
1517 /* A trigger can only appear once per channel */
1523 * Remove and release the client list from
1524 * notification_trigger_clients_ht.
1526 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1527 lttng_condition_hash(condition
),
1531 node
= cds_lfht_iter_get_node(&iter
);
1533 client_list
= caa_container_of(node
, struct notification_client_list
,
1534 notification_trigger_ht_node
);
1535 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1536 &client_list
->list
, node
) {
1537 free(client_list_element
);
1539 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1542 /* Remove trigger from triggers_ht. */
1543 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1544 struct lttng_trigger_ht_element
, node
);
1545 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1547 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1548 lttng_condition_destroy(condition
);
1549 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1550 lttng_action_destroy(action
);
1551 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1552 free(trigger_ht_element
);
1556 *_cmd_reply
= cmd_reply
;
1561 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1562 int handle_notification_thread_command(
1563 struct notification_thread_handle
*handle
,
1564 struct notification_thread_state
*state
)
1568 struct notification_thread_command
*cmd
;
1570 /* Read the event pipe to put it back into a quiescent state. */
1571 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1577 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1578 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1579 struct notification_thread_command
, cmd_list_node
);
1580 switch (cmd
->type
) {
1581 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1582 DBG("[notification-thread] Received register trigger command");
1583 ret
= handle_notification_thread_command_register_trigger(
1584 state
, cmd
->parameters
.trigger
,
1587 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1588 DBG("[notification-thread] Received unregister trigger command");
1589 ret
= handle_notification_thread_command_unregister_trigger(
1590 state
, cmd
->parameters
.trigger
,
1593 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1594 DBG("[notification-thread] Received add channel command");
1595 ret
= handle_notification_thread_command_add_channel(
1597 cmd
->parameters
.add_channel
.session
.name
,
1598 cmd
->parameters
.add_channel
.session
.uid
,
1599 cmd
->parameters
.add_channel
.session
.gid
,
1600 cmd
->parameters
.add_channel
.channel
.name
,
1601 cmd
->parameters
.add_channel
.channel
.domain
,
1602 cmd
->parameters
.add_channel
.channel
.key
,
1603 cmd
->parameters
.add_channel
.channel
.capacity
,
1606 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1607 DBG("[notification-thread] Received remove channel command");
1608 ret
= handle_notification_thread_command_remove_channel(
1609 state
, cmd
->parameters
.remove_channel
.key
,
1610 cmd
->parameters
.remove_channel
.domain
,
1613 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1614 DBG("[notification-thread] Received quit command");
1615 cmd
->reply_code
= LTTNG_OK
;
1619 ERR("[notification-thread] Unknown internal command received");
1627 cds_list_del(&cmd
->cmd_list_node
);
1628 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1629 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1632 /* Wake-up and return a fatal error to the calling thread. */
1633 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1634 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1635 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1637 /* Indicate a fatal error to the caller. */
1642 unsigned long hash_client_socket(int socket
)
1644 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1648 int socket_set_non_blocking(int socket
)
1652 /* Set the pipe as non-blocking. */
1653 ret
= fcntl(socket
, F_GETFL
, 0);
1655 PERROR("fcntl get socket flags");
1660 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1662 PERROR("fcntl set O_NONBLOCK socket flag");
1665 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1671 int client_reset_inbound_state(struct notification_client
*client
)
1675 ret
= lttng_dynamic_buffer_set_size(
1676 &client
->communication
.inbound
.buffer
, 0);
1679 client
->communication
.inbound
.bytes_to_receive
=
1680 sizeof(struct lttng_notification_channel_message
);
1681 client
->communication
.inbound
.msg_type
=
1682 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1683 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1684 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1685 ret
= lttng_dynamic_buffer_set_size(
1686 &client
->communication
.inbound
.buffer
,
1687 client
->communication
.inbound
.bytes_to_receive
);
1691 int handle_notification_thread_client_connect(
1692 struct notification_thread_state
*state
)
1695 struct notification_client
*client
;
1697 DBG("[notification-thread] Handling new notification channel client connection");
1699 client
= zmalloc(sizeof(*client
));
1705 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1706 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1707 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1708 client
->communication
.inbound
.expect_creds
= true;
1709 ret
= client_reset_inbound_state(client
);
1711 ERR("[notification-thread] Failed to reset client communication's inbound state");
1716 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1718 ERR("[notification-thread] Failed to accept new notification channel client connection");
1723 client
->socket
= ret
;
1725 ret
= socket_set_non_blocking(client
->socket
);
1727 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1731 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1733 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1738 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1739 LPOLLIN
| LPOLLERR
|
1740 LPOLLHUP
| LPOLLRDHUP
);
1742 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1746 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1750 cds_lfht_add(state
->client_socket_ht
,
1751 hash_client_socket(client
->socket
),
1752 &client
->client_socket_ht_node
);
1757 notification_client_destroy(client
, state
);
1761 int handle_notification_thread_client_disconnect(
1763 struct notification_thread_state
*state
)
1766 struct notification_client
*client
;
1769 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1771 client
= get_client_from_socket(client_socket
, state
);
1773 /* Internal state corruption, fatal error. */
1774 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1780 ret
= lttng_poll_del(&state
->events
, client_socket
);
1782 ERR("[notification-thread] Failed to remove client socket from poll set");
1784 cds_lfht_del(state
->client_socket_ht
,
1785 &client
->client_socket_ht_node
);
1786 notification_client_destroy(client
, state
);
1792 int handle_notification_thread_client_disconnect_all(
1793 struct notification_thread_state
*state
)
1795 struct cds_lfht_iter iter
;
1796 struct notification_client
*client
;
1797 bool error_encoutered
= false;
1800 DBG("[notification-thread] Closing all client connections");
1801 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1802 client_socket_ht_node
) {
1805 ret
= handle_notification_thread_client_disconnect(
1806 client
->socket
, state
);
1808 error_encoutered
= true;
1812 return error_encoutered
? 1 : 0;
1815 int handle_notification_thread_trigger_unregister_all(
1816 struct notification_thread_state
*state
)
1818 bool error_occurred
= false;
1819 struct cds_lfht_iter iter
;
1820 struct lttng_trigger_ht_element
*trigger_ht_element
;
1822 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1824 int ret
= handle_notification_thread_command_unregister_trigger(
1825 state
, trigger_ht_element
->trigger
, NULL
);
1827 error_occurred
= true;
1830 return error_occurred
? -1 : 0;
1834 int client_flush_outgoing_queue(struct notification_client
*client
,
1835 struct notification_thread_state
*state
)
1838 size_t to_send_count
;
1840 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1841 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1842 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1845 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1846 client
->communication
.outbound
.buffer
.data
,
1848 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1849 (ret
> 0 && ret
< to_send_count
)) {
1850 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1852 to_send_count
-= max(ret
, 0);
1854 memcpy(client
->communication
.outbound
.buffer
.data
,
1855 client
->communication
.outbound
.buffer
.data
+
1856 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1858 ret
= lttng_dynamic_buffer_set_size(
1859 &client
->communication
.outbound
.buffer
,
1866 * We want to be notified whenever there is buffer space
1867 * available to send the rest of the payload.
1869 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1870 CLIENT_POLL_MASK_IN_OUT
);
1874 } else if (ret
< 0) {
1875 /* Generic error, disconnect the client. */
1876 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1878 ret
= handle_notification_thread_client_disconnect(
1879 client
->socket
, state
);
1884 /* No error and flushed the queue completely. */
1885 ret
= lttng_dynamic_buffer_set_size(
1886 &client
->communication
.outbound
.buffer
, 0);
1890 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1891 CLIENT_POLL_MASK_IN
);
1896 client
->communication
.outbound
.queued_command_reply
= false;
1897 client
->communication
.outbound
.dropped_notification
= false;
1906 int client_send_command_reply(struct notification_client
*client
,
1907 struct notification_thread_state
*state
,
1908 enum lttng_notification_channel_status status
)
1911 struct lttng_notification_channel_command_reply reply
= {
1912 .status
= (int8_t) status
,
1914 struct lttng_notification_channel_message msg
= {
1915 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1916 .size
= sizeof(reply
),
1918 char buffer
[sizeof(msg
) + sizeof(reply
)];
1920 if (client
->communication
.outbound
.queued_command_reply
) {
1921 /* Protocol error. */
1925 memcpy(buffer
, &msg
, sizeof(msg
));
1926 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1927 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1929 /* Enqueue buffer to outgoing queue and flush it. */
1930 ret
= lttng_dynamic_buffer_append(
1931 &client
->communication
.outbound
.buffer
,
1932 buffer
, sizeof(buffer
));
1937 ret
= client_flush_outgoing_queue(client
, state
);
1942 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1943 /* Queue could not be emptied. */
1944 client
->communication
.outbound
.queued_command_reply
= true;
1953 int client_dispatch_message(struct notification_client
*client
,
1954 struct notification_thread_state
*state
)
1958 if (client
->communication
.inbound
.msg_type
!=
1959 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1960 client
->communication
.inbound
.msg_type
!=
1961 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1962 !client
->validated
) {
1963 WARN("[notification-thread] client attempted a command before handshake");
1968 switch (client
->communication
.inbound
.msg_type
) {
1969 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1972 * Receiving message header. The function will be called again
1973 * once the rest of the message as been received and can be
1976 const struct lttng_notification_channel_message
*msg
;
1978 assert(sizeof(*msg
) ==
1979 client
->communication
.inbound
.buffer
.size
);
1980 msg
= (const struct lttng_notification_channel_message
*)
1981 client
->communication
.inbound
.buffer
.data
;
1983 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1984 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1989 switch (msg
->type
) {
1990 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1991 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1992 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1996 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2000 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2001 client
->communication
.inbound
.msg_type
=
2002 (enum lttng_notification_channel_message_type
) msg
->type
;
2003 ret
= lttng_dynamic_buffer_set_size(
2004 &client
->communication
.inbound
.buffer
, msg
->size
);
2010 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2012 struct lttng_notification_channel_command_handshake
*handshake_client
;
2013 struct lttng_notification_channel_command_handshake handshake_reply
= {
2014 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2015 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2017 struct lttng_notification_channel_message msg_header
= {
2018 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2019 .size
= sizeof(handshake_reply
),
2021 enum lttng_notification_channel_status status
=
2022 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2023 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2025 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2026 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2027 sizeof(handshake_reply
));
2030 (struct lttng_notification_channel_command_handshake
*)
2031 client
->communication
.inbound
.buffer
.data
;
2032 client
->major
= handshake_client
->major
;
2033 client
->minor
= handshake_client
->minor
;
2034 if (!client
->communication
.inbound
.creds_received
) {
2035 ERR("[notification-thread] No credentials received from client");
2040 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2041 &client
->communication
.inbound
.creds
);
2042 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2043 &client
->communication
.inbound
.creds
);
2044 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2045 client
->uid
, client
->gid
, (int) client
->major
,
2046 (int) client
->minor
);
2048 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2049 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2052 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2053 send_buffer
, sizeof(send_buffer
));
2055 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2059 ret
= client_flush_outgoing_queue(client
, state
);
2064 ret
= client_send_command_reply(client
, state
, status
);
2066 ERR("[notification-thread] Failed to send reply to notification channel client");
2070 /* Set reception state to receive the next message header. */
2071 ret
= client_reset_inbound_state(client
);
2073 ERR("[notification-thread] Failed to reset client communication's inbound state");
2076 client
->validated
= true;
2079 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2080 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2082 struct lttng_condition
*condition
;
2083 enum lttng_notification_channel_status status
=
2084 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2085 const struct lttng_buffer_view condition_view
=
2086 lttng_buffer_view_from_dynamic_buffer(
2087 &client
->communication
.inbound
.buffer
,
2089 size_t expected_condition_size
=
2090 client
->communication
.inbound
.buffer
.size
;
2092 ret
= lttng_condition_create_from_buffer(&condition_view
,
2094 if (ret
!= expected_condition_size
) {
2095 ERR("[notification-thread] Malformed condition received from client");
2099 if (client
->communication
.inbound
.msg_type
==
2100 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2101 ret
= notification_thread_client_subscribe(client
,
2102 condition
, state
, &status
);
2104 ret
= notification_thread_client_unsubscribe(client
,
2105 condition
, state
, &status
);
2111 ret
= client_send_command_reply(client
, state
, status
);
2113 ERR("[notification-thread] Failed to send reply to notification channel client");
2117 /* Set reception state to receive the next message header. */
2118 ret
= client_reset_inbound_state(client
);
2120 ERR("[notification-thread] Failed to reset client communication's inbound state");
2132 /* Incoming data from client. */
2133 int handle_notification_thread_client_in(
2134 struct notification_thread_state
*state
, int socket
)
2137 struct notification_client
*client
;
2141 client
= get_client_from_socket(socket
, state
);
2143 /* Internal error, abort. */
2148 offset
= client
->communication
.inbound
.buffer
.size
-
2149 client
->communication
.inbound
.bytes_to_receive
;
2150 if (client
->communication
.inbound
.expect_creds
) {
2151 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2152 client
->communication
.inbound
.buffer
.data
+ offset
,
2153 client
->communication
.inbound
.bytes_to_receive
,
2154 &client
->communication
.inbound
.creds
);
2156 client
->communication
.inbound
.expect_creds
= false;
2157 client
->communication
.inbound
.creds_received
= true;
2160 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2161 client
->communication
.inbound
.buffer
.data
+ offset
,
2162 client
->communication
.inbound
.bytes_to_receive
);
2165 goto error_disconnect_client
;
2168 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2169 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2170 ret
= client_dispatch_message(client
, state
);
2173 * Only returns an error if this client must be
2176 goto error_disconnect_client
;
2183 error_disconnect_client
:
2184 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2188 /* Client ready to receive outgoing data. */
2189 int handle_notification_thread_client_out(
2190 struct notification_thread_state
*state
, int socket
)
2193 struct notification_client
*client
;
2195 client
= get_client_from_socket(socket
, state
);
2197 /* Internal error, abort. */
2202 ret
= client_flush_outgoing_queue(client
, state
);
2211 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2212 const struct channel_state_sample
*sample
,
2213 uint64_t buffer_capacity
)
2215 bool result
= false;
2217 enum lttng_condition_type condition_type
;
2218 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2219 condition
, struct lttng_condition_buffer_usage
,
2222 if (use_condition
->threshold_bytes
.set
) {
2223 threshold
= use_condition
->threshold_bytes
.value
;
2226 * Threshold was expressed as a ratio.
2228 * TODO the threshold (in bytes) of conditions expressed
2229 * as a ratio of total buffer size could be cached to
2230 * forego this double-multiplication or it could be performed
2231 * as fixed-point math.
2233 * Note that caching should accomodate the case where the
2234 * condition applies to multiple channels (i.e. don't assume
2235 * that all channels matching my_chann* have the same size...)
2237 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2238 (double) buffer_capacity
);
2241 condition_type
= lttng_condition_get_type(condition
);
2242 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2243 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2244 threshold
, sample
->highest_usage
);
2247 * The low condition should only be triggered once _all_ of the
2248 * streams in a channel have gone below the "low" threshold.
2250 if (sample
->highest_usage
<= threshold
) {
2254 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2255 threshold
, sample
->highest_usage
);
2258 * For high buffer usage scenarios, we want to trigger whenever
2259 * _any_ of the streams has reached the "high" threshold.
2261 if (sample
->highest_usage
>= threshold
) {
2270 bool evaluate_session_consumed_size_condition(
2271 const struct lttng_condition
*condition
,
2272 uint64_t session_consumed_size
)
2275 const struct lttng_condition_session_consumed_size
*size_condition
=
2276 container_of(condition
,
2277 struct lttng_condition_session_consumed_size
,
2280 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2281 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2282 threshold
, session_consumed_size
);
2283 return session_consumed_size
>= threshold
;
2287 int evaluate_condition(const struct lttng_condition
*condition
,
2288 struct lttng_evaluation
**evaluation
,
2289 const struct notification_thread_state
*state
,
2290 const struct channel_state_sample
*previous_sample
,
2291 const struct channel_state_sample
*latest_sample
,
2292 uint64_t previous_session_consumed_total
,
2293 uint64_t latest_session_consumed_total
,
2294 struct channel_info
*channel_info
)
2297 enum lttng_condition_type condition_type
;
2298 const bool previous_sample_available
= !!previous_sample
;
2299 bool previous_sample_result
= false;
2300 bool latest_sample_result
;
2302 condition_type
= lttng_condition_get_type(condition
);
2304 switch (condition_type
) {
2305 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2306 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2307 if (caa_likely(previous_sample_available
)) {
2308 previous_sample_result
=
2309 evaluate_buffer_usage_condition(condition
,
2310 previous_sample
, channel_info
->capacity
);
2312 latest_sample_result
= evaluate_buffer_usage_condition(
2313 condition
, latest_sample
,
2314 channel_info
->capacity
);
2316 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2317 if (caa_likely(previous_sample_available
)) {
2318 previous_sample_result
=
2319 evaluate_session_consumed_size_condition(
2321 previous_session_consumed_total
);
2323 latest_sample_result
=
2324 evaluate_session_consumed_size_condition(
2326 latest_session_consumed_total
);
2329 /* Unknown condition type; internal error. */
2333 if (!latest_sample_result
||
2334 (previous_sample_result
== latest_sample_result
)) {
2336 * Only trigger on a condition evaluation transition.
2338 * NOTE: This edge-triggered logic may not be appropriate for
2339 * future condition types.
2344 if (!evaluation
|| !latest_sample_result
) {
2348 switch (condition_type
) {
2349 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2350 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2351 *evaluation
= lttng_evaluation_buffer_usage_create(
2353 latest_sample
->highest_usage
,
2354 channel_info
->capacity
);
2356 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2357 *evaluation
= lttng_evaluation_session_consumed_size_create(
2358 latest_session_consumed_total
);
2373 int client_enqueue_dropped_notification(struct notification_client
*client
,
2374 struct notification_thread_state
*state
)
2377 struct lttng_notification_channel_message msg
= {
2378 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2382 ret
= lttng_dynamic_buffer_append(
2383 &client
->communication
.outbound
.buffer
, &msg
,
2389 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
2390 const struct lttng_evaluation
*evaluation
,
2391 struct notification_client_list
* client_list
,
2392 struct notification_thread_state
*state
,
2393 uid_t channel_uid
, gid_t channel_gid
)
2396 struct lttng_dynamic_buffer msg_buffer
;
2397 struct notification_client_list_element
*client_list_element
, *tmp
;
2398 const struct lttng_notification notification
= {
2399 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
2400 .evaluation
= (struct lttng_evaluation
*) evaluation
,
2402 struct lttng_notification_channel_message msg_header
= {
2403 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
2406 lttng_dynamic_buffer_init(&msg_buffer
);
2408 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
2409 sizeof(msg_header
));
2414 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
2416 ERR("[notification-thread] Failed to serialize notification");
2421 /* Update payload size. */
2422 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
2423 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
2425 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2426 &client_list
->list
, node
) {
2427 struct notification_client
*client
=
2428 client_list_element
->client
;
2430 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2432 /* Client is not allowed to monitor this channel. */
2433 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2437 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2438 client
->socket
, msg_buffer
.size
);
2439 if (client
->communication
.outbound
.buffer
.size
) {
2441 * Outgoing data is already buffered for this client;
2442 * drop the notification and enqueue a "dropped
2443 * notification" message if this is the first dropped
2444 * notification since the socket spilled-over to the
2447 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2449 if (!client
->communication
.outbound
.dropped_notification
) {
2450 client
->communication
.outbound
.dropped_notification
= true;
2451 ret
= client_enqueue_dropped_notification(
2460 ret
= lttng_dynamic_buffer_append_buffer(
2461 &client
->communication
.outbound
.buffer
,
2467 ret
= client_flush_outgoing_queue(client
, state
);
2474 lttng_dynamic_buffer_reset(&msg_buffer
);
2478 int handle_notification_thread_channel_sample(
2479 struct notification_thread_state
*state
, int pipe
,
2480 enum lttng_domain_type domain
)
2483 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2484 struct channel_info
*channel_info
;
2485 struct cds_lfht_node
*node
;
2486 struct cds_lfht_iter iter
;
2487 struct lttng_channel_trigger_list
*trigger_list
;
2488 struct lttng_trigger_list_element
*trigger_list_element
;
2489 bool previous_sample_available
= false;
2490 struct channel_state_sample previous_sample
, latest_sample
;
2491 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2494 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2495 * ensuring that read/write of sampling messages are atomic.
2497 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2498 if (ret
!= sizeof(sample_msg
)) {
2499 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2506 latest_sample
.key
.key
= sample_msg
.key
;
2507 latest_sample
.key
.domain
= domain
;
2508 latest_sample
.highest_usage
= sample_msg
.highest
;
2509 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2510 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2514 /* Retrieve the channel's informations */
2515 cds_lfht_lookup(state
->channels_ht
,
2516 hash_channel_key(&latest_sample
.key
),
2520 node
= cds_lfht_iter_get_node(&iter
);
2521 if (caa_unlikely(!node
)) {
2523 * Not an error since the consumer can push a sample to the pipe
2524 * and the rest of the session daemon could notify us of the
2525 * channel's destruction before we get a chance to process that
2528 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2529 latest_sample
.key
.key
,
2530 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2534 channel_info
= caa_container_of(node
, struct channel_info
,
2536 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2538 latest_sample
.key
.key
,
2539 channel_info
->session_info
->name
,
2540 latest_sample
.highest_usage
,
2541 latest_sample
.lowest_usage
,
2542 latest_sample
.channel_total_consumed
);
2544 previous_session_consumed_total
=
2545 channel_info
->session_info
->consumed_data_size
;
2547 /* Retrieve the channel's last sample, if it exists, and update it. */
2548 cds_lfht_lookup(state
->channel_state_ht
,
2549 hash_channel_key(&latest_sample
.key
),
2550 match_channel_state_sample
,
2553 node
= cds_lfht_iter_get_node(&iter
);
2554 if (caa_likely(node
)) {
2555 struct channel_state_sample
*stored_sample
;
2557 /* Update the sample stored. */
2558 stored_sample
= caa_container_of(node
,
2559 struct channel_state_sample
,
2560 channel_state_ht_node
);
2562 memcpy(&previous_sample
, stored_sample
,
2563 sizeof(previous_sample
));
2564 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2565 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2566 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2567 previous_sample_available
= true;
2569 latest_session_consumed_total
=
2570 previous_session_consumed_total
+
2571 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2574 * This is the channel's first sample, allocate space for and
2575 * store the new sample.
2577 struct channel_state_sample
*stored_sample
;
2579 stored_sample
= zmalloc(sizeof(*stored_sample
));
2580 if (!stored_sample
) {
2585 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2586 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2587 cds_lfht_add(state
->channel_state_ht
,
2588 hash_channel_key(&stored_sample
->key
),
2589 &stored_sample
->channel_state_ht_node
);
2591 latest_session_consumed_total
=
2592 previous_session_consumed_total
+
2593 latest_sample
.channel_total_consumed
;
2596 channel_info
->session_info
->consumed_data_size
=
2597 latest_session_consumed_total
;
2599 /* Find triggers associated with this channel. */
2600 cds_lfht_lookup(state
->channel_triggers_ht
,
2601 hash_channel_key(&latest_sample
.key
),
2602 match_channel_trigger_list
,
2605 node
= cds_lfht_iter_get_node(&iter
);
2606 if (caa_likely(!node
)) {
2610 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2611 channel_triggers_ht_node
);
2612 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2614 const struct lttng_condition
*condition
;
2615 const struct lttng_action
*action
;
2616 const struct lttng_trigger
*trigger
;
2617 struct notification_client_list
*client_list
;
2618 struct lttng_evaluation
*evaluation
= NULL
;
2620 trigger
= trigger_list_element
->trigger
;
2621 condition
= lttng_trigger_get_const_condition(trigger
);
2623 action
= lttng_trigger_get_const_action(trigger
);
2625 /* Notify actions are the only type currently supported. */
2626 assert(lttng_action_get_type_const(action
) ==
2627 LTTNG_ACTION_TYPE_NOTIFY
);
2630 * Check if any client is subscribed to the result of this
2633 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2634 lttng_condition_hash(condition
),
2638 node
= cds_lfht_iter_get_node(&iter
);
2641 client_list
= caa_container_of(node
,
2642 struct notification_client_list
,
2643 notification_trigger_ht_node
);
2644 if (cds_list_empty(&client_list
->list
)) {
2646 * No clients interested in the evaluation's result,
2652 ret
= evaluate_condition(condition
, &evaluation
, state
,
2653 previous_sample_available
? &previous_sample
: NULL
,
2655 previous_session_consumed_total
,
2656 latest_session_consumed_total
,
2658 if (caa_unlikely(ret
)) {
2662 if (caa_likely(!evaluation
)) {
2666 /* Dispatch evaluation result to all clients. */
2667 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2668 evaluation
, client_list
, state
,
2669 channel_info
->session_info
->uid
,
2670 channel_info
->session_info
->gid
);
2671 lttng_evaluation_destroy(evaluation
);
2672 if (caa_unlikely(ret
)) {