-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmtest.c
241 lines (201 loc) · 5.22 KB
/
mtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* test.c
*
* Tests kernel handling of shared private data pages.
*
* (C) Stephen C. Tweedie <sct@redhat.com>, 1998
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "misc_lib.h"
int memory = 8; /* Default --- use 8 MB of heap */
int rprocesses = 8; /* Number of reader processes */
int wprocesses = 1; /* Number of writer processes */
int do_fork = 0; /* Enable random fork/die if true */
int pagesize;
char *progname;
char *heap; /* Allocated space for a large working set */
int *patterns; /* Record the patterns we expect in each heap page */
int nr_pages; /* How many pages are in the heap */
static void setup_memory(void); /* Setup the heap and pattern arrays*/
static void fork_child(int n, int writer); /* Spawn a new child process */
static void fork_new_child(void); /* Perform a fork/die
in this child */
static void run_test(int writer); /* Run the test sweep
within a child */
static void null_handler(int); /* Just a null signal handler */
static void page_error(int); /* Report a heap
pattern mismatch */
#define page_val(page) (* (int *) (heap + pagesize * (page)))
static void usage(char *error)
{
if (error)
fprintf (stderr, "%s: %s\n", progname, error);
fprintf (stderr,
"Usage: %s [-h] [-m memsize] "
"[-r readers] [-w writers]\n", progname);
exit (error ? 1 : 0);
}
int main(int argc, char *argv[])
{
int c, i;
progname = argv[0];
while (c = getopt(argc, argv, "fhm:r:w:"), c != EOF) {
switch (c) {
case ':':
usage("missing argument");
case '?':
usage("unrecognised argument");
case 'f':
do_fork = 1;
break;
case 'h':
usage(0);
case 'm':
memory = strtoul(optarg, 0, 0);
break;
case 'r':
rprocesses = strtoul(optarg, 0, 0);
break;
case 'w':
wprocesses = strtoul(optarg, 0, 0);
break;
default:
usage("unknown error");
}
}
fprintf (stderr, "Starting test run with %d megabyte heap.\n", memory);
setup_memory();
for (i=0; i<rprocesses; i++)
fork_child(i, 0);
for (; i<rprocesses+wprocesses; i++)
fork_child(i, 1);
fprintf (stderr, "%d child processes started.\n", i);
for (;;) {
pid_t pid;
int status;
/* Catch child error statuses and report them. */
pid = wait3(&status, 0, 0);
if (pid < 0) /* No more children? */
exit(0);
if (WIFEXITED (status)) {
if (WEXITSTATUS (status))
fprintf (stderr,
"Child %d exited with status %d\n",
pid, WEXITSTATUS(status));
else
fprintf (stderr,
"Child %d exited with normally\n",
pid);
} else {
fprintf (stderr,
"Child %d exited with signal %d\n",
pid, WTERMSIG(status));
}
}
}
static void setup_memory(void)
{
int i;
pagesize = getpagesize();
nr_pages = memory * 1024 * 1024 / pagesize;
fprintf (stderr, "Setting up %d %dkB pages for test...",
nr_pages, pagesize);
patterns = safe_malloc(nr_pages * sizeof(*patterns),"setup_memory");
heap = safe_malloc(nr_pages * pagesize, "setup_memory");
for (i=0; i<nr_pages; i++) {
page_val(i) = i;
patterns[i] = i;
}
fprintf (stderr, " done.\n");
}
static void fork_child(int n, int writer)
{
pid_t pid, parent;
parent = getpid();
signal (SIGUSR1, null_handler);
pid = fork();
if (pid) {
/* Are we the parent? Wait for the child to print the
startup banner. */
pause();
} else {
/* Are we the child? Print a banner, then signal the parent
to continue. */
fprintf (stderr, "Child %02d started with pid %05d, %s\n",
n, getpid(), writer ? "writer" : "readonly");
kill (parent, SIGUSR1);
run_test(writer);
/* The test should never terminate. Exit with an error if
it does. */
exit(2);
}
}
static void run_test(int writer)
{
int count = 0;
int time_to_live = 0;
int page;
/* Give each child a different random seed. */
srandom(getpid() * time(0));
for (;;) {
/* Track the time until the next fork/die round */
if (do_fork) {
if (time_to_live) {
if (!--time_to_live)
fork_new_child();
}
else
time_to_live = random() % 50;
}
/* Pick a page and check its contents. */
page = ((unsigned) random()) % nr_pages;
if (page_val(page) != patterns[page])
page_error(page);
/* Writer tasks should modify pages occasionally, too. */
if (writer && count++ > 10) {
count = 0;
patterns[page] = page_val(page) = random();
}
}
}
static void fork_new_child(void)
{
int old_pid = getpid();
int pid;
pid = fork();
if (pid == -1) {
perror("fork");
exit(errno);
}
if (pid) {
/* Are we the parent? Wait for the child to print the
fork banner. */
pause();
exit(0);
} else {
/* Are we the child? Print a banner, then signal the parent
to continue. */
fprintf (stderr, "Child %05d forked into pid %05d\n",
old_pid, getpid());
kill (old_pid, SIGUSR1);
}
}
static void null_handler(int n)
{
}
static void page_error(int page)
{
fprintf (stderr,
"Child %05d failed at page %d, address %p: "
"expected %08x, found %08x\n",
getpid(), page, &page_val(page),
patterns[page], page_val(page));
exit(3);
}