Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More psi-monitor info #400

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions psi-monitor/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AM_CFLAGS = -Wall -Werror

sbin_PROGRAMS = psi-monitor
psi_monitor_SOURCES = psi-monitor.c

Expand Down
48 changes: 42 additions & 6 deletions psi-monitor/psi-monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include <err.h>
#include <unistd.h>
#include <string.h>

#define DEBUG false
#include <getopt.h>

/* Daemon parameters */
#define POLL_INTERVAL 5
Expand All @@ -19,6 +18,23 @@
#define PSI_MEMORY_FILE "/proc/pressure/memory"
#define BUFSIZE 256

static bool opt_debug = false;
static const char *short_options = "dh";
static struct option long_options[] = {
{"debug", 0, 0, 'd'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};

static void usage(const char *progname) {
printf("Usage: %s [OPTION]...\n"
"Invoke out of memory killer on excessive memory pressure.\n"
"\n"
" -d, --debug\tprint debugging messages\n"
" -h, --help\tdisplay this help and exit\n",
progname);
}

static ssize_t fstr(const char *path, char *rbuf, const char *wbuf) {
int fd;
ssize_t n;
Expand Down Expand Up @@ -46,12 +62,28 @@ static ssize_t fstr(const char *path, char *rbuf, const char *wbuf) {
}

static void sysrq_trigger_oom() {
printf("Above threshold limit, killing task and pausing for recovery\n");
fstr(SYSRQ_TRIGGER_FILE, NULL, "f");
sleep(RECOVERY_INTERVAL);
}

int main(int argc, char **argv) {
while (true) {
int c = getopt_long(argc, argv, short_options, long_options, NULL);
if (c == -1)
break;

switch (c) {
case 'd':
opt_debug = true;
break;
case 'h':
usage(argv[0]);
return 0;
default:
return 1;
}
}

setvbuf(stdout, NULL, _IOLBF, 0);
printf("poll_interval=%ds, recovery_interval=%ds, stall_threshold=%d%%\n",
POLL_INTERVAL, RECOVERY_INTERVAL, MEM_THRESHOLD);
Expand All @@ -68,12 +100,16 @@ int main(int argc, char **argv) {
i+=11; /* skip "full avg10=" */

sscanf(buf+i, "%f", &full_avg10);
if (DEBUG) printf("full_avg10=%f\n", full_avg10);
if (opt_debug) printf("full_avg10=%f\n", full_avg10);

if (full_avg10 > MEM_THRESHOLD)
if (full_avg10 > MEM_THRESHOLD) {
printf("Memory pressure %.1f%% above threshold limit %d%%, "
"killing task and pausing %d seconds for recovery\n",
full_avg10, MEM_THRESHOLD, RECOVERY_INTERVAL);
sysrq_trigger_oom();
else
} else {
sleep(POLL_INTERVAL);
}
}

return 0;
Expand Down