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