1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2017 Intel Corporation
*/
#include <string.h>
#include <sys/queue.h>
#include <rte_common.h>
#include <rte_malloc.h>
#include <rte_metrics.h>
#include <rte_lcore.h>
#include <rte_memzone.h>
#include <rte_spinlock.h>
#define RTE_METRICS_MAX_METRICS 256
#define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
/**
* Internal stats metadata and value entry.
*
* @internal
*/
struct rte_metrics_meta_s {
/** Name of metric */
char name[RTE_METRICS_MAX_NAME_LEN];
/** Current value for metric */
uint64_t value[RTE_MAX_ETHPORTS];
/** Used for global metrics */
uint64_t global_value;
/** Index of next root element (zero for none) */
uint16_t idx_next_set;
/** Index of next metric in set (zero for none) */
uint16_t idx_next_stat;
};
/**
* Internal stats info structure.
*
* @internal
* Offsets into metadata are used instead of pointers because ASLR
* means that having the same physical addresses in different
* processes is not guaranteed.
*/
struct rte_metrics_data_s {
/** Index of last metadata entry with valid data.
* This value is not valid if cnt_stats is zero.
*/
uint16_t idx_last_set;
/** Number of metrics. */
uint16_t cnt_stats;
/** Metric data memory block. */
struct rte_metrics_meta_s metadata[RTE_METRICS_MAX_METRICS];
/** Metric data access lock */
rte_spinlock_t lock;
};
void
rte_metrics_init(int socket_id)
{
struct rte_metrics_data_s *stats;
const struct rte_memzone *memzone;
if (rte_eal_process_type() != RTE_PROC_PRIMARY)
return;
memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
if (memzone != NULL)
return;
memzone = rte_memzone_reserve(RTE_METRICS_MEMZONE_NAME,
sizeof(struct rte_metrics_data_s), socket_id, 0);
if (memzone == NULL)
rte_exit(EXIT_FAILURE, "Unable to allocate stats memzone\n");
stats = memzone->addr;
memset(stats, 0, sizeof(struct rte_metrics_data_s));
rte_spinlock_init(&stats->lock);
}
int
rte_metrics_reg_name(const char *name)
{
const char * const list_names[] = {name};
return rte_metrics_reg_names(list_names, 1);
}
int
rte_metrics_reg_names(const char * const *names, uint16_t cnt_names)
{
struct rte_metrics_meta_s *entry = NULL;
struct rte_metrics_data_s *stats;
const struct rte_memzone *memzone;
uint16_t idx_name;
uint16_t idx_base;
/* Some sanity checks */
if (cnt_names < 1 || names == NULL)
return -EINVAL;
memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
if (memzone == NULL)
return -EIO;
stats = memzone->addr;
if (stats->cnt_stats + cnt_names >= RTE_METRICS_MAX_METRICS)
return -ENOMEM;
rte_spinlock_lock(&stats->lock);
/* Overwritten later if this is actually first set.. */
stats->metadata[stats->idx_last_set].idx_next_set = stats->cnt_stats;
stats->idx_last_set = idx_base = stats->cnt_stats;
for (idx_name = 0; idx_name < cnt_names; idx_name++) {
entry = &stats->metadata[idx_name + stats->cnt_stats];
strncpy(entry->name, names[idx_name],
RTE_METRICS_MAX_NAME_LEN);
/* Enforce NULL-termination */
entry->name[RTE_METRICS_MAX_NAME_LEN - 1] = '\0';
memset(entry->value, 0, sizeof(entry->value));
entry->idx_next_stat = idx_name + stats->cnt_stats + 1;
}
entry->idx_next_stat = 0;
entry->idx_next_set = 0;
stats->cnt_stats += cnt_names;
rte_spinlock_unlock(&stats->lock);
return idx_base;
}
int
rte_metrics_update_value(int port_id, uint16_t key, const uint64_t value)
{
return rte_metrics_update_values(port_id, key, &value, 1);
}
int
rte_metrics_update_values(int port_id,
uint16_t key,
const uint64_t *values,
uint32_t count)
{
struct rte_metrics_meta_s *entry;
struct rte_metrics_data_s *stats;
const struct rte_memzone *memzone;
uint16_t idx_metric;
uint16_t idx_value;
uint16_t cnt_setsize;
if (port_id != RTE_METRICS_GLOBAL &&
(port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
return -EINVAL;
if (values == NULL)
return -EINVAL;
memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
if (memzone == NULL)
return -EIO;
stats = memzone->addr;
rte_spinlock_lock(&stats->lock);
idx_metric = key;
cnt_setsize = 1;
while (idx_metric < stats->cnt_stats) {
entry = &stats->metadata[idx_metric];
if (entry->idx_next_stat == 0)
break;
cnt_setsize++;
idx_metric++;
}
/* Check update does not cross set border */
if (count > cnt_setsize) {
rte_spinlock_unlock(&stats->lock);
return -ERANGE;
}
if (port_id == RTE_METRICS_GLOBAL)
for (idx_value = 0; idx_value < count; idx_value++) {
idx_metric = key + idx_value;
stats->metadata[idx_metric].global_value =
values[idx_value];
}
else
for (idx_value = 0; idx_value < count; idx_value++) {
idx_metric = key + idx_value;
stats->metadata[idx_metric].value[port_id] =
values[idx_value];
}
rte_spinlock_unlock(&stats->lock);
return 0;
}
int
rte_metrics_get_names(struct rte_metric_name *names,
uint16_t capacity)
{
struct rte_metrics_data_s *stats;
const struct rte_memzone *memzone;
uint16_t idx_name;
int return_value;
memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
/* If not allocated, fail silently */
if (memzone == NULL)
return 0;
stats = memzone->addr;
rte_spinlock_lock(&stats->lock);
if (names != NULL) {
if (capacity < stats->cnt_stats) {
return_value = stats->cnt_stats;
rte_spinlock_unlock(&stats->lock);
return return_value;
}
for (idx_name = 0; idx_name < stats->cnt_stats; idx_name++)
strncpy(names[idx_name].name,
stats->metadata[idx_name].name,
RTE_METRICS_MAX_NAME_LEN);
}
return_value = stats->cnt_stats;
rte_spinlock_unlock(&stats->lock);
return return_value;
}
int
rte_metrics_get_values(int port_id,
struct rte_metric_value *values,
uint16_t capacity)
{
struct rte_metrics_meta_s *entry;
struct rte_metrics_data_s *stats;
const struct rte_memzone *memzone;
uint16_t idx_name;
int return_value;
if (port_id != RTE_METRICS_GLOBAL &&
(port_id < 0 || port_id >= RTE_MAX_ETHPORTS))
return -EINVAL;
memzone = rte_memzone_lookup(RTE_METRICS_MEMZONE_NAME);
/* If not allocated, fail silently */
if (memzone == NULL)
return 0;
stats = memzone->addr;
rte_spinlock_lock(&stats->lock);
if (values != NULL) {
if (capacity < stats->cnt_stats) {
return_value = stats->cnt_stats;
rte_spinlock_unlock(&stats->lock);
return return_value;
}
if (port_id == RTE_METRICS_GLOBAL)
for (idx_name = 0;
idx_name < stats->cnt_stats;
idx_name++) {
entry = &stats->metadata[idx_name];
values[idx_name].key = idx_name;
values[idx_name].value = entry->global_value;
}
else
for (idx_name = 0;
idx_name < stats->cnt_stats;
idx_name++) {
entry = &stats->metadata[idx_name];
values[idx_name].key = idx_name;
values[idx_name].value = entry->value[port_id];
}
}
return_value = stats->cnt_stats;
rte_spinlock_unlock(&stats->lock);
return return_value;
}
|