From 7feab2f53d52f0501b0d9961580f65af2d39f5be Mon Sep 17 00:00:00 2001 From: yinbinbin Date: Fri, 14 Jul 2023 21:17:26 +0800 Subject: [PATCH] oomkill: print cmdline Signed-off-by: yinbinbin --- source/tools/monitor/oomkill/kill.c | 4 +++ source/tools/monitor/oomkill/meminfo.c | 41 ++++++++++++++++++++++++++ source/tools/monitor/oomkill/meminfo.h | 1 + 3 files changed, 46 insertions(+) diff --git a/source/tools/monitor/oomkill/kill.c b/source/tools/monitor/oomkill/kill.c index 7000dc48..11e88db6 100644 --- a/source/tools/monitor/oomkill/kill.c +++ b/source/tools/monitor/oomkill/kill.c @@ -427,6 +427,8 @@ procinfo_t find_largest_process(const poll_loop_args_t* args) */ void kill_process(const poll_loop_args_t* args, int sig, const procinfo_t* victim) { + char cmdline[512]; + if (victim->pid <= 0) { warn("Could not find a process to kill. Sleeping 1 second.\n"); if (args->notify) { @@ -444,10 +446,12 @@ void kill_process(const poll_loop_args_t* args, int sig, const procinfo_t* victi } else if (sig == 0) { sig_name = "0 (no-op signal)"; } + get_cmdline(victim->pid, cmdline, sizeof(cmdline)); // sig == 0 is used as a self-test during startup. Don't notify the user. if (sig != 0 || enable_debug) { warn("sending %s to process %d uid %d \"%s\": badness %ld, VmRSS %lld MiB adj:%d\n", sig_name, victim->pid, victim->uid, victim->name, victim->badness, victim->VmRSSkiB / 1024, victim->oom_score_adj); + warn("process cmdline:%s\n", cmdline); } int res = kill_wait(args, victim->pid, sig); diff --git a/source/tools/monitor/oomkill/meminfo.c b/source/tools/monitor/oomkill/meminfo.c index 4f02e886..18bab6b1 100644 --- a/source/tools/monitor/oomkill/meminfo.c +++ b/source/tools/monitor/oomkill/meminfo.c @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include #include "globals.h" #include "meminfo.h" @@ -253,6 +256,44 @@ int get_comm(int pid, char* out, size_t outlen) return 0; } +int get_cmdline(int pid, char *out, int out_len) { + char cmdline_file[128] = {0}; + int fd; + int i; + ssize_t nread, total = 0; + + sprintf(cmdline_file, "%s/%d/cmdline", procdir_path, pid); + + fd = open(cmdline_file, O_RDONLY); + if (fd == -1) { + printf("Failed to open %s\n", cmdline_file); + return -1; + } + + while ((nread = read(fd, out + total, out_len - total - 1)) > 0) { + total += nread; + } + + close(fd); + + if (total == -1) { + printf("Failed to read %s\n", cmdline_file); + return -1; + } + + // Replace '\0' with spaces when arguments are not separated by '\0' + for (i = 0; i < total; i++) { + if (out[i] == '\0') { + out[i] = ' '; + } + } + + out[total] = '\0'; + + return 0; +} + + // Get the effective uid (EUID) of `pid`. // Returns the uid (>= 0) or -errno on error. int get_uid(int pid) diff --git a/source/tools/monitor/oomkill/meminfo.h b/source/tools/monitor/oomkill/meminfo.h index 22df1ef5..4572b402 100644 --- a/source/tools/monitor/oomkill/meminfo.h +++ b/source/tools/monitor/oomkill/meminfo.h @@ -61,6 +61,7 @@ int get_oom_score(int pid); int get_oom_score_adj(const int pid, int* out); long long get_vm_rss_kib(int pid); int get_comm(int pid, char* out, size_t outlen); +int get_cmdline(int pid, char *out, int out_len); int get_uid(int pid); #endif -- Gitee