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>
11 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
14 * Persistent bit-rate data.
17 struct rte_stats_bitrate {
28 struct rte_stats_bitrates {
29 struct rte_stats_bitrate port_stats[RTE_MAX_ETHPORTS];
30 uint16_t id_stats_set;
33 struct rte_stats_bitrates *
34 rte_stats_bitrate_create(void)
36 return rte_zmalloc(NULL, sizeof(struct rte_stats_bitrates),
41 rte_stats_bitrate_reg(struct rte_stats_bitrates *bitrate_data)
43 const char * const names[] = {
44 "ewma_bits_in", "ewma_bits_out",
45 "mean_bits_in", "mean_bits_out",
46 "peak_bits_in", "peak_bits_out",
50 if (bitrate_data == NULL)
53 return_value = rte_metrics_reg_names(&names[0], ARRAY_SIZE(names));
54 if (return_value >= 0)
55 bitrate_data->id_stats_set = return_value;
60 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
63 struct rte_stats_bitrate *port_data;
64 struct rte_eth_stats eth_stats;
68 const int64_t alpha_percent = 20;
71 if (bitrate_data == NULL)
74 ret_code = rte_eth_stats_get(port_id, ð_stats);
78 port_data = &bitrate_data->port_stats[port_id];
80 /* Incoming bitrate. This is an iteratively calculated EWMA
81 * (Exponentially Weighted Moving Average) that uses a
82 * weighting factor of alpha_percent. An unsmoothed mean
83 * for just the current time delta is also calculated for the
84 * benefit of people who don't understand signal processing.
86 cnt_bits = (eth_stats.ibytes - port_data->last_ibytes) << 3;
87 port_data->last_ibytes = eth_stats.ibytes;
88 if (cnt_bits > port_data->peak_ibits)
89 port_data->peak_ibits = cnt_bits;
91 delta -= port_data->ewma_ibits;
92 /* The +-50 fixes integer rounding during division */
94 delta = (delta * alpha_percent + 50) / 100;
96 delta = (delta * alpha_percent - 50) / 100;
97 port_data->ewma_ibits += delta;
98 /* Integer roundoff prevents EWMA between 0 and (100/alpha_percent)
99 * ever reaching zero in no-traffic conditions
101 if (cnt_bits == 0 && delta == 0)
102 port_data->ewma_ibits = 0;
103 port_data->mean_ibits = cnt_bits;
105 /* Outgoing bitrate (also EWMA) */
106 cnt_bits = (eth_stats.obytes - port_data->last_obytes) << 3;
107 port_data->last_obytes = eth_stats.obytes;
108 if (cnt_bits > port_data->peak_obits)
109 port_data->peak_obits = cnt_bits;
111 delta -= port_data->ewma_obits;
113 delta = (delta * alpha_percent + 50) / 100;
115 delta = (delta * alpha_percent - 50) / 100;
116 port_data->ewma_obits += delta;
117 if (cnt_bits == 0 && delta == 0)
118 port_data->ewma_obits = 0;
119 port_data->mean_obits = cnt_bits;
121 values[0] = port_data->ewma_ibits;
122 values[1] = port_data->ewma_obits;
123 values[2] = port_data->mean_ibits;
124 values[3] = port_data->mean_obits;
125 values[4] = port_data->peak_ibits;
126 values[5] = port_data->peak_obits;
127 rte_metrics_update_values(port_id, bitrate_data->id_stats_set,
128 values, ARRAY_SIZE(values));