forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-mlockmany.c
230 lines (192 loc) · 5.87 KB
/
stress-mlockmany.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#define UNSET_MLOCK_PROCS (0)
#define DEFAULT_MLOCK_PROCS (1024)
static const stress_help_t help[] = {
{ NULL, "mlockmany N", "start N workers exercising many mlock/munlock processes" },
{ NULL, "mlockmany-ops N", "stop after N mlockmany bogo operations" },
{ NULL, "mlockmany-procs N", "use N child processes to mlock regions" },
{ NULL, NULL, NULL }
};
/*
* stress_set_mlockmany_procs()
* set number of processes to spawn to mlock pages
*/
static int stress_set_mlockmany_procs(const char *opt)
{
size_t mlockmany_procs;
mlockmany_procs = (size_t)stress_get_uint64(opt);
stress_check_range("mlockmany-procs", mlockmany_procs,
1, 1000000);
return stress_set_setting("mlockmany-procs", TYPE_ID_SIZE_T, &mlockmany_procs);
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_mlockmany_procs, stress_set_mlockmany_procs },
{ 0, NULL }
};
#if defined(HAVE_MLOCK)
/*
* stress_mlockmany()
* stress by forking and exiting
*/
static int stress_mlockmany(const stress_args_t *args)
{
pid_t *pids;
int ret;
#if defined(RLIMIT_MEMLOCK)
struct rlimit rlim;
#endif
size_t mlock_size, mlockmany_procs = UNSET_MLOCK_PROCS;
(void)stress_get_setting("mlockmany-procs", &mlockmany_procs);
stress_set_oom_adjustment(args->name, true);
/* Explicitly drop capabilities, makes it more OOM-able */
VOID_RET(int, stress_drop_capabilities(args->name));
if (mlockmany_procs == UNSET_MLOCK_PROCS) {
mlockmany_procs = args->num_instances > 0 ? DEFAULT_MLOCK_PROCS / args->num_instances : 1;
if (mlockmany_procs < 1)
mlockmany_procs = 1;
}
pids = calloc((size_t)mlockmany_procs, sizeof(*pids));
if (!pids) {
pr_inf("%s: cannot allocate pids array\n", args->name);
return EXIT_NO_RESOURCE;
}
#if defined(RLIMIT_MEMLOCK)
ret = getrlimit(RLIMIT_MEMLOCK, &rlim);
if (ret < 0) {
mlock_size = 8 * MB;
} else {
mlock_size = rlim.rlim_cur;
}
#else
mlock_size = args->page_size * 1024;
#endif
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
unsigned int i, n;
size_t shmall, freemem, totalmem, freeswap, last_freeswap;
(void)memset(pids, 0, sizeof(*pids) * mlockmany_procs);
stress_get_memlimits(&shmall, &freemem, &totalmem, &last_freeswap);
for (n = 0; n < mlockmany_procs; n++) {
pid_t pid;
if (!keep_stressing(args))
break;
stress_get_memlimits(&shmall, &freemem, &totalmem, &freeswap);
/* We detected swap being used, bail out */
if (last_freeswap > freeswap)
break;
/* Keep track of expanding free swap space */
if (freeswap > last_freeswap)
last_freeswap = freeswap;
pid = fork();
if (pid == 0) {
void *ptr = MAP_FAILED;
size_t mmap_size = mlock_size;
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
stress_set_oom_adjustment(args->name, true);
(void)sched_settings_apply(true);
shim_mlockall(0);
stress_get_memlimits(&shmall, &freemem, &totalmem, &freeswap);
/* We detected swap being used, bail out */
if (last_freeswap > freeswap)
_exit(0);
while (mmap_size > args->page_size) {
if (!keep_stressing(args))
_exit(0);
ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr != MAP_FAILED)
break;
mmap_size >>= 1;
}
if (ptr == MAP_FAILED)
_exit(0);
(void)stress_mincore_touch_pages(ptr, mmap_size);
mlock_size = mmap_size;
while (mlock_size > args->page_size) {
if (!keep_stressing(args))
_exit(0);
ret = shim_mlock(ptr, mlock_size);
if (ret == 0)
break;
mlock_size >>= 1;
}
for (;;) {
if (!keep_stressing(args))
goto unlock;
(void)shim_munlock(ptr, mlock_size);
if (!keep_stressing(args))
goto unmap;
(void)shim_mlock(ptr, mlock_size);
if (!keep_stressing(args))
goto unlock;
/* Try invalid sizes */
(void)shim_mlock(ptr, 0);
(void)shim_munlock(ptr, 0);
(void)shim_mlock(ptr, mlock_size << 1);
(void)shim_munlock(ptr, mlock_size << 1);
(void)shim_mlock(ptr, ~(size_t)0);
(void)shim_munlock(ptr, ~(size_t)0);
if (!keep_stressing(args))
goto unlock;
(void)shim_usleep_interruptible(10000);
}
unlock:
(void)shim_munlock(ptr, mlock_size);
unmap:
(void)munmap(ptr, mmap_size);
_exit(0);
}
if (pid > -1)
(void)setpgid(pids[n], g_pgrp);
pids[n] = pid;
if (!keep_stressing_flag())
break;
}
for (i = 0; i < n; i++) {
if (pids[i] > 0) {
int status;
/* Parent, wait for child */
(void)stress_killpid(pids[i]);
(void)shim_waitpid(pids[i], &status, 0);
inc_counter(args);
}
}
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
free(pids);
return EXIT_SUCCESS;
}
stressor_info_t stress_mlockmany_info = {
.stressor = stress_mlockmany,
.class = CLASS_VM | CLASS_OS | CLASS_PATHOLOGICAL,
.opt_set_funcs = opt_set_funcs,
.help = help
};
#else
stressor_info_t stress_mlockmany_info = {
.stressor = stress_not_implemented,
.class = CLASS_VM | CLASS_OS | CLASS_PATHOLOGICAL,
.opt_set_funcs = opt_set_funcs,
.help = help
};
#endif