Home | History | Annotate | Line # | Download | only in altqstat
qdisc_wfq.c revision 1.4
      1 /*	$NetBSD: qdisc_wfq.c,v 1.4 2006/10/12 19:59:13 peter Exp $	*/
      2 /*	$KAME: qdisc_wfq.c,v 1.5 2002/11/08 06:36:18 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 <signal.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 	sigset_t		omask;
     71 
     72 	strlcpy(wfq_stats.iface.wfq_ifacename, ifname,
     73 		sizeof(wfq_stats.iface.wfq_ifacename));
     74 
     75 	/*
     76 	 * first, find out how many queues are available
     77 	 */
     78 	for (i = 0; i < MAX_QSIZE; i++) {
     79 		wfq_stats.qid = i;
     80 		if (ioctl(fd, WFQ_GET_STATS, &wfq_stats) < 0)
     81 			break;
     82 	}
     83 	nqueues = i;
     84 	printf("wfq on %s: %d queues are used\n", ifname, nqueues);
     85 
     86 	if ((qinfo = malloc(nqueues * sizeof(struct wfqinfo))) == NULL)
     87 		err(1, "malloc failed!");
     88 	if ((top = malloc(ntop * sizeof(struct wfqinfo *))) == NULL)
     89 		err(1, "malloc failed!");
     90 
     91 #ifndef NO_CURSES
     92 	sleep(2);  /* wait a bit before clearing the screen */
     93 
     94 	initscr();
     95 #endif
     96 
     97 	gettimeofday(&last_time, NULL);
     98 	last_time.tv_sec -= interval;
     99 
    100 	while (count == 0 || cnt-- > 0) {
    101 
    102 		for (j = 0; j < ntop; j++)
    103 			top[j] = NULL;
    104 
    105 		for (i = 0; i < nqueues; i++) {
    106 			wfq_stats.qid = i;
    107 			if (ioctl(fd, WFQ_GET_STATS, &wfq_stats) < 0)
    108 				err(1, "ioctl WFQ_GET_STATS");
    109 
    110 			qinfo[i].qid = i;
    111 			qinfo[i].stats = wfq_stats.stats;
    112 		}
    113 
    114 		gettimeofday(&cur_time, NULL);
    115 		sec = calc_interval(&cur_time, &last_time);
    116 
    117 		/*
    118 		 * calculate the throughput of each queue
    119 		 */
    120 		for (i = 0; i < nqueues; i++) {
    121 			qinfo[i].bps = calc_rate(qinfo[i].stats.xmit_cnt.bytes,
    122 						 qinfo[i].last_bytes, sec);
    123 			qinfo[i].last_bytes = qinfo[i].stats.xmit_cnt.bytes;
    124 
    125 			for (j = 0; j < ntop; j++) {
    126 				if (top[j] == NULL) {
    127 					top[j] = &qinfo[i];
    128 					break;
    129 				}
    130 				if (top[j]->bps < qinfo[i].bps ||
    131 				    (top[j]->bps == qinfo[i].bps &&
    132 				     top[j]->stats.xmit_cnt.packets <
    133 				     qinfo[i].stats.xmit_cnt.packets)) {
    134 					for (k = ntop-1; k > j; k--)
    135 						top[k] = top[k-1];
    136 					top[j] = &qinfo[i];
    137 					break;
    138 				}
    139 			}
    140 		}
    141 
    142 		/*
    143 		 * display top
    144 		 */
    145 		printf("[QID] WEIGHT QSIZE(KB) SENT(pkts)     (KB)       DROP(pkts)     (KB)     bps\n\r");
    146 
    147 		for (j = 0; j < ntop; j++) {
    148 			if (top[j] != NULL)
    149 				printf("[%4d] %4d %4d %10llu %14llu %10llu %14llu %9s\n\r",
    150 				       top[j]->qid,
    151 				       top[j]->stats.weight,
    152 				       top[j]->stats.bytes / 1024,
    153 				       (ull)top[j]->stats.xmit_cnt.packets,
    154 				       (ull)top[j]->stats.xmit_cnt.bytes /1024,
    155 				       (ull)top[j]->stats.drop_cnt.packets,
    156 				       (ull)top[j]->stats.drop_cnt.bytes /1024,
    157 				       rate2str(top[j]->bps));
    158 			else
    159 				printf("\n");
    160 		}
    161 #ifndef NO_CURSES
    162 		refresh();
    163 		mvcur(ntop+1, 0, 0, 0);
    164 #endif
    165 
    166 		last_time = cur_time;
    167 
    168 		/* wait for alarm signal */
    169 		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
    170 			sigsuspend(&omask);
    171 	}
    172 
    173 }
    174