]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/diskinfo/diskinfo.c
Update xz to git snapshot of v5.0 branch as of 20110711 (post-5.0.3)
[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/param.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], physpath[MAXPATHLEN];
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                         if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
156                                 printf("\t%-12s\t# Physical path\n", physpath);
157                 }
158                 printf("\n");
159                 if (opt_c)
160                         commandtime(fd, mediasize, sectorsize);
161                 if (opt_t)
162                         speeddisk(fd, mediasize, sectorsize);
163 out:
164                 close(fd);
165         }
166         exit (exitval);
167 }
168
169
170 static char sector[65536];
171 static char mega[1024 * 1024];
172
173 static void
174 rdsect(int fd, u_int blockno, u_int sectorsize)
175 {
176         int error;
177
178         lseek(fd, (off_t)blockno * sectorsize, SEEK_SET);
179         error = read(fd, sector, sectorsize);
180         if (error != (int)sectorsize)
181                 err(1, "read error or disk too small for test.");
182 }
183
184 static void
185 rdmega(int fd)
186 {
187         int error;
188
189         error = read(fd, mega, sizeof(mega));
190         if (error != sizeof(mega))
191                 err(1, "read error or disk too small for test.");
192 }
193
194 static struct timeval tv1, tv2;
195
196 static void
197 T0(void)
198 {
199
200         fflush(stdout);
201         sync();
202         sleep(1);
203         sync();
204         sync();
205         gettimeofday(&tv1, NULL);
206 }
207
208 static void
209 TN(int count)
210 {
211         double dt;
212
213         gettimeofday(&tv2, NULL);
214         dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
215         dt += (tv2.tv_sec - tv1.tv_sec);
216         printf("%5d iter in %10.6f sec = %8.3f msec\n",
217                 count, dt, dt * 1000.0 / count);
218 }
219
220 static void
221 TR(double count)
222 {
223         double dt;
224
225         gettimeofday(&tv2, NULL);
226         dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
227         dt += (tv2.tv_sec - tv1.tv_sec);
228         printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
229                 count, dt, count / dt);
230 }
231
232 static void
233 speeddisk(int fd, off_t mediasize, u_int sectorsize)
234 {
235         int i;
236         uint b0, b1, sectorcount;
237
238         sectorcount = mediasize / sectorsize;
239
240         printf("Seek times:\n");
241         printf("\tFull stroke:\t");
242         b0 = 0;
243         b1 = sectorcount - 1 - 16384;
244         T0();
245         for (i = 0; i < 125; i++) {
246                 rdsect(fd, b0, sectorsize);
247                 b0 += 16384;
248                 rdsect(fd, b1, sectorsize);
249                 b1 -= 16384;
250         }
251         TN(250);
252
253         printf("\tHalf stroke:\t");
254         b0 = sectorcount / 4;
255         b1 = b0 + sectorcount / 2;
256         T0();
257         for (i = 0; i < 125; i++) {
258                 rdsect(fd, b0, sectorsize);
259                 b0 += 16384;
260                 rdsect(fd, b1, sectorsize);
261                 b1 += 16384;
262         }
263         TN(250);
264         printf("\tQuarter stroke:\t");
265         b0 = sectorcount / 4;
266         b1 = b0 + sectorcount / 4;
267         T0();
268         for (i = 0; i < 250; i++) {
269                 rdsect(fd, b0, sectorsize);
270                 b0 += 16384;
271                 rdsect(fd, b1, sectorsize);
272                 b1 += 16384;
273         }
274         TN(500);
275
276         printf("\tShort forward:\t");
277         b0 = sectorcount / 2;
278         T0();
279         for (i = 0; i < 400; i++) {
280                 rdsect(fd, b0, sectorsize);
281                 b0 += 16384;
282         }
283         TN(400);
284
285         printf("\tShort backward:\t");
286         b0 = sectorcount / 2;
287         T0();
288         for (i = 0; i < 400; i++) {
289                 rdsect(fd, b0, sectorsize);
290                 b0 -= 16384;
291         }
292         TN(400);
293
294         printf("\tSeq outer:\t");
295         b0 = 0;
296         T0();
297         for (i = 0; i < 2048; i++) {
298                 rdsect(fd, b0, sectorsize);
299                 b0++;
300         }
301         TN(2048);
302
303         printf("\tSeq inner:\t");
304         b0 = sectorcount - 2048 - 1;
305         T0();
306         for (i = 0; i < 2048; i++) {
307                 rdsect(fd, b0, sectorsize);
308                 b0++;
309         }
310         TN(2048);
311
312         printf("Transfer rates:\n");
313         printf("\toutside:     ");
314         rdsect(fd, 0, sectorsize);
315         T0();
316         for (i = 0; i < 100; i++) {
317                 rdmega(fd);
318         }
319         TR(100 * 1024);
320
321         printf("\tmiddle:      ");
322         b0 = sectorcount / 2;
323         rdsect(fd, b0, sectorsize);
324         T0();
325         for (i = 0; i < 100; i++) {
326                 rdmega(fd);
327         }
328         TR(100 * 1024);
329
330         printf("\tinside:      ");
331         b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;;
332         rdsect(fd, b0, sectorsize);
333         T0();
334         for (i = 0; i < 100; i++) {
335                 rdmega(fd);
336         }
337         TR(100 * 1024);
338
339         printf("\n");
340         return;
341 }
342
343 static void
344 commandtime(int fd, off_t mediasize, u_int sectorsize)
345 {       
346         double dtmega, dtsector;
347         int i;
348
349         printf("I/O command overhead:\n");
350         i = mediasize;
351         rdsect(fd, 0, sectorsize);
352         T0();
353         for (i = 0; i < 10; i++)
354                 rdmega(fd);
355         gettimeofday(&tv2, NULL);
356         dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6;
357         dtmega += (tv2.tv_sec - tv1.tv_sec);
358
359         printf("\ttime to read 10MB block    %10.6f sec\t= %8.3f msec/sector\n",
360                 dtmega, dtmega*100/2048);
361
362         rdsect(fd, 0, sectorsize);
363         T0();
364         for (i = 0; i < 20480; i++)
365                 rdsect(fd, 0, sectorsize);
366         gettimeofday(&tv2, NULL);
367         dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6;
368         dtsector += (tv2.tv_sec - tv1.tv_sec);
369
370         printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
371                 dtsector, dtsector*100/2048);
372         printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
373                 (dtsector - dtmega)*100/2048);
374
375         printf("\n");
376         return;
377 }