New upstream version 17.11.4
[deb_dpdk.git] / lib / librte_bitratestats / rte_bitrate.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
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>
39
40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
41
42 /*
43  * Persistent bit-rate data.
44  * @internal
45  */
46 struct rte_stats_bitrate {
47         uint64_t last_ibytes;
48         uint64_t last_obytes;
49         uint64_t peak_ibits;
50         uint64_t peak_obits;
51         uint64_t mean_ibits;
52         uint64_t mean_obits;
53         uint64_t ewma_ibits;
54         uint64_t ewma_obits;
55 };
56
57 struct rte_stats_bitrates {
58         struct rte_stats_bitrate port_stats[RTE_MAX_ETHPORTS];
59         uint16_t id_stats_set;
60 };
61
62 struct rte_stats_bitrates *
63 rte_stats_bitrate_create(void)
64 {
65         return rte_zmalloc(NULL, sizeof(struct rte_stats_bitrates),
66                 RTE_CACHE_LINE_SIZE);
67 }
68
69 int
70 rte_stats_bitrate_reg(struct rte_stats_bitrates *bitrate_data)
71 {
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",
76         };
77         int return_value;
78
79         if (bitrate_data == NULL)
80                 return -EINVAL;
81
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;
85         return return_value;
86 }
87
88 int
89 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
90                         uint16_t port_id)
91 {
92         struct rte_stats_bitrate *port_data;
93         struct rte_eth_stats eth_stats;
94         int ret_code;
95         uint64_t cnt_bits;
96         int64_t delta;
97         const int64_t alpha_percent = 20;
98         uint64_t values[6];
99
100         if (bitrate_data == NULL)
101                 return -EINVAL;
102
103         ret_code = rte_eth_stats_get(port_id, &eth_stats);
104         if (ret_code != 0)
105                 return ret_code;
106
107         port_data = &bitrate_data->port_stats[port_id];
108
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.
114          */
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;
119         delta = cnt_bits;
120         delta -= port_data->ewma_ibits;
121         /* The +-50 fixes integer rounding during division */
122         if (delta > 0)
123                 delta = (delta * alpha_percent + 50) / 100;
124         else
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
129          */
130         if (cnt_bits == 0 && delta == 0)
131                 port_data->ewma_ibits = 0;
132         port_data->mean_ibits = cnt_bits;
133
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;
139         delta = cnt_bits;
140         delta -= port_data->ewma_obits;
141         if (delta > 0)
142                 delta = (delta * alpha_percent + 50) / 100;
143         else
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;
149
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));
158         return 0;
159 }