2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
5 * Copyright (c) 2015 Spectra Logic Corporation
6 * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The names of the authors may not be used to endorse or promote
18 * products derived from this software without specific prior written
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 #include <geom/geom_disk.h>
52 #include <sys/param.h>
57 #define MAXTX (8*1024*1024)
58 #define MEGATX (1024*1024)
63 fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
67 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
69 static bool candelete(int fd);
70 static void speeddisk(int fd, off_t mediasize, u_int sectorsize);
71 static void commandtime(int fd, off_t mediasize, u_int sectorsize);
72 static void iopsbench(int fd, off_t mediasize, u_int sectorsize);
73 static void rotationrate(int fd, char *buf, size_t buflen);
74 static void slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize);
75 static int zonecheck(int fd, uint32_t *zone_mode, char *zone_str,
81 main(int argc, char **argv)
84 int i, ch, fd, error, exitval = 0;
85 char tstr[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
88 struct diocgattr_arg arg;
89 off_t mediasize, stripesize, stripeoffset;
90 u_int sectorsize, fwsectors, fwheads, zoned = 0, isreg;
93 while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
133 if ((opt_p && opt_s) || ((opt_p || opt_s) && (opt_c || opt_i || opt_t || opt_v))) {
134 warnx("-p or -s cannot be used with other options");
138 if (opt_S && !opt_w) {
139 warnx("-S require also -w");
143 if (posix_memalign((void **)&buf, PAGE_SIZE, MAXTX))
144 errx(1, "Can't allocate memory buffer");
145 for (i = 0; i < argc; i++) {
146 fd = open(argv[i], (opt_w ? O_RDWR : O_RDONLY) | O_DIRECT);
147 if (fd < 0 && errno == ENOENT && *argv[i] != '/') {
148 snprintf(tstr, sizeof(tstr), "%s%s", _PATH_DEV, argv[i]);
149 fd = open(tstr, O_RDONLY);
155 error = fstat(fd, &sb);
157 warn("cannot stat %s", argv[i]);
161 isreg = S_ISREG(sb.st_mode);
163 mediasize = sb.st_size;
164 sectorsize = S_BLKSIZE;
167 stripesize = sb.st_blksize;
169 if (opt_p || opt_s) {
170 warnx("-p and -s only operate on physical devices: %s", argv[i]);
175 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
176 printf("%s\n", physpath);
178 warnx("Failed to determine physpath for: %s", argv[i]);
183 if (ioctl(fd, DIOCGIDENT, ident) == 0) {
184 printf("%s\n", ident);
186 warnx("Failed to determine serial number for: %s", argv[i]);
190 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
192 warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
196 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
198 warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
202 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
205 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
208 error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
211 error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
214 error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
219 printf("%s", argv[i]);
220 printf("\t%u", sectorsize);
221 printf("\t%jd", (intmax_t)mediasize);
222 printf("\t%jd", (intmax_t)mediasize/sectorsize);
223 printf("\t%jd", (intmax_t)stripesize);
224 printf("\t%jd", (intmax_t)stripeoffset);
225 if (fwsectors != 0 && fwheads != 0) {
226 printf("\t%jd", (intmax_t)mediasize /
227 (fwsectors * fwheads * sectorsize));
228 printf("\t%u", fwheads);
229 printf("\t%u", fwsectors);
232 humanize_number(tstr, 5, (int64_t)mediasize, "",
233 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
234 printf("%s\n", argv[i]);
235 printf("\t%-12u\t# sectorsize\n", sectorsize);
236 printf("\t%-12jd\t# mediasize in bytes (%s)\n",
237 (intmax_t)mediasize, tstr);
238 printf("\t%-12jd\t# mediasize in sectors\n",
239 (intmax_t)mediasize/sectorsize);
240 printf("\t%-12jd\t# stripesize\n", stripesize);
241 printf("\t%-12jd\t# stripeoffset\n", stripeoffset);
242 if (fwsectors != 0 && fwheads != 0) {
243 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize /
244 (fwsectors * fwheads * sectorsize));
245 printf("\t%-12u\t# Heads according to firmware.\n", fwheads);
246 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors);
248 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
249 arg.len = sizeof(arg.value.str);
250 if (ioctl(fd, DIOCGATTR, &arg) == 0)
251 printf("\t%-12s\t# Disk descr.\n", arg.value.str);
252 if (ioctl(fd, DIOCGIDENT, ident) == 0)
253 printf("\t%-12s\t# Disk ident.\n", ident);
254 strlcpy(arg.name, "GEOM::attachment", sizeof(arg.name));
255 arg.len = sizeof(arg.value.str);
256 if (ioctl(fd, DIOCGATTR, &arg) == 0)
257 printf("\t%-12s\t# Attachment\n", arg.value.str);
258 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
259 printf("\t%-12s\t# Physical path\n", physpath);
260 printf("\t%-12s\t# TRIM/UNMAP support\n",
261 candelete(fd) ? "Yes" : "No");
262 rotationrate(fd, rrate, sizeof(rrate));
263 printf("\t%-12s\t# Rotation rate in RPM\n", rrate);
265 printf("\t%-12s\t# Zone Mode\n", zone_desc);
269 commandtime(fd, mediasize, sectorsize);
271 speeddisk(fd, mediasize, sectorsize);
273 iopsbench(fd, mediasize, sectorsize);
275 slogbench(fd, isreg, mediasize, sectorsize);
286 struct diocgattr_arg arg;
288 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
289 arg.len = sizeof(arg.value.i);
290 if (ioctl(fd, DIOCGATTR, &arg) == 0)
291 return (arg.value.i != 0);
297 rotationrate(int fd, char *rate, size_t buflen)
299 struct diocgattr_arg arg;
302 strlcpy(arg.name, "GEOM::rotation_rate", sizeof(arg.name));
303 arg.len = sizeof(arg.value.u16);
305 ret = ioctl(fd, DIOCGATTR, &arg);
306 if (ret < 0 || arg.value.u16 == DISK_RR_UNKNOWN)
307 snprintf(rate, buflen, "Unknown");
308 else if (arg.value.u16 == DISK_RR_NON_ROTATING)
309 snprintf(rate, buflen, "%d", 0);
310 else if (arg.value.u16 >= DISK_RR_MIN && arg.value.u16 <= DISK_RR_MAX)
311 snprintf(rate, buflen, "%d", arg.value.u16);
313 snprintf(rate, buflen, "Invalid");
317 rdsect(int fd, off_t blockno, u_int sectorsize)
321 if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
323 error = read(fd, buf, sectorsize);
326 if (error != (int)sectorsize)
327 errx(1, "disk too small for test.");
335 error = read(fd, buf, MEGATX);
339 errx(1, "disk too small for test.");
342 static struct timeval tv1, tv2;
353 gettimeofday(&tv1, NULL);
361 gettimeofday(&tv2, NULL);
362 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
363 dt += (tv2.tv_sec - tv1.tv_sec);
374 printf("%5d iter in %10.6f sec = %8.3f msec\n",
375 count, dt, dt * 1000.0 / count);
384 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
385 count, dt, count / dt);
394 printf("%8.0f ops in %10.6f sec = %8.0f IOPS\n",
395 count, dt, count / dt);
399 TS(u_int size, int count)
404 printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
405 dt * 1000000.0 / count, (double)size * count / dt / (1024 * 1024));
409 speeddisk(int fd, off_t mediasize, u_int sectorsize)
412 off_t b0, b1, sectorcount, step;
415 * Drives smaller than 1MB produce negative sector numbers,
416 * as do 2048 or fewer sectors.
418 sectorcount = mediasize / sectorsize;
419 if (mediasize < 1024 * 1024 || sectorcount < 2048)
423 step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
426 bulk = mediasize / (1024 * 1024);
430 printf("Seek times:\n");
431 printf("\tFull stroke:\t");
433 b1 = sectorcount - step;
435 for (i = 0; i < 125; i++) {
436 rdsect(fd, b0, sectorsize);
438 rdsect(fd, b1, sectorsize);
443 printf("\tHalf stroke:\t");
444 b0 = sectorcount / 4;
445 b1 = b0 + sectorcount / 2;
447 for (i = 0; i < 125; i++) {
448 rdsect(fd, b0, sectorsize);
450 rdsect(fd, b1, sectorsize);
454 printf("\tQuarter stroke:\t");
455 b0 = sectorcount / 4;
456 b1 = b0 + sectorcount / 4;
458 for (i = 0; i < 250; i++) {
459 rdsect(fd, b0, sectorsize);
461 rdsect(fd, b1, sectorsize);
466 printf("\tShort forward:\t");
467 b0 = sectorcount / 2;
469 for (i = 0; i < 400; i++) {
470 rdsect(fd, b0, sectorsize);
475 printf("\tShort backward:\t");
476 b0 = sectorcount / 2;
478 for (i = 0; i < 400; i++) {
479 rdsect(fd, b0, sectorsize);
484 printf("\tSeq outer:\t");
487 for (i = 0; i < 2048; i++) {
488 rdsect(fd, b0, sectorsize);
493 printf("\tSeq inner:\t");
494 b0 = sectorcount - 2048;
496 for (i = 0; i < 2048; i++) {
497 rdsect(fd, b0, sectorsize);
502 printf("\nTransfer rates:\n");
503 printf("\toutside: ");
504 rdsect(fd, 0, sectorsize);
506 for (i = 0; i < bulk; i++) {
511 printf("\tmiddle: ");
512 b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
513 rdsect(fd, b0, sectorsize);
515 for (i = 0; i < bulk; i++) {
520 printf("\tinside: ");
521 b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
522 rdsect(fd, b0, sectorsize);
524 for (i = 0; i < bulk; i++) {
534 commandtime(int fd, off_t mediasize, u_int sectorsize)
536 double dtmega, dtsector;
539 printf("I/O command overhead:\n");
541 rdsect(fd, 0, sectorsize);
543 for (i = 0; i < 10; i++)
547 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
548 dtmega, dtmega*100/2048);
550 rdsect(fd, 0, sectorsize);
552 for (i = 0; i < 20480; i++)
553 rdsect(fd, 0, sectorsize);
554 dtsector = delta_t();
556 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
557 dtsector, dtsector*100/2048);
558 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
559 (dtsector - dtmega)*100/2048);
566 iops(int fd, off_t mediasize, u_int sectorsize)
568 struct aiocb aios[NAIO], *aiop;
571 int error, i, queued, completed;
573 sectorcount = mediasize / sectorsize;
575 for (i = 0; i < NAIO; i++) {
577 bzero(aiop, sizeof(*aiop));
578 aiop->aio_buf = malloc(sectorsize);
579 if (aiop->aio_buf == NULL)
584 for (i = 0; i < NAIO; i++) {
587 aiop->aio_fildes = fd;
588 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
589 aiop->aio_nbytes = sectorsize;
591 error = aio_read(aiop);
600 ret = aio_waitcomplete(&aiop, NULL);
602 err(1, "aio_waitcomplete");
603 if (ret != (ssize_t)sectorsize)
604 errx(1, "short read");
608 if (delta_t() < 3.0) {
609 aiop->aio_fildes = fd;
610 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
611 aiop->aio_nbytes = sectorsize;
613 error = aio_read(aiop);
618 } else if (completed == queued) {
629 iopsbench(int fd, off_t mediasize, u_int sectorsize)
631 printf("Asynchronous random reads:\n");
633 printf("\tsectorsize: ");
634 iops(fd, mediasize, sectorsize);
636 if (sectorsize != 4096) {
637 printf("\t4 kbytes: ");
638 iops(fd, mediasize, 4096);
641 printf("\t32 kbytes: ");
642 iops(fd, mediasize, 32 * 1024);
644 printf("\t128 kbytes: ");
645 iops(fd, mediasize, 128 * 1024);
650 #define MAXIO (128*1024)
651 #define MAXIOS (MAXTX / MAXIO)
654 parwrite(int fd, size_t size, off_t off)
656 struct aiocb aios[MAXIOS];
661 // if size > MAXIO, use AIO to write n - 1 pieces in parallel
662 for (n = 0, o = 0; size > MAXIO; n++, size -= MAXIO, o += MAXIO) {
664 bzero(aiop, sizeof(*aiop));
665 aiop->aio_buf = &buf[o];
666 aiop->aio_fildes = fd;
667 aiop->aio_offset = off + o;
668 aiop->aio_nbytes = MAXIO;
669 error = aio_write(aiop);
671 err(EX_IOERR, "AIO write submit error");
673 // Use synchronous writes for the runt of size <= MAXIO
674 error = pwrite(fd, &buf[o], size, off + o);
676 err(EX_IOERR, "Sync write error");
678 error = aio_waitcomplete(&aiop, NULL);
680 err(EX_IOERR, "AIO write wait error");
685 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
689 int error, n, N, nowritecache = 0;
691 printf("Synchronous random writes:\n");
692 for (size = sectorsize; size <= MAXTX; size *= 2) {
693 printf("\t%4.4g kbytes: ", (double)size / 1024);
697 for (n = 0; n < 250; n++) {
698 off = random() % (mediasize / size);
699 parwrite(fd, size, off * size);
705 error = ioctl(fd, DIOCGFLUSH);
707 if (errno == ENOTSUP)
710 err(EX_IOERR, "Flush error");
714 } while (delta_t() < 1.0);
720 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
722 struct disk_zone_args zone_args;
725 bzero(&zone_args, sizeof(zone_args));
727 zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
728 error = ioctl(fd, DIOCZONECMD, &zone_args);
731 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
733 switch (*zone_mode) {
734 case DISK_ZONE_MODE_NONE:
735 snprintf(zone_str, zone_str_len, "Not_Zoned");
737 case DISK_ZONE_MODE_HOST_AWARE:
738 snprintf(zone_str, zone_str_len, "Host_Aware");
740 case DISK_ZONE_MODE_DRIVE_MANAGED:
741 snprintf(zone_str, zone_str_len, "Drive_Managed");
743 case DISK_ZONE_MODE_HOST_MANAGED:
744 snprintf(zone_str, zone_str_len, "Host_Managed");
747 snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",