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 if (bitrate_data == NULL)
82 return_value = rte_metrics_reg_names(&names[0], ARRAY_SIZE(names));
83 if (return_value >= 0)
84 bitrate_data->id_stats_set = return_value;
89 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
92 struct rte_stats_bitrate *port_data;
93 struct rte_eth_stats eth_stats;
97 const int64_t alpha_percent = 20;
100 if (bitrate_data == NULL)
103 ret_code = rte_eth_stats_get(port_id, ð_stats);
107 port_data = &bitrate_data->port_stats[port_id];
109 /* Incoming bitrate. This is an iteratively calculated EWMA
110 * (Exponentially Weighted Moving Average) that uses a
111 * weighting factor of alpha_percent. An unsmoothed mean
112 * for just the current time delta is also calculated for the
113 * benefit of people who don't understand signal processing.
115 cnt_bits = (eth_stats.ibytes - port_data->last_ibytes) << 3;
116 port_data->last_ibytes = eth_stats.ibytes;
117 if (cnt_bits > port_data->peak_ibits)
118 port_data->peak_ibits = cnt_bits;
120 delta -= port_data->ewma_ibits;
121 /* The +-50 fixes integer rounding during division */
123 delta = (delta * alpha_percent + 50) / 100;
125 delta = (delta * alpha_percent - 50) / 100;
126 port_data->ewma_ibits += delta;
127 /* Integer roundoff prevents EWMA between 0 and (100/alpha_percent)
128 * ever reaching zero in no-traffic conditions
130 if (cnt_bits == 0 && delta == 0)
131 port_data->ewma_ibits = 0;
132 port_data->mean_ibits = cnt_bits;
134 /* Outgoing bitrate (also EWMA) */
135 cnt_bits = (eth_stats.obytes - port_data->last_obytes) << 3;
136 port_data->last_obytes = eth_stats.obytes;
137 if (cnt_bits > port_data->peak_obits)
138 port_data->peak_obits = cnt_bits;
140 delta -= port_data->ewma_obits;
142 delta = (delta * alpha_percent + 50) / 100;
144 delta = (delta * alpha_percent - 50) / 100;
145 port_data->ewma_obits += delta;
146 if (cnt_bits == 0 && delta == 0)
147 port_data->ewma_obits = 0;
148 port_data->mean_obits = cnt_bits;
150 values[0] = port_data->ewma_ibits;
151 values[1] = port_data->ewma_obits;
152 values[2] = port_data->mean_ibits;
153 values[3] = port_data->mean_obits;
154 values[4] = port_data->peak_ibits;
155 values[5] = port_data->peak_obits;
156 rte_metrics_update_values(port_id, bitrate_data->id_stats_set,
157 values, ARRAY_SIZE(values));