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
;
402 assert(session_info
);
403 if (session_info
->channel_infos_ht
) {
404 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
406 ERR("[notification-thread] Failed to destroy channel information hash table");
409 free(session_info
->name
);
414 void session_info_get(struct session_info
*session_info
)
419 lttng_ref_get(&session_info
->ref
);
423 void session_info_put(struct session_info
*session_info
)
428 lttng_ref_put(&session_info
->ref
);
432 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
)
434 struct session_info
*session_info
;
438 session_info
= zmalloc(sizeof(*session_info
));
442 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
444 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
445 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
446 if (!session_info
->channel_infos_ht
) {
450 cds_lfht_node_init(&session_info
->sessions_ht_node
);
451 session_info
->name
= strdup(name
);
452 if (!session_info
->name
) {
455 session_info
->uid
= uid
;
456 session_info
->gid
= gid
;
460 session_info_put(session_info
);
465 void session_info_add_channel(struct session_info
*session_info
,
466 struct channel_info
*channel_info
)
469 cds_lfht_add(session_info
->channel_infos_ht
,
470 hash_channel_key(&channel_info
->key
),
471 &channel_info
->session_info_channels_ht_node
);
476 void session_info_remove_channel(struct session_info
*session_info
,
477 struct channel_info
*channel_info
)
480 cds_lfht_del(session_info
->channel_infos_ht
,
481 &channel_info
->session_info_channels_ht_node
);
486 struct channel_info
*channel_info_create(const char *channel_name
,
487 struct channel_key
*channel_key
, uint64_t channel_capacity
,
488 struct session_info
*session_info
)
490 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
496 cds_lfht_node_init(&channel_info
->channels_ht_node
);
497 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
498 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
499 channel_info
->capacity
= channel_capacity
;
501 channel_info
->name
= strdup(channel_name
);
502 if (!channel_info
->name
) {
507 * Set the references between session and channel infos:
508 * - channel_info holds a strong reference to session_info
509 * - session_info holds a weak reference to channel_info
511 session_info_get(session_info
);
512 session_info_add_channel(session_info
, channel_info
);
513 channel_info
->session_info
= session_info
;
517 channel_info_destroy(channel_info
);
521 /* This function must be called with the RCU read lock held. */
523 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
524 struct lttng_condition
*condition
,
525 struct notification_client
*client
,
526 struct notification_thread_state
*state
)
529 struct cds_lfht_iter iter
;
530 struct cds_lfht_node
*node
;
531 struct channel_info
*channel_info
= NULL
;
532 struct channel_key
*channel_key
= NULL
;
533 struct channel_state_sample
*last_sample
= NULL
;
534 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
535 struct lttng_evaluation
*evaluation
= NULL
;
536 struct notification_client_list client_list
= { 0 };
537 struct notification_client_list_element client_list_element
= { 0 };
544 /* Find the channel associated with the trigger. */
545 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
546 channel_trigger_list
, channel_triggers_ht_node
) {
547 struct lttng_trigger_list_element
*element
;
549 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
550 struct lttng_condition
*current_condition
=
551 lttng_trigger_get_condition(
554 assert(current_condition
);
555 if (!lttng_condition_is_equal(condition
,
556 current_condition
)) {
560 /* Found the trigger, save the channel key. */
561 channel_key
= &channel_trigger_list
->channel_key
;
565 /* The channel key was found stop iteration. */
571 /* No channel found; normal exit. */
572 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
577 /* Fetch channel info for the matching channel. */
578 cds_lfht_lookup(state
->channels_ht
,
579 hash_channel_key(channel_key
),
583 node
= cds_lfht_iter_get_node(&iter
);
585 channel_info
= caa_container_of(node
, struct channel_info
,
588 /* Retrieve the channel's last sample, if it exists. */
589 cds_lfht_lookup(state
->channel_state_ht
,
590 hash_channel_key(channel_key
),
591 match_channel_state_sample
,
594 node
= cds_lfht_iter_get_node(&iter
);
596 last_sample
= caa_container_of(node
,
597 struct channel_state_sample
,
598 channel_state_ht_node
);
600 /* Nothing to evaluate, no sample was ever taken. Normal exit */
601 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
606 ret
= evaluate_condition(condition
, &evaluation
, state
,
608 0, channel_info
->session_info
->consumed_data_size
,
611 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
616 /* Evaluation yielded nothing. Normal exit. */
617 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
623 * Create a temporary client list with the client currently
626 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
627 CDS_INIT_LIST_HEAD(&client_list
.list
);
628 client_list
.trigger
= trigger
;
630 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
631 client_list_element
.client
= client
;
632 cds_list_add(&client_list_element
.node
, &client_list
.list
);
634 /* Send evaluation result to the newly-subscribed client. */
635 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
636 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
637 state
, channel_info
->session_info
->uid
,
638 channel_info
->session_info
->gid
);
645 int notification_thread_client_subscribe(struct notification_client
*client
,
646 struct lttng_condition
*condition
,
647 struct notification_thread_state
*state
,
648 enum lttng_notification_channel_status
*_status
)
651 struct cds_lfht_iter iter
;
652 struct cds_lfht_node
*node
;
653 struct notification_client_list
*client_list
;
654 struct lttng_condition_list_element
*condition_list_element
= NULL
;
655 struct notification_client_list_element
*client_list_element
= NULL
;
656 enum lttng_notification_channel_status status
=
657 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
660 * Ensure that the client has not already subscribed to this condition
663 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
664 if (lttng_condition_is_equal(condition_list_element
->condition
,
666 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
671 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
672 if (!condition_list_element
) {
676 client_list_element
= zmalloc(sizeof(*client_list_element
));
677 if (!client_list_element
) {
685 * Add the newly-subscribed condition to the client's subscription list.
687 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
688 condition_list_element
->condition
= condition
;
689 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
691 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
692 lttng_condition_hash(condition
),
693 match_client_list_condition
,
696 node
= cds_lfht_iter_get_node(&iter
);
699 * No notification-emiting trigger registered with this
700 * condition. We don't evaluate the condition right away
701 * since this trigger is not registered yet.
703 free(client_list_element
);
707 client_list
= caa_container_of(node
, struct notification_client_list
,
708 notification_trigger_ht_node
);
710 * The condition to which the client just subscribed is evaluated
711 * at this point so that conditions that are already TRUE result
712 * in a notification being sent out.
714 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
716 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
722 * Add the client to the list of clients interested in a given trigger
723 * if a "notification" trigger with a corresponding condition was
726 client_list_element
->client
= client
;
727 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
728 cds_list_add(&client_list_element
->node
, &client_list
->list
);
737 free(condition_list_element
);
738 free(client_list_element
);
743 int notification_thread_client_unsubscribe(
744 struct notification_client
*client
,
745 struct lttng_condition
*condition
,
746 struct notification_thread_state
*state
,
747 enum lttng_notification_channel_status
*_status
)
749 struct cds_lfht_iter iter
;
750 struct cds_lfht_node
*node
;
751 struct notification_client_list
*client_list
;
752 struct lttng_condition_list_element
*condition_list_element
,
754 struct notification_client_list_element
*client_list_element
,
756 bool condition_found
= false;
757 enum lttng_notification_channel_status status
=
758 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
760 /* Remove the condition from the client's condition list. */
761 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
762 &client
->condition_list
, node
) {
763 if (!lttng_condition_is_equal(condition_list_element
->condition
,
768 cds_list_del(&condition_list_element
->node
);
770 * The caller may be iterating on the client's conditions to
771 * tear down a client's connection. In this case, the condition
772 * will be destroyed at the end.
774 if (condition
!= condition_list_element
->condition
) {
775 lttng_condition_destroy(
776 condition_list_element
->condition
);
778 free(condition_list_element
);
779 condition_found
= true;
783 if (!condition_found
) {
784 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
789 * Remove the client from the list of clients interested the trigger
790 * matching the condition.
793 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
794 lttng_condition_hash(condition
),
795 match_client_list_condition
,
798 node
= cds_lfht_iter_get_node(&iter
);
803 client_list
= caa_container_of(node
, struct notification_client_list
,
804 notification_trigger_ht_node
);
805 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
806 &client_list
->list
, node
) {
807 if (client_list_element
->client
->socket
!= client
->socket
) {
810 cds_list_del(&client_list_element
->node
);
811 free(client_list_element
);
817 lttng_condition_destroy(condition
);
825 void notification_client_destroy(struct notification_client
*client
,
826 struct notification_thread_state
*state
)
828 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
834 /* Release all conditions to which the client was subscribed. */
835 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
836 &client
->condition_list
, node
) {
837 (void) notification_thread_client_unsubscribe(client
,
838 condition_list_element
->condition
, state
, NULL
);
841 if (client
->socket
>= 0) {
842 (void) lttcomm_close_unix_sock(client
->socket
);
844 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
845 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
850 * Call with rcu_read_lock held (and hold for the lifetime of the returned
854 struct notification_client
*get_client_from_socket(int socket
,
855 struct notification_thread_state
*state
)
857 struct cds_lfht_iter iter
;
858 struct cds_lfht_node
*node
;
859 struct notification_client
*client
= NULL
;
861 cds_lfht_lookup(state
->client_socket_ht
,
862 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
864 (void *) (unsigned long) socket
,
866 node
= cds_lfht_iter_get_node(&iter
);
871 client
= caa_container_of(node
, struct notification_client
,
872 client_socket_ht_node
);
878 bool buffer_usage_condition_applies_to_channel(
879 struct lttng_condition
*condition
,
880 struct channel_info
*channel_info
)
882 enum lttng_condition_status status
;
883 enum lttng_domain_type condition_domain
;
884 const char *condition_session_name
= NULL
;
885 const char *condition_channel_name
= NULL
;
887 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
889 assert(status
== LTTNG_CONDITION_STATUS_OK
);
890 if (channel_info
->key
.domain
!= condition_domain
) {
894 status
= lttng_condition_buffer_usage_get_session_name(
895 condition
, &condition_session_name
);
896 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
898 status
= lttng_condition_buffer_usage_get_channel_name(
899 condition
, &condition_channel_name
);
900 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
902 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
905 if (strcmp(channel_info
->name
, condition_channel_name
)) {
915 bool session_consumed_size_condition_applies_to_channel(
916 struct lttng_condition
*condition
,
917 struct channel_info
*channel_info
)
919 enum lttng_condition_status status
;
920 const char *condition_session_name
= NULL
;
922 status
= lttng_condition_session_consumed_size_get_session_name(
923 condition
, &condition_session_name
);
924 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
926 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
936 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
937 struct channel_info
*channel_info
)
939 struct lttng_condition
*condition
;
940 bool trigger_applies
;
942 condition
= lttng_trigger_get_condition(trigger
);
947 switch (lttng_condition_get_type(condition
)) {
948 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
949 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
950 trigger_applies
= buffer_usage_condition_applies_to_channel(
951 condition
, channel_info
);
953 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
954 trigger_applies
= session_consumed_size_condition_applies_to_channel(
955 condition
, channel_info
);
961 return trigger_applies
;
967 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
968 struct notification_client
*client
)
970 bool applies
= false;
971 struct lttng_condition_list_element
*condition_list_element
;
973 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
975 applies
= lttng_condition_is_equal(
976 condition_list_element
->condition
,
977 lttng_trigger_get_condition(trigger
));
986 int match_session(struct cds_lfht_node
*node
, const void *key
)
988 const char *name
= key
;
989 struct session_info
*session_info
= caa_container_of(
990 node
, struct session_info
, sessions_ht_node
);
992 return !strcmp(session_info
->name
, name
);
996 struct session_info
*find_or_create_session_info(
997 struct notification_thread_state
*state
,
998 const char *name
, uid_t uid
, gid_t gid
)
1000 struct session_info
*session
= NULL
;
1001 struct cds_lfht_node
*node
;
1002 struct cds_lfht_iter iter
;
1005 cds_lfht_lookup(state
->sessions_ht
,
1006 hash_key_str(name
, lttng_ht_seed
),
1010 node
= cds_lfht_iter_get_node(&iter
);
1012 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1014 session
= caa_container_of(node
, struct session_info
,
1016 assert(session
->uid
== uid
);
1017 assert(session
->gid
== gid
);
1021 session
= session_info_create(name
, uid
, gid
);
1023 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1033 int handle_notification_thread_command_add_channel(
1034 struct notification_thread_state
*state
,
1035 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1036 const char *channel_name
, enum lttng_domain_type channel_domain
,
1037 uint64_t channel_key_int
, uint64_t channel_capacity
,
1038 enum lttng_error_code
*cmd_result
)
1040 struct cds_list_head trigger_list
;
1041 struct channel_info
*new_channel_info
= NULL
;
1042 struct channel_key channel_key
= {
1043 .key
= channel_key_int
,
1044 .domain
= channel_domain
,
1046 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1047 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1048 int trigger_count
= 0;
1049 struct cds_lfht_iter iter
;
1050 struct session_info
*session_info
= NULL
;
1052 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1053 channel_name
, session_name
, channel_key_int
,
1054 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1056 CDS_INIT_LIST_HEAD(&trigger_list
);
1058 session_info
= find_or_create_session_info(state
, session_name
,
1059 session_uid
, session_gid
);
1060 if (!session_info
) {
1061 /* Allocation error or an internal error occured. */
1065 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1066 channel_capacity
, session_info
);
1067 if (!new_channel_info
) {
1071 /* Build a list of all triggers applying to the new channel. */
1072 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1074 struct lttng_trigger_list_element
*new_element
;
1076 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1077 new_channel_info
)) {
1081 new_element
= zmalloc(sizeof(*new_element
));
1085 CDS_INIT_LIST_HEAD(&new_element
->node
);
1086 new_element
->trigger
= trigger_ht_element
->trigger
;
1087 cds_list_add(&new_element
->node
, &trigger_list
);
1091 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1093 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1094 if (!channel_trigger_list
) {
1097 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1098 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1099 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1100 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1103 /* Add channel to the channel_ht which owns the channel_infos. */
1104 cds_lfht_add(state
->channels_ht
,
1105 hash_channel_key(&new_channel_info
->key
),
1106 &new_channel_info
->channels_ht_node
);
1108 * Add the list of triggers associated with this channel to the
1109 * channel_triggers_ht.
1111 cds_lfht_add(state
->channel_triggers_ht
,
1112 hash_channel_key(&new_channel_info
->key
),
1113 &channel_trigger_list
->channel_triggers_ht_node
);
1115 *cmd_result
= LTTNG_OK
;
1118 channel_info_destroy(new_channel_info
);
1119 session_info_put(session_info
);
1124 int handle_notification_thread_command_remove_channel(
1125 struct notification_thread_state
*state
,
1126 uint64_t channel_key
, enum lttng_domain_type domain
,
1127 enum lttng_error_code
*cmd_result
)
1129 struct cds_lfht_node
*node
;
1130 struct cds_lfht_iter iter
;
1131 struct lttng_channel_trigger_list
*trigger_list
;
1132 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1133 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1134 struct channel_info
*channel_info
;
1136 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1137 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1141 cds_lfht_lookup(state
->channel_triggers_ht
,
1142 hash_channel_key(&key
),
1143 match_channel_trigger_list
,
1146 node
= cds_lfht_iter_get_node(&iter
);
1148 * There is a severe internal error if we are being asked to remove a
1149 * channel that doesn't exist.
1152 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1156 /* Free the list of triggers associated with this channel. */
1157 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1158 channel_triggers_ht_node
);
1159 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1160 &trigger_list
->list
, node
) {
1161 cds_list_del(&trigger_list_element
->node
);
1162 free(trigger_list_element
);
1164 cds_lfht_del(state
->channel_triggers_ht
, node
);
1167 /* Free sampled channel state. */
1168 cds_lfht_lookup(state
->channel_state_ht
,
1169 hash_channel_key(&key
),
1170 match_channel_state_sample
,
1173 node
= cds_lfht_iter_get_node(&iter
);
1175 * This is expected to be NULL if the channel is destroyed before we
1176 * received a sample.
1179 struct channel_state_sample
*sample
= caa_container_of(node
,
1180 struct channel_state_sample
,
1181 channel_state_ht_node
);
1183 cds_lfht_del(state
->channel_state_ht
, node
);
1187 /* Remove the channel from the channels_ht and free it. */
1188 cds_lfht_lookup(state
->channels_ht
,
1189 hash_channel_key(&key
),
1193 node
= cds_lfht_iter_get_node(&iter
);
1195 channel_info
= caa_container_of(node
, struct channel_info
,
1197 cds_lfht_del(state
->channels_ht
, node
);
1198 channel_info_destroy(channel_info
);
1201 *cmd_result
= LTTNG_OK
;
1206 int condition_is_supported(struct lttng_condition
*condition
)
1210 switch (lttng_condition_get_type(condition
)) {
1211 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1212 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1214 enum lttng_domain_type domain
;
1216 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1223 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1229 * Older kernel tracers don't expose the API to monitor their
1230 * buffers. Therefore, we reject triggers that require that
1231 * mechanism to be available to be evaluated.
1233 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1245 * FIXME A client's credentials are not checked when registering a trigger, nor
1246 * are they stored alongside with the trigger.
1248 * The effects of this are benign since:
1249 * - The client will succeed in registering the trigger, as it is valid,
1250 * - The trigger will, internally, be bound to the channel,
1251 * - The notifications will not be sent since the client's credentials
1252 * are checked against the channel at that moment.
1254 * If this function returns a non-zero value, it means something is
1255 * fundamentally broken and the whole subsystem/thread will be torn down.
1257 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1261 int handle_notification_thread_command_register_trigger(
1262 struct notification_thread_state
*state
,
1263 struct lttng_trigger
*trigger
,
1264 enum lttng_error_code
*cmd_result
)
1267 struct lttng_condition
*condition
;
1268 struct notification_client
*client
;
1269 struct notification_client_list
*client_list
= NULL
;
1270 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1271 struct notification_client_list_element
*client_list_element
, *tmp
;
1272 struct cds_lfht_node
*node
;
1273 struct cds_lfht_iter iter
;
1274 struct channel_info
*channel
;
1275 bool free_trigger
= true;
1279 condition
= lttng_trigger_get_condition(trigger
);
1282 ret
= condition_is_supported(condition
);
1285 } else if (ret
== 0) {
1286 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1289 /* Feature is supported, continue. */
1293 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1294 if (!trigger_ht_element
) {
1299 /* Add trigger to the trigger_ht. */
1300 cds_lfht_node_init(&trigger_ht_element
->node
);
1301 trigger_ht_element
->trigger
= trigger
;
1303 node
= cds_lfht_add_unique(state
->triggers_ht
,
1304 lttng_condition_hash(condition
),
1307 &trigger_ht_element
->node
);
1308 if (node
!= &trigger_ht_element
->node
) {
1309 /* Not a fatal error, simply report it to the client. */
1310 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1311 goto error_free_ht_element
;
1315 * Ownership of the trigger and of its wrapper was transfered to
1318 trigger_ht_element
= NULL
;
1319 free_trigger
= false;
1322 * The rest only applies to triggers that have a "notify" action.
1323 * It is not skipped as this is the only action type currently
1326 client_list
= zmalloc(sizeof(*client_list
));
1329 goto error_free_ht_element
;
1331 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1332 CDS_INIT_LIST_HEAD(&client_list
->list
);
1333 client_list
->trigger
= trigger
;
1335 /* Build a list of clients to which this new trigger applies. */
1336 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1337 client_socket_ht_node
) {
1338 if (!trigger_applies_to_client(trigger
, client
)) {
1342 client_list_element
= zmalloc(sizeof(*client_list_element
));
1343 if (!client_list_element
) {
1345 goto error_free_client_list
;
1347 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1348 client_list_element
->client
= client
;
1349 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1352 cds_lfht_add(state
->notification_trigger_clients_ht
,
1353 lttng_condition_hash(condition
),
1354 &client_list
->notification_trigger_ht_node
);
1357 * Add the trigger to list of triggers bound to the channels currently
1360 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1362 struct lttng_trigger_list_element
*trigger_list_element
;
1363 struct lttng_channel_trigger_list
*trigger_list
;
1365 if (!trigger_applies_to_channel(trigger
, channel
)) {
1369 cds_lfht_lookup(state
->channel_triggers_ht
,
1370 hash_channel_key(&channel
->key
),
1371 match_channel_trigger_list
,
1374 node
= cds_lfht_iter_get_node(&iter
);
1376 trigger_list
= caa_container_of(node
,
1377 struct lttng_channel_trigger_list
,
1378 channel_triggers_ht_node
);
1380 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1381 if (!trigger_list_element
) {
1383 goto error_free_client_list
;
1385 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1386 trigger_list_element
->trigger
= trigger
;
1387 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1391 * Since there is nothing preventing clients from subscribing to a
1392 * condition before the corresponding trigger is registered, we have
1393 * to evaluate this new condition right away.
1395 * At some point, we were waiting for the next "evaluation" (e.g. on
1396 * reception of a channel sample) to evaluate this new condition, but
1399 * The reason it was broken is that waiting for the next sample
1400 * does not allow us to properly handle transitions for edge-triggered
1403 * Consider this example: when we handle a new channel sample, we
1404 * evaluate each conditions twice: once with the previous state, and
1405 * again with the newest state. We then use those two results to
1406 * determine whether a state change happened: a condition was false and
1407 * became true. If a state change happened, we have to notify clients.
1409 * Now, if a client subscribes to a given notification and registers
1410 * a trigger *after* that subscription, we have to make sure the
1411 * condition is evaluated at this point while considering only the
1412 * current state. Otherwise, the next evaluation cycle may only see
1413 * that the evaluations remain the same (true for samples n-1 and n) and
1414 * the client will never know that the condition has been met.
1416 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1417 &client_list
->list
, node
) {
1418 ret
= evaluate_condition_for_client(trigger
, condition
,
1419 client_list_element
->client
, state
);
1421 goto error_free_client_list
;
1426 * Client list ownership transferred to the
1427 * notification_trigger_clients_ht.
1431 *cmd_result
= LTTNG_OK
;
1432 error_free_client_list
:
1434 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1435 &client_list
->list
, node
) {
1436 free(client_list_element
);
1440 error_free_ht_element
:
1441 free(trigger_ht_element
);
1444 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1446 lttng_condition_destroy(condition
);
1447 lttng_action_destroy(action
);
1448 lttng_trigger_destroy(trigger
);
1455 int handle_notification_thread_command_unregister_trigger(
1456 struct notification_thread_state
*state
,
1457 struct lttng_trigger
*trigger
,
1458 enum lttng_error_code
*_cmd_reply
)
1460 struct cds_lfht_iter iter
;
1461 struct cds_lfht_node
*node
, *triggers_ht_node
;
1462 struct lttng_channel_trigger_list
*trigger_list
;
1463 struct notification_client_list
*client_list
;
1464 struct notification_client_list_element
*client_list_element
, *tmp
;
1465 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1466 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1468 struct lttng_action
*action
;
1469 enum lttng_error_code cmd_reply
;
1473 cds_lfht_lookup(state
->triggers_ht
,
1474 lttng_condition_hash(condition
),
1478 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1479 if (!triggers_ht_node
) {
1480 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1483 cmd_reply
= LTTNG_OK
;
1486 /* Remove trigger from channel_triggers_ht. */
1487 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1488 channel_triggers_ht_node
) {
1489 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1491 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1492 &trigger_list
->list
, node
) {
1493 struct lttng_condition
*current_condition
=
1494 lttng_trigger_get_condition(
1495 trigger_element
->trigger
);
1497 assert(current_condition
);
1498 if (!lttng_condition_is_equal(condition
,
1499 current_condition
)) {
1503 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1504 cds_list_del(&trigger_element
->node
);
1505 /* A trigger can only appear once per channel */
1511 * Remove and release the client list from
1512 * notification_trigger_clients_ht.
1514 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1515 lttng_condition_hash(condition
),
1519 node
= cds_lfht_iter_get_node(&iter
);
1521 client_list
= caa_container_of(node
, struct notification_client_list
,
1522 notification_trigger_ht_node
);
1523 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1524 &client_list
->list
, node
) {
1525 free(client_list_element
);
1527 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1530 /* Remove trigger from triggers_ht. */
1531 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1532 struct lttng_trigger_ht_element
, node
);
1533 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1535 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1536 lttng_condition_destroy(condition
);
1537 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1538 lttng_action_destroy(action
);
1539 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1540 free(trigger_ht_element
);
1544 *_cmd_reply
= cmd_reply
;
1549 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1550 int handle_notification_thread_command(
1551 struct notification_thread_handle
*handle
,
1552 struct notification_thread_state
*state
)
1556 struct notification_thread_command
*cmd
;
1558 /* Read the event pipe to put it back into a quiescent state. */
1559 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1565 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1566 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1567 struct notification_thread_command
, cmd_list_node
);
1568 switch (cmd
->type
) {
1569 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1570 DBG("[notification-thread] Received register trigger command");
1571 ret
= handle_notification_thread_command_register_trigger(
1572 state
, cmd
->parameters
.trigger
,
1575 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1576 DBG("[notification-thread] Received unregister trigger command");
1577 ret
= handle_notification_thread_command_unregister_trigger(
1578 state
, cmd
->parameters
.trigger
,
1581 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1582 DBG("[notification-thread] Received add channel command");
1583 ret
= handle_notification_thread_command_add_channel(
1585 cmd
->parameters
.add_channel
.session
.name
,
1586 cmd
->parameters
.add_channel
.session
.uid
,
1587 cmd
->parameters
.add_channel
.session
.gid
,
1588 cmd
->parameters
.add_channel
.channel
.name
,
1589 cmd
->parameters
.add_channel
.channel
.domain
,
1590 cmd
->parameters
.add_channel
.channel
.key
,
1591 cmd
->parameters
.add_channel
.channel
.capacity
,
1594 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1595 DBG("[notification-thread] Received remove channel command");
1596 ret
= handle_notification_thread_command_remove_channel(
1597 state
, cmd
->parameters
.remove_channel
.key
,
1598 cmd
->parameters
.remove_channel
.domain
,
1601 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1602 DBG("[notification-thread] Received quit command");
1603 cmd
->reply_code
= LTTNG_OK
;
1607 ERR("[notification-thread] Unknown internal command received");
1615 cds_list_del(&cmd
->cmd_list_node
);
1616 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1617 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1620 /* Wake-up and return a fatal error to the calling thread. */
1621 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1622 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1623 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1625 /* Indicate a fatal error to the caller. */
1630 unsigned long hash_client_socket(int socket
)
1632 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1636 int socket_set_non_blocking(int socket
)
1640 /* Set the pipe as non-blocking. */
1641 ret
= fcntl(socket
, F_GETFL
, 0);
1643 PERROR("fcntl get socket flags");
1648 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1650 PERROR("fcntl set O_NONBLOCK socket flag");
1653 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1659 int client_reset_inbound_state(struct notification_client
*client
)
1663 ret
= lttng_dynamic_buffer_set_size(
1664 &client
->communication
.inbound
.buffer
, 0);
1667 client
->communication
.inbound
.bytes_to_receive
=
1668 sizeof(struct lttng_notification_channel_message
);
1669 client
->communication
.inbound
.msg_type
=
1670 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1671 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1672 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1673 ret
= lttng_dynamic_buffer_set_size(
1674 &client
->communication
.inbound
.buffer
,
1675 client
->communication
.inbound
.bytes_to_receive
);
1679 int handle_notification_thread_client_connect(
1680 struct notification_thread_state
*state
)
1683 struct notification_client
*client
;
1685 DBG("[notification-thread] Handling new notification channel client connection");
1687 client
= zmalloc(sizeof(*client
));
1693 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1694 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1695 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1696 client
->communication
.inbound
.expect_creds
= true;
1697 ret
= client_reset_inbound_state(client
);
1699 ERR("[notification-thread] Failed to reset client communication's inbound state");
1704 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1706 ERR("[notification-thread] Failed to accept new notification channel client connection");
1711 client
->socket
= ret
;
1713 ret
= socket_set_non_blocking(client
->socket
);
1715 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1719 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1721 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1726 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1727 LPOLLIN
| LPOLLERR
|
1728 LPOLLHUP
| LPOLLRDHUP
);
1730 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1734 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1738 cds_lfht_add(state
->client_socket_ht
,
1739 hash_client_socket(client
->socket
),
1740 &client
->client_socket_ht_node
);
1745 notification_client_destroy(client
, state
);
1749 int handle_notification_thread_client_disconnect(
1751 struct notification_thread_state
*state
)
1754 struct notification_client
*client
;
1757 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1759 client
= get_client_from_socket(client_socket
, state
);
1761 /* Internal state corruption, fatal error. */
1762 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1768 ret
= lttng_poll_del(&state
->events
, client_socket
);
1770 ERR("[notification-thread] Failed to remove client socket from poll set");
1772 cds_lfht_del(state
->client_socket_ht
,
1773 &client
->client_socket_ht_node
);
1774 notification_client_destroy(client
, state
);
1780 int handle_notification_thread_client_disconnect_all(
1781 struct notification_thread_state
*state
)
1783 struct cds_lfht_iter iter
;
1784 struct notification_client
*client
;
1785 bool error_encoutered
= false;
1788 DBG("[notification-thread] Closing all client connections");
1789 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1790 client_socket_ht_node
) {
1793 ret
= handle_notification_thread_client_disconnect(
1794 client
->socket
, state
);
1796 error_encoutered
= true;
1800 return error_encoutered
? 1 : 0;
1803 int handle_notification_thread_trigger_unregister_all(
1804 struct notification_thread_state
*state
)
1806 bool error_occurred
= false;
1807 struct cds_lfht_iter iter
;
1808 struct lttng_trigger_ht_element
*trigger_ht_element
;
1810 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1812 int ret
= handle_notification_thread_command_unregister_trigger(
1813 state
, trigger_ht_element
->trigger
, NULL
);
1815 error_occurred
= true;
1818 return error_occurred
? -1 : 0;
1822 int client_flush_outgoing_queue(struct notification_client
*client
,
1823 struct notification_thread_state
*state
)
1826 size_t to_send_count
;
1828 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1829 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1830 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1833 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1834 client
->communication
.outbound
.buffer
.data
,
1836 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1837 (ret
> 0 && ret
< to_send_count
)) {
1838 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1840 to_send_count
-= max(ret
, 0);
1842 memcpy(client
->communication
.outbound
.buffer
.data
,
1843 client
->communication
.outbound
.buffer
.data
+
1844 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1846 ret
= lttng_dynamic_buffer_set_size(
1847 &client
->communication
.outbound
.buffer
,
1854 * We want to be notified whenever there is buffer space
1855 * available to send the rest of the payload.
1857 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1858 CLIENT_POLL_MASK_IN_OUT
);
1862 } else if (ret
< 0) {
1863 /* Generic error, disconnect the client. */
1864 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1866 ret
= handle_notification_thread_client_disconnect(
1867 client
->socket
, state
);
1872 /* No error and flushed the queue completely. */
1873 ret
= lttng_dynamic_buffer_set_size(
1874 &client
->communication
.outbound
.buffer
, 0);
1878 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1879 CLIENT_POLL_MASK_IN
);
1884 client
->communication
.outbound
.queued_command_reply
= false;
1885 client
->communication
.outbound
.dropped_notification
= false;
1894 int client_send_command_reply(struct notification_client
*client
,
1895 struct notification_thread_state
*state
,
1896 enum lttng_notification_channel_status status
)
1899 struct lttng_notification_channel_command_reply reply
= {
1900 .status
= (int8_t) status
,
1902 struct lttng_notification_channel_message msg
= {
1903 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1904 .size
= sizeof(reply
),
1906 char buffer
[sizeof(msg
) + sizeof(reply
)];
1908 if (client
->communication
.outbound
.queued_command_reply
) {
1909 /* Protocol error. */
1913 memcpy(buffer
, &msg
, sizeof(msg
));
1914 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1915 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1917 /* Enqueue buffer to outgoing queue and flush it. */
1918 ret
= lttng_dynamic_buffer_append(
1919 &client
->communication
.outbound
.buffer
,
1920 buffer
, sizeof(buffer
));
1925 ret
= client_flush_outgoing_queue(client
, state
);
1930 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1931 /* Queue could not be emptied. */
1932 client
->communication
.outbound
.queued_command_reply
= true;
1941 int client_dispatch_message(struct notification_client
*client
,
1942 struct notification_thread_state
*state
)
1946 if (client
->communication
.inbound
.msg_type
!=
1947 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1948 client
->communication
.inbound
.msg_type
!=
1949 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1950 !client
->validated
) {
1951 WARN("[notification-thread] client attempted a command before handshake");
1956 switch (client
->communication
.inbound
.msg_type
) {
1957 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1960 * Receiving message header. The function will be called again
1961 * once the rest of the message as been received and can be
1964 const struct lttng_notification_channel_message
*msg
;
1966 assert(sizeof(*msg
) ==
1967 client
->communication
.inbound
.buffer
.size
);
1968 msg
= (const struct lttng_notification_channel_message
*)
1969 client
->communication
.inbound
.buffer
.data
;
1971 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1972 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1977 switch (msg
->type
) {
1978 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1979 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1980 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1984 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1988 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1989 client
->communication
.inbound
.msg_type
=
1990 (enum lttng_notification_channel_message_type
) msg
->type
;
1991 ret
= lttng_dynamic_buffer_set_size(
1992 &client
->communication
.inbound
.buffer
, msg
->size
);
1998 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2000 struct lttng_notification_channel_command_handshake
*handshake_client
;
2001 struct lttng_notification_channel_command_handshake handshake_reply
= {
2002 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2003 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2005 struct lttng_notification_channel_message msg_header
= {
2006 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2007 .size
= sizeof(handshake_reply
),
2009 enum lttng_notification_channel_status status
=
2010 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2011 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2013 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2014 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2015 sizeof(handshake_reply
));
2018 (struct lttng_notification_channel_command_handshake
*)
2019 client
->communication
.inbound
.buffer
.data
;
2020 client
->major
= handshake_client
->major
;
2021 client
->minor
= handshake_client
->minor
;
2022 if (!client
->communication
.inbound
.creds_received
) {
2023 ERR("[notification-thread] No credentials received from client");
2028 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2029 &client
->communication
.inbound
.creds
);
2030 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2031 &client
->communication
.inbound
.creds
);
2032 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2033 client
->uid
, client
->gid
, (int) client
->major
,
2034 (int) client
->minor
);
2036 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2037 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2040 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2041 send_buffer
, sizeof(send_buffer
));
2043 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2047 ret
= client_flush_outgoing_queue(client
, state
);
2052 ret
= client_send_command_reply(client
, state
, status
);
2054 ERR("[notification-thread] Failed to send reply to notification channel client");
2058 /* Set reception state to receive the next message header. */
2059 ret
= client_reset_inbound_state(client
);
2061 ERR("[notification-thread] Failed to reset client communication's inbound state");
2064 client
->validated
= true;
2067 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2068 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2070 struct lttng_condition
*condition
;
2071 enum lttng_notification_channel_status status
=
2072 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2073 const struct lttng_buffer_view condition_view
=
2074 lttng_buffer_view_from_dynamic_buffer(
2075 &client
->communication
.inbound
.buffer
,
2077 size_t expected_condition_size
=
2078 client
->communication
.inbound
.buffer
.size
;
2080 ret
= lttng_condition_create_from_buffer(&condition_view
,
2082 if (ret
!= expected_condition_size
) {
2083 ERR("[notification-thread] Malformed condition received from client");
2087 if (client
->communication
.inbound
.msg_type
==
2088 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2089 ret
= notification_thread_client_subscribe(client
,
2090 condition
, state
, &status
);
2092 ret
= notification_thread_client_unsubscribe(client
,
2093 condition
, state
, &status
);
2099 ret
= client_send_command_reply(client
, state
, status
);
2101 ERR("[notification-thread] Failed to send reply to notification channel client");
2105 /* Set reception state to receive the next message header. */
2106 ret
= client_reset_inbound_state(client
);
2108 ERR("[notification-thread] Failed to reset client communication's inbound state");
2120 /* Incoming data from client. */
2121 int handle_notification_thread_client_in(
2122 struct notification_thread_state
*state
, int socket
)
2125 struct notification_client
*client
;
2129 client
= get_client_from_socket(socket
, state
);
2131 /* Internal error, abort. */
2136 offset
= client
->communication
.inbound
.buffer
.size
-
2137 client
->communication
.inbound
.bytes_to_receive
;
2138 if (client
->communication
.inbound
.expect_creds
) {
2139 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2140 client
->communication
.inbound
.buffer
.data
+ offset
,
2141 client
->communication
.inbound
.bytes_to_receive
,
2142 &client
->communication
.inbound
.creds
);
2144 client
->communication
.inbound
.expect_creds
= false;
2145 client
->communication
.inbound
.creds_received
= true;
2148 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2149 client
->communication
.inbound
.buffer
.data
+ offset
,
2150 client
->communication
.inbound
.bytes_to_receive
);
2153 goto error_disconnect_client
;
2156 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2157 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2158 ret
= client_dispatch_message(client
, state
);
2161 * Only returns an error if this client must be
2164 goto error_disconnect_client
;
2171 error_disconnect_client
:
2172 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2176 /* Client ready to receive outgoing data. */
2177 int handle_notification_thread_client_out(
2178 struct notification_thread_state
*state
, int socket
)
2181 struct notification_client
*client
;
2183 client
= get_client_from_socket(socket
, state
);
2185 /* Internal error, abort. */
2190 ret
= client_flush_outgoing_queue(client
, state
);
2199 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2200 const struct channel_state_sample
*sample
,
2201 uint64_t buffer_capacity
)
2203 bool result
= false;
2205 enum lttng_condition_type condition_type
;
2206 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2207 condition
, struct lttng_condition_buffer_usage
,
2210 if (use_condition
->threshold_bytes
.set
) {
2211 threshold
= use_condition
->threshold_bytes
.value
;
2214 * Threshold was expressed as a ratio.
2216 * TODO the threshold (in bytes) of conditions expressed
2217 * as a ratio of total buffer size could be cached to
2218 * forego this double-multiplication or it could be performed
2219 * as fixed-point math.
2221 * Note that caching should accomodate the case where the
2222 * condition applies to multiple channels (i.e. don't assume
2223 * that all channels matching my_chann* have the same size...)
2225 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2226 (double) buffer_capacity
);
2229 condition_type
= lttng_condition_get_type(condition
);
2230 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2231 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2232 threshold
, sample
->highest_usage
);
2235 * The low condition should only be triggered once _all_ of the
2236 * streams in a channel have gone below the "low" threshold.
2238 if (sample
->highest_usage
<= threshold
) {
2242 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2243 threshold
, sample
->highest_usage
);
2246 * For high buffer usage scenarios, we want to trigger whenever
2247 * _any_ of the streams has reached the "high" threshold.
2249 if (sample
->highest_usage
>= threshold
) {
2258 bool evaluate_session_consumed_size_condition(
2259 const struct lttng_condition
*condition
,
2260 uint64_t session_consumed_size
)
2263 const struct lttng_condition_session_consumed_size
*size_condition
=
2264 container_of(condition
,
2265 struct lttng_condition_session_consumed_size
,
2268 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2269 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2270 threshold
, session_consumed_size
);
2271 return session_consumed_size
>= threshold
;
2275 int evaluate_condition(const struct lttng_condition
*condition
,
2276 struct lttng_evaluation
**evaluation
,
2277 const struct notification_thread_state
*state
,
2278 const struct channel_state_sample
*previous_sample
,
2279 const struct channel_state_sample
*latest_sample
,
2280 uint64_t previous_session_consumed_total
,
2281 uint64_t latest_session_consumed_total
,
2282 struct channel_info
*channel_info
)
2285 enum lttng_condition_type condition_type
;
2286 const bool previous_sample_available
= !!previous_sample
;
2287 bool previous_sample_result
= false;
2288 bool latest_sample_result
;
2290 condition_type
= lttng_condition_get_type(condition
);
2292 switch (condition_type
) {
2293 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2294 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2295 if (caa_likely(previous_sample_available
)) {
2296 previous_sample_result
=
2297 evaluate_buffer_usage_condition(condition
,
2298 previous_sample
, channel_info
->capacity
);
2300 latest_sample_result
= evaluate_buffer_usage_condition(
2301 condition
, latest_sample
,
2302 channel_info
->capacity
);
2304 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2305 if (caa_likely(previous_sample_available
)) {
2306 previous_sample_result
=
2307 evaluate_session_consumed_size_condition(
2309 previous_session_consumed_total
);
2311 latest_sample_result
=
2312 evaluate_session_consumed_size_condition(
2314 latest_session_consumed_total
);
2317 /* Unknown condition type; internal error. */
2321 if (!latest_sample_result
||
2322 (previous_sample_result
== latest_sample_result
)) {
2324 * Only trigger on a condition evaluation transition.
2326 * NOTE: This edge-triggered logic may not be appropriate for
2327 * future condition types.
2332 if (!evaluation
|| !latest_sample_result
) {
2336 switch (condition_type
) {
2337 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2338 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2339 *evaluation
= lttng_evaluation_buffer_usage_create(
2341 latest_sample
->highest_usage
,
2342 channel_info
->capacity
);
2344 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2345 *evaluation
= lttng_evaluation_session_consumed_size_create(
2347 latest_session_consumed_total
);
2362 int client_enqueue_dropped_notification(struct notification_client
*client
,
2363 struct notification_thread_state
*state
)
2366 struct lttng_notification_channel_message msg
= {
2367 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2371 ret
= lttng_dynamic_buffer_append(
2372 &client
->communication
.outbound
.buffer
, &msg
,
2378 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2379 struct lttng_evaluation
*evaluation
,
2380 struct notification_client_list
* client_list
,
2381 struct notification_thread_state
*state
,
2382 uid_t channel_uid
, gid_t channel_gid
)
2385 struct lttng_dynamic_buffer msg_buffer
;
2386 struct notification_client_list_element
*client_list_element
, *tmp
;
2387 struct lttng_notification
*notification
;
2388 struct lttng_condition
*condition
;
2389 ssize_t expected_notification_size
, notification_size
;
2390 struct lttng_notification_channel_message msg
;
2392 lttng_dynamic_buffer_init(&msg_buffer
);
2394 condition
= lttng_trigger_get_condition(trigger
);
2397 notification
= lttng_notification_create(condition
, evaluation
);
2398 if (!notification
) {
2403 expected_notification_size
= lttng_notification_serialize(notification
,
2405 if (expected_notification_size
< 0) {
2406 ERR("[notification-thread] Failed to get size of serialized notification");
2411 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2412 msg
.size
= (uint32_t) expected_notification_size
;
2413 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2418 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2419 msg_buffer
.size
+ expected_notification_size
);
2424 notification_size
= lttng_notification_serialize(notification
,
2425 msg_buffer
.data
+ sizeof(msg
));
2426 if (notification_size
!= expected_notification_size
) {
2427 ERR("[notification-thread] Failed to serialize notification");
2432 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2433 &client_list
->list
, node
) {
2434 struct notification_client
*client
=
2435 client_list_element
->client
;
2437 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2439 /* Client is not allowed to monitor this channel. */
2440 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2444 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2445 client
->socket
, msg_buffer
.size
);
2446 if (client
->communication
.outbound
.buffer
.size
) {
2448 * Outgoing data is already buffered for this client;
2449 * drop the notification and enqueue a "dropped
2450 * notification" message if this is the first dropped
2451 * notification since the socket spilled-over to the
2454 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2456 if (!client
->communication
.outbound
.dropped_notification
) {
2457 client
->communication
.outbound
.dropped_notification
= true;
2458 ret
= client_enqueue_dropped_notification(
2467 ret
= lttng_dynamic_buffer_append_buffer(
2468 &client
->communication
.outbound
.buffer
,
2474 ret
= client_flush_outgoing_queue(client
, state
);
2481 lttng_notification_destroy(notification
);
2482 lttng_dynamic_buffer_reset(&msg_buffer
);
2486 int handle_notification_thread_channel_sample(
2487 struct notification_thread_state
*state
, int pipe
,
2488 enum lttng_domain_type domain
)
2491 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2492 struct channel_info
*channel_info
;
2493 struct cds_lfht_node
*node
;
2494 struct cds_lfht_iter iter
;
2495 struct lttng_channel_trigger_list
*trigger_list
;
2496 struct lttng_trigger_list_element
*trigger_list_element
;
2497 bool previous_sample_available
= false;
2498 struct channel_state_sample previous_sample
, latest_sample
;
2499 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2502 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2503 * ensuring that read/write of sampling messages are atomic.
2505 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2506 if (ret
!= sizeof(sample_msg
)) {
2507 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2514 latest_sample
.key
.key
= sample_msg
.key
;
2515 latest_sample
.key
.domain
= domain
;
2516 latest_sample
.highest_usage
= sample_msg
.highest
;
2517 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2518 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2522 /* Retrieve the channel's informations */
2523 cds_lfht_lookup(state
->channels_ht
,
2524 hash_channel_key(&latest_sample
.key
),
2528 node
= cds_lfht_iter_get_node(&iter
);
2529 if (caa_unlikely(!node
)) {
2531 * Not an error since the consumer can push a sample to the pipe
2532 * and the rest of the session daemon could notify us of the
2533 * channel's destruction before we get a chance to process that
2536 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2537 latest_sample
.key
.key
,
2538 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2542 channel_info
= caa_container_of(node
, struct channel_info
,
2544 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2546 latest_sample
.key
.key
,
2547 channel_info
->session_info
->name
,
2548 latest_sample
.highest_usage
,
2549 latest_sample
.lowest_usage
,
2550 latest_sample
.channel_total_consumed
);
2552 previous_session_consumed_total
=
2553 channel_info
->session_info
->consumed_data_size
;
2555 /* Retrieve the channel's last sample, if it exists, and update it. */
2556 cds_lfht_lookup(state
->channel_state_ht
,
2557 hash_channel_key(&latest_sample
.key
),
2558 match_channel_state_sample
,
2561 node
= cds_lfht_iter_get_node(&iter
);
2562 if (caa_likely(node
)) {
2563 struct channel_state_sample
*stored_sample
;
2565 /* Update the sample stored. */
2566 stored_sample
= caa_container_of(node
,
2567 struct channel_state_sample
,
2568 channel_state_ht_node
);
2570 memcpy(&previous_sample
, stored_sample
,
2571 sizeof(previous_sample
));
2572 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2573 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2574 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2575 previous_sample_available
= true;
2577 latest_session_consumed_total
=
2578 previous_session_consumed_total
+
2579 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2582 * This is the channel's first sample, allocate space for and
2583 * store the new sample.
2585 struct channel_state_sample
*stored_sample
;
2587 stored_sample
= zmalloc(sizeof(*stored_sample
));
2588 if (!stored_sample
) {
2593 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2594 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2595 cds_lfht_add(state
->channel_state_ht
,
2596 hash_channel_key(&stored_sample
->key
),
2597 &stored_sample
->channel_state_ht_node
);
2599 latest_session_consumed_total
=
2600 previous_session_consumed_total
+
2601 latest_sample
.channel_total_consumed
;
2604 channel_info
->session_info
->consumed_data_size
=
2605 latest_session_consumed_total
;
2607 /* Find triggers associated with this channel. */
2608 cds_lfht_lookup(state
->channel_triggers_ht
,
2609 hash_channel_key(&latest_sample
.key
),
2610 match_channel_trigger_list
,
2613 node
= cds_lfht_iter_get_node(&iter
);
2614 if (caa_likely(!node
)) {
2618 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2619 channel_triggers_ht_node
);
2620 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2622 struct lttng_condition
*condition
;
2623 struct lttng_action
*action
;
2624 struct lttng_trigger
*trigger
;
2625 struct notification_client_list
*client_list
;
2626 struct lttng_evaluation
*evaluation
= NULL
;
2628 trigger
= trigger_list_element
->trigger
;
2629 condition
= lttng_trigger_get_condition(trigger
);
2631 action
= lttng_trigger_get_action(trigger
);
2633 /* Notify actions are the only type currently supported. */
2634 assert(lttng_action_get_type(action
) ==
2635 LTTNG_ACTION_TYPE_NOTIFY
);
2638 * Check if any client is subscribed to the result of this
2641 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2642 lttng_condition_hash(condition
),
2646 node
= cds_lfht_iter_get_node(&iter
);
2649 client_list
= caa_container_of(node
,
2650 struct notification_client_list
,
2651 notification_trigger_ht_node
);
2652 if (cds_list_empty(&client_list
->list
)) {
2654 * No clients interested in the evaluation's result,
2660 ret
= evaluate_condition(condition
, &evaluation
, state
,
2661 previous_sample_available
? &previous_sample
: NULL
,
2663 previous_session_consumed_total
,
2664 latest_session_consumed_total
,
2666 if (caa_unlikely(ret
)) {
2670 if (caa_likely(!evaluation
)) {
2674 /* Dispatch evaluation result to all clients. */
2675 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2676 evaluation
, client_list
, state
,
2677 channel_info
->session_info
->uid
,
2678 channel_info
->session_info
->gid
);
2679 lttng_evaluation_destroy(evaluation
);
2680 if (caa_unlikely(ret
)) {