xref: /freebsd/usr.sbin/diskinfo/diskinfo.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Poul-Henning Kamp
5  * Copyright (c) 2015 Spectra Logic Corporation
6  * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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
19  *    permission.
20  *
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
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <libutil.h>
46 #include <paths.h>
47 #include <err.h>
48 #include <geom/geom_disk.h>
49 #include <sysexits.h>
50 #include <sys/aio.h>
51 #include <sys/disk.h>
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 
56 #define	NAIO	128
57 #define	MAXTX	(8*1024*1024)
58 #define	MEGATX	(1024*1024)
59 
60 static void
61 usage(void)
62 {
63 	fprintf(stderr, "usage: diskinfo [-cipsStvw] disk ...\n");
64 	exit (1);
65 }
66 
67 static int opt_c, opt_i, opt_p, opt_s, opt_S, opt_t, opt_v, opt_w;
68 
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,
76 		     size_t zone_str_len);
77 
78 static uint8_t *buf;
79 
80 int
81 main(int argc, char **argv)
82 {
83 	struct stat sb;
84 	int i, ch, fd, error, exitval = 0;
85 	char tstr[BUFSIZ], ident[DISK_IDENT_SIZE], physpath[MAXPATHLEN];
86 	char zone_desc[64];
87 	char rrate[64];
88 	struct diocgattr_arg arg;
89 	off_t	mediasize, stripesize, stripeoffset;
90 	u_int	sectorsize, fwsectors, fwheads, zoned = 0, isreg;
91 	uint32_t zone_mode;
92 
93 	while ((ch = getopt(argc, argv, "cipsStvw")) != -1) {
94 		switch (ch) {
95 		case 'c':
96 			opt_c = 1;
97 			opt_v = 1;
98 			break;
99 		case 'i':
100 			opt_i = 1;
101 			opt_v = 1;
102 			break;
103 		case 'p':
104 			opt_p = 1;
105 			break;
106 		case 's':
107 			opt_s = 1;
108 			break;
109 		case 'S':
110 			opt_S = 1;
111 			opt_v = 1;
112 			break;
113 		case 't':
114 			opt_t = 1;
115 			opt_v = 1;
116 			break;
117 		case 'v':
118 			opt_v = 1;
119 			break;
120 		case 'w':
121 			opt_w = 1;
122 			break;
123 		default:
124 			usage();
125 		}
126 	}
127 	argc -= optind;
128 	argv += optind;
129 
130 	if (argc < 1)
131 		usage();
132 
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");
135 		usage();
136 	}
137 
138 	if (opt_S && !opt_w) {
139 		warnx("-S require also -w");
140 		usage();
141 	}
142 
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);
150 		}
151 		if (fd < 0) {
152 			warn("%s", argv[i]);
153 			exit(1);
154 		}
155 		error = fstat(fd, &sb);
156 		if (error != 0) {
157 			warn("cannot stat %s", argv[i]);
158 			exitval = 1;
159 			goto out;
160 		}
161 		isreg = S_ISREG(sb.st_mode);
162 		if (isreg) {
163 			mediasize = sb.st_size;
164 			sectorsize = S_BLKSIZE;
165 			fwsectors = 0;
166 			fwheads = 0;
167 			stripesize = sb.st_blksize;
168 			stripeoffset = 0;
169 			if (opt_p || opt_s) {
170 				warnx("-p and -s only operate on physical devices: %s", argv[i]);
171 				goto out;
172 			}
173 		} else {
174 			if (opt_p) {
175 				if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0) {
176 					printf("%s\n", physpath);
177 				} else {
178 					warnx("Failed to determine physpath for: %s", argv[i]);
179 				}
180 				goto out;
181 			}
182 			if (opt_s) {
183 				if (ioctl(fd, DIOCGIDENT, ident) == 0) {
184 					printf("%s\n", ident);
185 				} else {
186 					warnx("Failed to determine serial number for: %s", argv[i]);
187 				}
188 				goto out;
189 			}
190 			error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
191 			if (error) {
192 				warnx("%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]);
193 				exitval = 1;
194 				goto out;
195 			}
196 			error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
197 			if (error) {
198 				warnx("%s: ioctl(DIOCGSECTORSIZE) failed, probably not a disk.", argv[i]);
199 				exitval = 1;
200 				goto out;
201 			}
202 			error = ioctl(fd, DIOCGFWSECTORS, &fwsectors);
203 			if (error)
204 				fwsectors = 0;
205 			error = ioctl(fd, DIOCGFWHEADS, &fwheads);
206 			if (error)
207 				fwheads = 0;
208 			error = ioctl(fd, DIOCGSTRIPESIZE, &stripesize);
209 			if (error)
210 				stripesize = 0;
211 			error = ioctl(fd, DIOCGSTRIPEOFFSET, &stripeoffset);
212 			if (error)
213 				stripeoffset = 0;
214 			error = zonecheck(fd, &zone_mode, zone_desc, sizeof(zone_desc));
215 			if (error == 0)
216 				zoned = 1;
217 		}
218 		if (!opt_v) {
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);
230 			}
231 		} else {
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);
247 			}
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 			if (ioctl(fd, DIOCGPHYSPATH, physpath) == 0)
255 				printf("\t%-12s\t# Physical path\n", physpath);
256 			printf("\t%-12s\t# TRIM/UNMAP support\n",
257 			    candelete(fd) ? "Yes" : "No");
258 			rotationrate(fd, rrate, sizeof(rrate));
259 			printf("\t%-12s\t# Rotation rate in RPM\n", rrate);
260 			if (zoned != 0)
261 				printf("\t%-12s\t# Zone Mode\n", zone_desc);
262 		}
263 		printf("\n");
264 		if (opt_c)
265 			commandtime(fd, mediasize, sectorsize);
266 		if (opt_t)
267 			speeddisk(fd, mediasize, sectorsize);
268 		if (opt_i)
269 			iopsbench(fd, mediasize, sectorsize);
270 		if (opt_S)
271 			slogbench(fd, isreg, mediasize, sectorsize);
272 out:
273 		close(fd);
274 	}
275 	free(buf);
276 	exit (exitval);
277 }
278 
279 static bool
280 candelete(int fd)
281 {
282 	struct diocgattr_arg arg;
283 
284 	strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
285 	arg.len = sizeof(arg.value.i);
286 	if (ioctl(fd, DIOCGATTR, &arg) == 0)
287 		return (arg.value.i != 0);
288 	else
289 		return (false);
290 }
291 
292 static void
293 rotationrate(int fd, char *rate, size_t buflen)
294 {
295 	struct diocgattr_arg arg;
296 	int ret;
297 
298 	strlcpy(arg.name, "GEOM::rotation_rate", sizeof(arg.name));
299 	arg.len = sizeof(arg.value.u16);
300 
301 	ret = ioctl(fd, DIOCGATTR, &arg);
302 	if (ret < 0 || arg.value.u16 == DISK_RR_UNKNOWN)
303 		snprintf(rate, buflen, "Unknown");
304 	else if (arg.value.u16 == DISK_RR_NON_ROTATING)
305 		snprintf(rate, buflen, "%d", 0);
306 	else if (arg.value.u16 >= DISK_RR_MIN && arg.value.u16 <= DISK_RR_MAX)
307 		snprintf(rate, buflen, "%d", arg.value.u16);
308 	else
309 		snprintf(rate, buflen, "Invalid");
310 }
311 
312 static void
313 rdsect(int fd, off_t blockno, u_int sectorsize)
314 {
315 	int error;
316 
317 	if (lseek(fd, (off_t)blockno * sectorsize, SEEK_SET) == -1)
318 		err(1, "lseek");
319 	error = read(fd, buf, sectorsize);
320 	if (error == -1)
321 		err(1, "read");
322 	if (error != (int)sectorsize)
323 		errx(1, "disk too small for test.");
324 }
325 
326 static void
327 rdmega(int fd)
328 {
329 	int error;
330 
331 	error = read(fd, buf, MEGATX);
332 	if (error == -1)
333 		err(1, "read");
334 	if (error != MEGATX)
335 		errx(1, "disk too small for test.");
336 }
337 
338 static struct timeval tv1, tv2;
339 
340 static void
341 T0(void)
342 {
343 
344 	fflush(stdout);
345 	sync();
346 	sleep(1);
347 	sync();
348 	sync();
349 	gettimeofday(&tv1, NULL);
350 }
351 
352 static double
353 delta_t(void)
354 {
355 	double dt;
356 
357 	gettimeofday(&tv2, NULL);
358 	dt = (tv2.tv_usec - tv1.tv_usec) / 1e6;
359 	dt += (tv2.tv_sec - tv1.tv_sec);
360 
361 	return (dt);
362 }
363 
364 static void
365 TN(int count)
366 {
367 	double dt;
368 
369 	dt = delta_t();
370 	printf("%5d iter in %10.6f sec = %8.3f msec\n",
371 		count, dt, dt * 1000.0 / count);
372 }
373 
374 static void
375 TR(double count)
376 {
377 	double dt;
378 
379 	dt = delta_t();
380 	printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n",
381 		count, dt, count / dt);
382 }
383 
384 static void
385 TI(double count)
386 {
387 	double dt;
388 
389 	dt = delta_t();
390 	printf("%8.0f ops in  %10.6f sec = %8.0f IOPS\n",
391 		count, dt, count / dt);
392 }
393 
394 static void
395 TS(u_int size, int count)
396 {
397 	double dt;
398 
399 	dt = delta_t();
400 	printf("%8.1f usec/IO = %8.1f Mbytes/s\n",
401 	    dt * 1000000.0 / count, (double)size * count / dt / (1024 * 1024));
402 }
403 
404 static void
405 speeddisk(int fd, off_t mediasize, u_int sectorsize)
406 {
407 	int bulk, i;
408 	off_t b0, b1, sectorcount, step;
409 
410 	/*
411 	 * Drives smaller than 1MB produce negative sector numbers,
412 	 * as do 2048 or fewer sectors.
413 	 */
414 	sectorcount = mediasize / sectorsize;
415 	if (mediasize < 1024 * 1024 || sectorcount < 2048)
416 		return;
417 
418 
419 	step = 1ULL << (flsll(sectorcount / (4 * 200)) - 1);
420 	if (step > 16384)
421 		step = 16384;
422 	bulk = mediasize / (1024 * 1024);
423 	if (bulk > 100)
424 		bulk = 100;
425 
426 	printf("Seek times:\n");
427 	printf("\tFull stroke:\t");
428 	b0 = 0;
429 	b1 = sectorcount - step;
430 	T0();
431 	for (i = 0; i < 125; i++) {
432 		rdsect(fd, b0, sectorsize);
433 		b0 += step;
434 		rdsect(fd, b1, sectorsize);
435 		b1 -= step;
436 	}
437 	TN(250);
438 
439 	printf("\tHalf stroke:\t");
440 	b0 = sectorcount / 4;
441 	b1 = b0 + sectorcount / 2;
442 	T0();
443 	for (i = 0; i < 125; i++) {
444 		rdsect(fd, b0, sectorsize);
445 		b0 += step;
446 		rdsect(fd, b1, sectorsize);
447 		b1 += step;
448 	}
449 	TN(250);
450 	printf("\tQuarter stroke:\t");
451 	b0 = sectorcount / 4;
452 	b1 = b0 + sectorcount / 4;
453 	T0();
454 	for (i = 0; i < 250; i++) {
455 		rdsect(fd, b0, sectorsize);
456 		b0 += step;
457 		rdsect(fd, b1, sectorsize);
458 		b1 += step;
459 	}
460 	TN(500);
461 
462 	printf("\tShort forward:\t");
463 	b0 = sectorcount / 2;
464 	T0();
465 	for (i = 0; i < 400; i++) {
466 		rdsect(fd, b0, sectorsize);
467 		b0 += step;
468 	}
469 	TN(400);
470 
471 	printf("\tShort backward:\t");
472 	b0 = sectorcount / 2;
473 	T0();
474 	for (i = 0; i < 400; i++) {
475 		rdsect(fd, b0, sectorsize);
476 		b0 -= step;
477 	}
478 	TN(400);
479 
480 	printf("\tSeq outer:\t");
481 	b0 = 0;
482 	T0();
483 	for (i = 0; i < 2048; i++) {
484 		rdsect(fd, b0, sectorsize);
485 		b0++;
486 	}
487 	TN(2048);
488 
489 	printf("\tSeq inner:\t");
490 	b0 = sectorcount - 2048;
491 	T0();
492 	for (i = 0; i < 2048; i++) {
493 		rdsect(fd, b0, sectorsize);
494 		b0++;
495 	}
496 	TN(2048);
497 
498 	printf("\nTransfer rates:\n");
499 	printf("\toutside:     ");
500 	rdsect(fd, 0, sectorsize);
501 	T0();
502 	for (i = 0; i < bulk; i++) {
503 		rdmega(fd);
504 	}
505 	TR(bulk * 1024);
506 
507 	printf("\tmiddle:      ");
508 	b0 = sectorcount / 2 - bulk * (1024*1024 / sectorsize) / 2 - 1;
509 	rdsect(fd, b0, sectorsize);
510 	T0();
511 	for (i = 0; i < bulk; i++) {
512 		rdmega(fd);
513 	}
514 	TR(bulk * 1024);
515 
516 	printf("\tinside:      ");
517 	b0 = sectorcount - bulk * (1024*1024 / sectorsize) - 1;
518 	rdsect(fd, b0, sectorsize);
519 	T0();
520 	for (i = 0; i < bulk; i++) {
521 		rdmega(fd);
522 	}
523 	TR(bulk * 1024);
524 
525 	printf("\n");
526 	return;
527 }
528 
529 static void
530 commandtime(int fd, off_t mediasize, u_int sectorsize)
531 {
532 	double dtmega, dtsector;
533 	int i;
534 
535 	printf("I/O command overhead:\n");
536 	i = mediasize;
537 	rdsect(fd, 0, sectorsize);
538 	T0();
539 	for (i = 0; i < 10; i++)
540 		rdmega(fd);
541 	dtmega = delta_t();
542 
543 	printf("\ttime to read 10MB block    %10.6f sec\t= %8.3f msec/sector\n",
544 		dtmega, dtmega*100/2048);
545 
546 	rdsect(fd, 0, sectorsize);
547 	T0();
548 	for (i = 0; i < 20480; i++)
549 		rdsect(fd, 0, sectorsize);
550 	dtsector = delta_t();
551 
552 	printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n",
553 		dtsector, dtsector*100/2048);
554 	printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n",
555 		(dtsector - dtmega)*100/2048);
556 
557 	printf("\n");
558 	return;
559 }
560 
561 static void
562 iops(int fd, off_t mediasize, u_int sectorsize)
563 {
564 	struct aiocb aios[NAIO], *aiop;
565 	ssize_t ret;
566 	off_t sectorcount;
567 	int error, i, queued, completed;
568 
569 	sectorcount = mediasize / sectorsize;
570 
571 	for (i = 0; i < NAIO; i++) {
572 		aiop = &(aios[i]);
573 		bzero(aiop, sizeof(*aiop));
574 		aiop->aio_buf = malloc(sectorsize);
575 		if (aiop->aio_buf == NULL)
576 			err(1, "malloc");
577 	}
578 
579 	T0();
580 	for (i = 0; i < NAIO; i++) {
581 		aiop = &(aios[i]);
582 
583 		aiop->aio_fildes = fd;
584 		aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
585 		aiop->aio_nbytes = sectorsize;
586 
587 		error = aio_read(aiop);
588 		if (error != 0)
589 			err(1, "aio_read");
590 	}
591 
592 	queued = i;
593 	completed = 0;
594 
595 	for (;;) {
596 		ret = aio_waitcomplete(&aiop, NULL);
597 		if (ret < 0)
598 			err(1, "aio_waitcomplete");
599 		if (ret != (ssize_t)sectorsize)
600 			errx(1, "short read");
601 
602 		completed++;
603 
604 		if (delta_t() < 3.0) {
605 			aiop->aio_fildes = fd;
606 			aiop->aio_offset = (random() % (sectorcount)) * sectorsize;
607 			aiop->aio_nbytes = sectorsize;
608 
609 			error = aio_read(aiop);
610 			if (error != 0)
611 				err(1, "aio_read");
612 
613 			queued++;
614 		} else if (completed == queued) {
615 			break;
616 		}
617 	}
618 
619 	TI(completed);
620 
621 	return;
622 }
623 
624 static void
625 iopsbench(int fd, off_t mediasize, u_int sectorsize)
626 {
627 	printf("Asynchronous random reads:\n");
628 
629 	printf("\tsectorsize:  ");
630 	iops(fd, mediasize, sectorsize);
631 
632 	if (sectorsize != 4096) {
633 		printf("\t4 kbytes:    ");
634 		iops(fd, mediasize, 4096);
635 	}
636 
637 	printf("\t32 kbytes:   ");
638 	iops(fd, mediasize, 32 * 1024);
639 
640 	printf("\t128 kbytes:  ");
641 	iops(fd, mediasize, 128 * 1024);
642 
643 	printf("\n");
644 }
645 
646 #define MAXIO (128*1024)
647 #define MAXIOS (MAXTX / MAXIO)
648 
649 static void
650 parwrite(int fd, size_t size, off_t off)
651 {
652 	struct aiocb aios[MAXIOS];
653 	off_t o;
654 	int n, error;
655 	struct aiocb *aiop;
656 
657 	// if size > MAXIO, use AIO to write n - 1 pieces in parallel
658 	for (n = 0, o = 0; size > MAXIO; n++, size -= MAXIO, o += MAXIO) {
659 		aiop = &aios[n];
660 		bzero(aiop, sizeof(*aiop));
661 		aiop->aio_buf = &buf[o];
662 		aiop->aio_fildes = fd;
663 		aiop->aio_offset = off + o;
664 		aiop->aio_nbytes = MAXIO;
665 		error = aio_write(aiop);
666 		if (error != 0)
667 			err(EX_IOERR, "AIO write submit error");
668 	}
669 	// Use synchronous writes for the runt of size <= MAXIO
670 	error = pwrite(fd, &buf[o], size, off + o);
671 	if (error < 0)
672 		err(EX_IOERR, "Sync write error");
673 	for (; n > 0; n--) {
674 		error = aio_waitcomplete(&aiop, NULL);
675 		if (error < 0)
676 			err(EX_IOERR, "AIO write wait error");
677 	}
678 }
679 
680 static void
681 slogbench(int fd, int isreg, off_t mediasize, u_int sectorsize)
682 {
683 	off_t off;
684 	u_int size;
685 	int error, n, N, nowritecache = 0;
686 
687 	printf("Synchronous random writes:\n");
688 	for (size = sectorsize; size <= MAXTX; size *= 2) {
689 		printf("\t%4.4g kbytes: ", (double)size / 1024);
690 		N = 0;
691 		T0();
692 		do {
693 			for (n = 0; n < 250; n++) {
694 				off = random() % (mediasize / size);
695 				parwrite(fd, size, off * size);
696 				if (nowritecache)
697 					continue;
698 				if (isreg)
699 					error = fsync(fd);
700 				else
701 					error = ioctl(fd, DIOCGFLUSH);
702 				if (error < 0) {
703 					if (errno == ENOTSUP)
704 						nowritecache = 1;
705 					else
706 						err(EX_IOERR, "Flush error");
707 				}
708 			}
709 			N += 250;
710 		} while (delta_t() < 1.0);
711 		TS(size, N);
712 	}
713 }
714 
715 static int
716 zonecheck(int fd, uint32_t *zone_mode, char *zone_str, size_t zone_str_len)
717 {
718 	struct disk_zone_args zone_args;
719 	int error;
720 
721 	bzero(&zone_args, sizeof(zone_args));
722 
723 	zone_args.zone_cmd = DISK_ZONE_GET_PARAMS;
724 	error = ioctl(fd, DIOCZONECMD, &zone_args);
725 
726 	if (error == 0) {
727 		*zone_mode = zone_args.zone_params.disk_params.zone_mode;
728 
729 		switch (*zone_mode) {
730 		case DISK_ZONE_MODE_NONE:
731 			snprintf(zone_str, zone_str_len, "Not_Zoned");
732 			break;
733 		case DISK_ZONE_MODE_HOST_AWARE:
734 			snprintf(zone_str, zone_str_len, "Host_Aware");
735 			break;
736 		case DISK_ZONE_MODE_DRIVE_MANAGED:
737 			snprintf(zone_str, zone_str_len, "Drive_Managed");
738 			break;
739 		case DISK_ZONE_MODE_HOST_MANAGED:
740 			snprintf(zone_str, zone_str_len, "Host_Managed");
741 			break;
742 		default:
743 			snprintf(zone_str, zone_str_len, "Unknown_zone_mode_%u",
744 			    *zone_mode);
745 			break;
746 		}
747 	}
748 	return (error);
749 }
750