• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2009 FUJITSU LIMITED
4  * Author: Li Zefan <lizf@cn.fujitsu.com>
5  */
6 
7 #include <sys/mman.h>
8 #include <err.h>
9 #include <math.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 int flag_exit;
16 int flag_ready;
17 
18 int interval;
19 unsigned long memsize;
20 
21 char **pages;
22 int nr_page;
23 
touch_memory(void)24 void touch_memory(void)
25 {
26 	int i;
27 
28 	for (i = 0; i < nr_page; i++)
29 		pages[i][0] = 0xef;
30 }
31 
sigusr_handler(int signo)32 void sigusr_handler(int __attribute__ ((unused)) signo)
33 {
34 	int i;
35 	int pagesize;
36 
37 	pagesize = getpagesize();
38 
39 	nr_page = ceil((double)memsize / pagesize);
40 
41 	pages = calloc(nr_page, sizeof(char *));
42 	if (pages == NULL)
43 		errx(1, "calloc failed");
44 
45 	for (i = 0; i < nr_page; i++) {
46 		pages[i] = mmap(NULL, pagesize, PROT_WRITE | PROT_READ,
47 				MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
48 		if (pages[i] == MAP_FAILED)
49 			err(1, "map failed\n");
50 	}
51 
52 	flag_ready = 1;
53 }
54 
sigint_handler(int signo)55 void sigint_handler(int __attribute__ ((unused)) signo)
56 {
57 	flag_exit = 1;
58 }
59 
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 	char *end;
63 	struct sigaction sigint_action;
64 	struct sigaction sigusr_action;
65 
66 	if (argc != 3)
67 		errx(1, "wrong argument num");
68 
69 	memsize = strtoul(argv[1], &end, 10);
70 	if (*end != '\0')
71 		errx(1, "wrong memsize");
72 	memsize = memsize * 1024 * 1024;
73 
74 	interval = atoi(argv[2]);
75 	if (interval <= 0)
76 		interval = 1;
77 
78 	memset(&sigint_action, 0, sizeof(sigint_action));
79 	sigint_action.sa_handler = &sigint_handler;
80 	if (sigaction(SIGINT, &sigint_action, NULL))
81 		err(1, "sigaction(%s) failed", "SIGINT");
82 
83 	memset(&sigusr_action, 0, sizeof(sigusr_action));
84 	sigusr_action.sa_handler = &sigusr_handler;
85 	if (sigaction(SIGUSR1, &sigusr_action, NULL))
86 		err(1, "sigaction(%s) failed", "SIGUSR1");
87 
88 	while (!flag_exit) {
89 		sleep(interval);
90 
91 		if (flag_ready)
92 			touch_memory();
93 	}
94 
95 	return 0;
96 }
97