1/*-
2 * Copyright (c) 2006, Stephan Uphoff <ups@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
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.
14 *
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.
25 */
26
27
28#include <sys/types.h>
29#include <unistd.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <sys/mman.h>
35
36
37int prepareFile(const char* filename,int* fdp);
38int mapBuffer(char** bufferp,int fd1,int fd2);
39int startIO(int fd,char *buffer);
40
41static int pagesize;
42
43#define FILESIZE (32*1024)
44static char wbuffer[FILESIZE];
45
46/* Create a FILESIZE sized file - then remove file data from the cache*/
47int prepareFile(const char* filename,int* fdp)
48{
49  int fd;
50  int len;
51  int status;
52  void *addr;
53
54  fd = open(filename,O_CREAT | O_TRUNC | O_RDWR,S_IRWXU);
55  if (fd == -1)
56    {
57      perror("Creating file");
58      return fd;
59    }
60
61  len = write(fd,wbuffer,FILESIZE);
62  if (len < 0)
63    {
64      perror("Write failed");
65      return 1;
66    }
67
68  status = fsync(fd);
69   if (status != 0)
70    {
71        perror("fsync failed");
72	return 1;
73    }
74
75  addr = mmap(NULL,FILESIZE, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0);
76  if (addr == MAP_FAILED)
77    {
78      perror("Mmap failed");
79      return 1;
80    }
81
82  status = msync(addr,FILESIZE,MS_INVALIDATE | MS_SYNC);
83  if (status != 0)
84    {
85        perror("Msync failed");
86	return 1;
87    }
88
89  munmap(addr,FILESIZE);
90
91  *fdp = fd;
92  return 0;
93}
94
95
96/* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
97int mapBuffer(char** bufferp,int fd1,int fd2)
98{
99  void* addr;
100  char *buffer;
101
102  addr = mmap(NULL,pagesize*2, PROT_READ | PROT_WRITE , MAP_SHARED, fd1, 0);
103  if (addr == MAP_FAILED)
104    {
105      perror("Mmap failed");
106      return 1;
107    }
108
109  buffer = addr;
110  addr = mmap(buffer + pagesize,pagesize, PROT_READ | PROT_WRITE , MAP_FIXED |
111MAP_SHARED, fd2, 0);
112
113  if (addr == MAP_FAILED)
114    {
115      perror("Mmap2 failed");
116      return 1;
117    }
118  *bufferp = buffer;
119  return 0;
120}
121
122
123int startIO(int fd,char *buffer)
124{
125  ssize_t len;
126  len = write(fd,buffer,2*pagesize);
127  if (len == -1)
128    {
129      perror("write failed");
130      return 1;
131    }
132  return 0;
133}
134
135
136int main(int argc __unused, char *argv[] __unused)
137{
138
139  int fdA,fdB,fdDelayA,fdDelayB;
140  int status;
141  char *bufferA,*bufferB;
142  pid_t pid;
143
144  pagesize = getpagesize();
145
146  if ((prepareFile("A",&fdA))
147      || (prepareFile("B",&fdB))
148      || (prepareFile("DelayA",&fdDelayA))
149      || (prepareFile("DelayB",&fdDelayB))
150      || (mapBuffer(&bufferA,fdDelayA,fdB))
151      || (mapBuffer(&bufferB,fdDelayB,fdA)))
152    exit(1);
153
154  pid = fork();
155
156  if (pid == 0)
157    {
158      status = startIO(fdA,bufferA);
159      exit(status);
160    }
161
162  if (pid == -1)
163    {
164      exit(1);
165    }
166  status = startIO(fdB,bufferB);
167  exit(status);
168
169}
170
171
172
173
174
175