4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/queue.h>
37 #include <rte_common.h>
38 #include <rte_malloc.h>
39 #include <rte_metrics.h>
40 #include <rte_lcore.h>
41 #include <rte_memzone.h>
42 #include <rte_spinlock.h>
44 #define RTE_METRICS_MAX_METRICS 256
45 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
48 * Internal stats metadata and value entry.
52 struct rte_metrics_meta_s {
54 char name[RTE_METRICS_MAX_NAME_LEN];
55 /** Current value for metric */
56 uint64_t value[RTE_MAX_ETHPORTS];
57 /** Used for global metrics */
58 uint64_t global_value;
59 /** Index of next root element (zero for none) */
60 uint16_t idx_next_set;
61 /** Index of next metric in set (zero for none) */
62 uint16_t idx_next_stat;
66 * Internal stats info structure.
69 * Offsets into metadata are used instead of pointers because ASLR
70 * means that having the same physical addresses in different
71 * processes is not guaranteed.
73 struct rte_metrics_data_s {
74 /** Index of last metadata entry with valid data.
75 * This value is not valid if cnt_stats is zero.
77 uint16_t idx_last_set;
78 /** Number of metrics. */
80 /** Metric data memory block. */
81 struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
82 /** Metric data access lock */
87 rte_metrics_init(int socket_id)
89 struct rte_metrics_data_s *stats;
90 const struct rte_memzone *memzone;
92 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
95 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
98 memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
99 sizeof(struct rte_metrics_data_s), socket_id, 0);
101 rte_exit(EXIT_FAILURE, "Unable to allocate stats memzone\n");
102 stats = memzone->addr;
103 memset(stats, 0, sizeof(struct rte_metrics_data_s));
104 rte_spinlock_init(&stats->lock);
108 rte_metrics_reg_name(const char *name)
110 const char * const list_names[] = {name};
112 return rte_metrics_reg_names(list_names, 1);
116 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
118 struct rte_metrics_meta_s *entry;
119 struct rte_metrics_data_s *stats;
120 const struct rte_memzone *memzone;
124 /* Some sanity checks */
125 if (cnt_names < 1 || names == NULL)
128 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
131 stats = memzone->addr;
133 if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
136 rte_spinlock_lock(&stats->lock);
138 /* Overwritten later if this is actually first set.. */
139 stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
141 stats->idx_last_set = idx_base = stats->cnt_stats;
143 for (idx_name = 0; idx_name < cnt_names; idx_name++) {
144 entry = &stats->metadata[idx_name + stats->cnt_stats];
145 strncpy(entry->name, names[idx_name],
146 RTE_METRICS_MAX_NAME_LEN);
147 /* Enforce NULL-termination */
148 entry->name[RTE_METRICS_MAX_NAME_LEN - 1] = '\0';
149 memset(entry->value, 0, sizeof(entry->value));
150 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
152 entry->idx_next_stat = 0;
153 entry->idx_next_set = 0;
154 stats->cnt_stats += cnt_names;
156 rte_spinlock_unlock(&stats->lock);
162 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
164 return rte_metrics_update_values(port_id, key, &value, 1);
168 rte_metrics_update_values(int port_id,
170 const uint64_t *values,
173 struct rte_metrics_meta_s *entry;
174 struct rte_metrics_data_s *stats;
175 const struct rte_memzone *memzone;
178 uint16_t cnt_setsize;
180 if (port_id != RTE_METRICS_GLOBAL &&
181 (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
187 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
190 stats = memzone->addr;
192 rte_spinlock_lock(&stats->lock);
195 while (idx_metric < stats->cnt_stats) {
196 entry = &stats->metadata[idx_metric];
197 if (entry->idx_next_stat == 0)
202 /* Check update does not cross set border */
203 if (count > cnt_setsize) {
204 rte_spinlock_unlock(&stats->lock);
208 if (port_id == RTE_METRICS_GLOBAL)
209 for (idx_value = 0; idx_value < count; idx_value++) {
210 idx_metric = key + idx_value;
211 stats->metadata[idx_metric].global_value =
215 for (idx_value = 0; idx_value < count; idx_value++) {
216 idx_metric = key + idx_value;
217 stats->metadata[idx_metric].value[port_id] =
220 rte_spinlock_unlock(&stats->lock);
225 rte_metrics_get_names(struct rte_metric_name *names,
228 struct rte_metrics_data_s *stats;
229 const struct rte_memzone *memzone;
233 memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
234 /* If not allocated, fail silently */
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 strncpy(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);
272 /* If not allocated, fail silently */
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);