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)
99 for (idx_name = 0; idx_name < cnt_names; idx_name++)
100 if (names[idx_name] == NULL)
103 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
106 stats = memzone->addr;
108 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
111 rte_spinlock_lock(&stats->lock);
113 /* Overwritten later if this is actually first set.. */
114 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
116 stats->idx_last_set = idx_base = stats->cnt_stats;
118 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
119 entry = &stats->metadata[idx_name + stats->cnt_stats];
120 strlcpy(entry->name, names[idx_name], RTE_METRICS_MAX_NAME_LEN);
121 memset(entry->value, 0, sizeof(entry->value));
122 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
124 entry->idx_next_stat = 0;
125 entry->idx_next_set = 0;
126 stats->cnt_stats += cnt_names;
128 rte_spinlock_unlock(&stats->lock);
134 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
136 return rte_metrics_update_values(port_id, key, &value, 1);
140 rte_metrics_update_values(int port_id,
142 const uint64_t *values,
145 struct rte_metrics_meta_s *entry;
146 struct rte_metrics_data_s *stats;
147 const struct rte_memzone *memzone;
150 uint16_t cnt_setsize;
152 if (port_id != RTE_METRICS_GLOBAL &&
153 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
159 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
162 stats = memzone->addr;
164 rte_spinlock_lock(&stats->lock);
166 if (key >= stats->cnt_stats) {
167 rte_spinlock_unlock(&stats->lock);
172 while (idx_metric < stats->cnt_stats) {
173 entry = &stats->metadata[idx_metric];
174 if (entry->idx_next_stat == 0)
179 /* Check update does not cross set border */
180 if (count > cnt_setsize) {
181 rte_spinlock_unlock(&stats->lock);
185 if (port_id == RTE_METRICS_GLOBAL)
186 for (idx_value = 0; idx_value < count; idx_value++) {
187 idx_metric = key + idx_value;
188 stats->metadata[idx_metric].global_value =
192 for (idx_value = 0; idx_value < count; idx_value++) {
193 idx_metric = key + idx_value;
194 stats->metadata[idx_metric].value[port_id] =
197 rte_spinlock_unlock(&stats->lock);
202 rte_metrics_get_names(struct rte_metric_name *names,
205 struct rte_metrics_data_s *stats;
206 const struct rte_memzone *memzone;
210 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
214 stats = memzone->addr;
215 rte_spinlock_lock(&stats->lock);
217 if (capacity < stats->cnt_stats) {
218 return_value = stats->cnt_stats;
219 rte_spinlock_unlock(&stats->lock);
222 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
223 strlcpy(names[idx_name].name,
224 stats->metadata[idx_name].name,
225 RTE_METRICS_MAX_NAME_LEN);
227 return_value = stats->cnt_stats;
228 rte_spinlock_unlock(&stats->lock);
233 rte_metrics_get_values(int port_id,
234 struct rte_metric_value *values,
237 struct rte_metrics_meta_s *entry;
238 struct rte_metrics_data_s *stats;
239 const struct rte_memzone *memzone;
243 if (port_id != RTE_METRICS_GLOBAL &&
244 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
247 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
251 stats = memzone->addr;
252 rte_spinlock_lock(&stats->lock);
254 if (values != NULL) {
255 if (capacity < stats->cnt_stats) {
256 return_value = stats->cnt_stats;
257 rte_spinlock_unlock(&stats->lock);
260 if (port_id == RTE_METRICS_GLOBAL)
262 idx_name < stats->cnt_stats;
264 entry = &stats->metadata[idx_name];
265 values[idx_name].key = idx_name;
266 values[idx_name].value = entry->global_value;
270 idx_name < stats->cnt_stats;
272 entry = &stats->metadata[idx_name];
273 values[idx_name].key = idx_name;
274 values[idx_name].value = entry->value[port_id];
277 return_value = stats->cnt_stats;
278 rte_spinlock_unlock(&stats->lock);