[Nbd] [PATCH] nbd-client: adjust the oom-killer score in swap mode
- To: nbd-general@lists.sourceforge.net
- Subject: [Nbd] [PATCH] nbd-client: adjust the oom-killer score in swap mode
- From: "Dmitry V. Levin" <ldv@...1147...>
- Date: Fri, 22 Jun 2012 00:59:22 +0400
- Message-id: <20120621205921.GA25989@...1147...>
In swap mode, it is highly desirable to take all available measures that
could help nbd-client survival in low memory conditions. One of such
measures is to disable oom killing of nbd-client by adjusting the
oom-killer score.
Signed-off-by: Dmitry V. Levin <ldv@...1147...>
---
man/nbd-client.8.in.sgml | 5 +++--
nbd-client.c | 26 ++++++++++++++++++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/man/nbd-client.8.in.sgml b/man/nbd-client.8.in.sgml
index 4f24a61..8c862a1 100644
--- a/man/nbd-client.8.in.sgml
+++ b/man/nbd-client.8.in.sgml
@@ -211,8 +211,9 @@ manpage.1: manpage.sgml
<listitem>
<para>Specifies that this NBD device will be used as
swapspace. This option attempts to prevent deadlocks by
- performing mlockall() at an appropriate time. It does not
- however guarantee that such deadlocks can be avoided.</para>
+ performing mlockall() and adjusting the oom-killer score
+ at an appropriate time. It does not however guarantee
+ that such deadlocks can be avoided.</para>
</listitem>
</varlistentry>
<varlistentry>
diff --git a/nbd-client.c b/nbd-client.c
index 2321e7b..5526db0 100644
--- a/nbd-client.c
+++ b/nbd-client.c
@@ -372,6 +372,23 @@ void finish_sock(int sock, int nbd, int swap) {
mlockall(MCL_CURRENT | MCL_FUTURE);
}
+#ifdef __linux__
+static int
+oom_adjust(const char *file, const char *value)
+{
+ int fd, rc;
+ size_t len;
+
+ fd = open(file, O_WRONLY);
+ if (fd < 0)
+ return -1;
+ len = strlen(value);
+ rc = write(fd, value, len) != (ssize_t) len;
+ close(fd);
+ return rc ? -1 : 0;
+}
+#endif
+
void usage(char* errmsg, ...) {
if(errmsg) {
char tmp[256];
@@ -561,6 +578,15 @@ int main(int argc, char *argv[]) {
setsizes(nbd, size64, blocksize, flags);
set_timeout(nbd, timeout);
finish_sock(sock, nbd, swap);
+#ifdef __linux__
+ if (swap) {
+ /* try linux >= 2.6.36 interface first */
+ if (oom_adjust("/proc/self/oom_score_adj", "-1000")) {
+ /* fall back to linux <= 2.6.35 interface */
+ oom_adjust("/proc/self/oom_adj", "-17");
+ }
+ }
+#endif
/* Go daemon */
--
ldv
Reply to: