From patchwork Sat Jul 9 18:37:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 13717 Received: (qmail 49189 invoked by alias); 9 Jul 2016 18:49:34 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 48962 invoked by uid 89); 9 Jul 2016 18:49:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_NEUTRAL autolearn=no version=3.3.2 spammy=H*m:sourceware, H*F:U*siddhesh, HX-Envelope-From:sk:siddhes, testsrcs X-HELO: homiemail-a114.g.dreamhost.com From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Subject: [PATCH 2/2] Initialize tunable list with the GLIBC_TUNABLES environment variable Date: Sun, 10 Jul 2016 00:07:58 +0530 Message-Id: <1468089478-10085-3-git-send-email-siddhesh@sourceware.org> In-Reply-To: <1468089478-10085-1-git-send-email-siddhesh@sourceware.org> References: <1468089478-10085-1-git-send-email-siddhesh@sourceware.org> Read tunables values from the users using the GLIBC_TUNABLES environment variable. The value of this variable is a colon-separated list of name=value pairs. So a typical string would look like this: GLIBC_TUNABLES=glibc.malloc.mmap_threshold=2048:glibc.malloc.trim_threshold=1024 * elf/dl-tunables.c: Include mman.h and libc-internals.h. (tunables_strdup): New function. (parse_tunables): New function. (GLIBC_TUNABLES): New macro. (__tunables_init): Use the new functions and macro. * malloc/tst-malloc-usable-tunables.c: New test case. * malloc/tst-malloc-usable-static-tunables.c: New test case. * malloc/Makefile (tests, tests-static): Add tests. --- elf/dl-tunables.c | 93 ++++++++++++++++++++++++++++++ malloc/Makefile | 6 +- malloc/tst-malloc-usable-static-tunables.c | 1 + malloc/tst-malloc-usable-tunables.c | 1 + 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 malloc/tst-malloc-usable-static-tunables.c create mode 100644 malloc/tst-malloc-usable-tunables.c diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index ee8c2f0..8ed66c8 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -22,10 +22,14 @@ #include #include #include +#include +#include #define TUNABLES_INTERNAL 1 #include "dl-tunables.h" +#define GLIBC_TUNABLES "GLIBC_TUNABLES" + /* Compare environment names, bounded by the name hardcoded in glibc. */ static bool is_name (const char *orig, const char *envname) @@ -41,6 +45,27 @@ is_name (const char *orig, const char *envname) return false; } +static char *tunables_strdup (const char *in) +{ + size_t i = 0; + + while (in[i++]); + + char *out = __mmap (NULL, ALIGN_UP (i, __getpagesize ()), + PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, + 0); + + if (out == MAP_FAILED) + return NULL; + + i--; + + while (i-- > 0) + out[i] = in[i]; + + return out; +} + static bool get_next_env (char ***envp, char **name, size_t *namelen, char **val) { @@ -109,6 +134,66 @@ tunable_initialize (tunable_t *cur) } } +static void +parse_tunables (char *tunestr) +{ + if (tunestr == NULL || *tunestr == '\0') + return; + + char *p = tunestr; + + while (true) + { + char *name = p; + size_t len = 0; + + /* First, find where the name ends. */ + while (p[len] != '=' && p[len] != ':' && p[len] != '\0') + len++; + + /* If we reach the end of the string before getting a valid name-value + pair, bail out. */ + if (p[len] == '\0') + return; + + /* We did not find a valid name-value pair before encountering the + colon. */ + if (p[len]== ':') + { + p += len + 1; + continue; + } + + p += len + 1; + + char *value = p; + len = 0; + + while (p[len] != ':' && p[len] != '\0') + len++; + + char end = p[len]; + p[len] = '\0'; + + /* Add the tunable if it exists. */ + for (size_t i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++) + { + if (is_name (tunable_list[i].name, name)) + { + /* Tunable values take precedence over the env_alias. */ + tunable_list[i].strval = value; + tunable_initialize (&tunable_list[i]); + break; + } + } + + if (end == ':') + p += len + 1; + else + return; + } +} + /* Initialize the tunables list from the environment. For now we only use the ENV_ALIAS to find values. Later we will also use the tunable names to find values. */ @@ -121,6 +206,14 @@ __tunables_init (char **envp) while (get_next_env (&envp, &envname, &len, &envval)) { + if (is_name (GLIBC_TUNABLES, envname)) + { + char *val = tunables_strdup (envval); + if (val != NULL) + parse_tunables (val); + continue; + } + for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++) { tunable_t *cur = &tunable_list[i]; diff --git a/malloc/Makefile b/malloc/Makefile index 23ab2f4..ba34da1 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -30,8 +30,8 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ tst-pvalloc tst-memalign tst-mallopt tst-scratch_buffer \ tst-malloc-backtrace tst-malloc-thread-exit \ tst-malloc-thread-fail tst-malloc-fork-deadlock \ - tst-mallocfork2 -tests-static := tst-malloc-usable-static + tst-mallocfork2 tst-malloc-usable-tunables +tests-static := tst-malloc-usable-static tst-malloc-usable-static-tunables tests += $(tests-static) test-srcs = tst-mtrace @@ -141,6 +141,8 @@ endif tst-mcheck-ENV = MALLOC_CHECK_=3 tst-malloc-usable-ENV = MALLOC_CHECK_=3 tst-malloc-usable-static-ENV = $(tst-malloc-usable-ENV) +tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 +tst-malloc-usable-static-tunables-ENV = $(tst-malloc-usable-tunables-ENV) # Uncomment this for test releases. For public releases it is too expensive. #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1 diff --git a/malloc/tst-malloc-usable-static-tunables.c b/malloc/tst-malloc-usable-static-tunables.c new file mode 100644 index 0000000..8907db0 --- /dev/null +++ b/malloc/tst-malloc-usable-static-tunables.c @@ -0,0 +1 @@ +#include diff --git a/malloc/tst-malloc-usable-tunables.c b/malloc/tst-malloc-usable-tunables.c new file mode 100644 index 0000000..8907db0 --- /dev/null +++ b/malloc/tst-malloc-usable-tunables.c @@ -0,0 +1 @@ +#include