Home | History | Annotate | Line # | Download | only in altqstat
qdisc_hfsc.c revision 1.5
      1 /*	$NetBSD: qdisc_hfsc.c,v 1.5 2006/10/12 19:59:13 peter Exp $	*/
      2 /*	$KAME: qdisc_hfsc.c,v 1.8 2003/07/10 12:09:38 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_hfsc.h>
     37 
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <unistd.h>
     41 #include <string.h>
     42 #include <signal.h>
     43 #include <math.h>
     44 #include <errno.h>
     45 #include <err.h>
     46 
     47 #include "quip_client.h"
     48 #include "altqstat.h"
     49 
     50 #define NCLASSES	64
     51 
     52 void
     53 hfsc_stat_loop(int fd, const char *ifname, int count, int interval)
     54 {
     55 	struct hfsc_classstats	stats1[NCLASSES], stats2[NCLASSES];
     56 	char			clnames[NCLASSES][128];
     57 	struct hfsc_class_stats	get_stats;
     58 	struct hfsc_classstats	*sp, *lp, *new, *last, *tmp;
     59 	struct timeval		cur_time, last_time;
     60 	int			i;
     61 	double			sec;
     62 	int			cnt = count;
     63 	sigset_t		omask;
     64 	u_int64_t		stattime;
     65 	u_int32_t		machclk_freq;
     66 
     67 	strlcpy(get_stats.iface.hfsc_ifname, ifname,
     68 		sizeof(get_stats.iface.hfsc_ifname));
     69 	new = &stats1[0];
     70 	last = &stats2[0];
     71 
     72 	/* invalidate class ids */
     73 	for (i=0; i<NCLASSES; i++)
     74 		last[i].class_id = 999999; /* XXX */
     75 
     76 	while (count == 0 || cnt-- > 0) {
     77 		get_stats.nskip = 0;
     78 		get_stats.nclasses = NCLASSES;
     79 		get_stats.stats = new;
     80 
     81 		if (ioctl(fd, HFSC_GETSTATS, &get_stats) < 0)
     82 			err(1, "ioctl HFSC_GETSTATS");
     83 
     84 		gettimeofday(&cur_time, NULL);
     85 		sec = calc_interval(&cur_time, &last_time);
     86 
     87 		stattime = get_stats.cur_time;
     88 		machclk_freq = get_stats.machclk_freq;
     89 		printf("\ncur_time:%#llx %u classes %u packets in the tree\n",
     90 		    (ull)stattime,
     91 		    get_stats.hif_classes, get_stats.hif_packets);
     92 
     93 		for (i=0; i<get_stats.nclasses; i++) {
     94 			sp = &new[i];
     95 			lp = &last[i];
     96 
     97 			if (sp->class_id != lp->class_id) {
     98 				quip_chandle2name(ifname, sp->class_handle,
     99 						  clnames[i], sizeof(clnames[0]));
    100 				continue;
    101 			}
    102 
    103 			printf("[%2d %s] handle:%#x [rt %s %ums %s][ls %s %ums %s]",
    104 			       sp->class_id, clnames[i], sp->class_handle,
    105 			       rate2str((double)sp->rsc.m1), sp->rsc.d,
    106 			       rate2str((double)sp->rsc.m2),
    107 			       rate2str((double)sp->fsc.m1), sp->fsc.d,
    108 			       rate2str((double)sp->fsc.m2));
    109 			if (sp->usc.m1 != 0 || sp->usc.m2 != 0)
    110 				printf("[ul %s %ums %s]\n",
    111 				       rate2str((double)sp->usc.m1), sp->usc.d,
    112 				       rate2str((double)sp->usc.m2));
    113 			else
    114 				 printf("\n");
    115 			printf("  measured: %sbps [rt:%s ls:%s] qlen:%2d period:%u\n",
    116 			       rate2str(calc_rate(sp->total, lp->total, sec)),
    117 			       rate2str(calc_rate(sp->cumul, lp->cumul, sec)),
    118 			       rate2str(calc_rate(sp->total - sp->cumul,
    119 						  lp->total - lp->cumul, sec)),
    120 			       sp->qlength, sp->period);
    121 			printf("     packets:%llu (%llu bytes) drops:%llu (%llu bytes) \n",
    122 			       (ull)sp->xmit_cnt.packets,
    123 			       (ull)sp->xmit_cnt.bytes,
    124 			       (ull)sp->drop_cnt.packets,
    125 			       (ull)sp->drop_cnt.bytes);
    126 			printf("     cumul:%#llx total:%#llx\n",
    127 			       (ull)sp->cumul, (ull)sp->total);
    128 			printf("     e:%.2fus d:%.2fus f:%.2fus vt:%#llx\n",
    129 			    sp->e == 0 ? 0 : ((double)sp->e - (double)stattime)
    130 			    		     / machclk_freq * 1000000,
    131 			    sp->d == 0 ? 0 : ((double)sp->d - (double)stattime)
    132 			    		     / machclk_freq * 1000000,
    133 			    sp->f == 0 ? 0 : ((double)sp->f - (double)stattime)
    134 			    		     / machclk_freq * 1000000,
    135 			    (ull)sp->vt);
    136 			printf("     vtperiod:%u parentperiod:%u nactive:%d\n",
    137 			    sp->vtperiod, sp->parentperiod, sp->nactive);
    138 			printf("     initvt:%#llx cvtmax:%#llx vtoff:%#llx\n",
    139 			    (ull)sp->initvt, (ull)sp->cvtmax, (ull)sp->vtoff);
    140 
    141 			printf("     myf:%#llx cfmin:%#llx cvtmin:%#llx",
    142 			    (ull)sp->myf, (ull)sp->cfmin, (ull)sp->cvtmin);
    143 			printf(" myfadj:%#llx vtadj:%#llx\n",
    144 			    (ull)sp->myfadj, (ull)sp->vtadj);
    145 			if (sp->qtype == Q_RED)
    146 				print_redstats(sp->red);
    147 			else if (sp->qtype == Q_RIO)
    148 				print_riostats(sp->red);
    149 		}
    150 
    151 		/* swap the buffer pointers */
    152 		tmp = last;
    153 		last = new;
    154 		new = tmp;
    155 
    156 		last_time = cur_time;
    157 
    158 		/* wait for alarm signal */
    159 		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
    160 			sigsuspend(&omask);
    161 	}
    162 }
    163