plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
Cyril Hrubis | b2de554 | 2014-06-02 16:20:16 +0200 | [diff] [blame] | 3 | * AUTHOR : William Roske |
| 4 | * CO-PILOT : Dave Fenner |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Further, this software is distributed without any warranty that it is |
| 15 | * free of the rightful claim of any third person regarding infringement |
| 16 | * or the like. Any license provided herein, whether implied or |
| 17 | * otherwise, applies only to this software file. Patent licenses, if |
| 18 | * any, provided herein do not apply to combinations of this program with |
| 19 | * other software, or any other product whatsoever. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 22 | * with this program; if not, write the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * |
| 25 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 26 | * Mountain View, CA 94043, or: |
| 27 | * |
| 28 | * http://www.sgi.com |
| 29 | * |
| 30 | * For further information regarding this notice, see: |
| 31 | * |
| 32 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 33 | * |
| 34 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | |
| 36 | #include <unistd.h> |
| 37 | #include <errno.h> |
| 38 | #include <string.h> |
| 39 | #include <signal.h> |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/resource.h> |
| 42 | |
| 43 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
| 45 | #ifndef BSIZE |
| 46 | #define BSIZE BBSIZE |
| 47 | #endif |
| 48 | |
| 49 | void setup(); |
| 50 | void cleanup(); |
| 51 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #define MAX_SIZE_LC 1000 /* loop count test will reach max size */ |
| 53 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 54 | char *TCID = "brk01"; |
| 55 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | long Max_brk_byte_size; |
| 58 | long Beg_brk_val; |
| 59 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 60 | #if !defined(UCLINUX) |
| 61 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 64 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 65 | const char *msg; |
Cyril Hrubis | b2de554 | 2014-06-02 16:20:16 +0200 | [diff] [blame] | 66 | int incr; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | long nbrkpt; /* new brk point value */ |
| 68 | long cur_brk_val; /* current size returned by sbrk */ |
| 69 | long aft_brk_val; /* current size returned by sbrk */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Garrett Cooper | 8dca422 | 2010-12-18 03:57:35 -0800 | [diff] [blame] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Attempt to control how fast we get to test max size. |
| 78 | * Every MAX_SIZE_LC'th lc will be fastest test will reach max size. |
| 79 | */ |
| 80 | incr = (Max_brk_byte_size - Beg_brk_val) / (MAX_SIZE_LC / 2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | if ((incr * 2) < 4096) /* make sure that process will grow */ |
| 83 | incr += 4096 / 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 87 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | /* |
| 90 | * Determine new value to give brk |
| 91 | * Every even lc value, grow by 2 incr and |
| 92 | * every odd lc value, strink by one incr. |
| 93 | * If lc is equal to 3, no change, special case. |
| 94 | */ |
| 95 | cur_brk_val = (long)sbrk(0); |
| 96 | if (lc == 3) { |
| 97 | nbrkpt = cur_brk_val; /* no change, special one time case */ |
| 98 | } else if ((lc % 2) == 0) { |
| 99 | /* |
| 100 | * grow |
| 101 | */ |
| 102 | nbrkpt = cur_brk_val + (2 * incr); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | if (nbrkpt > Max_brk_byte_size) |
| 105 | nbrkpt = Beg_brk_val; /* start over */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | } else { |
| 108 | /* |
| 109 | * shrink |
| 110 | */ |
| 111 | nbrkpt = cur_brk_val - incr; |
| 112 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
| 114 | /**** |
| 115 | printf("cur_brk_val = %d, nbrkpt = %d, incr = %d, lc = %d\n", |
| 116 | cur_brk_val, nbrkpt, incr, lc); |
| 117 | ****/ |
| 118 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | TEST(brk((char *)nbrkpt)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 120 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | aft_brk_val = (long)sbrk(0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_resm(TFAIL | TTERRNO, |
vapier | 72d5856 | 2009-08-28 11:24:25 +0000 | [diff] [blame] | 125 | "brk(%ld) failed (size before %ld, after %ld)", |
| 126 | nbrkpt, cur_brk_val, aft_brk_val); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 129 | aft_brk_val = (long)sbrk(0); |
| 130 | if (aft_brk_val == nbrkpt) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 132 | tst_resm(TPASS, |
| 133 | "brk(%ld) returned %ld, new size verified by sbrk", |
| 134 | nbrkpt, TEST_RETURN); |
| 135 | } else { |
| 136 | tst_resm(TFAIL, |
| 137 | "brk(%ld) returned %ld, sbrk before %ld, after %ld", |
| 138 | nbrkpt, TEST_RETURN, |
| 139 | cur_brk_val, aft_brk_val); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | cleanup(); |
Garrett Cooper | 8dca422 | 2010-12-18 03:57:35 -0800 | [diff] [blame] | 146 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 149 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | unsigned long max_size; |
| 152 | int ncpus; |
| 153 | unsigned long ulim_sz; |
| 154 | unsigned long usr_mem_sz; |
| 155 | struct rlimit lim; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | /*if ((ulim_sz=ulimit(3,0)) == -1) |
vapier | 72d5856 | 2009-08-28 11:24:25 +0000 | [diff] [blame] | 160 | tst_brkm(TBROK|TERRNO, cleanup, "ulimit(3,0) failed"); */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | if (getrlimit(RLIMIT_DATA, &lim) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | tst_brkm(TBROK | TERRNO, cleanup, |
vapier | 72d5856 | 2009-08-28 11:24:25 +0000 | [diff] [blame] | 164 | "getrlimit(RLIMIT_DATA,%p) failed", &lim); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | ulim_sz = lim.rlim_cur; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | /* |
| 168 | * On IRIX, which is a demand paged system, memory is managed |
| 169 | * different than on Crays systems. For now, pick some value. |
| 170 | */ |
| 171 | usr_mem_sz = 1024 * 1024 * sizeof(long); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
Cyril Hrubis | b2de554 | 2014-06-02 16:20:16 +0200 | [diff] [blame] | 173 | if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TBROK | TERRNO, cleanup, |
Cyril Hrubis | b2de554 | 2014-06-02 16:20:16 +0200 | [diff] [blame] | 175 | "sysconf(_SC_NPROCESSORS_ONLN) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | /* |
| 178 | * allow 2*ncpus copies to run. |
| 179 | * never attempt to take more than a * 1/4 of memory (by single test) |
| 180 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | if (ulim_sz < usr_mem_sz) |
| 183 | max_size = ulim_sz; |
| 184 | else |
| 185 | max_size = usr_mem_sz; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 187 | max_size = max_size / (2 * ncpus); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | if (max_size > (usr_mem_sz / 4)) |
| 190 | max_size = usr_mem_sz / 4; /* only fourth mem by single test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | Beg_brk_val = (long)sbrk(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | /* |
| 195 | * allow at least 4 times a big as current. |
| 196 | * This will override above code. |
| 197 | */ |
| 198 | if (max_size < Beg_brk_val * 4) /* running on small mem and/or high # cpus */ |
| 199 | max_size = Beg_brk_val * 4; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | Max_brk_byte_size = max_size; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 205 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 207 | void cleanup(void) |
Garrett Cooper | 8dca422 | 2010-12-18 03:57:35 -0800 | [diff] [blame] | 208 | { |
Garrett Cooper | 8dca422 | 2010-12-18 03:57:35 -0800 | [diff] [blame] | 209 | } |
| 210 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 211 | #else |
| 212 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 213 | int main(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 214 | { |
Garrett Cooper | 8dca422 | 2010-12-18 03:57:35 -0800 | [diff] [blame] | 215 | tst_brkm(TCONF, NULL, "test is not available on uClinux"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 218 | #endif /* if !defined(UCLINUX) */ |