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