aboutsummaryrefslogtreecommitdiff
path: root/shm.c
diff options
context:
space:
mode:
authorMel Gorman <mel@csn.ul.ie>2008-08-07 18:42:39 +0100
committerEric Munson <ebmunson@us.ibm.com>2008-08-14 11:07:33 -0700
commit1f6b889ccf517a61b5caadaf8ba76b19dd1fb8a7 (patch)
treeef8016f39b3c2093daa5ec920491b12a4278312c /shm.c
parent296598a39d5cde8efe200917a8dc36c41e45c10e (diff)
downloadlibhugetlbfs-1f6b889ccf517a61b5caadaf8ba76b19dd1fb8a7.tar.gz
Allow shmget() to be overridden to add the SHM_HUGETLB flag
There are applications that are not hugepage-aware but use shared memory such as the postgres database. It is possible that the SHM_HUGETLB flag could be added to such an application via libhugetlbfs and overriding shmget() to add the SHM_HUGETLB to the flags. This patch enables libhugetlbfs to add the SHM_HUGETLB when a HUGETLB_SHM environment variable is set to "yes". It will work whether the application is linked to libhugetlbfs or loaded via LD_PRELOAD. Signed-off-by: Mel Gorman <mel@csn.ul.ie> Acked-by: Adam Litke <agl@us.ibm.com> Signed-off-by: Eric Munson <ebmunson@us.ibm.com>
Diffstat (limited to 'shm.c')
-rw-r--r--shm.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/shm.c b/shm.c
new file mode 100644
index 0000000..cc9995d
--- /dev/null
+++ b/shm.c
@@ -0,0 +1,82 @@
+/*
+ * libhugetlbfs - Easy use of Linux hugepages
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <sys/types.h>
+#include "libhugetlbfs_internal.h"
+#include "hugetlbfs.h"
+
+int shmget(key_t key, size_t size, int shmflg)
+{
+ static int (*real_shmget)(key_t key, size_t size, int shmflg) = NULL;
+ char *error;
+ int retval;
+ char *hugetlbshm_env;
+ size_t aligned_size = size;
+ int hugetlbshm_enabled = 0;
+
+ DEBUG("hugetlb_shmem: entering overridden shmget() call\n");
+
+ /* Get a handle to the "real" shmget system call */
+ if (!real_shmget) {
+ real_shmget = dlsym(RTLD_NEXT, "shmget");
+ if ((error = dlerror()) != NULL) {
+ ERROR("%s", error);
+ return -1;
+ }
+ }
+
+ /* Determine if shmget() calls should be overridden */
+ hugetlbshm_env = getenv("HUGETLB_SHM");
+ if (hugetlbshm_env && !strcmp(hugetlbshm_env, "yes"))
+ hugetlbshm_enabled = 1;
+
+ /* Align the size and set SHM_HUGETLB on request */
+ if (hugetlbshm_enabled) {
+ aligned_size = ALIGN(size, gethugepagesize());
+ if (size != aligned_size) {
+ DEBUG("hugetlb_shmem: size growth align %zd -> %zd\n",
+ size, aligned_size);
+ }
+
+ DEBUG("hugetlb_shmem: Adding SHM_HUGETLB flag\n");
+ shmflg |= SHM_HUGETLB;
+ } else {
+ DEBUG("hugetlb_shmem: shmget override not requested\n");
+ }
+
+ /* Call the "real" shmget. If hugepages fail, use small pages */
+ retval = real_shmget(key, aligned_size, shmflg);
+ if (retval == -1 && hugetlbshm_enabled) {
+ WARNING("While overriding shmget(%zd) to add SHM_HUGETLB: %s\n",
+ aligned_size, strerror(errno));
+ shmflg &= ~SHM_HUGETLB;
+ retval = real_shmget(key, size, shmflg);
+ DEBUG("Using small pages for shmget despite HUGETLB_SHM\n");
+ }
+
+ return retval;
+}