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 int metrics_initialized;
18 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
21 * Internal stats metadata and value entry.
25 struct rte_metrics_meta_s {
27 char name[RTE_METRICS_MAX_NAME_LEN];
28 /** Current value for metric */
29 uint64_t value[RTE_MAX_ETHPORTS];
30 /** Used for global metrics */
31 uint64_t global_value;
32 /** Index of next root element (zero for none) */
33 uint16_t idx_next_set;
34 /** Index of next metric in set (zero for none) */
35 uint16_t idx_next_stat;
39 * Internal stats info structure.
42 * Offsets into metadata are used instead of pointers because ASLR
43 * means that having the same physical addresses in different
44 * processes is not guaranteed.
46 struct rte_metrics_data_s {
47 /** Index of last metadata entry with valid data.
48 * This value is not valid if cnt_stats is zero.
50 uint16_t idx_last_set;
51 /** Number of metrics. */
53 /** Metric data memory block. */
54 struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
55 /** Metric data access lock */
60 rte_metrics_init(int socket_id)
62 struct rte_metrics_data_s *stats;
63 const struct rte_memzone *memzone;
65 if (metrics_initialized)
67 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
70 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
73 memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
74 sizeof(struct rte_metrics_data_s), socket_id, 0);
76 rte_exit(EXIT_FAILURE, "Unable to allocate stats memzone\n");
77 stats = memzone->addr;
78 memset(stats, 0, sizeof(struct rte_metrics_data_s));
79 rte_spinlock_init(&stats->lock);
80 metrics_initialized = 1;
84 rte_metrics_deinit(void)
86 struct rte_metrics_data_s *stats;
87 const struct rte_memzone *memzone;
90 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
93 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
97 stats = memzone->addr;
98 memset(stats, 0, sizeof(struct rte_metrics_data_s));
100 ret = rte_memzone_free(memzone);
102 metrics_initialized = 0;
107 rte_metrics_reg_name(const char *name)
109 const char * const list_names[] = {name};
111 return rte_metrics_reg_names(list_names, 1);
115 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
117 struct rte_metrics_meta_s *entry = NULL;
118 struct rte_metrics_data_s *stats;
119 const struct rte_memzone *memzone;
123 /* Some sanity checks */
124 if (cnt_names < 1 || names == NULL)
126 for (idx_name = 0; idx_name < cnt_names; idx_name++)
127 if (names[idx_name] == NULL)
130 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
133 stats = memzone->addr;
135 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
138 rte_spinlock_lock(&stats->lock);
140 /* Overwritten later if this is actually first set.. */
141 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
143 stats->idx_last_set = idx_base = stats->cnt_stats;
145 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
146 entry = &stats->metadata[idx_name + stats->cnt_stats];
147 strlcpy(entry->name, names[idx_name], RTE_METRICS_MAX_NAME_LEN);
148 memset(entry->value, 0, sizeof(entry->value));
149 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
151 entry->idx_next_stat = 0;
152 entry->idx_next_set = 0;
153 stats->cnt_stats += cnt_names;
155 rte_spinlock_unlock(&stats->lock);
161 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
163 return rte_metrics_update_values(port_id, key, &value, 1);
167 rte_metrics_update_values(int port_id,
169 const uint64_t *values,
172 struct rte_metrics_meta_s *entry;
173 struct rte_metrics_data_s *stats;
174 const struct rte_memzone *memzone;
177 uint16_t cnt_setsize;
179 if (port_id != RTE_METRICS_GLOBAL &&
180 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
186 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
189 stats = memzone->addr;
191 rte_spinlock_lock(&stats->lock);
193 if (key >= stats->cnt_stats) {
194 rte_spinlock_unlock(&stats->lock);
199 while (idx_metric < stats->cnt_stats) {
200 entry = &stats->metadata[idx_metric];
201 if (entry->idx_next_stat == 0)
206 /* Check update does not cross set border */
207 if (count > cnt_setsize) {
208 rte_spinlock_unlock(&stats->lock);
212 if (port_id == RTE_METRICS_GLOBAL)
213 for (idx_value = 0; idx_value < count; idx_value++) {
214 idx_metric = key + idx_value;
215 stats->metadata[idx_metric].global_value =
219 for (idx_value = 0; idx_value < count; idx_value++) {
220 idx_metric = key + idx_value;
221 stats->metadata[idx_metric].value[port_id] =
224 rte_spinlock_unlock(&stats->lock);
229 rte_metrics_get_names(struct rte_metric_name *names,
232 struct rte_metrics_data_s *stats;
233 const struct rte_memzone *memzone;
237 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
241 stats = memzone->addr;
242 rte_spinlock_lock(&stats->lock);
244 if (capacity < stats->cnt_stats) {
245 return_value = stats->cnt_stats;
246 rte_spinlock_unlock(&stats->lock);
249 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
250 strlcpy(names[idx_name].name,
251 stats->metadata[idx_name].name,
252 RTE_METRICS_MAX_NAME_LEN);
254 return_value = stats->cnt_stats;
255 rte_spinlock_unlock(&stats->lock);
260 rte_metrics_get_values(int port_id,
261 struct rte_metric_value *values,
264 struct rte_metrics_meta_s *entry;
265 struct rte_metrics_data_s *stats;
266 const struct rte_memzone *memzone;
270 if (port_id != RTE_METRICS_GLOBAL &&
271 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
274 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
278 stats = memzone->addr;
279 rte_spinlock_lock(&stats->lock);
281 if (values != NULL) {
282 if (capacity < stats->cnt_stats) {
283 return_value = stats->cnt_stats;
284 rte_spinlock_unlock(&stats->lock);
287 if (port_id == RTE_METRICS_GLOBAL)
289 idx_name < stats->cnt_stats;
291 entry = &stats->metadata[idx_name];
292 values[idx_name].key = idx_name;
293 values[idx_name].value = entry->global_value;
297 idx_name < stats->cnt_stats;
299 entry = &stats->metadata[idx_name];
300 values[idx_name].key = idx_name;
301 values[idx_name].value = entry->value[port_id];
304 return_value = stats->cnt_stats;
305 rte_spinlock_unlock(&stats->lock);