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
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 strlcpy(arg.name, "GEOM::attachment", sizeof(arg.name));
253 arg.len = sizeof(arg.value.str);
254 if (ioctl(fd, DIOCGATTR, &arg) == 0)
255 printf("\t%-12s\t# Attachment\n", arg.value.str);
256 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
257 printf("\t%-12s\t# Physical path\n", physpath);
258 printf("\t%-12s\t# TRIM/UNMAP support\n",
259 candelete(fd) ? "Yes" : "No");
260 rotationrate(fd, rrate, sizeof(rrate));
261 printf("\t%-12s\t# Rotation rate in RPM\n", rrate);
263 printf("\t%-12s\t# Zone Mode\n", zone_desc);
267 commandtime(fd, mediasize, sectorsize);
269 speeddisk(fd, mediasize, sectorsize);
271 iopsbench(fd, mediasize, sectorsize);
273 slogbench(fd, isreg, mediasize, sectorsize);
284 struct diocgattr_arg arg;
286 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
287 arg.len = sizeof(arg.value.i);
288 if (ioctl(fd, DIOCGATTR, &arg) == 0)
289 return (arg.value.i != 0);
295 rotationrate(int fd, char *rate, size_t buflen)
297 struct diocgattr_arg arg;
300 strlcpy(arg.name, "GEOM::rotation_rate", sizeof(arg.name));
301 arg.len = sizeof(arg.value.u16);
303 ret = ioctl(fd, DIOCGATTR, &arg);
304 if (ret < 0 || arg.value.u16 == DISK_RR_UNKNOWN)
305 snprintf(rate, buflen, "Unknown");
306 else if (arg.value.u16 == DISK_RR_NON_ROTATING)
307 snprintf(rate, buflen, "%d", 0);
308 else if (arg.value.u16 >= DISK_RR_MIN && arg.value.u16 <= DISK_RR_MAX)
309 snprintf(rate, buflen, "%d", arg.value.u16);
311 snprintf(rate, buflen, "Invalid");
315 rdsect(int fd, off_t blockno, u_int sectorsize)
319 if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
321 error = read(fd, buf, sectorsize);
324 if (error != (int)sectorsize)
325 errx(1, "disk too small for test.");
333 error = read(fd, buf, MEGATX);
337 errx(1, "disk too small for test.");
340 static struct timeval tv1, tv2;
351 gettimeofday(&tv1, NULL);
359 gettimeofday(&tv2, NULL);
360 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
361 dt += (tv2.tv_sec - tv1.tv_sec);
372 printf("%5d iter in %10.6f sec = %8.3f msec\n",
373 count, dt, dt * 1000.0 / count);
382 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
383 count, dt, count / dt);
392 printf("%8.0f ops in %10.6f sec = %8.0f IOPS\n",
393 count, dt, count / dt);
397 TS(u_int size, int count)
402 printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
403 dt * 1000000.0 / count, (double)size * count / dt / (1024 * 1024));
407 speeddisk(int fd, off_t mediasize, u_int sectorsize)
410 off_t b0, b1, sectorcount, step;
413 * Drives smaller than 1MB produce negative sector numbers,
414 * as do 2048 or fewer sectors.
416 sectorcount = mediasize / sectorsize;
417 if (mediasize < 1024 * 1024 || sectorcount < 2048)
421 step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
424 bulk = mediasize / (1024 * 1024);
428 printf("Seek times:\n");
429 printf("\tFull stroke:\t");
431 b1 = sectorcount - step;
433 for (i = 0; i < 125; i++) {
434 rdsect(fd, b0, sectorsize);
436 rdsect(fd, b1, sectorsize);
441 printf("\tHalf stroke:\t");
442 b0 = sectorcount / 4;
443 b1 = b0 + sectorcount / 2;
445 for (i = 0; i < 125; i++) {
446 rdsect(fd, b0, sectorsize);
448 rdsect(fd, b1, sectorsize);
452 printf("\tQuarter stroke:\t");
453 b0 = sectorcount / 4;
454 b1 = b0 + sectorcount / 4;
456 for (i = 0; i < 250; i++) {
457 rdsect(fd, b0, sectorsize);
459 rdsect(fd, b1, sectorsize);
464 printf("\tShort forward:\t");
465 b0 = sectorcount / 2;
467 for (i = 0; i < 400; i++) {
468 rdsect(fd, b0, sectorsize);
473 printf("\tShort backward:\t");
474 b0 = sectorcount / 2;
476 for (i = 0; i < 400; i++) {
477 rdsect(fd, b0, sectorsize);
482 printf("\tSeq outer:\t");
485 for (i = 0; i < 2048; i++) {
486 rdsect(fd, b0, sectorsize);
491 printf("\tSeq inner:\t");
492 b0 = sectorcount - 2048;
494 for (i = 0; i < 2048; i++) {
495 rdsect(fd, b0, sectorsize);
500 printf("\nTransfer rates:\n");
501 printf("\toutside: ");
502 rdsect(fd, 0, sectorsize);
504 for (i = 0; i < bulk; i++) {
509 printf("\tmiddle: ");
510 b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
511 rdsect(fd, b0, sectorsize);
513 for (i = 0; i < bulk; i++) {
518 printf("\tinside: ");
519 b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
520 rdsect(fd, b0, sectorsize);
522 for (i = 0; i < bulk; i++) {
531 commandtime(int fd, off_t mediasize, u_int sectorsize)
533 double dtmega, dtsector;
536 printf("I/O command overhead:\n");
538 rdsect(fd, 0, sectorsize);
540 for (i = 0; i < 10; i++)
544 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
545 dtmega, dtmega*100/2048);
547 rdsect(fd, 0, sectorsize);
549 for (i = 0; i < 20480; i++)
550 rdsect(fd, 0, sectorsize);
551 dtsector = delta_t();
553 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
554 dtsector, dtsector*100/2048);
555 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
556 (dtsector - dtmega)*100/2048);
562 iops(int fd, off_t mediasize, u_int sectorsize)
564 struct aiocb aios[NAIO], *aiop;
567 int error, i, queued, completed;
569 sectorcount = mediasize / sectorsize;
571 for (i = 0; i < NAIO; i++) {
573 bzero(aiop, sizeof(*aiop));
574 aiop->aio_buf = malloc(sectorsize);
575 if (aiop->aio_buf == NULL)
580 for (i = 0; i < NAIO; i++) {
583 aiop->aio_fildes = fd;
584 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
585 aiop->aio_nbytes = sectorsize;
587 error = aio_read(aiop);
596 ret = aio_waitcomplete(&aiop, NULL);
598 err(1, "aio_waitcomplete");
599 if (ret != (ssize_t)sectorsize)
600 errx(1, "short read");
604 if (delta_t() < 3.0) {
605 aiop->aio_fildes = fd;
606 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
607 aiop->aio_nbytes = sectorsize;
609 error = aio_read(aiop);
614 } else if (completed == queued) {
623 iopsbench(int fd, off_t mediasize, u_int sectorsize)
625 printf("Asynchronous random reads:\n");
627 printf("\tsectorsize: ");
628 iops(fd, mediasize, sectorsize);
630 if (sectorsize != 4096) {
631 printf("\t4 kbytes: ");
632 iops(fd, mediasize, 4096);
635 printf("\t32 kbytes: ");
636 iops(fd, mediasize, 32 * 1024);
638 printf("\t128 kbytes: ");
639 iops(fd, mediasize, 128 * 1024);
641 printf("\t1024 kbytes: ");
642 iops(fd, mediasize, 1024 * 1024);
647 #define MAXIO (128*1024)
648 #define MAXIOS (MAXTX / MAXIO)
651 parwrite(int fd, size_t size, off_t off)
653 struct aiocb aios[MAXIOS];
658 // if size > MAXIO, use AIO to write n - 1 pieces in parallel
659 for (n = 0, o = 0; size > MAXIO; n++, size -= MAXIO, o += MAXIO) {
661 bzero(aiop, sizeof(*aiop));
662 aiop->aio_buf = &buf[o];
663 aiop->aio_fildes = fd;
664 aiop->aio_offset = off + o;
665 aiop->aio_nbytes = MAXIO;
666 error = aio_write(aiop);
668 err(EX_IOERR, "AIO write submit error");
670 // Use synchronous writes for the runt of size <= MAXIO
671 error = pwrite(fd, &buf[o], size, off + o);
673 err(EX_IOERR, "Sync write error");
675 error = aio_waitcomplete(&aiop, NULL);
677 err(EX_IOERR, "AIO write wait error");
682 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
686 int error, n, N, nowritecache = 0;
688 printf("Synchronous random writes:\n");
689 for (size = sectorsize; size <= MAXTX; size *= 2) {
690 printf("\t%4.4g kbytes: ", (double)size / 1024);
694 for (n = 0; n < 250; n++) {
695 off = random() % (mediasize / size);
696 parwrite(fd, size, off * size);
702 error = ioctl(fd, DIOCGFLUSH);
704 if (errno == ENOTSUP)
707 err(EX_IOERR, "Flush error");
711 } while (delta_t() < 1.0);
717 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
719 struct disk_zone_args zone_args;
722 bzero(&zone_args, sizeof(zone_args));
724 zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
725 error = ioctl(fd, DIOCZONECMD, &zone_args);
728 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
730 switch (*zone_mode) {
731 case DISK_ZONE_MODE_NONE:
732 snprintf(zone_str, zone_str_len, "Not_Zoned");
734 case DISK_ZONE_MODE_HOST_AWARE:
735 snprintf(zone_str, zone_str_len, "Host_Aware");
737 case DISK_ZONE_MODE_DRIVE_MANAGED:
738 snprintf(zone_str, zone_str_len, "Drive_Managed");
740 case DISK_ZONE_MODE_HOST_MANAGED:
741 snprintf(zone_str, zone_str_len, "Host_Managed");
744 snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",