[PATCH] Use new oom_score_adj without a new compile-time constant - Mailing list pgsql-hackers

From Dan McGee
Subject [PATCH] Use new oom_score_adj without a new compile-time constant
Date
Msg-id 1316463078-1068-1-git-send-email-dan@archlinux.org
Whole thread Raw
In response to Re: /proc/self/oom_adj is deprecated in newer Linux kernels  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [PATCH] Use new oom_score_adj without a new compile-time constant
List 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@archlinux.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



pgsql-hackers by date:

Previous
From: Matthew Wilcox
Date:
Subject: Re: Improve lseek scalability v3
Next
From: Dan McGee
Date:
Subject: Re: [PATCH] Use new oom_score_adj without a new compile-time constant