blob: 08f159640e2c477745adbcb8186f5e937030439a [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/* 11/05/2002 Port to LTP robbiew@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
Wanlong Gao354ebb42012-12-07 10:10:04 +080023 /* page02.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 Writes start in middle of array and proceede to ends.
37>BUGS: <
38======================================================================*/
39
40#include <stdio.h>
41#include <signal.h>
robbiewc0325f72003-02-10 18:46:26 +000042#include <errno.h>
robbiew13d2fb42002-12-04 19:16:13 +000043
44#ifdef LINUX
45#include <stdlib.h>
46#include <unistd.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080047#include <stdlib.h>
robbiew13d2fb42002-12-04 19:16:13 +000048#include <sys/wait.h>
49#endif
50
51/** LTP Port **/
52#include "test.h"
robbiew13d2fb42002-12-04 19:16:13 +000053
54#define FAILED 0
55#define PASSED 1
56
57int local_flag = PASSED;
58int block_number;
robbiew13d2fb42002-12-04 19:16:13 +000059
Wanlong Gao354ebb42012-12-07 10:10:04 +080060char *TCID = "page02"; /* Test program identifier. */
61int TST_TOTAL = 1; /* Total number of test cases. */
robbiew13d2fb42002-12-04 19:16:13 +000062/**************/
63
Wanlong Gao354ebb42012-12-07 10:10:04 +080064int bd_arg(char *);
robbiew13d2fb42002-12-04 19:16:13 +000065int chld_flag;
66int parent_pid;
67
68int main(argc, argv)
Wanlong Gao354ebb42012-12-07 10:10:04 +080069int argc;
70char *argv[];
robbiew13d2fb42002-12-04 19:16:13 +000071{
72 int nchild;
73 int memory_size, half_memory_size;
74 int error_count, i, j, pid, status;
75 int *memory_pointer;
76 int *up_pointer, *down_pointer;
77 int child, count;
78 int chld();
79
80 parent_pid = getpid();
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +000082
robbiew13d2fb42002-12-04 19:16:13 +000083 if (signal(SIGUSR1, (void (*)())chld) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 tst_resm(TBROK, "signal failed");
robbiew13d2fb42002-12-04 19:16:13 +000085 exit(1);
86 }
87
88 if (argc < 2) {
89 memory_size = 128 * 1024;
90 nchild = 5;
91 } else if (argc == 3) {
92 if (sscanf(argv[1], "%d", &memory_size) != 1)
93 bd_arg(argv[1]);
94 if (sscanf(argv[2], "%d", &nchild) != 1)
95 bd_arg(argv[2]);
96 } else {
97 printf("page02 [memory size (words)] [nchild]\n");
98 tst_resm(TCONF, "\tBad arg count.\n");
99 exit(1);
100 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 half_memory_size = memory_size / 2;
robbiew13d2fb42002-12-04 19:16:13 +0000102
robbiew13d2fb42002-12-04 19:16:13 +0000103 error_count = 0;
104
105 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 /* */
107 /* attempt to fork a number of */
108 /* identical processes */
109 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000110 /****************************************/
111
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800112 for (i = 1; i <= nchild; i++) {
robbiew13d2fb42002-12-04 19:16:13 +0000113 chld_flag = 0;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800114 if ((pid = fork()) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 tst_resm(TBROK,
116 "Fork failed (may be OK if under stress)");
robbiew13d2fb42002-12-04 19:16:13 +0000117 tst_resm(TINFO, "System resource may be too low.\n");
118 local_flag = PASSED;
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100119 tst_brkm(TBROK, tst_rmdir, "Reason: %s\n",
120 strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 } else if (pid == 0) {
robbiew13d2fb42002-12-04 19:16:13 +0000122 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 /* */
124 /* allocate memory of size */
125 /* "memory_size" */
126 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000127 /********************************/
128
Cyril Hrubisd218f342014-09-23 13:14:56 +0200129 memory_pointer = malloc(memory_size * sizeof(int));
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800130 if (memory_pointer == 0) {
robbiew13d2fb42002-12-04 19:16:13 +0000131 tst_resm(TBROK, "\tCannot malloc memory.\n");
132 if (i < 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_resm(TBROK,
134 "\tThis should not happen to first two children.\n");
135 tst_resm(TBROK, "\tChild %d - fail.\n",
136 i);
robbiew13d2fb42002-12-04 19:16:13 +0000137 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_resm(TBROK,
139 "\tThis is ok for all but first two children.\n");
140 tst_resm(TBROK, "\tChild %d - ok.\n",
141 i);
robbiew13d2fb42002-12-04 19:16:13 +0000142 kill(parent_pid, SIGUSR1);
143 _exit(0);
144 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_resm(TBROK, "malloc fail");
146 tst_resm(TFAIL,
147 "\t\nImpossible to allocate memory of size %d in process %d\n",
148 memory_size, i);
robbiew13d2fb42002-12-04 19:16:13 +0000149 kill(parent_pid, SIGUSR1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000150 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000151 }
152 kill(parent_pid, SIGUSR1);
Garrett Cooper2c282152010-12-16 00:55:50 -0800153
robbiew13d2fb42002-12-04 19:16:13 +0000154 down_pointer = up_pointer = memory_pointer +
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 (memory_size / 2);
robbiew13d2fb42002-12-04 19:16:13 +0000156
157 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 /* */
159 /* write to it */
160 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000161 /********************************/
162
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800163 for (j = 1; j <= half_memory_size; j++) {
robbiew13d2fb42002-12-04 19:16:13 +0000164 *(up_pointer++) = j;
165 *(down_pointer--) = j;
166 }
167 sleep(1);
168
169 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 /* */
171 /* and read from it to */
172 /* check that what was written */
173 /* is still there */
174 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000175 /********************************/
176
177 down_pointer = up_pointer = memory_pointer +
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 (memory_size / 2);
robbiew13d2fb42002-12-04 19:16:13 +0000179
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800180 for (j = 1; j <= half_memory_size; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 if (*(up_pointer++) != j)
182 error_count++;
183 if (*(down_pointer--) != j)
184 error_count++;
subrata_modakbdbaec52009-02-26 12:14:51 +0000185 }
robbiew13d2fb42002-12-04 19:16:13 +0000186 exit(error_count);
187 }
188 while (!chld_flag)
189 sleep(1);
190 }
191
192 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 /* */
194 /* wait for the child processes */
195 /* to teminate and report the # */
196 /* of deviations recognized */
197 /* */
robbiew13d2fb42002-12-04 19:16:13 +0000198 /****************************************/
199
200 count = 0;
201 while ((child = wait(&status)) > 0) {
202#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 tst_resm(TINFO, "\tTest {%d} exited status %d\n", child,
204 status);
robbiew13d2fb42002-12-04 19:16:13 +0000205#endif
206 if (status)
207 local_flag = FAILED;
208 count++;
209 }
210
211 if (count != nchild) {
212 tst_resm(TFAIL, "\tWrong number of children waited on.\n");
213 tst_resm(TFAIL, "\tCount = %d, expected = %d.\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 count, nchild);
robbiew13d2fb42002-12-04 19:16:13 +0000215 }
216
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
218 : tst_resm(TPASS, "Test passed");
219 tst_rmdir();
220 tst_exit();
robbiew13d2fb42002-12-04 19:16:13 +0000221
222}
223
224int bd_arg(str)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225char *str;
robbiew13d2fb42002-12-04 19:16:13 +0000226{
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100227 tst_brkm(TCONF, NULL, "\tCannot parse %s as a number.\n", str);
robbiew13d2fb42002-12-04 19:16:13 +0000228}
229
230int chld()
231{
232 if (signal(SIGUSR1, (void (*)())chld) == SIG_ERR) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100233 tst_brkm(TBROK, NULL, "signal failed");
robbiew13d2fb42002-12-04 19:16:13 +0000234 }
235 chld_flag++;
subrata_modak43337a32009-02-26 11:43:51 +0000236 return 0;
Garrett Cooperbacc8492011-01-14 00:36:17 -0800237}