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
46 fprintf(stderr, "usage: diskinfo [-tv] disk ...\n");
50 static int opt_t, opt_v;
52 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
55 main(int argc, char **argv)
60 u_int sectorsize, fwsectors, fwheads;
62 while ((ch = getopt(argc, argv, "tv")) != -1) {
81 for (i = 0; i < argc; i++) {
82 fd = open(argv[i], O_RDONLY);
83 if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
84 sprintf(buf, "%s%s", _PATH_DEV, argv[i]);
85 fd = open(buf, O_RDONLY);
89 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
91 err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
92 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
94 err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
95 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
98 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
102 printf("%s", argv[i]);
103 printf("\t%u", sectorsize);
104 printf("\t%jd", (intmax_t)mediasize);
105 printf("\t%jd", (intmax_t)mediasize/sectorsize);
106 if (fwsectors != 0 && fwheads != 0) {
107 printf("\t%jd", (intmax_t)mediasize /
108 (fwsectors * fwheads * sectorsize));
109 printf("\t%u", fwheads);
110 printf("\t%u", fwsectors);
113 printf("%s\n", argv[i]);
114 printf("\t%-12u\t# sectorsize\n", sectorsize);
115 printf("\t%-12jd\t# mediasize in bytes\n",
116 (intmax_t)mediasize);
117 printf("\t%-12jd\t# mediasize in sectors\n",
118 (intmax_t)mediasize/sectorsize);
119 if (fwsectors != 0 && fwheads != 0) {
120 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
121 (fwsectors * fwheads * sectorsize));
122 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
123 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
128 speeddisk(fd, mediasize, sectorsize);
135 static char sector[65536];
136 static char mega[1024 * 1024];
139 rdsect(int fd, u_int blockno, u_int sectorsize)
143 lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
144 error = read(fd, sector, sectorsize);
145 if (error != (int)sectorsize)
146 err(1, "read error or disk too small for test.");
154 error = read(fd, mega, sizeof(mega));
155 if (error != sizeof(mega))
156 err(1, "read error or disk too small for test.");
159 static struct timeval tv1, tv2;
170 gettimeofday(&tv1, NULL);
178 gettimeofday(&tv2, NULL);
179 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
180 dt += (tv2.tv_sec - tv1.tv_sec);
181 printf("%5d iter in %10.6f sec = %8.3f msec\n",
182 count, dt, dt * 1000.0 / count);
190 gettimeofday(&tv2, NULL);
191 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
192 dt += (tv2.tv_sec - tv1.tv_sec);
193 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
194 count, dt, count / dt);
198 speeddisk(int fd, off_t mediasize, u_int sectorsize)
201 uint b0, b1, sectorcount;
203 sectorcount = mediasize / sectorsize;
205 printf("Seek times:\n");
206 printf("\tFull stroke:\t");
208 b1 = sectorcount - 1 - 16384;
210 for (i = 0; i < 125; i++) {
211 rdsect(fd, b0, sectorsize);
213 rdsect(fd, b1, sectorsize);
218 printf("\tHalf stroke:\t");
219 b0 = sectorcount / 4;
220 b1 = b0 + sectorcount / 2;
222 for (i = 0; i < 125; i++) {
223 rdsect(fd, b0, sectorsize);
225 rdsect(fd, b1, sectorsize);
229 printf("\tQuarter stroke:\t");
230 b0 = sectorcount / 4;
231 b1 = b0 + sectorcount / 4;
233 for (i = 0; i < 250; i++) {
234 rdsect(fd, b0, sectorsize);
236 rdsect(fd, b1, sectorsize);
241 printf("\tShort forward:\t");
242 b0 = sectorcount / 2;
244 for (i = 0; i < 400; i++) {
245 rdsect(fd, b0, sectorsize);
250 printf("\tShort backward:\t");
251 b0 = sectorcount / 2;
253 for (i = 0; i < 400; i++) {
254 rdsect(fd, b0, sectorsize);
259 printf("\tSeq outer:\t");
262 for (i = 0; i < 2048; i++) {
263 rdsect(fd, b0, sectorsize);
268 printf("\tSeq inner:\t");
269 b0 = sectorcount - 2048 - 1;
271 for (i = 0; i < 2048; i++) {
272 rdsect(fd, b0, sectorsize);
277 printf("Transfer rates:\n");
278 printf("\toutside: ");
279 rdsect(fd, 0, sectorsize);
281 for (i = 0; i < 100; i++) {
286 printf("\tmiddle: ");
287 b0 = sectorcount / 2;
288 rdsect(fd, b0, sectorsize);
290 for (i = 0; i < 100; i++) {
295 printf("\tinside: ");
296 b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
297 rdsect(fd, b0, sectorsize);
299 for (i = 0; i < 100; i++) {