2 * Copyright (c) 2006, Stephan Uphoff <ups@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
38 int prepareFile(char* filename,int* fdp);
39 int mapBuffer(char** bufferp,int fd1,int fd2);
40 int startIO(int fd,char *buffer);
44 #define FILESIZE (32*1024)
45 char wbuffer[FILESIZE];
47 /* Create a FILESIZE sized file - then remove file data from the cache*/
48 int prepareFile(char* filename,int* fdp)
55 fd = open(filename,O_CREAT | O_TRUNC | O_RDWR,S_IRWXU);
58 perror("Creating file");
62 len = write(fd,wbuffer,FILESIZE);
65 perror("Write failed");
72 perror("fsync failed");
76 addr = mmap(NULL,FILESIZE, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0);
77 if (addr == MAP_FAILED)
79 perror("Mmap failed");
83 status = msync(addr,FILESIZE,MS_INVALIDATE | MS_SYNC);
86 perror("Msync failed");
90 munmap(addr,FILESIZE);
97 /* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
98 int mapBuffer(char** bufferp,int fd1,int fd2)
103 addr = mmap(NULL,pagesize*2, PROT_READ | PROT_WRITE , MAP_SHARED, fd1, 0);
104 if (addr == MAP_FAILED)
106 perror("Mmap failed");
111 addr = mmap(buffer + pagesize,pagesize, PROT_READ | PROT_WRITE , MAP_FIXED |
114 if (addr == MAP_FAILED)
116 perror("Mmap2 failed");
124 int startIO(int fd,char *buffer)
127 len = write(fd,buffer,2*pagesize);
130 perror("write failed");
137 int main(int argc,char *argv[],char *envp[])
140 int fdA,fdB,fdDelayA,fdDelayB;
142 char *bufferA,*bufferB;
145 pagesize = getpagesize();
147 if ((prepareFile("A",&fdA))
148 || (prepareFile("B",&fdB))
149 || (prepareFile("DelayA",&fdDelayA))
150 || (prepareFile("DelayB",&fdDelayB))
151 || (mapBuffer(&bufferA,fdDelayA,fdB))
152 || (mapBuffer(&bufferB,fdDelayB,fdA)))
159 status = startIO(fdA,bufferA);
167 status = startIO(fdB,bufferB);