mempool: fix slow allocation of large mempools
[dpdk.git] / lib / librte_latencystats / rte_latencystats.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <stdbool.h>
8 #include <math.h>
9
10 #include <rte_string_fns.h>
11 #include <rte_mbuf.h>
12 #include <rte_log.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>
18
19 #include "rte_latencystats.h"
20
21 /** Nano seconds per second */
22 #define NS_PER_SEC 1E9
23
24 /** Clock cycles per nano second */
25 static uint64_t
26 latencystat_cycles_per_ns(void)
27 {
28         return rte_get_timer_hz() / NS_PER_SEC;
29 }
30
31 /* Macros for printing using RTE_LOG */
32 #define RTE_LOGTYPE_LATENCY_STATS RTE_LOGTYPE_USER1
33
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;
39
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 */
45 };
46
47 static struct rte_latency_stats *glob_stats;
48
49 struct rxtx_cbs {
50         const struct rte_eth_rxtx_callback *cb;
51 };
52
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];
55
56 struct latency_stats_nameoff {
57         char name[RTE_ETH_XSTATS_NAME_SIZE];
58         unsigned int offset;
59 };
60
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)},
66 };
67
68 #define NUM_LATENCY_STATS (sizeof(lat_stats_strings) / \
69                                 sizeof(lat_stats_strings[0]))
70
71 int32_t
72 rte_latencystats_update(void)
73 {
74         unsigned int i;
75         float *stats_ptr = NULL;
76         uint64_t values[NUM_LATENCY_STATS] = {0};
77         int ret;
78
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());
84         }
85
86         ret = rte_metrics_update_values(RTE_METRICS_GLOBAL,
87                                         latency_stats_index,
88                                         values, NUM_LATENCY_STATS);
89         if (ret < 0)
90                 RTE_LOG(INFO, LATENCY_STATS, "Failed to push the stats\n");
91
92         return ret;
93 }
94
95 static void
96 rte_latencystats_fill_values(struct rte_metric_value *values)
97 {
98         unsigned int i;
99         float *stats_ptr = NULL;
100
101         for (i = 0; i < NUM_LATENCY_STATS; i++) {
102                 stats_ptr = RTE_PTR_ADD(glob_stats,
103                                 lat_stats_strings[i].offset);
104                 values[i].key = i;
105                 values[i].value = (uint64_t)floor((*stats_ptr)/
106                                                 latencystat_cycles_per_ns());
107         }
108 }
109
110 static uint16_t
111 add_time_stamps(uint16_t pid __rte_unused,
112                 uint16_t qid __rte_unused,
113                 struct rte_mbuf **pkts,
114                 uint16_t nb_pkts,
115                 uint16_t max_pkts __rte_unused,
116                 void *user_cb __rte_unused)
117 {
118         unsigned int i;
119         uint64_t diff_tsc, now;
120
121         /*
122          * For every sample interval,
123          * time stamp is marked on one received packet.
124          */
125         now = rte_rdtsc();
126         for (i = 0; i < nb_pkts; i++) {
127                 diff_tsc = now - prev_tsc;
128                 timer_tsc += diff_tsc;
129
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;
134                         timer_tsc = 0;
135                 }
136                 prev_tsc = now;
137                 now = rte_rdtsc();
138         }
139
140         return nb_pkts;
141 }
142
143 static uint16_t
144 calc_latency(uint16_t pid __rte_unused,
145                 uint16_t qid __rte_unused,
146                 struct rte_mbuf **pkts,
147                 uint16_t nb_pkts,
148                 void *_ __rte_unused)
149 {
150         unsigned int i, cnt = 0;
151         uint64_t now;
152         float latency[nb_pkts];
153         static float prev_latency;
154         /*
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.
158          */
159         const float alpha = 0.2;
160
161         now = rte_rdtsc();
162         for (i = 0; i < nb_pkts; i++) {
163                 if (pkts[i]->ol_flags & PKT_RX_TIMESTAMP)
164                         latency[cnt++] = now - pkts[i]->timestamp;
165         }
166
167         for (i = 0; i < cnt; i++) {
168                 /*
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
175                  * i-1 and i.
176                  * Reference: Calculated as per RFC 5481, sec 4.1,
177                  * RFC 3393 sec 4.5, RFC 1889 sec.
178                  */
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];
187                 /*
188                  * The average latency is measured using exponential moving
189                  * average, i.e. using EWMA
190                  * https://en.wikipedia.org/wiki/Moving_average
191                  */
192                 glob_stats->avg_latency +=
193                         alpha * (latency[i] - glob_stats->avg_latency);
194                 prev_latency = latency[i];
195         }
196
197         return nb_pkts;
198 }
199
200 int
201 rte_latencystats_init(uint64_t app_samp_intvl,
202                 rte_latency_stats_flow_type_fn user_cb)
203 {
204         unsigned int i;
205         uint16_t pid;
206         uint16_t qid;
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;
211         int ret;
212
213         if (rte_memzone_lookup(MZ_RTE_LATENCY_STATS))
214                 return -EEXIST;
215
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);
219         if (mz == NULL) {
220                 RTE_LOG(ERR, LATENCY_STATS, "Cannot reserve memory: %s:%d\n",
221                         __func__, __LINE__);
222                 return -ENOMEM;
223         }
224
225         glob_stats = mz->addr;
226         samp_intvl = app_samp_intvl * latencystat_cycles_per_ns();
227
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;
231
232         latency_stats_index = rte_metrics_reg_names(ptr_strings,
233                                                         NUM_LATENCY_STATS);
234         if (latency_stats_index < 0) {
235                 RTE_LOG(DEBUG, LATENCY_STATS,
236                         "Failed to register latency stats names\n");
237                 return -1;
238         }
239
240         /** Register Rx/Tx callbacks */
241         RTE_ETH_FOREACH_DEV(pid) {
242                 struct rte_eth_dev_info dev_info;
243
244                 ret = rte_eth_dev_info_get(pid, &dev_info);
245                 if (ret != 0) {
246                         RTE_LOG(INFO, LATENCY_STATS,
247                                 "Error during getting device (port %u) info: %s\n",
248                                 pid, strerror(-ret));
249
250                         continue;
251                 }
252
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);
257                         if (!cbs->cb)
258                                 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
259                                         "register Rx callback for pid=%d, "
260                                         "qid=%d\n", pid, qid);
261                 }
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);
266                         if (!cbs->cb)
267                                 RTE_LOG(INFO, LATENCY_STATS, "Failed to "
268                                         "register Tx callback for pid=%d, "
269                                         "qid=%d\n", pid, qid);
270                 }
271         }
272         return 0;
273 }
274
275 int
276 rte_latencystats_uninit(void)
277 {
278         uint16_t pid;
279         uint16_t qid;
280         int ret = 0;
281         struct rxtx_cbs *cbs = NULL;
282         const struct rte_memzone *mz = NULL;
283
284         /** De register Rx/Tx callbacks */
285         RTE_ETH_FOREACH_DEV(pid) {
286                 struct rte_eth_dev_info dev_info;
287
288                 ret = rte_eth_dev_info_get(pid, &dev_info);
289                 if (ret != 0) {
290                         RTE_LOG(INFO, LATENCY_STATS,
291                                 "Error during getting device (port %u) info: %s\n",
292                                 pid, strerror(-ret));
293
294                         continue;
295                 }
296
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);
300                         if (ret)
301                                 RTE_LOG(INFO, LATENCY_STATS, "failed to "
302                                         "remove Rx callback for pid=%d, "
303                                         "qid=%d\n", pid, qid);
304                 }
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);
308                         if (ret)
309                                 RTE_LOG(INFO, LATENCY_STATS, "failed to "
310                                         "remove Tx callback for pid=%d, "
311                                         "qid=%d\n", pid, qid);
312                 }
313         }
314
315         /* free up the memzone */
316         mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
317         if (mz)
318                 rte_memzone_free(mz);
319
320         return 0;
321 }
322
323 int
324 rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)
325 {
326         unsigned int i;
327
328         if (names == NULL || size < NUM_LATENCY_STATS)
329                 return NUM_LATENCY_STATS;
330
331         for (i = 0; i < NUM_LATENCY_STATS; i++)
332                 strlcpy(names[i].name, lat_stats_strings[i].name,
333                         sizeof(names[i].name));
334
335         return NUM_LATENCY_STATS;
336 }
337
338 int
339 rte_latencystats_get(struct rte_metric_value *values, uint16_t size)
340 {
341         if (size < NUM_LATENCY_STATS || values == NULL)
342                 return NUM_LATENCY_STATS;
343
344         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
345                 const struct rte_memzone *mz;
346                 mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);
347                 if (mz == NULL) {
348                         RTE_LOG(ERR, LATENCY_STATS,
349                                 "Latency stats memzone not found\n");
350                         return -ENOMEM;
351                 }
352                 glob_stats =  mz->addr;
353         }
354
355         /* Retrieve latency stats */
356         rte_latencystats_fill_values(values);
357
358         return NUM_LATENCY_STATS;
359 }