xref: /freebsd/usr.sbin/diskinfo/diskinfo.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*-
2  * Copyright (c) 2003 Poul-Henning Kamp
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The names of the authors may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <libutil.h>
39 #include <paths.h>
40 #include <err.h>
41 #include <sys/disk.h>
42 #include <sys/time.h>
43 
44 static void
45 usage(void)
46 {
47 	fprintf(stderr, "usage: diskinfo [-ctv] disk ...\n");
48 	exit (1);
49 }
50 
51 static int opt_c, opt_t, opt_v;
52 
53 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
54 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
55 
56 int
57 main(int argc, char **argv)
58 {
59 	int i, ch, fd, error;
60 	char buf[BUFSIZ], ident[DISK_IDENT_SIZE];
61 	off_t	mediasize, stripesize, stripeoffset;
62 	u_int	sectorsize, fwsectors, fwheads;
63 
64 	while ((ch = getopt(argc, argv, "ctv")) != -1) {
65 		switch (ch) {
66 		case 'c':
67 			opt_c = 1;
68 			opt_v = 1;
69 			break;
70 		case 't':
71 			opt_t = 1;
72 			opt_v = 1;
73 			break;
74 		case 'v':
75 			opt_v = 1;
76 			break;
77 		default:
78 			usage();
79 		}
80 	}
81 	argc -= optind;
82 	argv += optind;
83 
84 	if (argc < 1)
85 		usage();
86 
87 	for (i = 0; i < argc; i++) {
88 		fd = open(argv[i], O_RDONLY);
89 		if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
90 			sprintf(buf, "%s%s", _PATH_DEV, argv[i]);
91 			fd = open(buf, O_RDONLY);
92 		}
93 		if (fd < 0)
94 			err(1, argv[i]);
95 		error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
96 		if (error)
97 			err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
98 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
99 		if (error)
100 			err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
101 		error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
102 		if (error)
103 			fwsectors = 0;
104 		error = ioctl(fd, DIOCGFWHEADS, &fwheads);
105 		if (error)
106 			fwheads = 0;
107 		error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
108 		if (error)
109 			stripesize = 0;
110 		error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
111 		if (error)
112 			stripeoffset = 0;
113 		if (!opt_v) {
114 			printf("%s", argv[i]);
115 			printf("\t%u", sectorsize);
116 			printf("\t%jd", (intmax_t)mediasize);
117 			printf("\t%jd", (intmax_t)mediasize/sectorsize);
118 			printf("\t%jd", (intmax_t)stripesize);
119 			printf("\t%jd", (intmax_t)stripeoffset);
120 			if (fwsectors != 0 && fwheads != 0) {
121 				printf("\t%jd", (intmax_t)mediasize /
122 				    (fwsectors * fwheads * sectorsize));
123 				printf("\t%u", fwheads);
124 				printf("\t%u", fwsectors);
125 			}
126 		} else {
127 			humanize_number(buf, 5, (int64_t)mediasize, "",
128 			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
129 			printf("%s\n", argv[i]);
130 			printf("\t%-12u\t# sectorsize\n", sectorsize);
131 			printf("\t%-12jd\t# mediasize in bytes (%s)\n",
132 			    (intmax_t)mediasize, buf);
133 			printf("\t%-12jd\t# mediasize in sectors\n",
134 			    (intmax_t)mediasize/sectorsize);
135 			printf("\t%-12jd\t# stripesize\n", stripesize);
136 			printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
137 			if (fwsectors != 0 && fwheads != 0) {
138 				printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
139 				    (fwsectors * fwheads * sectorsize));
140 				printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
141 				printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
142 			}
143 			if (ioctl(fd, DIOCGIDENT, ident) == 0)
144 				printf("\t%-12s\t# Disk ident.\n", ident);
145 		}
146 		printf("\n");
147 		if (opt_c)
148 			commandtime(fd, mediasize, sectorsize);
149 		if (opt_t)
150 			speeddisk(fd, mediasize, sectorsize);
151 		close(fd);
152 	}
153 	exit (0);
154 }
155 
156 
157 static char sector[65536];
158 static char mega[1024 * 1024];
159 
160 static void
161 rdsect(int fd, u_int blockno, u_int sectorsize)
162 {
163 	int error;
164 
165 	lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
166 	error = read(fd, sector, sectorsize);
167 	if (error != (int)sectorsize)
168 		err(1, "read error or disk too small for test.");
169 }
170 
171 static void
172 rdmega(int fd)
173 {
174 	int error;
175 
176 	error = read(fd, mega, sizeof(mega));
177 	if (error != sizeof(mega))
178 		err(1, "read error or disk too small for test.");
179 }
180 
181 static struct timeval tv1, tv2;
182 
183 static void
184 T0(void)
185 {
186 
187 	fflush(stdout);
188 	sync();
189 	sleep(1);
190 	sync();
191 	sync();
192 	gettimeofday(&tv1, NULL);
193 }
194 
195 static void
196 TN(int count)
197 {
198 	double dt;
199 
200 	gettimeofday(&tv2, NULL);
201 	dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
202 	dt += (tv2.tv_sec - tv1.tv_sec);
203 	printf("%5d iter in %10.6f sec = %8.3f msec\n",
204 		count, dt, dt * 1000.0 / count);
205 }
206 
207 static void
208 TR(double count)
209 {
210 	double dt;
211 
212 	gettimeofday(&tv2, NULL);
213 	dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
214 	dt += (tv2.tv_sec - tv1.tv_sec);
215 	printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
216 		count, dt, count / dt);
217 }
218 
219 static void
220 speeddisk(int fd, off_t mediasize, u_int sectorsize)
221 {
222 	int i;
223 	uint b0, b1, sectorcount;
224 
225 	sectorcount = mediasize / sectorsize;
226 
227 	printf("Seek times:\n");
228 	printf("\tFull stroke:\t");
229 	b0 = 0;
230 	b1 = sectorcount - 1 - 16384;
231 	T0();
232 	for (i = 0; i < 125; i++) {
233 		rdsect(fd, b0, sectorsize);
234 		b0 += 16384;
235 		rdsect(fd, b1, sectorsize);
236 		b1 -= 16384;
237 	}
238 	TN(250);
239 
240 	printf("\tHalf stroke:\t");
241 	b0 = sectorcount / 4;
242 	b1 = b0 + sectorcount / 2;
243 	T0();
244 	for (i = 0; i < 125; i++) {
245 		rdsect(fd, b0, sectorsize);
246 		b0 += 16384;
247 		rdsect(fd, b1, sectorsize);
248 		b1 += 16384;
249 	}
250 	TN(250);
251 	printf("\tQuarter stroke:\t");
252 	b0 = sectorcount / 4;
253 	b1 = b0 + sectorcount / 4;
254 	T0();
255 	for (i = 0; i < 250; i++) {
256 		rdsect(fd, b0, sectorsize);
257 		b0 += 16384;
258 		rdsect(fd, b1, sectorsize);
259 		b1 += 16384;
260 	}
261 	TN(500);
262 
263 	printf("\tShort forward:\t");
264 	b0 = sectorcount / 2;
265 	T0();
266 	for (i = 0; i < 400; i++) {
267 		rdsect(fd, b0, sectorsize);
268 		b0 += 16384;
269 	}
270 	TN(400);
271 
272 	printf("\tShort backward:\t");
273 	b0 = sectorcount / 2;
274 	T0();
275 	for (i = 0; i < 400; i++) {
276 		rdsect(fd, b0, sectorsize);
277 		b0 -= 16384;
278 	}
279 	TN(400);
280 
281 	printf("\tSeq outer:\t");
282 	b0 = 0;
283 	T0();
284 	for (i = 0; i < 2048; i++) {
285 		rdsect(fd, b0, sectorsize);
286 		b0++;
287 	}
288 	TN(2048);
289 
290 	printf("\tSeq inner:\t");
291 	b0 = sectorcount - 2048 - 1;
292 	T0();
293 	for (i = 0; i < 2048; i++) {
294 		rdsect(fd, b0, sectorsize);
295 		b0++;
296 	}
297 	TN(2048);
298 
299 	printf("Transfer rates:\n");
300 	printf("\toutside:     ");
301 	rdsect(fd, 0, sectorsize);
302 	T0();
303 	for (i = 0; i < 100; i++) {
304 		rdmega(fd);
305 	}
306 	TR(100 * 1024);
307 
308 	printf("\tmiddle:      ");
309 	b0 = sectorcount / 2;
310 	rdsect(fd, b0, sectorsize);
311 	T0();
312 	for (i = 0; i < 100; i++) {
313 		rdmega(fd);
314 	}
315 	TR(100 * 1024);
316 
317 	printf("\tinside:      ");
318 	b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
319 	rdsect(fd, b0, sectorsize);
320 	T0();
321 	for (i = 0; i < 100; i++) {
322 		rdmega(fd);
323 	}
324 	TR(100 * 1024);
325 
326 	printf("\n");
327 	return;
328 }
329 
330 static void
331 commandtime(int fd, off_t mediasize, u_int sectorsize)
332 {
333 	double dtmega, dtsector;
334 	int i;
335 
336 	printf("I/O command overhead:\n");
337 	i = mediasize;
338 	rdsect(fd, 0, sectorsize);
339 	T0();
340 	for (i = 0; i < 10; i++)
341 		rdmega(fd);
342 	gettimeofday(&tv2, NULL);
343 	dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6;
344 	dtmega += (tv2.tv_sec - tv1.tv_sec);
345 
346 	printf("\ttime to read 10MB block    %10.6f sec\t= %8.3f msec/sector\n",
347 		dtmega, dtmega*100/2048);
348 
349 	rdsect(fd, 0, sectorsize);
350 	T0();
351 	for (i = 0; i < 20480; i++)
352 		rdsect(fd, 0, sectorsize);
353 	gettimeofday(&tv2, NULL);
354 	dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6;
355 	dtsector += (tv2.tv_sec - tv1.tv_sec);
356 
357 	printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
358 		dtsector, dtsector*100/2048);
359 	printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
360 		(dtsector - dtmega)*100/2048);
361 
362 	printf("\n");
363 	return;
364 }
365