2 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "event-notifier-error-accounting.hpp"
9 #include "lttng-ust-error.hpp"
10 #include "ust-app.hpp"
12 #include <common/error.hpp>
13 #include <common/hashtable/hashtable.hpp>
14 #include <common/index-allocator.hpp>
15 #include <common/kernel-ctl/kernel-ctl.hpp>
16 #include <common/shm.hpp>
17 #include <common/urcu.hpp>
19 #include <lttng/trigger/trigger-internal.hpp>
26 #include <urcu/compiler.h>
28 #define ERROR_COUNTER_INDEX_HT_INITIAL_SIZE 16
31 struct index_ht_entry
{
32 struct lttng_ht_node_u64 node
;
33 uint64_t error_counter_index
;
34 struct rcu_head rcu_head
;
37 struct ust_error_accounting_entry
{
40 struct lttng_ht_node_u64 node
;
41 struct rcu_head rcu_head
;
42 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
44 * Those `lttng_ust_abi_object_data` are anonymous handles to the
46 * They are only used to be duplicated for each new applications of the
47 * user. To destroy them, call with the `sock` parameter set to -1.
48 * e.g. `lttng_ust_ctl_release_object(-1, data)`;
50 struct lttng_ust_abi_object_data
*counter
;
51 struct lttng_ust_abi_object_data
**cpu_counters
;
52 int nr_counter_cpu_fds
;
55 struct kernel_error_accounting_entry
{
59 struct kernel_error_accounting_entry kernel_error_accounting_entry
;
61 /* Hashtable mapping uid to error_account_entry. */
62 struct lttng_ht
*error_counter_uid_ht
;
64 struct error_accounting_state
{
65 struct lttng_index_allocator
*index_allocator
;
66 /* Hashtable mapping event notifier token to index_ht_entry. */
67 struct lttng_ht
*indices_ht
;
68 uint64_t number_indices
;
71 struct error_accounting_state ust_state
;
72 struct error_accounting_state kernel_state
;
75 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
76 const char **trigger_name
,
77 uid_t
*trigger_owner_uid
)
79 enum lttng_trigger_status trigger_status
;
81 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
82 switch (trigger_status
) {
83 case LTTNG_TRIGGER_STATUS_OK
:
85 case LTTNG_TRIGGER_STATUS_UNSET
:
86 *trigger_name
= "(anonymous)";
92 trigger_status
= lttng_trigger_get_owner_uid(trigger
, trigger_owner_uid
);
93 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
96 static inline const char *
97 error_accounting_status_str(enum event_notifier_error_accounting_status status
)
100 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
102 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
:
104 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
106 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
:
108 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
:
109 return "NO_INDEX_AVAILABLE";
110 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
117 #ifdef HAVE_LIBLTTNG_UST_CTL
119 struct event_notifier_counter
{
120 pthread_mutex_t lock
;
124 struct event_notifier_counter the_event_notifier_counter
;
127 static void free_ust_error_accounting_entry(struct rcu_head
*head
)
130 struct ust_error_accounting_entry
*entry
=
131 lttng::utils::container_of(head
, &ust_error_accounting_entry::rcu_head
);
133 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
134 lttng_ust_ctl_release_object(-1, entry
->cpu_counters
[i
]);
135 free(entry
->cpu_counters
[i
]);
138 free(entry
->cpu_counters
);
140 lttng_ust_ctl_release_object(-1, entry
->counter
);
141 free(entry
->counter
);
143 lttng_ust_ctl_destroy_counter(entry
->daemon_counter
);
148 static bool ust_error_accounting_entry_get(struct ust_error_accounting_entry
*entry
)
150 return urcu_ref_get_unless_zero(&entry
->ref
);
153 static void ust_error_accounting_entry_release(struct urcu_ref
*entry_ref
)
155 struct ust_error_accounting_entry
*entry
=
156 lttng::utils::container_of(entry_ref
, &ust_error_accounting_entry::ref
);
158 lttng::urcu::read_lock_guard read_lock
;
159 cds_lfht_del(error_counter_uid_ht
->ht
, &entry
->node
.node
);
160 call_rcu(&entry
->rcu_head
, free_ust_error_accounting_entry
);
163 static void ust_error_accounting_entry_put(struct ust_error_accounting_entry
*entry
)
169 urcu_ref_put(&entry
->ref
, ust_error_accounting_entry_release
);
173 * Put one reference to every UID entries.
175 static void put_ref_all_ust_error_accounting_entry()
177 struct lttng_ht_iter iter
;
178 struct ust_error_accounting_entry
*uid_entry
;
180 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
183 lttng::urcu::read_lock_guard read_lock
;
185 cds_lfht_for_each_entry (
186 error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
187 ust_error_accounting_entry_put(uid_entry
);
193 * Get one reference to every UID entries.
195 static void get_ref_all_ust_error_accounting_entry()
197 struct lttng_ht_iter iter
;
198 struct ust_error_accounting_entry
*uid_entry
;
200 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
203 lttng::urcu::read_lock_guard read_lock
;
205 cds_lfht_for_each_entry (
206 error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
207 ust_error_accounting_entry_get(uid_entry
);
212 #endif /* HAVE_LIBLTTNG_UST_CTL */
214 static enum event_notifier_error_accounting_status
215 init_error_accounting_state(struct error_accounting_state
*state
, uint64_t index_count
)
217 enum event_notifier_error_accounting_status status
;
221 state
->number_indices
= index_count
;
223 state
->index_allocator
= lttng_index_allocator_create(index_count
);
224 if (!state
->index_allocator
) {
225 ERR("Failed to allocate event notifier error counter index allocator");
226 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
230 state
->indices_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
231 if (!state
->indices_ht
) {
232 ERR("Failed to allocate error counter indices hash table");
233 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
234 goto error_indices_ht
;
237 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
241 lttng_index_allocator_destroy(state
->index_allocator
);
242 state
->index_allocator
= nullptr;
247 static void fini_error_accounting_state(struct error_accounting_state
*state
)
252 * Will assert if some error counter indices were not released (an
255 lttng_ht_destroy(state
->indices_ht
);
256 lttng_index_allocator_destroy(state
->index_allocator
);
259 enum event_notifier_error_accounting_status
260 event_notifier_error_accounting_init(uint64_t buffer_size_kernel
, uint64_t buffer_size_ust
)
262 enum event_notifier_error_accounting_status status
;
264 status
= init_error_accounting_state(&kernel_state
, buffer_size_kernel
);
265 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
266 ERR("Failed to initialize kernel event notifier accounting state: status = %s",
267 error_accounting_status_str(status
));
271 status
= init_error_accounting_state(&ust_state
, buffer_size_ust
);
272 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
273 ERR("Failed to initialize UST event notifier accounting state: status = %s",
274 error_accounting_status_str(status
));
275 goto error_ust_state
;
278 error_counter_uid_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
279 if (!error_counter_uid_ht
) {
280 ERR("Failed to allocate UID to error counter accountant hash table");
281 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
285 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
289 fini_error_accounting_state(&ust_state
);
291 fini_error_accounting_state(&kernel_state
);
297 * Return the error counteur index associated to this event notifier tracer
298 * token. Returns _STATUS_OK if found and _STATUS_NOT_FOUND otherwise.
300 static enum event_notifier_error_accounting_status
get_error_counter_index_for_token(
301 struct error_accounting_state
*state
, uint64_t tracer_token
, uint64_t *error_counter_index
)
303 struct lttng_ht_node_u64
*node
;
304 struct lttng_ht_iter iter
;
305 const struct index_ht_entry
*index_entry
;
306 enum event_notifier_error_accounting_status status
;
307 lttng::urcu::read_lock_guard read_guard
;
309 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
310 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
312 index_entry
= lttng::utils::container_of(node
, &index_ht_entry::node
);
313 *error_counter_index
= index_entry
->error_counter_index
;
314 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
316 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
;
322 #ifdef HAVE_LIBLTTNG_UST_CTL
324 * Find the entry for this app's UID, the caller acquires a reference if the
327 static struct ust_error_accounting_entry
*ust_error_accounting_entry_find(struct lttng_ht
*uid_ht
,
328 const struct ust_app
*app
)
330 struct ust_error_accounting_entry
*entry
;
331 struct lttng_ht_node_u64
*node
;
332 struct lttng_ht_iter iter
;
333 uint64_t key
= app
->uid
;
335 lttng_ht_lookup(uid_ht
, &key
, &iter
);
336 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
337 if (node
== nullptr) {
342 entry
= caa_container_of(node
, struct ust_error_accounting_entry
, node
);
344 got_ref
= ust_error_accounting_entry_get(entry
);
354 * Create the entry for this app's UID, the caller acquires a reference to the
357 static struct ust_error_accounting_entry
*
358 ust_error_accounting_entry_create(const struct ust_app
*app
)
360 int i
, ret
, *cpu_counter_fds
= nullptr;
361 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
362 struct lttng_ust_abi_object_data
*counter
, **cpu_counters
;
363 struct ust_error_accounting_entry
*entry
= nullptr;
364 lttng_ust_ctl_counter_dimension dimension
= {};
366 dimension
.size
= ust_state
.number_indices
;
367 dimension
.has_underflow
= false;
368 dimension
.has_overflow
= false;
370 if (!ust_app_supports_counters(app
)) {
371 DBG("Refusing to create accounting entry for application (unsupported feature): app name = '%s', app ppid = %d",
377 entry
= zmalloc
<ust_error_accounting_entry
>();
379 PERROR("Failed to allocate event notifier error acounting entry")
383 urcu_ref_init(&entry
->ref
);
384 entry
->uid
= app
->uid
;
385 entry
->nr_counter_cpu_fds
= lttng_ust_ctl_get_nr_cpu_per_counter();
387 cpu_counter_fds
= calloc
<int>(entry
->nr_counter_cpu_fds
);
388 if (!cpu_counter_fds
) {
389 PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
393 entry
->nr_counter_cpu_fds
* sizeof(*cpu_counter_fds
));
394 goto error_counter_cpu_fds_alloc
;
397 /* Initialize to an invalid fd value to closes fds in case of error. */
398 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
399 cpu_counter_fds
[i
] = -1;
402 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
404 PERROR("Failed to allocate event notifier error counter lttng_ust_abi_object_data array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
408 entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
409 goto error_counter_cpus_alloc
;
412 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
413 cpu_counter_fds
[i
] = shm_create_anonymous("event-notifier-error-accounting");
414 if (cpu_counter_fds
[i
] == -1) {
415 ERR("Failed to create event notifier error accounting shared memory for application user: application uid = %d, pid = %d, application name = '%s'",
419 goto error_shm_alloc
;
424 * Ownership of the file descriptors transferred to the ustctl object.
426 daemon_counter
= lttng_ust_ctl_create_counter(1,
430 entry
->nr_counter_cpu_fds
,
432 LTTNG_UST_CTL_COUNTER_BITNESS_32
,
433 LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
,
434 LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
,
436 if (!daemon_counter
) {
437 goto error_create_daemon_counter
;
440 ret
= lttng_ust_ctl_create_counter_data(daemon_counter
, &counter
);
442 ERR("Failed to create userspace tracer counter data for application user: uid = %d, pid = %d, application name = '%s'",
446 goto error_create_counter_data
;
449 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
450 ret
= lttng_ust_ctl_create_counter_cpu_data(daemon_counter
, i
, &cpu_counters
[i
]);
452 ERR("Failed to create userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
456 goto error_create_counter_cpu_data
;
460 entry
->daemon_counter
= daemon_counter
;
461 entry
->counter
= counter
;
462 entry
->cpu_counters
= cpu_counters
;
464 lttng_ht_node_init_u64(&entry
->node
, entry
->uid
);
465 lttng_ht_add_unique_u64(error_counter_uid_ht
, &entry
->node
);
469 error_create_counter_cpu_data
:
470 /* Teardown any allocated cpu counters. */
471 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
472 if (!cpu_counters
[i
]) {
474 * Early-exit when error occurred before all cpu
475 * counters could be initialized.
480 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
481 free(cpu_counters
[i
]);
484 lttng_ust_ctl_release_object(-1, entry
->counter
);
485 free(entry
->counter
);
486 error_create_counter_data
:
487 lttng_ust_ctl_destroy_counter(daemon_counter
);
488 error_create_daemon_counter
:
490 /* Error occurred before per-cpu SHMs were handed-off to ustctl. */
491 if (cpu_counter_fds
) {
492 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
493 if (cpu_counter_fds
[i
] < 0) {
495 * Early-exit when error occurred before all cpu
496 * counter shm fds could be initialized.
501 ret
= close(cpu_counter_fds
[i
]);
503 PERROR("Failed to close error counter per-CPU shm file descriptor: fd = %d",
510 error_counter_cpus_alloc
:
511 error_counter_cpu_fds_alloc
:
516 free(cpu_counter_fds
);
520 static enum event_notifier_error_accounting_status
521 send_counter_data_to_ust(struct ust_app
*app
, struct lttng_ust_abi_object_data
*new_counter
)
524 enum event_notifier_error_accounting_status status
;
526 /* Attach counter to trigger group. */
527 pthread_mutex_lock(&app
->sock_lock
);
528 ret
= lttng_ust_ctl_send_counter_data_to_ust(
529 app
->sock
, app
->event_notifier_group
.object
->handle
, new_counter
);
530 pthread_mutex_unlock(&app
->sock_lock
);
532 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
533 ERR("Failed to send counter data to application: application name = '%s', pid = %d, ret = %d",
537 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
539 DBG3("Failed to send counter data to application (application is dead): application name = '%s', pid = %d, ret = %d",
543 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
549 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
554 static enum event_notifier_error_accounting_status
555 send_counter_cpu_data_to_ust(struct ust_app
*app
,
556 struct lttng_ust_abi_object_data
*counter
,
557 struct lttng_ust_abi_object_data
*counter_cpu
)
560 enum event_notifier_error_accounting_status status
;
562 pthread_mutex_lock(&app
->sock_lock
);
563 ret
= lttng_ust_ctl_send_counter_cpu_data_to_ust(app
->sock
, counter
, counter_cpu
);
564 pthread_mutex_unlock(&app
->sock_lock
);
566 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
567 ERR("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
571 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
573 DBG3("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
577 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
583 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
588 enum event_notifier_error_accounting_status
589 event_notifier_error_accounting_register_app(struct ust_app
*app
)
593 struct lttng_ust_abi_object_data
*new_counter
;
594 struct ust_error_accounting_entry
*entry
;
595 enum event_notifier_error_accounting_status status
;
596 struct lttng_ust_abi_object_data
**cpu_counters
;
597 lttng::urcu::read_lock_guard read_lock
;
599 if (!ust_app_supports_counters(app
)) {
600 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
;
605 * Check if we already have a error counter for the user id of this
606 * app. If not, create one.
608 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
609 if (entry
== nullptr) {
611 * Take the event notifier counter lock before creating the new
612 * entry to ensure that no event notifier is registered between
613 * the the entry creation and event notifier count check.
615 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
617 entry
= ust_error_accounting_entry_create(app
);
619 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
620 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
621 goto error_creating_entry
;
625 * We just created a new UID entry, If there are event
626 * notifiers already registered, take one reference on their
629 if (the_event_notifier_counter
.count
> 0) {
630 ust_error_accounting_entry_get(entry
);
633 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
636 /* Duplicate counter object data. */
637 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter
, entry
->counter
);
639 ERR("Failed to duplicate event notifier error accounting counter for application user: application uid = %d, pid = %d, application name = '%s'",
643 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
644 goto error_duplicate_counter
;
647 status
= send_counter_data_to_ust(app
, new_counter
);
648 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
649 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
650 goto error_send_counter_data
;
653 ERR("Failed to send counter data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
654 error_accounting_status_str(status
),
658 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
659 goto error_send_counter_data
;
662 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
664 PERROR("Failed to allocate event notifier error counter lttng_ust_abi_object_data array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
668 entry
->nr_counter_cpu_fds
* sizeof(**cpu_counters
));
669 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
670 goto error_allocate_cpu_counters
;
673 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
674 struct lttng_ust_abi_object_data
*new_counter_cpu
= nullptr;
676 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter_cpu
,
677 entry
->cpu_counters
[i
]);
679 ERR("Failed to duplicate userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
683 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
684 goto error_duplicate_cpu_counter
;
687 cpu_counters
[i
] = new_counter_cpu
;
689 status
= send_counter_cpu_data_to_ust(app
, new_counter
, new_counter_cpu
);
690 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
691 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
692 goto error_send_cpu_counter_data
;
695 ERR("Failed to send counter cpu data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
696 error_accounting_status_str(status
),
700 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
701 goto error_send_cpu_counter_data
;
705 app
->event_notifier_group
.counter
= new_counter
;
706 new_counter
= nullptr;
707 app
->event_notifier_group
.nr_counter_cpu
= entry
->nr_counter_cpu_fds
;
708 app
->event_notifier_group
.counter_cpu
= cpu_counters
;
709 cpu_counters
= nullptr;
712 error_send_cpu_counter_data
:
713 error_duplicate_cpu_counter
:
714 /* Teardown any duplicated cpu counters. */
715 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
716 if (!cpu_counters
[i
]) {
718 * Early-exit when error occurred before all cpu
719 * counters could be initialized.
724 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
725 free(cpu_counters
[i
]);
730 error_allocate_cpu_counters
:
731 error_send_counter_data
:
732 lttng_ust_ctl_release_object(-1, new_counter
);
734 error_duplicate_counter
:
735 ust_error_accounting_entry_put(entry
);
736 error_creating_entry
:
737 app
->event_notifier_group
.counter
= nullptr;
742 enum event_notifier_error_accounting_status
743 event_notifier_error_accounting_unregister_app(struct ust_app
*app
)
745 enum event_notifier_error_accounting_status status
;
746 struct ust_error_accounting_entry
*entry
;
749 lttng::urcu::read_lock_guard read_lock
;
751 /* If an error occurred during app registration no entry was created. */
752 if (!app
->event_notifier_group
.counter
) {
753 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
757 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
758 if (entry
== nullptr) {
759 ERR("Failed to find event notitifier error accounting entry on application teardown: pid = %d, application name = '%s'",
762 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
766 * Put the entry twice as we acquired a reference from the
767 * `ust_error_accounting_entry_find()` above.
769 ust_error_accounting_entry_put(entry
);
770 ust_error_accounting_entry_put(entry
);
773 for (i
= 0; i
< app
->event_notifier_group
.nr_counter_cpu
; i
++) {
774 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter_cpu
[i
]);
775 free(app
->event_notifier_group
.counter_cpu
[i
]);
778 free(app
->event_notifier_group
.counter_cpu
);
780 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter
);
781 free(app
->event_notifier_group
.counter
);
783 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
788 static enum event_notifier_error_accounting_status
789 event_notifier_error_accounting_ust_get_count(const struct lttng_trigger
*trigger
, uint64_t *count
)
791 struct lttng_ht_iter iter
;
792 struct ust_error_accounting_entry
*uid_entry
;
793 uint64_t error_counter_index
, global_sum
= 0;
794 enum event_notifier_error_accounting_status status
;
795 size_t dimension_indexes
[1];
796 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
797 uid_t trigger_owner_uid
;
798 const char *trigger_name
;
800 lttng::urcu::read_lock_guard read_lock
;
802 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
804 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
, &error_counter_index
);
805 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
806 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
807 ", trigger name = '%s', trigger owner uid = %d, status = %s",
810 (int) trigger_owner_uid
,
811 error_accounting_status_str(status
));
815 dimension_indexes
[0] = error_counter_index
;
818 * Iterate over all the UID entries.
819 * We aggregate the value of all uid entries regardless of if the uid
820 * matches the trigger's uid because a user that is allowed to register
821 * a trigger to a given sessiond is also allowed to create an event
822 * notifier on all apps that this sessiond is aware of.
824 cds_lfht_for_each_entry (error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
826 int64_t local_value
= 0;
827 bool overflow
= false, underflow
= false;
829 ret
= lttng_ust_ctl_counter_aggregate(uid_entry
->daemon_counter
,
834 if (ret
|| local_value
< 0) {
836 ERR("Failed to aggregate event notifier error counter values of trigger: trigger name = '%s', trigger owner uid = %d",
838 (int) trigger_owner_uid
);
839 } else if (local_value
< 0) {
840 ERR("Negative event notifier error counter value encountered during aggregation: trigger name = '%s', trigger owner uid = %d, value = %" PRId64
,
842 (int) trigger_owner_uid
,
848 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
852 /* Cast is safe as negative values are checked-for above. */
853 global_sum
+= (uint64_t) local_value
;
857 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
863 static enum event_notifier_error_accounting_status
864 event_notifier_error_accounting_ust_clear(const struct lttng_trigger
*trigger
)
866 struct lttng_ht_iter iter
;
867 struct ust_error_accounting_entry
*uid_entry
;
868 uint64_t error_counter_index
;
869 enum event_notifier_error_accounting_status status
;
870 size_t dimension_index
;
871 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
873 lttng::urcu::read_lock_guard read_lock
;
875 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
, &error_counter_index
);
876 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
877 uid_t trigger_owner_uid
;
878 const char *trigger_name
;
880 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
882 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
883 ", trigger name = '%s', trigger owner uid = %d, status = %s",
886 (int) trigger_owner_uid
,
887 error_accounting_status_str(status
));
891 dimension_index
= error_counter_index
;
894 * Go over all error counters (ignoring uid) as a trigger (and trigger
895 * errors) can be generated from any applications that this session
896 * daemon is managing.
898 cds_lfht_for_each_entry (error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
900 lttng_ust_ctl_counter_clear(uid_entry
->daemon_counter
, &dimension_index
);
903 uid_t trigger_owner_uid
;
904 const char *trigger_name
;
906 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
907 ERR("Failed to clear event notifier counter value for trigger: counter uid = %d, trigger name = '%s', trigger owner uid = %d",
908 (int) uid_entry
->node
.key
,
910 (int) trigger_owner_uid
);
911 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
916 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
920 #endif /* HAVE_LIBLTTNG_UST_CTL */
922 static enum event_notifier_error_accounting_status
923 event_notifier_error_accounting_kernel_clear(const struct lttng_trigger
*trigger
)
926 uint64_t error_counter_index
;
927 enum event_notifier_error_accounting_status status
;
928 struct lttng_kernel_abi_counter_clear counter_clear
= {};
930 status
= get_error_counter_index_for_token(
931 &kernel_state
, lttng_trigger_get_tracer_token(trigger
), &error_counter_index
);
932 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
933 uid_t trigger_owner_uid
;
934 const char *trigger_name
;
936 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
938 ERR("Failed to get event notifier error counter index: trigger owner uid = %d, trigger name = '%s', status = '%s'",
941 error_accounting_status_str(status
));
945 counter_clear
.index
.number_dimensions
= 1;
946 counter_clear
.index
.dimension_indexes
[0] = error_counter_index
;
948 ret
= kernctl_counter_clear(kernel_error_accounting_entry
.error_counter_fd
, &counter_clear
);
950 uid_t trigger_owner_uid
;
951 const char *trigger_name
;
953 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
955 ERR("Failed to clear kernel event notifier error counter: trigger owner uid = %d, trigger name = '%s'",
958 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
962 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
967 enum event_notifier_error_accounting_status
968 event_notifier_error_accounting_register_kernel(int kernel_event_notifier_group_fd
)
970 int error_counter_fd
= -1, ret
;
971 enum event_notifier_error_accounting_status status
;
972 lttng_kernel_abi_counter_conf error_counter_conf
= {
973 .arithmetic
= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
,
974 .bitness
= sizeof(void *) == sizeof(uint32_t) ?
975 LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
976 LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
,
977 .number_dimensions
= 1,
978 .global_sum_step
= 0,
983 error_counter_conf
.dimensions
[0].size
= kernel_state
.number_indices
;
984 error_counter_conf
.dimensions
[0].has_underflow
= false;
985 error_counter_conf
.dimensions
[0].has_overflow
= false;
987 ret
= kernctl_create_event_notifier_group_error_counter(kernel_event_notifier_group_fd
,
988 &error_counter_conf
);
990 PERROR("Failed to create event notifier group error counter through kernel ioctl: kernel_event_notifier_group_fd = %d",
991 kernel_event_notifier_group_fd
);
992 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
996 error_counter_fd
= ret
;
998 /* Prevent fd duplication after execlp(). */
999 ret
= fcntl(error_counter_fd
, F_SETFD
, FD_CLOEXEC
);
1001 PERROR("Failed to set FD_CLOEXEC flag on event notifier error counter file descriptor: error_counter_fd = %d",
1003 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1007 DBG("Created kernel event notifier group error counter: fd = %d", error_counter_fd
);
1009 kernel_error_accounting_entry
.error_counter_fd
= error_counter_fd
;
1010 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1016 static enum event_notifier_error_accounting_status
create_error_counter_index_for_token(
1017 struct error_accounting_state
*state
, uint64_t tracer_token
, uint64_t *error_counter_index
)
1019 struct index_ht_entry
*index_entry
;
1020 enum lttng_index_allocator_status index_alloc_status
;
1021 uint64_t local_error_counter_index
;
1022 enum event_notifier_error_accounting_status status
;
1024 LTTNG_ASSERT(state
);
1026 /* Allocate a new index for that counter. */
1027 index_alloc_status
=
1028 lttng_index_allocator_alloc(state
->index_allocator
, &local_error_counter_index
);
1029 switch (index_alloc_status
) {
1030 case LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY
:
1031 DBG("No indices left in the configured event notifier error counter: "
1032 "number-of-indices = %" PRIu64
,
1033 lttng_index_allocator_get_index_count(state
->index_allocator
));
1034 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
;
1036 case LTTNG_INDEX_ALLOCATOR_STATUS_OK
:
1039 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1043 index_entry
= zmalloc
<index_ht_entry
>();
1044 if (index_entry
== nullptr) {
1045 PERROR("Failed to allocate event notifier error counter hash table entry");
1046 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
1050 index_entry
->error_counter_index
= local_error_counter_index
;
1051 lttng_ht_node_init_u64(&index_entry
->node
, tracer_token
);
1052 lttng_ht_add_unique_u64(state
->indices_ht
, &index_entry
->node
);
1054 DBG("Allocated error counter index for tracer token: tracer token = %" PRIu64
1055 ", index = %" PRIu64
,
1057 local_error_counter_index
);
1058 *error_counter_index
= local_error_counter_index
;
1059 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1064 enum event_notifier_error_accounting_status
1065 event_notifier_error_accounting_register_event_notifier(const struct lttng_trigger
*trigger
,
1066 uint64_t *error_counter_index
)
1068 enum event_notifier_error_accounting_status status
;
1069 uint64_t local_error_counter_index
;
1070 struct error_accounting_state
*state
;
1072 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1073 case LTTNG_DOMAIN_KERNEL
:
1074 state
= &kernel_state
;
1076 case LTTNG_DOMAIN_UST
:
1077 case LTTNG_DOMAIN_PYTHON
:
1078 case LTTNG_DOMAIN_JUL
:
1079 case LTTNG_DOMAIN_LOG4J
:
1087 * Check if this event notifier already has a error counter index
1090 status
= get_error_counter_index_for_token(
1091 state
, lttng_trigger_get_tracer_token(trigger
), &local_error_counter_index
);
1093 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
1095 uid_t trigger_owner_uid
;
1096 const char *trigger_name
;
1098 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1100 DBG("Event notifier error counter index not found for tracer token (allocating a new one): trigger name = '%s', trigger owner uid = %d, tracer token = %" PRIu64
,
1103 lttng_trigger_get_tracer_token(trigger
));
1105 status
= create_error_counter_index_for_token(
1106 state
, lttng_trigger_get_tracer_token(trigger
), &local_error_counter_index
);
1107 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1108 ERR("Error creating index for token: status = %s, trigger name = '%s', trigger owner uid = %d",
1109 error_accounting_status_str(status
),
1116 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
1117 *error_counter_index
= local_error_counter_index
;
1118 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1124 #ifdef HAVE_LIBLTTNG_UST_CTL
1125 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1126 case LTTNG_DOMAIN_UST
:
1127 case LTTNG_DOMAIN_PYTHON
:
1128 case LTTNG_DOMAIN_JUL
:
1129 case LTTNG_DOMAIN_LOG4J
:
1130 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1131 the_event_notifier_counter
.count
++;
1132 if (the_event_notifier_counter
.count
== 1) {
1134 * On the first event notifier, we get a reference to
1135 * every existing UID entries. This ensures that the
1136 * entries are kept around if there are still
1137 * registered event notifiers but no apps.
1139 get_ref_all_ust_error_accounting_entry();
1141 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1146 #endif /* HAVE_LIBLTTNG_UST_CTL */
1152 static enum event_notifier_error_accounting_status
1153 event_notifier_error_accounting_kernel_get_count(const struct lttng_trigger
*trigger
,
1156 struct lttng_kernel_abi_counter_aggregate counter_aggregate
= {};
1157 enum event_notifier_error_accounting_status status
;
1158 uint64_t error_counter_index
;
1161 status
= get_error_counter_index_for_token(
1162 &kernel_state
, lttng_trigger_get_tracer_token(trigger
), &error_counter_index
);
1163 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1164 ERR("Error getting index for token: status=%s",
1165 error_accounting_status_str(status
));
1169 counter_aggregate
.index
.number_dimensions
= 1;
1170 counter_aggregate
.index
.dimension_indexes
[0] = error_counter_index
;
1172 LTTNG_ASSERT(kernel_error_accounting_entry
.error_counter_fd
);
1174 ret
= kernctl_counter_get_aggregate_value(kernel_error_accounting_entry
.error_counter_fd
,
1175 &counter_aggregate
);
1176 if (ret
|| counter_aggregate
.value
.value
< 0) {
1177 uid_t trigger_owner_uid
;
1178 const char *trigger_name
;
1180 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1182 if (counter_aggregate
.value
.value
< 0) {
1183 ERR("Invalid negative event notifier error counter value: trigger owner = %d, trigger name = '%s', value = %" PRId64
,
1186 counter_aggregate
.value
.value
);
1188 ERR("Failed to getting event notifier error count: trigger owner = %d, trigger name = '%s', ret = %d",
1194 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1198 /* Error count can't be negative. */
1199 LTTNG_ASSERT(counter_aggregate
.value
.value
>= 0);
1200 *count
= (uint64_t) counter_aggregate
.value
.value
;
1202 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1208 enum event_notifier_error_accounting_status
1209 event_notifier_error_accounting_get_count(const struct lttng_trigger
*trigger
, uint64_t *count
)
1211 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1212 case LTTNG_DOMAIN_KERNEL
:
1213 return event_notifier_error_accounting_kernel_get_count(trigger
, count
);
1214 case LTTNG_DOMAIN_UST
:
1215 case LTTNG_DOMAIN_PYTHON
:
1216 case LTTNG_DOMAIN_JUL
:
1217 case LTTNG_DOMAIN_LOG4J
:
1218 #ifdef HAVE_LIBLTTNG_UST_CTL
1219 return event_notifier_error_accounting_ust_get_count(trigger
, count
);
1222 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1223 #endif /* HAVE_LIBLTTNG_UST_CTL */
1229 static enum event_notifier_error_accounting_status
1230 event_notifier_error_accounting_clear(const struct lttng_trigger
*trigger
)
1232 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1233 case LTTNG_DOMAIN_KERNEL
:
1234 return event_notifier_error_accounting_kernel_clear(trigger
);
1235 case LTTNG_DOMAIN_UST
:
1236 case LTTNG_DOMAIN_PYTHON
:
1237 case LTTNG_DOMAIN_JUL
:
1238 case LTTNG_DOMAIN_LOG4J
:
1239 #ifdef HAVE_LIBLTTNG_UST_CTL
1240 return event_notifier_error_accounting_ust_clear(trigger
);
1242 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1243 #endif /* HAVE_LIBLTTNG_UST_CTL */
1249 static void free_index_ht_entry(struct rcu_head
*head
)
1251 struct index_ht_entry
*entry
= caa_container_of(head
, struct index_ht_entry
, rcu_head
);
1256 void event_notifier_error_accounting_unregister_event_notifier(const struct lttng_trigger
*trigger
)
1258 struct lttng_ht_iter iter
;
1259 struct lttng_ht_node_u64
*node
;
1260 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
1261 enum event_notifier_error_accounting_status status
;
1262 struct error_accounting_state
*state
;
1264 lttng::urcu::read_lock_guard read_lock
;
1266 status
= event_notifier_error_accounting_clear(trigger
);
1267 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1268 /* Trigger details already logged by callee on error. */
1269 ERR("Failed to clear event notifier error counter during unregistration of event notifier: status = '%s'",
1270 error_accounting_status_str(status
));
1274 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1275 case LTTNG_DOMAIN_KERNEL
:
1276 state
= &kernel_state
;
1278 #ifdef HAVE_LIBLTTNG_UST_CTL
1279 case LTTNG_DOMAIN_UST
:
1280 case LTTNG_DOMAIN_PYTHON
:
1281 case LTTNG_DOMAIN_JUL
:
1282 case LTTNG_DOMAIN_LOG4J
:
1285 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1286 the_event_notifier_counter
.count
--;
1287 if (the_event_notifier_counter
.count
== 0) {
1289 * When unregistering the last event notifier, put one
1290 * reference to every uid entries on the behalf of all
1293 put_ref_all_ust_error_accounting_entry();
1296 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1299 #endif /* HAVE_LIBLTTNG_UST_CTL */
1304 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
1305 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
1308 struct index_ht_entry
*index_entry
=
1309 lttng::utils::container_of(node
, &index_ht_entry::node
);
1310 enum lttng_index_allocator_status index_alloc_status
;
1312 index_alloc_status
= lttng_index_allocator_release(
1313 state
->index_allocator
, index_entry
->error_counter_index
);
1314 if (index_alloc_status
!= LTTNG_INDEX_ALLOCATOR_STATUS_OK
) {
1315 uid_t trigger_owner_uid
;
1316 const char *trigger_name
;
1318 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1320 ERR("Failed to release event notifier error counter index: index = %" PRIu64
1321 ", trigger name = '%s', trigger owner uid = %d",
1322 index_entry
->error_counter_index
,
1324 (int) trigger_owner_uid
);
1325 /* Don't exit, perform the rest of the clean-up. */
1328 del_ret
= lttng_ht_del(state
->indices_ht
, &iter
);
1329 LTTNG_ASSERT(!del_ret
);
1330 call_rcu(&index_entry
->rcu_head
, free_index_ht_entry
);
1336 void event_notifier_error_accounting_fini()
1338 if (kernel_error_accounting_entry
.error_counter_fd
) {
1339 const int ret
= close(kernel_error_accounting_entry
.error_counter_fd
);
1342 PERROR("Failed to close kernel event notifier error counter");
1346 lttng_ht_destroy(error_counter_uid_ht
);
1348 fini_error_accounting_state(&kernel_state
);
1349 fini_error_accounting_state(&ust_state
);