2 * Copyright (c) 2003 Poul-Henning Kamp
3 * Copyright (c) 2015 Spectra Logic Corporation
4 * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
48 #include <sys/param.h>
57 fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
61 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
63 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
64 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
65 static void iopsbench(int fd, off_t mediasize, u_int sectorsize);
66 static void slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize);
67 static int zonecheck(int fd, uint32_t *zone_mode, char *zone_str,
71 main(int argc, char **argv)
74 int i, ch, fd, error, exitval = 0;
75 char buf[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
77 struct diocgattr_arg arg;
78 off_t mediasize, stripesize, stripeoffset;
79 u_int sectorsize, fwsectors, fwheads, zoned = 0, isreg;
82 while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
122 if ((opt_p && opt_s) || ((opt_p || opt_s) && (opt_c || opt_i || opt_t || opt_v))) {
123 warnx("-p or -s cannot be used with other options");
127 if (opt_S && !opt_w) {
128 warnx("-S require also -w");
132 for (i = 0; i < argc; i++) {
133 fd = open(argv[i], (opt_w ? O_RDWR : O_RDONLY) | O_DIRECT);
134 if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
135 snprintf(buf, BUFSIZ, "%s%s", _PATH_DEV, argv[i]);
136 fd = open(buf, O_RDONLY);
142 error = fstat(fd, &sb);
144 warn("cannot stat %s", argv[i]);
148 isreg = S_ISREG(sb.st_mode);
150 mediasize = sb.st_size;
151 sectorsize = S_BLKSIZE;
154 stripesize = sb.st_blksize;
156 if (opt_p || opt_s) {
157 warnx("-p and -s only operate on physical devices: %s", argv[i]);
162 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
163 printf("%s\n", physpath);
165 warnx("Failed to determine physpath for: %s", argv[i]);
170 if (ioctl(fd, DIOCGIDENT, ident) == 0) {
171 printf("%s\n", ident);
173 warnx("Failed to determine serial number for: %s", argv[i]);
177 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
179 warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
183 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
185 warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
189 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
192 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
195 error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
198 error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
201 error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
206 printf("%s", argv[i]);
207 printf("\t%u", sectorsize);
208 printf("\t%jd", (intmax_t)mediasize);
209 printf("\t%jd", (intmax_t)mediasize/sectorsize);
210 printf("\t%jd", (intmax_t)stripesize);
211 printf("\t%jd", (intmax_t)stripeoffset);
212 if (fwsectors != 0 && fwheads != 0) {
213 printf("\t%jd", (intmax_t)mediasize /
214 (fwsectors * fwheads * sectorsize));
215 printf("\t%u", fwheads);
216 printf("\t%u", fwsectors);
219 humanize_number(buf, 5, (int64_t)mediasize, "",
220 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
221 printf("%s\n", argv[i]);
222 printf("\t%-12u\t# sectorsize\n", sectorsize);
223 printf("\t%-12jd\t# mediasize in bytes (%s)\n",
224 (intmax_t)mediasize, buf);
225 printf("\t%-12jd\t# mediasize in sectors\n",
226 (intmax_t)mediasize/sectorsize);
227 printf("\t%-12jd\t# stripesize\n", stripesize);
228 printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
229 if (fwsectors != 0 && fwheads != 0) {
230 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
231 (fwsectors * fwheads * sectorsize));
232 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
233 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
235 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
236 arg.len = sizeof(arg.value.str);
237 if (ioctl(fd, DIOCGATTR, &arg) == 0)
238 printf("\t%-12s\t# Disk descr.\n", arg.value.str);
239 if (ioctl(fd, DIOCGIDENT, ident) == 0)
240 printf("\t%-12s\t# Disk ident.\n", ident);
241 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
242 printf("\t%-12s\t# Physical path\n", physpath);
244 printf("\t%-12s\t# Zone Mode\n", zone_desc);
248 commandtime(fd, mediasize, sectorsize);
250 speeddisk(fd, mediasize, sectorsize);
252 iopsbench(fd, mediasize, sectorsize);
254 slogbench(fd, isreg, mediasize, sectorsize);
261 #define MAXTX (8*1024*1024)
262 #define MEGATX (1024*1024)
263 static uint8_t buf[MAXTX];
266 rdsect(int fd, off_t blockno, u_int sectorsize)
270 if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
272 error = read(fd, buf, sectorsize);
275 if (error != (int)sectorsize)
276 errx(1, "disk too small for test.");
284 error = read(fd, buf, MEGATX);
288 errx(1, "disk too small for test.");
291 static struct timeval tv1, tv2;
302 gettimeofday(&tv1, NULL);
310 gettimeofday(&tv2, NULL);
311 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
312 dt += (tv2.tv_sec - tv1.tv_sec);
323 printf("%5d iter in %10.6f sec = %8.3f msec\n",
324 count, dt, dt * 1000.0 / count);
333 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
334 count, dt, count / dt);
343 printf("%8.0f ops in %10.6f sec = %8.0f IOPS\n",
344 count, dt, count / dt);
348 TS(u_int size, int count)
353 printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
354 dt * 1000000.0 / count, size * count / dt / (1024 * 1024));
358 speeddisk(int fd, off_t mediasize, u_int sectorsize)
361 off_t b0, b1, sectorcount, step;
363 sectorcount = mediasize / sectorsize;
364 if (sectorcount <= 0)
365 return; /* Can't test devices with no sectors */
367 step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
370 bulk = mediasize / (1024 * 1024);
374 printf("Seek times:\n");
375 printf("\tFull stroke:\t");
377 b1 = sectorcount - step;
379 for (i = 0; i < 125; i++) {
380 rdsect(fd, b0, sectorsize);
382 rdsect(fd, b1, sectorsize);
387 printf("\tHalf stroke:\t");
388 b0 = sectorcount / 4;
389 b1 = b0 + sectorcount / 2;
391 for (i = 0; i < 125; i++) {
392 rdsect(fd, b0, sectorsize);
394 rdsect(fd, b1, sectorsize);
398 printf("\tQuarter stroke:\t");
399 b0 = sectorcount / 4;
400 b1 = b0 + sectorcount / 4;
402 for (i = 0; i < 250; i++) {
403 rdsect(fd, b0, sectorsize);
405 rdsect(fd, b1, sectorsize);
410 printf("\tShort forward:\t");
411 b0 = sectorcount / 2;
413 for (i = 0; i < 400; i++) {
414 rdsect(fd, b0, sectorsize);
419 printf("\tShort backward:\t");
420 b0 = sectorcount / 2;
422 for (i = 0; i < 400; i++) {
423 rdsect(fd, b0, sectorsize);
428 printf("\tSeq outer:\t");
431 for (i = 0; i < 2048; i++) {
432 rdsect(fd, b0, sectorsize);
437 printf("\tSeq inner:\t");
438 b0 = sectorcount - 2048;
440 for (i = 0; i < 2048; i++) {
441 rdsect(fd, b0, sectorsize);
446 printf("\nTransfer rates:\n");
447 printf("\toutside: ");
448 rdsect(fd, 0, sectorsize);
450 for (i = 0; i < bulk; i++) {
455 printf("\tmiddle: ");
456 b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
457 rdsect(fd, b0, sectorsize);
459 for (i = 0; i < bulk; i++) {
464 printf("\tinside: ");
465 b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
466 rdsect(fd, b0, sectorsize);
468 for (i = 0; i < bulk; i++) {
478 commandtime(int fd, off_t mediasize, u_int sectorsize)
480 double dtmega, dtsector;
483 printf("I/O command overhead:\n");
485 rdsect(fd, 0, sectorsize);
487 for (i = 0; i < 10; i++)
491 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
492 dtmega, dtmega*100/2048);
494 rdsect(fd, 0, sectorsize);
496 for (i = 0; i < 20480; i++)
497 rdsect(fd, 0, sectorsize);
498 dtsector = delta_t();
500 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
501 dtsector, dtsector*100/2048);
502 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
503 (dtsector - dtmega)*100/2048);
510 iops(int fd, off_t mediasize, u_int sectorsize)
512 struct aiocb aios[NAIO], *aiop;
515 int error, i, queued, completed;
517 sectorcount = mediasize / sectorsize;
519 for (i = 0; i < NAIO; i++) {
521 bzero(aiop, sizeof(*aiop));
522 aiop->aio_buf = malloc(sectorsize);
523 if (aiop->aio_buf == NULL)
528 for (i = 0; i < NAIO; i++) {
531 aiop->aio_fildes = fd;
532 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
533 aiop->aio_nbytes = sectorsize;
535 error = aio_read(aiop);
544 ret = aio_waitcomplete(&aiop, NULL);
546 err(1, "aio_waitcomplete");
547 if (ret != (ssize_t)sectorsize)
548 errx(1, "short read");
552 if (delta_t() < 3.0) {
553 aiop->aio_fildes = fd;
554 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
555 aiop->aio_nbytes = sectorsize;
557 error = aio_read(aiop);
562 } else if (completed == queued) {
573 iopsbench(int fd, off_t mediasize, u_int sectorsize)
575 printf("Asynchronous random reads:\n");
577 printf("\tsectorsize: ");
578 iops(fd, mediasize, sectorsize);
580 if (sectorsize != 4096) {
581 printf("\t4 kbytes: ");
582 iops(fd, mediasize, 4096);
585 printf("\t32 kbytes: ");
586 iops(fd, mediasize, 32 * 1024);
588 printf("\t128 kbytes: ");
589 iops(fd, mediasize, 128 * 1024);
594 #define MAXIO (128*1024)
595 #define MAXIOS (MAXTX / MAXIO)
598 parwrite(int fd, size_t size, off_t off)
600 struct aiocb aios[MAXIOS];
606 for (n = 0, o = 0; size > MAXIO; n++, size -= s, o += s) {
607 s = (size >= MAXIO) ? MAXIO : size;
609 bzero(aiop, sizeof(*aiop));
610 aiop->aio_buf = &buf[o];
611 aiop->aio_fildes = fd;
612 aiop->aio_offset = off + o;
613 aiop->aio_nbytes = s;
614 error = aio_write(aiop);
616 err(EX_IOERR, "AIO write submit error");
618 error = pwrite(fd, &buf[o], size, off + o);
620 err(EX_IOERR, "Sync write error");
622 error = aio_waitcomplete(&aiop, NULL);
624 err(EX_IOERR, "AIO write wait error");
629 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
635 printf("Synchronous random writes:\n");
636 for (size = sectorsize; size <= MAXTX; size *= 2) {
637 printf("\t%4.4g kbytes: ", (double)size / 1024);
641 for (n = 0; n < 250; n++) {
642 off = random() % (mediasize / size);
643 parwrite(fd, size, off * size);
647 error = ioctl(fd, DIOCGFLUSH);
649 err(EX_IOERR, "Flush error");
652 } while (delta_t() < 1.0);
658 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
660 struct disk_zone_args zone_args;
663 bzero(&zone_args, sizeof(zone_args));
665 zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
666 error = ioctl(fd, DIOCZONECMD, &zone_args);
669 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
671 switch (*zone_mode) {
672 case DISK_ZONE_MODE_NONE:
673 snprintf(zone_str, zone_str_len, "Not_Zoned");
675 case DISK_ZONE_MODE_HOST_AWARE:
676 snprintf(zone_str, zone_str_len, "Host_Aware");
678 case DISK_ZONE_MODE_DRIVE_MANAGED:
679 snprintf(zone_str, zone_str_len, "Drive_Managed");
681 case DISK_ZONE_MODE_HOST_MANAGED:
682 snprintf(zone_str, zone_str_len, "Host_Managed");
685 snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",