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;
89 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
92 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
96 stats = memzone->addr;
97 memset(stats, 0, sizeof(struct rte_metrics_data_s));
99 return rte_memzone_free(memzone);
104 rte_metrics_reg_name(const char *name)
106 const char * const list_names[] = {name};
108 return rte_metrics_reg_names(list_names, 1);
112 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
114 struct rte_metrics_meta_s *entry = NULL;
115 struct rte_metrics_data_s *stats;
116 const struct rte_memzone *memzone;
120 /* Some sanity checks */
121 if (cnt_names < 1 || names == NULL)
123 for (idx_name = 0; idx_name < cnt_names; idx_name++)
124 if (names[idx_name] == NULL)
127 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
130 stats = memzone->addr;
132 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
135 rte_spinlock_lock(&stats->lock);
137 /* Overwritten later if this is actually first set.. */
138 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
140 stats->idx_last_set = idx_base = stats->cnt_stats;
142 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
143 entry = &stats->metadata[idx_name + stats->cnt_stats];
144 strlcpy(entry->name, names[idx_name], RTE_METRICS_MAX_NAME_LEN);
145 memset(entry->value, 0, sizeof(entry->value));
146 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
148 entry->idx_next_stat = 0;
149 entry->idx_next_set = 0;
150 stats->cnt_stats += cnt_names;
152 rte_spinlock_unlock(&stats->lock);
158 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
160 return rte_metrics_update_values(port_id, key, &value, 1);
164 rte_metrics_update_values(int port_id,
166 const uint64_t *values,
169 struct rte_metrics_meta_s *entry;
170 struct rte_metrics_data_s *stats;
171 const struct rte_memzone *memzone;
174 uint16_t cnt_setsize;
176 if (port_id != RTE_METRICS_GLOBAL &&
177 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
183 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
186 stats = memzone->addr;
188 rte_spinlock_lock(&stats->lock);
190 if (key >= stats->cnt_stats) {
191 rte_spinlock_unlock(&stats->lock);
196 while (idx_metric < stats->cnt_stats) {
197 entry = &stats->metadata[idx_metric];
198 if (entry->idx_next_stat == 0)
203 /* Check update does not cross set border */
204 if (count > cnt_setsize) {
205 rte_spinlock_unlock(&stats->lock);
209 if (port_id == RTE_METRICS_GLOBAL)
210 for (idx_value = 0; idx_value < count; idx_value++) {
211 idx_metric = key + idx_value;
212 stats->metadata[idx_metric].global_value =
216 for (idx_value = 0; idx_value < count; idx_value++) {
217 idx_metric = key + idx_value;
218 stats->metadata[idx_metric].value[port_id] =
221 rte_spinlock_unlock(&stats->lock);
226 rte_metrics_get_names(struct rte_metric_name *names,
229 struct rte_metrics_data_s *stats;
230 const struct rte_memzone *memzone;
234 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
238 stats = memzone->addr;
239 rte_spinlock_lock(&stats->lock);
241 if (capacity < stats->cnt_stats) {
242 return_value = stats->cnt_stats;
243 rte_spinlock_unlock(&stats->lock);
246 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
247 strlcpy(names[idx_name].name,
248 stats->metadata[idx_name].name,
249 RTE_METRICS_MAX_NAME_LEN);
251 return_value = stats->cnt_stats;
252 rte_spinlock_unlock(&stats->lock);
257 rte_metrics_get_values(int port_id,
258 struct rte_metric_value *values,
261 struct rte_metrics_meta_s *entry;
262 struct rte_metrics_data_s *stats;
263 const struct rte_memzone *memzone;
267 if (port_id != RTE_METRICS_GLOBAL &&
268 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
271 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
275 stats = memzone->addr;
276 rte_spinlock_lock(&stats->lock);
278 if (values != NULL) {
279 if (capacity < stats->cnt_stats) {
280 return_value = stats->cnt_stats;
281 rte_spinlock_unlock(&stats->lock);
284 if (port_id == RTE_METRICS_GLOBAL)
286 idx_name < stats->cnt_stats;
288 entry = &stats->metadata[idx_name];
289 values[idx_name].key = idx_name;
290 values[idx_name].value = entry->global_value;
294 idx_name < stats->cnt_stats;
296 entry = &stats->metadata[idx_name];
297 values[idx_name].key = idx_name;
298 values[idx_name].value = entry->value[port_id];
301 return_value = stats->cnt_stats;
302 rte_spinlock_unlock(&stats->lock);