New upstream version 17.11-rc3
[deb_dpdk.git] / examples / qos_sched / stats.c
1 /*-
2  *  *   BSD LICENSE
3  *  *
4  *  *   Copyright(c) 2010-2014 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 <unistd.h>
35 #include <string.h>
36
37 #include "main.h"
38
39 int
40 qavg_q(uint16_t port_id, uint32_t subport_id, uint32_t pipe_id, uint8_t tc,
41                 uint8_t q)
42 {
43         struct rte_sched_queue_stats stats;
44         struct rte_sched_port *port;
45         uint16_t qlen;
46         uint32_t queue_id, count, i;
47         uint32_t average;
48
49         for (i = 0; i < nb_pfc; i++) {
50                 if (qos_conf[i].tx_port == port_id)
51                         break;
52         }
53         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port || pipe_id >= port_params.n_pipes_per_subport
54                         || tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE || q >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
55                 return -1;
56
57         port = qos_conf[i].sched_port;
58
59         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + pipe_id);
60         queue_id = queue_id + (tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + q);
61
62         average = 0;
63
64         for (count = 0; count < qavg_ntimes; count++) {
65                 rte_sched_queue_read_stats(port, queue_id, &stats, &qlen);
66                 average += qlen;
67                 usleep(qavg_period);
68         }
69
70         average /= qavg_ntimes;
71
72         printf("\nAverage queue size: %" PRIu32 " bytes.\n\n", average);
73
74         return 0;
75 }
76
77 int
78 qavg_tcpipe(uint16_t port_id, uint32_t subport_id, uint32_t pipe_id,
79              uint8_t tc)
80 {
81         struct rte_sched_queue_stats stats;
82         struct rte_sched_port *port;
83         uint16_t qlen;
84         uint32_t queue_id, count, i;
85         uint32_t average, part_average;
86
87         for (i = 0; i < nb_pfc; i++) {
88                 if (qos_conf[i].tx_port == port_id)
89                         break;
90         }
91         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port || pipe_id >= port_params.n_pipes_per_subport
92                         || tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
93                 return -1;
94
95         port = qos_conf[i].sched_port;
96
97         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + pipe_id);
98
99         average = 0;
100
101         for (count = 0; count < qavg_ntimes; count++) {
102                 part_average = 0;
103                 for (i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
104                         rte_sched_queue_read_stats(port, queue_id + (tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + i), &stats, &qlen);
105                         part_average += qlen;
106                 }
107                 average += part_average / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
108                 usleep(qavg_period);
109         }
110
111         average /= qavg_ntimes;
112
113         printf("\nAverage queue size: %" PRIu32 " bytes.\n\n", average);
114
115         return 0;
116 }
117
118 int
119 qavg_pipe(uint16_t port_id, uint32_t subport_id, uint32_t pipe_id)
120 {
121         struct rte_sched_queue_stats stats;
122         struct rte_sched_port *port;
123         uint16_t qlen;
124         uint32_t queue_id, count, i;
125         uint32_t average, part_average;
126
127         for (i = 0; i < nb_pfc; i++) {
128                 if (qos_conf[i].tx_port == port_id)
129                         break;
130         }
131         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port || pipe_id >= port_params.n_pipes_per_subport)
132                 return -1;
133
134         port = qos_conf[i].sched_port;
135
136         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + pipe_id);
137
138         average = 0;
139
140         for (count = 0; count < qavg_ntimes; count++) {
141                 part_average = 0;
142                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
143                         rte_sched_queue_read_stats(port, queue_id + i, &stats, &qlen);
144                         part_average += qlen;
145                 }
146                 average += part_average / (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
147                 usleep(qavg_period);
148         }
149
150         average /= qavg_ntimes;
151
152         printf("\nAverage queue size: %" PRIu32 " bytes.\n\n", average);
153
154         return 0;
155 }
156
157 int
158 qavg_tcsubport(uint16_t port_id, uint32_t subport_id, uint8_t tc)
159 {
160         struct rte_sched_queue_stats stats;
161         struct rte_sched_port *port;
162         uint16_t qlen;
163         uint32_t queue_id, count, i, j;
164         uint32_t average, part_average;
165
166         for (i = 0; i < nb_pfc; i++) {
167                 if (qos_conf[i].tx_port == port_id)
168                         break;
169         }
170         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port || tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
171                 return -1;
172
173         port = qos_conf[i].sched_port;
174
175         average = 0;
176
177         for (count = 0; count < qavg_ntimes; count++) {
178                 part_average = 0;
179                 for (i = 0; i < port_params.n_pipes_per_subport; i++) {
180                         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + i);
181
182                         for (j = 0; j < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; j++) {
183                                 rte_sched_queue_read_stats(port, queue_id + (tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + j), &stats, &qlen);
184                                 part_average += qlen;
185                         }
186                 }
187
188                 average += part_average / (port_params.n_pipes_per_subport * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
189                 usleep(qavg_period);
190         }
191
192         average /= qavg_ntimes;
193
194         printf("\nAverage queue size: %" PRIu32 " bytes.\n\n", average);
195
196         return 0;
197 }
198
199 int
200 qavg_subport(uint16_t port_id, uint32_t subport_id)
201 {
202         struct rte_sched_queue_stats stats;
203         struct rte_sched_port *port;
204         uint16_t qlen;
205         uint32_t queue_id, count, i, j;
206         uint32_t average, part_average;
207
208         for (i = 0; i < nb_pfc; i++) {
209                 if (qos_conf[i].tx_port == port_id)
210                         break;
211         }
212         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port)
213                 return -1;
214
215         port = qos_conf[i].sched_port;
216
217         average = 0;
218
219         for (count = 0; count < qavg_ntimes; count++) {
220                 part_average = 0;
221                 for (i = 0; i < port_params.n_pipes_per_subport; i++) {
222                         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + i);
223
224                         for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; j++) {
225                                 rte_sched_queue_read_stats(port, queue_id + j, &stats, &qlen);
226                                 part_average += qlen;
227                         }
228                 }
229
230                 average += part_average / (port_params.n_pipes_per_subport * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
231                 usleep(qavg_period);
232         }
233
234         average /= qavg_ntimes;
235
236         printf("\nAverage queue size: %" PRIu32 " bytes.\n\n", average);
237
238         return 0;
239 }
240
241 int
242 subport_stat(uint16_t port_id, uint32_t subport_id)
243 {
244         struct rte_sched_subport_stats stats;
245         struct rte_sched_port *port;
246         uint32_t tc_ov[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
247         uint8_t i;
248
249         for (i = 0; i < nb_pfc; i++) {
250                 if (qos_conf[i].tx_port == port_id)
251                         break;
252         }
253         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port)
254                 return -1;
255
256         port = qos_conf[i].sched_port;
257         memset (tc_ov, 0, sizeof(tc_ov));
258
259         rte_sched_subport_read_stats(port, subport_id, &stats, tc_ov);
260
261         printf("\n");
262         printf("+----+-------------+-------------+-------------+-------------+-------------+\n");
263         printf("| TC |   Pkts OK   |Pkts Dropped |  Bytes OK   |Bytes Dropped|  OV Status  |\n");
264         printf("+----+-------------+-------------+-------------+-------------+-------------+\n");
265
266         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
267                 printf("|  %d | %11" PRIu32 " | %11" PRIu32 " | %11" PRIu32 " | %11" PRIu32 " | %11" PRIu32 " |\n", i,
268                                 stats.n_pkts_tc[i], stats.n_pkts_tc_dropped[i],
269                                 stats.n_bytes_tc[i], stats.n_bytes_tc_dropped[i], tc_ov[i]);
270                 printf("+----+-------------+-------------+-------------+-------------+-------------+\n");
271         }
272         printf("\n");
273
274         return 0;
275 }
276
277 int
278 pipe_stat(uint16_t port_id, uint32_t subport_id, uint32_t pipe_id)
279 {
280         struct rte_sched_queue_stats stats;
281         struct rte_sched_port *port;
282         uint16_t qlen;
283         uint8_t i, j;
284         uint32_t queue_id;
285
286         for (i = 0; i < nb_pfc; i++) {
287                 if (qos_conf[i].tx_port == port_id)
288                         break;
289         }
290         if (i == nb_pfc || subport_id >= port_params.n_subports_per_port || pipe_id >= port_params.n_pipes_per_subport)
291                 return -1;
292
293         port = qos_conf[i].sched_port;
294
295         queue_id = RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * (subport_id * port_params.n_pipes_per_subport + pipe_id);
296
297         printf("\n");
298         printf("+----+-------+-------------+-------------+-------------+-------------+-------------+\n");
299         printf("| TC | Queue |   Pkts OK   |Pkts Dropped |  Bytes OK   |Bytes Dropped|    Length   |\n");
300         printf("+----+-------+-------------+-------------+-------------+-------------+-------------+\n");
301
302         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
303                 for (j = 0; j < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; j++) {
304
305                         rte_sched_queue_read_stats(port, queue_id + (i * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + j), &stats, &qlen);
306
307                         printf("|  %d |   %d   | %11" PRIu32 " | %11" PRIu32 " | %11" PRIu32 " | %11" PRIu32 " | %11i |\n", i, j,
308                                         stats.n_pkts, stats.n_pkts_dropped, stats.n_bytes, stats.n_bytes_dropped, qlen);
309                         printf("+----+-------+-------------+-------------+-------------+-------------+-------------+\n");
310                 }
311                 if (i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1)
312                         printf("+----+-------+-------------+-------------+-------------+-------------+-------------+\n");
313         }
314         printf("\n");
315
316         return 0;
317 }