blob: 0b866d54965d358ea5811fa13aa3d2c1fa457efb [file] [log] [blame]
robbiew13d2fb42002-12-04 19:16:13 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew13d2fb42002-12-04 19:16:13 +000018 */
19
20/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
21/* 11/01/2002 Port to LTP robbiew@us.ibm.com */
22
Wanlong Gao354ebb42012-12-07 10:10:04 +080023 /* page01.c */
robbiew13d2fb42002-12-04 19:16:13 +000024/*======================================================================
25 =================== TESTPLAN SEGMENT ===================
26CALLS: malloc(3)
27
28 Run with KILL flag
29
30>KEYS: < paging behavior
31>WHAT: < Does the system balk at heavy demands on it's paging facilities?
32>HOW: < Create a number of process, each of which requests a large
33 < chunk of memory to be assigned to an array. Write to each
34 < element in that array, and verify that what was written/stored
35 < is what was expected.
36>BUGS: <
37======================================================================*/
38
Garrett Cooperbacc8492011-01-14 00:36:17 -080039#include <sys/types.h>
40#include <sys/wait.h>
robbiewc0325f72003-02-10 18:46:26 +000041#include <errno.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080042#include <stdio.h>
robbiew13d2fb42002-12-04 19:16:13 +000043#include <stdlib.h>
robbiew13d2fb42002-12-04 19:16:13 +000044
Wanlong Gao354ebb42012-12-07 10:10:04 +080045int bd_arg(char *);
robbiew13d2fb42002-12-04 19:16:13 +000046
47/** LTP Port **/
48#include "test.h"
49#include "usctest.h"
50
51void blenter(void);
52void setup(void);
53void anyfail(void);
54void ok_exit(void);
55void forkfail(void);
Wanlong Gao354ebb42012-12-07 10:10:04 +080056void terror(char *);
robbiew13d2fb42002-12-04 19:16:13 +000057int instress(void);
58
59#define FAILED 0
60#define PASSED 1
61
62int local_flag = PASSED;
63int block_number;
robbiew13d2fb42002-12-04 19:16:13 +000064FILE *temp;
65
Wanlong Gao354ebb42012-12-07 10:10:04 +080066char *TCID = "page01"; /* Test program identifier. */
67int TST_TOTAL = 1; /* Total number of test cases. */
robbiew13d2fb42002-12-04 19:16:13 +000068/**************/
69
robbiew13d2fb42002-12-04 19:16:13 +000070int main(argc, argv)
Wanlong Gao354ebb42012-12-07 10:10:04 +080071int argc;
72char *argv[];
robbiew13d2fb42002-12-04 19:16:13 +000073{
74 int nchild;
75 int memory_size;
76 int error_count, i, j, pid, status;
77 int *number_pointer;
78 int *memory_pointer;
79 int child, count;
80
robbiew13d2fb42002-12-04 19:16:13 +000081 setup();
82
83 if (argc < 2) {
84 memory_size = 256 * 1024;
85 nchild = 50;
86 } else if (argc == 3) {
87 if (sscanf(argv[1], "%d", &memory_size) != 1)
88 bd_arg(argv[1]);
89 if (sscanf(argv[2], "%d", &nchild) != 1)
90 bd_arg(argv[2]);
91 } else {
92 printf("page01 [memory size (words)] [nchild]\n");
93 tst_resm(TCONF, "\tBad arg count.\n");
94 exit(1);
95 }
96
robbiew13d2fb42002-12-04 19:16:13 +000097 blenter();
98
99 error_count = 0;
100
101 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 /* */
103 /* attempt to fork a number of */
104 /* identical processes */
105 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000106 /****************************************/
107
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800108 for (i = 1; i <= nchild; i++) {
109 if ((pid = fork()) == -1) {
robbiew13d2fb42002-12-04 19:16:13 +0000110 terror("Fork failed (may be OK if under stress)");
111 if (instress())
112 ok_exit();
113 forkfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 } else if (pid == 0) {
robbiew13d2fb42002-12-04 19:16:13 +0000115 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 /* */
117 /* allocate memory of size */
118 /* "memory_size" */
119 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000120 /********************************/
121
Cyril Hrubisd218f342014-09-23 13:14:56 +0200122 memory_pointer = malloc(memory_size * sizeof(int));
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800123 if (memory_pointer == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_resm(TBROK,
125 "Cannot allocate memory - malloc failed.\n");
robbiew13d2fb42002-12-04 19:16:13 +0000126 if (i < 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_resm(TBROK,
128 "This should not happen for first two children.\n");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100129 tst_brkm(TFAIL, NULL,
130 "Child %d - fail.\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 i);
robbiew13d2fb42002-12-04 19:16:13 +0000132 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_resm(TCONF,
134 "This is ok for all but first two children.\n");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100135 tst_brkm(TCONF, NULL,
136 "Child %d - ok.\n", i);
robbiew13d2fb42002-12-04 19:16:13 +0000137 }
138 }
139 number_pointer = memory_pointer;
140
141 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 /* */
143 /* write to it */
144 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000145 /********************************/
146
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800147 for (j = 1; j <= memory_size; j++)
robbiew13d2fb42002-12-04 19:16:13 +0000148 *(number_pointer++) = j;
149 sleep(1);
150
151 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 /* */
153 /* and read from it to */
154 /* check that what was written */
155 /* is still there */
156 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000157 /********************************/
158
159 number_pointer = memory_pointer;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800160 for (j = 1; j <= memory_size; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 if (*(number_pointer++) != j)
162 error_count++;
subrata_modakbdbaec52009-02-26 12:14:51 +0000163 }
robbiew13d2fb42002-12-04 19:16:13 +0000164 exit(error_count);
165 }
166 }
167
168 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 /* */
170 /* wait for the child processes */
171 /* to teminate and report the # */
172 /* of deviations recognized */
173 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000174 /****************************************/
175
176 count = 0;
177 while ((child = wait(&status)) > 0) {
178#ifdef DEBUG
179 tst_resm(TINFO, "Test {%d} exited status %d\n", child, status);
180#endif
181 if (status)
182 local_flag = FAILED;
183 count++;
184 }
185
186 if (count != nchild) {
187 tst_resm(TWARN, "Wrong number of children waited on.\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_resm(TWARN, "Count = %d, expected = %d.\n", count, nchild);
robbiew13d2fb42002-12-04 19:16:13 +0000189 }
190
191 anyfail();
192 /** NOT REACHED **/
Garrett Cooper2c282152010-12-16 00:55:50 -0800193 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000194}
195
196int bd_arg(str)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197char *str;
robbiew13d2fb42002-12-04 19:16:13 +0000198{
199 tst_resm(TCONF, "\tCannot parse %s as a number.\n", str);
200 exit(1);
201}
202
203/** LTP Port **/
204/*
205 * setup
206 *
207 * Do set up - here its a dummy function
208 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209void setup()
robbiew13d2fb42002-12-04 19:16:13 +0000210{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 tst_tmpdir();
212 temp = stderr;
robbiew13d2fb42002-12-04 19:16:13 +0000213}
214
215/*
216 * Function: blenter()
217 *
218 * Description: Print message on entering a new block
219 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220void blenter()
robbiew13d2fb42002-12-04 19:16:13 +0000221{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 local_flag = PASSED;
223 return;
robbiew13d2fb42002-12-04 19:16:13 +0000224}
225
226/*
227 *
228 * Function: anyfail()
229 *
230 * Description: Exit a test.
231 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800232void anyfail()
robbiew13d2fb42002-12-04 19:16:13 +0000233{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
235 : tst_resm(TPASS, "Test passed");
236 tst_rmdir();
237 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000238}
239
240/*
241 * ok_exit
242 *
243 * Calling block passed the test
244 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800245void ok_exit()
robbiew13d2fb42002-12-04 19:16:13 +0000246{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 local_flag = PASSED;
248 return;
robbiew13d2fb42002-12-04 19:16:13 +0000249}
250
251/*
252 * forkfail()
253 *
254 * exit on failure
255 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256void forkfail()
robbiew13d2fb42002-12-04 19:16:13 +0000257{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100258 tst_brkm(TBROK, tst_rmdir, "Reason: %s\n", strerror(errno));
robbiew13d2fb42002-12-04 19:16:13 +0000259}
260
261/*
262 * Function: terror
263 *
264 * Description: prints error message this may not be because some part of the
265 * test case failed, for example fork() failed. We will log this
266 * failure as TBROK instead of TFAIL.
267 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268void terror(char *message)
robbiew13d2fb42002-12-04 19:16:13 +0000269{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800270 tst_resm(TBROK, "Reason: %s:%s\n", message, strerror(errno));
271 return;
robbiew13d2fb42002-12-04 19:16:13 +0000272}
273
274/*
275 * instress
276 *
277 * Assume that we are always running under stress, so this function will
278 * return > 0 value always.
279 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800280int instress()
robbiew13d2fb42002-12-04 19:16:13 +0000281{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800282 tst_resm(TINFO, "System resource may be too low, fork() malloc()"
283 " etc are likely to fail.\n");
284 return 1;
Garrett Cooperbacc8492011-01-14 00:36:17 -0800285}