xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/mmapstress/mmapstress08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003	Port to LTP	[email protected] */
2*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
3*49cdfc7eSAndroid Build Coastguard Worker /*
4*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2003
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
18*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker /* as_anon_get:
22*49cdfc7eSAndroid Build Coastguard Worker  *	This program tests the kernel primitive as_anon_get by using up lots of
23*49cdfc7eSAndroid Build Coastguard Worker  *	level 2 page tables causing the kernel to switch to large blocks of
24*49cdfc7eSAndroid Build Coastguard Worker  *	anonymous backing store allocation.  This is done by allocating pages 4
25*49cdfc7eSAndroid Build Coastguard Worker  *	megs apart since each pt handles 1024 pages of 4096 bytes each.  Each
26*49cdfc7eSAndroid Build Coastguard Worker  *	page thus requires another page table.  The pages are then unmapped to
27*49cdfc7eSAndroid Build Coastguard Worker  *	switch back to small swap space allocations.
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
34*49cdfc7eSAndroid Build Coastguard Worker /*****	LTP Port	*****/
35*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
36*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
37*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
40*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmapstress08";
41*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
42*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #if defined(__i386__) || defined(__x86_64__)
45*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
46*49cdfc7eSAndroid Build Coastguard Worker void ok_exit();
47*49cdfc7eSAndroid Build Coastguard Worker /*****  **      **      *****/
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker #define NPTEPG		(1024)
50*49cdfc7eSAndroid Build Coastguard Worker /*#define GRAN_NUMBER	(1<<2)*/
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker #define GRAN_NUMBER	(1<<8)
53*49cdfc7eSAndroid Build Coastguard Worker 	/* == 256 @ 4MB per mmap(2), we span a total of 1 GB */
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker extern time_t time(time_t *);
56*49cdfc7eSAndroid Build Coastguard Worker extern char *ctime(const time_t *);
57*49cdfc7eSAndroid Build Coastguard Worker extern long sysconf(int name);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d: " M "\n", argv[0], \
60*49cdfc7eSAndroid Build Coastguard Worker 			errno)
61*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])62*49cdfc7eSAndroid Build Coastguard Worker  /*ARGSUSED*/ int main(int argc, char *argv[])
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	caddr_t mmapaddr, munmap_begin;
65*49cdfc7eSAndroid Build Coastguard Worker 	long pagesize = sysconf(_SC_PAGE_SIZE);
66*49cdfc7eSAndroid Build Coastguard Worker 	int i;
67*49cdfc7eSAndroid Build Coastguard Worker 	time_t t;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
70*49cdfc7eSAndroid Build Coastguard Worker 	//(void)printf("%s: Started %s", argv[0], ctime(&t));
71*49cdfc7eSAndroid Build Coastguard Worker 	if (sbrk(pagesize - ((u_long) sbrk(0) % (u_long) pagesize)) ==
72*49cdfc7eSAndroid Build Coastguard Worker 	    (char *)-1) {
73*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't round up brk to a page boundary");
74*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
75*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 	/* The brk is now at the begining of a page. */
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if ((munmap_begin = mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) {
80*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't find top of brk");
81*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
82*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker 	/* burn level 2 ptes by spacing mmaps 4Meg apart */
85*49cdfc7eSAndroid Build Coastguard Worker 	/* This should switch to large anonymous swap space granularity */
86*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < GRAN_NUMBER; i++) {
87*49cdfc7eSAndroid Build Coastguard Worker 		if (mmap(mmapaddr, pagesize, PROT_READ | PROT_WRITE,
88*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0) == (caddr_t) - 1) {
89*49cdfc7eSAndroid Build Coastguard Worker 			ERROR("mmap failed");
90*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
91*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
92*49cdfc7eSAndroid Build Coastguard Worker 		}
93*49cdfc7eSAndroid Build Coastguard Worker 		mmapaddr += NPTEPG * pagesize;
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker 	/* Free bizillion level2 ptes to switch to small granularity */
96*49cdfc7eSAndroid Build Coastguard Worker 	if (munmap(munmap_begin, (size_t) (mmapaddr - munmap_begin))) {
97*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("munmap failed");
98*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
99*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
102*49cdfc7eSAndroid Build Coastguard Worker 	//(void)printf("%s: Finished %s", argv[0], ctime(&t));
103*49cdfc7eSAndroid Build Coastguard Worker 	ok_exit();
104*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker /*****  LTP Port        *****/
ok_exit(void)108*49cdfc7eSAndroid Build Coastguard Worker void ok_exit(void)
109*49cdfc7eSAndroid Build Coastguard Worker {
110*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "Test passed");
111*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker 
anyfail(void)114*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TFAIL, NULL, "Test failed");
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker #else /* defined(__i386__) || defined(__x86_64__) */
main(void)120*49cdfc7eSAndroid Build Coastguard Worker int main(void)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "Test is only applicable for IA-32 and x86-64.");
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker #endif
125*49cdfc7eSAndroid Build Coastguard Worker /*****  **      **      *****/
126