[sheepdog] [PATCH v4] sheep: avoid the OOM killer

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Fri Jul 5 08:30:10 CEST 2013


Because sheep provides virtual disks to many VMs, sheep is an
important process and it shouldn't be killed by the OOM killer of
Linux. This patch implements a mechanism for avoiding the OOM killer.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---

v4:
 - rename the label: end -> out
 - updating the comment for describing the behavior of the OOM killer

v3:
 - renaming the function: avoid_oom_killer() -> disable_oom_killing()
 - include <linux/oom.h> for the constant OOM_SCORE_ADJ_MIN
 - move the function to lib/util.c for other programs like shepherd

v2:
 - remove the meaningless comment
 - add a description for the value -1000

 include/util.h |    1 +
 lib/util.c     |   32 ++++++++++++++++++++++++++++++++
 sheep/sheep.c  |    4 ++++
 3 files changed, 37 insertions(+)

diff --git a/include/util.h b/include/util.h
index e70bb9f..26e4bd9 100644
--- a/include/util.h
+++ b/include/util.h
@@ -102,6 +102,7 @@ void trim_zero_blocks(void *buf, uint64_t *offset, uint32_t *len);
 void untrim_zero_blocks(void *buf, uint64_t offset, uint32_t len,
 			uint32_t requested_len);
 int atomic_create_and_write(const char *path, char *buf, size_t len);
+int disable_oom_killing(void);
 
 /* a type safe version of qsort() */
 #define xqsort(base, nmemb, compar)					\
diff --git a/lib/util.c b/lib/util.c
index 6fd3817..ee43674 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -23,6 +23,7 @@
 #include <signal.h>
 #include <sys/xattr.h>
 #include <fcntl.h>
+#include <linux/oom.h>
 
 #include "util.h"
 #include "logger.h"
@@ -539,3 +540,34 @@ close_fd:
 end:
 	return ret;
 }
+
+int disable_oom_killing(void)
+{
+	int fd, ret = 0;
+	char path[PATH_MAX], score_str[8];
+
+	/*
+	 * Processes of Linux which wrote a string representation of
+	 * OOM_SCORE_ADJ_MIN to /proc/<their pid>/oom_score_adj are excluded by
+	 * the target list of the OOM killer certainly.
+	 */
+
+	snprintf(path, PATH_MAX, "/proc/%d/oom_score_adj", getpid());
+	fd = open(path, O_WRONLY);
+	if (fd < 0) {
+		sd_eprintf("opening %s failed, %m", path);
+		ret = -1;
+		goto out;
+	}
+
+	snprintf(score_str, 8, "%d\n", OOM_SCORE_ADJ_MIN);
+
+	if (xwrite(fd, score_str, strlen(score_str)) != 6) {
+		sd_eprintf("writing to %s failed, %m", path);
+		ret = -1;
+	}
+
+	close(fd);
+out:
+	return ret;
+}
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 84bd269..7852c14 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -419,6 +419,10 @@ static void check_host_env(void)
 	 * sized request.
 	 */
 	mallopt(M_MMAP_THRESHOLD, 512 * 1024);
+
+	if (disable_oom_killing() < 0)
+		sd_eprintf("WARN: adjusting OOM failed, this process has a"
+			" possibility of being killed by the OOM killer");
 }
 
 static int lock_and_daemon(bool daemonize, const char *base_dir)
-- 
1.7.10.4




More information about the sheepdog mailing list