aiop.c revision 178823
1/*
2 * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9 * research program.
10 *
11 * Copyright (c) 1980, 1989, 1993
12 *	The Regents of the University of California.  All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/tools/regression/aio/aiop/aiop.c 178823 2008-05-07 07:23:47Z adrian $");
41
42#include <stdio.h>
43#include <stdlib.h>
44#include <unistd.h>
45#include <sys/time.h>
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <time.h>
49#include <sys/ioctl.h>
50#include <sys/disk.h>
51#include <aio.h>
52#include <fcntl.h>
53#include <string.h>
54#include <ctype.h>
55#include <assert.h>
56
57/*
58 * This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
59 * Its specifically designed to work under FreeBSD and its derivatives;
60 * note how I cheat by using aio_waitcomplete().
61 *
62 * TODO:
63 *
64 * + Add write support; so we can make sure we're not hitting throughput issues
65 *   with read/modify/write of entire tracks of the disk
66 * + Add in per-op stats - time and offset - so one could start mapping out
67 *   the speed hotspots of the disk
68 * + Add in different distributions - random, normal, left/right skewed normal,
69 *   zipf, etc - and perhaps add the ability to run concurrent distributions
70 *   (so a normal and a zipf; and also a random read; zipf write, etc.)
71 *
72 * Adrian Chadd <adrian@creative.net.au>
73 */
74
75static size_t
76disk_getsize(int fd)
77{
78	off_t mediasize;
79
80	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
81		perror("ioctl(DIOCGMEDIASIZE)");
82		exit(1);
83	}
84	return mediasize;
85}
86
87
88void
89set_aio(struct aiocb *a, int fd, off_t offset, int size, char *buf)
90{
91	int r;
92	bzero(a, sizeof(*a));
93	a->aio_fildes = fd;
94	a->aio_nbytes = size;
95	a->aio_offset = offset;
96	a->aio_buf = buf;
97	r = aio_read(a);
98	if (r != 0) {
99		perror("aio_read");
100		exit(1);
101	}
102}
103
104int
105main(int argc, char *argv[])
106{
107	int fd;
108	struct stat sb;
109	struct aiocb *aio;
110	char **abuf;
111	const char *fn;
112	int aio_len;
113	int io_size, nrun;
114	off_t file_size, offset;
115	struct aiocb *a;
116	int i, n;
117        struct timeval st, et, rt;
118        float f_rt;
119
120
121	if (argc < 5) {
122		printf("Usage: %s <file> <io size> <number of runs> <concurrency>\n", argv[0]);
123		exit(1);
124	}
125
126	fn = argv[1];
127	io_size = atoi(argv[2]);
128	nrun = atoi(argv[3]);
129	aio_len = atoi(argv[4]);
130
131	/*
132	 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
133	 * Lets instead treat random() as returning a block offset w/ block size
134	 * being "io_size", so we can handle > 4 gig files.
135	 */
136
137	fd = open(fn, O_RDONLY | O_DIRECT);
138	if (fd < 0) {
139		perror("open");
140		exit(1);
141	}
142	if (fstat(fd, &sb) < 0) {
143		perror("fstat");
144		exit(1);
145	}
146	if (S_ISREG(sb.st_mode)) {
147		file_size = sb.st_size;
148	} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
149		file_size = disk_getsize(fd);
150	} else {
151		perror("unknown file type\n");
152		exit(1);
153	}
154	printf("File: %s; File size %ld bytes\n", fn, (long) file_size);
155
156	aio = calloc(aio_len, sizeof(struct aiocb));
157	abuf = calloc(aio_len, sizeof(char *));
158	for (i = 0; i < aio_len; i++) {
159		abuf[i] = calloc(1, io_size * sizeof(char));
160	}
161
162	/* Fill with the initial contents */
163        gettimeofday(&st, NULL);
164	for (i = 0; i < aio_len; i++) {
165                offset = random() % (file_size / io_size);
166                offset *= io_size;
167		set_aio(aio + i, fd, offset, io_size, abuf[i]);
168	}
169
170	for (i = 0; i < nrun; i++) {
171		aio_waitcomplete(&a, NULL);
172		n = a - aio;
173		assert(n < aio_len);
174		assert(n >= 0);
175                offset = random() % (file_size / io_size);
176                offset *= io_size;
177		set_aio(aio + n, fd, offset, io_size, abuf[n]);
178	}
179
180        gettimeofday(&et, NULL);
181        timersub(&et, &st, &rt);
182        f_rt = ((float) (rt.tv_usec)) / 1000000.0;
183        f_rt += (float) (rt.tv_sec);
184        printf("Runtime: %.2f seconds, ", f_rt);
185        printf("Op rate: %.2f ops/sec, ", ((float) (nrun))  / f_rt);
186        printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
187
188
189
190	exit(0);
191}
192