blob: f54e8940e5c4f816c25e09116e927bb6b6a5f2dd [file] [log] [blame]
robbiew84457092003-01-10 17:48:12 +00001/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
2/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
3/*
4 * Copyright (c) International Business Machines Corp., 2003
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew84457092003-01-10 17:48:12 +000019 */
20
21/* as_anon_get:
22 * This program tests the kernel primitive as_anon_get by using up lots of
23 * level 2 page tables causing the kernel to switch to large blocks of
24 * anonymous backing store allocation. This is done by allocating pages 4
25 * megs apart since each pt handles 1024 pages of 4096 bytes each. Each
26 * page thus requires another page table. The pages are then unmapped to
27 * switch back to small swap space allocations.
28 */
29#include <sys/types.h>
30#include <sys/mman.h>
robbiew84457092003-01-10 17:48:12 +000031#include <unistd.h>
32#include <errno.h>
33#include <stdio.h>
34/***** LTP Port *****/
35#include "test.h"
robbiew84457092003-01-10 17:48:12 +000036#define FAILED 0
37#define PASSED 1
38
39int local_flag = PASSED;
40char *TCID = "mmapstress08";
41FILE *temp;
42int TST_TOTAL = 1;
robbiew84457092003-01-10 17:48:12 +000043
subrata_modak8f9b2c22008-12-12 10:41:59 +000044#if defined(__i386__) || defined(__x86_64__)
robbiew84457092003-01-10 17:48:12 +000045int anyfail();
46void ok_exit();
47/***** ** ** *****/
48
robbiew84457092003-01-10 17:48:12 +000049#define NPTEPG (1024)
50/*#define GRAN_NUMBER (1<<2)*/
51
52#define GRAN_NUMBER (1<<8)
53 /* == 256 @ 4MB per mmap(2), we span a total of 1 GB */
54
Wanlong Gao354ebb42012-12-07 10:10:04 +080055extern time_t time(time_t *);
56extern char *ctime(const time_t *);
57extern long sysconf(int name);
robbiew84457092003-01-10 17:48:12 +000058
59#define ERROR(M) (void)fprintf(stderr, "%s: errno = %d: " M "\n", argv[0], \
60 errno)
61
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 /*ARGSUSED*/ int main(int argc, char *argv[])
robbiew84457092003-01-10 17:48:12 +000063{
64 caddr_t mmapaddr, munmap_begin;
65 long pagesize = sysconf(_SC_PAGE_SIZE);
66 int i;
67 time_t t;
68
69 (void)time(&t);
70 //(void)printf("%s: Started %s", argv[0], ctime(&t));
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 if (sbrk(pagesize - ((u_long) sbrk(0) % (u_long) pagesize)) ==
72 (char *)-1) {
robbiew84457092003-01-10 17:48:12 +000073 ERROR("couldn't round up brk to a page boundary");
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 local_flag = FAILED;
75 anyfail();
robbiew84457092003-01-10 17:48:12 +000076 }
77 /* The brk is now at the begining of a page. */
78
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 if ((munmap_begin = mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +000080 ERROR("couldn't find top of brk");
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 local_flag = FAILED;
82 anyfail();
robbiew84457092003-01-10 17:48:12 +000083 }
84 mmapaddr = 0;
85 /* burn level 2 ptes by spacing mmaps 4Meg apart */
86 /* This should switch to large anonymous swap space granularity */
87 for (i = 0; i < GRAN_NUMBER; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 if (mmap(mmapaddr, pagesize, PROT_READ | PROT_WRITE,
89 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0) == (caddr_t) - 1) {
robbiew84457092003-01-10 17:48:12 +000090 ERROR("mmap failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 local_flag = FAILED;
92 anyfail();
robbiew84457092003-01-10 17:48:12 +000093 }
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 mmapaddr += NPTEPG * pagesize;
robbiew84457092003-01-10 17:48:12 +000095 }
96 /* Free bizillion level2 ptes to switch to small granularity */
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 if (munmap(munmap_begin, (size_t) (mmapaddr - munmap_begin))) {
robbiew84457092003-01-10 17:48:12 +000098 ERROR("munmap failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 local_flag = FAILED;
100 anyfail();
robbiew84457092003-01-10 17:48:12 +0000101 }
102 (void)time(&t);
103 //(void)printf("%s: Finished %s", argv[0], ctime(&t));
104 ok_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800105 tst_exit();
robbiew84457092003-01-10 17:48:12 +0000106}
107
robbiew84457092003-01-10 17:48:12 +0000108/***** LTP Port *****/
109void ok_exit()
110{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 tst_resm(TPASS, "Test passed\n");
robbiew84457092003-01-10 17:48:12 +0000112 tst_exit();
113}
114
robbiew84457092003-01-10 17:48:12 +0000115int anyfail()
116{
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100117 tst_brkm(TFAIL, NULL, "Test failed\n");
robbiew84457092003-01-10 17:48:12 +0000118}
119
subrata_modak8f9b2c22008-12-12 10:41:59 +0000120#else /* defined(__i386__) || defined(__x86_64__) */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121int main(void)
subrata_modak8f9b2c22008-12-12 10:41:59 +0000122{
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100123 tst_brkm(TCONF, NULL, "Test is only applicable for IA-32 and x86-64.");
subrata_modak8f9b2c22008-12-12 10:41:59 +0000124}
125#endif
Chris Dearmanec6edca2012-10-17 19:54:01 -0700126/***** ** ** *****/