robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 1 | /* 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 19 | */ |
| 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> |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <errno.h> |
| 33 | #include <stdio.h> |
| 34 | /***** LTP Port *****/ |
| 35 | #include "test.h" |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 36 | #define FAILED 0 |
| 37 | #define PASSED 1 |
| 38 | |
| 39 | int local_flag = PASSED; |
| 40 | char *TCID = "mmapstress08"; |
| 41 | FILE *temp; |
| 42 | int TST_TOTAL = 1; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 43 | |
subrata_modak | 8f9b2c2 | 2008-12-12 10:41:59 +0000 | [diff] [blame] | 44 | #if defined(__i386__) || defined(__x86_64__) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 45 | int anyfail(); |
| 46 | void ok_exit(); |
| 47 | /***** ** ** *****/ |
| 48 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 49 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 55 | extern time_t time(time_t *); |
| 56 | extern char *ctime(const time_t *); |
| 57 | extern long sysconf(int name); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 58 | |
| 59 | #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d: " M "\n", argv[0], \ |
| 60 | errno) |
| 61 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | /*ARGSUSED*/ int main(int argc, char *argv[]) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 63 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | if (sbrk(pagesize - ((u_long) sbrk(0) % (u_long) pagesize)) == |
| 72 | (char *)-1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 73 | ERROR("couldn't round up brk to a page boundary"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 74 | local_flag = FAILED; |
| 75 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 76 | } |
| 77 | /* The brk is now at the begining of a page. */ |
| 78 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | if ((munmap_begin = mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 80 | ERROR("couldn't find top of brk"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | local_flag = FAILED; |
| 82 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 83 | } |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | if (mmap(mmapaddr, pagesize, PROT_READ | PROT_WRITE, |
| 89 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 90 | ERROR("mmap failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | local_flag = FAILED; |
| 92 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 93 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | mmapaddr += NPTEPG * pagesize; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 95 | } |
| 96 | /* Free bizillion level2 ptes to switch to small granularity */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | if (munmap(munmap_begin, (size_t) (mmapaddr - munmap_begin))) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 98 | ERROR("munmap failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | local_flag = FAILED; |
| 100 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 101 | } |
| 102 | (void)time(&t); |
| 103 | //(void)printf("%s: Finished %s", argv[0], ctime(&t)); |
| 104 | ok_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 105 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 108 | /***** LTP Port *****/ |
| 109 | void ok_exit() |
| 110 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | tst_resm(TPASS, "Test passed\n"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 112 | tst_exit(); |
| 113 | } |
| 114 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 115 | int anyfail() |
| 116 | { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 117 | tst_brkm(TFAIL, NULL, "Test failed\n"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
subrata_modak | 8f9b2c2 | 2008-12-12 10:41:59 +0000 | [diff] [blame] | 120 | #else /* defined(__i386__) || defined(__x86_64__) */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | int main(void) |
subrata_modak | 8f9b2c2 | 2008-12-12 10:41:59 +0000 | [diff] [blame] | 122 | { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 123 | tst_brkm(TCONF, NULL, "Test is only applicable for IA-32 and x86-64."); |
subrata_modak | 8f9b2c2 | 2008-12-12 10:41:59 +0000 | [diff] [blame] | 124 | } |
| 125 | #endif |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 126 | /***** ** ** *****/ |