1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_string_fns.h>
10 #include <rte_malloc.h>
11 #include <rte_metrics.h>
12 #include <rte_lcore.h>
13 #include <rte_memzone.h>
14 #include <rte_spinlock.h>
16 #define RTE_METRICS_MAX_METRICS 256
17 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
20 * Internal stats metadata and value entry.
24 struct rte_metrics_meta_s {
26 char name[RTE_METRICS_MAX_NAME_LEN];
27 /** Current value for metric */
28 uint64_t value[RTE_MAX_ETHPORTS];
29 /** Used for global metrics */
30 uint64_t global_value;
31 /** Index of next root element (zero for none) */
32 uint16_t idx_next_set;
33 /** Index of next metric in set (zero for none) */
34 uint16_t idx_next_stat;
38 * Internal stats info structure.
41 * Offsets into metadata are used instead of pointers because ASLR
42 * means that having the same physical addresses in different
43 * processes is not guaranteed.
45 struct rte_metrics_data_s {
46 /** Index of last metadata entry with valid data.
47 * This value is not valid if cnt_stats is zero.
49 uint16_t idx_last_set;
50 /** Number of metrics. */
52 /** Metric data memory block. */
53 struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
54 /** Metric data access lock */
59 rte_metrics_init(int socket_id)
61 struct rte_metrics_data_s *stats;
62 const struct rte_memzone *memzone;
64 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
67 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
70 memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
71 sizeof(struct rte_metrics_data_s), socket_id, 0);
73 rte_exit(EXIT_FAILURE, "Unable to allocate stats memzone\n");
74 stats = memzone->addr;
75 memset(stats, 0, sizeof(struct rte_metrics_data_s));
76 rte_spinlock_init(&stats->lock);
80 rte_metrics_reg_name(const char *name)
82 const char * const list_names[] = {name};
84 return rte_metrics_reg_names(list_names, 1);
88 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
90 struct rte_metrics_meta_s *entry = NULL;
91 struct rte_metrics_data_s *stats;
92 const struct rte_memzone *memzone;
96 /* Some sanity checks */
97 if (cnt_names < 1 || names == NULL)
100 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
103 stats = memzone->addr;
105 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
108 rte_spinlock_lock(&stats->lock);
110 /* Overwritten later if this is actually first set.. */
111 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
113 stats->idx_last_set = idx_base = stats->cnt_stats;
115 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
116 entry = &stats->metadata[idx_name + stats->cnt_stats];
117 strlcpy(entry->name, names[idx_name], RTE_METRICS_MAX_NAME_LEN);
118 memset(entry->value, 0, sizeof(entry->value));
119 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
121 entry->idx_next_stat = 0;
122 entry->idx_next_set = 0;
123 stats->cnt_stats += cnt_names;
125 rte_spinlock_unlock(&stats->lock);
131 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
133 return rte_metrics_update_values(port_id, key, &value, 1);
137 rte_metrics_update_values(int port_id,
139 const uint64_t *values,
142 struct rte_metrics_meta_s *entry;
143 struct rte_metrics_data_s *stats;
144 const struct rte_memzone *memzone;
147 uint16_t cnt_setsize;
149 if (port_id != RTE_METRICS_GLOBAL &&
150 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
156 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
159 stats = memzone->addr;
161 rte_spinlock_lock(&stats->lock);
164 while (idx_metric < stats->cnt_stats) {
165 entry = &stats->metadata[idx_metric];
166 if (entry->idx_next_stat == 0)
171 /* Check update does not cross set border */
172 if (count > cnt_setsize) {
173 rte_spinlock_unlock(&stats->lock);
177 if (port_id == RTE_METRICS_GLOBAL)
178 for (idx_value = 0; idx_value < count; idx_value++) {
179 idx_metric = key + idx_value;
180 stats->metadata[idx_metric].global_value =
184 for (idx_value = 0; idx_value < count; idx_value++) {
185 idx_metric = key + idx_value;
186 stats->metadata[idx_metric].value[port_id] =
189 rte_spinlock_unlock(&stats->lock);
194 rte_metrics_get_names(struct rte_metric_name *names,
197 struct rte_metrics_data_s *stats;
198 const struct rte_memzone *memzone;
202 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
203 /* If not allocated, fail silently */
207 stats = memzone->addr;
208 rte_spinlock_lock(&stats->lock);
210 if (capacity < stats->cnt_stats) {
211 return_value = stats->cnt_stats;
212 rte_spinlock_unlock(&stats->lock);
215 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
216 strlcpy(names[idx_name].name,
217 stats->metadata[idx_name].name,
218 RTE_METRICS_MAX_NAME_LEN);
220 return_value = stats->cnt_stats;
221 rte_spinlock_unlock(&stats->lock);
226 rte_metrics_get_values(int port_id,
227 struct rte_metric_value *values,
230 struct rte_metrics_meta_s *entry;
231 struct rte_metrics_data_s *stats;
232 const struct rte_memzone *memzone;
236 if (port_id != RTE_METRICS_GLOBAL &&
237 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
240 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
241 /* If not allocated, fail silently */
244 stats = memzone->addr;
245 rte_spinlock_lock(&stats->lock);
247 if (values != NULL) {
248 if (capacity < stats->cnt_stats) {
249 return_value = stats->cnt_stats;
250 rte_spinlock_unlock(&stats->lock);
253 if (port_id == RTE_METRICS_GLOBAL)
255 idx_name < stats->cnt_stats;
257 entry = &stats->metadata[idx_name];
258 values[idx_name].key = idx_name;
259 values[idx_name].value = entry->global_value;
263 idx_name < stats->cnt_stats;
265 entry = &stats->metadata[idx_name];
266 values[idx_name].key = idx_name;
267 values[idx_name].value = entry->value[port_id];
270 return_value = stats->cnt_stats;
271 rte_spinlock_unlock(&stats->lock);