1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <rte_string_fns.h>
13 #include <rte_cycles.h>
14 #include <rte_ethdev.h>
15 #include <rte_metrics.h>
16 #include <rte_memzone.h>
17 #include <rte_lcore.h>
19 #include "rte_latencystats.h"
21 /** Nano seconds per second */
22 #define NS_PER_SEC 1E9
24 /** Clock cycles per nano second */
26 latencystat_cycles_per_ns(void)
28 return rte_get_timer_hz() / NS_PER_SEC;
31 /* Macros for printing using RTE_LOG */
32 #define RTE_LOGTYPE_LATENCY_STATS RTE_LOGTYPE_USER1
34 static const char *MZ_RTE_LATENCY_STATS = "rte_latencystats";
35 static int latency_stats_index;
36 static uint64_t samp_intvl;
37 static uint64_t timer_tsc;
38 static uint64_t prev_tsc;
40 struct rte_latency_stats {
41 float min_latency; /**< Minimum latency in nano seconds */
42 float avg_latency; /**< Average latency in nano seconds */
43 float max_latency; /**< Maximum latency in nano seconds */
44 float jitter; /** Latency variation */
47 static struct rte_latency_stats *glob_stats;
50 const struct rte_eth_rxtx_callback *cb;
53 static struct rxtx_cbs rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
54 static struct rxtx_cbs tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
56 struct latency_stats_nameoff {
57 char name[RTE_ETH_XSTATS_NAME_SIZE];
61 static const struct latency_stats_nameoff lat_stats_strings[] = {
62 {"min_latency_ns", offsetof(struct rte_latency_stats, min_latency)},
63 {"avg_latency_ns", offsetof(struct rte_latency_stats, avg_latency)},
64 {"max_latency_ns", offsetof(struct rte_latency_stats, max_latency)},
65 {"jitter_ns", offsetof(struct rte_latency_stats, jitter)},
68 #define NUM_LATENCY_STATS (sizeof(lat_stats_strings) / \
69 sizeof(lat_stats_strings[0]))
72 rte_latencystats_update(void)
75 float *stats_ptr = NULL;
76 uint64_t values[NUM_LATENCY_STATS] = {0};
79 for (i = 0; i < NUM_LATENCY_STATS; i++) {
80 stats_ptr = RTE_PTR_ADD(glob_stats,
81 lat_stats_strings[i].offset);
82 values[i] = (uint64_t)floor((*stats_ptr)/
83 latencystat_cycles_per_ns());
86 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL,
88 values, NUM_LATENCY_STATS);
90 RTE_LOG(INFO, LATENCY_STATS, "Failed to push the stats\n");
96 rte_latencystats_fill_values(struct rte_metric_value *values)
99 float *stats_ptr = NULL;
101 for (i = 0; i < NUM_LATENCY_STATS; i++) {
102 stats_ptr = RTE_PTR_ADD(glob_stats,
103 lat_stats_strings[i].offset);
105 values[i].value = (uint64_t)floor((*stats_ptr)/
106 latencystat_cycles_per_ns());
111 add_time_stamps(uint16_t pid __rte_unused,
112 uint16_t qid __rte_unused,
113 struct rte_mbuf **pkts,
115 uint16_t max_pkts __rte_unused,
116 void *user_cb __rte_unused)
119 uint64_t diff_tsc, now;
122 * For every sample interval,
123 * time stamp is marked on one received packet.
126 for (i = 0; i < nb_pkts; i++) {
127 diff_tsc = now - prev_tsc;
128 timer_tsc += diff_tsc;
130 if ((pkts[i]->ol_flags & PKT_RX_TIMESTAMP) == 0
131 && (timer_tsc >= samp_intvl)) {
132 pkts[i]->timestamp = now;
133 pkts[i]->ol_flags |= PKT_RX_TIMESTAMP;
144 calc_latency(uint16_t pid __rte_unused,
145 uint16_t qid __rte_unused,
146 struct rte_mbuf **pkts,
148 void *_ __rte_unused)
150 unsigned int i, cnt = 0;
152 float latency[nb_pkts];
153 static float prev_latency;
155 * Alpha represents degree of weighting decrease in EWMA,
156 * a constant smoothing factor between 0 and 1. The value
157 * is used below for measuring average latency.
159 const float alpha = 0.2;
162 for (i = 0; i < nb_pkts; i++) {
163 if (pkts[i]->ol_flags & PKT_RX_TIMESTAMP)
164 latency[cnt++] = now - pkts[i]->timestamp;
167 for (i = 0; i < cnt; i++) {
169 * The jitter is calculated as statistical mean of interpacket
170 * delay variation. The "jitter estimate" is computed by taking
171 * the absolute values of the ipdv sequence and applying an
172 * exponential filter with parameter 1/16 to generate the
173 * estimate. i.e J=J+(|D(i-1,i)|-J)/16. Where J is jitter,
174 * D(i-1,i) is difference in latency of two consecutive packets
176 * Reference: Calculated as per RFC 5481, sec 4.1,
177 * RFC 3393 sec 4.5, RFC 1889 sec.
179 glob_stats->jitter += (fabsf(prev_latency - latency[i])
180 - glob_stats->jitter)/16;
181 if (glob_stats->min_latency == 0)
182 glob_stats->min_latency = latency[i];
183 else if (latency[i] < glob_stats->min_latency)
184 glob_stats->min_latency = latency[i];
185 else if (latency[i] > glob_stats->max_latency)
186 glob_stats->max_latency = latency[i];
188 * The average latency is measured using exponential moving
189 * average, i.e. using EWMA
190 * https://en.wikipedia.org/wiki/Moving_average
192 glob_stats->avg_latency +=
193 alpha * (latency[i] - glob_stats->avg_latency);
194 prev_latency = latency[i];
201 rte_latencystats_init(uint64_t app_samp_intvl,
202 rte_latency_stats_flow_type_fn user_cb)
207 struct rxtx_cbs *cbs = NULL;
208 const char *ptr_strings[NUM_LATENCY_STATS] = {0};
209 const struct rte_memzone *mz = NULL;
210 const unsigned int flags = 0;
213 if (rte_memzone_lookup(MZ_RTE_LATENCY_STATS))
216 /** Allocate stats in shared memory fo multi process support */
217 mz = rte_memzone_reserve(MZ_RTE_LATENCY_STATS, sizeof(*glob_stats),
218 rte_socket_id(), flags);
220 RTE_LOG(ERR, LATENCY_STATS, "Cannot reserve memory: %s:%d\n",
225 glob_stats = mz->addr;
226 samp_intvl = app_samp_intvl * latencystat_cycles_per_ns();
228 /** Register latency stats with stats library */
229 for (i = 0; i < NUM_LATENCY_STATS; i++)
230 ptr_strings[i] = lat_stats_strings[i].name;
232 latency_stats_index = rte_metrics_reg_names(ptr_strings,
234 if (latency_stats_index < 0) {
235 RTE_LOG(DEBUG, LATENCY_STATS,
236 "Failed to register latency stats names\n");
240 /** Register Rx/Tx callbacks */
241 RTE_ETH_FOREACH_DEV(pid) {
242 struct rte_eth_dev_info dev_info;
244 ret = rte_eth_dev_info_get(pid, &dev_info);
246 RTE_LOG(INFO, LATENCY_STATS,
247 "Error during getting device (port %u) info: %s\n",
248 pid, strerror(-ret));
253 for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
254 cbs = &rx_cbs[pid][qid];
255 cbs->cb = rte_eth_add_first_rx_callback(pid, qid,
256 add_time_stamps, user_cb);
258 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
259 "register Rx callback for pid=%d, "
260 "qid=%d\n", pid, qid);
262 for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
263 cbs = &tx_cbs[pid][qid];
264 cbs->cb = rte_eth_add_tx_callback(pid, qid,
265 calc_latency, user_cb);
267 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
268 "register Tx callback for pid=%d, "
269 "qid=%d\n", pid, qid);
276 rte_latencystats_uninit(void)
281 struct rxtx_cbs *cbs = NULL;
282 const struct rte_memzone *mz = NULL;
284 /** De register Rx/Tx callbacks */
285 RTE_ETH_FOREACH_DEV(pid) {
286 struct rte_eth_dev_info dev_info;
288 ret = rte_eth_dev_info_get(pid, &dev_info);
290 RTE_LOG(INFO, LATENCY_STATS,
291 "Error during getting device (port %u) info: %s\n",
292 pid, strerror(-ret));
297 for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
298 cbs = &rx_cbs[pid][qid];
299 ret = rte_eth_remove_rx_callback(pid, qid, cbs->cb);
301 RTE_LOG(INFO, LATENCY_STATS, "failed to "
302 "remove Rx callback for pid=%d, "
303 "qid=%d\n", pid, qid);
305 for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
306 cbs = &tx_cbs[pid][qid];
307 ret = rte_eth_remove_tx_callback(pid, qid, cbs->cb);
309 RTE_LOG(INFO, LATENCY_STATS, "failed to "
310 "remove Tx callback for pid=%d, "
311 "qid=%d\n", pid, qid);
315 /* free up the memzone */
316 mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
318 rte_memzone_free(mz);
324 rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)
328 if (names == NULL || size < NUM_LATENCY_STATS)
329 return NUM_LATENCY_STATS;
331 for (i = 0; i < NUM_LATENCY_STATS; i++)
332 strlcpy(names[i].name, lat_stats_strings[i].name,
333 sizeof(names[i].name));
335 return NUM_LATENCY_STATS;
339 rte_latencystats_get(struct rte_metric_value *values, uint16_t size)
341 if (size < NUM_LATENCY_STATS || values == NULL)
342 return NUM_LATENCY_STATS;
344 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
345 const struct rte_memzone *mz;
346 mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
348 RTE_LOG(ERR, LATENCY_STATS,
349 "Latency stats memzone not found\n");
352 glob_stats = mz->addr;
355 /* Retrieve latency stats */
356 rte_latencystats_fill_values(values);
358 return NUM_LATENCY_STATS;