]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/diskinfo/diskinfo.c
Update dialog to version 1.1-20110302.
[FreeBSD/FreeBSD.git] / usr.sbin / diskinfo / diskinfo.c
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, exitval = 0;
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                         warn("%s", argv[i]);
95                         exitval = 1;
96                         goto out;
97                 }
98                 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
99                 if (error) {
100                         warn("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
101                         exitval = 1;
102                         goto out;
103                 }
104                 error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
105                 if (error) {
106                         warn("%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]);
107                         exitval = 1;
108                         goto out;
109                 }
110                 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
111                 if (error)
112                         fwsectors = 0;
113                 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
114                 if (error)
115                         fwheads = 0;
116                 error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
117                 if (error)
118                         stripesize = 0;
119                 error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
120                 if (error)
121                         stripeoffset = 0;
122                 if (!opt_v) {
123                         printf("%s", argv[i]);
124                         printf("\t%u", sectorsize);
125                         printf("\t%jd", (intmax_t)mediasize);
126                         printf("\t%jd", (intmax_t)mediasize/sectorsize);
127                         printf("\t%jd", (intmax_t)stripesize);
128                         printf("\t%jd", (intmax_t)stripeoffset);
129                         if (fwsectors != 0 && fwheads != 0) {
130                                 printf("\t%jd", (intmax_t)mediasize /
131                                     (fwsectors * fwheads * sectorsize));
132                                 printf("\t%u", fwheads);
133                                 printf("\t%u", fwsectors);
134                         } 
135                 } else {
136                         humanize_number(buf, 5, (int64_t)mediasize, "",
137                             HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
138                         printf("%s\n", argv[i]);
139                         printf("\t%-12u\t# sectorsize\n", sectorsize);
140                         printf("\t%-12jd\t# mediasize in bytes (%s)\n",
141                             (intmax_t)mediasize, buf);
142                         printf("\t%-12jd\t# mediasize in sectors\n",
143                             (intmax_t)mediasize/sectorsize);
144                         printf("\t%-12jd\t# stripesize\n", stripesize);
145                         printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
146                         if (fwsectors != 0 && fwheads != 0) {
147                                 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
148                                     (fwsectors * fwheads * sectorsize));
149                                 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
150                                 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
151                         } 
152                         if (ioctl(fd, DIOCGIDENT, ident) == 0)
153                                 printf("\t%-12s\t# Disk ident.\n", ident);
154                 }
155                 printf("\n");
156                 if (opt_c)
157                         commandtime(fd, mediasize, sectorsize);
158                 if (opt_t)
159                         speeddisk(fd, mediasize, sectorsize);
160 out:
161                 close(fd);
162         }
163         exit (exitval);
164 }
165
166
167 static char sector[65536];
168 static char mega[1024 * 1024];
169
170 static void
171 rdsect(int fd, u_int blockno, u_int sectorsize)
172 {
173         int error;
174
175         lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
176         error = read(fd, sector, sectorsize);
177         if (error != (int)sectorsize)
178                 err(1, "read error or disk too small for test.");
179 }
180
181 static void
182 rdmega(int fd)
183 {
184         int error;
185
186         error = read(fd, mega, sizeof(mega));
187         if (error != sizeof(mega))
188                 err(1, "read error or disk too small for test.");
189 }
190
191 static struct timeval tv1, tv2;
192
193 static void
194 T0(void)
195 {
196
197         fflush(stdout);
198         sync();
199         sleep(1);
200         sync();
201         sync();
202         gettimeofday(&tv1, NULL);
203 }
204
205 static void
206 TN(int count)
207 {
208         double dt;
209
210         gettimeofday(&tv2, NULL);
211         dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
212         dt += (tv2.tv_sec - tv1.tv_sec);
213         printf("%5d iter in %10.6f sec = %8.3f msec\n",
214                 count, dt, dt * 1000.0 / count);
215 }
216
217 static void
218 TR(double count)
219 {
220         double dt;
221
222         gettimeofday(&tv2, NULL);
223         dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
224         dt += (tv2.tv_sec - tv1.tv_sec);
225         printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
226                 count, dt, count / dt);
227 }
228
229 static void
230 speeddisk(int fd, off_t mediasize, u_int sectorsize)
231 {
232         int i;
233         uint b0, b1, sectorcount;
234
235         sectorcount = mediasize / sectorsize;
236
237         printf("Seek times:\n");
238         printf("\tFull stroke:\t");
239         b0 = 0;
240         b1 = sectorcount - 1 - 16384;
241         T0();
242         for (i = 0; i < 125; i++) {
243                 rdsect(fd, b0, sectorsize);
244                 b0 += 16384;
245                 rdsect(fd, b1, sectorsize);
246                 b1 -= 16384;
247         }
248         TN(250);
249
250         printf("\tHalf stroke:\t");
251         b0 = sectorcount / 4;
252         b1 = b0 + sectorcount / 2;
253         T0();
254         for (i = 0; i < 125; i++) {
255                 rdsect(fd, b0, sectorsize);
256                 b0 += 16384;
257                 rdsect(fd, b1, sectorsize);
258                 b1 += 16384;
259         }
260         TN(250);
261         printf("\tQuarter stroke:\t");
262         b0 = sectorcount / 4;
263         b1 = b0 + sectorcount / 4;
264         T0();
265         for (i = 0; i < 250; i++) {
266                 rdsect(fd, b0, sectorsize);
267                 b0 += 16384;
268                 rdsect(fd, b1, sectorsize);
269                 b1 += 16384;
270         }
271         TN(500);
272
273         printf("\tShort forward:\t");
274         b0 = sectorcount / 2;
275         T0();
276         for (i = 0; i < 400; i++) {
277                 rdsect(fd, b0, sectorsize);
278                 b0 += 16384;
279         }
280         TN(400);
281
282         printf("\tShort backward:\t");
283         b0 = sectorcount / 2;
284         T0();
285         for (i = 0; i < 400; i++) {
286                 rdsect(fd, b0, sectorsize);
287                 b0 -= 16384;
288         }
289         TN(400);
290
291         printf("\tSeq outer:\t");
292         b0 = 0;
293         T0();
294         for (i = 0; i < 2048; i++) {
295                 rdsect(fd, b0, sectorsize);
296                 b0++;
297         }
298         TN(2048);
299
300         printf("\tSeq inner:\t");
301         b0 = sectorcount - 2048 - 1;
302         T0();
303         for (i = 0; i < 2048; i++) {
304                 rdsect(fd, b0, sectorsize);
305                 b0++;
306         }
307         TN(2048);
308
309         printf("Transfer rates:\n");
310         printf("\toutside:     ");
311         rdsect(fd, 0, sectorsize);
312         T0();
313         for (i = 0; i < 100; i++) {
314                 rdmega(fd);
315         }
316         TR(100 * 1024);
317
318         printf("\tmiddle:      ");
319         b0 = sectorcount / 2;
320         rdsect(fd, b0, sectorsize);
321         T0();
322         for (i = 0; i < 100; i++) {
323                 rdmega(fd);
324         }
325         TR(100 * 1024);
326
327         printf("\tinside:      ");
328         b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
329         rdsect(fd, b0, sectorsize);
330         T0();
331         for (i = 0; i < 100; i++) {
332                 rdmega(fd);
333         }
334         TR(100 * 1024);
335
336         printf("\n");
337         return;
338 }
339
340 static void
341 commandtime(int fd, off_t mediasize, u_int sectorsize)
342 {       
343         double dtmega, dtsector;
344         int i;
345
346         printf("I/O command overhead:\n");
347         i = mediasize;
348         rdsect(fd, 0, sectorsize);
349         T0();
350         for (i = 0; i < 10; i++)
351                 rdmega(fd);
352         gettimeofday(&tv2, NULL);
353         dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6;
354         dtmega += (tv2.tv_sec - tv1.tv_sec);
355
356         printf("\ttime to read 10MB block    %10.6f sec\t= %8.3f msec/sector\n",
357                 dtmega, dtmega*100/2048);
358
359         rdsect(fd, 0, sectorsize);
360         T0();
361         for (i = 0; i < 20480; i++)
362                 rdsect(fd, 0, sectorsize);
363         gettimeofday(&tv2, NULL);
364         dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6;
365         dtsector += (tv2.tv_sec - tv1.tv_sec);
366
367         printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
368                 dtsector, dtsector*100/2048);
369         printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
370                 (dtsector - dtmega)*100/2048);
371
372         printf("\n");
373         return;
374 }