xref: /freebsd/tools/tools/ath/athsurvey/athsurvey.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * Copyright (c) 2012 Adrian Chadd
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 #include "diag.h"
30 
31 #include "ah.h"
32 #include "ah_internal.h"
33 
34 #include <getopt.h>
35 #include <errno.h>
36 #include <err.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <unistd.h>
41 
42 const char *progname;
43 
44 static void
usage()45 usage()
46 {
47 	fprintf(stderr, "usage: %s [-i ifname]\n", progname);
48 	exit(-1);
49 }
50 
51 static int
get_survey_stats(int s,const char * ifname,HAL_CHANNEL_SURVEY * hs)52 get_survey_stats(int s, const char *ifname, HAL_CHANNEL_SURVEY *hs)
53 {
54 	uint16_t eedata;
55 	struct ath_diag atd;
56 
57 	memset(&atd, '\0', sizeof(atd));
58 
59 	atd.ad_id = HAL_DIAG_CHANSURVEY | ATH_DIAG_OUT;
60 	atd.ad_in_size = 0;
61 	atd.ad_in_data = NULL;
62 	atd.ad_out_size = sizeof(HAL_CHANNEL_SURVEY);
63 	atd.ad_out_data = (caddr_t) hs;
64 	strncpy(atd.ad_name, ifname, sizeof(atd.ad_name));
65 
66 	if (ioctl(s, SIOCGATHDIAG, &atd) < 0) {
67 		err(1, "ioctl: %s", atd.ad_name);
68 		return (0);
69 	}
70 	return (1);
71 }
72 
73 static void
process_survey_stats(HAL_CHANNEL_SURVEY * hs)74 process_survey_stats(HAL_CHANNEL_SURVEY *hs)
75 {
76 	int i;
77 	float tx = 0.0, rx = 0.0, cc = 0.0, cext = 0.0;
78 	float max_tx = 0.0, max_rx = 0.0, max_cc = 0.0, max_cext = 0.0;
79 	uint64_t avg_tx = 0, avg_rx = 0, avg_cc = 0, avg_cext = 0;
80 	float min_tx = 100.0, min_rx = 100.0, min_cc = 100.0, min_cext = 100.0;
81 	int n = 0;
82 	int cycle_count = 0, max_cycle_count = 0;
83 
84 	/* Calculate a percentage channel busy */
85 	for (i = 0; i < CHANNEL_SURVEY_SAMPLE_COUNT; i++) {
86 		/*
87 		 * Skip samples with no cycle count
88 		 */
89 		if (hs->samples[i].cycle_count == 0)
90 			continue;
91 		n++;
92 
93 		/*
94 		 * Grab cycle count, calculate maximum just for curiousity
95 		 */
96 		cycle_count = hs->samples[i].cycle_count;
97 		if (cycle_count > max_cycle_count)
98 			max_cycle_count = cycle_count;
99 
100 		/*
101 		 * Calculate percentage
102 		 */
103 		tx = (float) hs->samples[i].tx_busy * 100.0 /
104 		    hs->samples[i].cycle_count;
105 		rx = (float) hs->samples[i].rx_busy * 100.0 /
106 		    hs->samples[i].cycle_count;
107 		cc = (float) hs->samples[i].chan_busy * 100.0 /
108 		    hs->samples[i].cycle_count;
109 		cext = (float) hs->samples[i].ext_chan_busy * 100.0 /
110 		    hs->samples[i].cycle_count;
111 
112 		/*
113 		 * Update rolling average
114 		 * XXX to preserve some accuracy, keep two decimal points
115 		 * using "fixed" point math.
116 		 */
117 		avg_tx += (uint64_t) hs->samples[i].tx_busy * 10000 /
118 		    hs->samples[i].cycle_count;
119 		avg_rx += (uint64_t) hs->samples[i].rx_busy * 10000 /
120 		    hs->samples[i].cycle_count;
121 		avg_cc += (uint64_t) hs->samples[i].chan_busy * 10000 /
122 		    hs->samples[i].cycle_count;
123 		avg_cext += (uint64_t) hs->samples[i].ext_chan_busy * 10000 /
124 		    hs->samples[i].cycle_count;
125 
126 		/*
127 		 * Update maximum values
128 		 */
129 		if (tx > max_tx)
130 			max_tx = tx;
131 		if (rx > max_rx)
132 			max_rx = rx;
133 		if (cc > max_cc)
134 			max_cc = cc;
135 		if (cext > max_cext)
136 			max_cext = cext;
137 
138 		/*
139 		 * Update minimum values
140 		 */
141 		if (tx < min_tx)
142 			min_tx = tx;
143 		if (rx < min_rx)
144 			min_rx = rx;
145 		if (cc < min_cc)
146 			min_cc = cc;
147 		if (cext < min_cext)
148 			min_cext = cext;
149 	}
150 
151 	printf("(%4.1f %4.1f %4.1f %4.1f) ",
152 	    min_tx, min_rx, min_cc, min_cext);
153 	printf("(%4.1f %4.1f %4.1f %4.1f) ",
154 	    n == 0 ? 0.0 : (float) (avg_tx / n) / 100.0,
155 	    n == 0 ? 0.0 : (float) (avg_rx / n) / 100.0,
156 	    n == 0 ? 0.0 : (float) (avg_cc / n) / 100.0,
157 	    n == 0 ? 0.0 : (float) (avg_cext / n) / 100.0);
158 	printf("(%4.1f %4.1f %4.1f %4.1f)\n",
159 	    max_tx, max_rx, max_cc, max_cext);
160 }
161 
162 int
main(int argc,char * argv[])163 main(int argc, char *argv[])
164 {
165 	FILE *fd = NULL;
166 	const char *ifname;
167 	int c, s;
168 	int l = 0;
169 
170 	s = socket(AF_INET, SOCK_DGRAM, 0);
171 	if (s < 0)
172 		err(1, "socket");
173 	ifname = getenv("ATH");
174 	if (!ifname)
175 		ifname = ATH_DEFAULT;
176 
177 	progname = argv[0];
178 	while ((c = getopt(argc, argv, "i:")) != -1)
179 		switch (c) {
180 		case 'i':
181 			ifname = optarg;
182 			break;
183 		default:
184 			usage();
185 			/*NOTREACHED*/
186 		}
187 	argc -= optind;
188 	argv += optind;
189 
190 	/* Now, loop over and fetch some statistics .. */
191 	while (1) {
192 		HAL_CHANNEL_SURVEY hs;
193 		memset(&hs, '\0', sizeof(hs));
194 		if (get_survey_stats(s, ifname, &hs) == 0)
195 			break;
196 		/* XXX screen height! */
197 		if (l % 23 == 0) {
198 			printf("         "
199 			    "min                   "
200 			    "avg                   "
201 			    "max\n");
202 			printf("  tx%%  rx%%  bc%%  ec%%  ");
203 			printf("  tx%%  rx%%  bc%%  ec%%  ");
204 			printf("  tx%%  rx%%  bc%%  ec%%\n");
205 		}
206 		process_survey_stats(&hs);
207 		sleep(1);
208 		l++;
209 	}
210 
211 	return (0);
212 }
213 
214 
215