forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-bigheap.c
202 lines (187 loc) · 5.55 KB
/
stress-bigheap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
/*
* stress_set_bigheap_growth()
* Set bigheap growth from given opt arg string
*/
void stress_set_bigheap_growth(const char *opt)
{
uint64_t bigheap_growth;
bigheap_growth = get_uint64_byte(opt);
check_range_bytes("bigheap-growth", bigheap_growth,
MIN_BIGHEAP_GROWTH, MAX_BIGHEAP_GROWTH);
set_setting("bigheap-growth", TYPE_ID_UINT64, &bigheap_growth);
}
/*
* stress_bigheap()
* stress heap allocation
*/
int stress_bigheap(const args_t *args)
{
void *ptr = NULL, *last_ptr = NULL;
const size_t page_size = args->page_size;
const size_t stride = page_size;
size_t size = 0;
uint32_t ooms = 0, segvs = 0, nomems = 0;
pid_t pid;
uint8_t *last_ptr_end = NULL;
uint64_t bigheap_growth = DEFAULT_BIGHEAP_GROWTH;
if (!get_setting("bigheap-growth", &bigheap_growth)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
bigheap_growth = MAX_BIGHEAP_GROWTH;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
bigheap_growth = MIN_BIGHEAP_GROWTH;
}
if (bigheap_growth < page_size)
bigheap_growth = page_size;
/* Round growth size to nearest page size */
bigheap_growth &= ~(page_size - 1);
again:
if (!g_keep_stressing_flag)
return EXIT_SUCCESS;
pid = fork();
if (pid < 0) {
if (errno == EAGAIN)
goto again;
pr_err("%s: fork failed: errno=%d: (%s)\n",
args->name, errno, strerror(errno));
} else if (pid > 0) {
int status, ret;
(void)setpgid(pid, g_pgrp);
/* Parent, wait for child */
ret = waitpid(pid, &status, 0);
if (ret < 0) {
if (errno != EINTR)
pr_dbg("%s: waitpid(): errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)kill(pid, SIGTERM);
(void)kill(pid, SIGKILL);
(void)waitpid(pid, &status, 0);
} else if (WIFSIGNALED(status)) {
pr_dbg("%s: child died: %s (instance %d)\n",
args->name, stress_strsignal(WTERMSIG(status)),
args->instance);
/* If we got killed by OOM killer, re-start */
if (WTERMSIG(status) == SIGKILL) {
if (g_opt_flags & OPT_FLAGS_OOMABLE) {
log_system_mem_info();
pr_dbg("%s: assuming killed by OOM "
"killer, bailing out "
"(instance %d)\n",
args->name, args->instance);
_exit(0);
} else {
log_system_mem_info();
pr_dbg("%s: assuming killed by OOM "
"killer, restarting again "
"(instance %d)\n",
args->name, args->instance);
ooms++;
goto again;
}
}
/* If we got killed by sigsegv, re-start */
if (WTERMSIG(status) == SIGSEGV) {
pr_dbg("%s: killed by SIGSEGV, "
"restarting again "
"(instance %d)\n",
args->name, args->instance);
segvs++;
goto again;
}
}
} else if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
/* Make sure this is killable by OOM killer */
set_oom_adjustment(args->name, true);
do {
void *old_ptr = ptr;
size += (size_t)bigheap_growth;
/*
* With many instances running it is wise to
* double check before the next realloc as
* sometimes process start up is delayed for
* some time and we should bail out before
* exerting any more memory pressure
*/
if (!g_keep_stressing_flag)
goto abort;
ptr = realloc(old_ptr, size);
if (ptr == NULL) {
pr_dbg("%s: out of memory at %" PRIu64
" MB (instance %d)\n",
args->name, (uint64_t)(4096ULL * size) >> 20,
args->instance);
free(old_ptr);
size = 0;
nomems++;
} else {
size_t i, n;
uint8_t *u8ptr, *tmp;
if (last_ptr == ptr) {
tmp = u8ptr = last_ptr_end;
n = (size_t)bigheap_growth;
} else {
tmp = u8ptr = ptr;
n = size;
}
if (page_size > 0) {
size_t sz = page_size - 1;
uintptr_t pg_ptr = ((uintptr_t)ptr + sz) & ~sz;
size_t len = size - (pg_ptr - (uintptr_t)ptr);
(void)mincore_touch_pages((void *)pg_ptr, len);
}
for (i = 0; i < n; i+= stride, u8ptr += stride) {
if (!g_keep_stressing_flag)
goto abort;
*u8ptr = (uint8_t)i;
}
if (g_opt_flags & OPT_FLAGS_VERIFY) {
for (i = 0; i < n; i+= stride, tmp += stride) {
if (!g_keep_stressing_flag)
goto abort;
if (*tmp != (uint8_t)i)
pr_fail("%s: byte at location %p was 0x%" PRIx8
" instead of 0x%" PRIx8 "\n",
args->name, u8ptr, *tmp, (uint8_t)i);
}
}
last_ptr = ptr;
last_ptr_end = u8ptr;
}
inc_counter(args);
} while (keep_stressing());
abort:
free(ptr);
}
if (ooms + segvs + nomems > 0)
pr_dbg("%s: OOM restarts: %" PRIu32
", SEGV restarts: %" PRIu32
", out of memory restarts: %" PRIu32 ".\n",
args->name, ooms, segvs, nomems);
return EXIT_SUCCESS;
}