[compiler-rt] [scudo] Reduce thread contention in secondary cache releases. (PR #102984)
Joshua Baehring via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 12 20:53:45 PDT 2024
https://github.com/JoshuaMBa updated https://github.com/llvm/llvm-project/pull/102984
>From 1b0e35474b144091543ecc132a74aec36669d8f9 Mon Sep 17 00:00:00 2001
From: Joshua Baehring <jmbaehring at google.com>
Date: Mon, 12 Aug 2024 23:27:13 +0000
Subject: [PATCH] [scudo] Reduce thread contention in secondary cache releases.
Threads will skip the releaseOlderThan() call if another thread holds
the secondary cache lock because the other thread holding the
lock can release the old cache entries instead. This way threads will
not be backed up waiting to acquire the cache lock while another thread
is already performing the necessary releases.
---
compiler-rt/lib/scudo/standalone/secondary.h | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index e3dc8d4ef8247c..d96e2ddcb12393 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -351,9 +351,14 @@ class MapAllocatorCache {
for (MemMapT &EvictMemMap : EvictionMemMaps)
unmapCallBack(EvictMemMap);
- if (Interval >= 0) {
+ // If a thread already holds the mutex, the current thread will
+ // skip the release logic to reduce thread contention and leave
+ // the responsibility of releases to the next uncontended
+ // thread
+ if (Interval >= 0 && Mutex.tryLock()) {
// TODO: Add ReleaseToOS logic to LRU algorithm
releaseOlderThan(Time - static_cast<u64>(Interval) * 1000000);
+ Mutex.unlock();
}
}
@@ -455,7 +460,10 @@ class MapAllocatorCache {
return true;
}
- void releaseToOS() { releaseOlderThan(UINT64_MAX); }
+ void releaseToOS() {
+ ScopedLock L(Mutex);
+ releaseOlderThan(UINT64_MAX);
+ }
void disableMemoryTagging() EXCLUDES(Mutex) {
ScopedLock L(Mutex);
@@ -597,8 +605,7 @@ class MapAllocatorCache {
Entry.Time = 0;
}
- void releaseOlderThan(u64 Time) EXCLUDES(Mutex) {
- ScopedLock L(Mutex);
+ void releaseOlderThan(u64 Time) REQUIRES(Mutex) {
if (!EntriesCount || OldestTime == 0 || OldestTime > Time)
return;
OldestTime = 0;
More information about the llvm-commits
mailing list