1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
10 #include <rte_metrics.h>
11 #include <rte_lcore.h>
12 #include <rte_memzone.h>
13 #include <rte_spinlock.h>
15 #define RTE_METRICS_MAX_METRICS 256
16 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
19 * Internal stats metadata and value entry.
23 struct rte_metrics_meta_s {
25 char name[RTE_METRICS_MAX_NAME_LEN];
26 /** Current value for metric */
27 uint64_t value[RTE_MAX_ETHPORTS];
28 /** Used for global metrics */
29 uint64_t global_value;
30 /** Index of next root element (zero for none) */
31 uint16_t idx_next_set;
32 /** Index of next metric in set (zero for none) */
33 uint16_t idx_next_stat;
37 * Internal stats info structure.
40 * Offsets into metadata are used instead of pointers because ASLR
41 * means that having the same physical addresses in different
42 * processes is not guaranteed.
44 struct rte_metrics_data_s {
45 /** Index of last metadata entry with valid data.
46 * This value is not valid if cnt_stats is zero.
48 uint16_t idx_last_set;
49 /** Number of metrics. */
51 /** Metric data memory block. */
52 struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
53 /** Metric data access lock */
58 rte_metrics_init(int socket_id)
60 struct rte_metrics_data_s *stats;
61 const struct rte_memzone *memzone;
63 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
66 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
69 memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
70 sizeof(struct rte_metrics_data_s), socket_id, 0);
72 rte_exit(EXIT_FAILURE, "Unable to allocate stats memzone\n");
73 stats = memzone->addr;
74 memset(stats, 0, sizeof(struct rte_metrics_data_s));
75 rte_spinlock_init(&stats->lock);
79 rte_metrics_reg_name(const char *name)
81 const char * const list_names[] = {name};
83 return rte_metrics_reg_names(list_names, 1);
87 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
89 struct rte_metrics_meta_s *entry = NULL;
90 struct rte_metrics_data_s *stats;
91 const struct rte_memzone *memzone;
95 /* Some sanity checks */
96 if (cnt_names < 1 || names == NULL)
99 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
102 stats = memzone->addr;
104 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
107 rte_spinlock_lock(&stats->lock);
109 /* Overwritten later if this is actually first set.. */
110 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
112 stats->idx_last_set = idx_base = stats->cnt_stats;
114 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
115 entry = &stats->metadata[idx_name + stats->cnt_stats];
116 strncpy(entry->name, names[idx_name],
117 RTE_METRICS_MAX_NAME_LEN);
118 /* Enforce NULL-termination */
119 entry->name[RTE_METRICS_MAX_NAME_LEN - 1] = '\0';
120 memset(entry->value, 0, sizeof(entry->value));
121 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
123 entry->idx_next_stat = 0;
124 entry->idx_next_set = 0;
125 stats->cnt_stats += cnt_names;
127 rte_spinlock_unlock(&stats->lock);
133 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
135 return rte_metrics_update_values(port_id, key, &value, 1);
139 rte_metrics_update_values(int port_id,
141 const uint64_t *values,
144 struct rte_metrics_meta_s *entry;
145 struct rte_metrics_data_s *stats;
146 const struct rte_memzone *memzone;
149 uint16_t cnt_setsize;
151 if (port_id != RTE_METRICS_GLOBAL &&
152 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
158 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
161 stats = memzone->addr;
163 rte_spinlock_lock(&stats->lock);
166 while (idx_metric < stats->cnt_stats) {
167 entry = &stats->metadata[idx_metric];
168 if (entry->idx_next_stat == 0)
173 /* Check update does not cross set border */
174 if (count > cnt_setsize) {
175 rte_spinlock_unlock(&stats->lock);
179 if (port_id == RTE_METRICS_GLOBAL)
180 for (idx_value = 0; idx_value < count; idx_value++) {
181 idx_metric = key + idx_value;
182 stats->metadata[idx_metric].global_value =
186 for (idx_value = 0; idx_value < count; idx_value++) {
187 idx_metric = key + idx_value;
188 stats->metadata[idx_metric].value[port_id] =
191 rte_spinlock_unlock(&stats->lock);
196 rte_metrics_get_names(struct rte_metric_name *names,
199 struct rte_metrics_data_s *stats;
200 const struct rte_memzone *memzone;
204 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
205 /* If not allocated, fail silently */
209 stats = memzone->addr;
210 rte_spinlock_lock(&stats->lock);
212 if (capacity < stats->cnt_stats) {
213 return_value = stats->cnt_stats;
214 rte_spinlock_unlock(&stats->lock);
217 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
218 strncpy(names[idx_name].name,
219 stats->metadata[idx_name].name,
220 RTE_METRICS_MAX_NAME_LEN);
222 return_value = stats->cnt_stats;
223 rte_spinlock_unlock(&stats->lock);
228 rte_metrics_get_values(int port_id,
229 struct rte_metric_value *values,
232 struct rte_metrics_meta_s *entry;
233 struct rte_metrics_data_s *stats;
234 const struct rte_memzone *memzone;
238 if (port_id != RTE_METRICS_GLOBAL &&
239 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
242 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
243 /* If not allocated, fail silently */
246 stats = memzone->addr;
247 rte_spinlock_lock(&stats->lock);
249 if (values != NULL) {
250 if (capacity < stats->cnt_stats) {
251 return_value = stats->cnt_stats;
252 rte_spinlock_unlock(&stats->lock);
255 if (port_id == RTE_METRICS_GLOBAL)
257 idx_name < stats->cnt_stats;
259 entry = &stats->metadata[idx_name];
260 values[idx_name].key = idx_name;
261 values[idx_name].value = entry->global_value;
265 idx_name < stats->cnt_stats;
267 entry = &stats->metadata[idx_name];
268 values[idx_name].key = idx_name;
269 values[idx_name].value = entry->value[port_id];
272 return_value = stats->cnt_stats;
273 rte_spinlock_unlock(&stats->lock);