xref: /netbsd/usr.sbin/altq/altqstat/qdisc_wfq.c (revision bf9ec67e)
1 /*	$NetBSD: qdisc_wfq.c,v 1.3 2001/08/16 07:48:12 itojun Exp $	*/
2 /*	$KAME: qdisc_wfq.c,v 1.3 2001/08/15 12:51:59 kjc Exp $	*/
3 /*
4  * Copyright (C) 1999-2000
5  *	Sony Computer Science Laboratories, Inc.  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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <altq/altq.h>
36 #include <altq/altq_wfq.h>
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <math.h>
43 #include <errno.h>
44 #include <err.h>
45 #ifndef NO_CURSES
46 #include <curses.h>
47 #endif
48 
49 #include "altqstat.h"
50 
51 struct wfqinfo {
52 	int qid;
53 	queue_stats stats;
54 	u_quad_t last_bytes;
55 	double bps;
56 };
57 
58 #define NTOP		10
59 static int ntop = NTOP;
60 
61 void
62 wfq_stat_loop(int fd, const char *ifname, int count, int interval)
63 {
64 	struct wfq_getstats wfq_stats;
65 	struct timeval cur_time, last_time;
66 	int i, j, k, nqueues;
67 	double sec;
68 	struct wfqinfo *qinfo, **top;
69 	int cnt = count;
70 
71 	strlcpy(wfq_stats.iface.wfq_ifacename, ifname,
72 		sizeof(wfq_stats.iface.wfq_ifacename));
73 
74 	/*
75 	 * first, find out how many queues are available
76 	 */
77 	for (i = 0; i < MAX_QSIZE; i++) {
78 		wfq_stats.qid = i;
79 		if (ioctl(fd, WFQ_GET_STATS, &wfq_stats) < 0)
80 			break;
81 	}
82 	nqueues = i;
83 	printf("wfq on %s: %d queues are used\n", ifname, nqueues);
84 
85 	if ((qinfo = malloc(nqueues * sizeof(struct wfqinfo))) == NULL)
86 		err(1, "malloc failed!");
87 	if ((top = malloc(ntop * sizeof(struct wfqinfo *))) == NULL)
88 		err(1, "malloc failed!");
89 
90 #ifndef NO_CURSES
91 	sleep(2);  /* wait a bit before clearing the screen */
92 
93 	initscr();
94 #endif
95 
96 	gettimeofday(&last_time, NULL);
97 	last_time.tv_sec -= interval;
98 
99 	while (count == 0 || cnt-- > 0) {
100 
101 		for (j = 0; j < ntop; j++)
102 			top[j] = NULL;
103 
104 		for (i = 0; i < nqueues; i++) {
105 			wfq_stats.qid = i;
106 			if (ioctl(fd, WFQ_GET_STATS, &wfq_stats) < 0)
107 				err(1, "ioctl WFQ_GET_STATS");
108 
109 			qinfo[i].qid = i;
110 			qinfo[i].stats = wfq_stats.stats;
111 		}
112 
113 		gettimeofday(&cur_time, NULL);
114 		sec = calc_interval(&cur_time, &last_time);
115 
116 		/*
117 		 * calculate the throughput of each queue
118 		 */
119 		for (i = 0; i < nqueues; i++) {
120 			qinfo[i].bps = calc_rate(qinfo[i].stats.xmit_cnt.bytes,
121 						 qinfo[i].last_bytes, sec);
122 			qinfo[i].last_bytes = qinfo[i].stats.xmit_cnt.bytes;
123 
124 			for (j = 0; j < ntop; j++) {
125 				if (top[j] == NULL) {
126 					top[j] = &qinfo[i];
127 					break;
128 				}
129 				if (top[j]->bps < qinfo[i].bps ||
130 				    (top[j]->bps == qinfo[i].bps &&
131 				     top[j]->stats.xmit_cnt.packets <
132 				     qinfo[i].stats.xmit_cnt.packets)) {
133 					for (k = ntop-1; k > j; k--)
134 						top[k] = top[k-1];
135 					top[j] = &qinfo[i];
136 					break;
137 				}
138 			}
139 		}
140 
141 		/*
142 		 * display top
143 		 */
144 		printf("[QID] WEIGHT QSIZE(KB) SENT(pkts)     (KB)       DROP(pkts)     (KB)     bps\n\r");
145 
146 		for (j = 0; j < ntop; j++) {
147 			if (top[j] != NULL)
148 				printf("[%4d] %4d %4d %10llu %14llu %10llu %14llu %9s\n\r",
149 				       top[j]->qid,
150 				       top[j]->stats.weight,
151 				       top[j]->stats.bytes / 1024,
152 				       (ull)top[j]->stats.xmit_cnt.packets,
153 				       (ull)top[j]->stats.xmit_cnt.bytes /1024,
154 				       (ull)top[j]->stats.drop_cnt.packets,
155 				       (ull)top[j]->stats.drop_cnt.bytes /1024,
156 				       rate2str(top[j]->bps));
157 			else
158 				printf("\n");
159 		}
160 #ifndef NO_CURSES
161 		refresh();
162 		mvcur(ntop+1, 0, 0, 0);
163 #endif
164 
165 		last_time = cur_time;
166 		sleep(interval);
167 	}
168 
169 }
170