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.h>
16 #include <common/hashtable/hashtable.h>
17 #include <common/index-allocator.h>
18 #include <common/kernel-ctl/kernel-ctl.h>
19 #include <common/shm.h>
20 #include <lttng/trigger/trigger-internal.h>
22 #include "event-notifier-error-accounting.h"
23 #include "lttng-ust-error.h"
26 #define ERROR_COUNTER_INDEX_HT_INITIAL_SIZE 16
28 struct index_ht_entry
{
29 struct lttng_ht_node_u64 node
;
30 uint64_t error_counter_index
;
31 struct rcu_head rcu_head
;
34 struct ust_error_accounting_entry
{
37 struct lttng_ht_node_u64 node
;
38 struct rcu_head rcu_head
;
39 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
41 * Those `lttng_ust_abi_object_data` are anonymous handles to the
43 * They are only used to be duplicated for each new applications of the
44 * user. To destroy them, call with the `sock` parameter set to -1.
45 * e.g. `lttng_ust_ctl_release_object(-1, data)`;
47 struct lttng_ust_abi_object_data
*counter
;
48 struct lttng_ust_abi_object_data
**cpu_counters
;
49 int nr_counter_cpu_fds
;
52 struct kernel_error_accounting_entry
{
56 static struct kernel_error_accounting_entry kernel_error_accounting_entry
;
58 /* Hashtable mapping uid to error_account_entry. */
59 static struct lttng_ht
*error_counter_uid_ht
;
61 struct error_accounting_state
{
62 struct lttng_index_allocator
*index_allocator
;
63 /* Hashtable mapping event notifier token to index_ht_entry. */
64 struct lttng_ht
*indices_ht
;
65 uint64_t number_indices
;
68 static struct error_accounting_state ust_state
;
69 static struct error_accounting_state kernel_state
;
71 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
72 const char **trigger_name
,
73 uid_t
*trigger_owner_uid
)
75 enum lttng_trigger_status trigger_status
;
77 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
78 switch (trigger_status
) {
79 case LTTNG_TRIGGER_STATUS_OK
:
81 case LTTNG_TRIGGER_STATUS_UNSET
:
82 *trigger_name
= "(anonymous)";
88 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
90 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
94 const char *error_accounting_status_str(
95 enum event_notifier_error_accounting_status status
)
98 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
100 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
:
102 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
104 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
:
106 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
:
107 return "NO_INDEX_AVAILABLE";
108 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
115 #ifdef HAVE_LIBLTTNG_UST_CTL
116 struct event_notifier_counter
{
117 pthread_mutex_t lock
;
121 static struct event_notifier_counter the_event_notifier_counter
;
123 static void free_ust_error_accounting_entry(struct rcu_head
*head
)
126 struct ust_error_accounting_entry
*entry
=
127 caa_container_of(head
, typeof(*entry
), rcu_head
);
129 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
130 lttng_ust_ctl_release_object(-1, entry
->cpu_counters
[i
]);
131 free(entry
->cpu_counters
[i
]);
134 free(entry
->cpu_counters
);
136 lttng_ust_ctl_release_object(-1, entry
->counter
);
137 free(entry
->counter
);
139 lttng_ust_ctl_destroy_counter(entry
->daemon_counter
);
145 bool ust_error_accounting_entry_get(struct ust_error_accounting_entry
*entry
)
147 return urcu_ref_get_unless_zero(&entry
->ref
);
151 void ust_error_accounting_entry_release(struct urcu_ref
*entry_ref
)
153 struct ust_error_accounting_entry
*entry
=
154 container_of(entry_ref
, typeof(*entry
), ref
);
157 cds_lfht_del(error_counter_uid_ht
->ht
, &entry
->node
.node
);
158 call_rcu(&entry
->rcu_head
, free_ust_error_accounting_entry
);
164 void ust_error_accounting_entry_put(struct ust_error_accounting_entry
*entry
)
170 urcu_ref_put(&entry
->ref
, ust_error_accounting_entry_release
);
174 * Put one reference to every UID entries.
177 void put_ref_all_ust_error_accounting_entry(void)
179 struct lttng_ht_iter iter
;
180 struct ust_error_accounting_entry
*uid_entry
;
182 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
185 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
186 uid_entry
, node
.node
) {
187 ust_error_accounting_entry_put(uid_entry
);
194 * Get one reference to every UID entries.
197 void get_ref_all_ust_error_accounting_entry(void)
199 struct lttng_ht_iter iter
;
200 struct ust_error_accounting_entry
*uid_entry
;
202 ASSERT_LOCKED(the_event_notifier_counter
.lock
);
205 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
206 uid_entry
, node
.node
) {
207 ust_error_accounting_entry_get(uid_entry
);
213 #endif /* HAVE_LIBLTTNG_UST_CTL */
216 enum event_notifier_error_accounting_status
217 init_error_accounting_state(struct error_accounting_state
*state
,
218 uint64_t index_count
)
220 enum event_notifier_error_accounting_status status
;
224 state
->number_indices
= index_count
;
226 state
->index_allocator
= lttng_index_allocator_create(index_count
);
227 if (!state
->index_allocator
) {
228 ERR("Failed to allocate event notifier error counter index allocator");
229 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
233 state
->indices_ht
= lttng_ht_new(ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
,
235 if (!state
->indices_ht
) {
236 ERR("Failed to allocate error counter indices hash table");
237 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
238 goto error_indices_ht
;
241 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
245 lttng_index_allocator_destroy(state
->index_allocator
);
246 state
->index_allocator
= NULL
;
252 void fini_error_accounting_state(struct error_accounting_state
*state
)
257 * Will assert if some error counter indices were not released (an
260 lttng_ht_destroy(state
->indices_ht
);
261 lttng_index_allocator_destroy(state
->index_allocator
);
264 enum event_notifier_error_accounting_status
265 event_notifier_error_accounting_init(uint64_t buffer_size_kernel
,
266 uint64_t buffer_size_ust
)
268 enum event_notifier_error_accounting_status status
;
270 status
= init_error_accounting_state(&kernel_state
, buffer_size_kernel
);
271 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
272 ERR("Failed to initialize kernel event notifier accounting state: status = %s",
273 error_accounting_status_str(status
));
277 status
= init_error_accounting_state(&ust_state
, buffer_size_ust
);
278 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
279 ERR("Failed to initialize UST event notifier accounting state: status = %s",
280 error_accounting_status_str(status
));
281 goto error_ust_state
;
284 error_counter_uid_ht
= lttng_ht_new(
285 ERROR_COUNTER_INDEX_HT_INITIAL_SIZE
, LTTNG_HT_TYPE_U64
);
286 if (!error_counter_uid_ht
) {
287 ERR("Failed to allocate UID to error counter accountant hash table");
288 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
292 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
296 fini_error_accounting_state(&ust_state
);
298 fini_error_accounting_state(&kernel_state
);
304 * Return the error counteur index associated to this event notifier tracer
305 * token. Returns _STATUS_OK if found and _STATUS_NOT_FOUND otherwise.
308 enum event_notifier_error_accounting_status
get_error_counter_index_for_token(
309 struct error_accounting_state
*state
, uint64_t tracer_token
,
310 uint64_t *error_counter_index
)
312 struct lttng_ht_node_u64
*node
;
313 struct lttng_ht_iter iter
;
314 const struct index_ht_entry
*index_entry
;
315 enum event_notifier_error_accounting_status status
;
318 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
319 node
= lttng_ht_iter_get_node_u64(&iter
);
321 index_entry
= caa_container_of(
322 node
, const struct index_ht_entry
, node
);
323 *error_counter_index
= index_entry
->error_counter_index
;
324 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
326 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
;
333 #ifdef HAVE_LIBLTTNG_UST_CTL
335 * Find the entry for this app's UID, the caller acquires a reference if the
339 struct ust_error_accounting_entry
*ust_error_accounting_entry_find(
340 struct lttng_ht
*uid_ht
, const struct ust_app
*app
)
342 struct ust_error_accounting_entry
*entry
;
343 struct lttng_ht_node_u64
*node
;
344 struct lttng_ht_iter iter
;
345 uint64_t key
= app
->uid
;
347 lttng_ht_lookup(uid_ht
, &key
, &iter
);
348 node
= lttng_ht_iter_get_node_u64(&iter
);
354 entry
= caa_container_of(node
,
355 struct ust_error_accounting_entry
, node
);
357 got_ref
= ust_error_accounting_entry_get(entry
);
367 * Create the entry for this app's UID, the caller acquires a reference to the
371 struct ust_error_accounting_entry
*ust_error_accounting_entry_create(
372 struct lttng_ht
*uid_ht
, const struct ust_app
*app
)
374 int i
, ret
, *cpu_counter_fds
= NULL
;
375 struct lttng_ust_ctl_daemon_counter
*daemon_counter
;
376 struct lttng_ust_abi_object_data
*counter
, **cpu_counters
;
377 struct ust_error_accounting_entry
*entry
= NULL
;
378 const struct lttng_ust_ctl_counter_dimension dimension
= {
379 .size
= ust_state
.number_indices
,
380 .has_underflow
= false,
381 .has_overflow
= false,
384 if (!ust_app_supports_counters(app
)) {
385 DBG("Refusing to create accounting entry for application (unsupported feature): app name = '%s', app ppid = %d",
386 app
->name
, (int) app
->ppid
);
390 entry
= zmalloc(sizeof(struct ust_error_accounting_entry
));
392 PERROR("Failed to allocate event notifier error acounting entry")
396 urcu_ref_init(&entry
->ref
);
397 entry
->uid
= app
->uid
;
398 entry
->nr_counter_cpu_fds
= lttng_ust_ctl_get_nr_cpu_per_counter();
400 cpu_counter_fds
= zmalloc(entry
->nr_counter_cpu_fds
* sizeof(*cpu_counter_fds
));
401 if (!cpu_counter_fds
) {
402 PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
403 (int) app
->uid
, app
->name
, (int) app
->pid
,
404 entry
->nr_counter_cpu_fds
* sizeof(*cpu_counter_fds
));
405 goto error_counter_cpu_fds_alloc
;
408 /* Initialize to an invalid fd value to closes fds in case of error. */
409 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
410 cpu_counter_fds
[i
] = -1;
413 cpu_counters
= zmalloc(entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
415 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",
416 (int) app
->uid
, app
->name
, (int) app
->pid
,
417 entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
418 goto error_counter_cpus_alloc
;
421 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
422 cpu_counter_fds
[i
] = shm_create_anonymous("event-notifier-error-accounting");
423 if (cpu_counter_fds
[i
] == -1) {
424 ERR("Failed to create event notifier error accounting shared memory for application user: application uid = %d, pid = %d, application name = '%s'",
425 (int) app
->uid
, (int) app
->pid
, app
->name
);
426 goto error_shm_alloc
;
431 * Ownership of the file descriptors transferred to the ustctl object.
433 daemon_counter
= lttng_ust_ctl_create_counter(1, &dimension
, 0, -1,
434 entry
->nr_counter_cpu_fds
, cpu_counter_fds
,
435 LTTNG_UST_CTL_COUNTER_BITNESS_32
,
436 LTTNG_UST_CTL_COUNTER_ARITHMETIC_MODULAR
,
437 LTTNG_UST_CTL_COUNTER_ALLOC_PER_CPU
,
439 if (!daemon_counter
) {
440 goto error_create_daemon_counter
;
443 ret
= lttng_ust_ctl_create_counter_data(daemon_counter
, &counter
);
445 ERR("Failed to create userspace tracer counter data for application user: uid = %d, pid = %d, application name = '%s'",
446 (int) app
->uid
, (int) app
->pid
, app
->name
);
447 goto error_create_counter_data
;
450 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
451 ret
= lttng_ust_ctl_create_counter_cpu_data(daemon_counter
, i
,
454 ERR("Failed to create userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
455 (int) app
->uid
, (int) app
->pid
,
457 goto error_create_counter_cpu_data
;
461 entry
->daemon_counter
= daemon_counter
;
462 entry
->counter
= counter
;
463 entry
->cpu_counters
= cpu_counters
;
465 lttng_ht_node_init_u64(&entry
->node
, entry
->uid
);
466 lttng_ht_add_unique_u64(error_counter_uid_ht
, &entry
->node
);
470 error_create_counter_cpu_data
:
471 /* Teardown any allocated cpu counters. */
472 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
473 if (!cpu_counters
[i
]) {
475 * Early-exit when error occurred before all cpu
476 * counters could be initialized.
481 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
482 free(cpu_counters
[i
]);
485 lttng_ust_ctl_release_object(-1, entry
->counter
);
486 free(entry
->counter
);
487 error_create_counter_data
:
488 lttng_ust_ctl_destroy_counter(daemon_counter
);
489 error_create_daemon_counter
:
491 /* Error occurred before per-cpu SHMs were handed-off to ustctl. */
492 if (cpu_counter_fds
) {
493 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
494 if (cpu_counter_fds
[i
] < 0) {
496 * Early-exit when error occurred before all cpu
497 * counter shm fds could be initialized.
502 ret
= close(cpu_counter_fds
[i
]);
504 PERROR("Failed to close error counter per-CPU shm file descriptor: fd = %d",
511 error_counter_cpus_alloc
:
512 error_counter_cpu_fds_alloc
:
517 free(cpu_counter_fds
);
522 enum event_notifier_error_accounting_status
send_counter_data_to_ust(
524 struct lttng_ust_abi_object_data
*new_counter
)
527 enum event_notifier_error_accounting_status status
;
529 /* Attach counter to trigger group. */
530 pthread_mutex_lock(&app
->sock_lock
);
531 ret
= lttng_ust_ctl_send_counter_data_to_ust(app
->sock
,
532 app
->event_notifier_group
.object
->handle
, new_counter
);
533 pthread_mutex_unlock(&app
->sock_lock
);
535 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
536 ERR("Failed to send counter data to application: application name = '%s', pid = %d, ret = %d",
537 app
->name
, app
->pid
, ret
);
538 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
540 DBG3("Failed to send counter data to application (application is dead): application name = '%s', pid = %d, ret = %d",
541 app
->name
, app
->pid
, ret
);
542 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
;
548 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
554 enum event_notifier_error_accounting_status
send_counter_cpu_data_to_ust(
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
,
564 counter
, counter_cpu
);
565 pthread_mutex_unlock(&app
->sock_lock
);
567 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
568 ERR("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
569 app
->name
, app
->pid
, ret
);
570 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
572 DBG3("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_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(error_counter_uid_ht
,
617 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
618 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
619 goto error_creating_entry
;
623 * We just created a new UID entry, If there are event
624 * notifiers already registered, take one reference on their
627 if (the_event_notifier_counter
.count
> 0) {
628 ust_error_accounting_entry_get(entry
);
631 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
634 /* Duplicate counter object data. */
635 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter
,
638 ERR("Failed to duplicate event notifier error accounting counter for application user: application uid = %d, pid = %d, application name = '%s'",
639 (int) app
->uid
, (int) app
->pid
, app
->name
);
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
),
652 (int) app
->uid
, (int) app
->pid
, app
->name
);
653 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
654 goto error_send_counter_data
;
657 cpu_counters
= zmalloc(entry
->nr_counter_cpu_fds
* sizeof(struct lttng_ust_abi_object_data
*));
659 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",
660 (int) app
->uid
, app
->name
, (int) app
->pid
,
661 entry
->nr_counter_cpu_fds
* sizeof(**cpu_counters
));
662 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
663 goto error_allocate_cpu_counters
;
666 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
667 struct lttng_ust_abi_object_data
*new_counter_cpu
= NULL
;
669 ret
= lttng_ust_ctl_duplicate_ust_object_data(&new_counter_cpu
,
670 entry
->cpu_counters
[i
]);
672 ERR("Failed to duplicate userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
673 (int) app
->uid
, (int) app
->pid
,
675 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
676 goto error_duplicate_cpu_counter
;
679 cpu_counters
[i
] = new_counter_cpu
;
681 status
= send_counter_cpu_data_to_ust(app
, new_counter
,
683 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
684 if (status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
) {
685 goto error_send_cpu_counter_data
;
688 ERR("Failed to send counter cpu data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
689 error_accounting_status_str(status
),
690 (int) app
->uid
, (int) app
->pid
,
692 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
693 goto error_send_cpu_counter_data
;
697 app
->event_notifier_group
.counter
= new_counter
;
699 app
->event_notifier_group
.nr_counter_cpu
= entry
->nr_counter_cpu_fds
;
700 app
->event_notifier_group
.counter_cpu
= cpu_counters
;
704 error_send_cpu_counter_data
:
705 error_duplicate_cpu_counter
:
706 /* Teardown any duplicated cpu counters. */
707 for (i
= 0; i
< entry
->nr_counter_cpu_fds
; i
++) {
708 if (!cpu_counters
[i
]) {
710 * Early-exit when error occurred before all cpu
711 * counters could be initialized.
716 lttng_ust_ctl_release_object(-1, cpu_counters
[i
]);
717 free(cpu_counters
[i
]);
722 error_allocate_cpu_counters
:
723 error_send_counter_data
:
724 lttng_ust_ctl_release_object(-1, new_counter
);
726 error_duplicate_counter
:
727 ust_error_accounting_entry_put(entry
);
728 error_creating_entry
:
729 app
->event_notifier_group
.counter
= NULL
;
736 enum event_notifier_error_accounting_status
737 event_notifier_error_accounting_unregister_app(struct ust_app
*app
)
739 enum event_notifier_error_accounting_status status
;
740 struct ust_error_accounting_entry
*entry
;
745 /* If an error occurred during app registration no entry was created. */
746 if (!app
->event_notifier_group
.counter
) {
747 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
751 entry
= ust_error_accounting_entry_find(error_counter_uid_ht
, app
);
753 ERR("Failed to find event notitifier error accounting entry on application teardown: pid = %d, application name = '%s'",
754 app
->pid
, app
->name
);
755 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
759 * Put the entry twice as we acquired a reference from the
760 * `ust_error_accounting_entry_find()` above.
762 ust_error_accounting_entry_put(entry
);
763 ust_error_accounting_entry_put(entry
);
766 for (i
= 0; i
< app
->event_notifier_group
.nr_counter_cpu
; i
++) {
767 lttng_ust_ctl_release_object(app
->sock
,
768 app
->event_notifier_group
.counter_cpu
[i
]);
769 free(app
->event_notifier_group
.counter_cpu
[i
]);
772 free(app
->event_notifier_group
.counter_cpu
);
774 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.counter
);
775 free(app
->event_notifier_group
.counter
);
777 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
784 enum event_notifier_error_accounting_status
785 event_notifier_error_accounting_ust_get_count(
786 const struct lttng_trigger
*trigger
, uint64_t *count
)
788 struct lttng_ht_iter iter
;
789 struct ust_error_accounting_entry
*uid_entry
;
790 uint64_t error_counter_index
, global_sum
= 0;
791 enum event_notifier_error_accounting_status status
;
792 size_t dimension_indexes
[1];
793 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
794 uid_t trigger_owner_uid
;
795 const char *trigger_name
;
800 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
802 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
,
803 &error_counter_index
);
804 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
806 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
", trigger name = '%s', trigger owner uid = %d, status = %s",
807 tracer_token
, trigger_name
,
808 (int) trigger_owner_uid
,
809 error_accounting_status_str(status
));
813 dimension_indexes
[0] = error_counter_index
;
816 * Iterate over all the UID entries.
817 * We aggregate the value of all uid entries regardless of if the uid
818 * matches the trigger's uid because a user that is allowed to register
819 * a trigger to a given sessiond is also allowed to create an event
820 * notifier on all apps that this sessiond is aware of.
822 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
823 uid_entry
, node
.node
) {
825 int64_t local_value
= 0;
826 bool overflow
= false, underflow
= false;
828 ret
= lttng_ust_ctl_counter_aggregate(uid_entry
->daemon_counter
,
829 dimension_indexes
, &local_value
, &overflow
,
831 if (ret
|| local_value
< 0) {
833 ERR("Failed to aggregate event notifier error counter values of trigger: trigger name = '%s', trigger owner uid = %d",
835 (int) trigger_owner_uid
);
836 } else if (local_value
< 0) {
837 ERR("Negative event notifier error counter value encountered during aggregation: trigger name = '%s', trigger owner uid = %d, value = %" PRId64
,
839 (int) trigger_owner_uid
,
845 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
849 /* Cast is safe as negative values are checked-for above. */
850 global_sum
+= (uint64_t) local_value
;
854 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
862 enum event_notifier_error_accounting_status
event_notifier_error_accounting_ust_clear(
863 const struct lttng_trigger
*trigger
)
865 struct lttng_ht_iter iter
;
866 struct ust_error_accounting_entry
*uid_entry
;
867 uint64_t error_counter_index
;
868 enum event_notifier_error_accounting_status status
;
869 size_t dimension_index
;
870 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
873 status
= get_error_counter_index_for_token(&ust_state
, tracer_token
,
874 &error_counter_index
);
875 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
876 uid_t trigger_owner_uid
;
877 const char *trigger_name
;
879 get_trigger_info_for_log(trigger
, &trigger_name
,
882 ERR("Failed to retrieve index for tracer token: token = %" PRIu64
", trigger name = '%s', trigger owner uid = %d, status = %s",
883 tracer_token
, trigger_name
,
884 (int) trigger_owner_uid
,
885 error_accounting_status_str(status
));
889 dimension_index
= error_counter_index
;
892 * Go over all error counters (ignoring uid) as a trigger (and trigger
893 * errors) can be generated from any applications that this session
894 * daemon is managing.
896 cds_lfht_for_each_entry(error_counter_uid_ht
->ht
, &iter
.iter
,
897 uid_entry
, node
.node
) {
898 const int ret
= lttng_ust_ctl_counter_clear(uid_entry
->daemon_counter
,
902 uid_t trigger_owner_uid
;
903 const char *trigger_name
;
905 get_trigger_info_for_log(trigger
, &trigger_name
,
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
, trigger_name
,
909 (int) trigger_owner_uid
);
910 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
915 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
920 #endif /* HAVE_LIBLTTNG_UST_CTL */
923 enum event_notifier_error_accounting_status
924 event_notifier_error_accounting_kernel_clear(
925 const struct lttng_trigger
*trigger
)
928 uint64_t error_counter_index
;
929 enum event_notifier_error_accounting_status status
;
930 struct lttng_kernel_abi_counter_clear counter_clear
= {};
932 status
= get_error_counter_index_for_token(&kernel_state
,
933 lttng_trigger_get_tracer_token(trigger
),
934 &error_counter_index
);
935 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
936 uid_t trigger_owner_uid
;
937 const char *trigger_name
;
939 get_trigger_info_for_log(
940 trigger
, &trigger_name
, &trigger_owner_uid
);
942 ERR("Failed to get event notifier error counter index: trigger owner uid = %d, trigger name = '%s', status = '%s'",
943 trigger_owner_uid
, trigger_name
,
944 error_accounting_status_str(status
));
948 counter_clear
.index
.number_dimensions
= 1;
949 counter_clear
.index
.dimension_indexes
[0] = error_counter_index
;
951 ret
= kernctl_counter_clear(
952 kernel_error_accounting_entry
.error_counter_fd
,
955 uid_t trigger_owner_uid
;
956 const char *trigger_name
;
958 get_trigger_info_for_log(
959 trigger
, &trigger_name
, &trigger_owner_uid
);
961 ERR("Failed to clear kernel event notifier error counter: trigger owner uid = %d, trigger name = '%s'",
962 trigger_owner_uid
, trigger_name
);
963 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
967 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
972 enum event_notifier_error_accounting_status
973 event_notifier_error_accounting_register_kernel(
974 int kernel_event_notifier_group_fd
)
976 int error_counter_fd
= -1, ret
;
977 enum event_notifier_error_accounting_status status
;
978 const struct lttng_kernel_abi_counter_conf error_counter_conf
= {
979 .arithmetic
= LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR
,
980 .bitness
= sizeof(void *) == sizeof(uint32_t) ?
981 LTTNG_KERNEL_ABI_COUNTER_BITNESS_32
:
982 LTTNG_KERNEL_ABI_COUNTER_BITNESS_64
,
983 .global_sum_step
= 0,
984 .number_dimensions
= 1,
985 .dimensions
[0].size
= kernel_state
.number_indices
,
986 .dimensions
[0].has_underflow
= false,
987 .dimensions
[0].has_overflow
= false,
990 ret
= kernctl_create_event_notifier_group_error_counter(
991 kernel_event_notifier_group_fd
, &error_counter_conf
);
993 PERROR("Failed to create event notifier group error counter through kernel ioctl: kernel_event_notifier_group_fd = %d",
994 kernel_event_notifier_group_fd
);
995 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
999 error_counter_fd
= ret
;
1001 /* Prevent fd duplication after execlp(). */
1002 ret
= fcntl(error_counter_fd
, F_SETFD
, FD_CLOEXEC
);
1004 PERROR("Failed to set FD_CLOEXEC flag on event notifier error counter file descriptor: error_counter_fd = %d",
1006 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1010 DBG("Created kernel event notifier group error counter: fd = %d",
1013 kernel_error_accounting_entry
.error_counter_fd
=
1015 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1022 enum event_notifier_error_accounting_status
create_error_counter_index_for_token(
1023 struct error_accounting_state
*state
, uint64_t tracer_token
,
1024 uint64_t *error_counter_index
)
1026 struct index_ht_entry
*index_entry
;
1027 enum lttng_index_allocator_status index_alloc_status
;
1028 uint64_t local_error_counter_index
;
1029 enum event_notifier_error_accounting_status status
;
1031 LTTNG_ASSERT(state
);
1033 /* Allocate a new index for that counter. */
1034 index_alloc_status
= lttng_index_allocator_alloc(state
->index_allocator
,
1035 &local_error_counter_index
);
1036 switch (index_alloc_status
) {
1037 case LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY
:
1038 DBG("No indices left in the configured event notifier error counter: "
1039 "number-of-indices = %"PRIu64
,
1040 lttng_index_allocator_get_index_count(
1041 state
->index_allocator
));
1042 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
;
1044 case LTTNG_INDEX_ALLOCATOR_STATUS_OK
:
1047 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1051 index_entry
= zmalloc(sizeof(*index_entry
));
1052 if (index_entry
== NULL
) {
1053 PERROR("Failed to allocate event notifier error counter hash table entry");
1054 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM
;
1058 index_entry
->error_counter_index
= local_error_counter_index
;
1059 lttng_ht_node_init_u64(&index_entry
->node
, tracer_token
);
1060 lttng_ht_add_unique_u64(state
->indices_ht
, &index_entry
->node
);
1062 DBG("Allocated error counter index for tracer token: tracer token = %" PRIu64
", index = %" PRIu64
,
1063 tracer_token
, local_error_counter_index
);
1064 *error_counter_index
= local_error_counter_index
;
1065 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1070 enum event_notifier_error_accounting_status
1071 event_notifier_error_accounting_register_event_notifier(
1072 const struct lttng_trigger
*trigger
,
1073 uint64_t *error_counter_index
)
1075 enum event_notifier_error_accounting_status status
;
1076 uint64_t local_error_counter_index
;
1077 struct error_accounting_state
*state
;
1079 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1080 case LTTNG_DOMAIN_KERNEL
:
1081 state
= &kernel_state
;
1083 case LTTNG_DOMAIN_UST
:
1084 case LTTNG_DOMAIN_PYTHON
:
1085 case LTTNG_DOMAIN_JUL
:
1086 case LTTNG_DOMAIN_LOG4J
:
1094 * Check if this event notifier already has a error counter index
1097 status
= get_error_counter_index_for_token(state
,
1098 lttng_trigger_get_tracer_token(trigger
),
1099 &local_error_counter_index
);
1101 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOT_FOUND
:
1103 uid_t trigger_owner_uid
;
1104 const char *trigger_name
;
1106 get_trigger_info_for_log(
1107 trigger
, &trigger_name
, &trigger_owner_uid
);
1109 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
,
1110 trigger_name
, trigger_owner_uid
,
1111 lttng_trigger_get_tracer_token(trigger
));
1113 status
= create_error_counter_index_for_token(state
,
1114 lttng_trigger_get_tracer_token(trigger
),
1115 &local_error_counter_index
);
1116 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1117 ERR("Error creating index for token: status = %s, trigger name = '%s', trigger owner uid = %d",
1118 error_accounting_status_str(status
),
1119 trigger_name
, trigger_owner_uid
);
1124 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
1125 *error_counter_index
= local_error_counter_index
;
1126 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1132 #ifdef HAVE_LIBLTTNG_UST_CTL
1133 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1134 case LTTNG_DOMAIN_UST
:
1135 case LTTNG_DOMAIN_PYTHON
:
1136 case LTTNG_DOMAIN_JUL
:
1137 case LTTNG_DOMAIN_LOG4J
:
1138 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1139 the_event_notifier_counter
.count
++;
1140 if (the_event_notifier_counter
.count
== 1) {
1142 * On the first event notifier, we get a reference to
1143 * every existing UID entries. This ensures that the
1144 * entries are kept around if there are still
1145 * registered event notifiers but no apps.
1147 get_ref_all_ust_error_accounting_entry();
1149 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1154 #endif /* HAVE_LIBLTTNG_UST_CTL */
1162 enum event_notifier_error_accounting_status
1163 event_notifier_error_accounting_kernel_get_count(
1164 const struct lttng_trigger
*trigger
, uint64_t *count
)
1166 struct lttng_kernel_abi_counter_aggregate counter_aggregate
= {};
1167 enum event_notifier_error_accounting_status status
;
1168 uint64_t error_counter_index
;
1171 status
= get_error_counter_index_for_token(&kernel_state
,
1172 lttng_trigger_get_tracer_token(trigger
),
1173 &error_counter_index
);
1174 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1175 ERR("Error getting index for token: status=%s",
1176 error_accounting_status_str(status
));
1180 counter_aggregate
.index
.number_dimensions
= 1;
1181 counter_aggregate
.index
.dimension_indexes
[0] = error_counter_index
;
1183 LTTNG_ASSERT(kernel_error_accounting_entry
.error_counter_fd
);
1185 ret
= kernctl_counter_get_aggregate_value(
1186 kernel_error_accounting_entry
.error_counter_fd
,
1187 &counter_aggregate
);
1188 if (ret
|| counter_aggregate
.value
.value
< 0) {
1189 uid_t trigger_owner_uid
;
1190 const char *trigger_name
;
1192 get_trigger_info_for_log(trigger
, &trigger_name
,
1193 &trigger_owner_uid
);
1195 if (counter_aggregate
.value
.value
< 0) {
1196 ERR("Invalid negative event notifier error counter value: trigger owner = %d, trigger name = '%s', value = %" PRId64
,
1197 trigger_owner_uid
, trigger_name
,
1198 counter_aggregate
.value
.value
);
1200 ERR("Failed to getting event notifier error count: trigger owner = %d, trigger name = '%s', ret = %d",
1201 trigger_owner_uid
, trigger_name
, ret
);
1204 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR
;
1208 /* Error count can't be negative. */
1209 LTTNG_ASSERT(counter_aggregate
.value
.value
>= 0);
1210 *count
= (uint64_t) counter_aggregate
.value
.value
;
1212 status
= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1218 enum event_notifier_error_accounting_status
1219 event_notifier_error_accounting_get_count(
1220 const struct lttng_trigger
*trigger
, uint64_t *count
)
1222 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1223 case LTTNG_DOMAIN_KERNEL
:
1224 return event_notifier_error_accounting_kernel_get_count(
1226 case LTTNG_DOMAIN_UST
:
1227 case LTTNG_DOMAIN_PYTHON
:
1228 case LTTNG_DOMAIN_JUL
:
1229 case LTTNG_DOMAIN_LOG4J
:
1230 #ifdef HAVE_LIBLTTNG_UST_CTL
1231 return event_notifier_error_accounting_ust_get_count(trigger
,
1235 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1236 #endif /* HAVE_LIBLTTNG_UST_CTL */
1243 enum event_notifier_error_accounting_status
1244 event_notifier_error_accounting_clear(const struct lttng_trigger
*trigger
)
1246 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1247 case LTTNG_DOMAIN_KERNEL
:
1248 return event_notifier_error_accounting_kernel_clear(trigger
);
1249 case LTTNG_DOMAIN_UST
:
1250 case LTTNG_DOMAIN_PYTHON
:
1251 case LTTNG_DOMAIN_JUL
:
1252 case LTTNG_DOMAIN_LOG4J
:
1253 #ifdef HAVE_LIBLTTNG_UST_CTL
1254 return event_notifier_error_accounting_ust_clear(trigger
);
1256 return EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
;
1257 #endif /* HAVE_LIBLTTNG_UST_CTL */
1263 static void free_index_ht_entry(struct rcu_head
*head
)
1265 struct index_ht_entry
*entry
= caa_container_of(head
,
1266 struct index_ht_entry
, rcu_head
);
1271 void event_notifier_error_accounting_unregister_event_notifier(
1272 const struct lttng_trigger
*trigger
)
1274 struct lttng_ht_iter iter
;
1275 struct lttng_ht_node_u64
*node
;
1276 const uint64_t tracer_token
= lttng_trigger_get_tracer_token(trigger
);
1277 enum event_notifier_error_accounting_status status
;
1278 struct error_accounting_state
*state
;
1280 status
= event_notifier_error_accounting_clear(trigger
);
1281 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1282 /* Trigger details already logged by callee on error. */
1283 ERR("Failed to clear event notifier error counter during unregistration of event notifier: status = '%s'",
1284 error_accounting_status_str(status
));
1290 switch (lttng_trigger_get_underlying_domain_type_restriction(trigger
)) {
1291 case LTTNG_DOMAIN_KERNEL
:
1292 state
= &kernel_state
;
1294 #ifdef HAVE_LIBLTTNG_UST_CTL
1295 case LTTNG_DOMAIN_UST
:
1296 case LTTNG_DOMAIN_PYTHON
:
1297 case LTTNG_DOMAIN_JUL
:
1298 case LTTNG_DOMAIN_LOG4J
:
1301 pthread_mutex_lock(&the_event_notifier_counter
.lock
);
1302 the_event_notifier_counter
.count
--;
1303 if (the_event_notifier_counter
.count
== 0) {
1306 * When unregistering the last event notifier, put one
1307 * reference to every uid entries on the behalf of all
1310 put_ref_all_ust_error_accounting_entry();
1313 pthread_mutex_unlock(&the_event_notifier_counter
.lock
);
1316 #endif /* HAVE_LIBLTTNG_UST_CTL */
1321 lttng_ht_lookup(state
->indices_ht
, &tracer_token
, &iter
);
1322 node
= lttng_ht_iter_get_node_u64(&iter
);
1325 struct index_ht_entry
*index_entry
= caa_container_of(
1326 node
, typeof(*index_entry
), node
);
1327 enum lttng_index_allocator_status index_alloc_status
;
1329 index_alloc_status
= lttng_index_allocator_release(
1330 state
->index_allocator
,
1331 index_entry
->error_counter_index
);
1332 if (index_alloc_status
!= LTTNG_INDEX_ALLOCATOR_STATUS_OK
) {
1333 uid_t trigger_owner_uid
;
1334 const char *trigger_name
;
1336 get_trigger_info_for_log(trigger
, &trigger_name
,
1337 &trigger_owner_uid
);
1339 ERR("Failed to release event notifier error counter index: index = %" PRIu64
", trigger name = '%s', trigger owner uid = %d",
1340 index_entry
->error_counter_index
,
1341 trigger_name
, (int) trigger_owner_uid
);
1342 /* Don't exit, perform the rest of the clean-up. */
1345 del_ret
= lttng_ht_del(state
->indices_ht
, &iter
);
1346 LTTNG_ASSERT(!del_ret
);
1347 call_rcu(&index_entry
->rcu_head
, free_index_ht_entry
);
1354 void event_notifier_error_accounting_fini(void)
1356 if (kernel_error_accounting_entry
.error_counter_fd
) {
1357 const int ret
= close(kernel_error_accounting_entry
.error_counter_fd
);
1360 PERROR("Failed to close kernel event notifier error counter");
1364 lttng_ht_destroy(error_counter_uid_ht
);
1366 fini_error_accounting_state(&kernel_state
);
1367 fini_error_accounting_state(&ust_state
);