[LTP] [PATCH v3 1/3] lib: add functions to adjust oom score
Li Wang
liwang@redhat.com
Mon Dec 20 10:54:14 CET 2021
This introduces function to LTP for adjusting the oom_score_adj of
target process, which may be helpful in OOM tests to prevent kernel
killing the main or lib process during test running.
The exported global tst_enable_oom_protection function can be used
at anywhere you want to protect, but please remember that if you
do enable protection on a process($PID) that all the children will
inherit its score and be ignored by OOM Killer as well. So that's
why tst_disable_oom_protection is recommended to combination in use.
Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
Notes:
v2 --> v3
* rename to tst_disable_oom_protection
* support set PID as 0 to protect current process
include/tst_memutils.h | 20 ++++++++++++++++++++
lib/tst_memutils.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index f605f544e..a5265423d 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -25,4 +25,24 @@ void tst_pollute_memory(size_t maxsize, int fillchar);
*/
long long tst_available_mem(void);
+/*
+ * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
+ * echo -1000 >/proc/$PID/oom_score_adj
+ * If the pid is 0 which means it will set on current(self) process.
+ *
+ * Note:
+ * This exported tst_enable_oom_protection function can be used at anywhere
+ * you want to protect, but please remember that if you do enable protection
+ * on a process($PID) that all the children will inherit its score and be
+ * ignored by OOM Killer as well. So that's why tst_disable_oom_protection
+ * is recommended to combination in use.
+ */
+void tst_enable_oom_protection(pid_t pid);
+
+/*
+ * Disable the OOM protection for the process($PID).
+ * echo 0 >/proc/$PID/oom_score_adj
+ */
+void tst_disable_oom_protection(pid_t pid);
+
#endif /* TST_MEMUTILS_H__ */
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index bd09cf6fa..0757fe654 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -3,6 +3,7 @@
* Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
*/
+#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/sysinfo.h>
@@ -91,3 +92,34 @@ long long tst_available_mem(void)
return mem_available;
}
+
+static void set_oom_score_adj(pid_t pid, int value)
+{
+ int val;
+ char score_path[64];
+
+ if (access("/proc/self/oom_score_adj", F_OK) == -1) {
+ tst_res(TINFO, "Warning: oom_score_adj does not exist");
+ return;
+ }
+
+ if (pid == 0)
+ sprintf(score_path, "/proc/self/oom_score_adj");
+ else
+ sprintf(score_path, "/proc/%d/oom_score_adj", pid);
+
+ SAFE_FILE_PRINTF(score_path, "%d", value);
+ SAFE_FILE_SCANF(score_path, "%d", &val);
+ if (val != value)
+ tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
+}
+
+void tst_enable_oom_protection(pid_t pid)
+{
+ set_oom_score_adj(pid, -1000);
+}
+
+void tst_disable_oom_protection(pid_t pid)
+{
+ set_oom_score_adj(pid, 0);
+}
--
2.31.1
More information about the ltp
mailing list