[PATCH] Use new oom_score_adj without a new compile-time constant

From: Dan McGee <dan(at)archlinux(dot)org>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] Use new oom_score_adj without a new compile-time constant
Date: 2011-09-19 20:11:18
Message-ID: 1316463078-1068-1-git-send-email-dan@archlinux.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

This is one way to prevent the kernel warning message without having to
introduce a new constant. Scale the old oom_adj-style value the same way
the kernel internally does it and use that instead if oom_score_adj is
available for writing.

Signed-off-by: Dan McGee <dan(at)archlinux(dot)org>
---

This addresses some of the concerns raised on the ML and will at least keep
those of us running modern kernels happy.

Alternatively one could switch the symbol used to be the new style and have the
old one computed; however this is a pain for legacy vs. current versions.

src/backend/postmaster/fork_process.c | 22 +++++++++++++++++++++-
1 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/src/backend/postmaster/fork_process.c b/src/backend/postmaster/fork_process.c
index b2fe9a1..3cded54 100644
--- a/src/backend/postmaster/fork_process.c
+++ b/src/backend/postmaster/fork_process.c
@@ -81,16 +81,36 @@ fork_process(void)
* Use open() not stdio, to ensure we control the open flags. Some
* Linux security environments reject anything but O_WRONLY.
*/
- int fd = open("/proc/self/oom_adj", O_WRONLY, 0);
+ int fd = open("/proc/self/oom_score_adj", O_WRONLY, 0);

/* We ignore all errors */
if (fd >= 0)
{
char buf[16];
+ int oom_score_adj;

+ /*
+ * The compile-time value is the old style oom_adj;
+ * scale it the same way the kernel does to
+ * convert to the new style oom_score_adj. This
+ * should become a constant at compile time.
+ * Valid values range from -17 (never kill) to
+ * 15; no attempt of validation is done.
+ */
+ oom_score_adj = LINUX_OOM_ADJ * 1000 / 17;
snprintf(buf, sizeof(buf), "%d\n", LINUX_OOM_ADJ);
(void) write(fd, buf, strlen(buf));
close(fd);
+ } else if (errno == EEXIST) {
+ int fd = open("/proc/self/oom_adj", O_WRONLY, 0);
+ if (fd >= 0)
+ {
+ char buf[16];
+
+ snprintf(buf, sizeof(buf), "%d\n", LINUX_OOM_ADJ);
+ (void) write(fd, buf, strlen(buf));
+ close(fd);
+ }
}
}
#endif /* LINUX_OOM_ADJ */
--
1.7.6.1

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dan McGee 2011-09-19 20:36:50 Re: [PATCH] Use new oom_score_adj without a new compile-time constant
Previous Message Joshua Tolley 2011-09-19 19:56:31 Re: Grouping Sets