-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemstop.c
More file actions
106 lines (85 loc) · 2.66 KB
/
memstop.c
File metadata and controls
106 lines (85 loc) · 2.66 KB
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
/*
memstop - A LD_PRELOAD shared object that delays execution when memory is low.
Copyright (C) 2025 Dr. Sebastian Urban
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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct MemoryInfo {
long total_kb;
long available_kb;
};
static int get_memory_info(struct MemoryInfo *mem_info) {
mem_info->total_kb = -1;
mem_info->available_kb = -1;
FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL)
return -1;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "MemTotal:", 9) == 0)
sscanf(line, "MemTotal: %ld kB", &mem_info->total_kb);
else if (strncmp(line, "MemAvailable:", 13) == 0)
sscanf(line, "MemAvailable: %ld kB", &mem_info->available_kb);
if (mem_info->total_kb != -1 && mem_info->available_kb != -1)
break;
}
fclose(fp);
if (mem_info->total_kb == -1 || mem_info->available_kb == -1)
return -1;
return 0;
}
void __attribute__((constructor)) memstop(void) {
struct MemoryInfo memory;
long required_kb;
long percent = 10;
char *env;
bool verbose = false;
bool stats = false;
bool informed = false;
env = getenv("MEMSTOP_PERCENT");
if (env) {
percent = atol(env);
if (percent < 0)
percent = 0;
if (percent > 100)
percent = 100;
}
env = getenv("MEMSTOP_VERBOSE");
if (env)
verbose = true;
while (true) {
if (get_memory_info(&memory) != 0)
return;
if (verbose && !stats) {
fprintf(stderr,
"==== memstop: %ld%% required, %ld%% available (%ld MB / %ld MB) "
"====\n",
percent, memory.available_kb * 100 / memory.total_kb,
memory.available_kb / 1024, memory.total_kb / 1024);
stats = true;
}
required_kb = memory.total_kb * percent / 100;
if (memory.available_kb >= required_kb)
break;
if (verbose && !informed) {
fprintf(stderr, "==== memstop hold ====\n");
informed = true;
}
usleep(10000);
}
if (verbose && informed)
fprintf(stderr, "==== memstop release ====\n");
}