robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 18 | */ |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 23 | /* page01.c */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 24 | /*====================================================================== |
| 25 | =================== TESTPLAN SEGMENT =================== |
| 26 | CALLS: 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 Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/wait.h> |
robbiew | c0325f7 | 2003-02-10 18:46:26 +0000 | [diff] [blame] | 41 | #include <errno.h> |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 42 | #include <stdio.h> |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 43 | #include <stdlib.h> |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 44 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | int bd_arg(char *); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 46 | |
| 47 | /** LTP Port **/ |
| 48 | #include "test.h" |
| 49 | #include "usctest.h" |
| 50 | |
| 51 | void blenter(void); |
| 52 | void setup(void); |
| 53 | void anyfail(void); |
| 54 | void ok_exit(void); |
| 55 | void forkfail(void); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 56 | void terror(char *); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 57 | int instress(void); |
| 58 | |
| 59 | #define FAILED 0 |
| 60 | #define PASSED 1 |
| 61 | |
| 62 | int local_flag = PASSED; |
| 63 | int block_number; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 64 | FILE *temp; |
| 65 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | char *TCID = "page01"; /* Test program identifier. */ |
| 67 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 68 | /**************/ |
| 69 | |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 70 | int main(argc, argv) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | int argc; |
| 72 | char *argv[]; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 73 | { |
| 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 | |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 81 | 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 | |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 97 | blenter(); |
| 98 | |
| 99 | error_count = 0; |
| 100 | |
| 101 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | /* */ |
| 103 | /* attempt to fork a number of */ |
| 104 | /* identical processes */ |
| 105 | /* */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 106 | /****************************************/ |
| 107 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 108 | for (i = 1; i <= nchild; i++) { |
| 109 | if ((pid = fork()) == -1) { |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 110 | terror("Fork failed (may be OK if under stress)"); |
| 111 | if (instress()) |
| 112 | ok_exit(); |
| 113 | forkfail(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | } else if (pid == 0) { |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 115 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | /* */ |
| 117 | /* allocate memory of size */ |
| 118 | /* "memory_size" */ |
| 119 | /* */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 120 | /********************************/ |
| 121 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 122 | memory_pointer = malloc(memory_size * sizeof(int)); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 123 | if (memory_pointer == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_resm(TBROK, |
| 125 | "Cannot allocate memory - malloc failed.\n"); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 126 | if (i < 2) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_resm(TBROK, |
| 128 | "This should not happen for first two children.\n"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 129 | tst_brkm(TFAIL, NULL, |
| 130 | "Child %d - fail.\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 131 | i); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 132 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | tst_resm(TCONF, |
| 134 | "This is ok for all but first two children.\n"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 135 | tst_brkm(TCONF, NULL, |
| 136 | "Child %d - ok.\n", i); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | number_pointer = memory_pointer; |
| 140 | |
| 141 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | /* */ |
| 143 | /* write to it */ |
| 144 | /* */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 145 | /********************************/ |
| 146 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 147 | for (j = 1; j <= memory_size; j++) |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 148 | *(number_pointer++) = j; |
| 149 | sleep(1); |
| 150 | |
| 151 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | /* */ |
| 153 | /* and read from it to */ |
| 154 | /* check that what was written */ |
| 155 | /* is still there */ |
| 156 | /* */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 157 | /********************************/ |
| 158 | |
| 159 | number_pointer = memory_pointer; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 160 | for (j = 1; j <= memory_size; j++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | if (*(number_pointer++) != j) |
| 162 | error_count++; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 163 | } |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 164 | exit(error_count); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | /* */ |
| 170 | /* wait for the child processes */ |
| 171 | /* to teminate and report the # */ |
| 172 | /* of deviations recognized */ |
| 173 | /* */ |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 174 | /****************************************/ |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_resm(TWARN, "Count = %d, expected = %d.\n", count, nchild); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | anyfail(); |
| 192 | /** NOT REACHED **/ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 193 | tst_exit(); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | int bd_arg(str) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | char *str; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 198 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | void setup() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 210 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | tst_tmpdir(); |
| 212 | temp = stderr; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Function: blenter() |
| 217 | * |
| 218 | * Description: Print message on entering a new block |
| 219 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | void blenter() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 221 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | local_flag = PASSED; |
| 223 | return; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * |
| 228 | * Function: anyfail() |
| 229 | * |
| 230 | * Description: Exit a test. |
| 231 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 232 | void anyfail() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 233 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 234 | (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed") |
| 235 | : tst_resm(TPASS, "Test passed"); |
| 236 | tst_rmdir(); |
| 237 | tst_exit(); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * ok_exit |
| 242 | * |
| 243 | * Calling block passed the test |
| 244 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 245 | void ok_exit() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 246 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | local_flag = PASSED; |
| 248 | return; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * forkfail() |
| 253 | * |
| 254 | * exit on failure |
| 255 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 256 | void forkfail() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 257 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 258 | tst_brkm(TBROK, tst_rmdir, "Reason: %s\n", strerror(errno)); |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 259 | } |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 268 | void terror(char *message) |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 269 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 270 | tst_resm(TBROK, "Reason: %s:%s\n", message, strerror(errno)); |
| 271 | return; |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 272 | } |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 280 | int instress() |
robbiew | 13d2fb4 | 2002-12-04 19:16:13 +0000 | [diff] [blame] | 281 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 282 | tst_resm(TINFO, "System resource may be too low, fork() malloc()" |
| 283 | " etc are likely to fail.\n"); |
| 284 | return 1; |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 285 | } |