xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/page/page01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
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 /* 06/30/2001	Port to Linux	[email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 11/01/2002	Port to LTP  	[email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker 			   /* page01.c */
24*49cdfc7eSAndroid Build Coastguard Worker /*======================================================================
25*49cdfc7eSAndroid Build Coastguard Worker 	=================== TESTPLAN SEGMENT ===================
26*49cdfc7eSAndroid Build Coastguard Worker CALLS:	malloc(3)
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	Run with KILL flag
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker >KEYS:  < paging behavior
31*49cdfc7eSAndroid Build Coastguard Worker >WHAT:  < Does the system balk at heavy demands on it's paging facilities?
32*49cdfc7eSAndroid Build Coastguard Worker >HOW:   < Create a number of process, each of which requests a large
33*49cdfc7eSAndroid Build Coastguard Worker 	< chunk of memory to be assigned to an array.  Write to each
34*49cdfc7eSAndroid Build Coastguard Worker 	< element in that array, and verify that what was written/stored
35*49cdfc7eSAndroid Build Coastguard Worker 	< is what was expected.
36*49cdfc7eSAndroid Build Coastguard Worker >BUGS:  <
37*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker int bd_arg(char *);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
48*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker void blenter(void);
51*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
52*49cdfc7eSAndroid Build Coastguard Worker void anyfail(void);
53*49cdfc7eSAndroid Build Coastguard Worker void ok_exit(void);
54*49cdfc7eSAndroid Build Coastguard Worker void forkfail(void);
55*49cdfc7eSAndroid Build Coastguard Worker void terror(char *);
56*49cdfc7eSAndroid Build Coastguard Worker int instress(void);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
59*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
62*49cdfc7eSAndroid Build Coastguard Worker int block_number;
63*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "page01";		/* Test program identifier.    */
66*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;		/* Total number of test cases. */
67*49cdfc7eSAndroid Build Coastguard Worker /**************/
68*49cdfc7eSAndroid Build Coastguard Worker 
main(argc,argv)69*49cdfc7eSAndroid Build Coastguard Worker int main(argc, argv)
70*49cdfc7eSAndroid Build Coastguard Worker int argc;
71*49cdfc7eSAndroid Build Coastguard Worker char *argv[];
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	int nchild;
74*49cdfc7eSAndroid Build Coastguard Worker 	int memory_size;
75*49cdfc7eSAndroid Build Coastguard Worker 	int error_count, i, j, pid, status;
76*49cdfc7eSAndroid Build Coastguard Worker 	int *number_pointer;
77*49cdfc7eSAndroid Build Coastguard Worker 	int *memory_pointer;
78*49cdfc7eSAndroid Build Coastguard Worker 	int child, count;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	setup();
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 2) {
83*49cdfc7eSAndroid Build Coastguard Worker 		memory_size = 256 * 1024;
84*49cdfc7eSAndroid Build Coastguard Worker 		nchild = 50;
85*49cdfc7eSAndroid Build Coastguard Worker 	} else if (argc == 3) {
86*49cdfc7eSAndroid Build Coastguard Worker 		if (sscanf(argv[1], "%d", &memory_size) != 1)
87*49cdfc7eSAndroid Build Coastguard Worker 			bd_arg(argv[1]);
88*49cdfc7eSAndroid Build Coastguard Worker 		if (sscanf(argv[2], "%d", &nchild) != 1)
89*49cdfc7eSAndroid Build Coastguard Worker 			bd_arg(argv[2]);
90*49cdfc7eSAndroid Build Coastguard Worker 	} else {
91*49cdfc7eSAndroid Build Coastguard Worker 		printf("page01 [memory size (words)]  [nchild]\n");
92*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TCONF, "\tBad arg count.");
93*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	blenter();
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	error_count = 0;
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
101*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
102*49cdfc7eSAndroid Build Coastguard Worker 	/*      attempt to fork a number of     */
103*49cdfc7eSAndroid Build Coastguard Worker 	/*      identical processes             */
104*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
105*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 1; i <= nchild; i++) {
108*49cdfc7eSAndroid Build Coastguard Worker 		if ((pid = fork()) == -1) {
109*49cdfc7eSAndroid Build Coastguard Worker 			terror("Fork failed (may be OK if under stress)");
110*49cdfc7eSAndroid Build Coastguard Worker 			if (instress())
111*49cdfc7eSAndroid Build Coastguard Worker 				ok_exit();
112*49cdfc7eSAndroid Build Coastguard Worker 			forkfail();
113*49cdfc7eSAndroid Build Coastguard Worker 		} else if (pid == 0) {
114*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
115*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
116*49cdfc7eSAndroid Build Coastguard Worker 			/*   allocate memory  of size   */
117*49cdfc7eSAndroid Build Coastguard Worker 			/*    "memory_size"             */
118*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
119*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 			memory_pointer = malloc(memory_size * sizeof(int));
122*49cdfc7eSAndroid Build Coastguard Worker 			if (memory_pointer == 0) {
123*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TBROK,
124*49cdfc7eSAndroid Build Coastguard Worker 					 "Cannot allocate memory - malloc failed.");
125*49cdfc7eSAndroid Build Coastguard Worker 				if (i < 2) {
126*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TBROK,
127*49cdfc7eSAndroid Build Coastguard Worker 						 "This should not happen for first two children.");
128*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL, NULL,
129*49cdfc7eSAndroid Build Coastguard Worker 						 "Child %d - fail.",
130*49cdfc7eSAndroid Build Coastguard Worker 						 i);
131*49cdfc7eSAndroid Build Coastguard Worker 				} else {
132*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TCONF,
133*49cdfc7eSAndroid Build Coastguard Worker 						 "This is ok for all but first two children.");
134*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TCONF, NULL,
135*49cdfc7eSAndroid Build Coastguard Worker 						 "Child %d - ok.", i);
136*49cdfc7eSAndroid Build Coastguard Worker 				}
137*49cdfc7eSAndroid Build Coastguard Worker 			}
138*49cdfc7eSAndroid Build Coastguard Worker 			number_pointer = memory_pointer;
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
141*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
142*49cdfc7eSAndroid Build Coastguard Worker 			/*         write to it          */
143*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
144*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 			for (j = 1; j <= memory_size; j++)
147*49cdfc7eSAndroid Build Coastguard Worker 				*(number_pointer++) = j;
148*49cdfc7eSAndroid Build Coastguard Worker 			sleep(1);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
151*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
152*49cdfc7eSAndroid Build Coastguard Worker 			/*      and read from it to     */
153*49cdfc7eSAndroid Build Coastguard Worker 			/*  check that what was written */
154*49cdfc7eSAndroid Build Coastguard Worker 			/*       is still there         */
155*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
156*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 			number_pointer = memory_pointer;
159*49cdfc7eSAndroid Build Coastguard Worker 			for (j = 1; j <= memory_size; j++) {
160*49cdfc7eSAndroid Build Coastguard Worker 				if (*(number_pointer++) != j)
161*49cdfc7eSAndroid Build Coastguard Worker 					error_count++;
162*49cdfc7eSAndroid Build Coastguard Worker 			}
163*49cdfc7eSAndroid Build Coastguard Worker 			exit(error_count);
164*49cdfc7eSAndroid Build Coastguard Worker 		}
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
168*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
169*49cdfc7eSAndroid Build Coastguard Worker 	/*      wait for the child processes    */
170*49cdfc7eSAndroid Build Coastguard Worker 	/*      to teminate and report the #    */
171*49cdfc7eSAndroid Build Coastguard Worker 	/*      of deviations recognized        */
172*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
173*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	count = 0;
176*49cdfc7eSAndroid Build Coastguard Worker 	while ((child = wait(&status)) > 0) {
177*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
178*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Test {%d} exited status %d\n", child, status);
179*49cdfc7eSAndroid Build Coastguard Worker #endif
180*49cdfc7eSAndroid Build Coastguard Worker 		if (status)
181*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
182*49cdfc7eSAndroid Build Coastguard Worker 		count++;
183*49cdfc7eSAndroid Build Coastguard Worker 	}
184*49cdfc7eSAndroid Build Coastguard Worker 
185*49cdfc7eSAndroid Build Coastguard Worker 	if (count != nchild) {
186*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN, "Wrong number of children waited on.");
187*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN, "Count = %d, expected = %d.", count, nchild);
188*49cdfc7eSAndroid Build Coastguard Worker 	}
189*49cdfc7eSAndroid Build Coastguard Worker 
190*49cdfc7eSAndroid Build Coastguard Worker 	anyfail();
191*49cdfc7eSAndroid Build Coastguard Worker 	/** NOT REACHED **/
192*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker 
bd_arg(str)195*49cdfc7eSAndroid Build Coastguard Worker int bd_arg(str)
196*49cdfc7eSAndroid Build Coastguard Worker char *str;
197*49cdfc7eSAndroid Build Coastguard Worker {
198*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TCONF, "\tCannot parse %s as a number.", str);
199*49cdfc7eSAndroid Build Coastguard Worker 	exit(1);
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker 
202*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
203*49cdfc7eSAndroid Build Coastguard Worker /*
204*49cdfc7eSAndroid Build Coastguard Worker  * setup
205*49cdfc7eSAndroid Build Coastguard Worker  *
206*49cdfc7eSAndroid Build Coastguard Worker  * Do set up - here its a dummy function
207*49cdfc7eSAndroid Build Coastguard Worker  */
setup()208*49cdfc7eSAndroid Build Coastguard Worker void setup()
209*49cdfc7eSAndroid Build Coastguard Worker {
210*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
211*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
212*49cdfc7eSAndroid Build Coastguard Worker }
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker /*
215*49cdfc7eSAndroid Build Coastguard Worker  * Function: blenter()
216*49cdfc7eSAndroid Build Coastguard Worker  *
217*49cdfc7eSAndroid Build Coastguard Worker  * Description: Print message on entering a new block
218*49cdfc7eSAndroid Build Coastguard Worker  */
blenter()219*49cdfc7eSAndroid Build Coastguard Worker void blenter()
220*49cdfc7eSAndroid Build Coastguard Worker {
221*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
222*49cdfc7eSAndroid Build Coastguard Worker 	return;
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker /*
226*49cdfc7eSAndroid Build Coastguard Worker  *
227*49cdfc7eSAndroid Build Coastguard Worker  * Function: anyfail()
228*49cdfc7eSAndroid Build Coastguard Worker  *
229*49cdfc7eSAndroid Build Coastguard Worker  * Description: Exit a test.
230*49cdfc7eSAndroid Build Coastguard Worker  */
anyfail()231*49cdfc7eSAndroid Build Coastguard Worker void anyfail()
232*49cdfc7eSAndroid Build Coastguard Worker {
233*49cdfc7eSAndroid Build Coastguard Worker 	(local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
234*49cdfc7eSAndroid Build Coastguard Worker 	    : tst_resm(TPASS, "Test passed");
235*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
236*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker 
239*49cdfc7eSAndroid Build Coastguard Worker /*
240*49cdfc7eSAndroid Build Coastguard Worker  * ok_exit
241*49cdfc7eSAndroid Build Coastguard Worker  *
242*49cdfc7eSAndroid Build Coastguard Worker  * Calling block passed the test
243*49cdfc7eSAndroid Build Coastguard Worker  */
ok_exit()244*49cdfc7eSAndroid Build Coastguard Worker void ok_exit()
245*49cdfc7eSAndroid Build Coastguard Worker {
246*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
247*49cdfc7eSAndroid Build Coastguard Worker 	return;
248*49cdfc7eSAndroid Build Coastguard Worker }
249*49cdfc7eSAndroid Build Coastguard Worker 
250*49cdfc7eSAndroid Build Coastguard Worker /*
251*49cdfc7eSAndroid Build Coastguard Worker  * forkfail()
252*49cdfc7eSAndroid Build Coastguard Worker  *
253*49cdfc7eSAndroid Build Coastguard Worker  * exit on failure
254*49cdfc7eSAndroid Build Coastguard Worker  */
forkfail()255*49cdfc7eSAndroid Build Coastguard Worker void forkfail()
256*49cdfc7eSAndroid Build Coastguard Worker {
257*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TBROK, tst_rmdir, "Reason: %s", strerror(errno));
258*49cdfc7eSAndroid Build Coastguard Worker }
259*49cdfc7eSAndroid Build Coastguard Worker 
260*49cdfc7eSAndroid Build Coastguard Worker /*
261*49cdfc7eSAndroid Build Coastguard Worker  * Function: terror
262*49cdfc7eSAndroid Build Coastguard Worker  *
263*49cdfc7eSAndroid Build Coastguard Worker  * Description: prints error message this may not be because some part of the
264*49cdfc7eSAndroid Build Coastguard Worker  *              test case failed, for example fork() failed. We will log this
265*49cdfc7eSAndroid Build Coastguard Worker  *              failure as TBROK instead of TFAIL.
266*49cdfc7eSAndroid Build Coastguard Worker  */
terror(char * message)267*49cdfc7eSAndroid Build Coastguard Worker void terror(char *message)
268*49cdfc7eSAndroid Build Coastguard Worker {
269*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TBROK, "Reason: %s:%s", message, strerror(errno));
270*49cdfc7eSAndroid Build Coastguard Worker 	return;
271*49cdfc7eSAndroid Build Coastguard Worker }
272*49cdfc7eSAndroid Build Coastguard Worker 
273*49cdfc7eSAndroid Build Coastguard Worker /*
274*49cdfc7eSAndroid Build Coastguard Worker  * instress
275*49cdfc7eSAndroid Build Coastguard Worker  *
276*49cdfc7eSAndroid Build Coastguard Worker  * Assume that we are always running under stress, so this function will
277*49cdfc7eSAndroid Build Coastguard Worker  * return > 0 value always.
278*49cdfc7eSAndroid Build Coastguard Worker  */
instress()279*49cdfc7eSAndroid Build Coastguard Worker int instress()
280*49cdfc7eSAndroid Build Coastguard Worker {
281*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "System resource may be too low, fork() malloc()"
282*49cdfc7eSAndroid Build Coastguard Worker 		 " etc are likely to fail.");
283*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
284*49cdfc7eSAndroid Build Coastguard Worker }
285