1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
12 #include <rte_cycles.h>
13 #include <rte_ethdev.h>
14 #include <rte_metrics.h>
15 #include <rte_memzone.h>
16 #include <rte_lcore.h>
18 #include "rte_latencystats.h"
20 /** Nano seconds per second */
21 #define NS_PER_SEC 1E9
23 /** Clock cycles per nano second */
25 latencystat_cycles_per_ns(void)
27 return rte_get_timer_hz() / NS_PER_SEC;
30 /* Macros for printing using RTE_LOG */
31 #define RTE_LOGTYPE_LATENCY_STATS RTE_LOGTYPE_USER1
33 static const char *MZ_RTE_LATENCY_STATS = "rte_latencystats";
34 static int latency_stats_index;
35 static uint64_t samp_intvl;
36 static uint64_t timer_tsc;
37 static uint64_t prev_tsc;
39 struct rte_latency_stats {
40 float min_latency; /**< Minimum latency in nano seconds */
41 float avg_latency; /**< Average latency in nano seconds */
42 float max_latency; /**< Maximum latency in nano seconds */
43 float jitter; /** Latency variation */
46 static struct rte_latency_stats *glob_stats;
49 const struct rte_eth_rxtx_callback *cb;
52 static struct rxtx_cbs rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
53 static struct rxtx_cbs tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
55 struct latency_stats_nameoff {
56 char name[RTE_ETH_XSTATS_NAME_SIZE];
60 static const struct latency_stats_nameoff lat_stats_strings[] = {
61 {"min_latency_ns", offsetof(struct rte_latency_stats, min_latency)},
62 {"avg_latency_ns", offsetof(struct rte_latency_stats, avg_latency)},
63 {"max_latency_ns", offsetof(struct rte_latency_stats, max_latency)},
64 {"jitter_ns", offsetof(struct rte_latency_stats, jitter)},
67 #define NUM_LATENCY_STATS (sizeof(lat_stats_strings) / \
68 sizeof(lat_stats_strings[0]))
71 rte_latencystats_update(void)
74 float *stats_ptr = NULL;
75 uint64_t values[NUM_LATENCY_STATS] = {0};
78 for (i = 0; i < NUM_LATENCY_STATS; i++) {
79 stats_ptr = RTE_PTR_ADD(glob_stats,
80 lat_stats_strings[i].offset);
81 values[i] = (uint64_t)floor((*stats_ptr)/
82 latencystat_cycles_per_ns());
85 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL,
87 values, NUM_LATENCY_STATS);
89 RTE_LOG(INFO, LATENCY_STATS, "Failed to push the stats\n");
95 rte_latencystats_fill_values(struct rte_metric_value *values)
98 float *stats_ptr = NULL;
100 for (i = 0; i < NUM_LATENCY_STATS; i++) {
101 stats_ptr = RTE_PTR_ADD(glob_stats,
102 lat_stats_strings[i].offset);
104 values[i].value = (uint64_t)floor((*stats_ptr)/
105 latencystat_cycles_per_ns());
110 add_time_stamps(uint16_t pid __rte_unused,
111 uint16_t qid __rte_unused,
112 struct rte_mbuf **pkts,
114 uint16_t max_pkts __rte_unused,
115 void *user_cb __rte_unused)
118 uint64_t diff_tsc, now;
121 * For every sample interval,
122 * time stamp is marked on one received packet.
125 for (i = 0; i < nb_pkts; i++) {
126 diff_tsc = now - prev_tsc;
127 timer_tsc += diff_tsc;
129 if ((pkts[i]->ol_flags & PKT_RX_TIMESTAMP) == 0
130 && (timer_tsc >= samp_intvl)) {
131 pkts[i]->timestamp = now;
132 pkts[i]->ol_flags |= PKT_RX_TIMESTAMP;
143 calc_latency(uint16_t pid __rte_unused,
144 uint16_t qid __rte_unused,
145 struct rte_mbuf **pkts,
147 void *_ __rte_unused)
149 unsigned int i, cnt = 0;
151 float latency[nb_pkts];
152 static float prev_latency;
154 * Alpha represents degree of weighting decrease in EWMA,
155 * a constant smoothing factor between 0 and 1. The value
156 * is used below for measuring average latency.
158 const float alpha = 0.2;
161 for (i = 0; i < nb_pkts; i++) {
162 if (pkts[i]->ol_flags & PKT_RX_TIMESTAMP)
163 latency[cnt++] = now - pkts[i]->timestamp;
166 for (i = 0; i < cnt; i++) {
168 * The jitter is calculated as statistical mean of interpacket
169 * delay variation. The "jitter estimate" is computed by taking
170 * the absolute values of the ipdv sequence and applying an
171 * exponential filter with parameter 1/16 to generate the
172 * estimate. i.e J=J+(|D(i-1,i)|-J)/16. Where J is jitter,
173 * D(i-1,i) is difference in latency of two consecutive packets
175 * Reference: Calculated as per RFC 5481, sec 4.1,
176 * RFC 3393 sec 4.5, RFC 1889 sec.
178 glob_stats->jitter += (fabsf(prev_latency - latency[i])
179 - glob_stats->jitter)/16;
180 if (glob_stats->min_latency == 0)
181 glob_stats->min_latency = latency[i];
182 else if (latency[i] < glob_stats->min_latency)
183 glob_stats->min_latency = latency[i];
184 else if (latency[i] > glob_stats->max_latency)
185 glob_stats->max_latency = latency[i];
187 * The average latency is measured using exponential moving
188 * average, i.e. using EWMA
189 * https://en.wikipedia.org/wiki/Moving_average
191 glob_stats->avg_latency +=
192 alpha * (latency[i] - glob_stats->avg_latency);
193 prev_latency = latency[i];
200 rte_latencystats_init(uint64_t app_samp_intvl,
201 rte_latency_stats_flow_type_fn user_cb)
206 struct rxtx_cbs *cbs = NULL;
207 const char *ptr_strings[NUM_LATENCY_STATS] = {0};
208 const struct rte_memzone *mz = NULL;
209 const unsigned int flags = 0;
211 if (rte_memzone_lookup(MZ_RTE_LATENCY_STATS))
214 /** Allocate stats in shared memory fo multi process support */
215 mz = rte_memzone_reserve(MZ_RTE_LATENCY_STATS, sizeof(*glob_stats),
216 rte_socket_id(), flags);
218 RTE_LOG(ERR, LATENCY_STATS, "Cannot reserve memory: %s:%d\n",
223 glob_stats = mz->addr;
224 samp_intvl = app_samp_intvl * latencystat_cycles_per_ns();
226 /** Register latency stats with stats library */
227 for (i = 0; i < NUM_LATENCY_STATS; i++)
228 ptr_strings[i] = lat_stats_strings[i].name;
230 latency_stats_index = rte_metrics_reg_names(ptr_strings,
232 if (latency_stats_index < 0) {
233 RTE_LOG(DEBUG, LATENCY_STATS,
234 "Failed to register latency stats names\n");
238 /** Register Rx/Tx callbacks */
239 RTE_ETH_FOREACH_DEV(pid) {
240 struct rte_eth_dev_info dev_info;
241 rte_eth_dev_info_get(pid, &dev_info);
242 for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
243 cbs = &rx_cbs[pid][qid];
244 cbs->cb = rte_eth_add_first_rx_callback(pid, qid,
245 add_time_stamps, user_cb);
247 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
248 "register Rx callback for pid=%d, "
249 "qid=%d\n", pid, qid);
251 for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
252 cbs = &tx_cbs[pid][qid];
253 cbs->cb = rte_eth_add_tx_callback(pid, qid,
254 calc_latency, user_cb);
256 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
257 "register Tx callback for pid=%d, "
258 "qid=%d\n", pid, qid);
265 rte_latencystats_uninit(void)
270 struct rxtx_cbs *cbs = NULL;
271 const struct rte_memzone *mz = NULL;
273 /** De register Rx/Tx callbacks */
274 RTE_ETH_FOREACH_DEV(pid) {
275 struct rte_eth_dev_info dev_info;
276 rte_eth_dev_info_get(pid, &dev_info);
277 for (qid = 0; qid < dev_info.nb_rx_queues; qid++) {
278 cbs = &rx_cbs[pid][qid];
279 ret = rte_eth_remove_rx_callback(pid, qid, cbs->cb);
281 RTE_LOG(INFO, LATENCY_STATS, "failed to "
282 "remove Rx callback for pid=%d, "
283 "qid=%d\n", pid, qid);
285 for (qid = 0; qid < dev_info.nb_tx_queues; qid++) {
286 cbs = &tx_cbs[pid][qid];
287 ret = rte_eth_remove_tx_callback(pid, qid, cbs->cb);
289 RTE_LOG(INFO, LATENCY_STATS, "failed to "
290 "remove Tx callback for pid=%d, "
291 "qid=%d\n", pid, qid);
295 /* free up the memzone */
296 mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
298 rte_memzone_free(mz);
304 rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)
308 if (names == NULL || size < NUM_LATENCY_STATS)
309 return NUM_LATENCY_STATS;
311 for (i = 0; i < NUM_LATENCY_STATS; i++)
312 snprintf(names[i].name, sizeof(names[i].name),
313 "%s", lat_stats_strings[i].name);
315 return NUM_LATENCY_STATS;
319 rte_latencystats_get(struct rte_metric_value *values, uint16_t size)
321 if (size < NUM_LATENCY_STATS || values == NULL)
322 return NUM_LATENCY_STATS;
324 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
325 const struct rte_memzone *mz;
326 mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
328 RTE_LOG(ERR, LATENCY_STATS,
329 "Latency stats memzone not found\n");
332 glob_stats = mz->addr;
335 /* Retrieve latency stats */
336 rte_latencystats_fill_values(values);
338 return NUM_LATENCY_STATS;