blob: 61ac932e4cba85e80b78874ed1cccb5fb4ecff49 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubisb2de5542014-06-02 16:20:16 +02003 * AUTHOR : William Roske
4 * CO-PILOT : Dave Fenner
plars865695b2001-08-27 22:15:12 +00005 *
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 Gaofed96412012-10-24 10:10:29 +080022 * with this program; if not, write the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000024 *
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 */
plars865695b2001-08-27 22:15:12 +000035
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"
plars865695b2001-08-27 22:15:12 +000044
45#ifndef BSIZE
46#define BSIZE BBSIZE
47#endif
48
49void setup();
50void cleanup();
51
plars865695b2001-08-27 22:15:12 +000052#define MAX_SIZE_LC 1000 /* loop count test will reach max size */
53
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054char *TCID = "brk01";
55int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000056
57long Max_brk_byte_size;
58long Beg_brk_val;
59
robbiewd34d5812005-07-11 22:28:09 +000060#if !defined(UCLINUX)
61
subrata_modak56207ce2009-03-23 13:35:39 +000062int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000063{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
Cyril Hrubisb2de5542014-06-02 16:20:16 +020066 int incr;
subrata_modak56207ce2009-03-23 13:35:39 +000067 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 */
plars865695b2001-08-27 22:15:12 +000070
Garrett Cooper8dca4222010-12-18 03:57:35 -080071 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080073
subrata_modak56207ce2009-03-23 13:35:39 +000074 setup();
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 /*
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);
plars865695b2001-08-27 22:15:12 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082 if ((incr * 2) < 4096) /* make sure that process will grow */
83 incr += 4096 / 2;
plars865695b2001-08-27 22:15:12 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +000086
Caspar Zhangd59a6592013-03-07 14:59:12 +080087 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000088
subrata_modak56207ce2009-03-23 13:35:39 +000089 /*
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);
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 if (nbrkpt > Max_brk_byte_size)
105 nbrkpt = Beg_brk_val; /* start over */
plars865695b2001-08-27 22:15:12 +0000106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 } else {
108 /*
109 * shrink
110 */
111 nbrkpt = cur_brk_val - incr;
112 }
plars865695b2001-08-27 22:15:12 +0000113
114/****
115 printf("cur_brk_val = %d, nbrkpt = %d, incr = %d, lc = %d\n",
116 cur_brk_val, nbrkpt, incr, lc);
117****/
118
subrata_modak56207ce2009-03-23 13:35:39 +0000119 TEST(brk((char *)nbrkpt));
subrata_modakbdbaec52009-02-26 12:14:51 +0000120
subrata_modak56207ce2009-03-23 13:35:39 +0000121 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 aft_brk_val = (long)sbrk(0);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_resm(TFAIL | TTERRNO,
vapier72d58562009-08-28 11:24:25 +0000125 "brk(%ld) failed (size before %ld, after %ld)",
126 nbrkpt, cur_brk_val, aft_brk_val);
plars865695b2001-08-27 22:15:12 +0000127
subrata_modak56207ce2009-03-23 13:35:39 +0000128 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200129 aft_brk_val = (long)sbrk(0);
130 if (aft_brk_val == nbrkpt) {
plars865695b2001-08-27 22:15:12 +0000131
Cyril Hrubise38b9612014-06-02 17:20:57 +0200132 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_modak56207ce2009-03-23 13:35:39 +0000140 }
141 }
142
Garrett Cooper2c282152010-12-16 00:55:50 -0800143 }
plars865695b2001-08-27 22:15:12 +0000144
subrata_modak56207ce2009-03-23 13:35:39 +0000145 cleanup();
Garrett Cooper8dca4222010-12-18 03:57:35 -0800146 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800147}
plars865695b2001-08-27 22:15:12 +0000148
Mike Frysingerc57fba52014-04-09 18:56:30 -0400149void setup(void)
plars865695b2001-08-27 22:15:12 +0000150{
subrata_modak56207ce2009-03-23 13:35:39 +0000151 unsigned long max_size;
152 int ncpus;
153 unsigned long ulim_sz;
154 unsigned long usr_mem_sz;
155 struct rlimit lim;
plars865695b2001-08-27 22:15:12 +0000156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 /*if ((ulim_sz=ulimit(3,0)) == -1)
vapier72d58562009-08-28 11:24:25 +0000160 tst_brkm(TBROK|TERRNO, cleanup, "ulimit(3,0) failed"); */
plars865695b2001-08-27 22:15:12 +0000161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 if (getrlimit(RLIMIT_DATA, &lim) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 tst_brkm(TBROK | TERRNO, cleanup,
vapier72d58562009-08-28 11:24:25 +0000164 "getrlimit(RLIMIT_DATA,%p) failed", &lim);
subrata_modak56207ce2009-03-23 13:35:39 +0000165 ulim_sz = lim.rlim_cur;
plars865695b2001-08-27 22:15:12 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 /*
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);
plars865695b2001-08-27 22:15:12 +0000172
Cyril Hrubisb2de5542014-06-02 16:20:16 +0200173 if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm(TBROK | TERRNO, cleanup,
Cyril Hrubisb2de5542014-06-02 16:20:16 +0200175 "sysconf(_SC_NPROCESSORS_ONLN) failed");
plars865695b2001-08-27 22:15:12 +0000176
subrata_modak56207ce2009-03-23 13:35:39 +0000177 /*
178 * allow 2*ncpus copies to run.
179 * never attempt to take more than a * 1/4 of memory (by single test)
180 */
plars865695b2001-08-27 22:15:12 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 if (ulim_sz < usr_mem_sz)
183 max_size = ulim_sz;
184 else
185 max_size = usr_mem_sz;
plars865695b2001-08-27 22:15:12 +0000186
subrata_modak56207ce2009-03-23 13:35:39 +0000187 max_size = max_size / (2 * ncpus);
plars865695b2001-08-27 22:15:12 +0000188
subrata_modak56207ce2009-03-23 13:35:39 +0000189 if (max_size > (usr_mem_sz / 4))
190 max_size = usr_mem_sz / 4; /* only fourth mem by single test */
plars865695b2001-08-27 22:15:12 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 Beg_brk_val = (long)sbrk(0);
plars865695b2001-08-27 22:15:12 +0000193
subrata_modak56207ce2009-03-23 13:35:39 +0000194 /*
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;
plars865695b2001-08-27 22:15:12 +0000200
subrata_modak56207ce2009-03-23 13:35:39 +0000201 Max_brk_byte_size = max_size;
plars865695b2001-08-27 22:15:12 +0000202
subrata_modak56207ce2009-03-23 13:35:39 +0000203 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000204
Garrett Cooper2c282152010-12-16 00:55:50 -0800205}
plars865695b2001-08-27 22:15:12 +0000206
Mike Frysingerc57fba52014-04-09 18:56:30 -0400207void cleanup(void)
Garrett Cooper8dca4222010-12-18 03:57:35 -0800208{
Garrett Cooper8dca4222010-12-18 03:57:35 -0800209}
210
robbiewd34d5812005-07-11 22:28:09 +0000211#else
212
Mike Frysingerc57fba52014-04-09 18:56:30 -0400213int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000214{
Garrett Cooper8dca4222010-12-18 03:57:35 -0800215 tst_brkm(TCONF, NULL, "test is not available on uClinux");
robbiewd34d5812005-07-11 22:28:09 +0000216}
217
Chris Dearmanec6edca2012-10-17 19:54:01 -0700218#endif /* if !defined(UCLINUX) */