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.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(struct lttng_trigger
*trigger
,
170 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 struct lttng_condition
*_condition
)
297 unsigned long hash
= 0;
298 struct lttng_condition_buffer_usage
*condition
;
300 condition
= container_of(_condition
,
301 struct lttng_condition_buffer_usage
, parent
);
303 if (condition
->session_name
) {
304 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
306 if (condition
->channel_name
) {
307 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
309 if (condition
->domain
.set
) {
310 hash
^= hash_key_ulong(
311 (void *) condition
->domain
.type
,
314 if (condition
->threshold_ratio
.set
) {
317 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
318 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
319 } else if (condition
->threshold_bytes
.set
) {
322 val
= condition
->threshold_bytes
.value
;
323 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
329 unsigned long lttng_condition_session_consumed_size_hash(
330 struct lttng_condition
*_condition
)
332 unsigned long hash
= 0;
333 struct lttng_condition_session_consumed_size
*condition
;
336 condition
= container_of(_condition
,
337 struct lttng_condition_session_consumed_size
, parent
);
339 if (condition
->session_name
) {
340 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
342 val
= condition
->consumed_threshold_bytes
.value
;
343 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
348 * The lttng_condition hashing code is kept in this file (rather than
349 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
350 * don't want to link in liblttng-ctl.
353 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
355 switch (condition
->type
) {
356 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
357 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
358 return lttng_condition_buffer_usage_hash(condition
);
359 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
360 return lttng_condition_session_consumed_size_hash(condition
);
362 ERR("[notification-thread] Unexpected condition type caught");
368 unsigned long hash_channel_key(struct channel_key
*key
)
370 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
371 unsigned long domain_hash
= hash_key_ulong(
372 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
374 return key_hash
^ domain_hash
;
378 void channel_info_destroy(struct channel_info
*channel_info
)
384 if (channel_info
->session_info
) {
385 session_info_remove_channel(channel_info
->session_info
,
387 session_info_put(channel_info
->session_info
);
389 if (channel_info
->name
) {
390 free(channel_info
->name
);
395 /* Don't call directly, use the ref-counting mechanism. */
397 void session_info_destroy(void *_data
)
399 struct session_info
*session_info
= _data
;
401 assert(session_info
);
402 if (session_info
->channel_infos_ht
) {
403 cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
405 free(session_info
->name
);
410 void session_info_get(struct session_info
*session_info
)
415 lttng_ref_get(&session_info
->ref
);
419 void session_info_put(struct session_info
*session_info
)
424 lttng_ref_put(&session_info
->ref
);
428 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
)
430 struct session_info
*session_info
;
434 session_info
= zmalloc(sizeof(*session_info
));
438 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
440 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
441 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
442 if (!session_info
->channel_infos_ht
) {
446 cds_lfht_node_init(&session_info
->sessions_ht_node
);
447 session_info
->name
= strdup(name
);
448 if (!session_info
->name
) {
451 session_info
->uid
= uid
;
452 session_info
->gid
= gid
;
456 session_info_put(session_info
);
461 void session_info_add_channel(struct session_info
*session_info
,
462 struct channel_info
*channel_info
)
465 cds_lfht_add(session_info
->channel_infos_ht
,
466 hash_channel_key(&channel_info
->key
),
467 &channel_info
->session_info_channels_ht_node
);
472 void session_info_remove_channel(struct session_info
*session_info
,
473 struct channel_info
*channel_info
)
476 cds_lfht_del(session_info
->channel_infos_ht
,
477 &channel_info
->session_info_channels_ht_node
);
482 struct channel_info
*channel_info_create(const char *channel_name
,
483 struct channel_key
*channel_key
, uint64_t channel_capacity
,
484 struct session_info
*session_info
)
486 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
492 cds_lfht_node_init(&channel_info
->channels_ht_node
);
493 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
494 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
495 channel_info
->capacity
= channel_capacity
;
497 channel_info
->name
= strdup(channel_name
);
498 if (!channel_info
->name
) {
503 * Set the references between session and channel infos:
504 * - channel_info holds a strong reference to session_info
505 * - session_info holds a weak reference to channel_info
507 session_info_get(session_info
);
508 session_info_add_channel(session_info
, channel_info
);
509 channel_info
->session_info
= session_info
;
513 channel_info_destroy(channel_info
);
517 /* This function must be called with the RCU read lock held. */
519 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
520 struct lttng_condition
*condition
,
521 struct notification_client
*client
,
522 struct notification_thread_state
*state
)
525 struct cds_lfht_iter iter
;
526 struct cds_lfht_node
*node
;
527 struct channel_info
*channel_info
= NULL
;
528 struct channel_key
*channel_key
= NULL
;
529 struct channel_state_sample
*last_sample
= NULL
;
530 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
531 struct lttng_evaluation
*evaluation
= NULL
;
532 struct notification_client_list client_list
= { 0 };
533 struct notification_client_list_element client_list_element
= { 0 };
540 /* Find the channel associated with the trigger. */
541 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
542 channel_trigger_list
, channel_triggers_ht_node
) {
543 struct lttng_trigger_list_element
*element
;
545 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
546 struct lttng_condition
*current_condition
=
547 lttng_trigger_get_condition(
550 assert(current_condition
);
551 if (!lttng_condition_is_equal(condition
,
552 current_condition
)) {
556 /* Found the trigger, save the channel key. */
557 channel_key
= &channel_trigger_list
->channel_key
;
561 /* The channel key was found stop iteration. */
567 /* No channel found; normal exit. */
568 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
573 /* Fetch channel info for the matching channel. */
574 cds_lfht_lookup(state
->channels_ht
,
575 hash_channel_key(channel_key
),
579 node
= cds_lfht_iter_get_node(&iter
);
581 channel_info
= caa_container_of(node
, struct channel_info
,
584 /* Retrieve the channel's last sample, if it exists. */
585 cds_lfht_lookup(state
->channel_state_ht
,
586 hash_channel_key(channel_key
),
587 match_channel_state_sample
,
590 node
= cds_lfht_iter_get_node(&iter
);
592 last_sample
= caa_container_of(node
,
593 struct channel_state_sample
,
594 channel_state_ht_node
);
596 /* Nothing to evaluate, no sample was ever taken. Normal exit */
597 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
602 ret
= evaluate_condition(condition
, &evaluation
, state
,
604 0, channel_info
->session_info
->consumed_data_size
,
607 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
612 /* Evaluation yielded nothing. Normal exit. */
613 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
619 * Create a temporary client list with the client currently
622 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
623 CDS_INIT_LIST_HEAD(&client_list
.list
);
624 client_list
.trigger
= trigger
;
626 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
627 client_list_element
.client
= client
;
628 cds_list_add(&client_list_element
.node
, &client_list
.list
);
630 /* Send evaluation result to the newly-subscribed client. */
631 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
632 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
633 state
, channel_info
->session_info
->uid
,
634 channel_info
->session_info
->gid
);
641 int notification_thread_client_subscribe(struct notification_client
*client
,
642 struct lttng_condition
*condition
,
643 struct notification_thread_state
*state
,
644 enum lttng_notification_channel_status
*_status
)
647 struct cds_lfht_iter iter
;
648 struct cds_lfht_node
*node
;
649 struct notification_client_list
*client_list
;
650 struct lttng_condition_list_element
*condition_list_element
= NULL
;
651 struct notification_client_list_element
*client_list_element
= NULL
;
652 enum lttng_notification_channel_status status
=
653 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
656 * Ensure that the client has not already subscribed to this condition
659 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
660 if (lttng_condition_is_equal(condition_list_element
->condition
,
662 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
667 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
668 if (!condition_list_element
) {
672 client_list_element
= zmalloc(sizeof(*client_list_element
));
673 if (!client_list_element
) {
681 * Add the newly-subscribed condition to the client's subscription list.
683 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
684 condition_list_element
->condition
= condition
;
685 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
687 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
688 lttng_condition_hash(condition
),
689 match_client_list_condition
,
692 node
= cds_lfht_iter_get_node(&iter
);
695 * No notification-emiting trigger registered with this
696 * condition. We don't evaluate the condition right away
697 * since this trigger is not registered yet.
699 free(client_list_element
);
703 client_list
= caa_container_of(node
, struct notification_client_list
,
704 notification_trigger_ht_node
);
706 * The condition to which the client just subscribed is evaluated
707 * at this point so that conditions that are already TRUE result
708 * in a notification being sent out.
710 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
712 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
718 * Add the client to the list of clients interested in a given trigger
719 * if a "notification" trigger with a corresponding condition was
722 client_list_element
->client
= client
;
723 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
724 cds_list_add(&client_list_element
->node
, &client_list
->list
);
733 free(condition_list_element
);
734 free(client_list_element
);
739 int notification_thread_client_unsubscribe(
740 struct notification_client
*client
,
741 struct lttng_condition
*condition
,
742 struct notification_thread_state
*state
,
743 enum lttng_notification_channel_status
*_status
)
745 struct cds_lfht_iter iter
;
746 struct cds_lfht_node
*node
;
747 struct notification_client_list
*client_list
;
748 struct lttng_condition_list_element
*condition_list_element
,
750 struct notification_client_list_element
*client_list_element
,
752 bool condition_found
= false;
753 enum lttng_notification_channel_status status
=
754 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
756 /* Remove the condition from the client's condition list. */
757 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
758 &client
->condition_list
, node
) {
759 if (!lttng_condition_is_equal(condition_list_element
->condition
,
764 cds_list_del(&condition_list_element
->node
);
766 * The caller may be iterating on the client's conditions to
767 * tear down a client's connection. In this case, the condition
768 * will be destroyed at the end.
770 if (condition
!= condition_list_element
->condition
) {
771 lttng_condition_destroy(
772 condition_list_element
->condition
);
774 free(condition_list_element
);
775 condition_found
= true;
779 if (!condition_found
) {
780 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
785 * Remove the client from the list of clients interested the trigger
786 * matching the condition.
789 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
790 lttng_condition_hash(condition
),
791 match_client_list_condition
,
794 node
= cds_lfht_iter_get_node(&iter
);
799 client_list
= caa_container_of(node
, struct notification_client_list
,
800 notification_trigger_ht_node
);
801 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
802 &client_list
->list
, node
) {
803 if (client_list_element
->client
->socket
!= client
->socket
) {
806 cds_list_del(&client_list_element
->node
);
807 free(client_list_element
);
813 lttng_condition_destroy(condition
);
821 void notification_client_destroy(struct notification_client
*client
,
822 struct notification_thread_state
*state
)
824 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
830 /* Release all conditions to which the client was subscribed. */
831 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
832 &client
->condition_list
, node
) {
833 (void) notification_thread_client_unsubscribe(client
,
834 condition_list_element
->condition
, state
, NULL
);
837 if (client
->socket
>= 0) {
838 (void) lttcomm_close_unix_sock(client
->socket
);
840 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
841 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
846 * Call with rcu_read_lock held (and hold for the lifetime of the returned
850 struct notification_client
*get_client_from_socket(int socket
,
851 struct notification_thread_state
*state
)
853 struct cds_lfht_iter iter
;
854 struct cds_lfht_node
*node
;
855 struct notification_client
*client
= NULL
;
857 cds_lfht_lookup(state
->client_socket_ht
,
858 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
860 (void *) (unsigned long) socket
,
862 node
= cds_lfht_iter_get_node(&iter
);
867 client
= caa_container_of(node
, struct notification_client
,
868 client_socket_ht_node
);
874 bool buffer_usage_condition_applies_to_channel(
875 struct lttng_condition
*condition
,
876 struct channel_info
*channel_info
)
878 enum lttng_condition_status status
;
879 enum lttng_domain_type condition_domain
;
880 const char *condition_session_name
= NULL
;
881 const char *condition_channel_name
= NULL
;
883 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
885 assert(status
== LTTNG_CONDITION_STATUS_OK
);
886 if (channel_info
->key
.domain
!= condition_domain
) {
890 status
= lttng_condition_buffer_usage_get_session_name(
891 condition
, &condition_session_name
);
892 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
894 status
= lttng_condition_buffer_usage_get_channel_name(
895 condition
, &condition_channel_name
);
896 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
898 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
901 if (strcmp(channel_info
->name
, condition_channel_name
)) {
911 bool session_consumed_size_condition_applies_to_channel(
912 struct lttng_condition
*condition
,
913 struct channel_info
*channel_info
)
915 enum lttng_condition_status status
;
916 const char *condition_session_name
= NULL
;
918 status
= lttng_condition_session_consumed_size_get_session_name(
919 condition
, &condition_session_name
);
920 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
922 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
932 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
933 struct channel_info
*channel_info
)
935 struct lttng_condition
*condition
;
936 bool trigger_applies
;
938 condition
= lttng_trigger_get_condition(trigger
);
943 switch (lttng_condition_get_type(condition
)) {
944 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
945 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
946 trigger_applies
= buffer_usage_condition_applies_to_channel(
947 condition
, channel_info
);
949 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
950 trigger_applies
= session_consumed_size_condition_applies_to_channel(
951 condition
, channel_info
);
957 return trigger_applies
;
963 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
964 struct notification_client
*client
)
966 bool applies
= false;
967 struct lttng_condition_list_element
*condition_list_element
;
969 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
971 applies
= lttng_condition_is_equal(
972 condition_list_element
->condition
,
973 lttng_trigger_get_condition(trigger
));
982 int match_session(struct cds_lfht_node
*node
, const void *key
)
984 const char *name
= key
;
985 struct session_info
*session_info
= caa_container_of(
986 node
, struct session_info
, sessions_ht_node
);
988 return !strcmp(session_info
->name
, name
);
992 struct session_info
*find_or_create_session_info(
993 struct notification_thread_state
*state
,
994 const char *name
, uid_t uid
, gid_t gid
)
996 struct session_info
*session
= NULL
;
997 struct cds_lfht_node
*node
;
998 struct cds_lfht_iter iter
;
1001 cds_lfht_lookup(state
->sessions_ht
,
1002 hash_key_str(name
, lttng_ht_seed
),
1006 node
= cds_lfht_iter_get_node(&iter
);
1008 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1010 session
= caa_container_of(node
, struct session_info
,
1012 assert(session
->uid
== uid
);
1013 assert(session
->gid
== gid
);
1017 session
= session_info_create(name
, uid
, gid
);
1019 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1029 int handle_notification_thread_command_add_channel(
1030 struct notification_thread_state
*state
,
1031 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1032 const char *channel_name
, enum lttng_domain_type channel_domain
,
1033 uint64_t channel_key_int
, uint64_t channel_capacity
,
1034 enum lttng_error_code
*cmd_result
)
1036 struct cds_list_head trigger_list
;
1037 struct channel_info
*new_channel_info
= NULL
;
1038 struct channel_key channel_key
= {
1039 .key
= channel_key_int
,
1040 .domain
= channel_domain
,
1042 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1043 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1044 int trigger_count
= 0;
1045 struct cds_lfht_iter iter
;
1046 struct session_info
*session_info
= NULL
;
1048 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1049 channel_name
, session_name
, channel_key_int
,
1050 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1052 CDS_INIT_LIST_HEAD(&trigger_list
);
1054 session_info
= find_or_create_session_info(state
, session_name
,
1055 session_uid
, session_gid
);
1056 if (!session_info
) {
1057 /* Allocation error or an internal error occured. */
1061 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1062 channel_capacity
, session_info
);
1063 if (!new_channel_info
) {
1067 /* Build a list of all triggers applying to the new channel. */
1068 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1070 struct lttng_trigger_list_element
*new_element
;
1072 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1073 new_channel_info
)) {
1077 new_element
= zmalloc(sizeof(*new_element
));
1081 CDS_INIT_LIST_HEAD(&new_element
->node
);
1082 new_element
->trigger
= trigger_ht_element
->trigger
;
1083 cds_list_add(&new_element
->node
, &trigger_list
);
1087 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1089 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1090 if (!channel_trigger_list
) {
1093 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1094 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1095 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1096 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1099 /* Add channel to the channel_ht which owns the channel_infos. */
1100 cds_lfht_add(state
->channels_ht
,
1101 hash_channel_key(&new_channel_info
->key
),
1102 &new_channel_info
->channels_ht_node
);
1104 * Add the list of triggers associated with this channel to the
1105 * channel_triggers_ht.
1107 cds_lfht_add(state
->channel_triggers_ht
,
1108 hash_channel_key(&new_channel_info
->key
),
1109 &channel_trigger_list
->channel_triggers_ht_node
);
1111 *cmd_result
= LTTNG_OK
;
1114 channel_info_destroy(new_channel_info
);
1115 session_info_put(session_info
);
1120 int handle_notification_thread_command_remove_channel(
1121 struct notification_thread_state
*state
,
1122 uint64_t channel_key
, enum lttng_domain_type domain
,
1123 enum lttng_error_code
*cmd_result
)
1125 struct cds_lfht_node
*node
;
1126 struct cds_lfht_iter iter
;
1127 struct lttng_channel_trigger_list
*trigger_list
;
1128 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1129 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1130 struct channel_info
*channel_info
;
1132 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1133 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1137 cds_lfht_lookup(state
->channel_triggers_ht
,
1138 hash_channel_key(&key
),
1139 match_channel_trigger_list
,
1142 node
= cds_lfht_iter_get_node(&iter
);
1144 * There is a severe internal error if we are being asked to remove a
1145 * channel that doesn't exist.
1148 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1152 /* Free the list of triggers associated with this channel. */
1153 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1154 channel_triggers_ht_node
);
1155 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1156 &trigger_list
->list
, node
) {
1157 cds_list_del(&trigger_list_element
->node
);
1158 free(trigger_list_element
);
1160 cds_lfht_del(state
->channel_triggers_ht
, node
);
1163 /* Free sampled channel state. */
1164 cds_lfht_lookup(state
->channel_state_ht
,
1165 hash_channel_key(&key
),
1166 match_channel_state_sample
,
1169 node
= cds_lfht_iter_get_node(&iter
);
1171 * This is expected to be NULL if the channel is destroyed before we
1172 * received a sample.
1175 struct channel_state_sample
*sample
= caa_container_of(node
,
1176 struct channel_state_sample
,
1177 channel_state_ht_node
);
1179 cds_lfht_del(state
->channel_state_ht
, node
);
1183 /* Remove the channel from the channels_ht and free it. */
1184 cds_lfht_lookup(state
->channels_ht
,
1185 hash_channel_key(&key
),
1189 node
= cds_lfht_iter_get_node(&iter
);
1191 channel_info
= caa_container_of(node
, struct channel_info
,
1193 cds_lfht_del(state
->channels_ht
, node
);
1194 channel_info_destroy(channel_info
);
1197 *cmd_result
= LTTNG_OK
;
1202 int condition_is_supported(struct lttng_condition
*condition
)
1206 switch (lttng_condition_get_type(condition
)) {
1207 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1208 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1210 enum lttng_domain_type domain
;
1212 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1219 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1225 * Older kernel tracers don't expose the API to monitor their
1226 * buffers. Therefore, we reject triggers that require that
1227 * mechanism to be available to be evaluated.
1229 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1241 * FIXME A client's credentials are not checked when registering a trigger, nor
1242 * are they stored alongside with the trigger.
1244 * The effects of this are benign since:
1245 * - The client will succeed in registering the trigger, as it is valid,
1246 * - The trigger will, internally, be bound to the channel,
1247 * - The notifications will not be sent since the client's credentials
1248 * are checked against the channel at that moment.
1250 * If this function returns a non-zero value, it means something is
1251 * fundamentally broken and the whole subsystem/thread will be torn down.
1253 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1257 int handle_notification_thread_command_register_trigger(
1258 struct notification_thread_state
*state
,
1259 struct lttng_trigger
*trigger
,
1260 enum lttng_error_code
*cmd_result
)
1263 struct lttng_condition
*condition
;
1264 struct notification_client
*client
;
1265 struct notification_client_list
*client_list
= NULL
;
1266 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1267 struct notification_client_list_element
*client_list_element
, *tmp
;
1268 struct cds_lfht_node
*node
;
1269 struct cds_lfht_iter iter
;
1270 struct channel_info
*channel
;
1271 bool free_trigger
= true;
1275 condition
= lttng_trigger_get_condition(trigger
);
1278 ret
= condition_is_supported(condition
);
1281 } else if (ret
== 0) {
1282 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1285 /* Feature is supported, continue. */
1289 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1290 if (!trigger_ht_element
) {
1295 /* Add trigger to the trigger_ht. */
1296 cds_lfht_node_init(&trigger_ht_element
->node
);
1297 trigger_ht_element
->trigger
= trigger
;
1299 node
= cds_lfht_add_unique(state
->triggers_ht
,
1300 lttng_condition_hash(condition
),
1303 &trigger_ht_element
->node
);
1304 if (node
!= &trigger_ht_element
->node
) {
1305 /* Not a fatal error, simply report it to the client. */
1306 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1307 goto error_free_ht_element
;
1311 * Ownership of the trigger and of its wrapper was transfered to
1314 trigger_ht_element
= NULL
;
1315 free_trigger
= false;
1318 * The rest only applies to triggers that have a "notify" action.
1319 * It is not skipped as this is the only action type currently
1322 client_list
= zmalloc(sizeof(*client_list
));
1325 goto error_free_ht_element
;
1327 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1328 CDS_INIT_LIST_HEAD(&client_list
->list
);
1329 client_list
->trigger
= trigger
;
1331 /* Build a list of clients to which this new trigger applies. */
1332 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1333 client_socket_ht_node
) {
1334 if (!trigger_applies_to_client(trigger
, client
)) {
1338 client_list_element
= zmalloc(sizeof(*client_list_element
));
1339 if (!client_list_element
) {
1341 goto error_free_client_list
;
1343 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1344 client_list_element
->client
= client
;
1345 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1348 cds_lfht_add(state
->notification_trigger_clients_ht
,
1349 lttng_condition_hash(condition
),
1350 &client_list
->notification_trigger_ht_node
);
1353 * Add the trigger to list of triggers bound to the channels currently
1356 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1358 struct lttng_trigger_list_element
*trigger_list_element
;
1359 struct lttng_channel_trigger_list
*trigger_list
;
1361 if (!trigger_applies_to_channel(trigger
, channel
)) {
1365 cds_lfht_lookup(state
->channel_triggers_ht
,
1366 hash_channel_key(&channel
->key
),
1367 match_channel_trigger_list
,
1370 node
= cds_lfht_iter_get_node(&iter
);
1372 trigger_list
= caa_container_of(node
,
1373 struct lttng_channel_trigger_list
,
1374 channel_triggers_ht_node
);
1376 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1377 if (!trigger_list_element
) {
1379 goto error_free_client_list
;
1381 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1382 trigger_list_element
->trigger
= trigger
;
1383 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1387 * Since there is nothing preventing clients from subscribing to a
1388 * condition before the corresponding trigger is registered, we have
1389 * to evaluate this new condition right away.
1391 * At some point, we were waiting for the next "evaluation" (e.g. on
1392 * reception of a channel sample) to evaluate this new condition, but
1395 * The reason it was broken is that waiting for the next sample
1396 * does not allow us to properly handle transitions for edge-triggered
1399 * Consider this example: when we handle a new channel sample, we
1400 * evaluate each conditions twice: once with the previous state, and
1401 * again with the newest state. We then use those two results to
1402 * determine whether a state change happened: a condition was false and
1403 * became true. If a state change happened, we have to notify clients.
1405 * Now, if a client subscribes to a given notification and registers
1406 * a trigger *after* that subscription, we have to make sure the
1407 * condition is evaluated at this point while considering only the
1408 * current state. Otherwise, the next evaluation cycle may only see
1409 * that the evaluations remain the same (true for samples n-1 and n) and
1410 * the client will never know that the condition has been met.
1412 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1413 &client_list
->list
, node
) {
1414 ret
= evaluate_condition_for_client(trigger
, condition
,
1415 client_list_element
->client
, state
);
1417 goto error_free_client_list
;
1422 * Client list ownership transferred to the
1423 * notification_trigger_clients_ht.
1427 *cmd_result
= LTTNG_OK
;
1428 error_free_client_list
:
1430 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1431 &client_list
->list
, node
) {
1432 free(client_list_element
);
1436 error_free_ht_element
:
1437 free(trigger_ht_element
);
1440 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1442 lttng_condition_destroy(condition
);
1443 lttng_action_destroy(action
);
1444 lttng_trigger_destroy(trigger
);
1451 int handle_notification_thread_command_unregister_trigger(
1452 struct notification_thread_state
*state
,
1453 struct lttng_trigger
*trigger
,
1454 enum lttng_error_code
*_cmd_reply
)
1456 struct cds_lfht_iter iter
;
1457 struct cds_lfht_node
*node
, *triggers_ht_node
;
1458 struct lttng_channel_trigger_list
*trigger_list
;
1459 struct notification_client_list
*client_list
;
1460 struct notification_client_list_element
*client_list_element
, *tmp
;
1461 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1462 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1464 struct lttng_action
*action
;
1465 enum lttng_error_code cmd_reply
;
1469 cds_lfht_lookup(state
->triggers_ht
,
1470 lttng_condition_hash(condition
),
1474 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1475 if (!triggers_ht_node
) {
1476 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1479 cmd_reply
= LTTNG_OK
;
1482 /* Remove trigger from channel_triggers_ht. */
1483 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1484 channel_triggers_ht_node
) {
1485 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1487 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1488 &trigger_list
->list
, node
) {
1489 struct lttng_condition
*current_condition
=
1490 lttng_trigger_get_condition(
1491 trigger_element
->trigger
);
1493 assert(current_condition
);
1494 if (!lttng_condition_is_equal(condition
,
1495 current_condition
)) {
1499 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1500 cds_list_del(&trigger_element
->node
);
1501 /* A trigger can only appear once per channel */
1507 * Remove and release the client list from
1508 * notification_trigger_clients_ht.
1510 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1511 lttng_condition_hash(condition
),
1515 node
= cds_lfht_iter_get_node(&iter
);
1517 client_list
= caa_container_of(node
, struct notification_client_list
,
1518 notification_trigger_ht_node
);
1519 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1520 &client_list
->list
, node
) {
1521 free(client_list_element
);
1523 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1526 /* Remove trigger from triggers_ht. */
1527 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1528 struct lttng_trigger_ht_element
, node
);
1529 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1531 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1532 lttng_condition_destroy(condition
);
1533 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1534 lttng_action_destroy(action
);
1535 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1536 free(trigger_ht_element
);
1540 *_cmd_reply
= cmd_reply
;
1545 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1546 int handle_notification_thread_command(
1547 struct notification_thread_handle
*handle
,
1548 struct notification_thread_state
*state
)
1552 struct notification_thread_command
*cmd
;
1554 /* Read the event pipe to put it back into a quiescent state. */
1555 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1561 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1562 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1563 struct notification_thread_command
, cmd_list_node
);
1564 switch (cmd
->type
) {
1565 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1566 DBG("[notification-thread] Received register trigger command");
1567 ret
= handle_notification_thread_command_register_trigger(
1568 state
, cmd
->parameters
.trigger
,
1571 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1572 DBG("[notification-thread] Received unregister trigger command");
1573 ret
= handle_notification_thread_command_unregister_trigger(
1574 state
, cmd
->parameters
.trigger
,
1577 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1578 DBG("[notification-thread] Received add channel command");
1579 ret
= handle_notification_thread_command_add_channel(
1581 cmd
->parameters
.add_channel
.session
.name
,
1582 cmd
->parameters
.add_channel
.session
.uid
,
1583 cmd
->parameters
.add_channel
.session
.gid
,
1584 cmd
->parameters
.add_channel
.channel
.name
,
1585 cmd
->parameters
.add_channel
.channel
.domain
,
1586 cmd
->parameters
.add_channel
.channel
.key
,
1587 cmd
->parameters
.add_channel
.channel
.capacity
,
1590 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1591 DBG("[notification-thread] Received remove channel command");
1592 ret
= handle_notification_thread_command_remove_channel(
1593 state
, cmd
->parameters
.remove_channel
.key
,
1594 cmd
->parameters
.remove_channel
.domain
,
1597 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1598 DBG("[notification-thread] Received quit command");
1599 cmd
->reply_code
= LTTNG_OK
;
1603 ERR("[notification-thread] Unknown internal command received");
1611 cds_list_del(&cmd
->cmd_list_node
);
1612 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1613 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1616 /* Wake-up and return a fatal error to the calling thread. */
1617 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1618 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1619 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1621 /* Indicate a fatal error to the caller. */
1626 unsigned long hash_client_socket(int socket
)
1628 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1632 int socket_set_non_blocking(int socket
)
1636 /* Set the pipe as non-blocking. */
1637 ret
= fcntl(socket
, F_GETFL
, 0);
1639 PERROR("fcntl get socket flags");
1644 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1646 PERROR("fcntl set O_NONBLOCK socket flag");
1649 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1655 int client_reset_inbound_state(struct notification_client
*client
)
1659 ret
= lttng_dynamic_buffer_set_size(
1660 &client
->communication
.inbound
.buffer
, 0);
1663 client
->communication
.inbound
.bytes_to_receive
=
1664 sizeof(struct lttng_notification_channel_message
);
1665 client
->communication
.inbound
.msg_type
=
1666 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1667 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1668 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1669 ret
= lttng_dynamic_buffer_set_size(
1670 &client
->communication
.inbound
.buffer
,
1671 client
->communication
.inbound
.bytes_to_receive
);
1675 int handle_notification_thread_client_connect(
1676 struct notification_thread_state
*state
)
1679 struct notification_client
*client
;
1681 DBG("[notification-thread] Handling new notification channel client connection");
1683 client
= zmalloc(sizeof(*client
));
1689 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1690 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1691 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1692 client
->communication
.inbound
.expect_creds
= true;
1693 ret
= client_reset_inbound_state(client
);
1695 ERR("[notification-thread] Failed to reset client communication's inbound state");
1700 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1702 ERR("[notification-thread] Failed to accept new notification channel client connection");
1707 client
->socket
= ret
;
1709 ret
= socket_set_non_blocking(client
->socket
);
1711 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1715 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1717 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1722 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1723 LPOLLIN
| LPOLLERR
|
1724 LPOLLHUP
| LPOLLRDHUP
);
1726 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1730 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1734 cds_lfht_add(state
->client_socket_ht
,
1735 hash_client_socket(client
->socket
),
1736 &client
->client_socket_ht_node
);
1741 notification_client_destroy(client
, state
);
1745 int handle_notification_thread_client_disconnect(
1747 struct notification_thread_state
*state
)
1750 struct notification_client
*client
;
1753 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1755 client
= get_client_from_socket(client_socket
, state
);
1757 /* Internal state corruption, fatal error. */
1758 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1764 ret
= lttng_poll_del(&state
->events
, client_socket
);
1766 ERR("[notification-thread] Failed to remove client socket from poll set");
1768 cds_lfht_del(state
->client_socket_ht
,
1769 &client
->client_socket_ht_node
);
1770 notification_client_destroy(client
, state
);
1776 int handle_notification_thread_client_disconnect_all(
1777 struct notification_thread_state
*state
)
1779 struct cds_lfht_iter iter
;
1780 struct notification_client
*client
;
1781 bool error_encoutered
= false;
1784 DBG("[notification-thread] Closing all client connections");
1785 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1786 client_socket_ht_node
) {
1789 ret
= handle_notification_thread_client_disconnect(
1790 client
->socket
, state
);
1792 error_encoutered
= true;
1796 return error_encoutered
? 1 : 0;
1799 int handle_notification_thread_trigger_unregister_all(
1800 struct notification_thread_state
*state
)
1802 bool error_occurred
= false;
1803 struct cds_lfht_iter iter
;
1804 struct lttng_trigger_ht_element
*trigger_ht_element
;
1806 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1808 int ret
= handle_notification_thread_command_unregister_trigger(
1809 state
, trigger_ht_element
->trigger
, NULL
);
1811 error_occurred
= true;
1814 return error_occurred
? -1 : 0;
1818 int client_flush_outgoing_queue(struct notification_client
*client
,
1819 struct notification_thread_state
*state
)
1822 size_t to_send_count
;
1824 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1825 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1826 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1829 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1830 client
->communication
.outbound
.buffer
.data
,
1832 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1833 (ret
> 0 && ret
< to_send_count
)) {
1834 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1836 to_send_count
-= max(ret
, 0);
1838 memcpy(client
->communication
.outbound
.buffer
.data
,
1839 client
->communication
.outbound
.buffer
.data
+
1840 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1842 ret
= lttng_dynamic_buffer_set_size(
1843 &client
->communication
.outbound
.buffer
,
1850 * We want to be notified whenever there is buffer space
1851 * available to send the rest of the payload.
1853 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1854 CLIENT_POLL_MASK_IN_OUT
);
1858 } else if (ret
< 0) {
1859 /* Generic error, disconnect the client. */
1860 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1862 ret
= handle_notification_thread_client_disconnect(
1863 client
->socket
, state
);
1868 /* No error and flushed the queue completely. */
1869 ret
= lttng_dynamic_buffer_set_size(
1870 &client
->communication
.outbound
.buffer
, 0);
1874 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1875 CLIENT_POLL_MASK_IN
);
1880 client
->communication
.outbound
.queued_command_reply
= false;
1881 client
->communication
.outbound
.dropped_notification
= false;
1890 int client_send_command_reply(struct notification_client
*client
,
1891 struct notification_thread_state
*state
,
1892 enum lttng_notification_channel_status status
)
1895 struct lttng_notification_channel_command_reply reply
= {
1896 .status
= (int8_t) status
,
1898 struct lttng_notification_channel_message msg
= {
1899 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1900 .size
= sizeof(reply
),
1902 char buffer
[sizeof(msg
) + sizeof(reply
)];
1904 if (client
->communication
.outbound
.queued_command_reply
) {
1905 /* Protocol error. */
1909 memcpy(buffer
, &msg
, sizeof(msg
));
1910 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1911 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1913 /* Enqueue buffer to outgoing queue and flush it. */
1914 ret
= lttng_dynamic_buffer_append(
1915 &client
->communication
.outbound
.buffer
,
1916 buffer
, sizeof(buffer
));
1921 ret
= client_flush_outgoing_queue(client
, state
);
1926 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1927 /* Queue could not be emptied. */
1928 client
->communication
.outbound
.queued_command_reply
= true;
1937 int client_dispatch_message(struct notification_client
*client
,
1938 struct notification_thread_state
*state
)
1942 if (client
->communication
.inbound
.msg_type
!=
1943 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1944 client
->communication
.inbound
.msg_type
!=
1945 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1946 !client
->validated
) {
1947 WARN("[notification-thread] client attempted a command before handshake");
1952 switch (client
->communication
.inbound
.msg_type
) {
1953 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1956 * Receiving message header. The function will be called again
1957 * once the rest of the message as been received and can be
1960 const struct lttng_notification_channel_message
*msg
;
1962 assert(sizeof(*msg
) ==
1963 client
->communication
.inbound
.buffer
.size
);
1964 msg
= (const struct lttng_notification_channel_message
*)
1965 client
->communication
.inbound
.buffer
.data
;
1967 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1968 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1973 switch (msg
->type
) {
1974 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1975 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1976 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1980 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1984 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1985 client
->communication
.inbound
.msg_type
=
1986 (enum lttng_notification_channel_message_type
) msg
->type
;
1987 ret
= lttng_dynamic_buffer_set_size(
1988 &client
->communication
.inbound
.buffer
, msg
->size
);
1994 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1996 struct lttng_notification_channel_command_handshake
*handshake_client
;
1997 struct lttng_notification_channel_command_handshake handshake_reply
= {
1998 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1999 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2001 struct lttng_notification_channel_message msg_header
= {
2002 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2003 .size
= sizeof(handshake_reply
),
2005 enum lttng_notification_channel_status status
=
2006 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2007 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2009 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2010 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2011 sizeof(handshake_reply
));
2014 (struct lttng_notification_channel_command_handshake
*)
2015 client
->communication
.inbound
.buffer
.data
;
2016 client
->major
= handshake_client
->major
;
2017 client
->minor
= handshake_client
->minor
;
2018 if (!client
->communication
.inbound
.creds_received
) {
2019 ERR("[notification-thread] No credentials received from client");
2024 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2025 &client
->communication
.inbound
.creds
);
2026 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2027 &client
->communication
.inbound
.creds
);
2028 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2029 client
->uid
, client
->gid
, (int) client
->major
,
2030 (int) client
->minor
);
2032 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2033 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2036 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2037 send_buffer
, sizeof(send_buffer
));
2039 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2043 ret
= client_flush_outgoing_queue(client
, state
);
2048 ret
= client_send_command_reply(client
, state
, status
);
2050 ERR("[notification-thread] Failed to send reply to notification channel client");
2054 /* Set reception state to receive the next message header. */
2055 ret
= client_reset_inbound_state(client
);
2057 ERR("[notification-thread] Failed to reset client communication's inbound state");
2060 client
->validated
= true;
2063 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2064 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2066 struct lttng_condition
*condition
;
2067 enum lttng_notification_channel_status status
=
2068 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2069 const struct lttng_buffer_view condition_view
=
2070 lttng_buffer_view_from_dynamic_buffer(
2071 &client
->communication
.inbound
.buffer
,
2073 size_t expected_condition_size
=
2074 client
->communication
.inbound
.buffer
.size
;
2076 ret
= lttng_condition_create_from_buffer(&condition_view
,
2078 if (ret
!= expected_condition_size
) {
2079 ERR("[notification-thread] Malformed condition received from client");
2083 if (client
->communication
.inbound
.msg_type
==
2084 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2085 ret
= notification_thread_client_subscribe(client
,
2086 condition
, state
, &status
);
2088 ret
= notification_thread_client_unsubscribe(client
,
2089 condition
, state
, &status
);
2095 ret
= client_send_command_reply(client
, state
, status
);
2097 ERR("[notification-thread] Failed to send reply to notification channel client");
2101 /* Set reception state to receive the next message header. */
2102 ret
= client_reset_inbound_state(client
);
2104 ERR("[notification-thread] Failed to reset client communication's inbound state");
2116 /* Incoming data from client. */
2117 int handle_notification_thread_client_in(
2118 struct notification_thread_state
*state
, int socket
)
2121 struct notification_client
*client
;
2125 client
= get_client_from_socket(socket
, state
);
2127 /* Internal error, abort. */
2132 offset
= client
->communication
.inbound
.buffer
.size
-
2133 client
->communication
.inbound
.bytes_to_receive
;
2134 if (client
->communication
.inbound
.expect_creds
) {
2135 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2136 client
->communication
.inbound
.buffer
.data
+ offset
,
2137 client
->communication
.inbound
.bytes_to_receive
,
2138 &client
->communication
.inbound
.creds
);
2140 client
->communication
.inbound
.expect_creds
= false;
2141 client
->communication
.inbound
.creds_received
= true;
2144 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2145 client
->communication
.inbound
.buffer
.data
+ offset
,
2146 client
->communication
.inbound
.bytes_to_receive
);
2149 goto error_disconnect_client
;
2152 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2153 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2154 ret
= client_dispatch_message(client
, state
);
2157 * Only returns an error if this client must be
2160 goto error_disconnect_client
;
2167 error_disconnect_client
:
2168 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2172 /* Client ready to receive outgoing data. */
2173 int handle_notification_thread_client_out(
2174 struct notification_thread_state
*state
, int socket
)
2177 struct notification_client
*client
;
2179 client
= get_client_from_socket(socket
, state
);
2181 /* Internal error, abort. */
2186 ret
= client_flush_outgoing_queue(client
, state
);
2195 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2196 const struct channel_state_sample
*sample
,
2197 uint64_t buffer_capacity
)
2199 bool result
= false;
2201 enum lttng_condition_type condition_type
;
2202 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2203 condition
, struct lttng_condition_buffer_usage
,
2206 if (use_condition
->threshold_bytes
.set
) {
2207 threshold
= use_condition
->threshold_bytes
.value
;
2210 * Threshold was expressed as a ratio.
2212 * TODO the threshold (in bytes) of conditions expressed
2213 * as a ratio of total buffer size could be cached to
2214 * forego this double-multiplication or it could be performed
2215 * as fixed-point math.
2217 * Note that caching should accomodate the case where the
2218 * condition applies to multiple channels (i.e. don't assume
2219 * that all channels matching my_chann* have the same size...)
2221 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2222 (double) buffer_capacity
);
2225 condition_type
= lttng_condition_get_type(condition
);
2226 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2227 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2228 threshold
, sample
->highest_usage
);
2231 * The low condition should only be triggered once _all_ of the
2232 * streams in a channel have gone below the "low" threshold.
2234 if (sample
->highest_usage
<= threshold
) {
2238 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2239 threshold
, sample
->highest_usage
);
2242 * For high buffer usage scenarios, we want to trigger whenever
2243 * _any_ of the streams has reached the "high" threshold.
2245 if (sample
->highest_usage
>= threshold
) {
2254 bool evaluate_session_consumed_size_condition(
2255 const struct lttng_condition
*condition
,
2256 uint64_t session_consumed_size
)
2259 const struct lttng_condition_session_consumed_size
*size_condition
=
2260 container_of(condition
,
2261 struct lttng_condition_session_consumed_size
,
2264 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2265 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2266 threshold
, session_consumed_size
);
2267 return session_consumed_size
>= threshold
;
2271 int evaluate_condition(const struct lttng_condition
*condition
,
2272 struct lttng_evaluation
**evaluation
,
2273 const struct notification_thread_state
*state
,
2274 const struct channel_state_sample
*previous_sample
,
2275 const struct channel_state_sample
*latest_sample
,
2276 uint64_t previous_session_consumed_total
,
2277 uint64_t latest_session_consumed_total
,
2278 struct channel_info
*channel_info
)
2281 enum lttng_condition_type condition_type
;
2282 const bool previous_sample_available
= !!previous_sample
;
2283 bool previous_sample_result
= false;
2284 bool latest_sample_result
;
2286 condition_type
= lttng_condition_get_type(condition
);
2288 switch (condition_type
) {
2289 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2290 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2291 if (caa_likely(previous_sample_available
)) {
2292 previous_sample_result
=
2293 evaluate_buffer_usage_condition(condition
,
2294 previous_sample
, channel_info
->capacity
);
2296 latest_sample_result
= evaluate_buffer_usage_condition(
2297 condition
, latest_sample
,
2298 channel_info
->capacity
);
2300 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2301 if (caa_likely(previous_sample_available
)) {
2302 previous_sample_result
=
2303 evaluate_session_consumed_size_condition(
2305 previous_session_consumed_total
);
2307 latest_sample_result
=
2308 evaluate_session_consumed_size_condition(
2310 latest_session_consumed_total
);
2313 /* Unknown condition type; internal error. */
2317 if (!latest_sample_result
||
2318 (previous_sample_result
== latest_sample_result
)) {
2320 * Only trigger on a condition evaluation transition.
2322 * NOTE: This edge-triggered logic may not be appropriate for
2323 * future condition types.
2328 if (!evaluation
|| !latest_sample_result
) {
2332 switch (condition_type
) {
2333 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2334 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2335 *evaluation
= lttng_evaluation_buffer_usage_create(
2337 latest_sample
->highest_usage
,
2338 channel_info
->capacity
);
2340 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2341 *evaluation
= lttng_evaluation_session_consumed_size_create(
2343 latest_session_consumed_total
);
2358 int client_enqueue_dropped_notification(struct notification_client
*client
,
2359 struct notification_thread_state
*state
)
2362 struct lttng_notification_channel_message msg
= {
2363 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2367 ret
= lttng_dynamic_buffer_append(
2368 &client
->communication
.outbound
.buffer
, &msg
,
2374 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2375 struct lttng_evaluation
*evaluation
,
2376 struct notification_client_list
* client_list
,
2377 struct notification_thread_state
*state
,
2378 uid_t channel_uid
, gid_t channel_gid
)
2381 struct lttng_dynamic_buffer msg_buffer
;
2382 struct notification_client_list_element
*client_list_element
, *tmp
;
2383 struct lttng_notification
*notification
;
2384 struct lttng_condition
*condition
;
2385 ssize_t expected_notification_size
, notification_size
;
2386 struct lttng_notification_channel_message msg
;
2388 lttng_dynamic_buffer_init(&msg_buffer
);
2390 condition
= lttng_trigger_get_condition(trigger
);
2393 notification
= lttng_notification_create(condition
, evaluation
);
2394 if (!notification
) {
2399 expected_notification_size
= lttng_notification_serialize(notification
,
2401 if (expected_notification_size
< 0) {
2402 ERR("[notification-thread] Failed to get size of serialized notification");
2407 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2408 msg
.size
= (uint32_t) expected_notification_size
;
2409 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2414 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2415 msg_buffer
.size
+ expected_notification_size
);
2420 notification_size
= lttng_notification_serialize(notification
,
2421 msg_buffer
.data
+ sizeof(msg
));
2422 if (notification_size
!= expected_notification_size
) {
2423 ERR("[notification-thread] Failed to serialize notification");
2428 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2429 &client_list
->list
, node
) {
2430 struct notification_client
*client
=
2431 client_list_element
->client
;
2433 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2435 /* Client is not allowed to monitor this channel. */
2436 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2440 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2441 client
->socket
, msg_buffer
.size
);
2442 if (client
->communication
.outbound
.buffer
.size
) {
2444 * Outgoing data is already buffered for this client;
2445 * drop the notification and enqueue a "dropped
2446 * notification" message if this is the first dropped
2447 * notification since the socket spilled-over to the
2450 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2452 if (!client
->communication
.outbound
.dropped_notification
) {
2453 client
->communication
.outbound
.dropped_notification
= true;
2454 ret
= client_enqueue_dropped_notification(
2463 ret
= lttng_dynamic_buffer_append_buffer(
2464 &client
->communication
.outbound
.buffer
,
2470 ret
= client_flush_outgoing_queue(client
, state
);
2477 lttng_notification_destroy(notification
);
2478 lttng_dynamic_buffer_reset(&msg_buffer
);
2482 int handle_notification_thread_channel_sample(
2483 struct notification_thread_state
*state
, int pipe
,
2484 enum lttng_domain_type domain
)
2487 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2488 struct channel_info
*channel_info
;
2489 struct cds_lfht_node
*node
;
2490 struct cds_lfht_iter iter
;
2491 struct lttng_channel_trigger_list
*trigger_list
;
2492 struct lttng_trigger_list_element
*trigger_list_element
;
2493 bool previous_sample_available
= false;
2494 struct channel_state_sample previous_sample
, latest_sample
;
2495 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2498 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2499 * ensuring that read/write of sampling messages are atomic.
2501 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2502 if (ret
!= sizeof(sample_msg
)) {
2503 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2510 latest_sample
.key
.key
= sample_msg
.key
;
2511 latest_sample
.key
.domain
= domain
;
2512 latest_sample
.highest_usage
= sample_msg
.highest
;
2513 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2514 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2518 /* Retrieve the channel's informations */
2519 cds_lfht_lookup(state
->channels_ht
,
2520 hash_channel_key(&latest_sample
.key
),
2524 node
= cds_lfht_iter_get_node(&iter
);
2525 if (caa_unlikely(!node
)) {
2527 * Not an error since the consumer can push a sample to the pipe
2528 * and the rest of the session daemon could notify us of the
2529 * channel's destruction before we get a chance to process that
2532 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2533 latest_sample
.key
.key
,
2534 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2538 channel_info
= caa_container_of(node
, struct channel_info
,
2540 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2542 latest_sample
.key
.key
,
2543 channel_info
->session_info
->name
,
2544 latest_sample
.highest_usage
,
2545 latest_sample
.lowest_usage
,
2546 latest_sample
.channel_total_consumed
);
2548 previous_session_consumed_total
=
2549 channel_info
->session_info
->consumed_data_size
;
2551 /* Retrieve the channel's last sample, if it exists, and update it. */
2552 cds_lfht_lookup(state
->channel_state_ht
,
2553 hash_channel_key(&latest_sample
.key
),
2554 match_channel_state_sample
,
2557 node
= cds_lfht_iter_get_node(&iter
);
2558 if (caa_likely(node
)) {
2559 struct channel_state_sample
*stored_sample
;
2561 /* Update the sample stored. */
2562 stored_sample
= caa_container_of(node
,
2563 struct channel_state_sample
,
2564 channel_state_ht_node
);
2566 memcpy(&previous_sample
, stored_sample
,
2567 sizeof(previous_sample
));
2568 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2569 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2570 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2571 previous_sample_available
= true;
2573 latest_session_consumed_total
=
2574 previous_session_consumed_total
+
2575 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2578 * This is the channel's first sample, allocate space for and
2579 * store the new sample.
2581 struct channel_state_sample
*stored_sample
;
2583 stored_sample
= zmalloc(sizeof(*stored_sample
));
2584 if (!stored_sample
) {
2589 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2590 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2591 cds_lfht_add(state
->channel_state_ht
,
2592 hash_channel_key(&stored_sample
->key
),
2593 &stored_sample
->channel_state_ht_node
);
2595 latest_session_consumed_total
=
2596 previous_session_consumed_total
+
2597 latest_sample
.channel_total_consumed
;
2600 channel_info
->session_info
->consumed_data_size
=
2601 latest_session_consumed_total
;
2603 /* Find triggers associated with this channel. */
2604 cds_lfht_lookup(state
->channel_triggers_ht
,
2605 hash_channel_key(&latest_sample
.key
),
2606 match_channel_trigger_list
,
2609 node
= cds_lfht_iter_get_node(&iter
);
2610 if (caa_likely(!node
)) {
2614 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2615 channel_triggers_ht_node
);
2616 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2618 struct lttng_condition
*condition
;
2619 struct lttng_action
*action
;
2620 struct lttng_trigger
*trigger
;
2621 struct notification_client_list
*client_list
;
2622 struct lttng_evaluation
*evaluation
= NULL
;
2624 trigger
= trigger_list_element
->trigger
;
2625 condition
= lttng_trigger_get_condition(trigger
);
2627 action
= lttng_trigger_get_action(trigger
);
2629 /* Notify actions are the only type currently supported. */
2630 assert(lttng_action_get_type(action
) ==
2631 LTTNG_ACTION_TYPE_NOTIFY
);
2634 * Check if any client is subscribed to the result of this
2637 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2638 lttng_condition_hash(condition
),
2642 node
= cds_lfht_iter_get_node(&iter
);
2645 client_list
= caa_container_of(node
,
2646 struct notification_client_list
,
2647 notification_trigger_ht_node
);
2648 if (cds_list_empty(&client_list
->list
)) {
2650 * No clients interested in the evaluation's result,
2656 ret
= evaluate_condition(condition
, &evaluation
, state
,
2657 previous_sample_available
? &previous_sample
: NULL
,
2659 previous_session_consumed_total
,
2660 latest_session_consumed_total
,
2662 if (caa_unlikely(ret
)) {
2666 if (caa_likely(!evaluation
)) {
2670 /* Dispatch evaluation result to all clients. */
2671 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2672 evaluation
, client_list
, state
,
2673 channel_info
->session_info
->uid
,
2674 channel_info
->session_info
->gid
);
2675 lttng_evaluation_destroy(evaluation
);
2676 if (caa_unlikely(ret
)) {