Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/test/stress2/testcases/rw/rw.c
39566 views
1
/*-
2
* Copyright (c) 2008 Peter Holm <[email protected]>
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, 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.
13
*
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
24
* SUCH DAMAGE.
25
*
26
*/
27
28
/* Write and check read a file */
29
30
#include <sys/param.h>
31
#include <sys/stat.h>
32
#include <err.h>
33
#include <fcntl.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <unistd.h>
37
38
#include "stress.h"
39
40
static char path[128];
41
static int starting_dir;
42
static unsigned long size;
43
44
#define MAXSIZE 256 * 1024
45
46
int
47
setup(int nb)
48
{
49
int64_t bl;
50
int64_t in;
51
int64_t reserve_in;
52
int64_t reserve_bl;
53
int pct;
54
55
if (nb == 0) {
56
getdf(&bl, &in);
57
size = bl / op->incarnations / 1024;
58
59
pct = 90;
60
if (op->hog == 0)
61
pct = random_int(1, 90);
62
size = size / 100 * pct + 1;
63
64
if (size > MAXSIZE)
65
size = MAXSIZE; /* arbitrary limit size pr. incarnation */
66
67
/* Resource requirements: */
68
while (size > 0) {
69
reserve_in = 2 * op->incarnations + 1;
70
reserve_bl = size * 1024 * op->incarnations +
71
(512 * 1024 * op->incarnations) +
72
64 * 1024;
73
if (reserve_bl <= bl && reserve_in <= in)
74
break;
75
size = size / 10 * 8;
76
}
77
if (size == 0)
78
reserve_bl = reserve_in = 0;
79
80
if (op->verbose > 1)
81
printf("rw(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
82
size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
83
reservedf(reserve_bl, reserve_in);
84
putval(size);
85
size = size * 1024;
86
} else {
87
size = getval();
88
size = size * 1024;
89
}
90
if (size == 0)
91
exit(0);
92
93
umask(0);
94
sprintf(path,"%s.%05d", getprogname(), getpid());
95
(void)mkdir(path, 0770);
96
if (chdir(path) == -1)
97
err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
98
if ((starting_dir = open(".", 0)) < 0)
99
err(1, ".");
100
101
return (0);
102
}
103
104
void
105
cleanup(void)
106
{
107
if (starting_dir == 0)
108
return;
109
if (fchdir(starting_dir) == -1)
110
err(1, "fchdir()");
111
if (close(starting_dir) < 0)
112
err(1, "close(starting_dir:%d)", starting_dir);
113
114
(void)system("find . -delete");
115
116
if (chdir("..") == -1)
117
err(1, "chdir(..)");
118
if (rmdir(path) == -1)
119
err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
120
size = 0;
121
}
122
123
int
124
test(void)
125
{
126
int buf[1024], index, to;
127
#ifdef TEST
128
int i;
129
#endif
130
int fd;
131
char file[128];
132
133
sprintf(file,"p%05d", getpid());
134
if ((fd = creat(file, 0660)) == -1)
135
err(1, "creat(%s)", file);
136
137
to = sizeof(buf);
138
index = 0;
139
while (index < (int)size) {
140
if (index + to > (int)size)
141
to = size - index;
142
#ifdef TEST
143
for (i = 0; i < to; i++)
144
buf[i] = index + i;
145
#endif
146
index += to;
147
if (write(fd, buf, to) != to)
148
err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
149
}
150
if (close(fd) == -1)
151
err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
152
153
if ((fd = open(file, O_RDONLY)) == -1)
154
err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
155
156
index = 0;
157
while (index < (int)size && done_testing == 0) {
158
if (index + to > (int)size)
159
to = size - index;
160
if (read(fd, buf, to) != to)
161
err(1, "rw read. %s.%d", __FILE__, __LINE__);
162
#ifdef TEST
163
for (i = 0; i < to; i++) {
164
if (buf[i] != index + i) {
165
fprintf(stderr,
166
"%s, pid %d: expected %d @ %d, got %d\n",
167
getprogname(), getpid(), index+i, index+i,
168
buf[i]);
169
exit(EXIT_FAILURE);
170
}
171
}
172
#endif
173
index += to;
174
}
175
if (close(fd) == -1)
176
err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
177
if (unlink(file) == -1)
178
err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
179
return (0);
180
}
181
182