xref: /netbsd-src/usr.sbin/altq/altqstat/qdisc_fifoq.c (revision ba31dc0ca8176faff9f4d91a768d3d97468abbd5)
1*ba31dc0cSpeter /*	$NetBSD: qdisc_fifoq.c,v 1.5 2006/10/28 11:43:02 peter Exp $	*/
2dd191f37Speter /*	$KAME: qdisc_fifoq.c,v 1.6 2002/11/08 06:36:18 kjc Exp $	*/
38726857eSthorpej /*
48726857eSthorpej  * Copyright (C) 1999-2000
58726857eSthorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
68726857eSthorpej  *
78726857eSthorpej  * Redistribution and use in source and binary forms, with or without
88726857eSthorpej  * modification, are permitted provided that the following conditions
98726857eSthorpej  * are met:
108726857eSthorpej  * 1. Redistributions of source code must retain the above copyright
118726857eSthorpej  *    notice, this list of conditions and the following disclaimer.
128726857eSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
138726857eSthorpej  *    notice, this list of conditions and the following disclaimer in the
148726857eSthorpej  *    documentation and/or other materials provided with the distribution.
158726857eSthorpej  *
168726857eSthorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
178726857eSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188726857eSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198726857eSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
208726857eSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218726857eSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228726857eSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238726857eSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248726857eSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258726857eSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268726857eSthorpej  * SUCH DAMAGE.
278726857eSthorpej  */
288726857eSthorpej 
298726857eSthorpej #include <sys/param.h>
308726857eSthorpej #include <sys/ioctl.h>
318726857eSthorpej #include <sys/time.h>
328726857eSthorpej #include <sys/socket.h>
338726857eSthorpej #include <net/if.h>
348726857eSthorpej #include <netinet/in.h>
358726857eSthorpej #include <altq/altq.h>
368726857eSthorpej #include <altq/altq_fifoq.h>
378726857eSthorpej 
388726857eSthorpej #include <stdio.h>
398726857eSthorpej #include <stdlib.h>
408726857eSthorpej #include <unistd.h>
418726857eSthorpej #include <string.h>
42dd191f37Speter #include <signal.h>
438726857eSthorpej #include <errno.h>
448726857eSthorpej #include <err.h>
458726857eSthorpej 
468726857eSthorpej #include "altqstat.h"
478726857eSthorpej 
488726857eSthorpej void
fifoq_stat_loop(int fd,const char * ifname,int count,int interval)498726857eSthorpej fifoq_stat_loop(int fd, const char *ifname, int count, int interval)
508726857eSthorpej {
518726857eSthorpej 	struct fifoq_getstats get_stats;
528726857eSthorpej 	struct timeval cur_time, last_time;
538726857eSthorpej 	u_int64_t last_bytes;
548726857eSthorpej 	double sec;
558726857eSthorpej 	int cnt = count;
56dd191f37Speter 	sigset_t		omask;
578726857eSthorpej 
588b8734d2Sitojun 	strlcpy(get_stats.iface.fifoq_ifname, ifname,
598b8734d2Sitojun 		sizeof(get_stats.iface.fifoq_ifname));
608726857eSthorpej 
618726857eSthorpej 	gettimeofday(&last_time, NULL);
628726857eSthorpej 	last_time.tv_sec -= interval;
638726857eSthorpej 	last_bytes = 0;
648726857eSthorpej 
65*ba31dc0cSpeter 	for (;;) {
668726857eSthorpej 		if (ioctl(fd, FIFOQ_GETSTATS, &get_stats) < 0)
678726857eSthorpej 			err(1, "ioctl FIFOQ_GETSTATS");
688726857eSthorpej 
698726857eSthorpej 		gettimeofday(&cur_time, NULL);
708726857eSthorpej 		sec = calc_interval(&cur_time, &last_time);
718726857eSthorpej 
728726857eSthorpej 		printf(" q_len:%d q_limit:%d period:%u\n",
738726857eSthorpej 		       get_stats.q_len, get_stats.q_limit, get_stats.period);
748726857eSthorpej 		printf(" xmit:%llu pkts (%llu bytes) drop:%llu pkts (%llu bytes)\n",
758726857eSthorpej 		       (ull)get_stats.xmit_cnt.packets,
768726857eSthorpej 		       (ull)get_stats.xmit_cnt.bytes,
778726857eSthorpej 		       (ull)get_stats.drop_cnt.packets,
788726857eSthorpej 		       (ull)get_stats.drop_cnt.bytes);
798726857eSthorpej 		printf(" throughput: %sbps\n",
808726857eSthorpej 		       rate2str(calc_rate(get_stats.xmit_cnt.bytes,
818726857eSthorpej 					  last_bytes, sec)));
828726857eSthorpej 
838726857eSthorpej 		last_bytes = get_stats.xmit_cnt.bytes;
848726857eSthorpej 		last_time = cur_time;
85dd191f37Speter 
86*ba31dc0cSpeter 		if (count != 0 && --cnt == 0)
87*ba31dc0cSpeter 			break;
88*ba31dc0cSpeter 
89dd191f37Speter 		/* wait for alarm signal */
90dd191f37Speter 		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
91dd191f37Speter 			sigsuspend(&omask);
928726857eSthorpej 	}
938726857eSthorpej }
94