Re: [PATCH v8 13/15] futex: Resize local futex hash table based on number of threads.
From: Peter Zijlstra
Date: Tue Feb 04 2025 - 05:22:04 EST
On Mon, Feb 03, 2025 at 02:59:33PM +0100, Sebastian Andrzej Siewior wrote:
> Automatically size the local hash based on the number of threads. The
> logic tries to allocate between 16 and futex_hashsize (the default for
> the system wide hash bucket) and uses 4 * number-of-threads.
> On CONFIG_BASE_SMALL configs the suggested size is always 2.
> + scoped_guard(rcu) {
> + threads = get_nr_threads(current);
How about something like:
threads = min(get_nr_threads(), num_online_cpus())
?
> + hb_p = rcu_dereference(current->mm->futex_phash);
> + if (hb_p)
> + current_buckets = hb_p->hash_mask + 1;
> + }
> +
> + if (IS_ENABLED(CONFIG_BASE_SMALL)) {
> + buckets = 2;
> +
Or... you just disable the local thing entirely for BASE_SMALL and have
it fall back to the global hash.
> + } else {
> + /*
> + * The default allocation will remain within
> + * 16 <= threads * 4 <= global hash size
> + */
> + buckets = roundup_pow_of_two(4 * threads);
> + buckets = max(buckets, 16);
> + buckets = min(buckets, futex_hashsize);
> + }
> + if (current_buckets >= buckets)
> + return 0;
> +
> + return futex_hash_allocate(buckets);
> }
>
> static int futex_hash_get_slots(void)
> --
> 2.47.2
>