The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Frederic Weisbecker <frederic@kernel.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Benjamin Segall <bsegall@google.com>,
	Eric Dumazet <edumazet@google.com>,
	Andrey Vagin <avagin@openvz.org>,
	Pavel Tikhomirov <ptikhomirov@virtuozzo.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Cyrill Gorcunov <gorcunov@gmail.com>
Subject: Re: [patch V3 12/18] posix-timers: Improve hash table performance
Date: Tue, 11 Mar 2025 14:44:45 +0100	[thread overview]
Message-ID: <Z9A-TWTX9Yl6zlst@localhost.localdomain> (raw)
In-Reply-To: <20250308155624.216091571@linutronix.de>

Le Sat, Mar 08, 2025 at 05:48:38PM +0100, Thomas Gleixner a écrit :
> Eric and Ben reported a significant performance bottleneck on the global
> hash, which is used to store posix timers for lookup.
> 
> Eric tried to do a lockless validation of a new timer ID before trying to
> insert the timer, but that does not solve the problem.
> 
> For the non-contended case this is a pointless exercise and for the
> contended case this extra lookup just creates enough interleaving that all
> tasks can make progress.
> 
> There are actually two real solutions to the problem:
> 
>   1) Provide a per process (signal struct) xarray storage
> 
>   2) Implement a smarter hash like the one in the futex code
> 
> #1 works perfectly fine for most cases, but the fact that CRIU enforced a
>    linear increasing timer ID to restore timers makes this problematic.
> 
>    It's easy enough to create a sparse timer ID space, which amounts very
>    fast to a large junk of memory consumed for the xarray. 2048 timers with
>    a ID offset of 512 consume more than one megabyte of memory for the
>    xarray storage.
> 
> #2 The main advantage of the futex hash is that it uses per hash bucket
>    locks instead of a global hash lock. Aside of that it is scaled
>    according to the number of CPUs at boot time.
> 
> Experiments with artifical benchmarks have shown that a scaled hash with
> per bucket locks comes pretty close to the xarray performance and in some
> scenarios it performes better.
> 
> Test 1:
> 
>      A single process creates 20000 timers and afterwards invokes
>      timer_getoverrun(2) on each of them:
> 
>             mainline        Eric   newhash   xarray
> create         23 ms       23 ms      9 ms     8 ms
> getoverrun     14 ms       14 ms      5 ms     4 ms
> 
> Test 2:
> 
>      A single process creates 50000 timers and afterwards invokes
>      timer_getoverrun(2) on each of them:
> 
>             mainline        Eric   newhash   xarray
> create         98 ms      219 ms     20 ms    18 ms
> getoverrun     62 ms       62 ms     10 ms     9 ms
> 
> Test 3:
> 
>      A single process creates 100000 timers and afterwards invokes
>      timer_getoverrun(2) on each of them:
> 
>             mainline        Eric   newhash   xarray
> create        313 ms      750 ms     48 ms    33 ms
> getoverrun    261 ms      260 ms     20 ms    14 ms
> 
> Erics changes create quite some overhead in the create() path due to the
> double list walk, as the main issue according to perf is the list walk
> itself. With 100k timers each hash bucket contains ~200 timers, which in
> the worst case need to be all inspected. The same problem applies for
> getoverrun() where the lookup has to walk through the hash buckets to find
> the timer it is looking for.
> 
> The scaled hash obviously reduces hash collisions and lock contention
> significantly. This becomes more prominent with concurrency.
> 
> Test 4:
> 
>      A process creates 63 threads and all threads wait on a barrier before
>      each instance creates 20000 timers and afterwards invokes
>      timer_getoverrun(2) on each of them. The threads are pinned on
>      seperate CPUs to achive maximum concurrency. The numbers are the
>      average times per thread:
> 
>             mainline        Eric   newhash   xarray
> create     180239 ms    38599 ms    579 ms   813 ms
> getoverrun   2645 ms     2642 ms     32 ms     7 ms
> 
> Test 5:
> 
>      A process forks 63 times and all forks wait on a barrier before each
>      instance creates 20000 timers and afterwards invokes
>      timer_getoverrun(2) on each of them. The processes are pinned on
>      seperate CPUs to achive maximum concurrency. The numbers are the
>      average times per process:
> 
>             mainline        eric   newhash   xarray
> create     157253 ms    40008 ms     83 ms    60 ms
> getoverrun   2611 ms     2614 ms     40 ms     4 ms
> 
> So clearly the reduction of lock contention with Eric's changes makes a
> significant difference for the create() loop, but it does not mitigate the
> problem of long list walks, which is clearly visible on the getoverrun()
> side because that is purely dominated by the lookup itself. Once the timer
> is found, the syscall just reads from the timer structure with no other
> locks or code paths involved and returns.
> 
> The reason for the difference between the thread and the fork case for the
> new hash and the xarray is that both suffer from contention on
> sighand::siglock and the xarray suffers additionally from contention on the
> xarray lock on insertion.
> 
> The only case where the reworked hash slighly outperforms the xarray is a
> tight loop which creates and deletes timers.
> 
> Test 4:
> 
>      A process creates 63 threads and all threads wait on a barrier before
>      each instance runs a loop which creates and deletes a timer 100000
>      times in a row. The threads are pinned on seperate CPUs to achive
>      maximum concurrency. The numbers are the average times per thread:
> 
>             mainline        Eric   newhash   xarray
> loop	    5917  ms	 5897 ms   5473 ms  7846 ms
> 
> Test 5:
> 
>      A process forks 63 times and all forks wait on a barrier before each
>      each instance runs a loop which creates and deletes a timer 100000
>      times in a row. The processes are pinned on seperate CPUs to achive
>      maximum concurrency. The numbers are the average times per process:
> 
>             mainline        Eric   newhash   xarray
> loop	     5137 ms	 7828 ms    891 ms   872 ms
> 
> In both test there is not much contention on the hash, but the ucount
> accounting for the signal and in the thread case the sighand::siglock
> contention (plus the xarray locking) contribute dominantly to the overhead.
> 
> As the memory consumption of the xarray in the sparse ID case is
> significant, the scaled hash with per bucket locks seems to be the better
> overall option. While the xarray has faster lookup times for a large number
> of timers, the actual syscall usage, which requires the lookup is not an
> extreme hotpath. Most applications utilize signal delivery and all syscalls
> except timer_getoverrun(2) are all but cheap.
> 
> So implement a scaled hash with per bucket locks, which offers the best
> tradeoff between performance and memory consumption.
> 
> Reported-by: Eric Dumazet <edumazet@google.com>
> Reported-by: Benjamin Segall <bsegall@google.com>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Acked-by: Frederic Weisbecker <frederic@kernel.org>

  reply	other threads:[~2025-03-11 13:44 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-08 16:48 [patch V3 00/18] posix-timers: Rework the global hash table and provide a sane mechanism for CRIU Thomas Gleixner
2025-03-08 16:48 ` [patch V3 01/18] posix-timers: Ensure that timer initialization is fully visible Thomas Gleixner
2025-03-08 21:39   ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 02/18] posix-timers: Initialise timer before adding it to the hash table Thomas Gleixner
2025-03-11 13:25   ` Frederic Weisbecker
2025-03-11 14:16     ` Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 03/18] posix-timers: Add cond_resched() to posix_timer_add() search loop Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 04/18] posix-timers: Cleanup includes Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 05/18] posix-timers: Remove a few paranoid warnings Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 06/18] posix-timers: Remove SLAB_PANIC from kmem cache Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 07/18] posix-timers: Use guards in a few places Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 08/18] posix-timers: Simplify lock/unlock_timer() Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 09/18] posix-timers: Rework timer removal Thomas Gleixner
2025-03-09 23:17   ` Frederic Weisbecker
2025-03-10  6:33     ` Thomas Gleixner
2025-03-10  8:13   ` [patch V3a " Thomas Gleixner
2025-03-13 11:31     ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 10/18] posix-timers: Make lock_timer() use guard() Thomas Gleixner
2025-03-10 11:57   ` Frederic Weisbecker
2025-03-10 17:36     ` Thomas Gleixner
2025-03-10 22:16       ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Peter Zijlstra
2025-03-08 16:48 ` [patch V3 11/18] posix-timers: Make signal_struct:: Next_posix_timer_id an atomic_t Thomas Gleixner
2025-03-10 22:57   ` Frederic Weisbecker
2025-03-11 13:41   ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 12/18] posix-timers: Improve hash table performance Thomas Gleixner
2025-03-11 13:44   ` Frederic Weisbecker [this message]
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 13/18] posix-timers: Switch to jhash32() Thomas Gleixner
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 14/18] posix-timers: Avoid false cacheline sharing Thomas Gleixner
2025-03-11 13:53   ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-13 22:13   ` [patch V3 14/18] " David Laight
2025-03-17  6:20   ` Nysal Jan K.A.
2025-03-08 16:48 ` [patch V3 15/18] posix-timers: Make per process list RCU safe Thomas Gleixner
2025-03-11 15:29   ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 16/18] posix-timers: Dont iterate /proc/$PID/timers with sighand:: Siglock held Thomas Gleixner
2025-03-08 22:38   ` Cyrill Gorcunov
2025-03-11 15:26   ` Frederic Weisbecker
2025-03-13 11:31   ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 17/18] posix-timers: Provide a mechanism to allocate a given timer ID Thomas Gleixner
2025-03-08 22:25   ` Cyrill Gorcunov
2025-03-11 21:35   ` Frederic Weisbecker
2025-03-11 22:05     ` Thomas Gleixner
2025-03-11 22:07       ` [patch V3a " Thomas Gleixner
2025-03-11 22:32         ` Frederic Weisbecker
2025-03-12  7:56           ` Cyrill Gorcunov
2025-03-12 11:24             ` Thomas Gleixner
2025-03-12 11:31               ` Thomas Gleixner
2025-03-12 12:41               ` Cyrill Gorcunov
2025-03-12 17:45                 ` Thomas Gleixner
2025-03-13 11:31         ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-12 12:59     ` [patch V3 17/18] " Cyrill Gorcunov
2025-03-08 16:48 ` [patch V3 18/18] selftests/timers/posix-timers: Add a test for exact allocation mode Thomas Gleixner
2025-03-10  8:11   ` [patch V3a " Thomas Gleixner
2025-03-11 21:44     ` Frederic Weisbecker
2025-03-13 11:31     ` [tip: timers/core] " tip-bot2 for Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z9A-TWTX9Yl6zlst@localhost.localdomain \
    --to=frederic@kernel.org \
    --cc=anna-maria@linutronix.de \
    --cc=avagin@openvz.org \
    --cc=bsegall@google.com \
    --cc=edumazet@google.com \
    --cc=gorcunov@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox