+#ifdef HAVE_LIBLTTNG_UST_CTL
+static
+struct error_account_entry *get_uid_accounting_entry(const struct ust_app *app)
+{
+ struct error_account_entry *entry;
+ struct lttng_ht_node_u64 *node;
+ struct lttng_ht_iter iter;
+ uint64_t key = app->uid;
+
+ lttng_ht_lookup(error_counter_uid_ht, &key, &iter);
+ node = lttng_ht_iter_get_node_u64(&iter);
+ if(node == NULL) {
+ entry = NULL;
+ } else {
+ entry = caa_container_of(node, struct error_account_entry, node);
+ }
+
+ return entry;
+}
+
+static
+struct error_account_entry *create_uid_accounting_entry(
+ const struct ust_app *app)
+{
+ int i, ret;
+ struct ustctl_daemon_counter *daemon_counter;
+ struct lttng_ust_abi_object_data *counter, **cpu_counters;
+ int *cpu_counter_fds = NULL;
+ struct error_account_entry *entry = NULL;
+ const struct ustctl_counter_dimension dimension = {
+ .size = error_counter_size,
+ .has_underflow = false,
+ .has_overflow = false,
+ };
+
+ entry = zmalloc(sizeof(struct error_account_entry));
+ if (!entry) {
+ PERROR("Failed to allocate event notifier error acounting entry")
+ goto error;
+ }
+
+ entry->nr_counter_cpu_fds = ustctl_get_nr_cpu_per_counter();
+ cpu_counter_fds = zmalloc(entry->nr_counter_cpu_fds * sizeof(*cpu_counter_fds));
+ if (!cpu_counter_fds) {
+ PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu",
+ (int) app->uid, app->name, (int) app->pid,
+ entry->nr_counter_cpu_fds * sizeof(*cpu_counter_fds));
+ ret = -1;
+ goto error_counter_cpu_fds_alloc;
+ }
+
+ /* Initialize to an invalid fd value to closes fds in case of error. */
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ cpu_counter_fds[i] = -1;
+ }
+
+ cpu_counters = zmalloc(entry->nr_counter_cpu_fds * sizeof(**cpu_counters));
+ if (!cpu_counters) {
+ 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",
+ (int) app->uid, app->name, (int) app->pid,
+ entry->nr_counter_cpu_fds * sizeof(**cpu_counters));
+ ret = -1;
+ goto error_counter_cpus_alloc;
+ }
+
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ cpu_counter_fds[i] = shm_create_anonymous("event-notifier-error-accounting");
+ if (cpu_counter_fds[i] == -1) {
+ ERR("Failed to create event notifier error accounting shared memory for application user: application uid = %d, pid = %d, application name = '%s'",
+ (int) app->uid, (int) app->pid, app->name);
+ goto error_shm_alloc;
+ }
+ }
+
+ /*
+ * Ownership of the file descriptors transferred to the ustctl object.
+ */
+ daemon_counter = ustctl_create_counter(1, &dimension, 0, -1,
+ entry->nr_counter_cpu_fds, cpu_counter_fds,
+ USTCTL_COUNTER_BITNESS_32,
+ USTCTL_COUNTER_ARITHMETIC_MODULAR,
+ USTCTL_COUNTER_ALLOC_PER_CPU,
+ false);
+ if (!daemon_counter) {
+ goto error_create_daemon_counter;
+ }
+
+ ret = ustctl_create_counter_data(daemon_counter, &counter);
+ if (ret) {
+ ERR("Failed to create userspace tracer counter data for application user: uid = %d, pid = %d, application name = '%s'",
+ (int) app->uid, (int) app->pid, app->name);
+ goto error_create_counter_data;
+ }
+
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ ret = ustctl_create_counter_cpu_data(daemon_counter, i,
+ &cpu_counters[i]);
+ if (ret) {
+ ERR("Failed to create userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
+ (int) app->uid, (int) app->pid,
+ app->name);
+ goto error_create_counter_cpu_data;
+ }
+ }
+
+ entry->daemon_counter = daemon_counter;
+ entry->counter = counter;
+ entry->cpu_counters = cpu_counters;
+
+ lttng_ht_node_init_u64(&entry->node, app->uid);
+ lttng_ht_add_unique_u64(error_counter_uid_ht, &entry->node);
+
+ goto end;
+
+error_create_counter_cpu_data:
+ /* Teardown any allocated cpu counters. */
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ if (!cpu_counters[i]) {
+ /*
+ * Early-exit when error occurred before all cpu
+ * counters could be initialized.
+ */
+ break;
+ }
+
+ ustctl_release_object(-1, cpu_counters[i]);
+ free(cpu_counters[i]);
+ }
+
+ ustctl_release_object(-1, entry->counter);
+ free(entry->counter);
+error_create_counter_data:
+ ustctl_destroy_counter(daemon_counter);
+error_create_daemon_counter:
+error_shm_alloc:
+ /* Error occured before per-cpu SHMs were handed-off to ustctl. */
+ if (cpu_counter_fds) {
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ if (cpu_counter_fds[i] < 0) {
+ /*
+ * Early-exit when error occurred before all cpu
+ * counter shm fds could be initialized.
+ */
+ break;
+ }
+
+ ret = close(cpu_counter_fds[i]);
+ if (ret) {
+ PERROR("Failed to close error counter per-CPU shm file descriptor: fd = %d", cpu_counter_fds[i]);
+ }
+ }
+ }
+
+ free(cpu_counters);
+error_counter_cpus_alloc:
+error_counter_cpu_fds_alloc:
+ free(entry);
+error:
+ entry = NULL;
+end:
+ free(cpu_counter_fds);
+ return entry;
+}
+
+static
+enum event_notifier_error_accounting_status send_counter_data_to_ust(
+ struct ust_app *app,
+ struct lttng_ust_abi_object_data *new_counter)
+{
+ int ret;
+ enum event_notifier_error_accounting_status status;
+
+ /* Attach counter to trigger group. */
+ pthread_mutex_lock(&app->sock_lock);
+ ret = ustctl_send_counter_data_to_ust(app->sock,
+ app->event_notifier_group.object->handle, new_counter);
+ pthread_mutex_unlock(&app->sock_lock);
+ if (ret < 0) {
+ if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
+ ERR("Failed to send counter data to application: application name = '%s', pid = %d, ret = %d",
+ app->name, app->pid, ret);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ } else {
+ DBG3("Failed to send counter data to application (application is dead): application name = '%s', pid = %d, ret = %d",
+ app->name, app->pid, ret);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD;
+ }
+
+ goto end;
+ }
+
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK;
+end:
+ return status;
+}
+
+static
+enum event_notifier_error_accounting_status send_counter_cpu_data_to_ust(
+ struct ust_app *app,
+ struct lttng_ust_abi_object_data *counter,
+ struct lttng_ust_abi_object_data *counter_cpu)
+{
+ int ret;
+ enum event_notifier_error_accounting_status status;
+
+ pthread_mutex_lock(&app->sock_lock);
+ ret = ustctl_send_counter_cpu_data_to_ust(app->sock,
+ counter, counter_cpu);
+ pthread_mutex_unlock(&app->sock_lock);
+ if (ret < 0) {
+ if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
+ ERR("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
+ app->name, app->pid, ret);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ } else {
+ DBG3("Failed to send counter CPU data to application: application name = '%s', pid = %d, ret = %d",
+ app->name, app->pid, ret);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD;
+ }
+
+ goto end;
+ }
+
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK;
+end:
+ return status;
+}
+
+enum event_notifier_error_accounting_status
+event_notifier_error_accounting_register_app(struct ust_app *app)
+{
+ int ret;
+ uint64_t i;
+ struct lttng_ust_abi_object_data *new_counter;
+ struct error_account_entry *entry;
+ enum event_notifier_error_accounting_status status;
+ struct lttng_ust_abi_object_data **cpu_counters;
+
+ /*
+ * Check if we already have a error counter for the user id of this
+ * app. If not, create one.
+ */
+ rcu_read_lock();
+ entry = get_uid_accounting_entry(app);
+ if (entry == NULL) {
+ entry = create_uid_accounting_entry(app);
+ if (!entry) {
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto end;
+ }
+ }
+
+ /* Duplicate counter object data. */
+ ret = ustctl_duplicate_ust_object_data(&new_counter,
+ entry->counter);
+ if (ret) {
+ ERR("Failed to duplicate event notifier error accounting counter for application user: application uid = %d, pid = %d, application name = '%s'",
+ (int) app->uid, (int) app->pid, app->name);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto end;
+ }
+
+ status = send_counter_data_to_ust(app, new_counter);
+ if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ ERR("Failed to send counter data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
+ error_accounting_status_str(status),
+ (int) app->uid, (int) app->pid, app->name);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto error_send_counter_data;
+ }
+
+ cpu_counters = zmalloc(entry->nr_counter_cpu_fds * sizeof(struct lttng_ust_abi_object_data));
+ if (!cpu_counters) {
+ 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",
+ (int) app->uid, app->name, (int) app->pid,
+ entry->nr_counter_cpu_fds * sizeof(**cpu_counters));
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM;
+ goto error_allocate_cpu_counters;
+ }
+
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ struct lttng_ust_abi_object_data *new_counter_cpu = NULL;
+
+ ret = ustctl_duplicate_ust_object_data(&new_counter_cpu,
+ entry->cpu_counters[i]);
+ if (ret) {
+ ERR("Failed to duplicate userspace tracer counter cpu data for application user: uid = %d, pid = %d, application name = '%s'",
+ (int) app->uid, (int) app->pid,
+ app->name);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM;
+ goto error_duplicate_cpu_counter;
+ }
+
+ cpu_counters[i] = new_counter_cpu;
+
+ status = send_counter_cpu_data_to_ust(app, new_counter,
+ new_counter_cpu);
+ if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ ERR("Failed to send counter cpu data to application tracer: status = %s, application uid = %d, pid = %d, application name = '%s'",
+ error_accounting_status_str(status),
+ (int) app->uid, (int) app->pid,
+ app->name);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto error_send_cpu_counter_data;
+ }
+ }
+
+ app->event_notifier_group.counter = new_counter;
+ new_counter = NULL;
+ app->event_notifier_group.nr_counter_cpu = entry->nr_counter_cpu_fds;
+ app->event_notifier_group.counter_cpu = cpu_counters;
+ cpu_counters = NULL;
+ goto end;
+
+error_allocate_cpu_counters:
+error_send_cpu_counter_data:
+error_duplicate_cpu_counter:
+ /* Teardown any duplicated cpu counters. */
+ for (i = 0; i < entry->nr_counter_cpu_fds; i++) {
+ if (!cpu_counters[i]) {
+ /*
+ * Early-exit when error occurred before all cpu
+ * counters could be initialized.
+ */
+ break;
+ }
+
+ ustctl_release_object(-1, cpu_counters[i]);
+ free(cpu_counters[i]);
+ }
+
+error_send_counter_data:
+ ustctl_release_object(-1, new_counter);
+ free(new_counter);
+end:
+ rcu_read_unlock();
+ return status;
+}
+
+enum event_notifier_error_accounting_status
+event_notifier_error_accounting_unregister_app(struct ust_app *app)
+{
+ enum event_notifier_error_accounting_status status;
+ struct error_account_entry *entry;
+ int i;
+
+ rcu_read_lock();
+ entry = get_uid_accounting_entry(app);
+ if (entry == NULL) {
+ ERR("Failed to find event notitifier error accounting entry on application teardown: pid = %d, application name = '%s'",
+ app->pid, app->name);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto end;
+ }
+
+ for (i = 0; i < app->event_notifier_group.nr_counter_cpu; i++) {
+ ustctl_release_object(app->sock,
+ app->event_notifier_group.counter_cpu[i]);
+ free(app->event_notifier_group.counter_cpu[i]);
+ }
+
+ free(app->event_notifier_group.counter_cpu);
+
+ ustctl_release_object(app->sock, app->event_notifier_group.counter);
+ free(app->event_notifier_group.counter);
+
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK;
+end:
+ rcu_read_unlock();
+ return status;
+}
+
+static
+enum event_notifier_error_accounting_status
+event_notifier_error_accounting_ust_get_count(
+ const struct lttng_trigger *trigger, uint64_t *count)
+{
+ struct lttng_ht_iter iter;
+ struct error_account_entry *uid_entry;
+ uint64_t error_counter_index, global_sum = 0;
+ enum event_notifier_error_accounting_status status;
+ size_t dimension_indexes[1];
+ const uint64_t tracer_token = lttng_trigger_get_tracer_token(trigger);
+
+ /*
+ * Go over all error counters (ignoring uid) as a trigger (and trigger
+ * errors) can be generated from any applications that this session
+ * daemon is managing.
+ */
+
+ rcu_read_lock();
+
+ status = get_error_counter_index_for_token(
+ tracer_token,
+ &error_counter_index);
+ if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ uid_t trigger_owner_uid;
+ const char *trigger_name;
+
+ get_trigger_info_for_log(trigger, &trigger_name,
+ &trigger_owner_uid);
+
+ ERR("Failed to retrieve index for tracer token: token = %" PRIu64 ", trigger name = '%s', trigger owner uid = %d, status = %s",
+ tracer_token, trigger_name,
+ (int) trigger_owner_uid,
+ error_accounting_status_str(status));
+ goto end;
+ }
+
+ dimension_indexes[0] = error_counter_index;
+
+ cds_lfht_for_each_entry(error_counter_uid_ht->ht, &iter.iter,
+ uid_entry, node.node) {
+ int ret;
+ int64_t local_value = 0;
+ bool overflow = false, underflow = false;
+
+ ret = ustctl_counter_aggregate(uid_entry->daemon_counter,
+ dimension_indexes, &local_value, &overflow,
+ &underflow);
+ if (ret || local_value < 0) {
+ uid_t trigger_owner_uid;
+ const char *trigger_name;
+
+ get_trigger_info_for_log(trigger, &trigger_name,
+ &trigger_owner_uid);
+
+ if (ret) {
+ ERR("Failed to aggregate event notifier error counter values of trigger: trigger name = '%s', trigger owner uid = %d",
+ trigger_name,
+ (int) trigger_owner_uid);
+ } else if (local_value < 0) {
+ ERR("Negative event notifier error counter value encountered during aggregation: trigger name = '%s', trigger owner uid = %d, value = %" PRId64,
+ trigger_name,
+ (int) trigger_owner_uid,
+ local_value);
+ } else {
+ abort();
+ }
+
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto end;
+ }
+
+ /* Cast is safe as negative values are checked-for above. */
+ global_sum += (uint64_t) local_value;
+
+ }
+
+ *count = global_sum;
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK;
+
+end:
+ rcu_read_unlock();
+ return status;
+}
+
+static
+enum event_notifier_error_accounting_status event_notifier_error_accounting_ust_clear(
+ const struct lttng_trigger *trigger)
+{
+ struct lttng_ht_iter iter;
+ struct error_account_entry *uid_entry;
+ uint64_t error_counter_index;
+ enum event_notifier_error_accounting_status status;
+ size_t dimension_index;
+ const uint64_t tracer_token = lttng_trigger_get_tracer_token(trigger);
+
+ /*
+ * Go over all error counters (ignoring uid) as a trigger (and trigger
+ * errors) can be generated from any applications that this session
+ * daemon is managing.
+ */
+
+ rcu_read_lock();
+ status = get_error_counter_index_for_token(
+ tracer_token,
+ &error_counter_index);
+ if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ uid_t trigger_owner_uid;
+ const char *trigger_name;
+
+ get_trigger_info_for_log(trigger, &trigger_name,
+ &trigger_owner_uid);
+
+ ERR("Failed to retrieve index for tracer token: token = %" PRIu64 ", trigger name = '%s', trigger owner uid = %d, status = %s",
+ tracer_token, trigger_name,
+ (int) trigger_owner_uid,
+ error_accounting_status_str(status));
+ goto end;
+ }
+
+ dimension_index = error_counter_index;
+
+ cds_lfht_for_each_entry(error_counter_uid_ht->ht, &iter.iter,
+ uid_entry, node.node) {
+ const int ret = ustctl_counter_clear(uid_entry->daemon_counter,
+ &dimension_index);
+
+ if (ret) {
+ uid_t trigger_owner_uid;
+ const char *trigger_name;
+
+ get_trigger_info_for_log(trigger, &trigger_name,
+ &trigger_owner_uid);
+ ERR("Failed to clear event notifier counter value for trigger: counter uid = %d, trigger name = '%s', trigger owner uid = %d",
+ (int) uid_entry->node.key, trigger_name,
+ (int) trigger_owner_uid);
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_ERR;
+ goto end;
+ }
+ }
+
+ status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK;
+end:
+ rcu_read_unlock();
+ return status;
+}
+#endif /* HAVE_LIBLTTNG_UST_CTL */
+