Skip to content

Commit d062b93

Browse files
committed
membomb: add mmap variant
1 parent 017adca commit d062b93

File tree

7 files changed

+126
-9
lines changed

7 files changed

+126
-9
lines changed

contrib/membomb/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/membomb
22
/membomb.subthread
33
/membomb.mmap
4+
/keep_active
5+
/membomb.mmap_anon
6+
/membomb.mmap_file

contrib/membomb/Makefile

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

33
.PHONY: all
4-
all: membomb membomb.subthread membomb.mmap
4+
all: membomb membomb.subthread membomb.mmap_anon membomb.mmap_file keep_active
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
12+
membomb.mmap_anon: $(wildcard *.c *.h) Makefile
13+
$(CC) $(CFLAGS) -lpthread -o $@ eat_all_memory.c membomb.mmap_anon.c
14+
15+
membomb.mmap_file: $(wildcard *.c *.h) Makefile
16+
$(CC) $(CFLAGS) -lpthread -o $@ eat_all_memory.c membomb.mmap_file.c
17+
18+
keep_active: $(wildcard *.c *.h) Makefile
19+
$(CC) $(CFLAGS) -lpthread -o $@ keep_active.c
1420

1521
.PHONY: format
1622
format:
1723
clang-format --style=file -i *.c
1824

1925
.PHONY: clean
2026
clean:
21-
rm -f membomb membomb.subthread membomb.mmap
27+
rm -f membomb membomb.subthread membomb.mmap_anon membomb.mmap_file keep_active

contrib/membomb/eat_all_memory.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <errno.h>
2+
#include <fcntl.h>
13
#include <signal.h>
24
#include <stdio.h>
35
#include <stdlib.h>
@@ -7,8 +9,6 @@
79

810
#include "eat_all_memory.h"
911

10-
#define NUM_PAGES 10
11-
1212
static void handle_sigterm(int sig)
1313
{
1414
printf("blocking SIGTERM %d\n", sig);
@@ -17,7 +17,11 @@ static void handle_sigterm(int sig)
1717
void eat_all_memory(eat_how_enum eat_how)
1818
{
1919
long page_size = sysconf(_SC_PAGESIZE);
20-
long bs = page_size * NUM_PAGES;
20+
long num_pages = 10;
21+
if (eat_how == EAT_MMAP_FILE) {
22+
num_pages = 10000;
23+
}
24+
long bs = page_size * num_pages;
2125
long cnt = 0, last_sum = 0;
2226
struct timeval tv1;
2327
signal(SIGTERM, handle_sigterm);
@@ -39,16 +43,42 @@ void eat_all_memory(eat_how_enum eat_how)
3943
continue;
4044
}
4145
break;
46+
case EAT_MMAP_FILE:
47+
char tmp_path[] = "/var/tmp/membomb.mmap_file.XXXXXX";
48+
int fd = mkstemp(tmp_path);
49+
if (fd == -1) {
50+
perror("mkstemp failed");
51+
sleep(1);
52+
continue;
53+
}
54+
int ret = unlink(tmp_path);
55+
if (ret) {
56+
perror("unlink failed");
57+
}
58+
if (ret) {
59+
errno = ret;
60+
perror("ftruncate failed");
61+
sleep(1);
62+
continue;
63+
}
64+
p = mmap(NULL, bs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
65+
if (p == MAP_FAILED) {
66+
perror("mmap failed");
67+
sleep(1);
68+
continue;
69+
}
70+
break;
4271
default:
4372
fprintf(stderr, "BUG: unknown eat_how=%d\n", eat_how);
4473
exit(1);
4574
}
46-
for (int i = 0; i < NUM_PAGES; i++) {
75+
for (int i = 0; i < num_pages; i++) {
4776
// Write to each page so the kernel really has to allocate it.
4877
p[i * page_size] = 0xab;
4978
}
5079
cnt++;
51-
if (cnt % 1000 == 0) {
80+
const int cnt_per_100mb = 100 * 1024 * 1024 / bs;
81+
if (cnt % cnt_per_100mb == 0) {
5282
long sum = bs * cnt / 1024 / 1024;
5383
struct timeval tv2;
5484
gettimeofday(&tv2, NULL);

contrib/membomb/eat_all_memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
typedef enum {
22
EAT_MALLOC,
33
EAT_MMAP_ANON,
4+
EAT_MMAP_FILE,
45
} eat_how_enum;
56

67
void eat_all_memory(eat_how_enum eat_how);

contrib/membomb/keep_active.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <errno.h>
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <sys/mman.h>
6+
#include <sys/time.h>
7+
#include <unistd.h>
8+
9+
int main()
10+
{
11+
const long gigabyte = 1024 * 1024 * 1024;
12+
const long size = 10 * gigabyte;
13+
char tmp_path[] = "/var/tmp/keep_active.XXXXXX";
14+
int fd = mkstemp(tmp_path);
15+
if (fd == -1) {
16+
perror("mkstemp failed");
17+
exit(1);
18+
}
19+
int ret = unlink(tmp_path);
20+
if (ret) {
21+
perror("unlink failed");
22+
exit(1);
23+
}
24+
ret = posix_fallocate(fd, 0, size);
25+
if (ret) {
26+
errno = ret;
27+
perror("posix_fallocate failed");
28+
exit(1);
29+
}
30+
fsync(fd);
31+
32+
printf("Allocated %ld GiB\n", size / gigabyte);
33+
printf("Spinning on file reads...\n");
34+
struct timeval tv1;
35+
gettimeofday(&tv1, NULL);
36+
while (1) {
37+
long increment = sysconf(_SC_PAGESIZE);
38+
char buf[1];
39+
for (long off = 0; off < size; off += increment) {
40+
ret = pread(fd, buf, sizeof(buf), off);
41+
if (ret <= 0) {
42+
perror("pread");
43+
exit(1);
44+
}
45+
}
46+
// Print stats
47+
struct timeval tv2;
48+
gettimeofday(&tv2, NULL);
49+
long delta = tv2.tv_sec - tv1.tv_sec;
50+
// Convert to microseconds
51+
delta *= 1000000;
52+
// Add microsecond delta
53+
delta = delta + tv2.tv_usec - tv1.tv_usec;
54+
// Byte-per-Microsecond = MB/s
55+
long mbps = size / delta;
56+
printf("%4ld MiB (%4ld MiB/s)\n", size / 1024 / 1024, mbps);
57+
gettimeofday(&tv1, NULL);
58+
}
59+
}
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 on normal file\n");
17+
eat_all_memory(EAT_MMAP_FILE);
18+
}

0 commit comments

Comments
 (0)