blob: 3812cef283472b3afc79d29e5c9ada5165029668 [file] [log] [blame]
robbiewe093b822003-01-10 18:24:49 +00001/* IBM Corporation */
2/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
3/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
4/*
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
subrata_modak4bb656a2009-02-26 12:02:09 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
robbiewe093b822003-01-10 18:24:49 +000012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
Garrett Cooper2c282152010-12-16 00:55:50 -080014 *
robbiewe093b822003-01-10 18:24:49 +000015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewe093b822003-01-10 18:24:49 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020#define _GNU_SOURCE 1
robbiewe093b822003-01-10 18:24:49 +000021#include <stdio.h>
22#include <fcntl.h>
23#include <signal.h>
24#include <sys/mman.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <sys/types.h>
31#include <limits.h>
32/***** LTP Port *****/
33#include "test.h"
robbiewe093b822003-01-10 18:24:49 +000034#define FAILED 0
35#define PASSED 1
36
37int local_flag = PASSED;
subrata_modak4bb656a2009-02-26 12:02:09 +000038char *TCID = "mmapstress10";
robbiewe093b822003-01-10 18:24:49 +000039FILE *temp;
40int TST_TOTAL = 1;
robbiewe093b822003-01-10 18:24:49 +000041
42int anyfail();
43void ok_exit();
44/***** ** ** *****/
45
robbiewe093b822003-01-10 18:24:49 +000046/*
47 * This test stresses mmaps, specifically the code dealing with
48 * mapping of fragments. It forks a specified number of children,
49 * all of whom mmap the same file, make a given number of accesses
50 * to random pages in the map (reading & writing and comparing data).
51 * Then the child exits and the parent forks another to take its place.
52 * Each time a child is forked, it stats the file and maps the full
53 * length of the file. Meanwhile, another child is forked which
subrata_modak4bb656a2009-02-26 12:02:09 +000054 * continually writes to the file. It loops writing some bytes (default
robbiewe093b822003-01-10 18:24:49 +000055 * 20), then sleeps some seconds (default 1). This causes the file
56 * to gradually grow, crossing fragment boundaries, etc.
57 * Then it forks yet *another* child who maps the file in extend
58 * mode, just to check out interaction. (Because this will cause
59 * 0 padding at end of file, children can't test as exactly as in tmmap -
60 * have to check for zero or pattern...)
61 *
62 * This program continues to run until it either receives a SIGINT,
63 * or times out (if a timeout value is specified). When either of
64 * these things happens, it cleans up its kids, then checks the
65 * file to make sure it has the correct data.
66 *
67 * usage:
68 * mmapstress10 -p nprocs [-t minutes -w nbytes -s secs -f filesize
69 * -S sparseoffset -r -o -m -l -d]
70 * where:
71 * -p nprocs - specifies the number of mapping children
72 * to create. (nprocs + 1 children actually
73 * get created, since one is the writer child)
74 * -t minutes - specifies minutes to run. If not specified,
75 * default is to run forever until a SIGINT
76 * is received.
77 * -w nbytes - specifies number of bytes for writer process
78 * to write at a time (i.e. between sleeps).
79 * defaults to GROWSIZE bytes.
80 * -s secs - specifies number of seconds for writer process
81 * to sleep between writes. Defaults to
82 * SLEEPTIME seconds.
83 * -f filesize - initial filesize (defaults to FILESIZE)
84 * -S sparseoffset - when non-zero, causes a sparse area to
85 * be left before the data, meaning that the
86 * actual initial file size is sparseoffset +
87 * filesize. Useful for testing large files.
88 * (default is 0).
89 * -r - randomize number of pages map children check.
90 * (random % MAXLOOPS). If not specified, each
91 * child checks MAXLOOPS pages.
92 * -o - randomize offset of file to map. (default is 0)
93 * -m - do random msync/fsyncs as well
94 * -l - if set, the output file is not removed on
95 * program exit.
96 * -d - enable debug outputd
97 *
98 * Compile with -DLARGE_FILE to enable file sizes > 2 GB.
99 */
100
101#define MAXLOOPS 500 /* max pages for map children to write */
102#define GROWSIZE 20 /* # bytes to write per write call */
103#define SLEEPTIME 1 /* # secs to sleep between writes */
subrata_modakbdbaec52009-02-26 12:14:51 +0000104#define FILESIZE 4096 /* initial filesize set up by parent */
robbiew4be90982003-07-31 15:15:25 +0000105#ifdef roundup
106#undef roundup
107#endif
robbiewe093b822003-01-10 18:24:49 +0000108#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
109#define min(x, y) (((x) < (y)) ? (x) : (y))
110
111#define SIZE_MAX UINT_MAX
112
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113extern time_t time(time_t *);
114extern char *ctime(const time_t *);
robbiewe093b822003-01-10 18:24:49 +0000115extern void *malloc(size_t);
116extern void exit(int);
117extern long lrand48(void);
118extern void srand(unsigned);
119extern void srand48(long);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120extern int rand(void);
121extern int atoi(const char *);
robbiewe093b822003-01-10 18:24:49 +0000122
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123char *usage =
124 "-p nprocs [-t minutes -w nbytes -s secs -f fsize -S sparseoffset -r -o -m -l -d]";
robbiewe093b822003-01-10 18:24:49 +0000125
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126typedef unsigned char uchar_t; //Ananda 12/17/02
robbiewe093b822003-01-10 18:24:49 +0000127
robbiewe093b822003-01-10 18:24:49 +0000128void child_mapper(char *file, unsigned procno, unsigned nprocs);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129void child_writer(char *file, uchar_t * buf);
130int fileokay(char *file, uchar_t * expbuf);
robbiewe093b822003-01-10 18:24:49 +0000131unsigned int initrand(void);
132void finish(int sig);
133void clean_up_file(int sig);
134int finished = 0;
135int leavefile = 0;
136
137int debug = 0;
138int growsize = GROWSIZE;
139int sleeptime = SLEEPTIME;
140#ifdef LARGE_FILE
141off64_t filesize = FILESIZE;
142off64_t sparseoffset = 0;
143#else /* LARGE_FILE */
144off_t filesize = FILESIZE;
145off_t sparseoffset = 0;
146#endif /* LARGE_FILE */
147unsigned randloops = 0;
148unsigned dosync = 0;
149unsigned do_offset = 0;
150unsigned pattern = 0;
151char filename[64];
152
Subrata Modak76a720a2010-07-03 21:08:18 +0530153void clean_mapper(int sig);
154void clean_writer(int sig);
155
156int fd_mapper = 0;
157caddr_t maddr_mapper;
158size_t mapsize_mapper;
159
160int fd_writer = 0;
161
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162int main(int argc, char *argv[])
robbiewe093b822003-01-10 18:24:49 +0000163{
164 char *progname;
165 int fd;
166 int c;
167 extern char *optarg;
168 unsigned nprocs = 0;
169 unsigned procno;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 pid_t *pidarray = NULL;
robbiewe093b822003-01-10 18:24:49 +0000171 pid_t pid;
172 pid_t wr_pid = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 uchar_t *buf = NULL;
robbiewe093b822003-01-10 18:24:49 +0000174 unsigned int seed;
175 int pagesize = sysconf(_SC_PAGE_SIZE);
subrata_modakae1b3952008-10-30 03:53:09 +0000176 float alarmtime = 0;
robbiewe093b822003-01-10 18:24:49 +0000177 struct sigaction sa;
178 unsigned i;
179 int write_cnt;
180 uchar_t data;
181 int no_prob = 0;
182 int wait_stat;
183 time_t t;
184#ifdef LARGE_FILE
185 off64_t bytes_left;
186#else /* LARGE_FILE */
187 off_t bytes_left;
188#endif /* LARGE_FILE */
189
190 progname = *argv;
191 tst_tmpdir();
192 if (argc < 2) {
193 (void)fprintf(stderr, "usage: %s %s\n", progname, usage);
194 exit(1);
195 }
196
197 while ((c = getopt(argc, argv, "S:omdlrf:p:t:w:s:")) != -1) {
198 switch (c) {
199 case 'd':
200 debug = 1;
201 break;
202 case 't':
subrata_modakae1b3952008-10-30 03:53:09 +0000203 alarmtime = atof(optarg) * 60;
robbiewe093b822003-01-10 18:24:49 +0000204 break;
205 case 'p':
206 nprocs = atoi(optarg);
207 break;
208 case 'l':
209 leavefile = 1;
210 break;
211 case 's':
212 sleeptime = atoi(optarg);
213 if (sleeptime < 0) {
214 (void)fprintf(stderr, "error: negative "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800215 "sleeptime\n");
216 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000217 }
218 break;
219 case 'w':
220 growsize = atoi(optarg);
221 if (growsize < 0) {
222 (void)fprintf(stderr, "error: negative write "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 "size\n");
224 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000225 }
226 break;
227 case 'f':
228#ifdef LARGE_FILE
229 filesize = atoll(optarg);
230#else /* LARGE_FILE */
231 filesize = atoi(optarg);
232#endif /* LARGE_FILE */
233 if (filesize < 0) {
234 (void)fprintf(stderr, "error: negative "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 "filesize\n");
236 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000237 }
238 break;
239 case 'r':
240 randloops = 1;
241 break;
242 case 'm':
243 dosync = 1;
244 break;
245 case 'o':
246 do_offset = 1;
247 break;
248 case 'S':
249#ifdef LARGE_FILE
250 sparseoffset = atoll(optarg);
251#else /* LARGE_FILE */
252 sparseoffset = atoi(optarg);
253#endif /* LARGE_FILE */
254 if (sparseoffset % pagesize != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000255 fprintf(stderr,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 "sparseoffset must be pagesize multiple\n");
257 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000258 }
259 break;
260 default:
261 (void)fprintf(stderr, "usage: %s %s\n", progname,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 usage);
robbiewe093b822003-01-10 18:24:49 +0000263 anyfail();
264 }
265 }
266
267 if (nprocs > 255) {
268 (void)fprintf(stderr, "invalid nprocs %d - (range 0-255)\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800269 nprocs);
270 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000271 }
272 (void)time(&t);
273 //(void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port
274
275 (void)sprintf(filename, "%sout.%d", progname, getpid());
276 seed = initrand();
277 pattern = seed & 0xff;
278
279 if (debug) {
280#ifdef LARGE_FILE
subrata_modak4bb656a2009-02-26 12:02:09 +0000281 (void)printf("creating file <%s> with %Ld bytes, pattern %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800282 filename, filesize, pattern);
robbiewe093b822003-01-10 18:24:49 +0000283#else /* LARGE_FILE */
subrata_modak4bb656a2009-02-26 12:02:09 +0000284 (void)printf("creating file <%s> with %ld bytes, pattern %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800285 filename, filesize, pattern);
robbiewe093b822003-01-10 18:24:49 +0000286#endif /* LARGE_FILE */
287 if (alarmtime)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800288 (void)printf("running for %f minutes\n",
289 alarmtime / 60);
robbiewe093b822003-01-10 18:24:49 +0000290 else
291 (void)printf("running with no time limit\n");
292 }
293
294 /*
295 * Plan for death by signal. User may have specified
296 * a time limit, in which case set an alarm and catch SIGALRM.
297 * Also catch and cleanup with SIGINT, SIGQUIT, and SIGTERM.
298 */
299 sa.sa_handler = finish;
300 sa.sa_flags = 0;
301 if (sigemptyset(&sa.sa_mask)) {
302 perror("sigempty error");
303 goto cleanup;
304 }
305
306 if (sigaction(SIGINT, &sa, 0) == -1) {
307 perror("sigaction error SIGINT");
308 goto cleanup;
309 }
310 if (alarmtime) {
311 if (sigaction(SIGALRM, &sa, 0) == -1) {
312 perror("sigaction error");
313 goto cleanup;
314 }
315 (void)alarm(alarmtime);
316 }
317 /* If we get a SIGQUIT or SIGTERM, clean up and exit immediately. */
318 sa.sa_handler = clean_up_file;
319 if (sigaction(SIGQUIT, &sa, 0) == -1) {
320 perror("sigaction error SIGQUIT");
321 goto cleanup;
322 }
323 if (sigaction(SIGTERM, &sa, 0) == -1) {
324 perror("sigaction error SIGTERM");
325 goto cleanup;
326 }
robbiewe093b822003-01-10 18:24:49 +0000327#ifdef LARGE_FILE
Wanlong Gao354ebb42012-12-07 10:10:04 +0800328 if ((fd = open64(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000329#else /* LARGE_FILE */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800330 if ((fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000331#endif /* LARGE_FILE */
332 perror("open error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800333 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000334 }
335
Cyril Hrubisd218f342014-09-23 13:14:56 +0200336 if ((buf = malloc(pagesize + growsize)) == NULL
337 || (pidarray = malloc(nprocs * sizeof(pid_t))) == NULL) {
robbiewe093b822003-01-10 18:24:49 +0000338 perror("malloc error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800339 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000340 }
341
342 for (i = 0; i < nprocs; i++)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800343 *(pidarray + i) = 0;
robbiewe093b822003-01-10 18:24:49 +0000344
345 for (i = 0, data = 0; i < pagesize; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800346 *(buf + i) = (data + pattern) & 0xff;
robbiewe093b822003-01-10 18:24:49 +0000347 if (++data == nprocs)
348 data = 0;
349 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800350 for (data = 0; i < pagesize + growsize; i++) {
351 *(buf + i) = (data + pattern) & 0xff;
robbiewe093b822003-01-10 18:24:49 +0000352 if (++data == nprocs)
353 data = 0;
354 }
355
356#ifdef LARGE_FILE
357 if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
358#else /* LARGE_FILE */
359 if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
360#endif /* LARGE_FILE */
361 perror("lseek");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800362 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000363 }
364
365 for (bytes_left = filesize; bytes_left; bytes_left -= c) {
366 write_cnt = min(pagesize, bytes_left);
367 if ((c = write(fd, (char *)buf, write_cnt)) != write_cnt) {
368 if (c == -1) {
369 perror("write error");
370 } else {
371 (void)fprintf(stderr, "write: wrote %d of %d "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800372 "bytes\n", c, write_cnt);
robbiewe093b822003-01-10 18:24:49 +0000373 }
374 (void)close(fd);
375 (void)unlink(filename);
376 anyfail();
377 }
378 }
379
380 (void)close(fd);
381
382 /*
383 * Fork off mmap children.
384 */
385 for (procno = 0; procno < nprocs; procno++) {
386 switch (pid = fork()) {
387
388 case -1:
389 perror("fork error");
390 goto cleanup;
391
392 case 0:
393 child_mapper(filename, procno, nprocs);
394 exit(0);
395
396 default:
397 pidarray[procno] = pid;
398 }
399 }
400
401 /*
402 * Now fork off an additional process to continually
403 * write to (and grow) the file.
404 */
405 if ((wr_pid = fork()) == -1) {
406 perror("fork error");
407 goto cleanup;
408 } else if (wr_pid == 0) { /* child */
409 child_writer(filename, buf);
410 exit(0);
411 }
412
413 /*
414 * Now wait for children and refork them as needed.
415 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000416
robbiewe093b822003-01-10 18:24:49 +0000417 while (!finished) {
418 pid = wait(&wait_stat);
419 /*
420 * Block signals while processing child exit.
421 */
422
423 if (sighold(SIGALRM) || sighold(SIGINT)) {
424 perror("sighold error");
425 goto cleanup;
426 }
427
428 if (pid != -1) {
429 /*
430 * Check exit status, then refork with the
431 * appropriate procno.
432 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000433 if (!WIFEXITED(wait_stat)
robbiewe093b822003-01-10 18:24:49 +0000434 || WEXITSTATUS(wait_stat) != 0) {
435 (void)fprintf(stderr, "child exit with err "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800436 "<x%x>\n", wait_stat);
robbiewe093b822003-01-10 18:24:49 +0000437 goto cleanup;
438 }
439 for (i = 0; i < nprocs; i++)
440 if (pid == pidarray[i])
441 break;
442 if (i == nprocs) {
443 if (pid == wr_pid) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000444 (void)fprintf(stderr,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800445 "writer child unexpected exit <x%x>\n",
446 wait_stat);
robbiewe093b822003-01-10 18:24:49 +0000447 wr_pid = 0;
448 } else
449 (void)fprintf(stderr, "unknown child "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800450 "pid %d, <x%x>\n",
451 pid, wait_stat);
robbiewe093b822003-01-10 18:24:49 +0000452 goto cleanup;
453 }
454
455 if ((pid = fork()) == -1) {
456 perror("fork error");
457 pidarray[i] = 0;
458 goto cleanup;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800459 } else if (pid == 0) { /* child */
robbiewe093b822003-01-10 18:24:49 +0000460 child_mapper(filename, i, nprocs);
461 exit(0);
462 } else
463 pidarray[i] = pid;
464 } else {
465 /*
466 * wait returned an error. If EINTR, then
467 * normal finish, else it's an unexpected
468 * error...
469 */
470 if (errno != EINTR || !finished) {
471 perror("unexpected wait error");
472 goto cleanup;
473 }
474 }
475 if (sigrelse(SIGALRM) || sigrelse(SIGINT)) {
476 perror("sigrelse error");
477 goto cleanup;
478 }
479 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000480
robbiewe093b822003-01-10 18:24:49 +0000481 /*
482 * Finished! Check the file for sanity, then kill all
483 * the children and done!.
484 */
485
486 (void)alarm(0);
487 no_prob = 1;
488
489cleanup:
490 for (i = 0; i < nprocs; i++)
Subrata Modak76a720a2010-07-03 21:08:18 +0530491 (void)kill(pidarray[i], SIGUSR1);
492 (void)kill(wr_pid, SIGUSR1);
robbiewe093b822003-01-10 18:24:49 +0000493
494 while (wait(&wait_stat) != -1 || errno != ECHILD)
495 continue;
496
497 if (no_prob) { /* only check file if no errors */
498 if (!fileokay(filename, buf)) {
499 (void)fprintf(stderr, "file data incorrect!\n");
500 (void)printf(" leaving file <%s>\n", filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800501 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000502
503 } else {
504 (void)printf("file data okay\n");
505 if (!leavefile)
506 (void)unlink(filename);
507 }
508 } else
509 (void)printf(" leaving file <%s>\n", filename);
subrata_modakbdbaec52009-02-26 12:14:51 +0000510
robbiewe093b822003-01-10 18:24:49 +0000511 (void)time(&t);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800512// (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
robbiewe093b822003-01-10 18:24:49 +0000513 ok_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800514 tst_exit();
robbiewe093b822003-01-10 18:24:49 +0000515}
516
robbiewe093b822003-01-10 18:24:49 +0000517/*
518 * Child process that reads/writes map. The child stats the file
519 * to determine the size, maps the size of the file, then reads/writes
520 * its own locations on random pages of the map (its locations being
521 * determined based on nprocs & procno). After a specific number of
522 * iterations, it exits.
523 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800524void child_mapper(char *file, unsigned procno, unsigned nprocs)
robbiewe093b822003-01-10 18:24:49 +0000525{
526#ifdef LARGE_FILE
527 struct stat64 statbuf;
528 off64_t filesize;
529 off64_t offset;
530#else /* LARGE_FILE */
531 struct stat statbuf;
532 off_t filesize;
533 off_t offset;
534#endif /* LARGE_FILE */
535 size_t validsize;
Subrata Modak76a720a2010-07-03 21:08:18 +0530536 caddr_t paddr;
robbiewe093b822003-01-10 18:24:49 +0000537 int pagesize = sysconf(_SC_PAGE_SIZE);
538 unsigned randpage;
539 unsigned int seed;
540 unsigned loopcnt;
541 unsigned nloops;
subrata_modak4bb656a2009-02-26 12:02:09 +0000542 unsigned mappages;
robbiewe093b822003-01-10 18:24:49 +0000543 unsigned mapflags;
544 unsigned i;
Subrata Modak76a720a2010-07-03 21:08:18 +0530545 struct sigaction sa_mapper;
robbiewe093b822003-01-10 18:24:49 +0000546
547 mapflags = MAP_SHARED;
548
Wanlong Gao354ebb42012-12-07 10:10:04 +0800549 seed = initrand(); /* initialize random seed */
robbiewe093b822003-01-10 18:24:49 +0000550
Subrata Modak76a720a2010-07-03 21:08:18 +0530551 sa_mapper.sa_handler = clean_mapper;
552 sa_mapper.sa_flags = 0;
553 if (sigemptyset(&sa_mapper.sa_mask)) {
554 perror("sigempty error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800555 anyfail();
Subrata Modak76a720a2010-07-03 21:08:18 +0530556 }
557
558 if (sigaction(SIGUSR1, &sa_mapper, 0) == -1) {
559 perror("sigaction error SIGUSR1");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800560 anyfail();
Subrata Modak76a720a2010-07-03 21:08:18 +0530561 }
robbiewe093b822003-01-10 18:24:49 +0000562#ifdef LARGE_FILE
Subrata Modak76a720a2010-07-03 21:08:18 +0530563 if ((fd_mapper = open64(file, O_RDWR)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000564#else /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530565 if ((fd_mapper = open(file, O_RDWR)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000566#endif /* LARGE_FILE */
567 perror("open error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800568 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000569 }
robbiewe093b822003-01-10 18:24:49 +0000570#ifdef LARGE_FILE
Subrata Modak76a720a2010-07-03 21:08:18 +0530571 if (fstat64(fd_mapper, &statbuf) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000572#else /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530573 if (fstat(fd_mapper, &statbuf) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000574#endif /* LARGE_FILE */
575 perror("stat error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800576 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000577 }
578 filesize = statbuf.st_size;
579
580 if (statbuf.st_size - sparseoffset > SIZE_MAX) {
581 fprintf(stderr, "size_t overflow when setting up map\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800582 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000583 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800584 mapsize_mapper = (size_t) (statbuf.st_size - sparseoffset);
Subrata Modak76a720a2010-07-03 21:08:18 +0530585 mappages = roundup(mapsize_mapper, pagesize) / pagesize;
robbiewe093b822003-01-10 18:24:49 +0000586 offset = sparseoffset;
587 if (do_offset) {
588 int pageoffset = lrand48() % mappages;
589 int byteoffset = pageoffset * pagesize;
590 offset += byteoffset;
Subrata Modak76a720a2010-07-03 21:08:18 +0530591 mapsize_mapper -= byteoffset;
robbiewe093b822003-01-10 18:24:49 +0000592 mappages -= pageoffset;
593 }
robbiewe093b822003-01-10 18:24:49 +0000594#ifdef LARGE_FILE
Wanlong Gao354ebb42012-12-07 10:10:04 +0800595 if ((maddr_mapper = mmap64(0, mapsize_mapper, PROT_READ | PROT_WRITE,
596 mapflags, fd_mapper,
597 offset)) == (caddr_t) - 1) {
robbiewe093b822003-01-10 18:24:49 +0000598#else /* LARGE_FILE */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800599 if ((maddr_mapper = mmap(0, mapsize_mapper, PROT_READ | PROT_WRITE,
600 mapflags, fd_mapper,
601 offset)) == (caddr_t) - 1) {
robbiewe093b822003-01-10 18:24:49 +0000602#endif /* LARGE_FILE */
603 perror("mmap error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800604 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000605 }
606
Subrata Modak76a720a2010-07-03 21:08:18 +0530607 (void)close(fd_mapper);
robbiewe093b822003-01-10 18:24:49 +0000608
609 nloops = (randloops) ? (lrand48() % MAXLOOPS) : MAXLOOPS;
610
611 if (debug) {
612#ifdef LARGE_FILE
613 (void)printf("child %d (pid %ld): seed %d, fsize %Ld, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800614 "mapsize %d, off %Ld, loop %d\n",
615 procno, getpid(), seed, filesize, mapsize_mapper,
616 offset / pagesize, nloops);
robbiewe093b822003-01-10 18:24:49 +0000617#else /* LARGE_FILE */
618 (void)printf("child %d (pid %d): seed %d, fsize %ld, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800619 "mapsize %ld, off %ld, loop %d\n",
620 procno, getpid(), seed, filesize,
621 (long)mapsize_mapper, offset / pagesize, nloops);
robbiewe093b822003-01-10 18:24:49 +0000622#endif /* LARGE_FILE */
623 }
624
625 /*
626 * Now loop read/writing random pages.
627 */
628 for (loopcnt = 0; loopcnt < nloops; loopcnt++) {
629 randpage = lrand48() % mappages;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800630 paddr = maddr_mapper + (randpage * pagesize); /* page address */
robbiewe093b822003-01-10 18:24:49 +0000631
Wanlong Gao354ebb42012-12-07 10:10:04 +0800632 if (randpage < mappages - 1 || !(mapsize_mapper % pagesize))
robbiewe093b822003-01-10 18:24:49 +0000633 validsize = pagesize;
634 else
Subrata Modak76a720a2010-07-03 21:08:18 +0530635 validsize = mapsize_mapper % pagesize;
robbiewe093b822003-01-10 18:24:49 +0000636
637 /*
638 * Because one child is mapping file in extend mode,
639 * it may be padded with zeros at end. So we can't
640 * do an exact check -- accept known pattern OR zeros.
641 */
642 for (i = procno; i < validsize; i += nprocs) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800643 if (*((unsigned char *)(paddr + i))
robbiewe093b822003-01-10 18:24:49 +0000644 != ((procno + pattern) & 0xff)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800645 && *((unsigned char *)(paddr + i)) != 0) {
robbiewe093b822003-01-10 18:24:49 +0000646 (void)fprintf(stderr, "child %d: invalid data "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800647 "<x%x>", procno,
648 *((unsigned char *)(paddr + i)));
649 (void)fprintf(stderr,
650 " at pg %d off %d, exp "
651 "<x%x>\n", randpage, i,
652 (procno + pattern) & 0xff);
653 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000654 }
655 /*
656 * Now write it.
657 */
658
Wanlong Gao354ebb42012-12-07 10:10:04 +0800659 *(paddr + i) = (procno + pattern) & 0xff;
robbiewe093b822003-01-10 18:24:49 +0000660 }
661 }
662 if (dosync) {
663 /*
664 * Exercise msync() as well!
665 */
666 randpage = lrand48() % mappages;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800667 paddr = maddr_mapper + (randpage * pagesize); /* page address */
668 if (msync(paddr, (mappages - randpage) * pagesize,
669 MS_SYNC) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000670 perror("msync error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800671 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000672 }
673 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800674 if (munmap(maddr_mapper, mapsize_mapper) == -1) {
Subrata Modak76a720a2010-07-03 21:08:18 +0530675 perror("munmap failed");
676 anyfail();
677 }
robbiewe093b822003-01-10 18:24:49 +0000678 exit(0);
679}
680
681/*
682 * child_writer
683 * The child process that continually (and slowly!!) grows
684 * the file. The purpose of this is to exercise the code
subrata_modak4bb656a2009-02-26 12:02:09 +0000685 * supporting mapping of fragments. The map children are
robbiewe093b822003-01-10 18:24:49 +0000686 * constantly reforking and will pick up the map changes, etc.
687 * This process executes until signalled (i.e. has no exit!)
subrata_modakbdbaec52009-02-26 12:14:51 +0000688 * unless error.
robbiewe093b822003-01-10 18:24:49 +0000689 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800690void child_writer(char *file, uchar_t * buf)
691{ /* buf already set up in main */
Subrata Modak76a720a2010-07-03 21:08:18 +0530692 struct sigaction sa_writer;
693
694 sa_writer.sa_handler = clean_writer;
695 sa_writer.sa_flags = 0;
696 if (sigemptyset(&sa_writer.sa_mask)) {
697 perror("sigempty error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800698 anyfail();
Subrata Modak76a720a2010-07-03 21:08:18 +0530699 }
700
701 if (sigaction(SIGUSR1, &sa_writer, 0) == -1) {
702 perror("sigaction error SIGUSR1");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800703 anyfail();
Subrata Modak76a720a2010-07-03 21:08:18 +0530704 }
robbiewe093b822003-01-10 18:24:49 +0000705#ifdef LARGE_FILE
706 struct stat64 statbuf;
707 off64_t off;
708#else /* LARGE_FILE */
709 struct stat statbuf;
710 off_t off;
711#endif /* LARGE_FILE */
712 int pagesize = sysconf(_SC_PAGE_SIZE);
713 uchar_t *p;
714 int cnt;
715
716#ifdef LARGE_FILE
Subrata Modak76a720a2010-07-03 21:08:18 +0530717 if ((fd_writer = open64(file, O_RDWR)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000718#else /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530719 if ((fd_writer = open(file, O_RDWR)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000720#endif /* LARGE_FILE */
721 perror("open error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800722 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000723 }
robbiewe093b822003-01-10 18:24:49 +0000724#ifdef LARGE_FILE
Subrata Modak76a720a2010-07-03 21:08:18 +0530725 if ((off = lseek64(fd_writer, 0, SEEK_END)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000726#else /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530727 if ((off = lseek(fd_writer, 0, SEEK_END)) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000728#endif /* LARGE_FILE */
729 perror("lseek error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800730 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000731 }
732
robbiewe093b822003-01-10 18:24:49 +0000733 for (;;) {
734#ifdef LARGE_FILE
Subrata Modak76a720a2010-07-03 21:08:18 +0530735 if (fstat64(fd_writer, &statbuf) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000736#else /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530737 if (fstat(fd_writer, &statbuf) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000738#endif /* LARGE_FILE */
739 perror("fstat error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800740 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000741 }
742#ifdef LARGE_FILE
743 if (debug)
subrata_modak4bb656a2009-02-26 12:02:09 +0000744 (void)printf("writer %d bytes at off %Ld, size %Ld\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800745 growsize, off, statbuf.st_size);
robbiewe093b822003-01-10 18:24:49 +0000746#else /* LARGE_FILE */
747 if (debug)
subrata_modak4bb656a2009-02-26 12:02:09 +0000748 (void)printf("writer %d bytes at off %ld, size %ld\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800749 growsize, off, statbuf.st_size);
robbiewe093b822003-01-10 18:24:49 +0000750#endif /* LARGE_FILE */
751
752 /*
753 * Write some number of bytes, then sleep some
754 * number of seconds...
755 * Need to keep track of our offset so write the
756 * right bytes.
757 */
758
759 p = buf + (off % pagesize);
760
Subrata Modak76a720a2010-07-03 21:08:18 +0530761 if ((cnt = write(fd_writer, p, growsize)) != growsize) {
robbiewe093b822003-01-10 18:24:49 +0000762 if (cnt == -1)
763 perror("write error");
764 else
765 (void)fprintf(stderr, "wrote %d of %d bytes\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800766 cnt, growsize);
767 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000768 }
769
770 off += growsize;
771
772 (void)sleep(sleeptime);
773 if (dosync) {
Subrata Modak76a720a2010-07-03 21:08:18 +0530774 if (fsync(fd_writer) == -1) {
robbiewe093b822003-01-10 18:24:49 +0000775 perror("fsync error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800776 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000777 }
778 }
779 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530780 close(fd_writer);
robbiewe093b822003-01-10 18:24:49 +0000781}
782
robbiewe093b822003-01-10 18:24:49 +0000783/*
784 * Make sure file has all the correct data.
785
786 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800787int fileokay(char *file, uchar_t * expbuf)
robbiewe093b822003-01-10 18:24:49 +0000788{
789#ifdef LARGE_FILE
790 struct stat64 statbuf;
791#else /* LARGE_FILE */
792 struct stat statbuf;
793#endif /* LARGE_FILE */
794 size_t mapsize;
795 uchar_t *readbuf;
796 unsigned mappages;
797 unsigned pagesize = sysconf(_SC_PAGE_SIZE);
798 int fd;
799 int cnt;
800 unsigned i, j;
801
802#ifdef LARGE_FILE
803 if ((fd = open64(file, O_RDONLY)) == -1) {
804#else /* LARGE_FILE */
805 if ((fd = open(file, O_RDONLY)) == -1) {
806#endif /* LARGE_FILE */
807 perror("open error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800808 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000809 }
810#ifdef LARGE_FILE
811 if (fstat64(fd, &statbuf) == -1) {
812#else /* LARGE_FILE */
813 if (fstat(fd, &statbuf) == -1) {
814#endif /* LARGE_FILE */
815 perror("stat error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800816 anyfail();
robbiewe093b822003-01-10 18:24:49 +0000817 }
818#ifdef LARGE_FILE
819 if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
820#else /* LARGE_FILE */
821 if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
822#endif /* LARGE_FILE */
823 perror("lseek");
824 exit(1);
825 }
826
Cyril Hrubisd218f342014-09-23 13:14:56 +0200827 readbuf = malloc(pagesize);
robbiewe093b822003-01-10 18:24:49 +0000828
829 if (statbuf.st_size - sparseoffset > SIZE_MAX) {
830 fprintf(stderr, "size_t overflow when setting up map\n");
831 exit(1);
832 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800833 mapsize = (size_t) (statbuf.st_size - sparseoffset);
robbiewe093b822003-01-10 18:24:49 +0000834 mappages = roundup(mapsize, pagesize) / pagesize;
835
836 for (i = 0; i < mappages; i++) {
837 cnt = read(fd, (char *)readbuf, pagesize);
838 if (cnt == -1) {
839 perror("read error");
Subrata Modak76a720a2010-07-03 21:08:18 +0530840 close(fd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000841 return 0;
robbiewe093b822003-01-10 18:24:49 +0000842 } else if (cnt != pagesize) {
843 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000844 * Okay if at last page in file...
robbiewe093b822003-01-10 18:24:49 +0000845 */
846 if ((i * pagesize) + cnt != mapsize) {
robbiewd764fa22003-04-03 21:59:03 +0000847 (void)fprintf(stderr, "read %d of %ld bytes\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800848 (i * pagesize) + cnt,
849 (long)mapsize);
Subrata Modak76a720a2010-07-03 21:08:18 +0530850 close(fd);
subrata_modak43337a32009-02-26 11:43:51 +0000851 return 0;
robbiewe093b822003-01-10 18:24:49 +0000852 }
853 }
854 /*
855 * Compare read bytes of data.
856 * May have zeros from map extend...
857 */
858 for (j = 0; j < cnt; j++) {
859 if (expbuf[j] != readbuf[j] && readbuf[j] != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000860 (void)fprintf(stderr,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800861 "read bad data: exp %c got %c",
862 expbuf[j], readbuf[j]);
robbiewe093b822003-01-10 18:24:49 +0000863#ifdef LARGE_FILE
864 (void)fprintf(stderr, ", pg %d off %d, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800865 "(fsize %Ld)\n", i, j,
866 statbuf.st_size);
robbiewe093b822003-01-10 18:24:49 +0000867#else /* LARGE_FILE */
868 (void)fprintf(stderr, ", pg %d off %d, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800869 "(fsize %ld)\n", i, j,
870 statbuf.st_size);
robbiewe093b822003-01-10 18:24:49 +0000871#endif /* LARGE_FILE */
Subrata Modak76a720a2010-07-03 21:08:18 +0530872 close(fd);
subrata_modak43337a32009-02-26 11:43:51 +0000873 return 0;
robbiewe093b822003-01-10 18:24:49 +0000874 }
875 }
876 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800877
Shuang Qiu29d35ae2013-05-28 11:42:08 +0800878 close(fd);
subrata_modak134e8962009-02-26 11:46:54 +0000879 return 1;
robbiewe093b822003-01-10 18:24:49 +0000880}
881
Wanlong Gao354ebb42012-12-07 10:10:04 +0800882 /*ARGSUSED*/ void finish(int sig)
robbiewe093b822003-01-10 18:24:49 +0000883{
884 finished++;
885 /* finish nicely and check the file contents */
886}
887
Wanlong Gao354ebb42012-12-07 10:10:04 +0800888 /*ARGSUSED*/ void clean_up_file(int sig)
robbiewe093b822003-01-10 18:24:49 +0000889{
890 if (!leavefile)
891 (void)unlink(filename);
892 exit(1);
893}
894
Subrata Modak76a720a2010-07-03 21:08:18 +0530895void clean_mapper(int sig)
896{
897 if (fd_mapper)
898 close(fd_mapper);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800899 munmap(maddr_mapper, mapsize_mapper);
900 exit(0);
Subrata Modak76a720a2010-07-03 21:08:18 +0530901}
902
903void clean_writer(int sig)
904{
905 if (fd_writer)
906 close(fd_writer);
907 exit(0);
908}
909
Wanlong Gao354ebb42012-12-07 10:10:04 +0800910unsigned int initrand(void)
robbiewe093b822003-01-10 18:24:49 +0000911{
912 unsigned int seed;
913
914 /*
915 * Initialize random seed... Got this from a test written
916 * by scooter:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800917 * Use srand/rand to diffuse the information from the
918 * time and pid. If you start several processes, then
919 * the time and pid information don't provide much
920 * variation.
robbiewe093b822003-01-10 18:24:49 +0000921 */
922 srand((unsigned int)getpid());
923 seed = rand();
Cyril Hrubis4e2bab82014-09-24 16:34:35 +0200924 srand((unsigned int)time(NULL));
robbiewe093b822003-01-10 18:24:49 +0000925 seed = (seed ^ rand()) % 100000;
926 srand48((long int)seed);
927 return (seed);
928}
929
robbiewe093b822003-01-10 18:24:49 +0000930/***** LTP Port *****/
931void ok_exit()
932{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800933 tst_resm(TPASS, "Test passed\n");
robbiewe093b822003-01-10 18:24:49 +0000934 tst_rmdir();
935 tst_exit();
936}
937
robbiewe093b822003-01-10 18:24:49 +0000938int anyfail()
939{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100940 tst_brkm(TFAIL, tst_rmdir, "Test failed\n");
robbiewe093b822003-01-10 18:24:49 +0000941}
942
Chris Dearmanec6edca2012-10-17 19:54:01 -0700943/***** ** ** *****/