llvm-project/compiler-rt/lib/scudo
Kostya Kortchinsky 4410e2c43f [scudo] Improve the scalability of the shared TSD model
Summary:
The shared TSD model in its current form doesn't scale. Here is an example of
rpc2-benchmark (with default parameters, which is threading heavy) on a 72-core
machines (defaulting to a `CompactSizeClassMap` and no Quarantine):
- with tcmalloc: 337K reqs/sec, peak RSS of 338MB;
- with scudo (exclusive): 321K reqs/sec, peak RSS of 637MB;
- with scudo (shared): 241K reqs/sec, peak RSS of 324MB.

This isn't great, since the exclusive model uses a lot of memory, while the
shared model doesn't even come close to be competitive.

This is mostly due to the fact that we are consistently scanning the TSD pool
starting at index 0 for an available TSD, which can result in a lot of failed
lock attempts, and touching some memory that needs not be touched.

This CL attempts to make things better in most situations:
- first, use a thread local variable on Linux (intead of pthread APIs) to store
  the current TSD in the shared model;
- move the locking boolean out of the TSD: this allows the compiler to use a
  register and potentially optimize out a branch instead of reading it from the
  TSD everytime (we also save a tiny bit of memory per TSD);
- 64-bit atomic operations on 32-bit ARM platforms happen to be expensive: so
  store the `Precedence` in a `uptr` instead of a `u64`. We lose some
  nanoseconds of precision and we'll wrap around at some point, but the benefit
  is worth it;
- change a `CHECK` to a `DCHECK`: this should never happen, but if something is
  ever terribly wrong, we'll crash on a near null AV if the TSD happens to be
  null;
- based on an idea by dvyukov@, we are implementing a bound random scan for
  an available TSD. This requires computing the coprimes for the number of TSDs,
  and attempting to lock up to 4 TSDs in an random order before falling back to
  the current one. This is obviously slightly more expansive when we have just
  2 TSDs (barely noticeable) but is otherwise beneficial. The `Precedence` still
  basically corresponds to the moment of the first contention on a TSD. To seed
  on random choice, we use the precedence of the current TSD since it is very
  likely to be non-zero (since we are in the slow path after a failed `tryLock`)

With those modifications, the benchmark yields to:
- with scudo (shared): 330K reqs/sec, peak RSS of 327MB.

So the shared model for this specific situation not only becomes competitive but
outperforms the exclusive model. I experimented with some values greater than 4
for the number of TSDs to attempt to lock and it yielded a decrease in QPS. Just
sticking with the current TSD is also a tad slower. Numbers on platforms with
less cores (eg: Android) remain similar.

Reviewers: alekseyshl, dvyukov, javed.absar

Reviewed By: alekseyshl, dvyukov

Subscribers: srhines, kristof.beyls, delcypher, llvm-commits, #sanitizers

Differential Revision: https://reviews.llvm.org/D47289

llvm-svn: 334410
2018-06-11 14:50:31 +00:00
..
CMakeLists.txt [sanitizer] Split Symbolizer/StackTraces from core RTSanitizerCommon 2018-04-16 16:32:19 +00:00
scudo_allocator.cpp [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_allocator.h [scudo] Introduce Chunk::getHeaderSize 2018-02-27 16:14:49 +00:00
scudo_allocator_combined.h [scudo] Adding an interface function to print allocator stats 2018-04-25 18:52:29 +00:00
scudo_allocator_secondary.h [sanitizer] Allow for the allocator "names" to be set by the tools 2018-04-13 19:21:27 +00:00
scudo_crc32.cpp [scudo] CRC32 optimizations 2017-05-09 15:12:38 +00:00
scudo_crc32.h [sanitizer] Add Scudo to the sanitizer lint checks. 2017-11-08 16:42:29 +00:00
scudo_flags.cpp [scudo] Allow options to be defined at compile time 2018-02-08 16:29:48 +00:00
scudo_flags.h [scudo] 32-bit and hardware agnostic support 2016-11-30 17:32:20 +00:00
scudo_flags.inc [scudo] Quarantine overhaul 2017-07-24 15:29:38 +00:00
scudo_interface_internal.h [scudo] Adding an interface function to print allocator stats 2018-04-25 18:52:29 +00:00
scudo_malloc.cpp [scudo] Overhaul malloc related interceptors 2018-01-25 20:42:44 +00:00
scudo_new_delete.cpp [scudo] Application & platform compatibility changes 2017-08-16 16:40:48 +00:00
scudo_platform.h [scudo] Allow for weak hooks, gated by a define 2018-01-23 23:07:42 +00:00
scudo_termination.cpp [scudo] Make logging more consistent 2018-03-07 16:22:16 +00:00
scudo_tsd.h [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_tsd_exclusive.cpp [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_tsd_exclusive.inc [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_tsd_shared.cpp [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_tsd_shared.inc [scudo] Improve the scalability of the shared TSD model 2018-06-11 14:50:31 +00:00
scudo_utils.cpp [scudo] Read ARM feature bits using Fuchsia APIs. 2018-04-23 14:53:13 +00:00
scudo_utils.h [scudo] Minor code generation improvement 2017-12-08 16:36:37 +00:00