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