Skip to content

Commit

Permalink
added option -a to sum up minimums in KiB and percentages for memory …
Browse files Browse the repository at this point in the history
…and swap instead of choosing lower
  • Loading branch information
mars4science committed Jan 20, 2024
1 parent 90f1a67 commit 9bd0d2d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
7 changes: 5 additions & 2 deletions MANPAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Use the same value for PERCENT and KILL_PERCENT if you always want to use SIGKIL
#### -M SIZE[,KILL_SIZE]
As an alternative to specifying a percentage of total memory, `-M` sets
the available memory minimum to SIZE KiB. The value is internally converted
to a percentage. If you pass both `-M` and `-m`, the lower value is used.
to a percentage. If you pass both `-M` and `-m`, the lower value is used by default.
Example: Reserve 10% of RAM but at most 1 GiB:

earlyoom -m 10 -M 1048576
Expand All @@ -63,10 +63,13 @@ earlyoom sends SIGKILL if at or below KILL_SIZE (default SIZE/2), otherwise SIGT
#### -S SIZE[,KILL_SIZE]
As an alternative to specifying a percentage of total swap, `-S` sets
the free swap minimum to SIZE KiB. The value is internally converted
to a percentage. If you pass both `-S` and `-s`, the lower value is used.
to a percentage. If you pass both `-S` and `-s`, the lower value is used by default.

Send SIGKILL if at or below KILL_SIZE (default SIZE/2), otherwise SIGTERM.

#### -a
To sum up values from KiB and from percentages of memory and swap instead of choosing lower.

#### -k
removed in earlyoom v1.2, ignored for compatibility

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Command line options
earlyoom v1.6.2-34-g75a8852-dirty
Usage: ./earlyoom [OPTION]...
-a sum up minimums in KiB and PERCENT
-m PERCENT[,KILL_PERCENT] set available memory minimum to PERCENT of total
(default 10 %).
earlyoom sends SIGTERM once below PERCENT, then
Expand Down Expand Up @@ -279,6 +280,8 @@ Changelog
([commit](https://github.com/rfjakob/earlyoom/commit/459d76296d3d0a0b59ee1e2e48ad2271429de916))
* Use `process_mrelease` ([#266](https://github.com/rfjakob/earlyoom/issues/266))
* Support `NO_COLOR` (https://no-color.org/)
* Allow summing up `-M` and `-m`, and `-S` and `-s` (`-a` option) instead of using the
lower value.

* v1.7, 2022-03-05
* Add `-N` flag to run a script every time a process is killed ([commit](https://github.com/rfjakob/earlyoom/commit/afe03606f077a1a17e6fbc238400b3ce7a9ef2be),
Expand Down
35 changes: 27 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int main(int argc, char* argv[])
meminfo_t m = parse_meminfo();

int c;
const char* short_opt = "m:s:M:S:kingN:dvr:ph";
const char* short_opt = "m:s:M:S:kingN:dvr:ph:a";
struct option long_opt[] = {
{ "prefer", required_argument, NULL, LONG_OPT_PREFER },
{ "avoid", required_argument, NULL, LONG_OPT_AVOID },
Expand All @@ -138,7 +138,7 @@ int main(int argc, char* argv[])
{ "help", no_argument, NULL, 'h' },
{ 0, 0, NULL, 0 } /* end-of-array marker */
};
bool have_m = 0, have_M = 0, have_s = 0, have_S = 0;
bool have_m = 0, have_M = 0, have_s = 0, have_S = 0, have_a = 0;
double mem_term_kib = 0, mem_kill_kib = 0, swap_term_kib = 0, swap_kill_kib = 0;

while ((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
Expand All @@ -149,6 +149,10 @@ int main(int argc, char* argv[])
case -1: /* no more arguments */
case 0: /* long option toggles */
break;
case 'a':
// to sum up minimums in KiB and percentages for memory and swap instead of choosing lower.
have_a = 1;
break;
case 'm':
// Use 99 as upper limit. Passing "-m 100" makes no sense.
tuple = parse_term_kill_tuple(optarg, 99);
Expand Down Expand Up @@ -251,6 +255,7 @@ int main(int argc, char* argv[])
fprintf(stderr,
"Usage: %s [OPTION]...\n"
"\n"
" -a sum up minimums in KiB and PERCENT\n"
" -m PERCENT[,KILL_PERCENT] set available memory minimum to PERCENT of total\n"
" (default 10 %%).\n"
" earlyoom sends SIGTERM once below PERCENT, then\n"
Expand Down Expand Up @@ -294,9 +299,16 @@ int main(int argc, char* argv[])
double M_term_percent = 100 * mem_term_kib / (double)m.MemTotalKiB;
double M_kill_percent = 100 * mem_kill_kib / (double)m.MemTotalKiB;
if (have_m) {
// Both -m and -M were passed. Use the lower of both values.
args.mem_term_percent = min(args.mem_term_percent, M_term_percent);
args.mem_kill_percent = min(args.mem_kill_percent, M_kill_percent);
// Both -m and -M were passed.
if (have_a) {
// sum up -m and -M values.
args.mem_term_percent = args.mem_term_percent + M_term_percent;
args.mem_kill_percent = args.mem_kill_percent + M_kill_percent;
} else {
// Use the lower of both values.
args.mem_term_percent = min(args.mem_term_percent, M_term_percent);
args.mem_kill_percent = min(args.mem_kill_percent, M_kill_percent);
}
} else {
// Only -M was passed.
args.mem_term_percent = M_term_percent;
Expand All @@ -308,9 +320,16 @@ int main(int argc, char* argv[])
double S_term_percent = 100 * swap_term_kib / (double)m.SwapTotalKiB;
double S_kill_percent = 100 * swap_kill_kib / (double)m.SwapTotalKiB;
if (have_s) {
// Both -s and -S were passed. Use the lower of both values.
args.swap_term_percent = min(args.swap_term_percent, S_term_percent);
args.swap_kill_percent = min(args.swap_kill_percent, S_kill_percent);
// Both -s and -S were passed.
if (have_a) {
// sum up -s and -S values.
args.swap_term_percent = args.swap_term_percent + S_term_percent;
args.swap_kill_percent = args.swap_kill_percent + S_kill_percent;
} else {
// Use the lower of both values.
args.swap_term_percent = min(args.swap_term_percent, S_term_percent);
args.swap_kill_percent = min(args.swap_kill_percent, S_kill_percent);
}
} else {
// Only -S was passed.
args.swap_term_percent = S_term_percent;
Expand Down

0 comments on commit 9bd0d2d

Please sign in to comment.