]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/regression/aio/aiop/aiop.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / regression / aio / aiop / aiop.c
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$");
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
75 typedef enum {
76         IOT_NONE = 0x00,
77         IOT_READ = 0x01,
78         IOT_WRITE = 0x02
79 } iot_t;
80
81 static size_t
82 disk_getsize(int fd)
83 {
84         off_t mediasize;        
85
86         if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
87                 perror("ioctl(DIOCGMEDIASIZE)");
88                 exit(1);
89         }
90         return mediasize;
91 }
92
93 iot_t
94 choose_aio(iot_t iomask)
95 {
96         /* choose a random read or write event, limited by the mask */
97         if (iomask == IOT_READ)
98                 return IOT_READ;
99         else if (iomask == IOT_WRITE)
100                 return IOT_WRITE;
101         return (random() & 0x01 ? IOT_READ : IOT_WRITE);
102 }
103
104 void
105 set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
106 {
107         int r;
108         bzero(a, sizeof(*a));
109         a->aio_fildes = fd;
110         a->aio_nbytes = size;
111         a->aio_offset = offset;
112         a->aio_buf = buf;
113         if (iot == IOT_READ)
114                 r = aio_read(a);
115         else
116                 r = aio_write(a);
117         if (r != 0) {
118                 perror("set_aio");
119                 exit(1);
120         }
121 }
122
123 int
124 main(int argc, char *argv[])
125 {
126         int fd;
127         struct stat sb;
128         struct aiocb *aio;
129         char **abuf;
130         const char *fn;
131         int aio_len;
132         int io_size, nrun;
133         off_t file_size, offset;
134         struct aiocb *a;
135         int i, n;
136         struct timeval st, et, rt;
137         float f_rt;
138         iot_t iowhat;
139
140
141         if (argc < 6) {
142                 printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", argv[0]);
143                 exit(1);
144         }
145
146         fn = argv[1];
147         io_size = atoi(argv[2]);
148         nrun = atoi(argv[3]);
149         aio_len = atoi(argv[4]);
150         if (strcmp(argv[5], "ro") == 0) {
151                 iowhat = IOT_READ;
152         } else if (strcmp(argv[5], "rw") == 0) {
153                 iowhat = IOT_READ | IOT_WRITE;
154         } else if (strcmp(argv[5], "wo") == 0) {
155                 iowhat = IOT_WRITE;
156         } else {
157                 fprintf(stderr, "needs to be ro, rw, wo!\n");
158                 exit(1);
159         }
160
161         /*
162          * Random returns values between 0 and (2^32)-1; only good for 4 gig.
163          * Lets instead treat random() as returning a block offset w/ block size
164          * being "io_size", so we can handle > 4 gig files.
165          */
166         if (iowhat == IOT_READ)
167                 fd = open(fn, O_RDONLY | O_DIRECT);
168         else if (iowhat == IOT_WRITE)
169                 fd = open(fn, O_WRONLY | O_DIRECT);
170         else
171                 fd = open(fn, O_RDWR | O_DIRECT);
172
173         if (fd < 0) {
174                 perror("open");
175                 exit(1);
176         }
177         if (fstat(fd, &sb) < 0) {
178                 perror("fstat");
179                 exit(1);
180         }
181         if (S_ISREG(sb.st_mode)) {
182                 file_size = sb.st_size;
183         } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
184                 file_size = disk_getsize(fd);
185         } else {
186                 perror("unknown file type\n");
187                 exit(1);
188         }
189         printf("File: %s; File size %qd bytes\n", fn, file_size);
190
191         aio = calloc(aio_len, sizeof(struct aiocb));
192         abuf = calloc(aio_len, sizeof(char *));
193         for (i = 0; i < aio_len; i++) {
194                 abuf[i] = calloc(1, io_size * sizeof(char));
195         }
196
197         /* Fill with the initial contents */
198         gettimeofday(&st, NULL);
199         for (i = 0; i < aio_len; i++) {
200                 offset = random() % (file_size / io_size);
201                 offset *= io_size;
202                 set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
203         }
204
205         for (i = 0; i < nrun; i++) {
206                 aio_waitcomplete(&a, NULL);
207                 n = a - aio;
208                 assert(n < aio_len);
209                 assert(n >= 0);
210                 offset = random() % (file_size / io_size);
211                 offset *= io_size;
212                 set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
213         }
214
215         gettimeofday(&et, NULL);
216         timersub(&et, &st, &rt);
217         f_rt = ((float) (rt.tv_usec)) / 1000000.0;
218         f_rt += (float) (rt.tv_sec);
219         printf("Runtime: %.2f seconds, ", f_rt);
220         printf("Op rate: %.2f ops/sec, ", ((float) (nrun))  / f_rt);
221         printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
222
223
224
225         exit(0);
226 }