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: Li Zefan <lizf@cn.fujitsu.com> */
20 /* */
21 /******************************************************************************/
22
23 #include <sys/mman.h>
24 #include <err.h>
25 #include <math.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 int flag_exit;
32 int flag_ready;
33
34 int interval;
35 unsigned long memsize;
36
37 char **pages;
38 int nr_page;
39
touch_memory()40 void touch_memory()
41 {
42 int i;
43
44 for (i = 0; i < nr_page; i++)
45 pages[i][0] = 0xef;
46 }
47
sigusr_handler(int signo)48 void sigusr_handler(int __attribute__ ((unused)) signo)
49 {
50 int i;
51 int pagesize;
52
53 pagesize = getpagesize();
54
55 nr_page = ceil((double)memsize / pagesize);
56
57 pages = calloc(nr_page, sizeof(char *));
58 if (pages == NULL)
59 errx(1, "calloc failed");
60
61 for (i = 0; i < nr_page; i++) {
62 pages[i] = mmap(NULL, pagesize, PROT_WRITE | PROT_READ,
63 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
64 if (pages[i] == MAP_FAILED)
65 err(1, "map failed\n");
66 }
67
68 flag_ready = 1;
69 }
70
sigint_handler(int signo)71 void sigint_handler(int __attribute__ ((unused)) signo)
72 {
73 flag_exit = 1;
74 }
75
main(int argc,char * argv[])76 int main(int argc, char *argv[])
77 {
78 char *end;
79 struct sigaction sigint_action;
80 struct sigaction sigusr_action;
81
82 if (argc != 3)
83 errx(1, "wrong argument num");
84
85 memsize = strtoul(argv[1], &end, 10);
86 if (*end != '\0')
87 errx(1, "wrong memsize");
88 memsize = memsize * 1024 * 1024;
89
90 interval = atoi(argv[2]);
91 if (interval <= 0)
92 interval = 1;
93
94 /* TODO (garrcoop): add error handling. */
95 memset(&sigint_action, 0, sizeof(sigint_action));
96 sigint_action.sa_handler = &sigint_handler;
97 sigaction(SIGINT, &sigint_action, NULL);
98
99 memset(&sigusr_action, 0, sizeof(sigusr_action));
100 sigusr_action.sa_handler = &sigusr_handler;
101 sigaction(SIGUSR1, &sigusr_action, NULL);
102
103 while (!flag_exit) {
104 sleep(interval);
105
106 if (flag_ready)
107 touch_memory();
108 }
109
110 return 0;
111 }
112