1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
2*49cdfc7eSAndroid Build Coastguard Worker /* */
3*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) International Business Machines Corp., 2001 */
4*49cdfc7eSAndroid Build Coastguard Worker /* */
5*49cdfc7eSAndroid Build Coastguard Worker /* This program is free software; you can redistribute it and/or modify */
6*49cdfc7eSAndroid Build Coastguard Worker /* it under the terms of the GNU General Public License as published by */
7*49cdfc7eSAndroid Build Coastguard Worker /* the Free Software Foundation; either version 2 of the License, or */
8*49cdfc7eSAndroid Build Coastguard Worker /* (at your option) any later version. */
9*49cdfc7eSAndroid Build Coastguard Worker /* */
10*49cdfc7eSAndroid Build Coastguard Worker /* This program is distributed in the hope that it will be useful, */
11*49cdfc7eSAndroid Build Coastguard Worker /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12*49cdfc7eSAndroid Build Coastguard Worker /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13*49cdfc7eSAndroid Build Coastguard Worker /* the GNU General Public License for more details. */
14*49cdfc7eSAndroid Build Coastguard Worker /* */
15*49cdfc7eSAndroid Build Coastguard Worker /* You should have received a copy of the GNU General Public License */
16*49cdfc7eSAndroid Build Coastguard Worker /* along with this program; if not, write to the Free Software */
17*49cdfc7eSAndroid Build Coastguard Worker /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18*49cdfc7eSAndroid Build Coastguard Worker /* */
19*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
22*49cdfc7eSAndroid Build Coastguard Worker /* */
23*49cdfc7eSAndroid Build Coastguard Worker /* History: July - 02 - 2001 Created by Manoj Iyer, IBM Austin TX. */
24*49cdfc7eSAndroid Build Coastguard Worker /* email:[email protected] */
25*49cdfc7eSAndroid Build Coastguard Worker /* */
26*49cdfc7eSAndroid Build Coastguard Worker /* July - 07 - 2001 Modified - changed MAP_PRIVATE to MAP_SHARED */
27*49cdfc7eSAndroid Build Coastguard Worker /* read defect 187 for details. */
28*49cdfc7eSAndroid Build Coastguard Worker /* */
29*49cdfc7eSAndroid Build Coastguard Worker /* July - 09 - 2001 Modified - added option to MAP_PRIVATE or */
30*49cdfc7eSAndroid Build Coastguard Worker /* MAP_SHARED, -p, default is to MAP_SHARED. */
31*49cdfc7eSAndroid Build Coastguard Worker /* */
32*49cdfc7eSAndroid Build Coastguard Worker /* July - 09 - 2001 Modified - added option '-a' MAP_ANONYMOUS. */
33*49cdfc7eSAndroid Build Coastguard Worker /* Default is to map a file. */
34*49cdfc7eSAndroid Build Coastguard Worker /* */
35*49cdfc7eSAndroid Build Coastguard Worker /* Aug - 01 - 2001 Modified - added option 'a' to getop list. */
36*49cdfc7eSAndroid Build Coastguard Worker /* */
37*49cdfc7eSAndroid Build Coastguard Worker /* Oct - 25 - 2001 Modified - changed scheme. Test will be run */
38*49cdfc7eSAndroid Build Coastguard Worker /* once unless -x option is used. */
39*49cdfc7eSAndroid Build Coastguard Worker /* */
40*49cdfc7eSAndroid Build Coastguard Worker /* Apr - 16 - 2003 Modified - replaced tempnam() use with */
41*49cdfc7eSAndroid Build Coastguard Worker /* mkstemp(). -Robbie Williamson */
42*49cdfc7eSAndroid Build Coastguard Worker /* email:[email protected] */
43*49cdfc7eSAndroid Build Coastguard Worker /* */
44*49cdfc7eSAndroid Build Coastguard Worker /* May - 12 - 2003 Modified - remove the huge files when */
45*49cdfc7eSAndroid Build Coastguard Worker /* we are done with the test - Paul Larson */
46*49cdfc7eSAndroid Build Coastguard Worker /* email:[email protected] */
47*49cdfc7eSAndroid Build Coastguard Worker /* File: mmap2.c */
48*49cdfc7eSAndroid Build Coastguard Worker /* */
49*49cdfc7eSAndroid Build Coastguard Worker /* Description: Test the LINUX memory manager. The program is aimed at */
50*49cdfc7eSAndroid Build Coastguard Worker /* stressing the memory manager by repeaded map/write/unmap of a */
51*49cdfc7eSAndroid Build Coastguard Worker /* large (by default 128MB) file. */
52*49cdfc7eSAndroid Build Coastguard Worker /* */
53*49cdfc7eSAndroid Build Coastguard Worker /* Create a file of the specified size in mb, map the file, */
54*49cdfc7eSAndroid Build Coastguard Worker /* change the contents of the file and unmap it. This is repeated*/
55*49cdfc7eSAndroid Build Coastguard Worker /* several times for the specified number of hours. */
56*49cdfc7eSAndroid Build Coastguard Worker /* */
57*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
60*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
61*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
62*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
63*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
64*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
65*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
66*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
67*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
68*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
70*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
71*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
72*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
73*49cdfc7eSAndroid Build Coastguard Worker #include <getopt.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker #define MB (1024 * 1024)
77*49cdfc7eSAndroid Build Coastguard Worker #ifndef TRUE
78*49cdfc7eSAndroid Build Coastguard Worker #define TRUE 1
79*49cdfc7eSAndroid Build Coastguard Worker #endif
80*49cdfc7eSAndroid Build Coastguard Worker #ifndef FALSE
81*49cdfc7eSAndroid Build Coastguard Worker #define FALSE 0
82*49cdfc7eSAndroid Build Coastguard Worker #endif
83*49cdfc7eSAndroid Build Coastguard Worker
mkfile(int size)84*49cdfc7eSAndroid Build Coastguard Worker static int mkfile(int size)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker int fd;
87*49cdfc7eSAndroid Build Coastguard Worker int index = 0;
88*49cdfc7eSAndroid Build Coastguard Worker char buff[4096];
89*49cdfc7eSAndroid Build Coastguard Worker char template[PATH_MAX];
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker memset(buff, 'a', 4096);
92*49cdfc7eSAndroid Build Coastguard Worker snprintf(template, PATH_MAX, "ashfileXXXXXX");
93*49cdfc7eSAndroid Build Coastguard Worker fd = mkstemp(template);
94*49cdfc7eSAndroid Build Coastguard Worker if (fd == -1) {
95*49cdfc7eSAndroid Build Coastguard Worker perror("mkfile(): mkstemp()");
96*49cdfc7eSAndroid Build Coastguard Worker return -1;
97*49cdfc7eSAndroid Build Coastguard Worker } else {
98*49cdfc7eSAndroid Build Coastguard Worker unlink(template);
99*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "creating tmp file and writing 'a' to it ");
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker while (index < (size * MB)) {
103*49cdfc7eSAndroid Build Coastguard Worker index += 4096;
104*49cdfc7eSAndroid Build Coastguard Worker if (write(fd, buff, 4096) == -1) {
105*49cdfc7eSAndroid Build Coastguard Worker perror("mkfile(): write()");
106*49cdfc7eSAndroid Build Coastguard Worker return -1;
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "created file of size %d\n"
110*49cdfc7eSAndroid Build Coastguard Worker "content of the file is 'a'\n", index);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker if (fsync(fd) == -1) {
113*49cdfc7eSAndroid Build Coastguard Worker perror("mkfile(): fsync()");
114*49cdfc7eSAndroid Build Coastguard Worker return -1;
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker return fd;
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker
sig_handler(int signal)119*49cdfc7eSAndroid Build Coastguard Worker static void sig_handler(int signal)
120*49cdfc7eSAndroid Build Coastguard Worker {
121*49cdfc7eSAndroid Build Coastguard Worker if (signal != SIGALRM) {
122*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "sig_handlder(): unexpected signal caught"
123*49cdfc7eSAndroid Build Coastguard Worker "[%d]\n", signal);
124*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
125*49cdfc7eSAndroid Build Coastguard Worker } else
126*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "Test ended, success\n");
127*49cdfc7eSAndroid Build Coastguard Worker exit(0);
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker
usage(char * progname)130*49cdfc7eSAndroid Build Coastguard Worker static void usage(char *progname)
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr,
133*49cdfc7eSAndroid Build Coastguard Worker "Usage: %s -h -s -x\n"
134*49cdfc7eSAndroid Build Coastguard Worker "\t -a set map_flags to MAP_ANONYMOUS\n"
135*49cdfc7eSAndroid Build Coastguard Worker "\t -h help, usage message.\n"
136*49cdfc7eSAndroid Build Coastguard Worker "\t -p set map_flag to MAP_PRIVATE.\tdefault:"
137*49cdfc7eSAndroid Build Coastguard Worker "MAP_SHARED\n"
138*49cdfc7eSAndroid Build Coastguard Worker "\t -s size of the file/memory to be mmaped.\tdefault:"
139*49cdfc7eSAndroid Build Coastguard Worker "128MB\n"
140*49cdfc7eSAndroid Build Coastguard Worker "\t -x time for which test is to be run.\tdefault:"
141*49cdfc7eSAndroid Build Coastguard Worker "24 Hrs\n", progname);
142*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker
get_available_memory_mb(void)145*49cdfc7eSAndroid Build Coastguard Worker unsigned long get_available_memory_mb(void)
146*49cdfc7eSAndroid Build Coastguard Worker {
147*49cdfc7eSAndroid Build Coastguard Worker unsigned long ps, pn;
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker ps = sysconf(_SC_PAGESIZE);
150*49cdfc7eSAndroid Build Coastguard Worker pn = sysconf(_SC_AVPHYS_PAGES);
151*49cdfc7eSAndroid Build Coastguard Worker return (ps / 1024) * pn / 1024;
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char ** argv)154*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker int fd;
157*49cdfc7eSAndroid Build Coastguard Worker unsigned long fsize = 128;
158*49cdfc7eSAndroid Build Coastguard Worker float exec_time = 24;
159*49cdfc7eSAndroid Build Coastguard Worker int c;
160*49cdfc7eSAndroid Build Coastguard Worker int sig_ndx;
161*49cdfc7eSAndroid Build Coastguard Worker int map_flags = MAP_SHARED;
162*49cdfc7eSAndroid Build Coastguard Worker int map_anon = FALSE;
163*49cdfc7eSAndroid Build Coastguard Worker int run_once = TRUE;
164*49cdfc7eSAndroid Build Coastguard Worker char *memptr;
165*49cdfc7eSAndroid Build Coastguard Worker unsigned long avail_memory_mb;
166*49cdfc7eSAndroid Build Coastguard Worker struct sigaction sigptr;
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker static struct signal_info {
169*49cdfc7eSAndroid Build Coastguard Worker int signum;
170*49cdfc7eSAndroid Build Coastguard Worker char *signame;
171*49cdfc7eSAndroid Build Coastguard Worker } sig_info[] = {
172*49cdfc7eSAndroid Build Coastguard Worker {
173*49cdfc7eSAndroid Build Coastguard Worker SIGHUP, "SIGHUP"}, {
174*49cdfc7eSAndroid Build Coastguard Worker SIGINT, "SIGINT"}, {
175*49cdfc7eSAndroid Build Coastguard Worker SIGQUIT, "SIGQUIT"}, {
176*49cdfc7eSAndroid Build Coastguard Worker SIGABRT, "SIGABRT"}, {
177*49cdfc7eSAndroid Build Coastguard Worker SIGBUS, "SIGBUS"}, {
178*49cdfc7eSAndroid Build Coastguard Worker SIGSEGV, "SIGSEGV"}, {
179*49cdfc7eSAndroid Build Coastguard Worker SIGALRM, "SIGALRM"}, {
180*49cdfc7eSAndroid Build Coastguard Worker SIGUSR1, "SIGUSR1"}, {
181*49cdfc7eSAndroid Build Coastguard Worker SIGUSR2, "SIGUSR2"}, {
182*49cdfc7eSAndroid Build Coastguard Worker -1, "ENDSIG"}
183*49cdfc7eSAndroid Build Coastguard Worker };
184*49cdfc7eSAndroid Build Coastguard Worker
185*49cdfc7eSAndroid Build Coastguard Worker while ((c = getopt(argc, argv, "ahps:x:")) != -1) {
186*49cdfc7eSAndroid Build Coastguard Worker switch (c) {
187*49cdfc7eSAndroid Build Coastguard Worker case 'a':
188*49cdfc7eSAndroid Build Coastguard Worker map_anon = TRUE;
189*49cdfc7eSAndroid Build Coastguard Worker break;
190*49cdfc7eSAndroid Build Coastguard Worker case 'h':
191*49cdfc7eSAndroid Build Coastguard Worker usage(argv[0]);
192*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
193*49cdfc7eSAndroid Build Coastguard Worker break;
194*49cdfc7eSAndroid Build Coastguard Worker case 'p':
195*49cdfc7eSAndroid Build Coastguard Worker map_flags = MAP_PRIVATE;
196*49cdfc7eSAndroid Build Coastguard Worker break;
197*49cdfc7eSAndroid Build Coastguard Worker case 's':
198*49cdfc7eSAndroid Build Coastguard Worker fsize = atoi(optarg);
199*49cdfc7eSAndroid Build Coastguard Worker if (fsize == 0)
200*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "Using default "
201*49cdfc7eSAndroid Build Coastguard Worker "fsize %lu MB\n", fsize = 128);
202*49cdfc7eSAndroid Build Coastguard Worker break;
203*49cdfc7eSAndroid Build Coastguard Worker case 'x':
204*49cdfc7eSAndroid Build Coastguard Worker exec_time = atof(optarg);
205*49cdfc7eSAndroid Build Coastguard Worker if (exec_time == 0)
206*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "Using default exec "
207*49cdfc7eSAndroid Build Coastguard Worker "time %f hrs", exec_time = (float)24);
208*49cdfc7eSAndroid Build Coastguard Worker run_once = FALSE;
209*49cdfc7eSAndroid Build Coastguard Worker break;
210*49cdfc7eSAndroid Build Coastguard Worker default:
211*49cdfc7eSAndroid Build Coastguard Worker usage(argv[0]);
212*49cdfc7eSAndroid Build Coastguard Worker break;
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker }
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "MM Stress test, map/write/unmap large file\n"
217*49cdfc7eSAndroid Build Coastguard Worker "\tTest scheduled to run for: %f\n"
218*49cdfc7eSAndroid Build Coastguard Worker "\tSize of temp file in MB: %lu\n", exec_time, fsize);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker avail_memory_mb = get_available_memory_mb();
221*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "Available memory: %ldMB\n", avail_memory_mb);
222*49cdfc7eSAndroid Build Coastguard Worker if (fsize > avail_memory_mb) {
223*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "Not enough memory to run this case\n");
224*49cdfc7eSAndroid Build Coastguard Worker exit(0);
225*49cdfc7eSAndroid Build Coastguard Worker }
226*49cdfc7eSAndroid Build Coastguard Worker
227*49cdfc7eSAndroid Build Coastguard Worker alarm(exec_time * 3600.00);
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker sigptr.sa_handler = sig_handler;
230*49cdfc7eSAndroid Build Coastguard Worker sigfillset(&sigptr.sa_mask);
231*49cdfc7eSAndroid Build Coastguard Worker sigptr.sa_flags = 0;
232*49cdfc7eSAndroid Build Coastguard Worker for (sig_ndx = 0; sig_info[sig_ndx].signum != -1; sig_ndx++) {
233*49cdfc7eSAndroid Build Coastguard Worker sigaddset(&sigptr.sa_mask, sig_info[sig_ndx].signum);
234*49cdfc7eSAndroid Build Coastguard Worker if (sigaction(sig_info[sig_ndx].signum, &sigptr,
235*49cdfc7eSAndroid Build Coastguard Worker NULL) == -1) {
236*49cdfc7eSAndroid Build Coastguard Worker perror("man(): sigaction()");
237*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "could not set handler for SIGALRM,"
238*49cdfc7eSAndroid Build Coastguard Worker "errno = %d\n", errno);
239*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
240*49cdfc7eSAndroid Build Coastguard Worker }
241*49cdfc7eSAndroid Build Coastguard Worker }
242*49cdfc7eSAndroid Build Coastguard Worker
243*49cdfc7eSAndroid Build Coastguard Worker do {
244*49cdfc7eSAndroid Build Coastguard Worker if (!map_anon) {
245*49cdfc7eSAndroid Build Coastguard Worker fd = mkfile(fsize);
246*49cdfc7eSAndroid Build Coastguard Worker if (fd == -1) {
247*49cdfc7eSAndroid Build Coastguard Worker fprintf(stderr, "main(): mkfile(): Failed "
248*49cdfc7eSAndroid Build Coastguard Worker "to create temp file.\n");
249*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker } else {
252*49cdfc7eSAndroid Build Coastguard Worker fd = -1;
253*49cdfc7eSAndroid Build Coastguard Worker map_flags = map_flags | MAP_ANONYMOUS;
254*49cdfc7eSAndroid Build Coastguard Worker }
255*49cdfc7eSAndroid Build Coastguard Worker memptr = mmap(0, (fsize * MB), PROT_READ | PROT_WRITE,
256*49cdfc7eSAndroid Build Coastguard Worker map_flags, fd, 0);
257*49cdfc7eSAndroid Build Coastguard Worker if (memptr == MAP_FAILED) {
258*49cdfc7eSAndroid Build Coastguard Worker perror("main(): mmap()");
259*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
260*49cdfc7eSAndroid Build Coastguard Worker } else
261*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "file mapped at %p\n"
262*49cdfc7eSAndroid Build Coastguard Worker "changing file content to 'A'\n", memptr);
263*49cdfc7eSAndroid Build Coastguard Worker
264*49cdfc7eSAndroid Build Coastguard Worker memset(memptr, 'A', ((fsize * MB) / sizeof(char)));
265*49cdfc7eSAndroid Build Coastguard Worker
266*49cdfc7eSAndroid Build Coastguard Worker if (msync(memptr, ((fsize * MB) / sizeof(char)),
267*49cdfc7eSAndroid Build Coastguard Worker MS_SYNC | MS_INVALIDATE) == -1) {
268*49cdfc7eSAndroid Build Coastguard Worker perror("main(): msync()");
269*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
270*49cdfc7eSAndroid Build Coastguard Worker }
271*49cdfc7eSAndroid Build Coastguard Worker
272*49cdfc7eSAndroid Build Coastguard Worker if (munmap(memptr, (fsize * MB) / sizeof(char)) == -1) {
273*49cdfc7eSAndroid Build Coastguard Worker perror("main(): munmap()");
274*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
275*49cdfc7eSAndroid Build Coastguard Worker } else
276*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "unmapped file at %p\n", memptr);
277*49cdfc7eSAndroid Build Coastguard Worker
278*49cdfc7eSAndroid Build Coastguard Worker close(fd);
279*49cdfc7eSAndroid Build Coastguard Worker sync();
280*49cdfc7eSAndroid Build Coastguard Worker } while (TRUE && !run_once);
281*49cdfc7eSAndroid Build Coastguard Worker exit(0);
282*49cdfc7eSAndroid Build Coastguard Worker }
283