blob: a5a11894f2bb888ec08f1368a8b9318254e588c1 [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"
robbiew13d2fb42002-12-04 19:16:13 +000049
50void blenter(void);
51void setup(void);
52void anyfail(void);
53void ok_exit(void);
54void forkfail(void);
Wanlong Gao354ebb42012-12-07 10:10:04 +080055void terror(char *);
robbiew13d2fb42002-12-04 19:16:13 +000056int instress(void);
57
58#define FAILED 0
59#define PASSED 1
60
61int local_flag = PASSED;
62int block_number;
robbiew13d2fb42002-12-04 19:16:13 +000063FILE *temp;
64
Wanlong Gao354ebb42012-12-07 10:10:04 +080065char *TCID = "page01"; /* Test program identifier. */
66int TST_TOTAL = 1; /* Total number of test cases. */
robbiew13d2fb42002-12-04 19:16:13 +000067/**************/
68
robbiew13d2fb42002-12-04 19:16:13 +000069int main(argc, argv)
Wanlong Gao354ebb42012-12-07 10:10:04 +080070int argc;
71char *argv[];
robbiew13d2fb42002-12-04 19:16:13 +000072{
73 int nchild;
74 int memory_size;
75 int error_count, i, j, pid, status;
76 int *number_pointer;
77 int *memory_pointer;
78 int child, count;
79
robbiew13d2fb42002-12-04 19:16:13 +000080 setup();
81
82 if (argc < 2) {
83 memory_size = 256 * 1024;
84 nchild = 50;
85 } else if (argc == 3) {
86 if (sscanf(argv[1], "%d", &memory_size) != 1)
87 bd_arg(argv[1]);
88 if (sscanf(argv[2], "%d", &nchild) != 1)
89 bd_arg(argv[2]);
90 } else {
91 printf("page01 [memory size (words)] [nchild]\n");
92 tst_resm(TCONF, "\tBad arg count.\n");
93 exit(1);
94 }
95
robbiew13d2fb42002-12-04 19:16:13 +000096 blenter();
97
98 error_count = 0;
99
100 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 /* */
102 /* attempt to fork a number of */
103 /* identical processes */
104 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000105 /****************************************/
106
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800107 for (i = 1; i <= nchild; i++) {
108 if ((pid = fork()) == -1) {
robbiew13d2fb42002-12-04 19:16:13 +0000109 terror("Fork failed (may be OK if under stress)");
110 if (instress())
111 ok_exit();
112 forkfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 } else if (pid == 0) {
robbiew13d2fb42002-12-04 19:16:13 +0000114 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 /* */
116 /* allocate memory of size */
117 /* "memory_size" */
118 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000119 /********************************/
120
Cyril Hrubisd218f342014-09-23 13:14:56 +0200121 memory_pointer = malloc(memory_size * sizeof(int));
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800122 if (memory_pointer == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 tst_resm(TBROK,
124 "Cannot allocate memory - malloc failed.\n");
robbiew13d2fb42002-12-04 19:16:13 +0000125 if (i < 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_resm(TBROK,
127 "This should not happen for first two children.\n");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100128 tst_brkm(TFAIL, NULL,
129 "Child %d - fail.\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 i);
robbiew13d2fb42002-12-04 19:16:13 +0000131 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_resm(TCONF,
133 "This is ok for all but first two children.\n");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100134 tst_brkm(TCONF, NULL,
135 "Child %d - ok.\n", i);
robbiew13d2fb42002-12-04 19:16:13 +0000136 }
137 }
138 number_pointer = memory_pointer;
139
140 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 /* */
142 /* write to it */
143 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000144 /********************************/
145
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800146 for (j = 1; j <= memory_size; j++)
robbiew13d2fb42002-12-04 19:16:13 +0000147 *(number_pointer++) = j;
148 sleep(1);
149
150 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 /* */
152 /* and read from it to */
153 /* check that what was written */
154 /* is still there */
155 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000156 /********************************/
157
158 number_pointer = memory_pointer;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800159 for (j = 1; j <= memory_size; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 if (*(number_pointer++) != j)
161 error_count++;
subrata_modakbdbaec52009-02-26 12:14:51 +0000162 }
robbiew13d2fb42002-12-04 19:16:13 +0000163 exit(error_count);
164 }
165 }
166
167 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 /* */
169 /* wait for the child processes */
170 /* to teminate and report the # */
171 /* of deviations recognized */
172 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000173 /****************************************/
174
175 count = 0;
176 while ((child = wait(&status)) > 0) {
177#ifdef DEBUG
178 tst_resm(TINFO, "Test {%d} exited status %d\n", child, status);
179#endif
180 if (status)
181 local_flag = FAILED;
182 count++;
183 }
184
185 if (count != nchild) {
186 tst_resm(TWARN, "Wrong number of children waited on.\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 tst_resm(TWARN, "Count = %d, expected = %d.\n", count, nchild);
robbiew13d2fb42002-12-04 19:16:13 +0000188 }
189
190 anyfail();
191 /** NOT REACHED **/
Garrett Cooper2c282152010-12-16 00:55:50 -0800192 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000193}
194
195int bd_arg(str)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196char *str;
robbiew13d2fb42002-12-04 19:16:13 +0000197{
198 tst_resm(TCONF, "\tCannot parse %s as a number.\n", str);
199 exit(1);
200}
201
202/** LTP Port **/
203/*
204 * setup
205 *
206 * Do set up - here its a dummy function
207 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208void setup()
robbiew13d2fb42002-12-04 19:16:13 +0000209{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 tst_tmpdir();
211 temp = stderr;
robbiew13d2fb42002-12-04 19:16:13 +0000212}
213
214/*
215 * Function: blenter()
216 *
217 * Description: Print message on entering a new block
218 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800219void blenter()
robbiew13d2fb42002-12-04 19:16:13 +0000220{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221 local_flag = PASSED;
222 return;
robbiew13d2fb42002-12-04 19:16:13 +0000223}
224
225/*
226 *
227 * Function: anyfail()
228 *
229 * Description: Exit a test.
230 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231void anyfail()
robbiew13d2fb42002-12-04 19:16:13 +0000232{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800233 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
234 : tst_resm(TPASS, "Test passed");
235 tst_rmdir();
236 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000237}
238
239/*
240 * ok_exit
241 *
242 * Calling block passed the test
243 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244void ok_exit()
robbiew13d2fb42002-12-04 19:16:13 +0000245{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800246 local_flag = PASSED;
247 return;
robbiew13d2fb42002-12-04 19:16:13 +0000248}
249
250/*
251 * forkfail()
252 *
253 * exit on failure
254 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800255void forkfail()
robbiew13d2fb42002-12-04 19:16:13 +0000256{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100257 tst_brkm(TBROK, tst_rmdir, "Reason: %s\n", strerror(errno));
robbiew13d2fb42002-12-04 19:16:13 +0000258}
259
260/*
261 * Function: terror
262 *
263 * Description: prints error message this may not be because some part of the
264 * test case failed, for example fork() failed. We will log this
265 * failure as TBROK instead of TFAIL.
266 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800267void terror(char *message)
robbiew13d2fb42002-12-04 19:16:13 +0000268{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800269 tst_resm(TBROK, "Reason: %s:%s\n", message, strerror(errno));
270 return;
robbiew13d2fb42002-12-04 19:16:13 +0000271}
272
273/*
274 * instress
275 *
276 * Assume that we are always running under stress, so this function will
277 * return > 0 value always.
278 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800279int instress()
robbiew13d2fb42002-12-04 19:16:13 +0000280{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 tst_resm(TINFO, "System resource may be too low, fork() malloc()"
282 " etc are likely to fail.\n");
283 return 1;
Garrett Cooperbacc8492011-01-14 00:36:17 -0800284}