Skip to content

Commit 017adca

Browse files
committed
membomb: add membomb.mmap
Created for #320 but does not actually repro the issue.
1 parent 73b713b commit 017adca

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

contrib/membomb/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/membomb
22
/membomb.subthread
3+
/membomb.mmap

contrib/membomb/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
CFLAGS += -Wall -Wextra -g -fstack-protector-all -std=gnu99
22

33
.PHONY: all
4-
all: membomb membomb.subthread
4+
all: membomb membomb.subthread membomb.mmap
55

66
membomb : $(wildcard *.c *.h) Makefile
77
$(CC) $(CFLAGS) -o $@ eat_all_memory.c membomb.c
88

99
membomb.subthread: $(wildcard *.c *.h) Makefile
1010
$(CC) $(CFLAGS) -lpthread -o $@ eat_all_memory.c membomb.subthread.c
1111

12+
membomb.mmap: $(wildcard *.c *.h) Makefile
13+
$(CC) $(CFLAGS) -lpthread -o $@ eat_all_memory.c membomb.mmap.c
14+
1215
.PHONY: format
1316
format:
1417
clang-format --style=file -i *.c
1518

1619
.PHONY: clean
1720
clean:
18-
rm -f membomb membomb.subthread
21+
rm -f membomb membomb.subthread membomb.mmap

contrib/membomb/eat_all_memory.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#include <sys/time.h>
66
#include <unistd.h>
77

8+
#include "eat_all_memory.h"
9+
810
#define NUM_PAGES 10
911

1012
static void handle_sigterm(int sig)
1113
{
1214
printf("blocking SIGTERM %d\n", sig);
1315
}
1416

15-
void eat_all_memory()
17+
void eat_all_memory(eat_how_enum eat_how)
1618
{
1719
long page_size = sysconf(_SC_PAGESIZE);
1820
long bs = page_size * NUM_PAGES;
@@ -21,10 +23,25 @@ void eat_all_memory()
2123
signal(SIGTERM, handle_sigterm);
2224
gettimeofday(&tv1, NULL);
2325
while (1) {
24-
char* p = malloc(bs);
25-
if (!p) {
26-
printf("malloc failed\n");
27-
continue;
26+
char* p = NULL;
27+
switch (eat_how) {
28+
case EAT_MALLOC:
29+
p = malloc(bs);
30+
if (!p) {
31+
perror("malloc failed");
32+
continue;
33+
}
34+
break;
35+
case EAT_MMAP_ANON:
36+
p = mmap(NULL, bs, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37+
if (p == MAP_FAILED) {
38+
perror("mmap failed");
39+
continue;
40+
}
41+
break;
42+
default:
43+
fprintf(stderr, "BUG: unknown eat_how=%d\n", eat_how);
44+
exit(1);
2845
}
2946
for (int i = 0; i < NUM_PAGES; i++) {
3047
// Write to each page so the kernel really has to allocate it.

contrib/membomb/eat_all_memory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
void eat_all_memory();
1+
typedef enum {
2+
EAT_MALLOC,
3+
EAT_MMAP_ANON,
4+
} eat_how_enum;
5+
6+
void eat_all_memory(eat_how_enum eat_how);

contrib/membomb/membomb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* This file is part of the earlyoom project: https://github.com/rfjakob/earlyoom
88
*/
99

10+
#include <stdio.h>
11+
1012
#include "eat_all_memory.h"
1113

1214
int main()
1315
{
14-
eat_all_memory();
16+
printf("Using malloc\n");
17+
eat_all_memory(EAT_MALLOC);
1518
}

contrib/membomb/membomb.mmap.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
/* Use up all memory that we can get, as fast as we can.
4+
* As progress information, prints how much memory we already
5+
* have.
6+
*
7+
* This file is part of the earlyoom project: https://github.com/rfjakob/earlyoom
8+
*/
9+
10+
#include <stdio.h>
11+
12+
#include "eat_all_memory.h"
13+
14+
int main()
15+
{
16+
printf("Using mmap MAP_ANONYMOUS\n");
17+
eat_all_memory(EAT_MMAP_ANON);
18+
}

contrib/membomb/membomb.subthread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
void* eat_all_memory_thread(__attribute__((__unused__)) void* arg)
1818
{
1919
printf("sub thread = pid %d\n", gettid());
20-
eat_all_memory();
20+
eat_all_memory(EAT_MALLOC);
2121
return NULL;
2222
}
2323

0 commit comments

Comments
 (0)