xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/page/page02.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 /* 11/05/2002	Port to LTP	[email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker 			   /* page02.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 	  Writes start in middle of array and proceede to ends.
37*49cdfc7eSAndroid Build Coastguard Worker >BUGS:  <
38*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #ifdef LINUX
45*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
49*49cdfc7eSAndroid Build Coastguard Worker #endif
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
52*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
55*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
58*49cdfc7eSAndroid Build Coastguard Worker int block_number;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "page02";		/* Test program identifier.    */
61*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;		/* Total number of test cases. */
62*49cdfc7eSAndroid Build Coastguard Worker /**************/
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker int bd_arg(char *);
65*49cdfc7eSAndroid Build Coastguard Worker int chld_flag;
66*49cdfc7eSAndroid Build Coastguard Worker int parent_pid;
67*49cdfc7eSAndroid Build Coastguard Worker 
main(argc,argv)68*49cdfc7eSAndroid Build Coastguard Worker int main(argc, argv)
69*49cdfc7eSAndroid Build Coastguard Worker int argc;
70*49cdfc7eSAndroid Build Coastguard Worker char *argv[];
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	int nchild;
73*49cdfc7eSAndroid Build Coastguard Worker 	int memory_size, half_memory_size;
74*49cdfc7eSAndroid Build Coastguard Worker 	int error_count, i, j, pid, status;
75*49cdfc7eSAndroid Build Coastguard Worker 	int *memory_pointer;
76*49cdfc7eSAndroid Build Coastguard Worker 	int *up_pointer, *down_pointer;
77*49cdfc7eSAndroid Build Coastguard Worker 	int child, count;
78*49cdfc7eSAndroid Build Coastguard Worker 	int chld();
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	parent_pid = getpid();
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	if (signal(SIGUSR1, (void (*)())chld) == SIG_ERR) {
84*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK, "signal failed");
85*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 2) {
89*49cdfc7eSAndroid Build Coastguard Worker 		memory_size = 128 * 1024;
90*49cdfc7eSAndroid Build Coastguard Worker 		nchild = 5;
91*49cdfc7eSAndroid Build Coastguard Worker 	} else if (argc == 3) {
92*49cdfc7eSAndroid Build Coastguard Worker 		if (sscanf(argv[1], "%d", &memory_size) != 1)
93*49cdfc7eSAndroid Build Coastguard Worker 			bd_arg(argv[1]);
94*49cdfc7eSAndroid Build Coastguard Worker 		if (sscanf(argv[2], "%d", &nchild) != 1)
95*49cdfc7eSAndroid Build Coastguard Worker 			bd_arg(argv[2]);
96*49cdfc7eSAndroid Build Coastguard Worker 	} else {
97*49cdfc7eSAndroid Build Coastguard Worker 		printf("page02 [memory size (words)]  [nchild]\n");
98*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TCONF, "\tBad arg count.");
99*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 	half_memory_size = memory_size / 2;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	error_count = 0;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
106*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
107*49cdfc7eSAndroid Build Coastguard Worker 	/*      attempt to fork a number of     */
108*49cdfc7eSAndroid Build Coastguard Worker 	/*      identical processes             */
109*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
110*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 1; i <= nchild; i++) {
113*49cdfc7eSAndroid Build Coastguard Worker 		chld_flag = 0;
114*49cdfc7eSAndroid Build Coastguard Worker 		if ((pid = fork()) == -1) {
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TBROK,
116*49cdfc7eSAndroid Build Coastguard Worker 				 "Fork failed (may be OK if under stress)");
117*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "System resource may be too low.");
118*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = PASSED;
119*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, tst_rmdir, "Reason: %s",
120*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
121*49cdfc7eSAndroid Build Coastguard Worker 		} else if (pid == 0) {
122*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
123*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
124*49cdfc7eSAndroid Build Coastguard Worker 			/*   allocate memory  of size   */
125*49cdfc7eSAndroid Build Coastguard Worker 			/*    "memory_size"             */
126*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
127*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 			memory_pointer = malloc(memory_size * sizeof(int));
130*49cdfc7eSAndroid Build Coastguard Worker 			if (memory_pointer == 0) {
131*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TBROK, "\tCannot malloc memory.");
132*49cdfc7eSAndroid Build Coastguard Worker 				if (i < 2) {
133*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TBROK,
134*49cdfc7eSAndroid Build Coastguard Worker 						 "\tThis should not happen to first two children.");
135*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TBROK, "\tChild %d - fail.",
136*49cdfc7eSAndroid Build Coastguard Worker 						 i);
137*49cdfc7eSAndroid Build Coastguard Worker 				} else {
138*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TBROK,
139*49cdfc7eSAndroid Build Coastguard Worker 						 "\tThis is ok for all but first two children.");
140*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TBROK, "\tChild %d - ok.",
141*49cdfc7eSAndroid Build Coastguard Worker 						 i);
142*49cdfc7eSAndroid Build Coastguard Worker 					kill(parent_pid, SIGUSR1);
143*49cdfc7eSAndroid Build Coastguard Worker 					_exit(0);
144*49cdfc7eSAndroid Build Coastguard Worker 				}
145*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TBROK, "malloc fail");
146*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL,
147*49cdfc7eSAndroid Build Coastguard Worker 					 "\tImpossible to allocate memory of size %d in process %d",
148*49cdfc7eSAndroid Build Coastguard Worker 					 memory_size, i);
149*49cdfc7eSAndroid Build Coastguard Worker 				kill(parent_pid, SIGUSR1);
150*49cdfc7eSAndroid Build Coastguard Worker 				tst_exit();
151*49cdfc7eSAndroid Build Coastguard Worker 			}
152*49cdfc7eSAndroid Build Coastguard Worker 			kill(parent_pid, SIGUSR1);
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 			down_pointer = up_pointer = memory_pointer +
155*49cdfc7eSAndroid Build Coastguard Worker 			    (memory_size / 2);
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
158*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
159*49cdfc7eSAndroid Build Coastguard Worker 			/*         write to it          */
160*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
161*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 			for (j = 1; j <= half_memory_size; j++) {
164*49cdfc7eSAndroid Build Coastguard Worker 				*(up_pointer++) = j;
165*49cdfc7eSAndroid Build Coastguard Worker 				*(down_pointer--) = j;
166*49cdfc7eSAndroid Build Coastguard Worker 			}
167*49cdfc7eSAndroid Build Coastguard Worker 			sleep(1);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
170*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
171*49cdfc7eSAndroid Build Coastguard Worker 			/*      and read from it to     */
172*49cdfc7eSAndroid Build Coastguard Worker 			/*  check that what was written */
173*49cdfc7eSAndroid Build Coastguard Worker 			/*       is still there         */
174*49cdfc7eSAndroid Build Coastguard Worker 			/*                              */
175*49cdfc7eSAndroid Build Coastguard Worker 			/********************************/
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 			down_pointer = up_pointer = memory_pointer +
178*49cdfc7eSAndroid Build Coastguard Worker 			    (memory_size / 2);
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 			for (j = 1; j <= half_memory_size; j++) {
181*49cdfc7eSAndroid Build Coastguard Worker 				if (*(up_pointer++) != j)
182*49cdfc7eSAndroid Build Coastguard Worker 					error_count++;
183*49cdfc7eSAndroid Build Coastguard Worker 				if (*(down_pointer--) != j)
184*49cdfc7eSAndroid Build Coastguard Worker 					error_count++;
185*49cdfc7eSAndroid Build Coastguard Worker 			}
186*49cdfc7eSAndroid Build Coastguard Worker 			exit(error_count);
187*49cdfc7eSAndroid Build Coastguard Worker 		}
188*49cdfc7eSAndroid Build Coastguard Worker 		while (!chld_flag)
189*49cdfc7eSAndroid Build Coastguard Worker 			sleep(1);
190*49cdfc7eSAndroid Build Coastguard Worker 	}
191*49cdfc7eSAndroid Build Coastguard Worker 
192*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
193*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
194*49cdfc7eSAndroid Build Coastguard Worker 	/*      wait for the child processes    */
195*49cdfc7eSAndroid Build Coastguard Worker 	/*      to teminate and report the #    */
196*49cdfc7eSAndroid Build Coastguard Worker 	/*      of deviations recognized        */
197*49cdfc7eSAndroid Build Coastguard Worker 	/*                                      */
198*49cdfc7eSAndroid Build Coastguard Worker 	/****************************************/
199*49cdfc7eSAndroid Build Coastguard Worker 
200*49cdfc7eSAndroid Build Coastguard Worker 	count = 0;
201*49cdfc7eSAndroid Build Coastguard Worker 	while ((child = wait(&status)) > 0) {
202*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
203*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "\tTest {%d} exited status %d\n", child,
204*49cdfc7eSAndroid Build Coastguard Worker 			 status);
205*49cdfc7eSAndroid Build Coastguard Worker #endif
206*49cdfc7eSAndroid Build Coastguard Worker 		if (status)
207*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
208*49cdfc7eSAndroid Build Coastguard Worker 		count++;
209*49cdfc7eSAndroid Build Coastguard Worker 	}
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	if (count != nchild) {
212*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "\tWrong number of children waited on.");
213*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "\tCount = %d, expected = %d.",
214*49cdfc7eSAndroid Build Coastguard Worker 			 count, nchild);
215*49cdfc7eSAndroid Build Coastguard Worker 	}
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	(local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
218*49cdfc7eSAndroid Build Coastguard Worker 	    : tst_resm(TPASS, "Test passed");
219*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
220*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
221*49cdfc7eSAndroid Build Coastguard Worker 
222*49cdfc7eSAndroid Build Coastguard Worker }
223*49cdfc7eSAndroid Build Coastguard Worker 
bd_arg(str)224*49cdfc7eSAndroid Build Coastguard Worker int bd_arg(str)
225*49cdfc7eSAndroid Build Coastguard Worker char *str;
226*49cdfc7eSAndroid Build Coastguard Worker {
227*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "\tCannot parse %s as a number.", str);
228*49cdfc7eSAndroid Build Coastguard Worker }
229*49cdfc7eSAndroid Build Coastguard Worker 
chld()230*49cdfc7eSAndroid Build Coastguard Worker int chld()
231*49cdfc7eSAndroid Build Coastguard Worker {
232*49cdfc7eSAndroid Build Coastguard Worker 	if (signal(SIGUSR1, (void (*)())chld) == SIG_ERR) {
233*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, NULL, "signal failed");
234*49cdfc7eSAndroid Build Coastguard Worker 	}
235*49cdfc7eSAndroid Build Coastguard Worker 	chld_flag++;
236*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
237*49cdfc7eSAndroid Build Coastguard Worker }
238