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
46 #include <geom/geom_disk.h>
50 #include <sys/param.h>
55 #define MAXTX (8*1024*1024)
56 #define MEGATX (1024*1024)
61 fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
65 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
67 static bool candelete(int fd);
68 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
69 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
70 static void iopsbench(int fd, off_t mediasize, u_int sectorsize);
71 static void rotationrate(int fd, char *buf, size_t buflen);
72 static void slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize);
73 static int zonecheck(int fd, uint32_t *zone_mode, char *zone_str,
79 main(int argc, char **argv)
82 int i, ch, fd, error, exitval = 0;
83 char tstr[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
86 struct diocgattr_arg arg;
87 off_t mediasize, stripesize, stripeoffset;
88 u_int sectorsize, fwsectors, fwheads, zoned = 0, isreg;
91 while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
131 if ((opt_p && opt_s) || ((opt_p || opt_s) && (opt_c || opt_i || opt_t || opt_v))) {
132 warnx("-p or -s cannot be used with other options");
136 if (opt_S && !opt_w) {
137 warnx("-S require also -w");
141 if (posix_memalign((void **)&buf, PAGE_SIZE, MAXTX))
142 errx(1, "Can't allocate memory buffer");
143 for (i = 0; i < argc; i++) {
144 fd = open(argv[i], (opt_w ? O_RDWR : O_RDONLY) | O_DIRECT);
145 if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
146 snprintf(tstr, sizeof(tstr), "%s%s", _PATH_DEV, argv[i]);
147 fd = open(tstr, O_RDONLY);
153 error = fstat(fd, &sb);
155 warn("cannot stat %s", argv[i]);
159 isreg = S_ISREG(sb.st_mode);
161 mediasize = sb.st_size;
162 sectorsize = S_BLKSIZE;
165 stripesize = sb.st_blksize;
167 if (opt_p || opt_s) {
168 warnx("-p and -s only operate on physical devices: %s", argv[i]);
173 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
174 printf("%s\n", physpath);
176 warnx("Failed to determine physpath for: %s", argv[i]);
181 if (ioctl(fd, DIOCGIDENT, ident) == 0) {
182 printf("%s\n", ident);
184 warnx("Failed to determine serial number for: %s", argv[i]);
188 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
190 warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
194 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
196 warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
200 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
203 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
206 error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
209 error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
212 error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
217 printf("%s", argv[i]);
218 printf("\t%u", sectorsize);
219 printf("\t%jd", (intmax_t)mediasize);
220 printf("\t%jd", (intmax_t)mediasize/sectorsize);
221 printf("\t%jd", (intmax_t)stripesize);
222 printf("\t%jd", (intmax_t)stripeoffset);
223 if (fwsectors != 0 && fwheads != 0) {
224 printf("\t%jd", (intmax_t)mediasize /
225 (fwsectors * fwheads * sectorsize));
226 printf("\t%u", fwheads);
227 printf("\t%u", fwsectors);
230 humanize_number(tstr, 5, (int64_t)mediasize, "",
231 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
232 printf("%s\n", argv[i]);
233 printf("\t%-12u\t# sectorsize\n", sectorsize);
234 printf("\t%-12jd\t# mediasize in bytes (%s)\n",
235 (intmax_t)mediasize, tstr);
236 printf("\t%-12jd\t# mediasize in sectors\n",
237 (intmax_t)mediasize/sectorsize);
238 printf("\t%-12jd\t# stripesize\n", stripesize);
239 printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
240 if (fwsectors != 0 && fwheads != 0) {
241 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
242 (fwsectors * fwheads * sectorsize));
243 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
244 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
246 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
247 arg.len = sizeof(arg.value.str);
248 if (ioctl(fd, DIOCGATTR, &arg) == 0)
249 printf("\t%-12s\t# Disk descr.\n", arg.value.str);
250 if (ioctl(fd, DIOCGIDENT, ident) == 0)
251 printf("\t%-12s\t# Disk ident.\n", ident);
252 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
253 printf("\t%-12s\t# Physical path\n", physpath);
254 printf("\t%-12s\t# TRIM/UNMAP support\n",
255 candelete(fd) ? "Yes" : "No");
256 rotationrate(fd, rrate, sizeof(rrate));
257 printf("\t%-12s\t# Rotation rate in RPM\n", rrate);
259 printf("\t%-12s\t# Zone Mode\n", zone_desc);
263 commandtime(fd, mediasize, sectorsize);
265 speeddisk(fd, mediasize, sectorsize);
267 iopsbench(fd, mediasize, sectorsize);
269 slogbench(fd, isreg, mediasize, sectorsize);
280 struct diocgattr_arg arg;
282 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
283 arg.len = sizeof(arg.value.i);
284 if (ioctl(fd, DIOCGATTR, &arg) == 0)
285 return (arg.value.i != 0);
291 rotationrate(int fd, char *rate, size_t buflen)
293 struct diocgattr_arg arg;
296 strlcpy(arg.name, "GEOM::rotation_rate", sizeof(arg.name));
297 arg.len = sizeof(arg.value.u16);
299 ret = ioctl(fd, DIOCGATTR, &arg);
300 if (ret < 0 || arg.value.u16 == DISK_RR_UNKNOWN)
301 snprintf(rate, buflen, "Unknown");
302 else if (arg.value.u16 == DISK_RR_NON_ROTATING)
303 snprintf(rate, buflen, "%d", 0);
304 else if (arg.value.u16 >= DISK_RR_MIN && arg.value.u16 <= DISK_RR_MAX)
305 snprintf(rate, buflen, "%d", arg.value.u16);
307 snprintf(rate, buflen, "Invalid");
311 rdsect(int fd, off_t blockno, u_int sectorsize)
315 if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
317 error = read(fd, buf, sectorsize);
320 if (error != (int)sectorsize)
321 errx(1, "disk too small for test.");
329 error = read(fd, buf, MEGATX);
333 errx(1, "disk too small for test.");
336 static struct timeval tv1, tv2;
347 gettimeofday(&tv1, NULL);
355 gettimeofday(&tv2, NULL);
356 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
357 dt += (tv2.tv_sec - tv1.tv_sec);
368 printf("%5d iter in %10.6f sec = %8.3f msec\n",
369 count, dt, dt * 1000.0 / count);
378 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
379 count, dt, count / dt);
388 printf("%8.0f ops in %10.6f sec = %8.0f IOPS\n",
389 count, dt, count / dt);
393 TS(u_int size, int count)
398 printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
399 dt * 1000000.0 / count, size * count / dt / (1024 * 1024));
403 speeddisk(int fd, off_t mediasize, u_int sectorsize)
406 off_t b0, b1, sectorcount, step;
408 sectorcount = mediasize / sectorsize;
409 if (sectorcount <= 0)
410 return; /* Can't test devices with no sectors */
412 step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
415 bulk = mediasize / (1024 * 1024);
419 printf("Seek times:\n");
420 printf("\tFull stroke:\t");
422 b1 = sectorcount - step;
424 for (i = 0; i < 125; i++) {
425 rdsect(fd, b0, sectorsize);
427 rdsect(fd, b1, sectorsize);
432 printf("\tHalf stroke:\t");
433 b0 = sectorcount / 4;
434 b1 = b0 + sectorcount / 2;
436 for (i = 0; i < 125; i++) {
437 rdsect(fd, b0, sectorsize);
439 rdsect(fd, b1, sectorsize);
443 printf("\tQuarter stroke:\t");
444 b0 = sectorcount / 4;
445 b1 = b0 + sectorcount / 4;
447 for (i = 0; i < 250; i++) {
448 rdsect(fd, b0, sectorsize);
450 rdsect(fd, b1, sectorsize);
455 printf("\tShort forward:\t");
456 b0 = sectorcount / 2;
458 for (i = 0; i < 400; i++) {
459 rdsect(fd, b0, sectorsize);
464 printf("\tShort backward:\t");
465 b0 = sectorcount / 2;
467 for (i = 0; i < 400; i++) {
468 rdsect(fd, b0, sectorsize);
473 printf("\tSeq outer:\t");
476 for (i = 0; i < 2048; i++) {
477 rdsect(fd, b0, sectorsize);
482 printf("\tSeq inner:\t");
483 b0 = sectorcount - 2048;
485 for (i = 0; i < 2048; i++) {
486 rdsect(fd, b0, sectorsize);
491 printf("\nTransfer rates:\n");
492 printf("\toutside: ");
493 rdsect(fd, 0, sectorsize);
495 for (i = 0; i < bulk; i++) {
500 printf("\tmiddle: ");
501 b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
502 rdsect(fd, b0, sectorsize);
504 for (i = 0; i < bulk; i++) {
509 printf("\tinside: ");
510 b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
511 rdsect(fd, b0, sectorsize);
513 for (i = 0; i < bulk; i++) {
523 commandtime(int fd, off_t mediasize, u_int sectorsize)
525 double dtmega, dtsector;
528 printf("I/O command overhead:\n");
530 rdsect(fd, 0, sectorsize);
532 for (i = 0; i < 10; i++)
536 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
537 dtmega, dtmega*100/2048);
539 rdsect(fd, 0, sectorsize);
541 for (i = 0; i < 20480; i++)
542 rdsect(fd, 0, sectorsize);
543 dtsector = delta_t();
545 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
546 dtsector, dtsector*100/2048);
547 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
548 (dtsector - dtmega)*100/2048);
555 iops(int fd, off_t mediasize, u_int sectorsize)
557 struct aiocb aios[NAIO], *aiop;
560 int error, i, queued, completed;
562 sectorcount = mediasize / sectorsize;
564 for (i = 0; i < NAIO; i++) {
566 bzero(aiop, sizeof(*aiop));
567 aiop->aio_buf = malloc(sectorsize);
568 if (aiop->aio_buf == NULL)
573 for (i = 0; i < NAIO; i++) {
576 aiop->aio_fildes = fd;
577 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
578 aiop->aio_nbytes = sectorsize;
580 error = aio_read(aiop);
589 ret = aio_waitcomplete(&aiop, NULL);
591 err(1, "aio_waitcomplete");
592 if (ret != (ssize_t)sectorsize)
593 errx(1, "short read");
597 if (delta_t() < 3.0) {
598 aiop->aio_fildes = fd;
599 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
600 aiop->aio_nbytes = sectorsize;
602 error = aio_read(aiop);
607 } else if (completed == queued) {
618 iopsbench(int fd, off_t mediasize, u_int sectorsize)
620 printf("Asynchronous random reads:\n");
622 printf("\tsectorsize: ");
623 iops(fd, mediasize, sectorsize);
625 if (sectorsize != 4096) {
626 printf("\t4 kbytes: ");
627 iops(fd, mediasize, 4096);
630 printf("\t32 kbytes: ");
631 iops(fd, mediasize, 32 * 1024);
633 printf("\t128 kbytes: ");
634 iops(fd, mediasize, 128 * 1024);
639 #define MAXIO (128*1024)
640 #define MAXIOS (MAXTX / MAXIO)
643 parwrite(int fd, size_t size, off_t off)
645 struct aiocb aios[MAXIOS];
651 for (n = 0, o = 0; size > MAXIO; n++, size -= s, o += s) {
652 s = (size >= MAXIO) ? MAXIO : size;
654 bzero(aiop, sizeof(*aiop));
655 aiop->aio_buf = &buf[o];
656 aiop->aio_fildes = fd;
657 aiop->aio_offset = off + o;
658 aiop->aio_nbytes = s;
659 error = aio_write(aiop);
661 err(EX_IOERR, "AIO write submit error");
663 error = pwrite(fd, &buf[o], size, off + o);
665 err(EX_IOERR, "Sync write error");
667 error = aio_waitcomplete(&aiop, NULL);
669 err(EX_IOERR, "AIO write wait error");
674 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
678 int error, n, N, nowritecache = 0;
680 printf("Synchronous random writes:\n");
681 for (size = sectorsize; size <= MAXTX; size *= 2) {
682 printf("\t%4.4g kbytes: ", (double)size / 1024);
686 for (n = 0; n < 250; n++) {
687 off = random() % (mediasize / size);
688 parwrite(fd, size, off * size);
694 error = ioctl(fd, DIOCGFLUSH);
696 if (errno == ENOTSUP)
699 err(EX_IOERR, "Flush error");
703 } while (delta_t() < 1.0);
709 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
711 struct disk_zone_args zone_args;
714 bzero(&zone_args, sizeof(zone_args));
716 zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
717 error = ioctl(fd, DIOCZONECMD, &zone_args);
720 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
722 switch (*zone_mode) {
723 case DISK_ZONE_MODE_NONE:
724 snprintf(zone_str, zone_str_len, "Not_Zoned");
726 case DISK_ZONE_MODE_HOST_AWARE:
727 snprintf(zone_str, zone_str_len, "Host_Aware");
729 case DISK_ZONE_MODE_DRIVE_MANAGED:
730 snprintf(zone_str, zone_str_len, "Drive_Managed");
732 case DISK_ZONE_MODE_HOST_MANAGED:
733 snprintf(zone_str, zone_str_len, "Host_Managed");
736 snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",