common/octeontx2: upgrade mbox definition to version 7
[dpdk.git] / lib / librte_metrics / rte_metrics.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <sys/queue.h>
7
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>
15
16 int metrics_initialized;
17
18 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
19
20 /**
21  * Internal stats metadata and value entry.
22  *
23  * @internal
24  */
25 struct rte_metrics_meta_s {
26         /** Name of metric */
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;
36 };
37
38 /**
39  * Internal stats info structure.
40  *
41  * @internal
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.
45  */
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.
49          */
50         uint16_t idx_last_set;
51         /**   Number of metrics. */
52         uint16_t cnt_stats;
53         /** Metric data memory block. */
54         struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
55         /** Metric data access lock */
56         rte_spinlock_t lock;
57 };
58
59 void
60 rte_metrics_init(int socket_id)
61 {
62         struct rte_metrics_data_s *stats;
63         const struct rte_memzone *memzone;
64
65         if (metrics_initialized)
66                 return;
67         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
68                 return;
69
70         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
71         if (memzone != NULL)
72                 return;
73         memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
74                 sizeof(struct rte_metrics_data_s), socket_id, 0);
75         if (memzone == NULL)
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;
81 }
82
83 int
84 rte_metrics_deinit(void)
85 {
86         struct rte_metrics_data_s *stats;
87         const struct rte_memzone *memzone;
88
89         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
90                 return -EINVAL;
91
92         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
93         if (memzone == NULL)
94                 return -EIO;
95
96         stats = memzone->addr;
97         memset(stats, 0, sizeof(struct rte_metrics_data_s));
98
99         return rte_memzone_free(memzone);
100
101 }
102
103 int
104 rte_metrics_reg_name(const char *name)
105 {
106         const char * const list_names[] = {name};
107
108         return rte_metrics_reg_names(list_names, 1);
109 }
110
111 int
112 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
113 {
114         struct rte_metrics_meta_s *entry = NULL;
115         struct rte_metrics_data_s *stats;
116         const struct rte_memzone *memzone;
117         uint16_t idx_name;
118         uint16_t idx_base;
119
120         /* Some sanity checks */
121         if (cnt_names < 1 || names == NULL)
122                 return -EINVAL;
123         for (idx_name = 0; idx_name < cnt_names; idx_name++)
124                 if (names[idx_name] == NULL)
125                         return -EINVAL;
126
127         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
128         if (memzone == NULL)
129                 return -EIO;
130         stats = memzone->addr;
131
132         if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
133                 return -ENOMEM;
134
135         rte_spinlock_lock(&stats->lock);
136
137         /* Overwritten later if this is actually first set.. */
138         stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
139
140         stats->idx_last_set = idx_base = stats->cnt_stats;
141
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;
147         }
148         entry->idx_next_stat = 0;
149         entry->idx_next_set = 0;
150         stats->cnt_stats += cnt_names;
151
152         rte_spinlock_unlock(&stats->lock);
153
154         return idx_base;
155 }
156
157 int
158 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
159 {
160         return rte_metrics_update_values(port_id, key, &value, 1);
161 }
162
163 int
164 rte_metrics_update_values(int port_id,
165         uint16_t key,
166         const uint64_t *values,
167         uint32_t count)
168 {
169         struct rte_metrics_meta_s *entry;
170         struct rte_metrics_data_s *stats;
171         const struct rte_memzone *memzone;
172         uint16_t idx_metric;
173         uint16_t idx_value;
174         uint16_t cnt_setsize;
175
176         if (port_id != RTE_METRICS_GLOBAL &&
177                         (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
178                 return -EINVAL;
179
180         if (values == NULL)
181                 return -EINVAL;
182
183         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
184         if (memzone == NULL)
185                 return -EIO;
186         stats = memzone->addr;
187
188         rte_spinlock_lock(&stats->lock);
189
190         if (key >= stats->cnt_stats) {
191                 rte_spinlock_unlock(&stats->lock);
192                 return -EINVAL;
193         }
194         idx_metric = key;
195         cnt_setsize = 1;
196         while (idx_metric < stats->cnt_stats) {
197                 entry = &stats->metadata[idx_metric];
198                 if (entry->idx_next_stat == 0)
199                         break;
200                 cnt_setsize++;
201                 idx_metric++;
202         }
203         /* Check update does not cross set border */
204         if (count > cnt_setsize) {
205                 rte_spinlock_unlock(&stats->lock);
206                 return -ERANGE;
207         }
208
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 =
213                                 values[idx_value];
214                 }
215         else
216                 for (idx_value = 0; idx_value < count; idx_value++) {
217                         idx_metric = key + idx_value;
218                         stats->metadata[idx_metric].value[port_id] =
219                                 values[idx_value];
220                 }
221         rte_spinlock_unlock(&stats->lock);
222         return 0;
223 }
224
225 int
226 rte_metrics_get_names(struct rte_metric_name *names,
227         uint16_t capacity)
228 {
229         struct rte_metrics_data_s *stats;
230         const struct rte_memzone *memzone;
231         uint16_t idx_name;
232         int return_value;
233
234         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
235         if (memzone == NULL)
236                 return -EIO;
237
238         stats = memzone->addr;
239         rte_spinlock_lock(&stats->lock);
240         if (names != NULL) {
241                 if (capacity < stats->cnt_stats) {
242                         return_value = stats->cnt_stats;
243                         rte_spinlock_unlock(&stats->lock);
244                         return return_value;
245                 }
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);
250         }
251         return_value = stats->cnt_stats;
252         rte_spinlock_unlock(&stats->lock);
253         return return_value;
254 }
255
256 int
257 rte_metrics_get_values(int port_id,
258         struct rte_metric_value *values,
259         uint16_t capacity)
260 {
261         struct rte_metrics_meta_s *entry;
262         struct rte_metrics_data_s *stats;
263         const struct rte_memzone *memzone;
264         uint16_t idx_name;
265         int return_value;
266
267         if (port_id != RTE_METRICS_GLOBAL &&
268                         (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
269                 return -EINVAL;
270
271         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
272         if (memzone == NULL)
273                 return -EIO;
274
275         stats = memzone->addr;
276         rte_spinlock_lock(&stats->lock);
277
278         if (values != NULL) {
279                 if (capacity < stats->cnt_stats) {
280                         return_value = stats->cnt_stats;
281                         rte_spinlock_unlock(&stats->lock);
282                         return return_value;
283                 }
284                 if (port_id == RTE_METRICS_GLOBAL)
285                         for (idx_name = 0;
286                                         idx_name < stats->cnt_stats;
287                                         idx_name++) {
288                                 entry = &stats->metadata[idx_name];
289                                 values[idx_name].key = idx_name;
290                                 values[idx_name].value = entry->global_value;
291                         }
292                 else
293                         for (idx_name = 0;
294                                         idx_name < stats->cnt_stats;
295                                         idx_name++) {
296                                 entry = &stats->metadata[idx_name];
297                                 values[idx_name].key = idx_name;
298                                 values[idx_name].value = entry->value[port_id];
299                         }
300         }
301         return_value = stats->cnt_stats;
302         rte_spinlock_unlock(&stats->lock);
303         return return_value;
304 }