2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/condition/session-consumed-size-internal.h>
36 #include <lttng/condition/session-rotation-internal.h>
37 #include <lttng/notification/channel-internal.h>
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 struct lttng_trigger_list_element
{
55 /* No ownership of the trigger object is assumed. */
56 const struct lttng_trigger
*trigger
;
57 struct cds_list_head node
;
60 struct lttng_channel_trigger_list
{
61 struct channel_key channel_key
;
62 /* List of struct lttng_trigger_list_element. */
63 struct cds_list_head list
;
64 /* Node in the channel_triggers_ht */
65 struct cds_lfht_node channel_triggers_ht_node
;
69 * List of triggers applying to a given session.
72 * - lttng_session_trigger_list_create()
73 * - lttng_session_trigger_list_build()
74 * - lttng_session_trigger_list_destroy()
75 * - lttng_session_trigger_list_add()
77 struct lttng_session_trigger_list
{
79 * Not owned by this; points to the session_info structure's
82 const char *session_name
;
83 /* List of struct lttng_trigger_list_element. */
84 struct cds_list_head list
;
85 /* Node in the session_triggers_ht */
86 struct cds_lfht_node session_triggers_ht_node
;
88 * Weak reference to the notification system's session triggers
91 * The session trigger list structure structure is owned by
92 * the session's session_info.
94 * The session_info is kept alive the the channel_infos holding a
95 * reference to it (reference counting). When those channels are
96 * destroyed (at runtime or on teardown), the reference they hold
97 * to the session_info are released. On destruction of session_info,
98 * session_info_destroy() will remove the list of triggers applying
99 * to this session from the notification system's state.
101 * This implies that the session_triggers_ht must be destroyed
102 * after the channels.
104 struct cds_lfht
*session_triggers_ht
;
105 /* Used for delayed RCU reclaim. */
106 struct rcu_head rcu_node
;
109 struct lttng_trigger_ht_element
{
110 struct lttng_trigger
*trigger
;
111 struct cds_lfht_node node
;
114 struct lttng_condition_list_element
{
115 struct lttng_condition
*condition
;
116 struct cds_list_head node
;
119 struct notification_client_list_element
{
120 struct notification_client
*client
;
121 struct cds_list_head node
;
124 struct notification_client_list
{
125 struct lttng_trigger
*trigger
;
126 struct cds_list_head list
;
127 struct cds_lfht_node notification_trigger_ht_node
;
130 struct notification_client
{
132 /* Client protocol version. */
133 uint8_t major
, minor
;
137 * Indicates if the credentials and versions of the client have been
142 * Conditions to which the client's notification channel is subscribed.
143 * List of struct lttng_condition_list_node. The condition member is
144 * owned by the client.
146 struct cds_list_head condition_list
;
147 struct cds_lfht_node client_socket_ht_node
;
151 * During the reception of a message, the reception
152 * buffers' "size" is set to contain the current
153 * message's complete payload.
155 struct lttng_dynamic_buffer buffer
;
156 /* Bytes left to receive for the current message. */
157 size_t bytes_to_receive
;
158 /* Type of the message being received. */
159 enum lttng_notification_channel_message_type msg_type
;
161 * Indicates whether or not credentials are expected
166 * Indicates whether or not credentials were received
170 /* Only used during credentials reception. */
171 lttng_sock_cred creds
;
175 * Indicates whether or not a notification addressed to
176 * this client was dropped because a command reply was
179 * A notification is dropped whenever the buffer is not
182 bool dropped_notification
;
184 * Indicates whether or not a command reply is already
185 * buffered. In this case, it means that the client is
186 * not consuming command replies before emitting a new
187 * one. This could be caused by a protocol error or a
188 * misbehaving/malicious client.
190 bool queued_command_reply
;
191 struct lttng_dynamic_buffer buffer
;
196 struct channel_state_sample
{
197 struct channel_key key
;
198 struct cds_lfht_node channel_state_ht_node
;
199 uint64_t highest_usage
;
200 uint64_t lowest_usage
;
201 uint64_t channel_total_consumed
;
204 static unsigned long hash_channel_key(struct channel_key
*key
);
205 static int evaluate_condition(const struct lttng_condition
*condition
,
206 struct lttng_evaluation
**evaluation
,
207 const struct notification_thread_state
*state
,
208 const struct channel_state_sample
*previous_sample
,
209 const struct channel_state_sample
*latest_sample
,
210 uint64_t previous_session_consumed_total
,
211 uint64_t latest_session_consumed_total
,
212 struct channel_info
*channel_info
);
214 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
215 const struct lttng_evaluation
*evaluation
,
216 struct notification_client_list
*client_list
,
217 struct notification_thread_state
*state
,
218 uid_t channel_uid
, gid_t channel_gid
);
221 /* session_info API */
223 void session_info_destroy(void *_data
);
225 void session_info_get(struct session_info
*session_info
);
227 void session_info_put(struct session_info
*session_info
);
229 struct session_info
*session_info_create(const char *name
,
230 uid_t uid
, gid_t gid
,
231 struct lttng_session_trigger_list
*trigger_list
);
233 void session_info_add_channel(struct session_info
*session_info
,
234 struct channel_info
*channel_info
);
236 void session_info_remove_channel(struct session_info
*session_info
,
237 struct channel_info
*channel_info
);
239 /* lttng_session_trigger_list API */
241 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
242 const char *session_name
,
243 struct cds_lfht
*session_triggers_ht
);
245 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
246 const struct notification_thread_state
*state
,
247 const char *session_name
);
249 void lttng_session_trigger_list_destroy(
250 struct lttng_session_trigger_list
*list
);
252 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
253 const struct lttng_trigger
*trigger
);
257 int match_client(struct cds_lfht_node
*node
, const void *key
)
259 /* This double-cast is intended to supress pointer-to-cast warning. */
260 int socket
= (int) (intptr_t) key
;
261 struct notification_client
*client
;
263 client
= caa_container_of(node
, struct notification_client
,
264 client_socket_ht_node
);
266 return !!(client
->socket
== socket
);
270 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
272 struct channel_key
*channel_key
= (struct channel_key
*) key
;
273 struct lttng_channel_trigger_list
*trigger_list
;
275 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
276 channel_triggers_ht_node
);
278 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
279 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
283 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
285 struct channel_key
*channel_key
= (struct channel_key
*) key
;
286 struct channel_state_sample
*sample
;
288 sample
= caa_container_of(node
, struct channel_state_sample
,
289 channel_state_ht_node
);
291 return !!((channel_key
->key
== sample
->key
.key
) &&
292 (channel_key
->domain
== sample
->key
.domain
));
296 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
298 struct channel_key
*channel_key
= (struct channel_key
*) key
;
299 struct channel_info
*channel_info
;
301 channel_info
= caa_container_of(node
, struct channel_info
,
304 return !!((channel_key
->key
== channel_info
->key
.key
) &&
305 (channel_key
->domain
== channel_info
->key
.domain
));
309 int match_condition(struct cds_lfht_node
*node
, const void *key
)
311 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
312 struct lttng_trigger_ht_element
*trigger
;
313 struct lttng_condition
*condition
;
315 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
317 condition
= lttng_trigger_get_condition(trigger
->trigger
);
320 return !!lttng_condition_is_equal(condition_key
, condition
);
324 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
326 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
327 struct notification_client_list
*client_list
;
328 struct lttng_condition
*condition
;
329 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
332 assert(condition_key
);
334 client_list
= caa_container_of(node
, struct notification_client_list
,
335 notification_trigger_ht_node
);
336 condition
= lttng_trigger_get_condition(client_list
->trigger
);
338 return !!lttng_condition_is_equal(condition_key
, condition
);
342 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
344 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
345 struct notification_client_list
*client_list
;
346 struct lttng_condition
*condition
;
348 assert(condition_key
);
350 client_list
= caa_container_of(node
, struct notification_client_list
,
351 notification_trigger_ht_node
);
352 condition
= lttng_trigger_get_condition(client_list
->trigger
);
354 return !!lttng_condition_is_equal(condition_key
, condition
);
358 unsigned long lttng_condition_buffer_usage_hash(
359 const struct lttng_condition
*_condition
)
362 unsigned long condition_type
;
363 struct lttng_condition_buffer_usage
*condition
;
365 condition
= container_of(_condition
,
366 struct lttng_condition_buffer_usage
, parent
);
368 condition_type
= (unsigned long) condition
->parent
.type
;
369 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
370 if (condition
->session_name
) {
371 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
373 if (condition
->channel_name
) {
374 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
376 if (condition
->domain
.set
) {
377 hash
^= hash_key_ulong(
378 (void *) condition
->domain
.type
,
381 if (condition
->threshold_ratio
.set
) {
384 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
385 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
386 } else if (condition
->threshold_bytes
.set
) {
389 val
= condition
->threshold_bytes
.value
;
390 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
396 unsigned long lttng_condition_session_consumed_size_hash(
397 const struct lttng_condition
*_condition
)
400 unsigned long condition_type
=
401 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
402 struct lttng_condition_session_consumed_size
*condition
;
405 condition
= container_of(_condition
,
406 struct lttng_condition_session_consumed_size
, parent
);
408 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
409 if (condition
->session_name
) {
410 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
412 val
= condition
->consumed_threshold_bytes
.value
;
413 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
418 unsigned long lttng_condition_session_rotation_hash(
419 const struct lttng_condition
*_condition
)
421 unsigned long hash
, condition_type
;
422 struct lttng_condition_session_rotation
*condition
;
424 condition
= container_of(_condition
,
425 struct lttng_condition_session_rotation
, parent
);
426 condition_type
= (unsigned long) condition
->parent
.type
;
427 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
428 assert(condition
->session_name
);
429 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
434 * The lttng_condition hashing code is kept in this file (rather than
435 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
436 * don't want to link in liblttng-ctl.
439 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
441 switch (condition
->type
) {
442 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
443 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
444 return lttng_condition_buffer_usage_hash(condition
);
445 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
446 return lttng_condition_session_consumed_size_hash(condition
);
447 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
448 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
449 return lttng_condition_session_rotation_hash(condition
);
451 ERR("[notification-thread] Unexpected condition type caught");
457 unsigned long hash_channel_key(struct channel_key
*key
)
459 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
460 unsigned long domain_hash
= hash_key_ulong(
461 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
463 return key_hash
^ domain_hash
;
467 void channel_info_destroy(struct channel_info
*channel_info
)
473 if (channel_info
->session_info
) {
474 session_info_remove_channel(channel_info
->session_info
,
476 session_info_put(channel_info
->session_info
);
478 if (channel_info
->name
) {
479 free(channel_info
->name
);
484 /* Don't call directly, use the ref-counting mechanism. */
486 void session_info_destroy(void *_data
)
488 struct session_info
*session_info
= _data
;
491 assert(session_info
);
492 if (session_info
->channel_infos_ht
) {
493 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
495 ERR("[notification-thread] Failed to destroy channel information hash table");
498 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
499 free(session_info
->name
);
504 void session_info_get(struct session_info
*session_info
)
509 lttng_ref_get(&session_info
->ref
);
513 void session_info_put(struct session_info
*session_info
)
518 lttng_ref_put(&session_info
->ref
);
522 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
523 struct lttng_session_trigger_list
*trigger_list
)
525 struct session_info
*session_info
;
529 session_info
= zmalloc(sizeof(*session_info
));
533 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
535 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
536 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
537 if (!session_info
->channel_infos_ht
) {
541 cds_lfht_node_init(&session_info
->sessions_ht_node
);
542 session_info
->name
= strdup(name
);
543 if (!session_info
->name
) {
546 session_info
->uid
= uid
;
547 session_info
->gid
= gid
;
548 session_info
->trigger_list
= trigger_list
;
552 session_info_put(session_info
);
557 void session_info_add_channel(struct session_info
*session_info
,
558 struct channel_info
*channel_info
)
561 cds_lfht_add(session_info
->channel_infos_ht
,
562 hash_channel_key(&channel_info
->key
),
563 &channel_info
->session_info_channels_ht_node
);
568 void session_info_remove_channel(struct session_info
*session_info
,
569 struct channel_info
*channel_info
)
572 cds_lfht_del(session_info
->channel_infos_ht
,
573 &channel_info
->session_info_channels_ht_node
);
578 struct channel_info
*channel_info_create(const char *channel_name
,
579 struct channel_key
*channel_key
, uint64_t channel_capacity
,
580 struct session_info
*session_info
)
582 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
588 cds_lfht_node_init(&channel_info
->channels_ht_node
);
589 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
590 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
591 channel_info
->capacity
= channel_capacity
;
593 channel_info
->name
= strdup(channel_name
);
594 if (!channel_info
->name
) {
599 * Set the references between session and channel infos:
600 * - channel_info holds a strong reference to session_info
601 * - session_info holds a weak reference to channel_info
603 session_info_get(session_info
);
604 session_info_add_channel(session_info
, channel_info
);
605 channel_info
->session_info
= session_info
;
609 channel_info_destroy(channel_info
);
613 /* This function must be called with the RCU read lock held. */
615 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
616 struct lttng_condition
*condition
,
617 struct notification_client
*client
,
618 struct notification_thread_state
*state
)
621 struct cds_lfht_iter iter
;
622 struct cds_lfht_node
*node
;
623 struct channel_info
*channel_info
= NULL
;
624 struct channel_key
*channel_key
= NULL
;
625 struct channel_state_sample
*last_sample
= NULL
;
626 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
627 struct lttng_evaluation
*evaluation
= NULL
;
628 struct notification_client_list client_list
= { 0 };
629 struct notification_client_list_element client_list_element
= { 0 };
636 /* Find the channel associated with the trigger. */
637 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
638 channel_trigger_list
, channel_triggers_ht_node
) {
639 struct lttng_trigger_list_element
*element
;
641 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
642 const struct lttng_condition
*current_condition
=
643 lttng_trigger_get_const_condition(
646 assert(current_condition
);
647 if (!lttng_condition_is_equal(condition
,
648 current_condition
)) {
652 /* Found the trigger, save the channel key. */
653 channel_key
= &channel_trigger_list
->channel_key
;
657 /* The channel key was found stop iteration. */
663 /* No channel found; normal exit. */
664 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
669 /* Fetch channel info for the matching channel. */
670 cds_lfht_lookup(state
->channels_ht
,
671 hash_channel_key(channel_key
),
675 node
= cds_lfht_iter_get_node(&iter
);
677 channel_info
= caa_container_of(node
, struct channel_info
,
680 /* Retrieve the channel's last sample, if it exists. */
681 cds_lfht_lookup(state
->channel_state_ht
,
682 hash_channel_key(channel_key
),
683 match_channel_state_sample
,
686 node
= cds_lfht_iter_get_node(&iter
);
688 last_sample
= caa_container_of(node
,
689 struct channel_state_sample
,
690 channel_state_ht_node
);
692 /* Nothing to evaluate, no sample was ever taken. Normal exit */
693 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
698 ret
= evaluate_condition(condition
, &evaluation
, state
,
700 0, channel_info
->session_info
->consumed_data_size
,
703 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
708 /* Evaluation yielded nothing. Normal exit. */
709 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
715 * Create a temporary client list with the client currently
718 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
719 CDS_INIT_LIST_HEAD(&client_list
.list
);
720 client_list
.trigger
= trigger
;
722 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
723 client_list_element
.client
= client
;
724 cds_list_add(&client_list_element
.node
, &client_list
.list
);
726 /* Send evaluation result to the newly-subscribed client. */
727 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
728 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
729 state
, channel_info
->session_info
->uid
,
730 channel_info
->session_info
->gid
);
737 int notification_thread_client_subscribe(struct notification_client
*client
,
738 struct lttng_condition
*condition
,
739 struct notification_thread_state
*state
,
740 enum lttng_notification_channel_status
*_status
)
743 struct cds_lfht_iter iter
;
744 struct cds_lfht_node
*node
;
745 struct notification_client_list
*client_list
;
746 struct lttng_condition_list_element
*condition_list_element
= NULL
;
747 struct notification_client_list_element
*client_list_element
= NULL
;
748 enum lttng_notification_channel_status status
=
749 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
752 * Ensure that the client has not already subscribed to this condition
755 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
756 if (lttng_condition_is_equal(condition_list_element
->condition
,
758 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
763 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
764 if (!condition_list_element
) {
768 client_list_element
= zmalloc(sizeof(*client_list_element
));
769 if (!client_list_element
) {
777 * Add the newly-subscribed condition to the client's subscription list.
779 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
780 condition_list_element
->condition
= condition
;
781 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
783 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
784 lttng_condition_hash(condition
),
785 match_client_list_condition
,
788 node
= cds_lfht_iter_get_node(&iter
);
791 * No notification-emiting trigger registered with this
792 * condition. We don't evaluate the condition right away
793 * since this trigger is not registered yet.
795 free(client_list_element
);
799 client_list
= caa_container_of(node
, struct notification_client_list
,
800 notification_trigger_ht_node
);
802 * The condition to which the client just subscribed is evaluated
803 * at this point so that conditions that are already TRUE result
804 * in a notification being sent out.
806 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
808 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
810 free(client_list_element
);
815 * Add the client to the list of clients interested in a given trigger
816 * if a "notification" trigger with a corresponding condition was
819 client_list_element
->client
= client
;
820 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
821 cds_list_add(&client_list_element
->node
, &client_list
->list
);
830 free(condition_list_element
);
831 free(client_list_element
);
836 int notification_thread_client_unsubscribe(
837 struct notification_client
*client
,
838 struct lttng_condition
*condition
,
839 struct notification_thread_state
*state
,
840 enum lttng_notification_channel_status
*_status
)
842 struct cds_lfht_iter iter
;
843 struct cds_lfht_node
*node
;
844 struct notification_client_list
*client_list
;
845 struct lttng_condition_list_element
*condition_list_element
,
847 struct notification_client_list_element
*client_list_element
,
849 bool condition_found
= false;
850 enum lttng_notification_channel_status status
=
851 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
853 /* Remove the condition from the client's condition list. */
854 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
855 &client
->condition_list
, node
) {
856 if (!lttng_condition_is_equal(condition_list_element
->condition
,
861 cds_list_del(&condition_list_element
->node
);
863 * The caller may be iterating on the client's conditions to
864 * tear down a client's connection. In this case, the condition
865 * will be destroyed at the end.
867 if (condition
!= condition_list_element
->condition
) {
868 lttng_condition_destroy(
869 condition_list_element
->condition
);
871 free(condition_list_element
);
872 condition_found
= true;
876 if (!condition_found
) {
877 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
882 * Remove the client from the list of clients interested the trigger
883 * matching the condition.
886 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
887 lttng_condition_hash(condition
),
888 match_client_list_condition
,
891 node
= cds_lfht_iter_get_node(&iter
);
896 client_list
= caa_container_of(node
, struct notification_client_list
,
897 notification_trigger_ht_node
);
898 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
899 &client_list
->list
, node
) {
900 if (client_list_element
->client
->socket
!= client
->socket
) {
903 cds_list_del(&client_list_element
->node
);
904 free(client_list_element
);
910 lttng_condition_destroy(condition
);
918 void notification_client_destroy(struct notification_client
*client
,
919 struct notification_thread_state
*state
)
921 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
927 /* Release all conditions to which the client was subscribed. */
928 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
929 &client
->condition_list
, node
) {
930 (void) notification_thread_client_unsubscribe(client
,
931 condition_list_element
->condition
, state
, NULL
);
934 if (client
->socket
>= 0) {
935 (void) lttcomm_close_unix_sock(client
->socket
);
937 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
938 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
943 * Call with rcu_read_lock held (and hold for the lifetime of the returned
947 struct notification_client
*get_client_from_socket(int socket
,
948 struct notification_thread_state
*state
)
950 struct cds_lfht_iter iter
;
951 struct cds_lfht_node
*node
;
952 struct notification_client
*client
= NULL
;
954 cds_lfht_lookup(state
->client_socket_ht
,
955 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
957 (void *) (unsigned long) socket
,
959 node
= cds_lfht_iter_get_node(&iter
);
964 client
= caa_container_of(node
, struct notification_client
,
965 client_socket_ht_node
);
971 bool buffer_usage_condition_applies_to_channel(
972 struct lttng_condition
*condition
,
973 struct channel_info
*channel_info
)
975 enum lttng_condition_status status
;
976 enum lttng_domain_type condition_domain
;
977 const char *condition_session_name
= NULL
;
978 const char *condition_channel_name
= NULL
;
980 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
982 assert(status
== LTTNG_CONDITION_STATUS_OK
);
983 if (channel_info
->key
.domain
!= condition_domain
) {
987 status
= lttng_condition_buffer_usage_get_session_name(
988 condition
, &condition_session_name
);
989 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
991 status
= lttng_condition_buffer_usage_get_channel_name(
992 condition
, &condition_channel_name
);
993 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
995 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
998 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1008 bool session_consumed_size_condition_applies_to_channel(
1009 struct lttng_condition
*condition
,
1010 struct channel_info
*channel_info
)
1012 enum lttng_condition_status status
;
1013 const char *condition_session_name
= NULL
;
1015 status
= lttng_condition_session_consumed_size_get_session_name(
1016 condition
, &condition_session_name
);
1017 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1019 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1029 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
1030 struct channel_info
*channel_info
)
1032 struct lttng_condition
*condition
;
1033 bool trigger_applies
;
1035 condition
= lttng_trigger_get_condition(trigger
);
1040 switch (lttng_condition_get_type(condition
)) {
1041 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1042 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1043 trigger_applies
= buffer_usage_condition_applies_to_channel(
1044 condition
, channel_info
);
1046 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1047 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1048 condition
, channel_info
);
1054 return trigger_applies
;
1060 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1061 struct notification_client
*client
)
1063 bool applies
= false;
1064 struct lttng_condition_list_element
*condition_list_element
;
1066 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1068 applies
= lttng_condition_is_equal(
1069 condition_list_element
->condition
,
1070 lttng_trigger_get_condition(trigger
));
1079 int match_session(struct cds_lfht_node
*node
, const void *key
)
1081 const char *name
= key
;
1082 struct session_info
*session_info
= caa_container_of(
1083 node
, struct session_info
, sessions_ht_node
);
1085 return !strcmp(session_info
->name
, name
);
1089 * Allocate an empty lttng_session_trigger_list for the session named
1092 * No ownership of 'session_name' is assumed by the session trigger list.
1093 * It is the caller's responsability to ensure the session name is alive
1094 * for as long as this list is.
1097 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1098 const char *session_name
,
1099 struct cds_lfht
*session_triggers_ht
)
1101 struct lttng_session_trigger_list
*list
;
1103 list
= zmalloc(sizeof(*list
));
1107 list
->session_name
= session_name
;
1108 CDS_INIT_LIST_HEAD(&list
->list
);
1109 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1110 list
->session_triggers_ht
= session_triggers_ht
;
1113 /* Publish the list through the session_triggers_ht. */
1114 cds_lfht_add(session_triggers_ht
,
1115 hash_key_str(session_name
, lttng_ht_seed
),
1116 &list
->session_triggers_ht_node
);
1123 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1125 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1130 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1132 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1134 /* Empty the list element by element, and then free the list itself. */
1135 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1136 &list
->list
, node
) {
1137 cds_list_del(&trigger_list_element
->node
);
1138 free(trigger_list_element
);
1141 /* Unpublish the list from the session_triggers_ht. */
1142 cds_lfht_del(list
->session_triggers_ht
,
1143 &list
->session_triggers_ht_node
);
1145 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1149 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1150 const struct lttng_trigger
*trigger
)
1153 struct lttng_trigger_list_element
*new_element
=
1154 zmalloc(sizeof(*new_element
));
1160 CDS_INIT_LIST_HEAD(&new_element
->node
);
1161 new_element
->trigger
= trigger
;
1162 cds_list_add(&new_element
->node
, &list
->list
);
1168 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1169 const char *session_name
)
1171 bool applies
= false;
1172 const struct lttng_condition
*condition
;
1174 condition
= lttng_trigger_get_const_condition(trigger
);
1175 switch (lttng_condition_get_type(condition
)) {
1176 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1177 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1179 enum lttng_condition_status condition_status
;
1180 const char *condition_session_name
;
1182 condition_status
= lttng_condition_session_rotation_get_session_name(
1183 condition
, &condition_session_name
);
1184 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1185 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1189 assert(condition_session_name
);
1190 applies
= !strcmp(condition_session_name
, session_name
);
1201 * Allocate and initialize an lttng_session_trigger_list which contains
1202 * all triggers that apply to the session named 'session_name'.
1204 * No ownership of 'session_name' is assumed by the session trigger list.
1205 * It is the caller's responsability to ensure the session name is alive
1206 * for as long as this list is.
1209 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1210 const struct notification_thread_state
*state
,
1211 const char *session_name
)
1213 int trigger_count
= 0;
1214 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1215 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1216 struct cds_lfht_iter iter
;
1218 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1219 state
->session_triggers_ht
);
1221 /* Add all triggers applying to the session named 'session_name'. */
1222 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1226 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1231 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1232 trigger_ht_element
->trigger
);
1240 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1242 return session_trigger_list
;
1244 lttng_session_trigger_list_destroy(session_trigger_list
);
1249 struct session_info
*find_or_create_session_info(
1250 struct notification_thread_state
*state
,
1251 const char *name
, uid_t uid
, gid_t gid
)
1253 struct session_info
*session
= NULL
;
1254 struct cds_lfht_node
*node
;
1255 struct cds_lfht_iter iter
;
1256 struct lttng_session_trigger_list
*trigger_list
;
1259 cds_lfht_lookup(state
->sessions_ht
,
1260 hash_key_str(name
, lttng_ht_seed
),
1264 node
= cds_lfht_iter_get_node(&iter
);
1266 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1268 session
= caa_container_of(node
, struct session_info
,
1270 assert(session
->uid
== uid
);
1271 assert(session
->gid
== gid
);
1275 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1276 if (!trigger_list
) {
1280 session
= session_info_create(name
, uid
, gid
, trigger_list
);
1282 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1286 trigger_list
= NULL
;
1288 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1289 &session
->sessions_ht_node
);
1294 session_info_put(session
);
1299 int handle_notification_thread_command_add_channel(
1300 struct notification_thread_state
*state
,
1301 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1302 const char *channel_name
, enum lttng_domain_type channel_domain
,
1303 uint64_t channel_key_int
, uint64_t channel_capacity
,
1304 enum lttng_error_code
*cmd_result
)
1306 struct cds_list_head trigger_list
;
1307 struct channel_info
*new_channel_info
= NULL
;
1308 struct channel_key channel_key
= {
1309 .key
= channel_key_int
,
1310 .domain
= channel_domain
,
1312 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1313 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1314 int trigger_count
= 0;
1315 struct cds_lfht_iter iter
;
1316 struct session_info
*session_info
= NULL
;
1318 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1319 channel_name
, session_name
, channel_key_int
,
1320 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1322 CDS_INIT_LIST_HEAD(&trigger_list
);
1324 session_info
= find_or_create_session_info(state
, session_name
,
1325 session_uid
, session_gid
);
1326 if (!session_info
) {
1327 /* Allocation error or an internal error occured. */
1331 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1332 channel_capacity
, session_info
);
1333 if (!new_channel_info
) {
1337 /* Build a list of all triggers applying to the new channel. */
1338 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1340 struct lttng_trigger_list_element
*new_element
;
1342 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1343 new_channel_info
)) {
1347 new_element
= zmalloc(sizeof(*new_element
));
1351 CDS_INIT_LIST_HEAD(&new_element
->node
);
1352 new_element
->trigger
= trigger_ht_element
->trigger
;
1353 cds_list_add(&new_element
->node
, &trigger_list
);
1357 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1359 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1360 if (!channel_trigger_list
) {
1363 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1364 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1365 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1366 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1369 /* Add channel to the channel_ht which owns the channel_infos. */
1370 cds_lfht_add(state
->channels_ht
,
1371 hash_channel_key(&new_channel_info
->key
),
1372 &new_channel_info
->channels_ht_node
);
1374 * Add the list of triggers associated with this channel to the
1375 * channel_triggers_ht.
1377 cds_lfht_add(state
->channel_triggers_ht
,
1378 hash_channel_key(&new_channel_info
->key
),
1379 &channel_trigger_list
->channel_triggers_ht_node
);
1381 *cmd_result
= LTTNG_OK
;
1384 channel_info_destroy(new_channel_info
);
1385 session_info_put(session_info
);
1390 int handle_notification_thread_command_remove_channel(
1391 struct notification_thread_state
*state
,
1392 uint64_t channel_key
, enum lttng_domain_type domain
,
1393 enum lttng_error_code
*cmd_result
)
1395 struct cds_lfht_node
*node
;
1396 struct cds_lfht_iter iter
;
1397 struct lttng_channel_trigger_list
*trigger_list
;
1398 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1399 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1400 struct channel_info
*channel_info
;
1402 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1403 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1407 cds_lfht_lookup(state
->channel_triggers_ht
,
1408 hash_channel_key(&key
),
1409 match_channel_trigger_list
,
1412 node
= cds_lfht_iter_get_node(&iter
);
1414 * There is a severe internal error if we are being asked to remove a
1415 * channel that doesn't exist.
1418 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1422 /* Free the list of triggers associated with this channel. */
1423 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1424 channel_triggers_ht_node
);
1425 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1426 &trigger_list
->list
, node
) {
1427 cds_list_del(&trigger_list_element
->node
);
1428 free(trigger_list_element
);
1430 cds_lfht_del(state
->channel_triggers_ht
, node
);
1433 /* Free sampled channel state. */
1434 cds_lfht_lookup(state
->channel_state_ht
,
1435 hash_channel_key(&key
),
1436 match_channel_state_sample
,
1439 node
= cds_lfht_iter_get_node(&iter
);
1441 * This is expected to be NULL if the channel is destroyed before we
1442 * received a sample.
1445 struct channel_state_sample
*sample
= caa_container_of(node
,
1446 struct channel_state_sample
,
1447 channel_state_ht_node
);
1449 cds_lfht_del(state
->channel_state_ht
, node
);
1453 /* Remove the channel from the channels_ht and free it. */
1454 cds_lfht_lookup(state
->channels_ht
,
1455 hash_channel_key(&key
),
1459 node
= cds_lfht_iter_get_node(&iter
);
1461 channel_info
= caa_container_of(node
, struct channel_info
,
1463 cds_lfht_del(state
->channels_ht
, node
);
1464 channel_info_destroy(channel_info
);
1467 *cmd_result
= LTTNG_OK
;
1472 int condition_is_supported(struct lttng_condition
*condition
)
1476 switch (lttng_condition_get_type(condition
)) {
1477 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1478 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1480 enum lttng_domain_type domain
;
1482 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1489 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1495 * Older kernel tracers don't expose the API to monitor their
1496 * buffers. Therefore, we reject triggers that require that
1497 * mechanism to be available to be evaluated.
1499 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1511 * FIXME A client's credentials are not checked when registering a trigger, nor
1512 * are they stored alongside with the trigger.
1514 * The effects of this are benign since:
1515 * - The client will succeed in registering the trigger, as it is valid,
1516 * - The trigger will, internally, be bound to the channel,
1517 * - The notifications will not be sent since the client's credentials
1518 * are checked against the channel at that moment.
1520 * If this function returns a non-zero value, it means something is
1521 * fundamentally broken and the whole subsystem/thread will be torn down.
1523 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1527 int handle_notification_thread_command_register_trigger(
1528 struct notification_thread_state
*state
,
1529 struct lttng_trigger
*trigger
,
1530 enum lttng_error_code
*cmd_result
)
1533 struct lttng_condition
*condition
;
1534 struct notification_client
*client
;
1535 struct notification_client_list
*client_list
= NULL
;
1536 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1537 struct notification_client_list_element
*client_list_element
, *tmp
;
1538 struct cds_lfht_node
*node
;
1539 struct cds_lfht_iter iter
;
1540 struct channel_info
*channel
;
1541 bool free_trigger
= true;
1545 condition
= lttng_trigger_get_condition(trigger
);
1548 ret
= condition_is_supported(condition
);
1551 } else if (ret
== 0) {
1552 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1555 /* Feature is supported, continue. */
1559 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1560 if (!trigger_ht_element
) {
1565 /* Add trigger to the trigger_ht. */
1566 cds_lfht_node_init(&trigger_ht_element
->node
);
1567 trigger_ht_element
->trigger
= trigger
;
1569 node
= cds_lfht_add_unique(state
->triggers_ht
,
1570 lttng_condition_hash(condition
),
1573 &trigger_ht_element
->node
);
1574 if (node
!= &trigger_ht_element
->node
) {
1575 /* Not a fatal error, simply report it to the client. */
1576 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1577 goto error_free_ht_element
;
1581 * Ownership of the trigger and of its wrapper was transfered to
1584 trigger_ht_element
= NULL
;
1585 free_trigger
= false;
1588 * The rest only applies to triggers that have a "notify" action.
1589 * It is not skipped as this is the only action type currently
1592 client_list
= zmalloc(sizeof(*client_list
));
1595 goto error_free_ht_element
;
1597 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1598 CDS_INIT_LIST_HEAD(&client_list
->list
);
1599 client_list
->trigger
= trigger
;
1601 /* Build a list of clients to which this new trigger applies. */
1602 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1603 client_socket_ht_node
) {
1604 if (!trigger_applies_to_client(trigger
, client
)) {
1608 client_list_element
= zmalloc(sizeof(*client_list_element
));
1609 if (!client_list_element
) {
1611 goto error_free_client_list
;
1613 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1614 client_list_element
->client
= client
;
1615 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1618 cds_lfht_add(state
->notification_trigger_clients_ht
,
1619 lttng_condition_hash(condition
),
1620 &client_list
->notification_trigger_ht_node
);
1623 * Add the trigger to list of triggers bound to the channels currently
1626 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1628 struct lttng_trigger_list_element
*trigger_list_element
;
1629 struct lttng_channel_trigger_list
*trigger_list
;
1631 if (!trigger_applies_to_channel(trigger
, channel
)) {
1635 cds_lfht_lookup(state
->channel_triggers_ht
,
1636 hash_channel_key(&channel
->key
),
1637 match_channel_trigger_list
,
1640 node
= cds_lfht_iter_get_node(&iter
);
1642 trigger_list
= caa_container_of(node
,
1643 struct lttng_channel_trigger_list
,
1644 channel_triggers_ht_node
);
1646 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1647 if (!trigger_list_element
) {
1649 goto error_free_client_list
;
1651 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1652 trigger_list_element
->trigger
= trigger
;
1653 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1657 * Since there is nothing preventing clients from subscribing to a
1658 * condition before the corresponding trigger is registered, we have
1659 * to evaluate this new condition right away.
1661 * At some point, we were waiting for the next "evaluation" (e.g. on
1662 * reception of a channel sample) to evaluate this new condition, but
1665 * The reason it was broken is that waiting for the next sample
1666 * does not allow us to properly handle transitions for edge-triggered
1669 * Consider this example: when we handle a new channel sample, we
1670 * evaluate each conditions twice: once with the previous state, and
1671 * again with the newest state. We then use those two results to
1672 * determine whether a state change happened: a condition was false and
1673 * became true. If a state change happened, we have to notify clients.
1675 * Now, if a client subscribes to a given notification and registers
1676 * a trigger *after* that subscription, we have to make sure the
1677 * condition is evaluated at this point while considering only the
1678 * current state. Otherwise, the next evaluation cycle may only see
1679 * that the evaluations remain the same (true for samples n-1 and n) and
1680 * the client will never know that the condition has been met.
1682 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1683 &client_list
->list
, node
) {
1684 ret
= evaluate_condition_for_client(trigger
, condition
,
1685 client_list_element
->client
, state
);
1687 goto error_free_client_list
;
1692 * Client list ownership transferred to the
1693 * notification_trigger_clients_ht.
1697 *cmd_result
= LTTNG_OK
;
1698 error_free_client_list
:
1700 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1701 &client_list
->list
, node
) {
1702 free(client_list_element
);
1706 error_free_ht_element
:
1707 free(trigger_ht_element
);
1710 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1712 lttng_condition_destroy(condition
);
1713 lttng_action_destroy(action
);
1714 lttng_trigger_destroy(trigger
);
1721 int handle_notification_thread_command_unregister_trigger(
1722 struct notification_thread_state
*state
,
1723 struct lttng_trigger
*trigger
,
1724 enum lttng_error_code
*_cmd_reply
)
1726 struct cds_lfht_iter iter
;
1727 struct cds_lfht_node
*node
, *triggers_ht_node
;
1728 struct lttng_channel_trigger_list
*trigger_list
;
1729 struct notification_client_list
*client_list
;
1730 struct notification_client_list_element
*client_list_element
, *tmp
;
1731 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1732 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1734 struct lttng_action
*action
;
1735 enum lttng_error_code cmd_reply
;
1739 cds_lfht_lookup(state
->triggers_ht
,
1740 lttng_condition_hash(condition
),
1744 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1745 if (!triggers_ht_node
) {
1746 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1749 cmd_reply
= LTTNG_OK
;
1752 /* Remove trigger from channel_triggers_ht. */
1753 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1754 channel_triggers_ht_node
) {
1755 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1757 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1758 &trigger_list
->list
, node
) {
1759 const struct lttng_condition
*current_condition
=
1760 lttng_trigger_get_const_condition(
1761 trigger_element
->trigger
);
1763 assert(current_condition
);
1764 if (!lttng_condition_is_equal(condition
,
1765 current_condition
)) {
1769 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1770 cds_list_del(&trigger_element
->node
);
1771 /* A trigger can only appear once per channel */
1777 * Remove and release the client list from
1778 * notification_trigger_clients_ht.
1780 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1781 lttng_condition_hash(condition
),
1785 node
= cds_lfht_iter_get_node(&iter
);
1787 client_list
= caa_container_of(node
, struct notification_client_list
,
1788 notification_trigger_ht_node
);
1789 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1790 &client_list
->list
, node
) {
1791 free(client_list_element
);
1793 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1796 /* Remove trigger from triggers_ht. */
1797 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1798 struct lttng_trigger_ht_element
, node
);
1799 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1801 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1802 lttng_condition_destroy(condition
);
1803 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1804 lttng_action_destroy(action
);
1805 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1806 free(trigger_ht_element
);
1810 *_cmd_reply
= cmd_reply
;
1815 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1816 int handle_notification_thread_command(
1817 struct notification_thread_handle
*handle
,
1818 struct notification_thread_state
*state
)
1822 struct notification_thread_command
*cmd
;
1824 /* Read the event pipe to put it back into a quiescent state. */
1825 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1831 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1832 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1833 struct notification_thread_command
, cmd_list_node
);
1834 switch (cmd
->type
) {
1835 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1836 DBG("[notification-thread] Received register trigger command");
1837 ret
= handle_notification_thread_command_register_trigger(
1838 state
, cmd
->parameters
.trigger
,
1841 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1842 DBG("[notification-thread] Received unregister trigger command");
1843 ret
= handle_notification_thread_command_unregister_trigger(
1844 state
, cmd
->parameters
.trigger
,
1847 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1848 DBG("[notification-thread] Received add channel command");
1849 ret
= handle_notification_thread_command_add_channel(
1851 cmd
->parameters
.add_channel
.session
.name
,
1852 cmd
->parameters
.add_channel
.session
.uid
,
1853 cmd
->parameters
.add_channel
.session
.gid
,
1854 cmd
->parameters
.add_channel
.channel
.name
,
1855 cmd
->parameters
.add_channel
.channel
.domain
,
1856 cmd
->parameters
.add_channel
.channel
.key
,
1857 cmd
->parameters
.add_channel
.channel
.capacity
,
1860 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1861 DBG("[notification-thread] Received remove channel command");
1862 ret
= handle_notification_thread_command_remove_channel(
1863 state
, cmd
->parameters
.remove_channel
.key
,
1864 cmd
->parameters
.remove_channel
.domain
,
1867 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1868 DBG("[notification-thread] Received quit command");
1869 cmd
->reply_code
= LTTNG_OK
;
1873 ERR("[notification-thread] Unknown internal command received");
1881 cds_list_del(&cmd
->cmd_list_node
);
1882 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1883 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1886 /* Wake-up and return a fatal error to the calling thread. */
1887 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1888 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1889 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1891 /* Indicate a fatal error to the caller. */
1896 unsigned long hash_client_socket(int socket
)
1898 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1902 int socket_set_non_blocking(int socket
)
1906 /* Set the pipe as non-blocking. */
1907 ret
= fcntl(socket
, F_GETFL
, 0);
1909 PERROR("fcntl get socket flags");
1914 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1916 PERROR("fcntl set O_NONBLOCK socket flag");
1919 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1925 int client_reset_inbound_state(struct notification_client
*client
)
1929 ret
= lttng_dynamic_buffer_set_size(
1930 &client
->communication
.inbound
.buffer
, 0);
1933 client
->communication
.inbound
.bytes_to_receive
=
1934 sizeof(struct lttng_notification_channel_message
);
1935 client
->communication
.inbound
.msg_type
=
1936 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1937 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1938 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1939 ret
= lttng_dynamic_buffer_set_size(
1940 &client
->communication
.inbound
.buffer
,
1941 client
->communication
.inbound
.bytes_to_receive
);
1945 int handle_notification_thread_client_connect(
1946 struct notification_thread_state
*state
)
1949 struct notification_client
*client
;
1951 DBG("[notification-thread] Handling new notification channel client connection");
1953 client
= zmalloc(sizeof(*client
));
1959 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1960 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1961 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1962 client
->communication
.inbound
.expect_creds
= true;
1963 ret
= client_reset_inbound_state(client
);
1965 ERR("[notification-thread] Failed to reset client communication's inbound state");
1970 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1972 ERR("[notification-thread] Failed to accept new notification channel client connection");
1977 client
->socket
= ret
;
1979 ret
= socket_set_non_blocking(client
->socket
);
1981 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1985 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1987 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1992 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1993 LPOLLIN
| LPOLLERR
|
1994 LPOLLHUP
| LPOLLRDHUP
);
1996 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2000 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2004 cds_lfht_add(state
->client_socket_ht
,
2005 hash_client_socket(client
->socket
),
2006 &client
->client_socket_ht_node
);
2011 notification_client_destroy(client
, state
);
2015 int handle_notification_thread_client_disconnect(
2017 struct notification_thread_state
*state
)
2020 struct notification_client
*client
;
2023 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2025 client
= get_client_from_socket(client_socket
, state
);
2027 /* Internal state corruption, fatal error. */
2028 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2034 ret
= lttng_poll_del(&state
->events
, client_socket
);
2036 ERR("[notification-thread] Failed to remove client socket from poll set");
2038 cds_lfht_del(state
->client_socket_ht
,
2039 &client
->client_socket_ht_node
);
2040 notification_client_destroy(client
, state
);
2046 int handle_notification_thread_client_disconnect_all(
2047 struct notification_thread_state
*state
)
2049 struct cds_lfht_iter iter
;
2050 struct notification_client
*client
;
2051 bool error_encoutered
= false;
2054 DBG("[notification-thread] Closing all client connections");
2055 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2056 client_socket_ht_node
) {
2059 ret
= handle_notification_thread_client_disconnect(
2060 client
->socket
, state
);
2062 error_encoutered
= true;
2066 return error_encoutered
? 1 : 0;
2069 int handle_notification_thread_trigger_unregister_all(
2070 struct notification_thread_state
*state
)
2072 bool error_occurred
= false;
2073 struct cds_lfht_iter iter
;
2074 struct lttng_trigger_ht_element
*trigger_ht_element
;
2076 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2078 int ret
= handle_notification_thread_command_unregister_trigger(
2079 state
, trigger_ht_element
->trigger
, NULL
);
2081 error_occurred
= true;
2084 return error_occurred
? -1 : 0;
2088 int client_flush_outgoing_queue(struct notification_client
*client
,
2089 struct notification_thread_state
*state
)
2092 size_t to_send_count
;
2094 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2095 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2096 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2099 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2100 client
->communication
.outbound
.buffer
.data
,
2102 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
2103 (ret
> 0 && ret
< to_send_count
)) {
2104 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2106 to_send_count
-= max(ret
, 0);
2108 memcpy(client
->communication
.outbound
.buffer
.data
,
2109 client
->communication
.outbound
.buffer
.data
+
2110 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2112 ret
= lttng_dynamic_buffer_set_size(
2113 &client
->communication
.outbound
.buffer
,
2120 * We want to be notified whenever there is buffer space
2121 * available to send the rest of the payload.
2123 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2124 CLIENT_POLL_MASK_IN_OUT
);
2128 } else if (ret
< 0) {
2129 /* Generic error, disconnect the client. */
2130 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2132 ret
= handle_notification_thread_client_disconnect(
2133 client
->socket
, state
);
2138 /* No error and flushed the queue completely. */
2139 ret
= lttng_dynamic_buffer_set_size(
2140 &client
->communication
.outbound
.buffer
, 0);
2144 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2145 CLIENT_POLL_MASK_IN
);
2150 client
->communication
.outbound
.queued_command_reply
= false;
2151 client
->communication
.outbound
.dropped_notification
= false;
2160 int client_send_command_reply(struct notification_client
*client
,
2161 struct notification_thread_state
*state
,
2162 enum lttng_notification_channel_status status
)
2165 struct lttng_notification_channel_command_reply reply
= {
2166 .status
= (int8_t) status
,
2168 struct lttng_notification_channel_message msg
= {
2169 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2170 .size
= sizeof(reply
),
2172 char buffer
[sizeof(msg
) + sizeof(reply
)];
2174 if (client
->communication
.outbound
.queued_command_reply
) {
2175 /* Protocol error. */
2179 memcpy(buffer
, &msg
, sizeof(msg
));
2180 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2181 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2183 /* Enqueue buffer to outgoing queue and flush it. */
2184 ret
= lttng_dynamic_buffer_append(
2185 &client
->communication
.outbound
.buffer
,
2186 buffer
, sizeof(buffer
));
2191 ret
= client_flush_outgoing_queue(client
, state
);
2196 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2197 /* Queue could not be emptied. */
2198 client
->communication
.outbound
.queued_command_reply
= true;
2207 int client_dispatch_message(struct notification_client
*client
,
2208 struct notification_thread_state
*state
)
2212 if (client
->communication
.inbound
.msg_type
!=
2213 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2214 client
->communication
.inbound
.msg_type
!=
2215 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2216 !client
->validated
) {
2217 WARN("[notification-thread] client attempted a command before handshake");
2222 switch (client
->communication
.inbound
.msg_type
) {
2223 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2226 * Receiving message header. The function will be called again
2227 * once the rest of the message as been received and can be
2230 const struct lttng_notification_channel_message
*msg
;
2232 assert(sizeof(*msg
) ==
2233 client
->communication
.inbound
.buffer
.size
);
2234 msg
= (const struct lttng_notification_channel_message
*)
2235 client
->communication
.inbound
.buffer
.data
;
2237 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2238 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2243 switch (msg
->type
) {
2244 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2245 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2246 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2250 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2254 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2255 client
->communication
.inbound
.msg_type
=
2256 (enum lttng_notification_channel_message_type
) msg
->type
;
2257 ret
= lttng_dynamic_buffer_set_size(
2258 &client
->communication
.inbound
.buffer
, msg
->size
);
2264 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2266 struct lttng_notification_channel_command_handshake
*handshake_client
;
2267 struct lttng_notification_channel_command_handshake handshake_reply
= {
2268 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2269 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2271 struct lttng_notification_channel_message msg_header
= {
2272 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2273 .size
= sizeof(handshake_reply
),
2275 enum lttng_notification_channel_status status
=
2276 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2277 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2279 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2280 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2281 sizeof(handshake_reply
));
2284 (struct lttng_notification_channel_command_handshake
*)
2285 client
->communication
.inbound
.buffer
.data
;
2286 client
->major
= handshake_client
->major
;
2287 client
->minor
= handshake_client
->minor
;
2288 if (!client
->communication
.inbound
.creds_received
) {
2289 ERR("[notification-thread] No credentials received from client");
2294 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2295 &client
->communication
.inbound
.creds
);
2296 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2297 &client
->communication
.inbound
.creds
);
2298 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2299 client
->uid
, client
->gid
, (int) client
->major
,
2300 (int) client
->minor
);
2302 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2303 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2306 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2307 send_buffer
, sizeof(send_buffer
));
2309 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2313 ret
= client_flush_outgoing_queue(client
, state
);
2318 ret
= client_send_command_reply(client
, state
, status
);
2320 ERR("[notification-thread] Failed to send reply to notification channel client");
2324 /* Set reception state to receive the next message header. */
2325 ret
= client_reset_inbound_state(client
);
2327 ERR("[notification-thread] Failed to reset client communication's inbound state");
2330 client
->validated
= true;
2333 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2334 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2336 struct lttng_condition
*condition
;
2337 enum lttng_notification_channel_status status
=
2338 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2339 const struct lttng_buffer_view condition_view
=
2340 lttng_buffer_view_from_dynamic_buffer(
2341 &client
->communication
.inbound
.buffer
,
2343 size_t expected_condition_size
=
2344 client
->communication
.inbound
.buffer
.size
;
2346 ret
= lttng_condition_create_from_buffer(&condition_view
,
2348 if (ret
!= expected_condition_size
) {
2349 ERR("[notification-thread] Malformed condition received from client");
2353 if (client
->communication
.inbound
.msg_type
==
2354 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2355 ret
= notification_thread_client_subscribe(client
,
2356 condition
, state
, &status
);
2358 ret
= notification_thread_client_unsubscribe(client
,
2359 condition
, state
, &status
);
2365 ret
= client_send_command_reply(client
, state
, status
);
2367 ERR("[notification-thread] Failed to send reply to notification channel client");
2371 /* Set reception state to receive the next message header. */
2372 ret
= client_reset_inbound_state(client
);
2374 ERR("[notification-thread] Failed to reset client communication's inbound state");
2386 /* Incoming data from client. */
2387 int handle_notification_thread_client_in(
2388 struct notification_thread_state
*state
, int socket
)
2391 struct notification_client
*client
;
2395 client
= get_client_from_socket(socket
, state
);
2397 /* Internal error, abort. */
2402 offset
= client
->communication
.inbound
.buffer
.size
-
2403 client
->communication
.inbound
.bytes_to_receive
;
2404 if (client
->communication
.inbound
.expect_creds
) {
2405 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2406 client
->communication
.inbound
.buffer
.data
+ offset
,
2407 client
->communication
.inbound
.bytes_to_receive
,
2408 &client
->communication
.inbound
.creds
);
2410 client
->communication
.inbound
.expect_creds
= false;
2411 client
->communication
.inbound
.creds_received
= true;
2414 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2415 client
->communication
.inbound
.buffer
.data
+ offset
,
2416 client
->communication
.inbound
.bytes_to_receive
);
2419 goto error_disconnect_client
;
2422 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2423 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2424 ret
= client_dispatch_message(client
, state
);
2427 * Only returns an error if this client must be
2430 goto error_disconnect_client
;
2437 error_disconnect_client
:
2438 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2442 /* Client ready to receive outgoing data. */
2443 int handle_notification_thread_client_out(
2444 struct notification_thread_state
*state
, int socket
)
2447 struct notification_client
*client
;
2449 client
= get_client_from_socket(socket
, state
);
2451 /* Internal error, abort. */
2456 ret
= client_flush_outgoing_queue(client
, state
);
2465 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2466 const struct channel_state_sample
*sample
,
2467 uint64_t buffer_capacity
)
2469 bool result
= false;
2471 enum lttng_condition_type condition_type
;
2472 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2473 condition
, struct lttng_condition_buffer_usage
,
2476 if (use_condition
->threshold_bytes
.set
) {
2477 threshold
= use_condition
->threshold_bytes
.value
;
2480 * Threshold was expressed as a ratio.
2482 * TODO the threshold (in bytes) of conditions expressed
2483 * as a ratio of total buffer size could be cached to
2484 * forego this double-multiplication or it could be performed
2485 * as fixed-point math.
2487 * Note that caching should accomodate the case where the
2488 * condition applies to multiple channels (i.e. don't assume
2489 * that all channels matching my_chann* have the same size...)
2491 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2492 (double) buffer_capacity
);
2495 condition_type
= lttng_condition_get_type(condition
);
2496 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2497 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2498 threshold
, sample
->highest_usage
);
2501 * The low condition should only be triggered once _all_ of the
2502 * streams in a channel have gone below the "low" threshold.
2504 if (sample
->highest_usage
<= threshold
) {
2508 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2509 threshold
, sample
->highest_usage
);
2512 * For high buffer usage scenarios, we want to trigger whenever
2513 * _any_ of the streams has reached the "high" threshold.
2515 if (sample
->highest_usage
>= threshold
) {
2524 bool evaluate_session_consumed_size_condition(
2525 const struct lttng_condition
*condition
,
2526 uint64_t session_consumed_size
)
2529 const struct lttng_condition_session_consumed_size
*size_condition
=
2530 container_of(condition
,
2531 struct lttng_condition_session_consumed_size
,
2534 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2535 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2536 threshold
, session_consumed_size
);
2537 return session_consumed_size
>= threshold
;
2541 int evaluate_condition(const struct lttng_condition
*condition
,
2542 struct lttng_evaluation
**evaluation
,
2543 const struct notification_thread_state
*state
,
2544 const struct channel_state_sample
*previous_sample
,
2545 const struct channel_state_sample
*latest_sample
,
2546 uint64_t previous_session_consumed_total
,
2547 uint64_t latest_session_consumed_total
,
2548 struct channel_info
*channel_info
)
2551 enum lttng_condition_type condition_type
;
2552 const bool previous_sample_available
= !!previous_sample
;
2553 bool previous_sample_result
= false;
2554 bool latest_sample_result
;
2556 condition_type
= lttng_condition_get_type(condition
);
2558 switch (condition_type
) {
2559 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2560 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2561 if (caa_likely(previous_sample_available
)) {
2562 previous_sample_result
=
2563 evaluate_buffer_usage_condition(condition
,
2564 previous_sample
, channel_info
->capacity
);
2566 latest_sample_result
= evaluate_buffer_usage_condition(
2567 condition
, latest_sample
,
2568 channel_info
->capacity
);
2570 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2571 if (caa_likely(previous_sample_available
)) {
2572 previous_sample_result
=
2573 evaluate_session_consumed_size_condition(
2575 previous_session_consumed_total
);
2577 latest_sample_result
=
2578 evaluate_session_consumed_size_condition(
2580 latest_session_consumed_total
);
2583 /* Unknown condition type; internal error. */
2587 if (!latest_sample_result
||
2588 (previous_sample_result
== latest_sample_result
)) {
2590 * Only trigger on a condition evaluation transition.
2592 * NOTE: This edge-triggered logic may not be appropriate for
2593 * future condition types.
2598 if (!evaluation
|| !latest_sample_result
) {
2602 switch (condition_type
) {
2603 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2604 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2605 *evaluation
= lttng_evaluation_buffer_usage_create(
2607 latest_sample
->highest_usage
,
2608 channel_info
->capacity
);
2610 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2611 *evaluation
= lttng_evaluation_session_consumed_size_create(
2612 latest_session_consumed_total
);
2627 int client_enqueue_dropped_notification(struct notification_client
*client
,
2628 struct notification_thread_state
*state
)
2631 struct lttng_notification_channel_message msg
= {
2632 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2636 ret
= lttng_dynamic_buffer_append(
2637 &client
->communication
.outbound
.buffer
, &msg
,
2643 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
2644 const struct lttng_evaluation
*evaluation
,
2645 struct notification_client_list
* client_list
,
2646 struct notification_thread_state
*state
,
2647 uid_t channel_uid
, gid_t channel_gid
)
2650 struct lttng_dynamic_buffer msg_buffer
;
2651 struct notification_client_list_element
*client_list_element
, *tmp
;
2652 const struct lttng_notification notification
= {
2653 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
2654 .evaluation
= (struct lttng_evaluation
*) evaluation
,
2656 struct lttng_notification_channel_message msg_header
= {
2657 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
2660 lttng_dynamic_buffer_init(&msg_buffer
);
2662 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
2663 sizeof(msg_header
));
2668 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
2670 ERR("[notification-thread] Failed to serialize notification");
2675 /* Update payload size. */
2676 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
2677 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
2679 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2680 &client_list
->list
, node
) {
2681 struct notification_client
*client
=
2682 client_list_element
->client
;
2684 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2686 /* Client is not allowed to monitor this channel. */
2687 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2691 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2692 client
->socket
, msg_buffer
.size
);
2693 if (client
->communication
.outbound
.buffer
.size
) {
2695 * Outgoing data is already buffered for this client;
2696 * drop the notification and enqueue a "dropped
2697 * notification" message if this is the first dropped
2698 * notification since the socket spilled-over to the
2701 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2703 if (!client
->communication
.outbound
.dropped_notification
) {
2704 client
->communication
.outbound
.dropped_notification
= true;
2705 ret
= client_enqueue_dropped_notification(
2714 ret
= lttng_dynamic_buffer_append_buffer(
2715 &client
->communication
.outbound
.buffer
,
2721 ret
= client_flush_outgoing_queue(client
, state
);
2728 lttng_dynamic_buffer_reset(&msg_buffer
);
2732 int handle_notification_thread_channel_sample(
2733 struct notification_thread_state
*state
, int pipe
,
2734 enum lttng_domain_type domain
)
2737 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2738 struct channel_info
*channel_info
;
2739 struct cds_lfht_node
*node
;
2740 struct cds_lfht_iter iter
;
2741 struct lttng_channel_trigger_list
*trigger_list
;
2742 struct lttng_trigger_list_element
*trigger_list_element
;
2743 bool previous_sample_available
= false;
2744 struct channel_state_sample previous_sample
, latest_sample
;
2745 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2748 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2749 * ensuring that read/write of sampling messages are atomic.
2751 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2752 if (ret
!= sizeof(sample_msg
)) {
2753 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2760 latest_sample
.key
.key
= sample_msg
.key
;
2761 latest_sample
.key
.domain
= domain
;
2762 latest_sample
.highest_usage
= sample_msg
.highest
;
2763 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2764 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2768 /* Retrieve the channel's informations */
2769 cds_lfht_lookup(state
->channels_ht
,
2770 hash_channel_key(&latest_sample
.key
),
2774 node
= cds_lfht_iter_get_node(&iter
);
2775 if (caa_unlikely(!node
)) {
2777 * Not an error since the consumer can push a sample to the pipe
2778 * and the rest of the session daemon could notify us of the
2779 * channel's destruction before we get a chance to process that
2782 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2783 latest_sample
.key
.key
,
2784 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2788 channel_info
= caa_container_of(node
, struct channel_info
,
2790 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2792 latest_sample
.key
.key
,
2793 channel_info
->session_info
->name
,
2794 latest_sample
.highest_usage
,
2795 latest_sample
.lowest_usage
,
2796 latest_sample
.channel_total_consumed
);
2798 previous_session_consumed_total
=
2799 channel_info
->session_info
->consumed_data_size
;
2801 /* Retrieve the channel's last sample, if it exists, and update it. */
2802 cds_lfht_lookup(state
->channel_state_ht
,
2803 hash_channel_key(&latest_sample
.key
),
2804 match_channel_state_sample
,
2807 node
= cds_lfht_iter_get_node(&iter
);
2808 if (caa_likely(node
)) {
2809 struct channel_state_sample
*stored_sample
;
2811 /* Update the sample stored. */
2812 stored_sample
= caa_container_of(node
,
2813 struct channel_state_sample
,
2814 channel_state_ht_node
);
2816 memcpy(&previous_sample
, stored_sample
,
2817 sizeof(previous_sample
));
2818 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2819 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2820 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2821 previous_sample_available
= true;
2823 latest_session_consumed_total
=
2824 previous_session_consumed_total
+
2825 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2828 * This is the channel's first sample, allocate space for and
2829 * store the new sample.
2831 struct channel_state_sample
*stored_sample
;
2833 stored_sample
= zmalloc(sizeof(*stored_sample
));
2834 if (!stored_sample
) {
2839 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2840 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2841 cds_lfht_add(state
->channel_state_ht
,
2842 hash_channel_key(&stored_sample
->key
),
2843 &stored_sample
->channel_state_ht_node
);
2845 latest_session_consumed_total
=
2846 previous_session_consumed_total
+
2847 latest_sample
.channel_total_consumed
;
2850 channel_info
->session_info
->consumed_data_size
=
2851 latest_session_consumed_total
;
2853 /* Find triggers associated with this channel. */
2854 cds_lfht_lookup(state
->channel_triggers_ht
,
2855 hash_channel_key(&latest_sample
.key
),
2856 match_channel_trigger_list
,
2859 node
= cds_lfht_iter_get_node(&iter
);
2860 if (caa_likely(!node
)) {
2864 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2865 channel_triggers_ht_node
);
2866 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2868 const struct lttng_condition
*condition
;
2869 const struct lttng_action
*action
;
2870 const struct lttng_trigger
*trigger
;
2871 struct notification_client_list
*client_list
;
2872 struct lttng_evaluation
*evaluation
= NULL
;
2874 trigger
= trigger_list_element
->trigger
;
2875 condition
= lttng_trigger_get_const_condition(trigger
);
2877 action
= lttng_trigger_get_const_action(trigger
);
2879 /* Notify actions are the only type currently supported. */
2880 assert(lttng_action_get_type_const(action
) ==
2881 LTTNG_ACTION_TYPE_NOTIFY
);
2884 * Check if any client is subscribed to the result of this
2887 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2888 lttng_condition_hash(condition
),
2892 node
= cds_lfht_iter_get_node(&iter
);
2895 client_list
= caa_container_of(node
,
2896 struct notification_client_list
,
2897 notification_trigger_ht_node
);
2898 if (cds_list_empty(&client_list
->list
)) {
2900 * No clients interested in the evaluation's result,
2906 ret
= evaluate_condition(condition
, &evaluation
, state
,
2907 previous_sample_available
? &previous_sample
: NULL
,
2909 previous_session_consumed_total
,
2910 latest_session_consumed_total
,
2912 if (caa_unlikely(ret
)) {
2916 if (caa_likely(!evaluation
)) {
2920 /* Dispatch evaluation result to all clients. */
2921 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2922 evaluation
, client_list
, state
,
2923 channel_info
->session_info
->uid
,
2924 channel_info
->session_info
->gid
);
2925 lttng_evaluation_destroy(evaluation
);
2926 if (caa_unlikely(ret
)) {