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 <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <getopt.h>
29 #include <err.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 #include "../cpuset_lib/common.h"
36
37 #define BUFFER_SIZE 100
38
39 volatile int end;
40
sighandler1(UNUSED int signo)41 void sighandler1(UNUSED int signo)
42 {
43 }
44
sighandler2(UNUSED int signo)45 void sighandler2(UNUSED int signo)
46 {
47 end = 1;
48 }
49
page_cache_hog(void)50 int page_cache_hog(void)
51 {
52 int fd = -1;
53 char buff[BUFFER_SIZE];
54 char path[BUFFER_SIZE];
55 int ret = 0;
56
57 sprintf(path, "%s", "DATAFILE");
58 fd = open(path, O_RDONLY);
59 if (fd == -1) {
60 warn("open %s failed", path);
61 return -1;
62 }
63
64 while ((ret = read(fd, buff, sizeof(buff))) > 0) ;
65 if (ret == -1)
66 warn("read %s failed", path);
67
68 close(fd);
69 return ret;
70 }
71
mem_hog(void)72 int mem_hog(void)
73 {
74 sigset_t signalset;
75 int fd;
76 int ret = 0;
77
78 if (sigemptyset(&signalset) < 0)
79 err(1, "sigemptyset()");
80 sigsuspend(&signalset);
81
82 while (!end) {
83 ret = page_cache_hog();
84
85 fd = open("./myfifo", O_WRONLY);
86 if (fd == -1)
87 err(1, "open fifo failed");
88
89 if (ret) {
90 if (write(fd, "0", 1) == -1)
91 warn("write fifo failed.");
92 } else {
93 if (write(fd, "1", 1) == -1)
94 warn("write fifo failed.");
95 }
96
97 close(fd);
98
99 sigsuspend(&signalset);
100 }
101
102 return ret;
103 }
104
main(void)105 int main(void)
106 {
107 struct sigaction sa1, sa2;
108
109 sa1.sa_handler = sighandler1;
110 if (sigemptyset(&sa1.sa_mask) < 0)
111 err(1, "sigemptyset()");
112
113 sa1.sa_flags = 0;
114 if (sigaction(SIGUSR1, &sa1, NULL) < 0)
115 err(1, "sigaction()");
116
117 sa2.sa_handler = sighandler2;
118 if (sigemptyset(&sa2.sa_mask) < 0)
119 err(1, "sigemptyset()");
120
121 sa2.sa_flags = 0;
122 if (sigaction(SIGUSR2, &sa2, NULL) < 0)
123 err(1, "sigaction()");
124
125 return mem_hog();
126 }
127