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