1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2003
4*49cdfc7eSAndroid Build Coastguard Worker * 01/02/2003 Port to LTP [email protected]
5*49cdfc7eSAndroid Build Coastguard Worker * 06/30/2001 Port to Linux [email protected]
6*49cdfc7eSAndroid Build Coastguard Worker * 10/03/2022 Refactor to LTP framework [email protected]
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker * This test stresses mmaps, without dealing with fragments or anything!
11*49cdfc7eSAndroid Build Coastguard Worker * It forks a specified number of children,
12*49cdfc7eSAndroid Build Coastguard Worker * all of whom mmap the same file, make a given number of accesses
13*49cdfc7eSAndroid Build Coastguard Worker * to random pages in the map (reading & writing and comparing data).
14*49cdfc7eSAndroid Build Coastguard Worker * Then the child exits and the parent forks another to take its place.
15*49cdfc7eSAndroid Build Coastguard Worker * Each time a child is forked, it stats the file and maps the full
16*49cdfc7eSAndroid Build Coastguard Worker * length of the file.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * This program continues to run until it either receives a SIGINT,
19*49cdfc7eSAndroid Build Coastguard Worker * or times out (if a timeout value is specified). When either of
20*49cdfc7eSAndroid Build Coastguard Worker * these things happens, it cleans up its kids, then checks the
21*49cdfc7eSAndroid Build Coastguard Worker * file to make sure it has the correct data.
22*49cdfc7eSAndroid Build Coastguard Worker */
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <float.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker #if _FILE_OFFSET_BITS == 64
40*49cdfc7eSAndroid Build Coastguard Worker # define FSIZE_MIN LONG_MIN
41*49cdfc7eSAndroid Build Coastguard Worker # define FSIZE_MAX LONG_MAX
42*49cdfc7eSAndroid Build Coastguard Worker #else
43*49cdfc7eSAndroid Build Coastguard Worker # define FSIZE_MIN INT_MIN
44*49cdfc7eSAndroid Build Coastguard Worker # define FSIZE_MAX INT_MAX
45*49cdfc7eSAndroid Build Coastguard Worker #endif
46*49cdfc7eSAndroid Build Coastguard Worker #define MAXLOOPS 500 /* max pages for map children to write */
47*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "mmapstress01.out"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #ifdef roundup
50*49cdfc7eSAndroid Build Coastguard Worker #undef roundup
51*49cdfc7eSAndroid Build Coastguard Worker #endif
52*49cdfc7eSAndroid Build Coastguard Worker #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker static unsigned int initrand(void);
55*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int);
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static char *debug;
58*49cdfc7eSAndroid Build Coastguard Worker static char *do_sync;
59*49cdfc7eSAndroid Build Coastguard Worker static char *do_offset;
60*49cdfc7eSAndroid Build Coastguard Worker static char *opt_filesize;
61*49cdfc7eSAndroid Build Coastguard Worker static char *opt_nprocs;
62*49cdfc7eSAndroid Build Coastguard Worker static char *opt_pattern;
63*49cdfc7eSAndroid Build Coastguard Worker static char *opt_sparseoffset;
64*49cdfc7eSAndroid Build Coastguard Worker static char *randloops;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker static int fd;
67*49cdfc7eSAndroid Build Coastguard Worker static volatile int finished;
68*49cdfc7eSAndroid Build Coastguard Worker static int nprocs;
69*49cdfc7eSAndroid Build Coastguard Worker static long long filesize = 4096;
70*49cdfc7eSAndroid Build Coastguard Worker static long long sparseoffset;
71*49cdfc7eSAndroid Build Coastguard Worker static size_t pagesize;
72*49cdfc7eSAndroid Build Coastguard Worker static int pattern;
73*49cdfc7eSAndroid Build Coastguard Worker
setup(void)74*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker struct sigaction sa;
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker sa.sa_handler = sighandler;
79*49cdfc7eSAndroid Build Coastguard Worker sa.sa_flags = 0;
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGEMPTYSET(&sa.sa_mask);
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(SIGINT, &sa, 0);
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(SIGQUIT, &sa, 0);
83*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(SIGTERM, &sa, 0);
84*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(SIGALRM, &sa, 0);
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker pagesize = sysconf(_SC_PAGE_SIZE);
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_filesize(opt_filesize, &filesize, 0, FSIZE_MAX))
89*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "invalid initial filesize '%s'", opt_filesize);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_filesize(opt_sparseoffset, &sparseoffset, FSIZE_MIN, FSIZE_MAX))
92*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "invalid sparse offset '%s'", opt_sparseoffset);
93*49cdfc7eSAndroid Build Coastguard Worker if (sparseoffset % pagesize != 0)
94*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "sparseoffset must be pagesize multiple");
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_int(opt_nprocs, &nprocs, 0, 255))
97*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "invalid number of mapping children '%s'",
98*49cdfc7eSAndroid Build Coastguard Worker opt_nprocs);
99*49cdfc7eSAndroid Build Coastguard Worker if (!opt_nprocs)
100*49cdfc7eSAndroid Build Coastguard Worker nprocs = MAX(MIN(tst_ncpus() - 1L, 20L), 1L);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_int(opt_pattern, &pattern, 0, 255))
103*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "invalid pattern '%s'", opt_pattern);
104*49cdfc7eSAndroid Build Coastguard Worker if (!opt_pattern)
105*49cdfc7eSAndroid Build Coastguard Worker pattern = initrand() & 0xff;
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "creating file <%s> with %lld bytes, pattern %d",
108*49cdfc7eSAndroid Build Coastguard Worker TEST_FILE, filesize, pattern);
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)111*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
114*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker /*
118*49cdfc7eSAndroid Build Coastguard Worker * Child process that reads/writes map. The child stats the file
119*49cdfc7eSAndroid Build Coastguard Worker * to determine the size, maps the size of the file, then reads/writes
120*49cdfc7eSAndroid Build Coastguard Worker * its own locations on random pages of the map (its locations being
121*49cdfc7eSAndroid Build Coastguard Worker * determined based on nprocs & procno). After a specific number of
122*49cdfc7eSAndroid Build Coastguard Worker * iterations, it exits.
123*49cdfc7eSAndroid Build Coastguard Worker */
child_mapper(char * file,unsigned int procno,unsigned int nprocs)124*49cdfc7eSAndroid Build Coastguard Worker static void child_mapper(char *file, unsigned int procno, unsigned int nprocs)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker struct stat statbuf;
127*49cdfc7eSAndroid Build Coastguard Worker off_t filesize;
128*49cdfc7eSAndroid Build Coastguard Worker off_t offset;
129*49cdfc7eSAndroid Build Coastguard Worker size_t validsize;
130*49cdfc7eSAndroid Build Coastguard Worker size_t mapsize;
131*49cdfc7eSAndroid Build Coastguard Worker char *maddr = NULL, *paddr;
132*49cdfc7eSAndroid Build Coastguard Worker unsigned int randpage;
133*49cdfc7eSAndroid Build Coastguard Worker unsigned int seed;
134*49cdfc7eSAndroid Build Coastguard Worker unsigned int loopcnt;
135*49cdfc7eSAndroid Build Coastguard Worker unsigned int nloops;
136*49cdfc7eSAndroid Build Coastguard Worker unsigned int mappages;
137*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker seed = initrand();
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(file, &statbuf);
142*49cdfc7eSAndroid Build Coastguard Worker filesize = statbuf.st_size;
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(file, O_RDWR);
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker if (statbuf.st_size - sparseoffset > UINT_MAX)
147*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "size_t overflow when setting up map");
148*49cdfc7eSAndroid Build Coastguard Worker mapsize = (size_t) (statbuf.st_size - sparseoffset);
149*49cdfc7eSAndroid Build Coastguard Worker mappages = roundup(mapsize, pagesize) / pagesize;
150*49cdfc7eSAndroid Build Coastguard Worker offset = sparseoffset;
151*49cdfc7eSAndroid Build Coastguard Worker if (do_offset) {
152*49cdfc7eSAndroid Build Coastguard Worker int pageoffset = lrand48() % mappages;
153*49cdfc7eSAndroid Build Coastguard Worker int byteoffset = pageoffset * pagesize;
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker offset += byteoffset;
156*49cdfc7eSAndroid Build Coastguard Worker mapsize -= byteoffset;
157*49cdfc7eSAndroid Build Coastguard Worker mappages -= pageoffset;
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker nloops = (randloops) ? (lrand48() % MAXLOOPS) : MAXLOOPS;
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker if (debug)
162*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "child %d (pid %d): seed %d, fsize %lld, mapsize %ld, off %lld, loop %d",
163*49cdfc7eSAndroid Build Coastguard Worker procno, getpid(), seed, (long long)filesize,
164*49cdfc7eSAndroid Build Coastguard Worker (long)mapsize, (long long)offset / pagesize, nloops);
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker maddr = SAFE_MMAP(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
167*49cdfc7eSAndroid Build Coastguard Worker offset);
168*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker for (loopcnt = 0; loopcnt < nloops; loopcnt++) {
171*49cdfc7eSAndroid Build Coastguard Worker randpage = lrand48() % mappages;
172*49cdfc7eSAndroid Build Coastguard Worker paddr = maddr + (randpage * pagesize); /* page address */
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker if (randpage < mappages - 1 || !(mapsize % pagesize))
175*49cdfc7eSAndroid Build Coastguard Worker validsize = pagesize;
176*49cdfc7eSAndroid Build Coastguard Worker else
177*49cdfc7eSAndroid Build Coastguard Worker validsize = mapsize % pagesize;
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker for (i = procno; i < validsize; i += nprocs) {
180*49cdfc7eSAndroid Build Coastguard Worker if (*((unsigned char *)(paddr + i))
181*49cdfc7eSAndroid Build Coastguard Worker != ((procno + pattern) & 0xff))
182*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL, "child %d: invalid data <x%x>\n"
183*49cdfc7eSAndroid Build Coastguard Worker " at pg %d off %d, exp <x%x>", procno,
184*49cdfc7eSAndroid Build Coastguard Worker *((unsigned char *)(paddr + i)),
185*49cdfc7eSAndroid Build Coastguard Worker randpage, i, (procno + pattern) & 0xff);
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker *(paddr + i) = (procno + pattern) & 0xff;
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Worker if (do_sync) {
192*49cdfc7eSAndroid Build Coastguard Worker randpage = lrand48() % mappages;
193*49cdfc7eSAndroid Build Coastguard Worker paddr = maddr + (randpage * pagesize); /* page address */
194*49cdfc7eSAndroid Build Coastguard Worker SAFE_MSYNC(paddr, (mappages - randpage) * pagesize, MS_SYNC);
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(maddr, mapsize);
198*49cdfc7eSAndroid Build Coastguard Worker exit(0);
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker /* Make sure file has all the correct data. */
fileokay(char * file,unsigned char * expbuf)202*49cdfc7eSAndroid Build Coastguard Worker static void fileokay(char *file, unsigned char *expbuf)
203*49cdfc7eSAndroid Build Coastguard Worker {
204*49cdfc7eSAndroid Build Coastguard Worker int cnt;
205*49cdfc7eSAndroid Build Coastguard Worker size_t mapsize;
206*49cdfc7eSAndroid Build Coastguard Worker struct stat statbuf;
207*49cdfc7eSAndroid Build Coastguard Worker unsigned char readbuf[pagesize];
208*49cdfc7eSAndroid Build Coastguard Worker unsigned int i, j;
209*49cdfc7eSAndroid Build Coastguard Worker unsigned int mappages;
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(file, O_RDONLY);
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(fd, &statbuf);
214*49cdfc7eSAndroid Build Coastguard Worker SAFE_LSEEK(fd, sparseoffset, SEEK_SET);
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker if (statbuf.st_size - sparseoffset > UINT_MAX)
217*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "size_t overflow when setting up map");
218*49cdfc7eSAndroid Build Coastguard Worker mapsize = (size_t) (statbuf.st_size - sparseoffset);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker mappages = roundup(mapsize, pagesize) / pagesize;
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < mappages; i++) {
223*49cdfc7eSAndroid Build Coastguard Worker cnt = SAFE_READ(0, fd, readbuf, pagesize);
224*49cdfc7eSAndroid Build Coastguard Worker if ((unsigned int)cnt != pagesize) {
225*49cdfc7eSAndroid Build Coastguard Worker /* Okay if at last page in file... */
226*49cdfc7eSAndroid Build Coastguard Worker if ((i * pagesize) + cnt != mapsize)
227*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL, "missing data: read %lu of %ld bytes",
228*49cdfc7eSAndroid Build Coastguard Worker (i * pagesize) + cnt, (long)mapsize);
229*49cdfc7eSAndroid Build Coastguard Worker }
230*49cdfc7eSAndroid Build Coastguard Worker /* Compare read bytes of data. */
231*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < (unsigned int)cnt; j++) {
232*49cdfc7eSAndroid Build Coastguard Worker if (expbuf[j] != readbuf[j])
233*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL,
234*49cdfc7eSAndroid Build Coastguard Worker "read bad data: exp %c got %c, pg %d off %d, (fsize %lld)",
235*49cdfc7eSAndroid Build Coastguard Worker expbuf[j], readbuf[j], i, j,
236*49cdfc7eSAndroid Build Coastguard Worker (long long)statbuf.st_size);
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker }
239*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
240*49cdfc7eSAndroid Build Coastguard Worker }
241*49cdfc7eSAndroid Build Coastguard Worker
sighandler(int sig LTP_ATTRIBUTE_UNUSED)242*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
243*49cdfc7eSAndroid Build Coastguard Worker {
244*49cdfc7eSAndroid Build Coastguard Worker finished++;
245*49cdfc7eSAndroid Build Coastguard Worker }
246*49cdfc7eSAndroid Build Coastguard Worker
initrand(void)247*49cdfc7eSAndroid Build Coastguard Worker static unsigned int initrand(void)
248*49cdfc7eSAndroid Build Coastguard Worker {
249*49cdfc7eSAndroid Build Coastguard Worker unsigned int seed;
250*49cdfc7eSAndroid Build Coastguard Worker
251*49cdfc7eSAndroid Build Coastguard Worker /*
252*49cdfc7eSAndroid Build Coastguard Worker * Use srand/rand to diffuse the information from the
253*49cdfc7eSAndroid Build Coastguard Worker * time and pid. If you start several processes, then
254*49cdfc7eSAndroid Build Coastguard Worker * the time and pid information don't provide much
255*49cdfc7eSAndroid Build Coastguard Worker * variation.
256*49cdfc7eSAndroid Build Coastguard Worker */
257*49cdfc7eSAndroid Build Coastguard Worker srand((unsigned int)getpid());
258*49cdfc7eSAndroid Build Coastguard Worker seed = rand();
259*49cdfc7eSAndroid Build Coastguard Worker srand((unsigned int)time(NULL));
260*49cdfc7eSAndroid Build Coastguard Worker seed = (seed ^ rand()) % 100000;
261*49cdfc7eSAndroid Build Coastguard Worker srand48((long)seed);
262*49cdfc7eSAndroid Build Coastguard Worker return seed;
263*49cdfc7eSAndroid Build Coastguard Worker }
264*49cdfc7eSAndroid Build Coastguard Worker
run(void)265*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
266*49cdfc7eSAndroid Build Coastguard Worker {
267*49cdfc7eSAndroid Build Coastguard Worker int c;
268*49cdfc7eSAndroid Build Coastguard Worker int i;
269*49cdfc7eSAndroid Build Coastguard Worker int wait_stat;
270*49cdfc7eSAndroid Build Coastguard Worker off_t bytes_left;
271*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
272*49cdfc7eSAndroid Build Coastguard Worker pid_t *pidarray;
273*49cdfc7eSAndroid Build Coastguard Worker size_t write_cnt;
274*49cdfc7eSAndroid Build Coastguard Worker unsigned char data;
275*49cdfc7eSAndroid Build Coastguard Worker unsigned char *buf;
276*49cdfc7eSAndroid Build Coastguard Worker
277*49cdfc7eSAndroid Build Coastguard Worker alarm(tst_remaining_runtime());
278*49cdfc7eSAndroid Build Coastguard Worker
279*49cdfc7eSAndroid Build Coastguard Worker finished = 0;
280*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, 0664);
281*49cdfc7eSAndroid Build Coastguard Worker buf = SAFE_MALLOC(pagesize);
282*49cdfc7eSAndroid Build Coastguard Worker pidarray = SAFE_MALLOC(nprocs * sizeof(pid_t));
283*49cdfc7eSAndroid Build Coastguard Worker
284*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nprocs; i++)
285*49cdfc7eSAndroid Build Coastguard Worker *(pidarray + i) = 0;
286*49cdfc7eSAndroid Build Coastguard Worker
287*49cdfc7eSAndroid Build Coastguard Worker for (i = 0, data = 0; i < (int)pagesize; i++) {
288*49cdfc7eSAndroid Build Coastguard Worker *(buf + i) = (data + pattern) & 0xff;
289*49cdfc7eSAndroid Build Coastguard Worker if (++data == nprocs)
290*49cdfc7eSAndroid Build Coastguard Worker data = 0;
291*49cdfc7eSAndroid Build Coastguard Worker }
292*49cdfc7eSAndroid Build Coastguard Worker SAFE_LSEEK(fd, (off_t)sparseoffset, SEEK_SET);
293*49cdfc7eSAndroid Build Coastguard Worker for (bytes_left = filesize; bytes_left; bytes_left -= c) {
294*49cdfc7eSAndroid Build Coastguard Worker write_cnt = MIN((long long)pagesize, (long long)bytes_left);
295*49cdfc7eSAndroid Build Coastguard Worker c = SAFE_WRITE(1, fd, buf, write_cnt);
296*49cdfc7eSAndroid Build Coastguard Worker }
297*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
298*49cdfc7eSAndroid Build Coastguard Worker
299*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nprocs; i++) {
300*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
301*49cdfc7eSAndroid Build Coastguard Worker
302*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
303*49cdfc7eSAndroid Build Coastguard Worker child_mapper(TEST_FILE, (unsigned int)i, (unsigned int)nprocs);
304*49cdfc7eSAndroid Build Coastguard Worker exit(0);
305*49cdfc7eSAndroid Build Coastguard Worker } else {
306*49cdfc7eSAndroid Build Coastguard Worker pidarray[i] = pid;
307*49cdfc7eSAndroid Build Coastguard Worker }
308*49cdfc7eSAndroid Build Coastguard Worker }
309*49cdfc7eSAndroid Build Coastguard Worker
310*49cdfc7eSAndroid Build Coastguard Worker while (!finished) {
311*49cdfc7eSAndroid Build Coastguard Worker pid = wait(&wait_stat);
312*49cdfc7eSAndroid Build Coastguard Worker if (pid != -1) {
313*49cdfc7eSAndroid Build Coastguard Worker if (!WIFEXITED(wait_stat)
314*49cdfc7eSAndroid Build Coastguard Worker || WEXITSTATUS(wait_stat) != 0)
315*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "child exit with err <x%x>",
316*49cdfc7eSAndroid Build Coastguard Worker wait_stat);
317*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nprocs; i++)
318*49cdfc7eSAndroid Build Coastguard Worker if (pid == pidarray[i])
319*49cdfc7eSAndroid Build Coastguard Worker break;
320*49cdfc7eSAndroid Build Coastguard Worker if (i == nprocs)
321*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "unknown child pid %d, <x%x>",
322*49cdfc7eSAndroid Build Coastguard Worker pid, wait_stat);
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
325*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
326*49cdfc7eSAndroid Build Coastguard Worker child_mapper(TEST_FILE, (unsigned int)i, (unsigned int)nprocs);
327*49cdfc7eSAndroid Build Coastguard Worker exit(0);
328*49cdfc7eSAndroid Build Coastguard Worker } else {
329*49cdfc7eSAndroid Build Coastguard Worker pidarray[i] = pid;
330*49cdfc7eSAndroid Build Coastguard Worker }
331*49cdfc7eSAndroid Build Coastguard Worker } else {
332*49cdfc7eSAndroid Build Coastguard Worker if (errno != EINTR || !finished)
333*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
334*49cdfc7eSAndroid Build Coastguard Worker "unexpected wait error");
335*49cdfc7eSAndroid Build Coastguard Worker }
336*49cdfc7eSAndroid Build Coastguard Worker }
337*49cdfc7eSAndroid Build Coastguard Worker alarm(0);
338*49cdfc7eSAndroid Build Coastguard Worker
339*49cdfc7eSAndroid Build Coastguard Worker fileokay(TEST_FILE, buf);
340*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "file has expected data");
341*49cdfc7eSAndroid Build Coastguard Worker }
342*49cdfc7eSAndroid Build Coastguard Worker
343*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
344*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
345*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
346*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
347*49cdfc7eSAndroid Build Coastguard Worker {"d", &debug, "Enable debug output"},
348*49cdfc7eSAndroid Build Coastguard Worker {"f:", &opt_filesize, "Initial filesize (default 4096)"},
349*49cdfc7eSAndroid Build Coastguard Worker {"m", &do_sync, "Do random msync/fsyncs as well"},
350*49cdfc7eSAndroid Build Coastguard Worker {"o", &do_offset, "Randomize the offset of file to map"},
351*49cdfc7eSAndroid Build Coastguard Worker {"p:", &opt_nprocs,
352*49cdfc7eSAndroid Build Coastguard Worker "Number of mapping children to create (default 1 < ncpus < 20)"},
353*49cdfc7eSAndroid Build Coastguard Worker {"P:", &opt_pattern,
354*49cdfc7eSAndroid Build Coastguard Worker "Use a fixed pattern (default random)"},
355*49cdfc7eSAndroid Build Coastguard Worker {"r", &randloops,
356*49cdfc7eSAndroid Build Coastguard Worker "Randomize number of pages map children check (random % 500), "
357*49cdfc7eSAndroid Build Coastguard Worker "otherwise each child checks 500 pages"},
358*49cdfc7eSAndroid Build Coastguard Worker {"S:", &opt_sparseoffset,
359*49cdfc7eSAndroid Build Coastguard Worker "When non-zero, causes the sparse area to be left before the data, "
360*49cdfc7eSAndroid Build Coastguard Worker "so that the actual initial filesize is sparseoffset + filesize "
361*49cdfc7eSAndroid Build Coastguard Worker "(default 0)"},
362*49cdfc7eSAndroid Build Coastguard Worker {},
363*49cdfc7eSAndroid Build Coastguard Worker },
364*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
365*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 12,
366*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
367*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
368*49cdfc7eSAndroid Build Coastguard Worker };
369