2 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <urcu/compiler.h>
15 #include <common/error.hpp>
16 #include <common/hashtable/hashtable.hpp>
17 #include <common/index-allocator.hpp>
18 #include <common/kernel-ctl/kernel-ctl.hpp>
19 #include <common/shm.hpp>
20 #include <lttng/trigger/trigger-internal.hpp>
22 #include "event-notifier-error-accounting.hpp"
23 #include "lttng-ust-error.hpp"
24 #include "ust-app.hpp"
26 #define ERROR_COUNTER_INDEX_HT_INITIAL_SIZE 16
29 struct index_ht_entry
{
30 struct lttng_ht_node_u64 node
;
31 uint64_t error_counter_index
;
32 struct rcu_head rcu_head
;
35 struct ust_error_accounting_entry
{
38 struct lttng_ht_node_u64 node
;
39 struct rcu_head rcu_head
;
40 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
42 * Those `lttng_ust_abi_object_data` are anonymous handles to the
44 * They are only used to be duplicated for each new applications of the
45 * user. To destroy them, call with the `sock` parameter set to -1.
46 * e.g. `lttng_ust_ctl_release_object(-1, data)`;
48 struct lttng_ust_abi_object_data
*counter
;
49 struct lttng_ust_abi_object_data
**cpu_counters
;
50 int nr_counter_cpu_fds
;
53 struct kernel_error_accounting_entry
{
57 struct kernel_error_accounting_entry kernel_error_accounting_entry
;
59 /* Hashtable mapping uid to error_account_entry. */
60 struct lttng_ht
*error_counter_uid_ht
;
62 struct error_accounting_state
{
63 struct lttng_index_allocator
*index_allocator
;
64 /* Hashtable mapping event notifier token to index_ht_entry. */
65 struct lttng_ht
*indices_ht
;
66 uint64_t number_indices
;
69 struct error_accounting_state ust_state
;
70 struct error_accounting_state kernel_state
;
73 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
74 const char **trigger_name
,
75 uid_t
*trigger_owner_uid
)
77 enum lttng_trigger_status trigger_status
;
79 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
80 switch (trigger_status
) {
81 case LTTNG_TRIGGER_STATUS_OK
:
83 case LTTNG_TRIGGER_STATUS_UNSET
:
84 *trigger_name
= "(anonymous)";
90 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
92 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
96 const char *error_accounting_status_str(
97 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 caa_container_of(head
, typeof(*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
);
149 bool ust_error_accounting_entry_get(struct ust_error_accounting_entry
*entry
)
151 return urcu_ref_get_unless_zero(&entry
->ref
);
155 void ust_error_accounting_entry_release(struct urcu_ref
*entry_ref
)
157 struct ust_error_accounting_entry
*entry
=
158 container_of(entry_ref
, typeof(*entry
), ref
);
161 cds_lfht_del(error_counter_uid_ht
->ht
, &entry
->node
.node
);
162 call_rcu(&entry
->rcu_head
, free_ust_error_accounting_entry
);
168 void ust_error_accounting_entry_put(struct ust_error_accounting_entry
*entry
)
174 urcu_ref_put(&entry
->ref
, ust_error_accounting_entry_release
);
178 * Put one reference to every UID entries.
181 void put_ref_all_ust_error_accounting_entry(void)
183 struct lttng_ht_iter iter
;
184 struct ust_error_accounting_entry
*uid_entry
;
186 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
189 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
190 uid_entry
, node
.node
) {
191 ust_error_accounting_entry_put(uid_entry
);
198 * Get one reference to every UID entries.
201 void get_ref_all_ust_error_accounting_entry(void)
203 struct lttng_ht_iter iter
;
204 struct ust_error_accounting_entry
*uid_entry
;
206 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
209 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
210 uid_entry
, node
.node
) {
211 ust_error_accounting_entry_get(uid_entry
);
217 #endif /* HAVE_LIBLTTNG_UST_CTL */
220 enum event_notifier_error_accounting_status
221 init_error_accounting_state(struct error_accounting_state
*state
,
222 uint64_t index_count
)
224 enum event_notifier_error_accounting_status status
;
228 state
->number_indices
= index_count
;
230 state
->index_allocator
= lttng_index_allocator_create(index_count
);
231 if (!state
->index_allocator
) {
232 ERR("Failed to allocate event notifier error counter index allocator");
233 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
237 state
->indices_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
,
239 if (!state
->indices_ht
) {
240 ERR("Failed to allocate error counter indices hash table");
241 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
242 goto error_indices_ht
;
245 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
249 lttng_index_allocator_destroy(state
->index_allocator
);
250 state
->index_allocator
= NULL
;
256 void fini_error_accounting_state(struct error_accounting_state
*state
)
261 * Will assert if some error counter indices were not released (an
264 lttng_ht_destroy(state
->indices_ht
);
265 lttng_index_allocator_destroy(state
->index_allocator
);
268 enum event_notifier_error_accounting_status
269 event_notifier_error_accounting_init(uint64_t buffer_size_kernel
,
270 uint64_t buffer_size_ust
)
272 enum event_notifier_error_accounting_status status
;
274 status
= init_error_accounting_state(&kernel_state
, buffer_size_kernel
);
275 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
276 ERR("Failed to initialize kernel event notifier accounting state: status = %s",
277 error_accounting_status_str(status
));
281 status
= init_error_accounting_state(&ust_state
, buffer_size_ust
);
282 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
283 ERR("Failed to initialize UST event notifier accounting state: status = %s",
284 error_accounting_status_str(status
));
285 goto error_ust_state
;
288 error_counter_uid_ht
= lttng_ht_new(
289 ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
290 if (!error_counter_uid_ht
) {
291 ERR("Failed to allocate UID to error counter accountant hash table");
292 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
296 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
300 fini_error_accounting_state(&ust_state
);
302 fini_error_accounting_state(&kernel_state
);
308 * Return the error counteur index associated to this event notifier tracer
309 * token. Returns _STATUS_OK if found and _STATUS_NOT_FOUND otherwise.
312 enum event_notifier_error_accounting_status
get_error_counter_index_for_token(
313 struct error_accounting_state
*state
, uint64_t tracer_token
,
314 uint64_t *error_counter_index
)
316 struct lttng_ht_node_u64
*node
;
317 struct lttng_ht_iter iter
;
318 const struct index_ht_entry
*index_entry
;
319 enum event_notifier_error_accounting_status status
;
322 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
323 node
= lttng_ht_iter_get_node_u64(&iter
);
325 index_entry
= caa_container_of(
326 node
, const struct index_ht_entry
, node
);
327 *error_counter_index
= index_entry
->error_counter_index
;
328 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
330 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
;
337 #ifdef HAVE_LIBLTTNG_UST_CTL
339 * Find the entry for this app's UID, the caller acquires a reference if the
343 struct ust_error_accounting_entry
*ust_error_accounting_entry_find(
344 struct lttng_ht
*uid_ht
, const struct ust_app
*app
)
346 struct ust_error_accounting_entry
*entry
;
347 struct lttng_ht_node_u64
*node
;
348 struct lttng_ht_iter iter
;
349 uint64_t key
= app
->uid
;
351 lttng_ht_lookup(uid_ht
, &key
, &iter
);
352 node
= lttng_ht_iter_get_node_u64(&iter
);
358 entry
= caa_container_of(node
,
359 struct ust_error_accounting_entry
, node
);
361 got_ref
= ust_error_accounting_entry_get(entry
);
371 * Create the entry for this app's UID, the caller acquires a reference to the
375 struct ust_error_accounting_entry
*ust_error_accounting_entry_create(
376 const struct ust_app
*app
)
378 int i
, ret
, *cpu_counter_fds
= NULL
;
379 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
380 struct lttng_ust_abi_object_data
*counter
, **cpu_counters
;
381 struct ust_error_accounting_entry
*entry
= NULL
;
382 lttng_ust_ctl_counter_dimension dimension
;
384 dimension
.size
= ust_state
.number_indices
;
385 dimension
.has_underflow
= false;
386 dimension
.has_overflow
= false;
388 if (!ust_app_supports_counters(app
)) {
389 DBG("Refusing to create accounting entry for application (unsupported feature): app name = '%s', app ppid = %d",
390 app
->name
, (int) app
->ppid
);
394 entry
= zmalloc
<ust_error_accounting_entry
>();
396 PERROR("Failed to allocate event notifier error acounting entry")
400 urcu_ref_init(&entry
->ref
);
401 entry
->uid
= app
->uid
;
402 entry
->nr_counter_cpu_fds
= lttng_ust_ctl_get_nr_cpu_per_counter();
404 cpu_counter_fds
= calloc
<int>(entry
->nr_counter_cpu_fds
);
405 if (!cpu_counter_fds
) {
406 PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
407 (int) app
->uid
, app
->name
, (int) app
->pid
,
408 entry
->nr_counter_cpu_fds
* sizeof(*cpu_counter_fds
));
409 goto error_counter_cpu_fds_alloc
;
412 /* Initialize to an invalid fd value to closes fds in case of error. */
413 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
414 cpu_counter_fds
[i
] = -1;
417 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
419 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",
420 (int) app
->uid
, app
->name
, (int) app
->pid
,
421 entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
422 goto error_counter_cpus_alloc
;
425 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
426 cpu_counter_fds
[i
] = shm_create_anonymous("event-notifier-error-accounting");
427 if (cpu_counter_fds
[i
] == -1) {
428 ERR("Failed to create event notifier error accounting shared memory for application user: application uid = %d, pid = %d, application name = '%s'",
429 (int) app
->uid
, (int) app
->pid
, app
->name
);
430 goto error_shm_alloc
;
435 * Ownership of the file descriptors transferred to the ustctl object.
437 daemon_counter
= lttng_ust_ctl_create_counter(1, &dimension
, 0, -1,
438 entry
->nr_counter_cpu_fds
, cpu_counter_fds
,
439 LTTNG_UST_CTL_COUNTER_BITNESS_32
,
440 LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
,
441 LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
,
443 if (!daemon_counter
) {
444 goto error_create_daemon_counter
;
447 ret
= lttng_ust_ctl_create_counter_data(daemon_counter
, &counter
);
449 ERR("Failed to create userspace tracer counter data for application user: uid = %d, pid = %d, application name = '%s'",
450 (int) app
->uid
, (int) app
->pid
, app
->name
);
451 goto error_create_counter_data
;
454 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
455 ret
= lttng_ust_ctl_create_counter_cpu_data(daemon_counter
, i
,
458 ERR("Failed to create userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
459 (int) app
->uid
, (int) app
->pid
,
461 goto error_create_counter_cpu_data
;
465 entry
->daemon_counter
= daemon_counter
;
466 entry
->counter
= counter
;
467 entry
->cpu_counters
= cpu_counters
;
469 lttng_ht_node_init_u64(&entry
->node
, entry
->uid
);
470 lttng_ht_add_unique_u64(error_counter_uid_ht
, &entry
->node
);
474 error_create_counter_cpu_data
:
475 /* Teardown any allocated cpu counters. */
476 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
477 if (!cpu_counters
[i
]) {
479 * Early-exit when error occurred before all cpu
480 * counters could be initialized.
485 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
486 free(cpu_counters
[i
]);
489 lttng_ust_ctl_release_object(-1, entry
->counter
);
490 free(entry
->counter
);
491 error_create_counter_data
:
492 lttng_ust_ctl_destroy_counter(daemon_counter
);
493 error_create_daemon_counter
:
495 /* Error occurred before per-cpu SHMs were handed-off to ustctl. */
496 if (cpu_counter_fds
) {
497 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
498 if (cpu_counter_fds
[i
] < 0) {
500 * Early-exit when error occurred before all cpu
501 * counter shm fds could be initialized.
506 ret
= close(cpu_counter_fds
[i
]);
508 PERROR("Failed to close error counter per-CPU shm file descriptor: fd = %d",
515 error_counter_cpus_alloc
:
516 error_counter_cpu_fds_alloc
:
521 free(cpu_counter_fds
);
526 enum event_notifier_error_accounting_status
send_counter_data_to_ust(
528 struct lttng_ust_abi_object_data
*new_counter
)
531 enum event_notifier_error_accounting_status status
;
533 /* Attach counter to trigger group. */
534 pthread_mutex_lock(&app
->sock_lock
);
535 ret
= lttng_ust_ctl_send_counter_data_to_ust(app
->sock
,
536 app
->event_notifier_group
.object
->handle
, new_counter
);
537 pthread_mutex_unlock(&app
->sock_lock
);
539 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
540 ERR("Failed to send counter data to application: application name = '%s', pid = %d, ret = %d",
541 app
->name
, app
->pid
, ret
);
542 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
544 DBG3("Failed to send counter data to application (application is dead): application name = '%s', pid = %d, ret = %d",
545 app
->name
, app
->pid
, ret
);
546 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
552 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
558 enum event_notifier_error_accounting_status
send_counter_cpu_data_to_ust(
560 struct lttng_ust_abi_object_data
*counter
,
561 struct lttng_ust_abi_object_data
*counter_cpu
)
564 enum event_notifier_error_accounting_status status
;
566 pthread_mutex_lock(&app
->sock_lock
);
567 ret
= lttng_ust_ctl_send_counter_cpu_data_to_ust(app
->sock
,
568 counter
, counter_cpu
);
569 pthread_mutex_unlock(&app
->sock_lock
);
571 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
572 ERR("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
573 app
->name
, app
->pid
, ret
);
574 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
576 DBG3("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
577 app
->name
, app
->pid
, ret
);
578 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
584 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
589 enum event_notifier_error_accounting_status
590 event_notifier_error_accounting_register_app(struct ust_app
*app
)
594 struct lttng_ust_abi_object_data
*new_counter
;
595 struct ust_error_accounting_entry
*entry
;
596 enum event_notifier_error_accounting_status status
;
597 struct lttng_ust_abi_object_data
**cpu_counters
;
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.
609 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
612 * Take the event notifier counter lock before creating the new
613 * entry to ensure that no event notifier is registered between
614 * the the entry creation and event notifier count check.
616 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
618 entry
= ust_error_accounting_entry_create(app
);
620 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
621 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
622 goto error_creating_entry
;
626 * We just created a new UID entry, If there are event
627 * notifiers already registered, take one reference on their
630 if (the_event_notifier_counter
.count
> 0) {
631 ust_error_accounting_entry_get(entry
);
634 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
637 /* Duplicate counter object data. */
638 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter
,
641 ERR("Failed to duplicate event notifier error accounting counter for application user: application uid = %d, pid = %d, application name = '%s'",
642 (int) app
->uid
, (int) app
->pid
, app
->name
);
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
),
655 (int) app
->uid
, (int) app
->pid
, app
->name
);
656 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
657 goto error_send_counter_data
;
660 cpu_counters
= calloc
<lttng_ust_abi_object_data
*>(entry
->nr_counter_cpu_fds
);
662 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",
663 (int) app
->uid
, app
->name
, (int) app
->pid
,
664 entry
->nr_counter_cpu_fds
* sizeof(**cpu_counters
));
665 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
666 goto error_allocate_cpu_counters
;
669 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
670 struct lttng_ust_abi_object_data
*new_counter_cpu
= NULL
;
672 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter_cpu
,
673 entry
->cpu_counters
[i
]);
675 ERR("Failed to duplicate userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
676 (int) app
->uid
, (int) app
->pid
,
678 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
679 goto error_duplicate_cpu_counter
;
682 cpu_counters
[i
] = new_counter_cpu
;
684 status
= send_counter_cpu_data_to_ust(app
, new_counter
,
686 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
687 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
688 goto error_send_cpu_counter_data
;
691 ERR("Failed to send counter cpu data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
692 error_accounting_status_str(status
),
693 (int) app
->uid
, (int) app
->pid
,
695 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
696 goto error_send_cpu_counter_data
;
700 app
->event_notifier_group
.counter
= new_counter
;
702 app
->event_notifier_group
.nr_counter_cpu
= entry
->nr_counter_cpu_fds
;
703 app
->event_notifier_group
.counter_cpu
= cpu_counters
;
707 error_send_cpu_counter_data
:
708 error_duplicate_cpu_counter
:
709 /* Teardown any duplicated cpu counters. */
710 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
711 if (!cpu_counters
[i
]) {
713 * Early-exit when error occurred before all cpu
714 * counters could be initialized.
719 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
720 free(cpu_counters
[i
]);
725 error_allocate_cpu_counters
:
726 error_send_counter_data
:
727 lttng_ust_ctl_release_object(-1, new_counter
);
729 error_duplicate_counter
:
730 ust_error_accounting_entry_put(entry
);
731 error_creating_entry
:
732 app
->event_notifier_group
.counter
= NULL
;
739 enum event_notifier_error_accounting_status
740 event_notifier_error_accounting_unregister_app(struct ust_app
*app
)
742 enum event_notifier_error_accounting_status status
;
743 struct ust_error_accounting_entry
*entry
;
748 /* If an error occurred during app registration no entry was created. */
749 if (!app
->event_notifier_group
.counter
) {
750 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
754 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
756 ERR("Failed to find event notitifier error accounting entry on application teardown: pid = %d, application name = '%s'",
757 app
->pid
, app
->name
);
758 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
762 * Put the entry twice as we acquired a reference from the
763 * `ust_error_accounting_entry_find()` above.
765 ust_error_accounting_entry_put(entry
);
766 ust_error_accounting_entry_put(entry
);
769 for (i
= 0; i
< app
->event_notifier_group
.nr_counter_cpu
; i
++) {
770 lttng_ust_ctl_release_object(app
->sock
,
771 app
->event_notifier_group
.counter_cpu
[i
]);
772 free(app
->event_notifier_group
.counter_cpu
[i
]);
775 free(app
->event_notifier_group
.counter_cpu
);
777 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter
);
778 free(app
->event_notifier_group
.counter
);
780 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
787 enum event_notifier_error_accounting_status
788 event_notifier_error_accounting_ust_get_count(
789 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
;
803 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
805 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
,
806 &error_counter_index
);
807 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
809 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
", trigger name = '%s', trigger owner uid = %d, status = %s",
810 tracer_token
, trigger_name
,
811 (int) trigger_owner_uid
,
812 error_accounting_status_str(status
));
816 dimension_indexes
[0] = error_counter_index
;
819 * Iterate over all the UID entries.
820 * We aggregate the value of all uid entries regardless of if the uid
821 * matches the trigger's uid because a user that is allowed to register
822 * a trigger to a given sessiond is also allowed to create an event
823 * notifier on all apps that this sessiond is aware of.
825 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
826 uid_entry
, node
.node
) {
828 int64_t local_value
= 0;
829 bool overflow
= false, underflow
= false;
831 ret
= lttng_ust_ctl_counter_aggregate(uid_entry
->daemon_counter
,
832 dimension_indexes
, &local_value
, &overflow
,
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
;
865 enum event_notifier_error_accounting_status
event_notifier_error_accounting_ust_clear(
866 const struct lttng_trigger
*trigger
)
868 struct lttng_ht_iter iter
;
869 struct ust_error_accounting_entry
*uid_entry
;
870 uint64_t error_counter_index
;
871 enum event_notifier_error_accounting_status status
;
872 size_t dimension_index
;
873 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
876 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
,
877 &error_counter_index
);
878 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
879 uid_t trigger_owner_uid
;
880 const char *trigger_name
;
882 get_trigger_info_for_log(trigger
, &trigger_name
,
885 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
", trigger name = '%s', trigger owner uid = %d, status = %s",
886 tracer_token
, trigger_name
,
887 (int) trigger_owner_uid
,
888 error_accounting_status_str(status
));
892 dimension_index
= error_counter_index
;
895 * Go over all error counters (ignoring uid) as a trigger (and trigger
896 * errors) can be generated from any applications that this session
897 * daemon is managing.
899 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
900 uid_entry
, node
.node
) {
901 const int ret
= lttng_ust_ctl_counter_clear(uid_entry
->daemon_counter
,
905 uid_t trigger_owner_uid
;
906 const char *trigger_name
;
908 get_trigger_info_for_log(trigger
, &trigger_name
,
910 ERR("Failed to clear event notifier counter value for trigger: counter uid = %d, trigger name = '%s', trigger owner uid = %d",
911 (int) uid_entry
->node
.key
, trigger_name
,
912 (int) trigger_owner_uid
);
913 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
918 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
923 #endif /* HAVE_LIBLTTNG_UST_CTL */
926 enum event_notifier_error_accounting_status
927 event_notifier_error_accounting_kernel_clear(
928 const struct lttng_trigger
*trigger
)
931 uint64_t error_counter_index
;
932 enum event_notifier_error_accounting_status status
;
933 struct lttng_kernel_abi_counter_clear counter_clear
= {};
935 status
= get_error_counter_index_for_token(&kernel_state
,
936 lttng_trigger_get_tracer_token(trigger
),
937 &error_counter_index
);
938 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
939 uid_t trigger_owner_uid
;
940 const char *trigger_name
;
942 get_trigger_info_for_log(
943 trigger
, &trigger_name
, &trigger_owner_uid
);
945 ERR("Failed to get event notifier error counter index: trigger owner uid = %d, trigger name = '%s', status = '%s'",
946 trigger_owner_uid
, trigger_name
,
947 error_accounting_status_str(status
));
951 counter_clear
.index
.number_dimensions
= 1;
952 counter_clear
.index
.dimension_indexes
[0] = error_counter_index
;
954 ret
= kernctl_counter_clear(
955 kernel_error_accounting_entry
.error_counter_fd
,
958 uid_t trigger_owner_uid
;
959 const char *trigger_name
;
961 get_trigger_info_for_log(
962 trigger
, &trigger_name
, &trigger_owner_uid
);
964 ERR("Failed to clear kernel event notifier error counter: trigger owner uid = %d, trigger name = '%s'",
965 trigger_owner_uid
, trigger_name
);
966 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
970 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
975 enum event_notifier_error_accounting_status
976 event_notifier_error_accounting_register_kernel(
977 int kernel_event_notifier_group_fd
)
979 int error_counter_fd
= -1, ret
;
980 enum event_notifier_error_accounting_status status
;
981 lttng_kernel_abi_counter_conf error_counter_conf
= {
982 .arithmetic
= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
,
983 .bitness
= sizeof(void *) == sizeof(uint32_t) ?
984 LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
985 LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
,
986 .number_dimensions
= 1,
987 .global_sum_step
= 0,
992 error_counter_conf
.dimensions
[0].size
= kernel_state
.number_indices
;
993 error_counter_conf
.dimensions
[0].has_underflow
= false;
994 error_counter_conf
.dimensions
[0].has_overflow
= false;
996 ret
= kernctl_create_event_notifier_group_error_counter(
997 kernel_event_notifier_group_fd
, &error_counter_conf
);
999 PERROR("Failed to create event notifier group error counter through kernel ioctl: kernel_event_notifier_group_fd = %d",
1000 kernel_event_notifier_group_fd
);
1001 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1005 error_counter_fd
= ret
;
1007 /* Prevent fd duplication after execlp(). */
1008 ret
= fcntl(error_counter_fd
, F_SETFD
, FD_CLOEXEC
);
1010 PERROR("Failed to set FD_CLOEXEC flag on event notifier error counter file descriptor: error_counter_fd = %d",
1012 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1016 DBG("Created kernel event notifier group error counter: fd = %d",
1019 kernel_error_accounting_entry
.error_counter_fd
=
1021 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1028 enum event_notifier_error_accounting_status
create_error_counter_index_for_token(
1029 struct error_accounting_state
*state
, uint64_t tracer_token
,
1030 uint64_t *error_counter_index
)
1032 struct index_ht_entry
*index_entry
;
1033 enum lttng_index_allocator_status index_alloc_status
;
1034 uint64_t local_error_counter_index
;
1035 enum event_notifier_error_accounting_status status
;
1037 LTTNG_ASSERT(state
);
1039 /* Allocate a new index for that counter. */
1040 index_alloc_status
= lttng_index_allocator_alloc(state
->index_allocator
,
1041 &local_error_counter_index
);
1042 switch (index_alloc_status
) {
1043 case LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY
:
1044 DBG("No indices left in the configured event notifier error counter: "
1045 "number-of-indices = %" PRIu64
,
1046 lttng_index_allocator_get_index_count(
1047 state
->index_allocator
));
1048 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
;
1050 case LTTNG_INDEX_ALLOCATOR_STATUS_OK
:
1053 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1057 index_entry
= zmalloc
<index_ht_entry
>();
1058 if (index_entry
== NULL
) {
1059 PERROR("Failed to allocate event notifier error counter hash table entry");
1060 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
1064 index_entry
->error_counter_index
= local_error_counter_index
;
1065 lttng_ht_node_init_u64(&index_entry
->node
, tracer_token
);
1066 lttng_ht_add_unique_u64(state
->indices_ht
, &index_entry
->node
);
1068 DBG("Allocated error counter index for tracer token: tracer token = %" PRIu64
", index = %" PRIu64
,
1069 tracer_token
, local_error_counter_index
);
1070 *error_counter_index
= local_error_counter_index
;
1071 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1076 enum event_notifier_error_accounting_status
1077 event_notifier_error_accounting_register_event_notifier(
1078 const struct lttng_trigger
*trigger
,
1079 uint64_t *error_counter_index
)
1081 enum event_notifier_error_accounting_status status
;
1082 uint64_t local_error_counter_index
;
1083 struct error_accounting_state
*state
;
1085 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1086 case LTTNG_DOMAIN_KERNEL
:
1087 state
= &kernel_state
;
1089 case LTTNG_DOMAIN_UST
:
1090 case LTTNG_DOMAIN_PYTHON
:
1091 case LTTNG_DOMAIN_JUL
:
1092 case LTTNG_DOMAIN_LOG4J
:
1100 * Check if this event notifier already has a error counter index
1103 status
= get_error_counter_index_for_token(state
,
1104 lttng_trigger_get_tracer_token(trigger
),
1105 &local_error_counter_index
);
1107 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
1109 uid_t trigger_owner_uid
;
1110 const char *trigger_name
;
1112 get_trigger_info_for_log(
1113 trigger
, &trigger_name
, &trigger_owner_uid
);
1115 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
,
1116 trigger_name
, trigger_owner_uid
,
1117 lttng_trigger_get_tracer_token(trigger
));
1119 status
= create_error_counter_index_for_token(state
,
1120 lttng_trigger_get_tracer_token(trigger
),
1121 &local_error_counter_index
);
1122 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1123 ERR("Error creating index for token: status = %s, trigger name = '%s', trigger owner uid = %d",
1124 error_accounting_status_str(status
),
1125 trigger_name
, trigger_owner_uid
);
1130 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
1131 *error_counter_index
= local_error_counter_index
;
1132 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1138 #ifdef HAVE_LIBLTTNG_UST_CTL
1139 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1140 case LTTNG_DOMAIN_UST
:
1141 case LTTNG_DOMAIN_PYTHON
:
1142 case LTTNG_DOMAIN_JUL
:
1143 case LTTNG_DOMAIN_LOG4J
:
1144 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1145 the_event_notifier_counter
.count
++;
1146 if (the_event_notifier_counter
.count
== 1) {
1148 * On the first event notifier, we get a reference to
1149 * every existing UID entries. This ensures that the
1150 * entries are kept around if there are still
1151 * registered event notifiers but no apps.
1153 get_ref_all_ust_error_accounting_entry();
1155 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1160 #endif /* HAVE_LIBLTTNG_UST_CTL */
1168 enum event_notifier_error_accounting_status
1169 event_notifier_error_accounting_kernel_get_count(
1170 const struct lttng_trigger
*trigger
, uint64_t *count
)
1172 struct lttng_kernel_abi_counter_aggregate counter_aggregate
= {};
1173 enum event_notifier_error_accounting_status status
;
1174 uint64_t error_counter_index
;
1177 status
= get_error_counter_index_for_token(&kernel_state
,
1178 lttng_trigger_get_tracer_token(trigger
),
1179 &error_counter_index
);
1180 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1181 ERR("Error getting index for token: status=%s",
1182 error_accounting_status_str(status
));
1186 counter_aggregate
.index
.number_dimensions
= 1;
1187 counter_aggregate
.index
.dimension_indexes
[0] = error_counter_index
;
1189 LTTNG_ASSERT(kernel_error_accounting_entry
.error_counter_fd
);
1191 ret
= kernctl_counter_get_aggregate_value(
1192 kernel_error_accounting_entry
.error_counter_fd
,
1193 &counter_aggregate
);
1194 if (ret
|| counter_aggregate
.value
.value
< 0) {
1195 uid_t trigger_owner_uid
;
1196 const char *trigger_name
;
1198 get_trigger_info_for_log(trigger
, &trigger_name
,
1199 &trigger_owner_uid
);
1201 if (counter_aggregate
.value
.value
< 0) {
1202 ERR("Invalid negative event notifier error counter value: trigger owner = %d, trigger name = '%s', value = %" PRId64
,
1203 trigger_owner_uid
, trigger_name
,
1204 counter_aggregate
.value
.value
);
1206 ERR("Failed to getting event notifier error count: trigger owner = %d, trigger name = '%s', ret = %d",
1207 trigger_owner_uid
, trigger_name
, ret
);
1210 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1214 /* Error count can't be negative. */
1215 LTTNG_ASSERT(counter_aggregate
.value
.value
>= 0);
1216 *count
= (uint64_t) counter_aggregate
.value
.value
;
1218 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1224 enum event_notifier_error_accounting_status
1225 event_notifier_error_accounting_get_count(
1226 const struct lttng_trigger
*trigger
, uint64_t *count
)
1228 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1229 case LTTNG_DOMAIN_KERNEL
:
1230 return event_notifier_error_accounting_kernel_get_count(
1232 case LTTNG_DOMAIN_UST
:
1233 case LTTNG_DOMAIN_PYTHON
:
1234 case LTTNG_DOMAIN_JUL
:
1235 case LTTNG_DOMAIN_LOG4J
:
1236 #ifdef HAVE_LIBLTTNG_UST_CTL
1237 return event_notifier_error_accounting_ust_get_count(trigger
,
1241 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1242 #endif /* HAVE_LIBLTTNG_UST_CTL */
1249 enum event_notifier_error_accounting_status
1250 event_notifier_error_accounting_clear(const struct lttng_trigger
*trigger
)
1252 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1253 case LTTNG_DOMAIN_KERNEL
:
1254 return event_notifier_error_accounting_kernel_clear(trigger
);
1255 case LTTNG_DOMAIN_UST
:
1256 case LTTNG_DOMAIN_PYTHON
:
1257 case LTTNG_DOMAIN_JUL
:
1258 case LTTNG_DOMAIN_LOG4J
:
1259 #ifdef HAVE_LIBLTTNG_UST_CTL
1260 return event_notifier_error_accounting_ust_clear(trigger
);
1262 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1263 #endif /* HAVE_LIBLTTNG_UST_CTL */
1269 static void free_index_ht_entry(struct rcu_head
*head
)
1271 struct index_ht_entry
*entry
= caa_container_of(head
,
1272 struct index_ht_entry
, rcu_head
);
1277 void event_notifier_error_accounting_unregister_event_notifier(
1278 const struct lttng_trigger
*trigger
)
1280 struct lttng_ht_iter iter
;
1281 struct lttng_ht_node_u64
*node
;
1282 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
1283 enum event_notifier_error_accounting_status status
;
1284 struct error_accounting_state
*state
;
1286 status
= event_notifier_error_accounting_clear(trigger
);
1287 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1288 /* Trigger details already logged by callee on error. */
1289 ERR("Failed to clear event notifier error counter during unregistration of event notifier: status = '%s'",
1290 error_accounting_status_str(status
));
1296 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1297 case LTTNG_DOMAIN_KERNEL
:
1298 state
= &kernel_state
;
1300 #ifdef HAVE_LIBLTTNG_UST_CTL
1301 case LTTNG_DOMAIN_UST
:
1302 case LTTNG_DOMAIN_PYTHON
:
1303 case LTTNG_DOMAIN_JUL
:
1304 case LTTNG_DOMAIN_LOG4J
:
1307 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1308 the_event_notifier_counter
.count
--;
1309 if (the_event_notifier_counter
.count
== 0) {
1312 * When unregistering the last event notifier, put one
1313 * reference to every uid entries on the behalf of all
1316 put_ref_all_ust_error_accounting_entry();
1319 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1322 #endif /* HAVE_LIBLTTNG_UST_CTL */
1327 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
1328 node
= lttng_ht_iter_get_node_u64(&iter
);
1331 struct index_ht_entry
*index_entry
= caa_container_of(
1332 node
, typeof(*index_entry
), node
);
1333 enum lttng_index_allocator_status index_alloc_status
;
1335 index_alloc_status
= lttng_index_allocator_release(
1336 state
->index_allocator
,
1337 index_entry
->error_counter_index
);
1338 if (index_alloc_status
!= LTTNG_INDEX_ALLOCATOR_STATUS_OK
) {
1339 uid_t trigger_owner_uid
;
1340 const char *trigger_name
;
1342 get_trigger_info_for_log(trigger
, &trigger_name
,
1343 &trigger_owner_uid
);
1345 ERR("Failed to release event notifier error counter index: index = %" PRIu64
", trigger name = '%s', trigger owner uid = %d",
1346 index_entry
->error_counter_index
,
1347 trigger_name
, (int) trigger_owner_uid
);
1348 /* Don't exit, perform the rest of the clean-up. */
1351 del_ret
= lttng_ht_del(state
->indices_ht
, &iter
);
1352 LTTNG_ASSERT(!del_ret
);
1353 call_rcu(&index_entry
->rcu_head
, free_index_ht_entry
);
1360 void event_notifier_error_accounting_fini(void)
1362 if (kernel_error_accounting_entry
.error_counter_fd
) {
1363 const int ret
= close(kernel_error_accounting_entry
.error_counter_fd
);
1366 PERROR("Failed to close kernel event notifier error counter");
1370 lttng_ht_destroy(error_counter_uid_ht
);
1372 fini_error_accounting_state(&kernel_state
);
1373 fini_error_accounting_state(&ust_state
);