From patchwork Mon Jun 28 11:44:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 44035 X-Patchwork-Delegate: siddhesh@gotplt.org Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3104C397201A for ; Mon, 28 Jun 2021 11:46:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3104C397201A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624880792; bh=5+V6S6SOyOFn4qoQlFcwveN9m/C7XQp5QpbX8AdMy3o=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=lG2BgepR8ldzNdGdZXYLvpzt4OSUTUppiX90r4jSF1egz31vrMxXtCb5s2o6ty1cw EmAshHhHGB7ge2vueXEvbuqRQYqzrKIFn4F891TGTE91AkpjOJmCc0BgkQUZIxRJLW 6hOsbgCGd8qmCZgXG2gTo7SnmekSvjgWeGpntreo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 39FE4396EC29 for ; Mon, 28 Jun 2021 11:44:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 39FE4396EC29 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-467-7g3Wf4rxONuNBiWxf4-BOg-1; Mon, 28 Jun 2021 07:44:44 -0400 X-MC-Unique: 7g3Wf4rxONuNBiWxf4-BOg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D46111084F40 for ; Mon, 28 Jun 2021 11:44:43 +0000 (UTC) Received: from oldenburg.str.redhat.com (ovpn-112-228.ams2.redhat.com [10.36.112.228]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2449C100164A; Mon, 28 Jun 2021 11:44:35 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Add glibc.pthread.stack_cache_size Date: Mon, 28 Jun 2021 13:44:33 +0200 Message-ID: <874kdiusbi.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Cc: Siddhesh Poyarekar , "Mark J. Wielaard" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The valgrind/helgrind test suite needs a way to make stack dealloction more prompt, and this feature seems to be generally useful. Tested on i686-linux-gnu, x86_64-linux-gnu. Built with build-many-glibcs.py. Confirmed manually with GDB that the stack cache size indeed changes as a result of setting the tunable. --- NEWS | 3 +++ manual/tunables.texi | 9 +++++++++ nptl/nptl-stack.c | 7 +++---- nptl/nptl-stack.h | 3 +++ nptl/pthread_mutex_conf.c | 9 +++++++++ sysdeps/nptl/dl-tunables.list | 4 ++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 37ba4334c6..a107b14112 100644 --- a/NEWS +++ b/NEWS @@ -49,6 +49,9 @@ Major new features: with names ending with .conf to logically classify the converter modules in that directory. +* A new tunable, glibc.pthread.stack_cache_size, can be used to + configure the thread stack size on Linux. + Deprecated and removed features, and other changes affecting compatibility: * The function pthread_mutex_consistent_np has been deprecated; programs diff --git a/manual/tunables.texi b/manual/tunables.texi index fe7c1313cc..d5d957fb5b 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -402,6 +402,15 @@ is acquired. The default value of this tunable is @samp{100}. @end deftp +@deftp Tunable glibc.pthread.stack_cache_size +This tunable configures the maximum size of the stack cache. Once the +stack cache exceeds this size, unused thread stacks are returned to +the kernel, to bring the cache size below this limit. + +The value is measured in bytes. The default is @samp{41943040} +(fourty mibibytes). +@end deftp + @node Hardware Capability Tunables @section Hardware Capability Tunables @cindex hardware capability tunables diff --git a/nptl/nptl-stack.c b/nptl/nptl-stack.c index 1a0c460ba8..d04feb0683 100644 --- a/nptl/nptl-stack.c +++ b/nptl/nptl-stack.c @@ -21,8 +21,7 @@ #include #include -/* Maximum size in kB of cache. 40MiBi by default. */ -static const size_t stack_cache_maxsize = 40 * 1024 * 1024; +size_t __nptl_stack_cache_maxsize = 40 * 1024 * 1024; void __nptl_stack_list_del (list_t *elem) @@ -103,8 +102,8 @@ queue_stack (struct pthread *stack) __nptl_stack_list_add (&stack->list, &GL (dl_stack_cache)); GL (dl_stack_cache_actsize) += stack->stackblock_size; - if (__glibc_unlikely (GL (dl_stack_cache_actsize) > stack_cache_maxsize)) - __nptl_free_stacks (stack_cache_maxsize); + if (GL (dl_stack_cache_actsize) > __nptl_stack_cache_maxsize) + __nptl_free_stacks (__nptl_stack_cache_maxsize); } void diff --git a/nptl/nptl-stack.h b/nptl/nptl-stack.h index a6bd8df77f..19d040431e 100644 --- a/nptl/nptl-stack.h +++ b/nptl/nptl-stack.h @@ -25,6 +25,9 @@ #include #include +/* Maximum size in kB of cache. 40MiBi by default. */ +extern size_t __nptl_stack_cache_maxsize attribute_hidden; + /* Check whether the stack is still used or not. */ static inline bool __nptl_stack_in_use (struct pthread *pd) diff --git a/nptl/pthread_mutex_conf.c b/nptl/pthread_mutex_conf.c index e6235dea47..c8f171c995 100644 --- a/nptl/pthread_mutex_conf.c +++ b/nptl/pthread_mutex_conf.c @@ -23,6 +23,7 @@ #include #include /* Get STDOUT_FILENO for _dl_printf. */ #include +#include struct mutex_config __mutex_aconf = { @@ -38,10 +39,18 @@ TUNABLE_CALLBACK (set_mutex_spin_count) (tunable_val_t *valp) __mutex_aconf.spin_count = (int32_t) (valp)->numval; } +static void +TUNABLE_CALLBACK (set_stack_cache_size) (tunable_val_t *valp) +{ + __nptl_stack_cache_maxsize = valp->numval; +} + void __pthread_tunables_init (void) { TUNABLE_GET (mutex_spin_count, int32_t, TUNABLE_CALLBACK (set_mutex_spin_count)); + TUNABLE_GET (stack_cache_size, size_t, + TUNABLE_CALLBACK (set_stack_cache_size)); } #endif diff --git a/sysdeps/nptl/dl-tunables.list b/sysdeps/nptl/dl-tunables.list index e3f6432e60..ac5d053298 100644 --- a/sysdeps/nptl/dl-tunables.list +++ b/sysdeps/nptl/dl-tunables.list @@ -23,5 +23,9 @@ glibc { maxval: 32767 default: 100 } + stack_cache_size { + type: SIZE_T + default: 41943040 + } } }