1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_ethdev.h>
7 #include <rte_malloc.h>
8 #include <rte_metrics.h>
9 #include <rte_bitrate.h>
12 * Persistent bit-rate data.
15 struct rte_stats_bitrate {
26 struct rte_stats_bitrates {
27 struct rte_stats_bitrate port_stats[RTE_MAX_ETHPORTS];
28 uint16_t id_stats_set;
31 struct rte_stats_bitrates *
32 rte_stats_bitrate_create(void)
34 return rte_zmalloc(NULL, sizeof(struct rte_stats_bitrates),
39 rte_stats_bitrate_reg(struct rte_stats_bitrates *bitrate_data)
41 const char * const names[] = {
42 "ewma_bits_in", "ewma_bits_out",
43 "mean_bits_in", "mean_bits_out",
44 "peak_bits_in", "peak_bits_out",
48 if (bitrate_data == NULL)
51 return_value = rte_metrics_reg_names(&names[0], RTE_DIM(names));
52 if (return_value >= 0)
53 bitrate_data->id_stats_set = return_value;
58 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
61 struct rte_stats_bitrate *port_data;
62 struct rte_eth_stats eth_stats;
66 const int64_t alpha_percent = 20;
70 if (bitrate_data == NULL)
73 ret_code = rte_eth_stats_get(port_id, ð_stats);
77 port_data = &bitrate_data->port_stats[port_id];
79 /* Incoming bitrate. This is an iteratively calculated EWMA
80 * (Exponentially Weighted Moving Average) that uses a
81 * weighting factor of alpha_percent. An unsmoothed mean
82 * for just the current time delta is also calculated for the
83 * benefit of people who don't understand signal processing.
85 cnt_bits = (eth_stats.ibytes - port_data->last_ibytes) << 3;
86 port_data->last_ibytes = eth_stats.ibytes;
87 if (cnt_bits > port_data->peak_ibits)
88 port_data->peak_ibits = cnt_bits;
90 delta -= port_data->ewma_ibits;
91 /* The +-50 fixes integer rounding during division */
93 delta = (delta * alpha_percent + 50) / 100;
95 delta = (delta * alpha_percent - 50) / 100;
96 port_data->ewma_ibits += delta;
97 /* Integer roundoff prevents EWMA between 0 and (100/alpha_percent)
98 * ever reaching zero in no-traffic conditions
100 if (cnt_bits == 0 && delta == 0)
101 port_data->ewma_ibits = 0;
102 port_data->mean_ibits = cnt_bits;
104 /* Outgoing bitrate (also EWMA) */
105 cnt_bits = (eth_stats.obytes - port_data->last_obytes) << 3;
106 port_data->last_obytes = eth_stats.obytes;
107 if (cnt_bits > port_data->peak_obits)
108 port_data->peak_obits = cnt_bits;
110 delta -= port_data->ewma_obits;
112 delta = (delta * alpha_percent + 50) / 100;
114 delta = (delta * alpha_percent - 50) / 100;
115 port_data->ewma_obits += delta;
116 if (cnt_bits == 0 && delta == 0)
117 port_data->ewma_obits = 0;
118 port_data->mean_obits = cnt_bits;
120 values[0] = port_data->ewma_ibits;
121 values[1] = port_data->ewma_obits;
122 values[2] = port_data->mean_ibits;
123 values[3] = port_data->mean_obits;
124 values[4] = port_data->peak_ibits;
125 values[5] = port_data->peak_obits;
126 ret = rte_metrics_update_values(port_id, bitrate_data->id_stats_set,
127 values, RTE_DIM(values));