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