2 * Copyright (c) 2008 Peter Holm <pho@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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* Write and check read a file */
35 #include <sys/param.h>
40 static char path[128];
41 static int starting_dir;
42 static unsigned long size;
44 #define MAXSIZE 256 * 1024
57 size = bl / op->incarnations / 1024;
61 pct = random_int(1, 90);
62 size = size / 100 * pct + 1;
65 size = MAXSIZE; /* arbitrary limit size pr. incarnation */
67 /* Resource requirements: */
69 reserve_in = 2 * op->incarnations + 1;
70 reserve_bl = size * 1024 * op->incarnations +
71 (512 * 1024 * op->incarnations) +
73 // printf("-- size = %lu, reserve(%jd, %jd)\n", size, reserve_bl/1024, reserve_in);
74 if (reserve_bl <= bl && reserve_in <= in)
79 reserve_bl = reserve_in = 0;
82 printf("rw(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
83 size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
84 reservedf(reserve_bl, reserve_in);
93 sprintf(path,"%s.%05d", getprogname(), getpid());
94 (void)mkdir(path, 0770);
95 if (chdir(path) == -1)
96 err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
97 if ((starting_dir = open(".", 0)) < 0)
109 if (fchdir(starting_dir) == -1)
111 if (close(starting_dir) < 0)
112 err(1, "close(starting_dir:%d)", starting_dir);
114 (void)system("find . -delete");
116 if (chdir("..") == -1)
118 if (rmdir(path) == -1)
119 err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
126 int buf[1024], index, to;
134 sprintf(file,"p%05d", getpid());
135 if ((fd = creat(file, 0660)) == -1)
136 err(1, "creat(%s)", file);
140 while (index < size) {
141 if (index + to > size)
144 for (i = 0; i < to; i++)
148 if (write(fd, buf, to) != to)
149 err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
152 err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
154 if ((fd = open(file, O_RDONLY)) == -1)
155 err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
158 while (index < size && done_testing == 0) {
159 if (index + to > size)
161 if (read(fd, buf, to) != to)
162 err(1, "rw read. %s.%d", __FILE__, __LINE__);
164 for (i = 0; i < to; i++) {
165 if (buf[i] != index + i) {
167 "%s, pid %d: expected %d @ %d, got %d\n",
168 getprogname(), getpid(), index+i, index+i,
177 err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
178 if (unlink(file) == -1)
179 err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);