forked from ColinIanKing/stress-ng
/
stress-shm-sysv.c
485 lines (436 loc) · 11.7 KB
/
stress-shm-sysv.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (C) 2013-2016 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 <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include "stress-ng.h"
#define KEY_GET_RETRIES (40)
/*
* Note, running this test with the --maximize option on
* low memory systems with many instances can trigger the
* OOM killer fairly easily. The test tries hard to reap
* reap shared memory segments that are left over if the
* child is killed, however if the OOM killer kills the
* parent that does the reaping, then one can be left with
* a system with many shared segments still reserved and
* little free memory.
*/
typedef struct {
int index;
int shm_id;
} shm_msg_t;
static size_t opt_shm_sysv_bytes = DEFAULT_SHM_SYSV_BYTES;
static size_t opt_shm_sysv_segments = DEFAULT_SHM_SYSV_SEGMENTS;
static bool set_shm_sysv_bytes = false;
static bool set_shm_sysv_segments = false;
static int shm_flags[] = {
#if defined(SHM_HUGETLB)
SHM_HUGETLB,
#endif
#if defined(SHM_HUGE_2MB)
SHM_HUGE_2MB,
#endif
#if defined(SHM_HUGE_1GB)
SHM_HUGE_1GB,
#endif
/* This will segv if no backing, so don't use it for now */
/*
#if defined(SHM_NO_RESERVE)
SHM_NO_RESERVE
#endif
*/
0
};
void stress_set_shm_sysv_bytes(const char *optarg)
{
set_shm_sysv_bytes = true;
opt_shm_sysv_bytes = (size_t)get_uint64_byte(optarg);
check_range("shm-sysv-bytes", opt_shm_sysv_bytes,
MIN_SHM_SYSV_BYTES, MAX_SHM_SYSV_BYTES);
}
void stress_set_shm_sysv_segments(const char *optarg)
{
opt_shm_sysv_segments = true;
opt_shm_sysv_segments = (size_t)get_uint64_byte(optarg);
check_range("shm-sysv-segments", opt_shm_sysv_segments,
MIN_SHM_SYSV_SEGMENTS, MAX_SHM_SYSV_SEGMENTS);
}
/*
* stress_shm_sysv_check()
* simple check if shared memory is sane
*/
static int stress_shm_sysv_check(
uint8_t *buf,
const size_t sz,
const size_t page_size)
{
uint8_t *ptr, *end = buf + sz;
uint8_t val;
for (val = 0, ptr = buf; ptr < end; ptr += page_size, val++) {
*ptr = val;
}
for (val = 0, ptr = buf; ptr < end; ptr += page_size, val++) {
if (*ptr != val)
return -1;
}
return 0;
}
/*
* handle_shm_sysv_sigalrm()
* catch SIGALRM, flag termination
*/
static MLOCKED void handle_shm_sysv_sigalrm(int dummy)
{
(void)dummy;
opt_do_run = false;
}
/*
* stress_shm_sysv_child()
* stress out the shm allocations. This can be killed by
* the out of memory killer, so we need to keep the parent
* informed of the allocated shared memory ids so these can
* be reaped cleanly if this process gets prematurely killed.
*/
static int stress_shm_sysv_child(
const int fd,
uint64_t *const counter,
const uint64_t max_ops,
const char *name,
const size_t max_sz,
const size_t page_size)
{
struct sigaction new_action;
void *addrs[MAX_SHM_SYSV_SEGMENTS];
key_t keys[MAX_SHM_SYSV_SEGMENTS];
int shm_ids[MAX_SHM_SYSV_SEGMENTS];
shm_msg_t msg;
size_t i;
int rc = EXIT_SUCCESS;
bool ok = true;
int mask = ~0;
int instances;
new_action.sa_handler = handle_shm_sysv_sigalrm;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
if (sigaction(SIGALRM, &new_action, NULL) < 0) {
pr_fail_err(name, "sigaction");
return EXIT_FAILURE;
}
memset(addrs, 0, sizeof(addrs));
memset(keys, 0, sizeof(keys));
for (i = 0; i < MAX_SHM_SYSV_SEGMENTS; i++)
shm_ids[i] = -1;
/* Make sure this is killable by OOM killer */
set_oom_adjustment(name, true);
if ((instances = stressor_instances(STRESS_SHM_SYSV)) < 1)
instances = (int)stress_get_processors_configured();
/* Should never happen, but be safe */
if (instances < 1)
instances = 1;
do {
size_t sz = max_sz;
for (i = 0; i < opt_shm_sysv_segments; i++) {
int shm_id, count = 0;
void *addr;
key_t key;
size_t shmall, freemem, totalmem;
/* Try hard not to overcommit at this current time */
stress_get_memlimits(&shmall, &freemem, &totalmem);
shmall /= instances;
freemem /= instances;
if ((shmall > page_size) && sz > shmall)
sz = shmall;
if ((freemem > page_size) && sz > freemem)
sz = freemem;
if (!opt_do_run)
goto reap;
for (count = 0; count < KEY_GET_RETRIES; count++) {
bool unique = true;
const int rnd =
mwc32() % SIZEOF_ARRAY(shm_flags);
const int rnd_flag = shm_flags[rnd] & mask;
if (sz < page_size)
goto reap;
/* Get a unique key */
do {
size_t j;
if (!opt_do_run)
goto reap;
/* Get a unique random key */
key = (key_t)mwc16();
for (j = 0; j < i - 1; j++) {
if (key == keys[j]) {
unique = false;
break;
}
}
if (!opt_do_run)
goto reap;
} while (!unique);
shm_id = shmget(key, sz,
IPC_CREAT | IPC_EXCL |
S_IRUSR | S_IWUSR | rnd_flag);
if (shm_id >= 0)
break;
if (errno == EINTR)
goto reap;
if (errno == EPERM) {
/* ignore using the flag again */
mask &= ~rnd_flag;
}
if ((errno == EINVAL) || (errno == ENOMEM)) {
/*
* On some systems we may need
* to reduce the size
*/
sz = sz / 2;
}
}
if (shm_id < 0) {
ok = false;
pr_fail(stderr, "%s: shmget failed: errno=%d (%s)\n",
name, errno, strerror(errno));
rc = EXIT_FAILURE;
goto reap;
}
/* Inform parent of the new shm ID */
msg.index = i;
msg.shm_id = shm_id;
if (write(fd, &msg, sizeof(msg)) < 0) {
pr_err(stderr, "%s: write failed: errno=%d: (%s)\n",
name, errno, strerror(errno));
rc = EXIT_FAILURE;
goto reap;
}
addr = shmat(shm_id, NULL, 0);
if (addr == (char *) -1) {
ok = false;
pr_fail(stderr, "%s: shmat failed: errno=%d (%s)\n",
name, errno, strerror(errno));
rc = EXIT_FAILURE;
goto reap;
}
addrs[i] = addr;
shm_ids[i] = shm_id;
keys[i] = key;
if (!opt_do_run)
goto reap;
(void)mincore_touch_pages(addr, sz);
if (!opt_do_run)
goto reap;
(void)madvise_random(addr, sz);
if (!opt_do_run)
goto reap;
if (stress_shm_sysv_check(addr, sz, page_size) < 0) {
ok = false;
pr_fail(stderr, "%s: memory check failed\n", name);
rc = EXIT_FAILURE;
goto reap;
}
(*counter)++;
}
reap:
for (i = 0; i < opt_shm_sysv_segments; i++) {
if (addrs[i]) {
if (shmdt(addrs[i]) < 0) {
pr_fail(stderr, "%s: shmdt failed: errno=%d (%s)\n",
name, errno, strerror(errno));
}
}
if (shm_ids[i] >= 0) {
if (shmctl(shm_ids[i], IPC_RMID, NULL) < 0) {
if (errno != EIDRM)
pr_fail(stderr, "%s: shmctl failed: errno=%d (%s)\n",
name, errno, strerror(errno));
}
}
/* Inform parent shm ID is now free */
msg.index = i;
msg.shm_id = -1;
if (write(fd, &msg, sizeof(msg)) < 0) {
pr_dbg(stderr, "%s: write failed: errno=%d: (%s)\n",
name, errno, strerror(errno));
ok = false;
}
addrs[i] = NULL;
shm_ids[i] = -1;
keys[i] = 0;
}
} while (ok && opt_do_run && (!max_ops || *counter < max_ops));
/* Inform parent of end of run */
msg.index = -1;
msg.shm_id = -1;
if (write(fd, &msg, sizeof(msg)) < 0) {
pr_err(stderr, "%s: write failed: errno=%d: (%s)\n",
name, errno, strerror(errno));
rc = EXIT_FAILURE;
}
return rc;
}
/*
* stress_shm_sysv()
* stress SYSTEM V shared memory
*/
int stress_shm_sysv(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
const size_t page_size = stress_get_pagesize();
size_t orig_sz, sz;
int pipefds[2];
int rc = EXIT_SUCCESS;
ssize_t i;
pid_t pid;
bool retry = true;
uint32_t restarts = 0;
if (!set_shm_sysv_bytes) {
if (opt_flags & OPT_FLAGS_MAXIMIZE)
opt_shm_sysv_bytes = MAX_SHM_SYSV_BYTES;
if (opt_flags & OPT_FLAGS_MINIMIZE)
opt_shm_sysv_bytes = MIN_SHM_SYSV_BYTES;
}
if (!set_shm_sysv_segments) {
if (opt_flags & OPT_FLAGS_MAXIMIZE)
opt_shm_sysv_segments = MAX_SHM_SYSV_SEGMENTS;
if (opt_flags & OPT_FLAGS_MINIMIZE)
opt_shm_sysv_segments = MIN_SHM_SYSV_SEGMENTS;
}
orig_sz = sz = opt_shm_sysv_bytes & ~(page_size - 1);
while (opt_do_run && retry) {
if (pipe(pipefds) < 0) {
pr_fail_dbg(name, "pipe");
return EXIT_FAILURE;
}
fork_again:
pid = fork();
if (pid < 0) {
/* Can't fork, retry? */
if (errno == EAGAIN)
goto fork_again;
pr_err(stderr, "%s: fork failed: errno=%d: (%s)\n",
name, errno, strerror(errno));
(void)close(pipefds[0]);
(void)close(pipefds[1]);
/* Nope, give up! */
return EXIT_FAILURE;
} else if (pid > 0) {
/* Parent */
int status, shm_ids[MAX_SHM_SYSV_SEGMENTS];
ssize_t n;
setpgid(pid, pgrp);
set_oom_adjustment(name, false);
(void)close(pipefds[1]);
for (i = 0; i < (ssize_t)opt_shm_sysv_segments; i++)
shm_ids[i] = -1;
while (opt_do_run) {
shm_msg_t msg;
/*
* Blocking read on child shm ID info
* pipe. We break out if pipe breaks
* on child death, or child tells us
* off its demise.
*/
n = read(pipefds[0], &msg, sizeof(msg));
if (n <= 0) {
if ((errno == EAGAIN) || (errno == EINTR))
continue;
if (errno) {
pr_fail_dbg(name, "read");
break;
}
pr_fail_dbg(name, "zero byte read");
break;
}
if ((msg.index < 0) ||
(msg.index >= MAX_SHM_SYSV_SEGMENTS)) {
retry = false;
break;
}
shm_ids[msg.index] = msg.shm_id;
}
(void)kill(pid, SIGALRM);
(void)waitpid(pid, &status, 0);
if (WIFSIGNALED(status)) {
if ((WTERMSIG(status) == SIGKILL) ||
(WTERMSIG(status) == SIGBUS)) {
pr_dbg(stderr, "%s: assuming killed by OOM killer, "
"restarting again (instance %d)\n",
name, instance);
restarts++;
}
}
(void)close(pipefds[1]);
/*
* The child may have been killed by the OOM killer or
* some other way, so it may have left the shared
* memory segment around. At this point the child
* has died, so we should be able to remove the
* shared memory segment.
*/
for (i = 0; i < (ssize_t)opt_shm_sysv_segments; i++) {
if (shm_ids[i] != -1)
(void)shmctl(shm_ids[i], IPC_RMID, NULL);
}
} else if (pid == 0) {
/* Child, stress memory */
setpgid(0, pgrp);
stress_parent_died_alarm();
/*
* Nicing the child may OOM it first as this
* doubles the OOM score
*/
if (nice(5) < 0)
pr_dbg(stderr, "%s: nice of child failed, "
"(instance %d)\n", name, instance);
(void)close(pipefds[0]);
rc = stress_shm_sysv_child(pipefds[1], counter,
max_ops, name, sz, page_size);
(void)close(pipefds[1]);
_exit(rc);
}
}
if (orig_sz != sz)
pr_dbg(stderr, "%s: reduced shared memory size from "
"%zu to %zu bytes\n", name, orig_sz, sz);
if (restarts) {
pr_dbg(stderr, "%s: OOM restarts: %" PRIu32 "\n",
name, restarts);
}
return rc;
}