public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nptl: Add glibc.pthread.stack_cache_size
@ 2021-06-28 11:44 Florian Weimer
  2021-06-28 12:18 ` Andreas Schwab
  2021-06-28 13:39 ` Siddhesh Poyarekar
  0 siblings, 2 replies; 5+ messages in thread
From: Florian Weimer @ 2021-06-28 11:44 UTC (permalink / raw)
  To: libc-alpha; +Cc: Mark J. Wielaard, Siddhesh Poyarekar

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 <ldsodefs.h>
 #include <pthreadP.h>
 
-/* 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 <list.h>
 #include <stdbool.h>
 
+/* 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 <stdbool.h>
 #include <unistd.h>  /* Get STDOUT_FILENO for _dl_printf.  */
 #include <elf/dl-tunables.h>
+#include <nptl-stack.h>
 
 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
+    }
   }
 }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nptl: Add glibc.pthread.stack_cache_size
  2021-06-28 11:44 [PATCH] nptl: Add glibc.pthread.stack_cache_size Florian Weimer
@ 2021-06-28 12:18 ` Andreas Schwab
  2021-06-28 12:22   ` Florian Weimer
  2021-06-28 13:39 ` Siddhesh Poyarekar
  1 sibling, 1 reply; 5+ messages in thread
From: Andreas Schwab @ 2021-06-28 12:18 UTC (permalink / raw)
  To: Florian Weimer via Libc-alpha
  Cc: Florian Weimer, Siddhesh Poyarekar, Mark J. Wielaard

On Jun 28 2021, Florian Weimer via Libc-alpha wrote:

> 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 <list.h>
>  #include <stdbool.h>
>  
> +/* Maximum size in kB of cache.  40MiBi by default.  */
> +extern size_t __nptl_stack_cache_maxsize attribute_hidden;

Kilobytes or bytes?

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nptl: Add glibc.pthread.stack_cache_size
  2021-06-28 12:18 ` Andreas Schwab
@ 2021-06-28 12:22   ` Florian Weimer
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Weimer @ 2021-06-28 12:22 UTC (permalink / raw)
  To: Andreas Schwab
  Cc: Florian Weimer via Libc-alpha, Siddhesh Poyarekar, Mark J. Wielaard

* Andreas Schwab:

> On Jun 28 2021, Florian Weimer via Libc-alpha wrote:
>
>> 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 <list.h>
>>  #include <stdbool.h>
>>  
>> +/* Maximum size in kB of cache.  40MiBi by default.  */
>> +extern size_t __nptl_stack_cache_maxsize attribute_hidden;
>
> Kilobytes or bytes?

Oh, looks like the original comment was wrong.  stackblock_size, among
other things, is measured in bytes.  I have changed it locally to:

/* Maximum size of the cache, in bytes.  40 MiB by default.  */

Thanks,
Florian


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nptl: Add glibc.pthread.stack_cache_size
  2021-06-28 11:44 [PATCH] nptl: Add glibc.pthread.stack_cache_size Florian Weimer
  2021-06-28 12:18 ` Andreas Schwab
@ 2021-06-28 13:39 ` Siddhesh Poyarekar
  2021-06-28 13:57   ` Florian Weimer
  1 sibling, 1 reply; 5+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-28 13:39 UTC (permalink / raw)
  To: Florian Weimer, libc-alpha; +Cc: Mark J. Wielaard

On 6/28/21 5:14 PM, Florian Weimer via Libc-alpha wrote:
> 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.

configure the size of the thread stack cache.

> +
>   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 <ldsodefs.h>
>   #include <pthreadP.h>
>   
> -/* 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);

Not a strong preference, but maybe this still remains just as unlikely? 
  A bulk of use cases shouldn't need frequent freeing.

>   }
>   
>   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 <list.h>
>   #include <stdbool.h>
>   
> +/* Maximum size in kB of cache.  40MiBi by default.  */

Comment needs fixing as Andreas pointed out.

> +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 <stdbool.h>
>   #include <unistd.h>  /* Get STDOUT_FILENO for _dl_printf.  */
>   #include <elf/dl-tunables.h>
> +#include <nptl-stack.h>
>   
>   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

OK.

> 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
> +    }
>     }
>   }

OK.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nptl: Add glibc.pthread.stack_cache_size
  2021-06-28 13:39 ` Siddhesh Poyarekar
@ 2021-06-28 13:57   ` Florian Weimer
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Weimer @ 2021-06-28 13:57 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: libc-alpha, Mark J. Wielaard

* Siddhesh Poyarekar:

>> 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.
>
> configure the size of the thread stack cache.

Fixed.

>> 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 <ldsodefs.h>
>>   #include <pthreadP.h>
>>   -/* 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);
>
> Not a strong preference, but maybe this still remains just as
> unlikely?   A bulk of use cases shouldn't need frequent freeing.

I'm going to bring it back in a v2.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-06-28 13:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-28 11:44 [PATCH] nptl: Add glibc.pthread.stack_cache_size Florian Weimer
2021-06-28 12:18 ` Andreas Schwab
2021-06-28 12:22   ` Florian Weimer
2021-06-28 13:39 ` Siddhesh Poyarekar
2021-06-28 13:57   ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).