4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_common.h>
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
37 #include <rte_metrics.h>
38 #include <rte_bitrate.h>
40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
43 * Persistent bit-rate data.
46 struct rte_stats_bitrate {
57 struct rte_stats_bitrates {
58 struct rte_stats_bitrate port_stats[RTE_MAX_ETHPORTS];
59 uint16_t id_stats_set;
62 struct rte_stats_bitrates *
63 rte_stats_bitrate_create(void)
65 return rte_zmalloc(NULL, sizeof(struct rte_stats_bitrates),
70 rte_stats_bitrate_reg(struct rte_stats_bitrates *bitrate_data)
72 const char * const names[] = {
73 "ewma_bits_in", "ewma_bits_out",
74 "mean_bits_in", "mean_bits_out",
75 "peak_bits_in", "peak_bits_out",
79 return_value = rte_metrics_reg_names(&names[0], ARRAY_SIZE(names));
80 if (return_value >= 0)
81 bitrate_data->id_stats_set = return_value;
86 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
89 struct rte_stats_bitrate *port_data;
90 struct rte_eth_stats eth_stats;
94 const int64_t alpha_percent = 20;
97 ret_code = rte_eth_stats_get(port_id, ð_stats);
101 port_data = &bitrate_data->port_stats[port_id];
103 /* Incoming bitrate. This is an iteratively calculated EWMA
104 * (Exponentially Weighted Moving Average) that uses a
105 * weighting factor of alpha_percent. An unsmoothed mean
106 * for just the current time delta is also calculated for the
107 * benefit of people who don't understand signal processing.
109 cnt_bits = (eth_stats.ibytes - port_data->last_ibytes) << 3;
110 port_data->last_ibytes = eth_stats.ibytes;
111 if (cnt_bits > port_data->peak_ibits)
112 port_data->peak_ibits = cnt_bits;
114 delta -= port_data->ewma_ibits;
115 /* The +-50 fixes integer rounding during division */
117 delta = (delta * alpha_percent + 50) / 100;
119 delta = (delta * alpha_percent - 50) / 100;
120 port_data->ewma_ibits += delta;
121 /* Integer roundoff prevents EWMA between 0 and (100/alpha_percent)
122 * ever reaching zero in no-traffic conditions
124 if (cnt_bits == 0 && delta == 0)
125 port_data->ewma_ibits = 0;
126 port_data->mean_ibits = cnt_bits;
128 /* Outgoing bitrate (also EWMA) */
129 cnt_bits = (eth_stats.obytes - port_data->last_obytes) << 3;
130 port_data->last_obytes = eth_stats.obytes;
131 if (cnt_bits > port_data->peak_obits)
132 port_data->peak_obits = cnt_bits;
134 delta -= port_data->ewma_obits;
136 delta = (delta * alpha_percent + 50) / 100;
138 delta = (delta * alpha_percent - 50) / 100;
139 port_data->ewma_obits += delta;
140 if (cnt_bits == 0 && delta == 0)
141 port_data->ewma_obits = 0;
142 port_data->mean_obits = cnt_bits;
144 values[0] = port_data->ewma_ibits;
145 values[1] = port_data->ewma_obits;
146 values[2] = port_data->mean_ibits;
147 values[3] = port_data->mean_obits;
148 values[4] = port_data->peak_ibits;
149 values[5] = port_data->peak_obits;
150 rte_metrics_update_values(port_id, bitrate_data->id_stats_set,
151 values, ARRAY_SIZE(values));