Skip to content

Commit

Permalink
kill: when we have swap space, give the kernel extra time
Browse files Browse the repository at this point in the history
With swap enabled, the kernel seems to need more than 100ms to free the memory
of the killed process. This means that earlyoom would immediately kill another
process. Sleep a little extra to give the kernel time to free the memory.
(Yes, this will sleep even if the kill has failed. Does no harm and keeps the
code simple.)
  • Loading branch information
rfjakob committed Jul 10, 2018
1 parent 081b42e commit bdac539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ uninstall-initscript: uninstall-bin
uninstall-bin:
rm -f $(DESTDIR)$(PREFIX)$(BINDIR)/earlyoom

format:
# Depends on earlyoom compilation to make sure the syntax is ok.
format: earlyoom
clang-format -i *.h *.c

test: earlyoom
Expand Down
13 changes: 12 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ int main(int argc, char* argv[])
perror("Could not set oom_score_adj to -1000 for earlyoom process - continuing anyway");
}

int tick_us = 100000; // 100 ms <=> 10 Hz
c = 0; // loop counter
report_interval = report_interval * 10; // loop runs at 10Hz
while (1) {
Expand All @@ -265,10 +266,20 @@ int main(int argc, char* argv[])
handle_oom(procdir, 9, kernel_oom_killer, ignore_oom_score_adj,
notif_command, prefer_regex, avoid_regex);
oom_cnt++;
// With swap enabled, the kernel seems to need more than 100ms to free the memory
// of the killed process. This means that earlyoom would immediately kill another
// process. Sleep a little extra to give the kernel time to free the memory.
// (Yes, this will sleep even if the kill has failed. Does no harm and keeps the
// code simple.)
if (m.SwapTotalMiB > 0) {
int sleep_cycles = 2;
usleep(sleep_cycles * tick_us);
c += sleep_cycles;
}
} else if (report_interval > 0 && c % report_interval == 0) {
print_mem_stats(stdout, m);
}
usleep(100000); // 100 ms <=> 10 Hz
usleep(tick_us);
c++;
}
return 0;
Expand Down

0 comments on commit bdac539

Please sign in to comment.