xref: /freebsd/usr.sbin/diskinfo/diskinfo.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 [-tv] disk ...\n");
48 	exit (1);
49 }
50 
51 static int opt_t, opt_v;
52 
53 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
54 
55 int
56 main(int argc, char **argv)
57 {
58 	int i, ch, fd, error;
59 	char buf[BUFSIZ];
60 	off_t	mediasize;
61 	u_int	sectorsize, fwsectors, fwheads;
62 
63 	while ((ch = getopt(argc, argv, "tv")) != -1) {
64 		switch (ch) {
65 		case 't':
66 			opt_t = 1;
67 			opt_v = 1;
68 			break;
69 		case 'v':
70 			opt_v = 1;
71 			break;
72 		default:
73 			usage();
74 		}
75 	}
76 	argc -= optind;
77 	argv += optind;
78 
79 	if (argc < 1)
80 		usage();
81 
82 	for (i = 0; i < argc; i++) {
83 		fd = open(argv[i], O_RDONLY);
84 		if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
85 			sprintf(buf, "%s%s", _PATH_DEV, argv[i]);
86 			fd = open(buf, O_RDONLY);
87 		}
88 		if (fd < 0)
89 			err(1, argv[i]);
90 		error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
91 		if (error)
92 			err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
93 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
94 		if (error)
95 			err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
96 		error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
97 		if (error)
98 			fwsectors = 0;
99 		error = ioctl(fd, DIOCGFWHEADS, &fwheads);
100 		if (error)
101 			fwheads = 0;
102 		if (!opt_v) {
103 			printf("%s", argv[i]);
104 			printf("\t%u", sectorsize);
105 			printf("\t%jd", (intmax_t)mediasize);
106 			printf("\t%jd", (intmax_t)mediasize/sectorsize);
107 			if (fwsectors != 0 && fwheads != 0) {
108 				printf("\t%jd", (intmax_t)mediasize /
109 				    (fwsectors * fwheads * sectorsize));
110 				printf("\t%u", fwheads);
111 				printf("\t%u", fwsectors);
112 			}
113 		} else {
114 			humanize_number(buf, 5, (int64_t)mediasize, "",
115 			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
116 			printf("%s\n", argv[i]);
117 			printf("\t%-12u\t# sectorsize\n", sectorsize);
118 			printf("\t%-12jd\t# mediasize in bytes (%s)\n",
119 			    (intmax_t)mediasize, buf);
120 			printf("\t%-12jd\t# mediasize in sectors\n",
121 			    (intmax_t)mediasize/sectorsize);
122 			if (fwsectors != 0 && fwheads != 0) {
123 				printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
124 				    (fwsectors * fwheads * sectorsize));
125 				printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
126 				printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
127 			}
128 		}
129 		printf("\n");
130 		if (opt_t)
131 			speeddisk(fd, mediasize, sectorsize);
132 		close(fd);
133 	}
134 	exit (0);
135 }
136 
137 
138 static char sector[65536];
139 static char mega[1024 * 1024];
140 
141 static void
142 rdsect(int fd, u_int blockno, u_int sectorsize)
143 {
144 	int error;
145 
146 	lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
147 	error = read(fd, sector, sectorsize);
148 	if (error != (int)sectorsize)
149 		err(1, "read error or disk too small for test.");
150 }
151 
152 static void
153 rdmega(int fd)
154 {
155 	int error;
156 
157 	error = read(fd, mega, sizeof(mega));
158 	if (error != sizeof(mega))
159 		err(1, "read error or disk too small for test.");
160 }
161 
162 static struct timeval tv1, tv2;
163 
164 static void
165 T0(void)
166 {
167 
168 	fflush(stdout);
169 	sync();
170 	sleep(1);
171 	sync();
172 	sync();
173 	gettimeofday(&tv1, NULL);
174 }
175 
176 static void
177 TN(int count)
178 {
179 	double dt;
180 
181 	gettimeofday(&tv2, NULL);
182 	dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
183 	dt += (tv2.tv_sec - tv1.tv_sec);
184 	printf("%5d iter in %10.6f sec = %8.3f msec\n",
185 		count, dt, dt * 1000.0 / count);
186 }
187 
188 static void
189 TR(double count)
190 {
191 	double dt;
192 
193 	gettimeofday(&tv2, NULL);
194 	dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
195 	dt += (tv2.tv_sec - tv1.tv_sec);
196 	printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
197 		count, dt, count / dt);
198 }
199 
200 static void
201 speeddisk(int fd, off_t mediasize, u_int sectorsize)
202 {
203 	int i;
204 	uint b0, b1, sectorcount;
205 
206 	sectorcount = mediasize / sectorsize;
207 
208 	printf("Seek times:\n");
209 	printf("\tFull stroke:\t");
210 	b0 = 0;
211 	b1 = sectorcount - 1 - 16384;
212 	T0();
213 	for (i = 0; i < 125; i++) {
214 		rdsect(fd, b0, sectorsize);
215 		b0 += 16384;
216 		rdsect(fd, b1, sectorsize);
217 		b1 -= 16384;
218 	}
219 	TN(250);
220 
221 	printf("\tHalf stroke:\t");
222 	b0 = sectorcount / 4;
223 	b1 = b0 + sectorcount / 2;
224 	T0();
225 	for (i = 0; i < 125; i++) {
226 		rdsect(fd, b0, sectorsize);
227 		b0 += 16384;
228 		rdsect(fd, b1, sectorsize);
229 		b1 += 16384;
230 	}
231 	TN(250);
232 	printf("\tQuarter stroke:\t");
233 	b0 = sectorcount / 4;
234 	b1 = b0 + sectorcount / 4;
235 	T0();
236 	for (i = 0; i < 250; i++) {
237 		rdsect(fd, b0, sectorsize);
238 		b0 += 16384;
239 		rdsect(fd, b1, sectorsize);
240 		b1 += 16384;
241 	}
242 	TN(500);
243 
244 	printf("\tShort forward:\t");
245 	b0 = sectorcount / 2;
246 	T0();
247 	for (i = 0; i < 400; i++) {
248 		rdsect(fd, b0, sectorsize);
249 		b0 += 16384;
250 	}
251 	TN(400);
252 
253 	printf("\tShort backward:\t");
254 	b0 = sectorcount / 2;
255 	T0();
256 	for (i = 0; i < 400; i++) {
257 		rdsect(fd, b0, sectorsize);
258 		b0 -= 16384;
259 	}
260 	TN(400);
261 
262 	printf("\tSeq outer:\t");
263 	b0 = 0;
264 	T0();
265 	for (i = 0; i < 2048; i++) {
266 		rdsect(fd, b0, sectorsize);
267 		b0++;
268 	}
269 	TN(2048);
270 
271 	printf("\tSeq inner:\t");
272 	b0 = sectorcount - 2048 - 1;
273 	T0();
274 	for (i = 0; i < 2048; i++) {
275 		rdsect(fd, b0, sectorsize);
276 		b0++;
277 	}
278 	TN(2048);
279 
280 	printf("Transfer rates:\n");
281 	printf("\toutside:     ");
282 	rdsect(fd, 0, sectorsize);
283 	T0();
284 	for (i = 0; i < 100; i++) {
285 		rdmega(fd);
286 	}
287 	TR(100 * 1024);
288 
289 	printf("\tmiddle:      ");
290 	b0 = sectorcount / 2;
291 	rdsect(fd, b0, sectorsize);
292 	T0();
293 	for (i = 0; i < 100; i++) {
294 		rdmega(fd);
295 	}
296 	TR(100 * 1024);
297 
298 	printf("\tinside:      ");
299 	b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
300 	rdsect(fd, b0, sectorsize);
301 	T0();
302 	for (i = 0; i < 100; i++) {
303 		rdmega(fd);
304 	}
305 	TR(100 * 1024);
306 
307 	printf("\n");
308 
309 	return;
310 }
311