2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
47 fprintf(stderr, "usage: diskinfo [-ctv] disk ...\n");
51 static int opt_c, opt_t, opt_v;
53 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
54 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
57 main(int argc, char **argv)
62 u_int sectorsize, fwsectors, fwheads;
64 while ((ch = getopt(argc, argv, "ctv")) != -1) {
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);
95 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
97 err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
98 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
100 err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
101 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
104 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
108 printf("%s", argv[i]);
109 printf("\t%u", sectorsize);
110 printf("\t%jd", (intmax_t)mediasize);
111 printf("\t%jd", (intmax_t)mediasize/sectorsize);
112 if (fwsectors != 0 && fwheads != 0) {
113 printf("\t%jd", (intmax_t)mediasize /
114 (fwsectors * fwheads * sectorsize));
115 printf("\t%u", fwheads);
116 printf("\t%u", fwsectors);
119 humanize_number(buf, 5, (int64_t)mediasize, "",
120 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
121 printf("%s\n", argv[i]);
122 printf("\t%-12u\t# sectorsize\n", sectorsize);
123 printf("\t%-12jd\t# mediasize in bytes (%s)\n",
124 (intmax_t)mediasize, buf);
125 printf("\t%-12jd\t# mediasize in sectors\n",
126 (intmax_t)mediasize/sectorsize);
127 if (fwsectors != 0 && fwheads != 0) {
128 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
129 (fwsectors * fwheads * sectorsize));
130 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
131 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
136 commandtime(fd, mediasize, sectorsize);
138 speeddisk(fd, mediasize, sectorsize);
145 static char sector[65536];
146 static char mega[1024 * 1024];
149 rdsect(int fd, u_int blockno, u_int sectorsize)
153 lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
154 error = read(fd, sector, sectorsize);
155 if (error != (int)sectorsize)
156 err(1, "read error or disk too small for test.");
164 error = read(fd, mega, sizeof(mega));
165 if (error != sizeof(mega))
166 err(1, "read error or disk too small for test.");
169 static struct timeval tv1, tv2;
180 gettimeofday(&tv1, NULL);
188 gettimeofday(&tv2, NULL);
189 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
190 dt += (tv2.tv_sec - tv1.tv_sec);
191 printf("%5d iter in %10.6f sec = %8.3f msec\n",
192 count, dt, dt * 1000.0 / count);
200 gettimeofday(&tv2, NULL);
201 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
202 dt += (tv2.tv_sec - tv1.tv_sec);
203 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
204 count, dt, count / dt);
208 speeddisk(int fd, off_t mediasize, u_int sectorsize)
211 uint b0, b1, sectorcount;
213 sectorcount = mediasize / sectorsize;
215 printf("Seek times:\n");
216 printf("\tFull stroke:\t");
218 b1 = sectorcount - 1 - 16384;
220 for (i = 0; i < 125; i++) {
221 rdsect(fd, b0, sectorsize);
223 rdsect(fd, b1, sectorsize);
228 printf("\tHalf stroke:\t");
229 b0 = sectorcount / 4;
230 b1 = b0 + sectorcount / 2;
232 for (i = 0; i < 125; i++) {
233 rdsect(fd, b0, sectorsize);
235 rdsect(fd, b1, sectorsize);
239 printf("\tQuarter stroke:\t");
240 b0 = sectorcount / 4;
241 b1 = b0 + sectorcount / 4;
243 for (i = 0; i < 250; i++) {
244 rdsect(fd, b0, sectorsize);
246 rdsect(fd, b1, sectorsize);
251 printf("\tShort forward:\t");
252 b0 = sectorcount / 2;
254 for (i = 0; i < 400; i++) {
255 rdsect(fd, b0, sectorsize);
260 printf("\tShort backward:\t");
261 b0 = sectorcount / 2;
263 for (i = 0; i < 400; i++) {
264 rdsect(fd, b0, sectorsize);
269 printf("\tSeq outer:\t");
272 for (i = 0; i < 2048; i++) {
273 rdsect(fd, b0, sectorsize);
278 printf("\tSeq inner:\t");
279 b0 = sectorcount - 2048 - 1;
281 for (i = 0; i < 2048; i++) {
282 rdsect(fd, b0, sectorsize);
287 printf("Transfer rates:\n");
288 printf("\toutside: ");
289 rdsect(fd, 0, sectorsize);
291 for (i = 0; i < 100; i++) {
296 printf("\tmiddle: ");
297 b0 = sectorcount / 2;
298 rdsect(fd, b0, sectorsize);
300 for (i = 0; i < 100; i++) {
305 printf("\tinside: ");
306 b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
307 rdsect(fd, b0, sectorsize);
309 for (i = 0; i < 100; i++) {
319 commandtime(int fd, off_t mediasize, u_int sectorsize)
321 double dtmega, dtsector;
324 printf("I/O command overhead:\n");
326 rdsect(fd, 0, sectorsize);
328 for (i = 0; i < 10; i++)
330 gettimeofday(&tv2, NULL);
331 dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6;
332 dtmega += (tv2.tv_sec - tv1.tv_sec);
334 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
335 dtmega, dtmega*100/2048);
337 rdsect(fd, 0, sectorsize);
339 for (i = 0; i < 20480; i++)
340 rdsect(fd, 0, sectorsize);
341 gettimeofday(&tv2, NULL);
342 dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6;
343 dtsector += (tv2.tv_sec - tv1.tv_sec);
345 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
346 dtsector, dtsector*100/2048);
347 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
348 (dtsector - dtmega)*100/2048);