1 /******************************************************************************/
2 /* */
3 /* Copyright (c) 2009 FUJITSU LIMITED */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation; either version 2 of the License, or */
8 /* (at your option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13 /* the GNU General Public License for more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with this program; if not, write to the Free Software */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18 /* */
19 /* Author: Miao Xie <miaox@cn.fujitsu.com> */
20 /* */
21 /******************************************************************************/
22
23 #include <sys/mman.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <libgen.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 int unit = 1024 * 1024;
33
34 //argv[1] memory use(M) (>0)
35
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 long int use;
39 if (argc != 2) {
40 fprintf(stderr, "usage: %s mmap-size-in-kB", basename(argv[0]));
41 exit(1);
42 }
43 if ((use = strtol(argv[1], NULL, 10)) < 0) {
44 errx(EINVAL, "Invalid mmap size specified (must be a long "
45 "int greater than 1)");
46 }
47
48 long int pagesize = getpagesize();
49 long int mmap_block = use * unit / 10 / pagesize;
50 long int total_block = 0;
51
52 while (pagesize * mmap_block > 2 * unit) {
53 unsigned long *addr = mmap(NULL, pagesize * mmap_block,
54 PROT_READ | PROT_WRITE,
55 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56 if (addr == MAP_FAILED) {
57 mmap_block = mmap_block / 2;
58 continue;
59 }
60 memset(addr, 0xF7, pagesize * mmap_block);
61 total_block += mmap_block;
62 if (total_block * pagesize >= use * unit)
63 break;
64 }
65 return 0;
66 }
67