| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * counter/counter-internal.h |
| 4 | * |
| 5 | * LTTng Counters Internal Header |
| 6 | * |
| 7 | * Copyright (C) 2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #ifndef _LTTNG_COUNTER_INTERNAL_H |
| 11 | #define _LTTNG_COUNTER_INTERNAL_H |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <counter/counter-types.h> |
| 16 | #include <counter/config.h> |
| 17 | |
| 18 | static inline int lttng_counter_validate_indexes(const struct lib_counter_config *config, |
| 19 | struct lib_counter *counter, |
| 20 | const size_t *dimension_indexes) |
| 21 | { |
| 22 | size_t nr_dimensions = counter->nr_dimensions, i; |
| 23 | |
| 24 | for (i = 0; i < nr_dimensions; i++) { |
| 25 | if (unlikely(dimension_indexes[i] >= counter->dimensions[i].max_nr_elem)) |
| 26 | return -EOVERFLOW; |
| 27 | } |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static inline size_t lttng_counter_get_index(const struct lib_counter_config *config, |
| 32 | struct lib_counter *counter, |
| 33 | const size_t *dimension_indexes) |
| 34 | { |
| 35 | size_t nr_dimensions = counter->nr_dimensions, i; |
| 36 | size_t index = 0; |
| 37 | |
| 38 | for (i = 0; i < nr_dimensions; i++) { |
| 39 | struct lib_counter_dimension *dimension = &counter->dimensions[i]; |
| 40 | const size_t *dimension_index = &dimension_indexes[i]; |
| 41 | |
| 42 | index += *dimension_index * dimension->stride; |
| 43 | } |
| 44 | return index; |
| 45 | } |
| 46 | |
| 47 | #endif /* _LTTNG_COUNTER_INTERNAL_H */ |