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>
18 #include <lttng/trigger/trigger-internal.hpp>
25 #include <urcu/compiler.h>
27 #define ERROR_COUNTER_INDEX_HT_INITIAL_SIZE 16
30 struct index_ht_entry
{
31 struct lttng_ht_node_u64 node
;
32 uint64_t error_counter_index
;
33 struct rcu_head rcu_head
;
36 struct ust_error_accounting_entry
{
39 struct lttng_ht_node_u64 node
;
40 struct rcu_head rcu_head
;
41 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
43 * Those `lttng_ust_abi_object_data` are anonymous handles to the
45 * They are only used to be duplicated for each new applications of the
46 * user. To destroy them, call with the `sock` parameter set to -1.
47 * e.g. `lttng_ust_ctl_release_object(-1, data)`;
49 struct lttng_ust_abi_object_data
*counter
;
50 struct lttng_ust_abi_object_data
**cpu_counters
;
51 int nr_counter_cpu_fds
;
54 struct kernel_error_accounting_entry
{
58 struct kernel_error_accounting_entry kernel_error_accounting_entry
;
60 /* Hashtable mapping uid to error_account_entry. */
61 struct lttng_ht
*error_counter_uid_ht
;
63 struct error_accounting_state
{
64 struct lttng_index_allocator
*index_allocator
;
65 /* Hashtable mapping event notifier token to index_ht_entry. */
66 struct lttng_ht
*indices_ht
;
67 uint64_t number_indices
;
70 struct error_accounting_state ust_state
;
71 struct error_accounting_state kernel_state
;
74 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
75 const char **trigger_name
,
76 uid_t
*trigger_owner_uid
)
78 enum lttng_trigger_status trigger_status
;
80 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
81 switch (trigger_status
) {
82 case LTTNG_TRIGGER_STATUS_OK
:
84 case LTTNG_TRIGGER_STATUS_UNSET
:
85 *trigger_name
= "(anonymous)";
91 trigger_status
= lttng_trigger_get_owner_uid(trigger
, trigger_owner_uid
);
92 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
95 static inline const char *
96 error_accounting_status_str(enum event_notifier_error_accounting_status status
)
99 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
101 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
:
103 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
105 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
:
107 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
:
108 return "NO_INDEX_AVAILABLE";
109 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
116 #ifdef HAVE_LIBLTTNG_UST_CTL
118 struct event_notifier_counter
{
119 pthread_mutex_t lock
;
123 struct event_notifier_counter the_event_notifier_counter
;
126 static void free_ust_error_accounting_entry(struct rcu_head
*head
)
129 struct ust_error_accounting_entry
*entry
=
130 lttng::utils::container_of(head
, &ust_error_accounting_entry::rcu_head
);
132 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
133 lttng_ust_ctl_release_object(-1, entry
->cpu_counters
[i
]);
134 free(entry
->cpu_counters
[i
]);
137 free(entry
->cpu_counters
);
139 lttng_ust_ctl_release_object(-1, entry
->counter
);
140 free(entry
->counter
);
142 lttng_ust_ctl_destroy_counter(entry
->daemon_counter
);
147 static bool ust_error_accounting_entry_get(struct ust_error_accounting_entry
*entry
)
149 return urcu_ref_get_unless_zero(&entry
->ref
);
152 static void ust_error_accounting_entry_release(struct urcu_ref
*entry_ref
)
154 struct ust_error_accounting_entry
*entry
=
155 lttng::utils::container_of(entry_ref
, &ust_error_accounting_entry::ref
);
158 cds_lfht_del(error_counter_uid_ht
->ht
, &entry
->node
.node
);
159 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(void)
177 struct lttng_ht_iter iter
;
178 struct ust_error_accounting_entry
*uid_entry
;
180 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
183 cds_lfht_for_each_entry (error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
184 ust_error_accounting_entry_put(uid_entry
);
191 * Get one reference to every UID entries.
193 static void get_ref_all_ust_error_accounting_entry(void)
195 struct lttng_ht_iter iter
;
196 struct ust_error_accounting_entry
*uid_entry
;
198 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
201 cds_lfht_for_each_entry (error_counter_uid_ht
->ht
, &iter
.iter
, uid_entry
, node
.node
) {
202 ust_error_accounting_entry_get(uid_entry
);
208 #endif /* HAVE_LIBLTTNG_UST_CTL */
210 static enum event_notifier_error_accounting_status
211 init_error_accounting_state(struct error_accounting_state
*state
, uint64_t index_count
)
213 enum event_notifier_error_accounting_status status
;
217 state
->number_indices
= index_count
;
219 state
->index_allocator
= lttng_index_allocator_create(index_count
);
220 if (!state
->index_allocator
) {
221 ERR("Failed to allocate event notifier error counter index allocator");
222 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
226 state
->indices_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
227 if (!state
->indices_ht
) {
228 ERR("Failed to allocate error counter indices hash table");
229 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
230 goto error_indices_ht
;
233 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
237 lttng_index_allocator_destroy(state
->index_allocator
);
238 state
->index_allocator
= NULL
;
243 static void fini_error_accounting_state(struct error_accounting_state
*state
)
248 * Will assert if some error counter indices were not released (an
251 lttng_ht_destroy(state
->indices_ht
);
252 lttng_index_allocator_destroy(state
->index_allocator
);
255 enum event_notifier_error_accounting_status
256 event_notifier_error_accounting_init(uint64_t buffer_size_kernel
, uint64_t buffer_size_ust
)
258 enum event_notifier_error_accounting_status status
;
260 status
= init_error_accounting_state(&kernel_state
, buffer_size_kernel
);
261 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
262 ERR("Failed to initialize kernel event notifier accounting state: status = %s",
263 error_accounting_status_str(status
));
267 status
= init_error_accounting_state(&ust_state
, buffer_size_ust
);
268 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
269 ERR("Failed to initialize UST event notifier accounting state: status = %s",
270 error_accounting_status_str(status
));
271 goto error_ust_state
;
274 error_counter_uid_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
275 if (!error_counter_uid_ht
) {
276 ERR("Failed to allocate UID to error counter accountant hash table");
277 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
281 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
285 fini_error_accounting_state(&ust_state
);
287 fini_error_accounting_state(&kernel_state
);
293 * Return the error counteur index associated to this event notifier tracer
294 * token. Returns _STATUS_OK if found and _STATUS_NOT_FOUND otherwise.
296 static enum event_notifier_error_accounting_status
get_error_counter_index_for_token(
297 struct error_accounting_state
*state
, uint64_t tracer_token
, uint64_t *error_counter_index
)
299 struct lttng_ht_node_u64
*node
;
300 struct lttng_ht_iter iter
;
301 const struct index_ht_entry
*index_entry
;
302 enum event_notifier_error_accounting_status status
;
305 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
306 node
= lttng_ht_iter_get_node_u64(&iter
);
308 index_entry
= lttng::utils::container_of(node
, &index_ht_entry::node
);
309 *error_counter_index
= index_entry
->error_counter_index
;
310 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
312 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
;
319 #ifdef HAVE_LIBLTTNG_UST_CTL
321 * Find the entry for this app's UID, the caller acquires a reference if the
324 static struct ust_error_accounting_entry
*ust_error_accounting_entry_find(struct lttng_ht
*uid_ht
,
325 const struct ust_app
*app
)
327 struct ust_error_accounting_entry
*entry
;
328 struct lttng_ht_node_u64
*node
;
329 struct lttng_ht_iter iter
;
330 uint64_t key
= app
->uid
;
332 lttng_ht_lookup(uid_ht
, &key
, &iter
);
333 node
= lttng_ht_iter_get_node_u64(&iter
);
339 entry
= caa_container_of(node
, struct ust_error_accounting_entry
, node
);
341 got_ref
= ust_error_accounting_entry_get(entry
);
351 * Create the entry for this app's UID, the caller acquires a reference to the
354 static struct ust_error_accounting_entry
*
355 ust_error_accounting_entry_create(const struct ust_app
*app
)
357 int i
, ret
, *cpu_counter_fds
= NULL
;
358 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
359 struct lttng_ust_abi_object_data
*counter
, **cpu_counters
;
360 struct ust_error_accounting_entry
*entry
= NULL
;
361 lttng_ust_ctl_counter_dimension dimension
= {};
363 dimension
.size
= ust_state
.number_indices
;
364 dimension
.has_underflow
= false;
365 dimension
.has_overflow
= false;
367 if (!ust_app_supports_counters(app
)) {
368 DBG("Refusing to create accounting entry for application (unsupported feature): app name = '%s', app ppid = %d",
374 entry
= zmalloc
<ust_error_accounting_entry
>();
376 PERROR("Failed to allocate event notifier error acounting entry")
380 urcu_ref_init(&entry
->ref
);
381 entry
->uid
= app
->uid
;
382 entry
->nr_counter_cpu_fds
= lttng_ust_ctl_get_nr_cpu_per_counter();
384 cpu_counter_fds
= calloc
<int>(entry
->nr_counter_cpu_fds
);
385 if (!cpu_counter_fds
) {
386 PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
390 entry
->nr_counter_cpu_fds
* sizeof(*cpu_counter_fds
));
391 goto error_counter_cpu_fds_alloc
;
394 /* Initialize to an invalid fd value to closes fds in case of error. */
395 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
396 cpu_counter_fds
[i
] = -1;
399 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
401 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",
405 entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
406 goto error_counter_cpus_alloc
;
409 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
410 cpu_counter_fds
[i
] = shm_create_anonymous("event-notifier-error-accounting");
411 if (cpu_counter_fds
[i
] == -1) {
412 ERR("Failed to create event notifier error accounting shared memory for application user: application uid = %d, pid = %d, application name = '%s'",
416 goto error_shm_alloc
;
421 * Ownership of the file descriptors transferred to the ustctl object.
423 daemon_counter
= lttng_ust_ctl_create_counter(1,
427 entry
->nr_counter_cpu_fds
,
429 LTTNG_UST_CTL_COUNTER_BITNESS_32
,
430 LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
,
431 LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
,
433 if (!daemon_counter
) {
434 goto error_create_daemon_counter
;
437 ret
= lttng_ust_ctl_create_counter_data(daemon_counter
, &counter
);
439 ERR("Failed to create userspace tracer counter data for application user: uid = %d, pid = %d, application name = '%s'",
443 goto error_create_counter_data
;
446 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
447 ret
= lttng_ust_ctl_create_counter_cpu_data(daemon_counter
, i
, &cpu_counters
[i
]);
449 ERR("Failed to create userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
453 goto error_create_counter_cpu_data
;
457 entry
->daemon_counter
= daemon_counter
;
458 entry
->counter
= counter
;
459 entry
->cpu_counters
= cpu_counters
;
461 lttng_ht_node_init_u64(&entry
->node
, entry
->uid
);
462 lttng_ht_add_unique_u64(error_counter_uid_ht
, &entry
->node
);
466 error_create_counter_cpu_data
:
467 /* Teardown any allocated cpu counters. */
468 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
469 if (!cpu_counters
[i
]) {
471 * Early-exit when error occurred before all cpu
472 * counters could be initialized.
477 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
478 free(cpu_counters
[i
]);
481 lttng_ust_ctl_release_object(-1, entry
->counter
);
482 free(entry
->counter
);
483 error_create_counter_data
:
484 lttng_ust_ctl_destroy_counter(daemon_counter
);
485 error_create_daemon_counter
:
487 /* Error occurred before per-cpu SHMs were handed-off to ustctl. */
488 if (cpu_counter_fds
) {
489 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
490 if (cpu_counter_fds
[i
] < 0) {
492 * Early-exit when error occurred before all cpu
493 * counter shm fds could be initialized.
498 ret
= close(cpu_counter_fds
[i
]);
500 PERROR("Failed to close error counter per-CPU shm file descriptor: fd = %d",
507 error_counter_cpus_alloc
:
508 error_counter_cpu_fds_alloc
:
513 free(cpu_counter_fds
);
517 static enum event_notifier_error_accounting_status
518 send_counter_data_to_ust(struct ust_app
*app
, struct lttng_ust_abi_object_data
*new_counter
)
521 enum event_notifier_error_accounting_status status
;
523 /* Attach counter to trigger group. */
524 pthread_mutex_lock(&app
->sock_lock
);
525 ret
= lttng_ust_ctl_send_counter_data_to_ust(
526 app
->sock
, app
->event_notifier_group
.object
->handle
, new_counter
);
527 pthread_mutex_unlock(&app
->sock_lock
);
529 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
530 ERR("Failed to send counter data to application: application name = '%s', pid = %d, ret = %d",
534 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
536 DBG3("Failed to send counter data to application (application is dead): application name = '%s', pid = %d, ret = %d",
540 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
546 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
551 static enum event_notifier_error_accounting_status
552 send_counter_cpu_data_to_ust(struct ust_app
*app
,
553 struct lttng_ust_abi_object_data
*counter
,
554 struct lttng_ust_abi_object_data
*counter_cpu
)
557 enum event_notifier_error_accounting_status status
;
559 pthread_mutex_lock(&app
->sock_lock
);
560 ret
= lttng_ust_ctl_send_counter_cpu_data_to_ust(app
->sock
, counter
, counter_cpu
);
561 pthread_mutex_unlock(&app
->sock_lock
);
563 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
564 ERR("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
568 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
570 DBG3("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
574 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
580 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
585 enum event_notifier_error_accounting_status
586 event_notifier_error_accounting_register_app(struct ust_app
*app
)
590 struct lttng_ust_abi_object_data
*new_counter
;
591 struct ust_error_accounting_entry
*entry
;
592 enum event_notifier_error_accounting_status status
;
593 struct lttng_ust_abi_object_data
**cpu_counters
;
595 if (!ust_app_supports_counters(app
)) {
596 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
;
601 * Check if we already have a error counter for the user id of this
602 * app. If not, create one.
605 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
608 * Take the event notifier counter lock before creating the new
609 * entry to ensure that no event notifier is registered between
610 * the the entry creation and event notifier count check.
612 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
614 entry
= ust_error_accounting_entry_create(app
);
616 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
617 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
618 goto error_creating_entry
;
622 * We just created a new UID entry, If there are event
623 * notifiers already registered, take one reference on their
626 if (the_event_notifier_counter
.count
> 0) {
627 ust_error_accounting_entry_get(entry
);
630 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
633 /* Duplicate counter object data. */
634 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter
, entry
->counter
);
636 ERR("Failed to duplicate event notifier error accounting counter for application user: application uid = %d, pid = %d, application name = '%s'",
640 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
641 goto error_duplicate_counter
;
644 status
= send_counter_data_to_ust(app
, new_counter
);
645 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
646 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
647 goto error_send_counter_data
;
650 ERR("Failed to send counter data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
651 error_accounting_status_str(status
),
655 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
656 goto error_send_counter_data
;
659 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
661 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",
665 entry
->nr_counter_cpu_fds
* sizeof(**cpu_counters
));
666 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
667 goto error_allocate_cpu_counters
;
670 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
671 struct lttng_ust_abi_object_data
*new_counter_cpu
= NULL
;
673 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter_cpu
,
674 entry
->cpu_counters
[i
]);
676 ERR("Failed to duplicate userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
680 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
681 goto error_duplicate_cpu_counter
;
684 cpu_counters
[i
] = new_counter_cpu
;
686 status
= send_counter_cpu_data_to_ust(app
, new_counter
, new_counter_cpu
);
687 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
688 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
689 goto error_send_cpu_counter_data
;
692 ERR("Failed to send counter cpu data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
693 error_accounting_status_str(status
),
697 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
698 goto error_send_cpu_counter_data
;
702 app
->event_notifier_group
.counter
= new_counter
;
704 app
->event_notifier_group
.nr_counter_cpu
= entry
->nr_counter_cpu_fds
;
705 app
->event_notifier_group
.counter_cpu
= cpu_counters
;
709 error_send_cpu_counter_data
:
710 error_duplicate_cpu_counter
:
711 /* Teardown any duplicated cpu counters. */
712 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
713 if (!cpu_counters
[i
]) {
715 * Early-exit when error occurred before all cpu
716 * counters could be initialized.
721 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
722 free(cpu_counters
[i
]);
727 error_allocate_cpu_counters
:
728 error_send_counter_data
:
729 lttng_ust_ctl_release_object(-1, new_counter
);
731 error_duplicate_counter
:
732 ust_error_accounting_entry_put(entry
);
733 error_creating_entry
:
734 app
->event_notifier_group
.counter
= NULL
;
741 enum event_notifier_error_accounting_status
742 event_notifier_error_accounting_unregister_app(struct ust_app
*app
)
744 enum event_notifier_error_accounting_status status
;
745 struct ust_error_accounting_entry
*entry
;
750 /* If an error occurred during app registration no entry was created. */
751 if (!app
->event_notifier_group
.counter
) {
752 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
756 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
758 ERR("Failed to find event notitifier error accounting entry on application teardown: pid = %d, application name = '%s'",
761 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
765 * Put the entry twice as we acquired a reference from the
766 * `ust_error_accounting_entry_find()` above.
768 ust_error_accounting_entry_put(entry
);
769 ust_error_accounting_entry_put(entry
);
772 for (i
= 0; i
< app
->event_notifier_group
.nr_counter_cpu
; i
++) {
773 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter_cpu
[i
]);
774 free(app
->event_notifier_group
.counter_cpu
[i
]);
777 free(app
->event_notifier_group
.counter_cpu
);
779 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter
);
780 free(app
->event_notifier_group
.counter
);
782 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
;
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
;
864 static enum event_notifier_error_accounting_status
865 event_notifier_error_accounting_ust_clear(const struct lttng_trigger
*trigger
)
867 struct lttng_ht_iter iter
;
868 struct ust_error_accounting_entry
*uid_entry
;
869 uint64_t error_counter_index
;
870 enum event_notifier_error_accounting_status status
;
871 size_t dimension_index
;
872 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
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
;
921 #endif /* HAVE_LIBLTTNG_UST_CTL */
923 static enum event_notifier_error_accounting_status
924 event_notifier_error_accounting_kernel_clear(const struct lttng_trigger
*trigger
)
927 uint64_t error_counter_index
;
928 enum event_notifier_error_accounting_status status
;
929 struct lttng_kernel_abi_counter_clear counter_clear
= {};
931 status
= get_error_counter_index_for_token(
932 &kernel_state
, lttng_trigger_get_tracer_token(trigger
), &error_counter_index
);
933 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
934 uid_t trigger_owner_uid
;
935 const char *trigger_name
;
937 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
939 ERR("Failed to get event notifier error counter index: trigger owner uid = %d, trigger name = '%s', status = '%s'",
942 error_accounting_status_str(status
));
946 counter_clear
.index
.number_dimensions
= 1;
947 counter_clear
.index
.dimension_indexes
[0] = error_counter_index
;
949 ret
= kernctl_counter_clear(kernel_error_accounting_entry
.error_counter_fd
, &counter_clear
);
951 uid_t trigger_owner_uid
;
952 const char *trigger_name
;
954 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
956 ERR("Failed to clear kernel event notifier error counter: trigger owner uid = %d, trigger name = '%s'",
959 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
963 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
968 enum event_notifier_error_accounting_status
969 event_notifier_error_accounting_register_kernel(int kernel_event_notifier_group_fd
)
971 int error_counter_fd
= -1, ret
;
972 enum event_notifier_error_accounting_status status
;
973 lttng_kernel_abi_counter_conf error_counter_conf
= {
974 .arithmetic
= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
,
975 .bitness
= sizeof(void *) == sizeof(uint32_t) ?
976 LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
977 LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
,
978 .number_dimensions
= 1,
979 .global_sum_step
= 0,
984 error_counter_conf
.dimensions
[0].size
= kernel_state
.number_indices
;
985 error_counter_conf
.dimensions
[0].has_underflow
= false;
986 error_counter_conf
.dimensions
[0].has_overflow
= false;
988 ret
= kernctl_create_event_notifier_group_error_counter(kernel_event_notifier_group_fd
,
989 &error_counter_conf
);
991 PERROR("Failed to create event notifier group error counter through kernel ioctl: kernel_event_notifier_group_fd = %d",
992 kernel_event_notifier_group_fd
);
993 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
997 error_counter_fd
= ret
;
999 /* Prevent fd duplication after execlp(). */
1000 ret
= fcntl(error_counter_fd
, F_SETFD
, FD_CLOEXEC
);
1002 PERROR("Failed to set FD_CLOEXEC flag on event notifier error counter file descriptor: error_counter_fd = %d",
1004 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1008 DBG("Created kernel event notifier group error counter: fd = %d", error_counter_fd
);
1010 kernel_error_accounting_entry
.error_counter_fd
= error_counter_fd
;
1011 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1017 static enum event_notifier_error_accounting_status
create_error_counter_index_for_token(
1018 struct error_accounting_state
*state
, uint64_t tracer_token
, uint64_t *error_counter_index
)
1020 struct index_ht_entry
*index_entry
;
1021 enum lttng_index_allocator_status index_alloc_status
;
1022 uint64_t local_error_counter_index
;
1023 enum event_notifier_error_accounting_status status
;
1025 LTTNG_ASSERT(state
);
1027 /* Allocate a new index for that counter. */
1028 index_alloc_status
=
1029 lttng_index_allocator_alloc(state
->index_allocator
, &local_error_counter_index
);
1030 switch (index_alloc_status
) {
1031 case LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY
:
1032 DBG("No indices left in the configured event notifier error counter: "
1033 "number-of-indices = %" PRIu64
,
1034 lttng_index_allocator_get_index_count(state
->index_allocator
));
1035 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
;
1037 case LTTNG_INDEX_ALLOCATOR_STATUS_OK
:
1040 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1044 index_entry
= zmalloc
<index_ht_entry
>();
1045 if (index_entry
== NULL
) {
1046 PERROR("Failed to allocate event notifier error counter hash table entry");
1047 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
1051 index_entry
->error_counter_index
= local_error_counter_index
;
1052 lttng_ht_node_init_u64(&index_entry
->node
, tracer_token
);
1053 lttng_ht_add_unique_u64(state
->indices_ht
, &index_entry
->node
);
1055 DBG("Allocated error counter index for tracer token: tracer token = %" PRIu64
1056 ", index = %" PRIu64
,
1058 local_error_counter_index
);
1059 *error_counter_index
= local_error_counter_index
;
1060 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1065 enum event_notifier_error_accounting_status
1066 event_notifier_error_accounting_register_event_notifier(const struct lttng_trigger
*trigger
,
1067 uint64_t *error_counter_index
)
1069 enum event_notifier_error_accounting_status status
;
1070 uint64_t local_error_counter_index
;
1071 struct error_accounting_state
*state
;
1073 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1074 case LTTNG_DOMAIN_KERNEL
:
1075 state
= &kernel_state
;
1077 case LTTNG_DOMAIN_UST
:
1078 case LTTNG_DOMAIN_PYTHON
:
1079 case LTTNG_DOMAIN_JUL
:
1080 case LTTNG_DOMAIN_LOG4J
:
1088 * Check if this event notifier already has a error counter index
1091 status
= get_error_counter_index_for_token(
1092 state
, lttng_trigger_get_tracer_token(trigger
), &local_error_counter_index
);
1094 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
1096 uid_t trigger_owner_uid
;
1097 const char *trigger_name
;
1099 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1101 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
,
1104 lttng_trigger_get_tracer_token(trigger
));
1106 status
= create_error_counter_index_for_token(
1107 state
, lttng_trigger_get_tracer_token(trigger
), &local_error_counter_index
);
1108 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1109 ERR("Error creating index for token: status = %s, trigger name = '%s', trigger owner uid = %d",
1110 error_accounting_status_str(status
),
1117 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
1118 *error_counter_index
= local_error_counter_index
;
1119 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1125 #ifdef HAVE_LIBLTTNG_UST_CTL
1126 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1127 case LTTNG_DOMAIN_UST
:
1128 case LTTNG_DOMAIN_PYTHON
:
1129 case LTTNG_DOMAIN_JUL
:
1130 case LTTNG_DOMAIN_LOG4J
:
1131 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1132 the_event_notifier_counter
.count
++;
1133 if (the_event_notifier_counter
.count
== 1) {
1135 * On the first event notifier, we get a reference to
1136 * every existing UID entries. This ensures that the
1137 * entries are kept around if there are still
1138 * registered event notifiers but no apps.
1140 get_ref_all_ust_error_accounting_entry();
1142 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1147 #endif /* HAVE_LIBLTTNG_UST_CTL */
1153 static enum event_notifier_error_accounting_status
1154 event_notifier_error_accounting_kernel_get_count(const struct lttng_trigger
*trigger
,
1157 struct lttng_kernel_abi_counter_aggregate counter_aggregate
= {};
1158 enum event_notifier_error_accounting_status status
;
1159 uint64_t error_counter_index
;
1162 status
= get_error_counter_index_for_token(
1163 &kernel_state
, lttng_trigger_get_tracer_token(trigger
), &error_counter_index
);
1164 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1165 ERR("Error getting index for token: status=%s",
1166 error_accounting_status_str(status
));
1170 counter_aggregate
.index
.number_dimensions
= 1;
1171 counter_aggregate
.index
.dimension_indexes
[0] = error_counter_index
;
1173 LTTNG_ASSERT(kernel_error_accounting_entry
.error_counter_fd
);
1175 ret
= kernctl_counter_get_aggregate_value(kernel_error_accounting_entry
.error_counter_fd
,
1176 &counter_aggregate
);
1177 if (ret
|| counter_aggregate
.value
.value
< 0) {
1178 uid_t trigger_owner_uid
;
1179 const char *trigger_name
;
1181 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1183 if (counter_aggregate
.value
.value
< 0) {
1184 ERR("Invalid negative event notifier error counter value: trigger owner = %d, trigger name = '%s', value = %" PRId64
,
1187 counter_aggregate
.value
.value
);
1189 ERR("Failed to getting event notifier error count: trigger owner = %d, trigger name = '%s', ret = %d",
1195 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1199 /* Error count can't be negative. */
1200 LTTNG_ASSERT(counter_aggregate
.value
.value
>= 0);
1201 *count
= (uint64_t) counter_aggregate
.value
.value
;
1203 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1209 enum event_notifier_error_accounting_status
1210 event_notifier_error_accounting_get_count(const struct lttng_trigger
*trigger
, uint64_t *count
)
1212 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1213 case LTTNG_DOMAIN_KERNEL
:
1214 return event_notifier_error_accounting_kernel_get_count(trigger
, count
);
1215 case LTTNG_DOMAIN_UST
:
1216 case LTTNG_DOMAIN_PYTHON
:
1217 case LTTNG_DOMAIN_JUL
:
1218 case LTTNG_DOMAIN_LOG4J
:
1219 #ifdef HAVE_LIBLTTNG_UST_CTL
1220 return event_notifier_error_accounting_ust_get_count(trigger
, count
);
1223 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1224 #endif /* HAVE_LIBLTTNG_UST_CTL */
1230 static enum event_notifier_error_accounting_status
1231 event_notifier_error_accounting_clear(const struct lttng_trigger
*trigger
)
1233 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1234 case LTTNG_DOMAIN_KERNEL
:
1235 return event_notifier_error_accounting_kernel_clear(trigger
);
1236 case LTTNG_DOMAIN_UST
:
1237 case LTTNG_DOMAIN_PYTHON
:
1238 case LTTNG_DOMAIN_JUL
:
1239 case LTTNG_DOMAIN_LOG4J
:
1240 #ifdef HAVE_LIBLTTNG_UST_CTL
1241 return event_notifier_error_accounting_ust_clear(trigger
);
1243 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1244 #endif /* HAVE_LIBLTTNG_UST_CTL */
1250 static void free_index_ht_entry(struct rcu_head
*head
)
1252 struct index_ht_entry
*entry
= caa_container_of(head
, struct index_ht_entry
, rcu_head
);
1257 void event_notifier_error_accounting_unregister_event_notifier(const struct lttng_trigger
*trigger
)
1259 struct lttng_ht_iter iter
;
1260 struct lttng_ht_node_u64
*node
;
1261 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
1262 enum event_notifier_error_accounting_status status
;
1263 struct error_accounting_state
*state
;
1265 status
= event_notifier_error_accounting_clear(trigger
);
1266 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1267 /* Trigger details already logged by callee on error. */
1268 ERR("Failed to clear event notifier error counter during unregistration of event notifier: status = '%s'",
1269 error_accounting_status_str(status
));
1275 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1276 case LTTNG_DOMAIN_KERNEL
:
1277 state
= &kernel_state
;
1279 #ifdef HAVE_LIBLTTNG_UST_CTL
1280 case LTTNG_DOMAIN_UST
:
1281 case LTTNG_DOMAIN_PYTHON
:
1282 case LTTNG_DOMAIN_JUL
:
1283 case LTTNG_DOMAIN_LOG4J
:
1286 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1287 the_event_notifier_counter
.count
--;
1288 if (the_event_notifier_counter
.count
== 0) {
1290 * When unregistering the last event notifier, put one
1291 * reference to every uid entries on the behalf of all
1294 put_ref_all_ust_error_accounting_entry();
1297 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1300 #endif /* HAVE_LIBLTTNG_UST_CTL */
1305 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
1306 node
= lttng_ht_iter_get_node_u64(&iter
);
1309 struct index_ht_entry
*index_entry
=
1310 lttng::utils::container_of(node
, &index_ht_entry::node
);
1311 enum lttng_index_allocator_status index_alloc_status
;
1313 index_alloc_status
= lttng_index_allocator_release(
1314 state
->index_allocator
, index_entry
->error_counter_index
);
1315 if (index_alloc_status
!= LTTNG_INDEX_ALLOCATOR_STATUS_OK
) {
1316 uid_t trigger_owner_uid
;
1317 const char *trigger_name
;
1319 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
1321 ERR("Failed to release event notifier error counter index: index = %" PRIu64
1322 ", trigger name = '%s', trigger owner uid = %d",
1323 index_entry
->error_counter_index
,
1325 (int) trigger_owner_uid
);
1326 /* Don't exit, perform the rest of the clean-up. */
1329 del_ret
= lttng_ht_del(state
->indices_ht
, &iter
);
1330 LTTNG_ASSERT(!del_ret
);
1331 call_rcu(&index_entry
->rcu_head
, free_index_ht_entry
);
1338 void event_notifier_error_accounting_fini(void)
1340 if (kernel_error_accounting_entry
.error_counter_fd
) {
1341 const int ret
= close(kernel_error_accounting_entry
.error_counter_fd
);
1344 PERROR("Failed to close kernel event notifier error counter");
1348 lttng_ht_destroy(error_counter_uid_ht
);
1350 fini_error_accounting_state(&kernel_state
);
1351 fini_error_accounting_state(&ust_state
);