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>
53 #define MAXTX (8*1024*1024)
54 #define MEGATX (1024*1024)
59 fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
63 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
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,
75 main(int argc, char **argv)
78 int i, ch, fd, error, exitval = 0;
79 char tstr[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
81 struct diocgattr_arg arg;
82 off_t mediasize, stripesize, stripeoffset;
83 u_int sectorsize, fwsectors, fwheads, zoned = 0, isreg;
86 while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
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");
131 if (opt_S && !opt_w) {
132 warnx("-S require also -w");
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);
148 error = fstat(fd, &sb);
150 warn("cannot stat %s", argv[i]);
154 isreg = S_ISREG(sb.st_mode);
156 mediasize = sb.st_size;
157 sectorsize = S_BLKSIZE;
160 stripesize = sb.st_blksize;
162 if (opt_p || opt_s) {
163 warnx("-p and -s only operate on physical devices: %s", argv[i]);
168 if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
169 printf("%s\n", physpath);
171 warnx("Failed to determine physpath for: %s", argv[i]);
176 if (ioctl(fd, DIOCGIDENT, ident) == 0) {
177 printf("%s\n", ident);
179 warnx("Failed to determine serial number for: %s", argv[i]);
183 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
185 warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
189 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
191 warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
195 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
198 error = ioctl(fd, DIOCGFWHEADS, &fwheads);
201 error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
204 error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
207 error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
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);
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);
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);
250 printf("\t%-12s\t# Zone Mode\n", zone_desc);
254 commandtime(fd, mediasize, sectorsize);
256 speeddisk(fd, mediasize, sectorsize);
258 iopsbench(fd, mediasize, sectorsize);
260 slogbench(fd, isreg, mediasize, sectorsize);
269 rdsect(int fd, off_t blockno, u_int sectorsize)
273 if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
275 error = read(fd, buf, sectorsize);
278 if (error != (int)sectorsize)
279 errx(1, "disk too small for test.");
287 error = read(fd, buf, MEGATX);
291 errx(1, "disk too small for test.");
294 static struct timeval tv1, tv2;
305 gettimeofday(&tv1, NULL);
313 gettimeofday(&tv2, NULL);
314 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
315 dt += (tv2.tv_sec - tv1.tv_sec);
326 printf("%5d iter in %10.6f sec = %8.3f msec\n",
327 count, dt, dt * 1000.0 / count);
336 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
337 count, dt, count / dt);
346 printf("%8.0f ops in %10.6f sec = %8.0f IOPS\n",
347 count, dt, count / dt);
351 TS(u_int size, int count)
356 printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
357 dt * 1000000.0 / count, size * count / dt / (1024 * 1024));
361 speeddisk(int fd, off_t mediasize, u_int sectorsize)
364 off_t b0, b1, sectorcount, step;
366 sectorcount = mediasize / sectorsize;
367 if (sectorcount <= 0)
368 return; /* Can't test devices with no sectors */
370 step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
373 bulk = mediasize / (1024 * 1024);
377 printf("Seek times:\n");
378 printf("\tFull stroke:\t");
380 b1 = sectorcount - step;
382 for (i = 0; i < 125; i++) {
383 rdsect(fd, b0, sectorsize);
385 rdsect(fd, b1, sectorsize);
390 printf("\tHalf stroke:\t");
391 b0 = sectorcount / 4;
392 b1 = b0 + sectorcount / 2;
394 for (i = 0; i < 125; i++) {
395 rdsect(fd, b0, sectorsize);
397 rdsect(fd, b1, sectorsize);
401 printf("\tQuarter stroke:\t");
402 b0 = sectorcount / 4;
403 b1 = b0 + sectorcount / 4;
405 for (i = 0; i < 250; i++) {
406 rdsect(fd, b0, sectorsize);
408 rdsect(fd, b1, sectorsize);
413 printf("\tShort forward:\t");
414 b0 = sectorcount / 2;
416 for (i = 0; i < 400; i++) {
417 rdsect(fd, b0, sectorsize);
422 printf("\tShort backward:\t");
423 b0 = sectorcount / 2;
425 for (i = 0; i < 400; i++) {
426 rdsect(fd, b0, sectorsize);
431 printf("\tSeq outer:\t");
434 for (i = 0; i < 2048; i++) {
435 rdsect(fd, b0, sectorsize);
440 printf("\tSeq inner:\t");
441 b0 = sectorcount - 2048;
443 for (i = 0; i < 2048; i++) {
444 rdsect(fd, b0, sectorsize);
449 printf("\nTransfer rates:\n");
450 printf("\toutside: ");
451 rdsect(fd, 0, sectorsize);
453 for (i = 0; i < bulk; i++) {
458 printf("\tmiddle: ");
459 b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
460 rdsect(fd, b0, sectorsize);
462 for (i = 0; i < bulk; i++) {
467 printf("\tinside: ");
468 b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
469 rdsect(fd, b0, sectorsize);
471 for (i = 0; i < bulk; i++) {
481 commandtime(int fd, off_t mediasize, u_int sectorsize)
483 double dtmega, dtsector;
486 printf("I/O command overhead:\n");
488 rdsect(fd, 0, sectorsize);
490 for (i = 0; i < 10; i++)
494 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n",
495 dtmega, dtmega*100/2048);
497 rdsect(fd, 0, sectorsize);
499 for (i = 0; i < 20480; i++)
500 rdsect(fd, 0, sectorsize);
501 dtsector = delta_t();
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);
513 iops(int fd, off_t mediasize, u_int sectorsize)
515 struct aiocb aios[NAIO], *aiop;
518 int error, i, queued, completed;
520 sectorcount = mediasize / sectorsize;
522 for (i = 0; i < NAIO; i++) {
524 bzero(aiop, sizeof(*aiop));
525 aiop->aio_buf = malloc(sectorsize);
526 if (aiop->aio_buf == NULL)
531 for (i = 0; i < NAIO; i++) {
534 aiop->aio_fildes = fd;
535 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
536 aiop->aio_nbytes = sectorsize;
538 error = aio_read(aiop);
547 ret = aio_waitcomplete(&aiop, NULL);
549 err(1, "aio_waitcomplete");
550 if (ret != (ssize_t)sectorsize)
551 errx(1, "short read");
555 if (delta_t() < 3.0) {
556 aiop->aio_fildes = fd;
557 aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
558 aiop->aio_nbytes = sectorsize;
560 error = aio_read(aiop);
565 } else if (completed == queued) {
576 iopsbench(int fd, off_t mediasize, u_int sectorsize)
578 printf("Asynchronous random reads:\n");
580 printf("\tsectorsize: ");
581 iops(fd, mediasize, sectorsize);
583 if (sectorsize != 4096) {
584 printf("\t4 kbytes: ");
585 iops(fd, mediasize, 4096);
588 printf("\t32 kbytes: ");
589 iops(fd, mediasize, 32 * 1024);
591 printf("\t128 kbytes: ");
592 iops(fd, mediasize, 128 * 1024);
597 #define MAXIO (128*1024)
598 #define MAXIOS (MAXTX / MAXIO)
601 parwrite(int fd, size_t size, off_t off)
603 struct aiocb aios[MAXIOS];
609 for (n = 0, o = 0; size > MAXIO; n++, size -= s, o += s) {
610 s = (size >= MAXIO) ? MAXIO : size;
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);
619 err(EX_IOERR, "AIO write submit error");
621 error = pwrite(fd, &buf[o], size, off + o);
623 err(EX_IOERR, "Sync write error");
625 error = aio_waitcomplete(&aiop, NULL);
627 err(EX_IOERR, "AIO write wait error");
632 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
636 int error, n, N, nowritecache = 0;
638 printf("Synchronous random writes:\n");
639 for (size = sectorsize; size <= MAXTX; size *= 2) {
640 printf("\t%4.4g kbytes: ", (double)size / 1024);
644 for (n = 0; n < 250; n++) {
645 off = random() % (mediasize / size);
646 parwrite(fd, size, off * size);
652 error = ioctl(fd, DIOCGFLUSH);
654 if (errno == ENOTSUP)
657 err(EX_IOERR, "Flush error");
661 } while (delta_t() < 1.0);
667 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
669 struct disk_zone_args zone_args;
672 bzero(&zone_args, sizeof(zone_args));
674 zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
675 error = ioctl(fd, DIOCZONECMD, &zone_args);
678 *zone_mode = zone_args.zone_params.disk_params.zone_mode;
680 switch (*zone_mode) {
681 case DISK_ZONE_MODE_NONE:
682 snprintf(zone_str, zone_str_len, "Not_Zoned");
684 case DISK_ZONE_MODE_HOST_AWARE:
685 snprintf(zone_str, zone_str_len, "Host_Aware");
687 case DISK_ZONE_MODE_DRIVE_MANAGED:
688 snprintf(zone_str, zone_str_len, "Drive_Managed");
690 case DISK_ZONE_MODE_HOST_MANAGED:
691 snprintf(zone_str, zone_str_len, "Host_Managed");
694 snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",