]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/diskinfo/diskinfo.c
MFV r323912: 8592 ZFS channel programs - rollback
[FreeBSD/FreeBSD.git] / usr.sbin / diskinfo / diskinfo.c
1 /*-
2  * Copyright (c) 2003 Poul-Henning Kamp
3  * Copyright (c) 2015 Spectra Logic Corporation
4  * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The names of the authors may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <libutil.h>
43 #include <paths.h>
44 #include <err.h>
45 #include <sysexits.h>
46 #include <sys/aio.h>
47 #include <sys/disk.h>
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51
52 #define NAIO    128
53 #define MAXTX   (8*1024*1024)
54 #define MEGATX  (1024*1024)
55
56 static void
57 usage(void)
58 {
59         fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
60         exit (1);
61 }
62
63 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
64
65 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
66 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
67 static void iopsbench(int fd, off_t mediasize, u_int sectorsize);
68 static void slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize);
69 static int zonecheck(int fd, uint32_t *zone_mode, char *zone_str,
70                      size_t zone_str_len);
71
72 static uint8_t *buf;
73
74 int
75 main(int argc, char **argv)
76 {
77         struct stat sb;
78         int i, ch, fd, error, exitval = 0;
79         char tstr[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
80         char zone_desc[64];
81         struct diocgattr_arg arg;
82         off_t   mediasize, stripesize, stripeoffset;
83         u_int   sectorsize, fwsectors, fwheads, zoned = 0, isreg;
84         uint32_t zone_mode;
85
86         while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
87                 switch (ch) {
88                 case 'c':
89                         opt_c = 1;
90                         opt_v = 1;
91                         break;
92                 case 'i':
93                         opt_i = 1;
94                         opt_v = 1;
95                         break;
96                 case 'p':
97                         opt_p = 1;
98                         break;
99                 case 's':
100                         opt_s = 1;
101                         break;
102                 case 'S':
103                         opt_S = 1;
104                         opt_v = 1;
105                         break;
106                 case 't':
107                         opt_t = 1;
108                         opt_v = 1;
109                         break;
110                 case 'v':
111                         opt_v = 1;
112                         break;
113                 case 'w':
114                         opt_w = 1;
115                         break;
116                 default:
117                         usage();
118                 }
119         }
120         argc -= optind;
121         argv += optind;
122
123         if (argc < 1)
124                 usage();
125
126         if ((opt_p && opt_s) || ((opt_p || opt_s) && (opt_c || opt_i || opt_t || opt_v))) {
127                 warnx("-p or -s cannot be used with other options");
128                 usage();
129         }
130
131         if (opt_S && !opt_w) {
132                 warnx("-S require also -w");
133                 usage();
134         }
135
136         if (posix_memalign((void **)&buf, PAGE_SIZE, MAXTX))
137                 errx(1, "Can't allocate memory buffer");
138         for (i = 0; i < argc; i++) {
139                 fd = open(argv[i], (opt_w ? O_RDWR : O_RDONLY) | O_DIRECT);
140                 if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
141                         snprintf(tstr, sizeof(tstr), "%s%s", _PATH_DEV, argv[i]);
142                         fd = open(tstr, O_RDONLY);
143                 }
144                 if (fd < 0) {
145                         warn("%s", argv[i]);
146                         exit(1);
147                 }
148                 error = fstat(fd, &sb);
149                 if (error != 0) {
150                         warn("cannot stat %s", argv[i]);
151                         exitval = 1;
152                         goto out;
153                 }
154                 isreg = S_ISREG(sb.st_mode);
155                 if (isreg) {
156                         mediasize = sb.st_size;
157                         sectorsize = S_BLKSIZE;
158                         fwsectors = 0;
159                         fwheads = 0;
160                         stripesize = sb.st_blksize;
161                         stripeoffset = 0;
162                         if (opt_p || opt_s) {
163                                 warnx("-p and -s only operate on physical devices: %s", argv[i]);
164                                 goto out;
165                         }
166                 } else {
167                         if (opt_p) {
168                                 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
169                                         printf("%s\n", physpath);
170                                 } else {
171                                         warnx("Failed to determine physpath for: %s", argv[i]);
172                                 }
173                                 goto out;
174                         }
175                         if (opt_s) {
176                                 if (ioctl(fd, DIOCGIDENT, ident) == 0) {
177                                         printf("%s\n", ident);
178                                 } else {
179                                         warnx("Failed to determine serial number for: %s", argv[i]);
180                                 }
181                                 goto out;
182                         }
183                         error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
184                         if (error) {
185                                 warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
186                                 exitval = 1;
187                                 goto out;
188                         }
189                         error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
190                         if (error) {
191                                 warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
192                                 exitval = 1;
193                                 goto out;
194                         }
195                         error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
196                         if (error)
197                                 fwsectors = 0;
198                         error = ioctl(fd, DIOCGFWHEADS, &fwheads);
199                         if (error)
200                                 fwheads = 0;
201                         error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
202                         if (error)
203                                 stripesize = 0;
204                         error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
205                         if (error)
206                                 stripeoffset = 0;
207                         error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
208                         if (error == 0)
209                                 zoned = 1;
210                 }
211                 if (!opt_v) {
212                         printf("%s", argv[i]);
213                         printf("\t%u", sectorsize);
214                         printf("\t%jd", (intmax_t)mediasize);
215                         printf("\t%jd", (intmax_t)mediasize/sectorsize);
216                         printf("\t%jd", (intmax_t)stripesize);
217                         printf("\t%jd", (intmax_t)stripeoffset);
218                         if (fwsectors != 0 && fwheads != 0) {
219                                 printf("\t%jd", (intmax_t)mediasize /
220                                     (fwsectors * fwheads * sectorsize));
221                                 printf("\t%u", fwheads);
222                                 printf("\t%u", fwsectors);
223                         } 
224                 } else {
225                         humanize_number(tstr, 5, (int64_t)mediasize, "",
226                             HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
227                         printf("%s\n", argv[i]);
228                         printf("\t%-12u\t# sectorsize\n", sectorsize);
229                         printf("\t%-12jd\t# mediasize in bytes (%s)\n",
230                             (intmax_t)mediasize, tstr);
231                         printf("\t%-12jd\t# mediasize in sectors\n",
232                             (intmax_t)mediasize/sectorsize);
233                         printf("\t%-12jd\t# stripesize\n", stripesize);
234                         printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
235                         if (fwsectors != 0 && fwheads != 0) {
236                                 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
237                                     (fwsectors * fwheads * sectorsize));
238                                 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
239                                 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
240                         } 
241                         strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
242                         arg.len = sizeof(arg.value.str);
243                         if (ioctl(fd, DIOCGATTR, &arg) == 0)
244                                 printf("\t%-12s\t# Disk descr.\n", arg.value.str);
245                         if (ioctl(fd, DIOCGIDENT, ident) == 0)
246                                 printf("\t%-12s\t# Disk ident.\n", ident);
247                         if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
248                                 printf("\t%-12s\t# Physical path\n", physpath);
249                         if (zoned != 0)
250                                 printf("\t%-12s\t# Zone Mode\n", zone_desc);
251                 }
252                 printf("\n");
253                 if (opt_c)
254                         commandtime(fd, mediasize, sectorsize);
255                 if (opt_t)
256                         speeddisk(fd, mediasize, sectorsize);
257                 if (opt_i)
258                         iopsbench(fd, mediasize, sectorsize);
259                 if (opt_S)
260                         slogbench(fd, isreg, mediasize, sectorsize);
261 out:
262                 close(fd);
263         }
264         free(buf);
265         exit (exitval);
266 }
267
268 static void
269 rdsect(int fd, off_t blockno, u_int sectorsize)
270 {
271         int error;
272
273         if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
274                 err(1, "lseek");
275         error = read(fd, buf, sectorsize);
276         if (error == -1)
277                 err(1, "read");
278         if (error != (int)sectorsize)
279                 errx(1, "disk too small for test.");
280 }
281
282 static void
283 rdmega(int fd)
284 {
285         int error;
286
287         error = read(fd, buf, MEGATX);
288         if (error == -1)
289                 err(1, "read");
290         if (error != MEGATX)
291                 errx(1, "disk too small for test.");
292 }
293
294 static struct timeval tv1, tv2;
295
296 static void
297 T0(void)
298 {
299
300         fflush(stdout);
301         sync();
302         sleep(1);
303         sync();
304         sync();
305         gettimeofday(&tv1, NULL);
306 }
307
308 static double
309 delta_t(void)
310 {
311         double dt;
312
313         gettimeofday(&tv2, NULL);
314         dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
315         dt += (tv2.tv_sec - tv1.tv_sec);
316
317         return (dt);
318 }
319
320 static void
321 TN(int count)
322 {
323         double dt;
324
325         dt = delta_t();
326         printf("%5d iter in %10.6f sec = %8.3f msec\n",
327                 count, dt, dt * 1000.0 / count);
328 }
329
330 static void
331 TR(double count)
332 {
333         double dt;
334
335         dt = delta_t();
336         printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
337                 count, dt, count / dt);
338 }
339
340 static void
341 TI(double count)
342 {
343         double dt;
344
345         dt = delta_t();
346         printf("%8.0f ops in  %10.6f sec = %8.0f IOPS\n",
347                 count, dt, count / dt);
348 }
349
350 static void
351 TS(u_int size, int count)
352 {
353         double dt;
354
355         dt = delta_t();
356         printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
357             dt * 1000000.0 / count, size * count / dt / (1024 * 1024));
358 }
359
360 static void
361 speeddisk(int fd, off_t mediasize, u_int sectorsize)
362 {
363         int bulk, i;
364         off_t b0, b1, sectorcount, step;
365
366         sectorcount = mediasize / sectorsize;
367         if (sectorcount <= 0)
368                 return;         /* Can't test devices with no sectors */
369
370         step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
371         if (step > 16384)
372                 step = 16384;
373         bulk = mediasize / (1024 * 1024);
374         if (bulk > 100)
375                 bulk = 100;
376
377         printf("Seek times:\n");
378         printf("\tFull stroke:\t");
379         b0 = 0;
380         b1 = sectorcount - step;
381         T0();
382         for (i = 0; i < 125; i++) {
383                 rdsect(fd, b0, sectorsize);
384                 b0 += step;
385                 rdsect(fd, b1, sectorsize);
386                 b1 -= step;
387         }
388         TN(250);
389
390         printf("\tHalf stroke:\t");
391         b0 = sectorcount / 4;
392         b1 = b0 + sectorcount / 2;
393         T0();
394         for (i = 0; i < 125; i++) {
395                 rdsect(fd, b0, sectorsize);
396                 b0 += step;
397                 rdsect(fd, b1, sectorsize);
398                 b1 += step;
399         }
400         TN(250);
401         printf("\tQuarter stroke:\t");
402         b0 = sectorcount / 4;
403         b1 = b0 + sectorcount / 4;
404         T0();
405         for (i = 0; i < 250; i++) {
406                 rdsect(fd, b0, sectorsize);
407                 b0 += step;
408                 rdsect(fd, b1, sectorsize);
409                 b1 += step;
410         }
411         TN(500);
412
413         printf("\tShort forward:\t");
414         b0 = sectorcount / 2;
415         T0();
416         for (i = 0; i < 400; i++) {
417                 rdsect(fd, b0, sectorsize);
418                 b0 += step;
419         }
420         TN(400);
421
422         printf("\tShort backward:\t");
423         b0 = sectorcount / 2;
424         T0();
425         for (i = 0; i < 400; i++) {
426                 rdsect(fd, b0, sectorsize);
427                 b0 -= step;
428         }
429         TN(400);
430
431         printf("\tSeq outer:\t");
432         b0 = 0;
433         T0();
434         for (i = 0; i < 2048; i++) {
435                 rdsect(fd, b0, sectorsize);
436                 b0++;
437         }
438         TN(2048);
439
440         printf("\tSeq inner:\t");
441         b0 = sectorcount - 2048;
442         T0();
443         for (i = 0; i < 2048; i++) {
444                 rdsect(fd, b0, sectorsize);
445                 b0++;
446         }
447         TN(2048);
448
449         printf("\nTransfer rates:\n");
450         printf("\toutside:     ");
451         rdsect(fd, 0, sectorsize);
452         T0();
453         for (i = 0; i < bulk; i++) {
454                 rdmega(fd);
455         }
456         TR(bulk * 1024);
457
458         printf("\tmiddle:      ");
459         b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
460         rdsect(fd, b0, sectorsize);
461         T0();
462         for (i = 0; i < bulk; i++) {
463                 rdmega(fd);
464         }
465         TR(bulk * 1024);
466
467         printf("\tinside:      ");
468         b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
469         rdsect(fd, b0, sectorsize);
470         T0();
471         for (i = 0; i < bulk; i++) {
472                 rdmega(fd);
473         }
474         TR(bulk * 1024);
475
476         printf("\n");
477         return;
478 }
479
480 static void
481 commandtime(int fd, off_t mediasize, u_int sectorsize)
482 {       
483         double dtmega, dtsector;
484         int i;
485
486         printf("I/O command overhead:\n");
487         i = mediasize;
488         rdsect(fd, 0, sectorsize);
489         T0();
490         for (i = 0; i < 10; i++)
491                 rdmega(fd);
492         dtmega = delta_t();
493
494         printf("\ttime to read 10MB block    %10.6f sec\t= %8.3f msec/sector\n",
495                 dtmega, dtmega*100/2048);
496
497         rdsect(fd, 0, sectorsize);
498         T0();
499         for (i = 0; i < 20480; i++)
500                 rdsect(fd, 0, sectorsize);
501         dtsector = delta_t();
502
503         printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
504                 dtsector, dtsector*100/2048);
505         printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
506                 (dtsector - dtmega)*100/2048);
507
508         printf("\n");
509         return;
510 }
511
512 static void
513 iops(int fd, off_t mediasize, u_int sectorsize)
514 {
515         struct aiocb aios[NAIO], *aiop;
516         ssize_t ret;
517         off_t sectorcount;
518         int error, i, queued, completed;
519
520         sectorcount = mediasize / sectorsize;
521
522         for (i = 0; i < NAIO; i++) {
523                 aiop = &(aios[i]);
524                 bzero(aiop, sizeof(*aiop));
525                 aiop->aio_buf = malloc(sectorsize);
526                 if (aiop->aio_buf == NULL)
527                         err(1, "malloc");
528         }
529
530         T0();
531         for (i = 0; i < NAIO; i++) {
532                 aiop = &(aios[i]);
533
534                 aiop->aio_fildes = fd;
535                 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
536                 aiop->aio_nbytes = sectorsize;
537
538                 error = aio_read(aiop);
539                 if (error != 0)
540                         err(1, "aio_read");
541         }
542
543         queued = i;
544         completed = 0;
545
546         for (;;) {
547                 ret = aio_waitcomplete(&aiop, NULL);
548                 if (ret < 0)
549                         err(1, "aio_waitcomplete");
550                 if (ret != (ssize_t)sectorsize)
551                         errx(1, "short read");
552
553                 completed++;
554
555                 if (delta_t() < 3.0) {
556                         aiop->aio_fildes = fd;
557                         aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
558                         aiop->aio_nbytes = sectorsize;
559
560                         error = aio_read(aiop);
561                         if (error != 0)
562                                 err(1, "aio_read");
563
564                         queued++;
565                 } else if (completed == queued) {
566                         break;
567                 }
568         }
569
570         TI(completed);
571
572         return;
573 }
574
575 static void
576 iopsbench(int fd, off_t mediasize, u_int sectorsize)
577 {
578         printf("Asynchronous random reads:\n");
579
580         printf("\tsectorsize:  ");
581         iops(fd, mediasize, sectorsize);
582
583         if (sectorsize != 4096) {
584                 printf("\t4 kbytes:    ");
585                 iops(fd, mediasize, 4096);
586         }
587
588         printf("\t32 kbytes:   ");
589         iops(fd, mediasize, 32 * 1024);
590
591         printf("\t128 kbytes:  ");
592         iops(fd, mediasize, 128 * 1024);
593
594         printf("\n");
595 }
596
597 #define MAXIO (128*1024)
598 #define MAXIOS (MAXTX / MAXIO)
599
600 static void
601 parwrite(int fd, size_t size, off_t off)
602 {
603         struct aiocb aios[MAXIOS];
604         off_t o;
605         size_t s;
606         int n, error;
607         struct aiocb *aiop;
608
609         for (n = 0, o = 0; size > MAXIO; n++, size -= s, o += s) {
610                 s = (size >= MAXIO) ? MAXIO : size;
611                 aiop = &aios[n];
612                 bzero(aiop, sizeof(*aiop));
613                 aiop->aio_buf = &buf[o];
614                 aiop->aio_fildes = fd;
615                 aiop->aio_offset = off + o;
616                 aiop->aio_nbytes = s;
617                 error = aio_write(aiop);
618                 if (error != 0)
619                         err(EX_IOERR, "AIO write submit error");
620         }
621         error = pwrite(fd, &buf[o], size, off + o);
622         if (error < 0)
623                 err(EX_IOERR, "Sync write error");
624         for (; n > 0; n--) {
625                 error = aio_waitcomplete(&aiop, NULL);
626                 if (error < 0)
627                         err(EX_IOERR, "AIO write wait error");
628         }
629 }
630
631 static void
632 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
633 {
634         off_t off;
635         u_int size;
636         int error, n, N, nowritecache = 0;
637
638         printf("Synchronous random writes:\n");
639         for (size = sectorsize; size <= MAXTX; size *= 2) {
640                 printf("\t%4.4g kbytes: ", (double)size / 1024);
641                 N = 0;
642                 T0();
643                 do {
644                         for (n = 0; n < 250; n++) {
645                                 off = random() % (mediasize / size);
646                                 parwrite(fd, size, off * size);
647                                 if (nowritecache)
648                                         continue;
649                                 if (isreg)
650                                         error = fsync(fd);
651                                 else
652                                         error = ioctl(fd, DIOCGFLUSH);
653                                 if (error < 0) {
654                                         if (errno == ENOTSUP)
655                                                 nowritecache = 1;
656                                         else
657                                                 err(EX_IOERR, "Flush error");
658                                 }
659                         }
660                         N += 250;
661                 } while (delta_t() < 1.0);
662                 TS(size, N);
663         }
664 }
665
666 static int
667 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
668 {
669         struct disk_zone_args zone_args;
670         int error;
671
672         bzero(&zone_args, sizeof(zone_args));
673
674         zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
675         error = ioctl(fd, DIOCZONECMD, &zone_args);
676
677         if (error == 0) {
678                 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
679
680                 switch (*zone_mode) {
681                 case DISK_ZONE_MODE_NONE:
682                         snprintf(zone_str, zone_str_len, "Not_Zoned");
683                         break;
684                 case DISK_ZONE_MODE_HOST_AWARE:
685                         snprintf(zone_str, zone_str_len, "Host_Aware");
686                         break;
687                 case DISK_ZONE_MODE_DRIVE_MANAGED:
688                         snprintf(zone_str, zone_str_len, "Drive_Managed");
689                         break;
690                 case DISK_ZONE_MODE_HOST_MANAGED:
691                         snprintf(zone_str, zone_str_len, "Host_Managed");
692                         break;
693                 default:
694                         snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",
695                             *zone_mode);
696                         break;
697                 }
698         }
699         return (error);
700 }