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