aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-06-13 15:34:57 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-06-13 15:35:52 -0400
commit93f4d7f806613d28842f956a84f31cc41d825503 (patch)
tree493db7fa8fc449a9cac2a75ac2a44b197e9f2271 /src
parentb9212e379c55564c8b6cdc8585b74606e90ec1ea (diff)
downloadpostgresql-93f4d7f806613d28842f956a84f31cc41d825503.tar.gz
postgresql-93f4d7f806613d28842f956a84f31cc41d825503.zip
Support Linux's oom_score_adj API as well as the older oom_adj API.
The simplest way to handle this is just to copy-and-paste the relevant code block in fork_process.c, so that's what I did. (It's possible that something more complicated would be useful to packagers who want to work with either the old or the new API; but at this point the number of such people is rapidly approaching zero, so let's just get the minimal thing done.) Update relevant documentation as well.
Diffstat (limited to 'src')
-rw-r--r--src/backend/postmaster/fork_process.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/backend/postmaster/fork_process.c b/src/backend/postmaster/fork_process.c
index c99ed432655..2a415e844be 100644
--- a/src/backend/postmaster/fork_process.c
+++ b/src/backend/postmaster/fork_process.c
@@ -68,12 +68,40 @@ fork_process(void)
* process sizes *including shared memory*. (This is unbelievably
* stupid, but the kernel hackers seem uninterested in improving it.)
* Therefore it's often a good idea to protect the postmaster by
- * setting its oom_adj value negative (which has to be done in a
- * root-owned startup script). If you just do that much, all child
+ * setting its oom_score_adj value negative (which has to be done in a
+ * root-owned startup script). If you just do that much, all child
* processes will also be protected against OOM kill, which might not
- * be desirable. You can then choose to build with LINUX_OOM_ADJ
- * #defined to 0, or some other value that you want child processes to
- * adopt here.
+ * be desirable. You can then choose to build with
+ * LINUX_OOM_SCORE_ADJ #defined to 0, or to some other value that you
+ * want child processes to adopt here.
+ */
+#ifdef LINUX_OOM_SCORE_ADJ
+ {
+ /*
+ * 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_score_adj", O_WRONLY, 0);
+
+ /* We ignore all errors */
+ if (fd >= 0)
+ {
+ char buf[16];
+ int rc;
+
+ snprintf(buf, sizeof(buf), "%d\n", LINUX_OOM_SCORE_ADJ);
+ rc = write(fd, buf, strlen(buf));
+ (void) rc;
+ close(fd);
+ }
+ }
+#endif /* LINUX_OOM_SCORE_ADJ */
+
+ /*
+ * Older Linux kernels have oom_adj not oom_score_adj. This works
+ * similarly except with a different scale of adjustment values.
+ * If it's necessary to build Postgres to work with either API,
+ * you can define both LINUX_OOM_SCORE_ADJ and LINUX_OOM_ADJ.
*/
#ifdef LINUX_OOM_ADJ
{