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