mempool: fix alignment of memzone length when populating
[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_reg_name(const char *name)
81 {
82         const char * const list_names[] = {name};
83
84         return rte_metrics_reg_names(list_names, 1);
85 }
86
87 int
88 rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
89 {
90         struct rte_metrics_meta_s *entry = NULL;
91         struct rte_metrics_data_s *stats;
92         const struct rte_memzone *memzone;
93         uint16_t idx_name;
94         uint16_t idx_base;
95
96         /* Some sanity checks */
97         if (cnt_names < 1 || names == NULL)
98                 return -EINVAL;
99
100         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
101         if (memzone == NULL)
102                 return -EIO;
103         stats = memzone->addr;
104
105         if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
106                 return -ENOMEM;
107
108         rte_spinlock_lock(&stats->lock);
109
110         /* Overwritten later if this is actually first set.. */
111         stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
112
113         stats->idx_last_set = idx_base = stats->cnt_stats;
114
115         for (idx_name = 0; idx_name < cnt_names; idx_name++) {
116                 entry = &stats->metadata[idx_name + stats->cnt_stats];
117                 strlcpy(entry->name, names[idx_name], RTE_METRICS_MAX_NAME_LEN);
118                 memset(entry->value, 0, sizeof(entry->value));
119                 entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
120         }
121         entry->idx_next_stat = 0;
122         entry->idx_next_set = 0;
123         stats->cnt_stats += cnt_names;
124
125         rte_spinlock_unlock(&stats->lock);
126
127         return idx_base;
128 }
129
130 int
131 rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
132 {
133         return rte_metrics_update_values(port_id, key, &value, 1);
134 }
135
136 int
137 rte_metrics_update_values(int port_id,
138         uint16_t key,
139         const uint64_t *values,
140         uint32_t count)
141 {
142         struct rte_metrics_meta_s *entry;
143         struct rte_metrics_data_s *stats;
144         const struct rte_memzone *memzone;
145         uint16_t idx_metric;
146         uint16_t idx_value;
147         uint16_t cnt_setsize;
148
149         if (port_id != RTE_METRICS_GLOBAL &&
150                         (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
151                 return -EINVAL;
152
153         if (values == NULL)
154                 return -EINVAL;
155
156         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
157         if (memzone == NULL)
158                 return -EIO;
159         stats = memzone->addr;
160
161         rte_spinlock_lock(&stats->lock);
162         idx_metric = key;
163         cnt_setsize = 1;
164         while (idx_metric < stats->cnt_stats) {
165                 entry = &stats->metadata[idx_metric];
166                 if (entry->idx_next_stat == 0)
167                         break;
168                 cnt_setsize++;
169                 idx_metric++;
170         }
171         /* Check update does not cross set border */
172         if (count > cnt_setsize) {
173                 rte_spinlock_unlock(&stats->lock);
174                 return -ERANGE;
175         }
176
177         if (port_id == RTE_METRICS_GLOBAL)
178                 for (idx_value = 0; idx_value < count; idx_value++) {
179                         idx_metric = key + idx_value;
180                         stats->metadata[idx_metric].global_value =
181                                 values[idx_value];
182                 }
183         else
184                 for (idx_value = 0; idx_value < count; idx_value++) {
185                         idx_metric = key + idx_value;
186                         stats->metadata[idx_metric].value[port_id] =
187                                 values[idx_value];
188                 }
189         rte_spinlock_unlock(&stats->lock);
190         return 0;
191 }
192
193 int
194 rte_metrics_get_names(struct rte_metric_name *names,
195         uint16_t capacity)
196 {
197         struct rte_metrics_data_s *stats;
198         const struct rte_memzone *memzone;
199         uint16_t idx_name;
200         int return_value;
201
202         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
203         /* If not allocated, fail silently */
204         if (memzone == NULL)
205                 return 0;
206
207         stats = memzone->addr;
208         rte_spinlock_lock(&stats->lock);
209         if (names != NULL) {
210                 if (capacity < stats->cnt_stats) {
211                         return_value = stats->cnt_stats;
212                         rte_spinlock_unlock(&stats->lock);
213                         return return_value;
214                 }
215                 for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
216                         strlcpy(names[idx_name].name,
217                                 stats->metadata[idx_name].name,
218                                 RTE_METRICS_MAX_NAME_LEN);
219         }
220         return_value = stats->cnt_stats;
221         rte_spinlock_unlock(&stats->lock);
222         return return_value;
223 }
224
225 int
226 rte_metrics_get_values(int port_id,
227         struct rte_metric_value *values,
228         uint16_t capacity)
229 {
230         struct rte_metrics_meta_s *entry;
231         struct rte_metrics_data_s *stats;
232         const struct rte_memzone *memzone;
233         uint16_t idx_name;
234         int return_value;
235
236         if (port_id != RTE_METRICS_GLOBAL &&
237                         (port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
238                 return -EINVAL;
239
240         memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
241         /* If not allocated, fail silently */
242         if (memzone == NULL)
243                 return 0;
244         stats = memzone->addr;
245         rte_spinlock_lock(&stats->lock);
246
247         if (values != NULL) {
248                 if (capacity < stats->cnt_stats) {
249                         return_value = stats->cnt_stats;
250                         rte_spinlock_unlock(&stats->lock);
251                         return return_value;
252                 }
253                 if (port_id == RTE_METRICS_GLOBAL)
254                         for (idx_name = 0;
255                                         idx_name < stats->cnt_stats;
256                                         idx_name++) {
257                                 entry = &stats->metadata[idx_name];
258                                 values[idx_name].key = idx_name;
259                                 values[idx_name].value = entry->global_value;
260                         }
261                 else
262                         for (idx_name = 0;
263                                         idx_name < stats->cnt_stats;
264                                         idx_name++) {
265                                 entry = &stats->metadata[idx_name];
266                                 values[idx_name].key = idx_name;
267                                 values[idx_name].value = entry->value[port_id];
268                         }
269         }
270         return_value = stats->cnt_stats;
271         rte_spinlock_unlock(&stats->lock);
272         return return_value;
273 }