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