aboutsummaryrefslogtreecommitdiff
path: root/tools/regression/aio/aiop/aiop.c
blob: a7c9525663d2bf35d2b1a9f0d068e49f808d3db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
 * All rights reserved.
 *
 * This software was developed for the FreeBSD Project by Marshall
 * Kirk McKusick and Network Associates Laboratories, the Security
 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
 * research program.
 *
 * Copyright (c) 1980, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <aio.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/*
 * This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
 * Its specifically designed to work under FreeBSD and its derivatives;
 * note how I cheat by using aio_waitcomplete().
 *
 * TODO:
 *
 * + Add write support; so we can make sure we're not hitting throughput issues
 *   with read/modify/write of entire tracks of the disk
 * + Add in per-op stats - time and offset - so one could start mapping out
 *   the speed hotspots of the disk
 * + Add in different distributions - random, normal, left/right skewed normal,
 *   zipf, etc - and perhaps add the ability to run concurrent distributions
 *   (so a normal and a zipf; and also a random read; zipf write, etc.)
 *
 * Adrian Chadd <adrian@creative.net.au>
 */

typedef enum {
	IOT_NONE = 0x00,
	IOT_READ = 0x01,
	IOT_WRITE = 0x02
} iot_t;

static size_t
disk_getsize(int fd)
{
	off_t mediasize;	

	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
		err(1, "ioctl(DIOCGMEDIASIZE)");
	return (mediasize);
}

static iot_t
choose_aio(iot_t iomask)
{
	/* choose a random read or write event, limited by the mask */
	if (iomask == IOT_READ)
		return IOT_READ;
	else if (iomask == IOT_WRITE)
		return IOT_WRITE;
	return (random() & 0x01 ? IOT_READ : IOT_WRITE);
}

static void
set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
{
	int r;
	bzero(a, sizeof(*a));
	a->aio_fildes = fd;
	a->aio_nbytes = size;
	a->aio_offset = offset;
	a->aio_buf = buf;
	if (iot == IOT_READ)
		r = aio_read(a);
	else
		r = aio_write(a);
	if (r != 0)
		err(1, "set_aio call failed");
}

int
main(int argc, char *argv[])
{
	int fd;
	struct stat sb;
	struct aiocb *aio;
	char **abuf;
	const char *fn;
	int aio_len;
	int io_size, nrun;
	off_t file_size, offset;
	struct aiocb *a;
	int i, n;
	struct timeval st, et, rt;
	float f_rt;
	iot_t iowhat;


	if (argc < 6) {
		printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n",
		    argv[0]);
		exit(1);
	}

	fn = argv[1];
	io_size = atoi(argv[2]);
	if (io_size <= 0)
		errx(1, "the I/O size must be >0");
	nrun = atoi(argv[3]);
	if (nrun <= 0)
		errx(1, "the number of runs must be >0");
	aio_len = atoi(argv[4]);
	if (aio_len <= 0)
		errx(1, "AIO concurrency must be >0");
	if (strcmp(argv[5], "ro") == 0)
		iowhat = IOT_READ;
	else if (strcmp(argv[5], "rw") == 0)
		iowhat = IOT_READ | IOT_WRITE;
	else if (strcmp(argv[5], "wo") == 0)
		iowhat = IOT_WRITE;
	else
		errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n");

	/*
	 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
	 * Lets instead treat random() as returning a block offset w/ block size
	 * being "io_size", so we can handle > 4 gig files.
	 */
	if (iowhat == IOT_READ)
		fd = open(fn, O_RDONLY | O_DIRECT);
	else if (iowhat == IOT_WRITE)
		fd = open(fn, O_WRONLY | O_DIRECT);
	else
		fd = open(fn, O_RDWR | O_DIRECT);

	if (fd < 0)
		err(1, "open failed");
	if (fstat(fd, &sb) < 0)
		err(1, "fstat failed");
	if (S_ISREG(sb.st_mode)) {
		file_size = sb.st_size;
	} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
		file_size = disk_getsize(fd);
	} else
		errx(1, "unknown file type");
	if (file_size <= 0)
		errx(1, "path provided too small");

	printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);

	aio = calloc(aio_len, sizeof(struct aiocb));
	abuf = calloc(aio_len, sizeof(char *));
	for (i = 0; i < aio_len; i++)
		abuf[i] = calloc(1, io_size * sizeof(char));

	/* Fill with the initial contents */
	gettimeofday(&st, NULL);
	for (i = 0; i < aio_len; i++) {
		offset = random() % (file_size / io_size);
		offset *= io_size;
		set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
	}

	for (i = 0; i < nrun; i++) {
		aio_waitcomplete(&a, NULL);
		n = a - aio;
		assert(n < aio_len);
		assert(n >= 0);
		offset = random() % (file_size / io_size);
		offset *= io_size;
		set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
	}

	gettimeofday(&et, NULL);
	timersub(&et, &st, &rt);
	f_rt = ((float) (rt.tv_usec)) / 1000000.0;
	f_rt += (float) (rt.tv_sec);
	printf("Runtime: %.2f seconds, ", f_rt);
	printf("Op rate: %.2f ops/sec, ", ((float) (nrun))  / f_rt);
	printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);



	exit(0);
}