[compiler-rt] [scudo] Reduce thread contention in secondary cache releases. (PR #102984)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 16:32:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Joshua Baehring (JoshuaMBa)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/102984.diff


1 Files Affected:

- (modified) compiler-rt/lib/scudo/standalone/secondary.h (+7-4) 


``````````diff
diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index e3dc8d4ef8247c..39610a8be2c428 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -351,9 +351,10 @@ class MapAllocatorCache {
     for (MemMapT &EvictMemMap : EvictionMemMaps)
       unmapCallBack(EvictMemMap);
 
-    if (Interval >= 0) {
+    if (Interval >= 0 && Mutex.tryLock()) {
       // TODO: Add ReleaseToOS logic to LRU algorithm
       releaseOlderThan(Time - static_cast<u64>(Interval) * 1000000);
+      Mutex.unlock();
     }
   }
 
@@ -455,7 +456,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 +601,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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/102984


More information about the llvm-commits mailing list