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 [-tv] disk ...\n");
51 static int opt_t, opt_v;
53 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
56 main(int argc, char **argv)
61 u_int sectorsize, fwsectors, fwheads;
63 while ((ch = getopt(argc, argv, "tv")) != -1) {
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);
90 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
92 err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
93 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
95 err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
96 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
99 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
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);
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);
131 speeddisk(fd, mediasize, sectorsize);
138 static char sector[65536];
139 static char mega[1024 * 1024];
142 rdsect(int fd, u_int blockno, u_int sectorsize)
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.");
157 error = read(fd, mega, sizeof(mega));
158 if (error != sizeof(mega))
159 err(1, "read error or disk too small for test.");
162 static struct timeval tv1, tv2;
173 gettimeofday(&tv1, NULL);
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);
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);
201 speeddisk(int fd, off_t mediasize, u_int sectorsize)
204 uint b0, b1, sectorcount;
206 sectorcount = mediasize / sectorsize;
208 printf("Seek times:\n");
209 printf("\tFull stroke:\t");
211 b1 = sectorcount - 1 - 16384;
213 for (i = 0; i < 125; i++) {
214 rdsect(fd, b0, sectorsize);
216 rdsect(fd, b1, sectorsize);
221 printf("\tHalf stroke:\t");
222 b0 = sectorcount / 4;
223 b1 = b0 + sectorcount / 2;
225 for (i = 0; i < 125; i++) {
226 rdsect(fd, b0, sectorsize);
228 rdsect(fd, b1, sectorsize);
232 printf("\tQuarter stroke:\t");
233 b0 = sectorcount / 4;
234 b1 = b0 + sectorcount / 4;
236 for (i = 0; i < 250; i++) {
237 rdsect(fd, b0, sectorsize);
239 rdsect(fd, b1, sectorsize);
244 printf("\tShort forward:\t");
245 b0 = sectorcount / 2;
247 for (i = 0; i < 400; i++) {
248 rdsect(fd, b0, sectorsize);
253 printf("\tShort backward:\t");
254 b0 = sectorcount / 2;
256 for (i = 0; i < 400; i++) {
257 rdsect(fd, b0, sectorsize);
262 printf("\tSeq outer:\t");
265 for (i = 0; i < 2048; i++) {
266 rdsect(fd, b0, sectorsize);
271 printf("\tSeq inner:\t");
272 b0 = sectorcount - 2048 - 1;
274 for (i = 0; i < 2048; i++) {
275 rdsect(fd, b0, sectorsize);
280 printf("Transfer rates:\n");
281 printf("\toutside: ");
282 rdsect(fd, 0, sectorsize);
284 for (i = 0; i < 100; i++) {
289 printf("\tmiddle: ");
290 b0 = sectorcount / 2;
291 rdsect(fd, b0, sectorsize);
293 for (i = 0; i < 100; i++) {
298 printf("\tinside: ");
299 b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
300 rdsect(fd, b0, sectorsize);
302 for (i = 0; i < 100; i++) {