New upstream version 17.11-rc3
[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         return_value = rte_metrics_reg_names(&names[0], ARRAY_SIZE(names));
80         if (return_value >= 0)
81                 bitrate_data->id_stats_set = return_value;
82         return return_value;
83 }
84
85 int
86 rte_stats_bitrate_calc(struct rte_stats_bitrates *bitrate_data,
87                         uint16_t port_id)
88 {
89         struct rte_stats_bitrate *port_data;
90         struct rte_eth_stats eth_stats;
91         int ret_code;
92         uint64_t cnt_bits;
93         int64_t delta;
94         const int64_t alpha_percent = 20;
95         uint64_t values[6];
96
97         ret_code = rte_eth_stats_get(port_id, &eth_stats);
98         if (ret_code != 0)
99                 return ret_code;
100
101         port_data = &bitrate_data->port_stats[port_id];
102
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.
108          */
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;
113         delta = cnt_bits;
114         delta -= port_data->ewma_ibits;
115         /* The +-50 fixes integer rounding during division */
116         if (delta > 0)
117                 delta = (delta * alpha_percent + 50) / 100;
118         else
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
123          */
124         if (cnt_bits == 0 && delta == 0)
125                 port_data->ewma_ibits = 0;
126         port_data->mean_ibits = cnt_bits;
127
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;
133         delta = cnt_bits;
134         delta -= port_data->ewma_obits;
135         if (delta > 0)
136                 delta = (delta * alpha_percent + 50) / 100;
137         else
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;
143
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));
152         return 0;
153 }