xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/mmapstress/mmapstress06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* IBM Corporation */
2*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003	Port to LTP	[email protected] */
3*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
4*49cdfc7eSAndroid Build Coastguard Worker /*
5*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2003
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
9*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
10*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
11*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
14*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
15*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
16*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
19*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
20*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21*49cdfc7eSAndroid Build Coastguard Worker  */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker  * mfile_swap:
25*49cdfc7eSAndroid Build Coastguard Worker  * Mmap a large (> ANON_GRAN_PAGES_MAX) shared, anonymous region before
26*49cdfc7eSAndroid Build Coastguard Worker  * swapping to use the second half of the kernel primitive mfile_swap.
27*49cdfc7eSAndroid Build Coastguard Worker  * However, this test does _NOT_ cause swapping to occur.  Instead it should be
28*49cdfc7eSAndroid Build Coastguard Worker  * run with waves or at the same time as a test which does cause swapping (i.e.
29*49cdfc7eSAndroid Build Coastguard Worker  * vsswapin or vfork_swap)
30*49cdfc7eSAndroid Build Coastguard Worker  */
31*49cdfc7eSAndroid Build Coastguard Worker #define _KMEMUSER
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker /*****  LTP Port        *****/
39*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
40*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
41*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
44*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmapstress06";	//mfile_swap
45*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
46*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
49*49cdfc7eSAndroid Build Coastguard Worker void ok_exit();
50*49cdfc7eSAndroid Build Coastguard Worker /*****	**	**	*****/
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker #define ANON_GRAN_PAGES_MAX	(32U)
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker extern time_t time(time_t *);
55*49cdfc7eSAndroid Build Coastguard Worker extern char *ctime(const time_t *);
56*49cdfc7eSAndroid Build Coastguard Worker extern int atoi(const char *);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker #define NMFPTEPG		(1024)
59*49cdfc7eSAndroid Build Coastguard Worker #define ERROR(M)	(void)fprintf(stderr, "%s: errno = %d; " M "\n", \
60*49cdfc7eSAndroid Build Coastguard Worker 				argv[0], errno);
61*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])62*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	caddr_t mmapaddr;
65*49cdfc7eSAndroid Build Coastguard Worker 	size_t pagesize = sysconf(_SC_PAGE_SIZE);
66*49cdfc7eSAndroid Build Coastguard Worker 	time_t t;
67*49cdfc7eSAndroid Build Coastguard Worker 	int sleep_time = 0;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (!argc) {
70*49cdfc7eSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "argc == 0\n");
71*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker 	if (argc != 2 || !(sleep_time = atoi(argv[1]))) {
74*49cdfc7eSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "usage: %s sleep_time\n", argv[0]);
75*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
78*49cdfc7eSAndroid Build Coastguard Worker //      (void)printf("%s: Started %s", argv[0], ctime(&t));  LTP Port
79*49cdfc7eSAndroid Build Coastguard Worker 	if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) {
80*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't round up brk");
81*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker 	if ((mmapaddr = sbrk(0)) == (char *)-1) {
84*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't find top of brk");
85*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 	/* mmapaddr is now on a page boundary after the brk segment */
88*49cdfc7eSAndroid Build Coastguard Worker 	if (mmap
89*49cdfc7eSAndroid Build Coastguard Worker 	    (mmapaddr, (ANON_GRAN_PAGES_MAX * NMFPTEPG + 1) * pagesize,
90*49cdfc7eSAndroid Build Coastguard Worker 	     PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, 0,
91*49cdfc7eSAndroid Build Coastguard Worker 	     0) == (caddr_t) - 1) {
92*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("large mmap failed");
93*49cdfc7eSAndroid Build Coastguard Worker 		printf("for this test to run, it needs a mmap space of\n");
94*49cdfc7eSAndroid Build Coastguard Worker 		printf("%d pages\n", (ANON_GRAN_PAGES_MAX * NMFPTEPG + 1));
95*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 	(void)sleep(sleep_time);
98*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
99*49cdfc7eSAndroid Build Coastguard Worker //      (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
100*49cdfc7eSAndroid Build Coastguard Worker 	ok_exit();
101*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker /*****	LTP Port	*****/
ok_exit(void)105*49cdfc7eSAndroid Build Coastguard Worker void ok_exit(void)
106*49cdfc7eSAndroid Build Coastguard Worker {
107*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "Test passed");
108*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
anyfail(void)111*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TFAIL, NULL, "Test failed");
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker /*****	**	**	*****/
117