Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Intel Corporation
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <math.h>
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : : #include <stdlib.h>
10 : : #include <string.h>
11 : :
12 : : #include <eal_export.h>
13 : : #include <rte_cycles.h>
14 : : #include <rte_eal.h>
15 : : #include <rte_errno.h>
16 : : #include <rte_ethdev.h>
17 : : #include <rte_lcore.h>
18 : : #include <rte_log.h>
19 : : #include <rte_mbuf.h>
20 : : #include <rte_mbuf_dyn.h>
21 : : #include <rte_memzone.h>
22 : : #include <rte_metrics.h>
23 : : #include <rte_spinlock.h>
24 : : #include <rte_string_fns.h>
25 : : #include <rte_stdatomic.h>
26 : :
27 : : #include "rte_latencystats.h"
28 : :
29 : : /** Nano seconds per second */
30 : : #define NS_PER_SEC 1E9
31 : :
32 : : static double cycles_per_ns;
33 : :
34 [ - + ]: 305 : static RTE_LOG_REGISTER_DEFAULT(latencystat_logtype, INFO);
35 : : #define RTE_LOGTYPE_LATENCY_STATS latencystat_logtype
36 : : #define LATENCY_STATS_LOG(level, ...) \
37 : : RTE_LOG_LINE(level, LATENCY_STATS, "" __VA_ARGS__)
38 : :
39 : : static uint64_t timestamp_dynflag;
40 : : static int timestamp_dynfield_offset = -1;
41 : :
42 : : static inline rte_mbuf_timestamp_t *
43 : : timestamp_dynfield(struct rte_mbuf *mbuf)
44 : : {
45 : 99955 : return RTE_MBUF_DYNFIELD(mbuf,
46 : : timestamp_dynfield_offset, rte_mbuf_timestamp_t *);
47 : : }
48 : :
49 : : /* Compare two 64 bit timer counter but deal with wraparound correctly. */
50 : : static inline bool tsc_after(uint64_t t0, uint64_t t1)
51 : : {
52 : 10000 : return (int64_t)(t1 - t0) < 0;
53 : : }
54 : :
55 : : #define tsc_before(a, b) tsc_after(b, a)
56 : :
57 : : static const char *MZ_RTE_LATENCY_STATS = "rte_latencystats";
58 : : static int latency_stats_index;
59 : :
60 : : static rte_spinlock_t sample_lock = RTE_SPINLOCK_INITIALIZER;
61 : : static uint64_t samp_intvl;
62 : : static RTE_ATOMIC(uint64_t) next_tsc;
63 : :
64 : : #define LATENCY_AVG_SCALE 4
65 : : #define LATENCY_JITTER_SCALE 16
66 : :
67 : : struct rte_latency_stats {
68 : : uint64_t min_latency; /**< Minimum latency */
69 : : uint64_t avg_latency; /**< Average latency */
70 : : uint64_t max_latency; /**< Maximum latency */
71 : : uint64_t jitter; /** Latency variation */
72 : : uint64_t samples; /** Number of latency samples */
73 : : rte_spinlock_t lock; /** Latency calculation lock */
74 : : };
75 : :
76 : : static struct rte_latency_stats *glob_stats;
77 : :
78 : : struct rxtx_cbs {
79 : : const struct rte_eth_rxtx_callback *cb;
80 : : };
81 : :
82 : : static struct rxtx_cbs rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
83 : : static struct rxtx_cbs tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
84 : :
85 : : struct latency_stats_nameoff {
86 : : char name[RTE_ETH_XSTATS_NAME_SIZE];
87 : : unsigned int offset;
88 : : unsigned int scale;
89 : : };
90 : :
91 : : static const struct latency_stats_nameoff lat_stats_strings[] = {
92 : : {"min_latency_ns", offsetof(struct rte_latency_stats, min_latency), 1},
93 : : {"avg_latency_ns", offsetof(struct rte_latency_stats, avg_latency), LATENCY_AVG_SCALE},
94 : : {"max_latency_ns", offsetof(struct rte_latency_stats, max_latency), 1},
95 : : {"jitter_ns", offsetof(struct rte_latency_stats, jitter), LATENCY_JITTER_SCALE},
96 : : {"samples", offsetof(struct rte_latency_stats, samples), 0},
97 : : };
98 : :
99 : : #define NUM_LATENCY_STATS RTE_DIM(lat_stats_strings)
100 : :
101 : : static void
102 : 4 : latencystats_collect(uint64_t values[])
103 : : {
104 : : unsigned int i, scale;
105 : : const uint64_t *stats;
106 : :
107 [ + - ]: 4 : if (glob_stats == NULL)
108 : : return;
109 : :
110 [ + + ]: 24 : for (i = 0; i < NUM_LATENCY_STATS; i++) {
111 : 20 : stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
112 : 20 : scale = lat_stats_strings[i].scale;
113 : :
114 : : /* used to mark samples which are not a time interval */
115 [ + + ]: 20 : if (scale == 0)
116 : 4 : values[i] = *stats;
117 : : else
118 : 16 : values[i] = floor(*stats / (cycles_per_ns * scale));
119 : : }
120 : : }
121 : :
122 : : RTE_EXPORT_SYMBOL(rte_latencystats_update)
123 : : int32_t
124 : 1 : rte_latencystats_update(void)
125 : : {
126 : : uint64_t values[NUM_LATENCY_STATS];
127 : : int ret;
128 : :
129 : 1 : latencystats_collect(values);
130 : :
131 : 1 : ret = rte_metrics_update_values(RTE_METRICS_GLOBAL,
132 : : latency_stats_index,
133 : : values, NUM_LATENCY_STATS);
134 [ - + ]: 1 : if (ret < 0)
135 : 0 : LATENCY_STATS_LOG(INFO, "Failed to push the stats");
136 : :
137 : 1 : return ret;
138 : : }
139 : :
140 : : static void
141 : 3 : rte_latencystats_fill_values(struct rte_metric_value *metrics)
142 : : {
143 : : uint64_t values[NUM_LATENCY_STATS];
144 : : unsigned int i;
145 : :
146 : 3 : latencystats_collect(values);
147 : :
148 [ + + ]: 18 : for (i = 0; i < NUM_LATENCY_STATS; i++) {
149 : 15 : metrics[i].key = i;
150 : 15 : metrics[i].value = values[i];
151 : : }
152 : 3 : }
153 : :
154 : : static uint16_t
155 : 10000 : add_time_stamps(uint16_t pid __rte_unused,
156 : : uint16_t qid __rte_unused,
157 : : struct rte_mbuf **pkts,
158 : : uint16_t nb_pkts,
159 : : uint16_t max_pkts __rte_unused,
160 : : void *user_cb __rte_unused)
161 : : {
162 : : unsigned int i;
163 : : uint64_t now = rte_rdtsc();
164 : :
165 : : /* Check without locking */
166 [ + - ]: 10000 : if (likely(tsc_before(now, rte_atomic_load_explicit(&next_tsc,
167 : : rte_memory_order_relaxed))))
168 : : return nb_pkts;
169 : :
170 : : /* Try and get sample, skip if sample is being done by other core. */
171 [ + - ]: 10000 : if (likely(rte_spinlock_trylock(&sample_lock))) {
172 [ + + ]: 109945 : for (i = 0; i < nb_pkts; i++) {
173 : 99955 : struct rte_mbuf *m = pkts[i];
174 : :
175 : : /* skip if already timestamped */
176 [ + + ]: 99955 : if (unlikely(m->ol_flags & timestamp_dynflag))
177 : : continue;
178 : :
179 : 10 : m->ol_flags |= timestamp_dynflag;
180 : 10 : *timestamp_dynfield(m) = now;
181 : 10 : rte_atomic_store_explicit(&next_tsc, now + samp_intvl,
182 : : rte_memory_order_relaxed);
183 : 10 : break;
184 : : }
185 : : rte_spinlock_unlock(&sample_lock);
186 : : }
187 : :
188 : : return nb_pkts;
189 : : }
190 : :
191 : : static uint16_t
192 : 10000 : calc_latency(uint16_t pid __rte_unused,
193 : : uint16_t qid __rte_unused,
194 : : struct rte_mbuf **pkts,
195 : : uint16_t nb_pkts,
196 : : void *_ __rte_unused)
197 : : {
198 : : unsigned int i;
199 : : uint64_t now, latency;
200 : : uint64_t ts_flags = 0;
201 : : static uint64_t prev_latency;
202 : :
203 [ + + ]: 110000 : for (i = 0; i < nb_pkts; i++)
204 : 100000 : ts_flags |= (pkts[i]->ol_flags & timestamp_dynflag);
205 : :
206 : : /* no samples in this burst, skip locking */
207 [ + + ]: 10000 : if (likely(ts_flags == 0))
208 : : return nb_pkts;
209 : :
210 : : now = rte_rdtsc();
211 : 9999 : rte_spinlock_lock(&glob_stats->lock);
212 [ + + ]: 109989 : for (i = 0; i < nb_pkts; i++) {
213 [ + + ]: 99990 : if (!(pkts[i]->ol_flags & timestamp_dynflag))
214 : 45 : continue;
215 : :
216 : 99945 : latency = now - *timestamp_dynfield(pkts[i]);
217 : :
218 [ + + ]: 99945 : if (glob_stats->samples++ == 0) {
219 : 1 : glob_stats->min_latency = latency;
220 : 1 : glob_stats->max_latency = latency;
221 : 1 : glob_stats->avg_latency = latency * 4;
222 : : /* start ad if previous sample had 0 latency */
223 : 1 : glob_stats->jitter = latency / LATENCY_JITTER_SCALE;
224 : : } else {
225 : : /*
226 : : * The jitter is calculated as statistical mean of interpacket
227 : : * delay variation. The "jitter estimate" is computed by taking
228 : : * the absolute values of the ipdv sequence and applying an
229 : : * exponential filter with parameter 1/16 to generate the
230 : : * estimate. i.e J=J+(|D(i-1,i)|-J)/16. Where J is jitter,
231 : : * D(i-1,i) is difference in latency of two consecutive packets
232 : : * i-1 and i. Jitter is scaled by 16.
233 : : * Reference: Calculated as per RFC 5481, sec 4.1,
234 : : * RFC 3393 sec 4.5, RFC 1889 sec.
235 : : */
236 : 99944 : long long delta = prev_latency - latency;
237 : 99944 : glob_stats->jitter += llabs(delta)
238 : 99944 : - glob_stats->jitter / LATENCY_JITTER_SCALE;
239 : :
240 [ + + ]: 99944 : if (latency < glob_stats->min_latency)
241 : 3 : glob_stats->min_latency = latency;
242 [ + + ]: 99944 : if (latency > glob_stats->max_latency)
243 : 9998 : glob_stats->max_latency = latency;
244 : : /*
245 : : * The average latency is measured using exponential moving
246 : : * average, i.e. using EWMA
247 : : * https://en.wikipedia.org/wiki/Moving_average
248 : : *
249 : : * Alpha is .25, avg_latency is scaled by 4.
250 : : */
251 : 99944 : glob_stats->avg_latency += latency
252 : 99944 : - glob_stats->avg_latency / LATENCY_AVG_SCALE;
253 : : }
254 : :
255 : 99945 : prev_latency = latency;
256 : : }
257 : 9999 : rte_spinlock_unlock(&glob_stats->lock);
258 : :
259 : 9999 : return nb_pkts;
260 : : }
261 : :
262 : : RTE_EXPORT_SYMBOL(rte_latencystats_init)
263 : : int
264 : 1 : rte_latencystats_init(uint64_t app_samp_intvl,
265 : : rte_latency_stats_flow_type_fn user_cb)
266 : : {
267 : : unsigned int i;
268 : : uint16_t pid;
269 : : uint16_t qid;
270 : : struct rxtx_cbs *cbs = NULL;
271 : 1 : const char *ptr_strings[NUM_LATENCY_STATS] = {0};
272 : : const struct rte_memzone *mz = NULL;
273 : : const unsigned int flags = 0;
274 : : int ret;
275 : :
276 [ + - ]: 1 : if (rte_memzone_lookup(MZ_RTE_LATENCY_STATS))
277 : : return -EEXIST;
278 : :
279 : : /** Reserved for possible future use */
280 [ + - ]: 1 : if (user_cb != NULL)
281 : : return -ENOTSUP;
282 : :
283 : : /** Allocate stats in shared memory fo multi process support */
284 : 1 : mz = rte_memzone_reserve(MZ_RTE_LATENCY_STATS, sizeof(*glob_stats),
285 : 1 : rte_socket_id(), flags);
286 [ - + ]: 1 : if (mz == NULL) {
287 : 0 : LATENCY_STATS_LOG(ERR, "Cannot reserve memory: %s:%d",
288 : : __func__, __LINE__);
289 : 0 : return -ENOMEM;
290 : : }
291 : :
292 : 1 : cycles_per_ns = (double)rte_get_tsc_hz() / NS_PER_SEC;
293 : :
294 : 1 : glob_stats = mz->addr;
295 : : rte_spinlock_init(&glob_stats->lock);
296 : 1 : samp_intvl = (uint64_t)(app_samp_intvl * cycles_per_ns);
297 : 1 : next_tsc = rte_rdtsc();
298 : :
299 : : /** Register latency stats with stats library */
300 [ + + ]: 6 : for (i = 0; i < NUM_LATENCY_STATS; i++)
301 : 5 : ptr_strings[i] = lat_stats_strings[i].name;
302 : :
303 : 1 : latency_stats_index = rte_metrics_reg_names(ptr_strings,
304 : : NUM_LATENCY_STATS);
305 [ - + ]: 1 : if (latency_stats_index < 0) {
306 : 0 : LATENCY_STATS_LOG(ERR,
307 : : "Failed to register latency stats names");
308 : 0 : return -1;
309 : : }
310 : :
311 : : /* Register mbuf field and flag for Rx timestamp */
312 : 1 : ret = rte_mbuf_dyn_rx_timestamp_register(×tamp_dynfield_offset,
313 : : ×tamp_dynflag);
314 [ - + ]: 1 : if (ret != 0) {
315 : 0 : LATENCY_STATS_LOG(ERR,
316 : : "Cannot register mbuf field/flag for timestamp");
317 : 0 : return -rte_errno;
318 : : }
319 : :
320 : : /** Register Rx/Tx callbacks */
321 [ + + ]: 2 : RTE_ETH_FOREACH_DEV(pid) {
322 : : struct rte_eth_dev_info dev_info;
323 : :
324 : 1 : ret = rte_eth_dev_info_get(pid, &dev_info);
325 [ - + ]: 1 : if (ret != 0) {
326 : 0 : LATENCY_STATS_LOG(NOTICE,
327 : : "Can not get info for device (port %u): %s",
328 : : pid, strerror(-ret));
329 : :
330 : 0 : continue;
331 : : }
332 : :
333 [ + + ]: 2 : for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
334 : 1 : cbs = &rx_cbs[pid][qid];
335 : 1 : cbs->cb = rte_eth_add_first_rx_callback(pid, qid,
336 : : add_time_stamps, NULL);
337 [ - + ]: 1 : if (!cbs->cb)
338 : 0 : LATENCY_STATS_LOG(NOTICE,
339 : : "Failed to register Rx callback for pid=%u, qid=%u",
340 : : pid, qid);
341 : : }
342 [ + + ]: 2 : for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
343 : 1 : cbs = &tx_cbs[pid][qid];
344 : 1 : cbs->cb = rte_eth_add_tx_callback(pid, qid,
345 : : calc_latency, NULL);
346 [ - + ]: 1 : if (!cbs->cb)
347 : 0 : LATENCY_STATS_LOG(NOTICE,
348 : : "Failed to register Tx callback for pid=%u, qid=%u",
349 : : pid, qid);
350 : : }
351 : : }
352 : : return 0;
353 : : }
354 : :
355 : : RTE_EXPORT_SYMBOL(rte_latencystats_uninit)
356 : : int
357 : 1 : rte_latencystats_uninit(void)
358 : : {
359 : : uint16_t pid;
360 : : uint16_t qid;
361 : : int ret = 0;
362 : : struct rxtx_cbs *cbs = NULL;
363 : : const struct rte_memzone *mz = NULL;
364 : :
365 : : /** De register Rx/Tx callbacks */
366 [ + + ]: 2 : RTE_ETH_FOREACH_DEV(pid) {
367 : : struct rte_eth_dev_info dev_info;
368 : :
369 : 1 : ret = rte_eth_dev_info_get(pid, &dev_info);
370 [ + - ]: 1 : if (ret != 0) {
371 : 0 : LATENCY_STATS_LOG(NOTICE,
372 : : "Can not get info for device (port %u): %s",
373 : : pid, strerror(-ret));
374 : 0 : continue;
375 : : }
376 : :
377 [ + + ]: 2 : for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
378 : 1 : cbs = &rx_cbs[pid][qid];
379 : 1 : ret = rte_eth_remove_rx_callback(pid, qid, cbs->cb);
380 [ - + ]: 1 : if (ret)
381 : 0 : LATENCY_STATS_LOG(NOTICE,
382 : : "Failed to remove Rx callback for pid=%u, qid=%u",
383 : : pid, qid);
384 : : }
385 [ + + ]: 2 : for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
386 : 1 : cbs = &tx_cbs[pid][qid];
387 : 1 : ret = rte_eth_remove_tx_callback(pid, qid, cbs->cb);
388 [ - + ]: 1 : if (ret)
389 : 0 : LATENCY_STATS_LOG(NOTICE,
390 : : "Failed to remove Tx callback for pid=%u, qid=%u",
391 : : pid, qid);
392 : : }
393 : : }
394 : :
395 : : /* free up the memzone */
396 : 1 : mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
397 : 1 : rte_memzone_free(mz);
398 : :
399 : 1 : return 0;
400 : : }
401 : :
402 : : RTE_EXPORT_SYMBOL(rte_latencystats_get_names)
403 : : int
404 : 4 : rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)
405 : : {
406 : : unsigned int i;
407 : :
408 [ + + ]: 4 : if (names == NULL || size < NUM_LATENCY_STATS)
409 : : return NUM_LATENCY_STATS;
410 : :
411 [ + + ]: 12 : for (i = 0; i < NUM_LATENCY_STATS; i++) {
412 [ - + ]: 10 : if (strlcpy(names[i].name, lat_stats_strings[i].name, sizeof(names[0].name))
413 : : >= sizeof(names[0].name))
414 : 0 : LATENCY_STATS_LOG(NOTICE, "Latency metric '%s' too long",
415 : : lat_stats_strings[i].name);
416 : : }
417 : :
418 : : return NUM_LATENCY_STATS;
419 : : }
420 : :
421 : : RTE_EXPORT_SYMBOL(rte_latencystats_get)
422 : : int
423 : 5 : rte_latencystats_get(struct rte_metric_value *values, uint16_t size)
424 : : {
425 [ + + ]: 5 : if (size < NUM_LATENCY_STATS || values == NULL)
426 : : return NUM_LATENCY_STATS;
427 : :
428 [ - + ]: 3 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
429 : : const struct rte_memzone *mz;
430 : 0 : mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
431 [ # # ]: 0 : if (mz == NULL) {
432 : 0 : LATENCY_STATS_LOG(ERR,
433 : : "Latency stats memzone not found");
434 : 0 : return -ENOMEM;
435 : : }
436 : 0 : glob_stats = mz->addr;
437 : : }
438 : :
439 : : /* Retrieve latency stats */
440 : 3 : rte_latencystats_fill_values(values);
441 : :
442 : 3 : return NUM_LATENCY_STATS;
443 : : }
|