4 # Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Variation of the datamove.sh scenario by not using "sysctl vm.old_msync=1"
35 # Test scenario by ups
39 sed '1,/^EOF/d' < $here/$0 > dl.c
44 $here/../testcases/swap/swap -t 10m -i 200 -h &
46 ps | grep swap | grep -v swap | awk '{print $1}' | xargs kill
57 #include <sys/types.h>
62 int prepareFile(char *filename, int *fdp);
63 int mapBuffer (char **bufferp, int fd1, int fd2);
64 int startIO (int fd, char *buffer);
68 #define FILESIZE (32*1024)
69 char wbuffer [FILESIZE];
71 /* Create a FILESIZE sized file - then remove file data from the cache */
73 prepareFile(char *filename, int *fdp)
80 fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
82 perror("Creating file");
85 len = write(fd, wbuffer, FILESIZE);
87 perror("Write failed");
92 perror("fsync failed");
95 addr = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
96 if (addr == MAP_FAILED) {
97 perror("Mmap failed");
100 status = msync(addr, FILESIZE, MS_INVALIDATE | MS_SYNC);
102 perror("Msync failed");
105 if (munmap(addr, FILESIZE) == -1) {
106 perror("munmap failed");
115 /* mmap a 2 page buffer - first page is from fd1, second page from fd2 */
117 mapBuffer(char **bufferp, int fd1, int fd2)
122 addr = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
123 if (addr == MAP_FAILED) {
124 perror("Mmap failed");
128 addr = mmap(buffer + pagesize, pagesize, PROT_READ | PROT_WRITE, MAP_FIXED |
131 if (addr == MAP_FAILED) {
132 perror("Mmap2 failed");
140 unmapBuffer(char *bufferp)
142 if (munmap(bufferp, pagesize * 2) == -1)
143 err(1, "unmap 1. buffer");
144 if (munmap(bufferp + pagesize * 2, pagesize * 2) == -1)
145 err(1, "unmap 2. buffer");
149 startIO(int fd, char *buffer)
153 len = write(fd, buffer, 2 * pagesize);
155 perror("write failed");
163 main(int argc, char *argv[], char *envp[])
166 int fdA, fdB, fdDelayA, fdDelayB;
169 char *bufferA, *bufferB;
172 pagesize = getpagesize();
174 for (i = 0; i < 1000; i++) {
175 if ((prepareFile("A", &fdA))
176 || (prepareFile("B", &fdB))
177 || (prepareFile("DelayA", &fdDelayA))
178 || (prepareFile("DelayB", &fdDelayB))
179 || (mapBuffer(&bufferA, fdDelayA, fdB))
180 || (mapBuffer(&bufferB, fdDelayB, fdA)))
186 status = startIO(fdA, bufferA);
193 status = startIO(fdB, bufferB);
194 if (wait(&status) == -1)
201 unmapBuffer(bufferA);
202 unmapBuffer(bufferB);