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 return_value = rte_metrics_reg_names(&names[0], ARRAY_SIZE(names));
51 if (return_value >= 0)
52 bitrate_data->id_stats_set = return_value;
57 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
60 struct rte_stats_bitrate *port_data;
61 struct rte_eth_stats eth_stats;
65 const int64_t alpha_percent = 20;
68 ret_code = rte_eth_stats_get(port_id, ð_stats);
72 port_data = &bitrate_data->port_stats[port_id];
74 /* Incoming bitrate. This is an iteratively calculated EWMA
75 * (Exponentially Weighted Moving Average) that uses a
76 * weighting factor of alpha_percent. An unsmoothed mean
77 * for just the current time delta is also calculated for the
78 * benefit of people who don't understand signal processing.
80 cnt_bits = (eth_stats.ibytes - port_data->last_ibytes) << 3;
81 port_data->last_ibytes = eth_stats.ibytes;
82 if (cnt_bits > port_data->peak_ibits)
83 port_data->peak_ibits = cnt_bits;
85 delta -= port_data->ewma_ibits;
86 /* The +-50 fixes integer rounding during division */
88 delta = (delta * alpha_percent + 50) / 100;
90 delta = (delta * alpha_percent - 50) / 100;
91 port_data->ewma_ibits += delta;
92 /* Integer roundoff prevents EWMA between 0 and (100/alpha_percent)
93 * ever reaching zero in no-traffic conditions
95 if (cnt_bits == 0 && delta == 0)
96 port_data->ewma_ibits = 0;
97 port_data->mean_ibits = cnt_bits;
99 /* Outgoing bitrate (also EWMA) */
100 cnt_bits = (eth_stats.obytes - port_data->last_obytes) << 3;
101 port_data->last_obytes = eth_stats.obytes;
102 if (cnt_bits > port_data->peak_obits)
103 port_data->peak_obits = cnt_bits;
105 delta -= port_data->ewma_obits;
107 delta = (delta * alpha_percent + 50) / 100;
109 delta = (delta * alpha_percent - 50) / 100;
110 port_data->ewma_obits += delta;
111 if (cnt_bits == 0 && delta == 0)
112 port_data->ewma_obits = 0;
113 port_data->mean_obits = cnt_bits;
115 values[0] = port_data->ewma_ibits;
116 values[1] = port_data->ewma_obits;
117 values[2] = port_data->mean_ibits;
118 values[3] = port_data->mean_obits;
119 values[4] = port_data->peak_ibits;
120 values[5] = port_data->peak_obits;
121 rte_metrics_update_values(port_id, bitrate_data->id_stats_set,
122 values, ARRAY_SIZE(values));