Skip to content

Conversation

@cferris1000
Copy link
Contributor

Add a methodology to allow tracing. By default, this is disabled, but it can be enabled for any OS that supports it.

Currently, only releaseToOSXXX functions have trace points added.

Add a methodology to allow tracing. By default, this is disabled,
but it can be enabled for any OS that supports it.

Currently, only releaseToOSXXX functions have trace points added.
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2025

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

Author: Christopher Ferris (cferris1000)

Changes

Add a methodology to allow tracing. By default, this is disabled, but it can be enabled for any OS that supports it.

Currently, only releaseToOSXXX functions have trace points added.


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

5 Files Affected:

  • (modified) compiler-rt/lib/scudo/standalone/combined.h (+3-1)
  • (modified) compiler-rt/lib/scudo/standalone/primary32.h (+4)
  • (modified) compiler-rt/lib/scudo/standalone/primary64.h (+5)
  • (modified) compiler-rt/lib/scudo/standalone/secondary.h (+11-3)
  • (added) compiler-rt/lib/scudo/standalone/tracing.h (+48)
diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index 985bfb49884d1..c9ba28a52f780 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -25,6 +25,7 @@
 #include "size_class_allocator.h"
 #include "stack_depot.h"
 #include "string_utils.h"
+#include "tracing.h"
 #include "tsd.h"
 
 #include "scudo/interface.h"
@@ -671,10 +672,11 @@ class Allocator {
 
   void releaseToOS(ReleaseToOS ReleaseType) {
     initThreadMaybe();
+    SCUDO_SCOPED_TRACE(GetReleaseToOSTraceName(ReleaseType));
     if (ReleaseType == ReleaseToOS::ForceAll)
       drainCaches();
     Primary.releaseToOS(ReleaseType);
-    Secondary.releaseToOS();
+    Secondary.releaseToOS(ReleaseType);
   }
 
   // Iterate over all chunks and call a callback for all busy chunks located
diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h
index e2de50b93adc3..49aa74adfc10a 100644
--- a/compiler-rt/lib/scudo/standalone/primary32.h
+++ b/compiler-rt/lib/scudo/standalone/primary32.h
@@ -511,6 +511,8 @@ uptr SizeClassAllocator32<Config>::tryReleaseToOS(uptr ClassId,
 
 template <typename Config>
 uptr SizeClassAllocator32<Config>::releaseToOS(ReleaseToOS ReleaseType) {
+  SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSTraceName(ReleaseType));
+
   uptr TotalReleasedBytes = 0;
   for (uptr I = 0; I < NumClasses; I++) {
     if (I == SizeClassMap::BatchClassId)
@@ -1056,6 +1058,8 @@ uptr SizeClassAllocator32<Config>::releaseToOSMaybe(SizeClassInfo *Sci,
                                                     uptr ClassId,
                                                     ReleaseToOS ReleaseType)
     REQUIRES(Sci->Mutex) {
+  SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSMaybeTraceName(ReleaseType));
+
   const uptr BlockSize = getSizeByClassId(ClassId);
 
   DCHECK_GE(Sci->FreeListInfo.PoppedBlocks, Sci->FreeListInfo.PushedBlocks);
diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h
index 3cb040c514eda..7727049426b47 100644
--- a/compiler-rt/lib/scudo/standalone/primary64.h
+++ b/compiler-rt/lib/scudo/standalone/primary64.h
@@ -22,6 +22,7 @@
 #include "stats.h"
 #include "string_utils.h"
 #include "thread_annotations.h"
+#include "tracing.h"
 
 namespace scudo {
 
@@ -1307,6 +1308,8 @@ uptr SizeClassAllocator64<Config>::tryReleaseToOS(uptr ClassId,
 
 template <typename Config>
 uptr SizeClassAllocator64<Config>::releaseToOS(ReleaseToOS ReleaseType) {
+  SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSTraceName(ReleaseType));
+
   uptr TotalReleasedBytes = 0;
   for (uptr I = 0; I < NumClasses; I++) {
     if (I == SizeClassMap::BatchClassId)
@@ -1376,6 +1379,8 @@ uptr SizeClassAllocator64<Config>::releaseToOSMaybe(RegionInfo *Region,
                                                     uptr ClassId,
                                                     ReleaseToOS ReleaseType)
     REQUIRES(Region->MMLock) EXCLUDES(Region->FLLock) {
+  SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSMaybeTraceName(ReleaseType));
+
   const uptr BlockSize = getSizeByClassId(ClassId);
   uptr BytesInFreeList;
   const uptr AllocatedUserEnd =
diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index 38c9a9e6e2d70..c9145002c667c 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -19,6 +19,7 @@
 #include "stats.h"
 #include "string_utils.h"
 #include "thread_annotations.h"
+#include "tracing.h"
 #include "vector.h"
 
 namespace scudo {
@@ -118,7 +119,7 @@ template <typename Config> class MapAllocatorNoCache {
   bool canCache(UNUSED uptr Size) { return false; }
   void disable() {}
   void enable() {}
-  void releaseToOS() {}
+  void releaseToOS(ReleaseToOS) {}
   void disableMemoryTagging() {}
   void unmapTestOnly() {}
   bool setOption(Option O, UNUSED sptr Value) {
@@ -351,6 +352,9 @@ class MapAllocatorCache {
       // same time will not actually release any extra elements. Therefore,
       // let any other thread continue, skipping the release.
       if (Mutex.tryLock()) {
+        SCUDO_SCOPED_TRACE(
+            GetSecondaryReleaseToOSTraceName(ReleaseToOS::Normal));
+
         // TODO: Add ReleaseToOS logic to LRU algorithm
         releaseOlderThan(Time - static_cast<u64>(Interval) * 1000000);
         Mutex.unlock();
@@ -499,7 +503,9 @@ class MapAllocatorCache {
     return true;
   }
 
-  void releaseToOS() EXCLUDES(Mutex) {
+  void releaseToOS([[maybe_unused]] ReleaseToOS ReleaseType) EXCLUDES(Mutex) {
+    SCUDO_SCOPED_TRACE(GetSecondaryReleaseToOSTraceName(ReleaseType));
+
     // Since this is a request to release everything, always wait for the
     // lock so that we guarantee all entries are released after this call.
     ScopedLock L(Mutex);
@@ -574,6 +580,8 @@ class MapAllocatorCache {
   }
 
   void releaseOlderThan(u64 Time) REQUIRES(Mutex) {
+    SCUDO_SCOPED_TRACE(GetSecondaryReleaseOlderThan());
+
     if (!LRUEntries.size() || OldestTime == 0 || OldestTime > Time)
       return;
     OldestTime = 0;
@@ -669,7 +677,7 @@ template <typename Config> class MapAllocator {
 
   bool setOption(Option O, sptr Value) { return Cache.setOption(O, Value); }
 
-  void releaseToOS() { Cache.releaseToOS(); }
+  void releaseToOS(ReleaseToOS ReleaseType) { Cache.releaseToOS(ReleaseType); }
 
   void disableMemoryTagging() { Cache.disableMemoryTagging(); }
 
diff --git a/compiler-rt/lib/scudo/standalone/tracing.h b/compiler-rt/lib/scudo/standalone/tracing.h
new file mode 100644
index 0000000000000..c21081c047931
--- /dev/null
+++ b/compiler-rt/lib/scudo/standalone/tracing.h
@@ -0,0 +1,48 @@
+//===-- tracing.h -----------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SCUDO_TRACING_H_
+#define SCUDO_TRACING_H_
+
+#if defined(SCUDO_ENABLE_TRACING)
+
+// This file must include definitions for all of the functions below.
+#include "custom_scudo_tracing.h"
+
+#else
+
+// Should start a trace in the given scope, and end the trace when going out of
+// scope.
+#define SCUDO_SCOPED_TRACE(Name)
+
+// Create a trace name for the call to releaseToOS.
+static inline const char *GetReleaseToOSTraceName(scudo::ReleaseToOS) {
+  return nullptr;
+}
+
+// Create a trace name for the call to releaseToOSMaybe in the primary.
+static inline const char *
+GetPrimaryReleaseToOSMaybeTraceName(scudo::ReleaseToOS) {
+  return nullptr;
+}
+
+static inline const char *GetPrimaryReleaseToOSTraceName(scudo::ReleaseToOS) {
+  return nullptr;
+}
+
+// Create a trace name for the call to releaseToOS in the secondary.
+static inline const char *GetSecondaryReleaseToOSTraceName(scudo::ReleaseToOS) {
+  return nullptr;
+}
+
+// Create a trace name for the call to releaseOlderThan in the secondary.
+static inline const char *GetSecondaryReleaseOlderThan() { return nullptr; }
+
+#endif
+
+#endif // SCUDO_TRACING_H_

Copy link
Contributor

@ChiaHungDuan ChiaHungDuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

uptr ClassId,
ReleaseToOS ReleaseType)
REQUIRES(Region->MMLock) EXCLUDES(Region->FLLock) {
SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSMaybeTraceName(ReleaseType));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, are we able to log the ClassId in trace?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that helps as an inspiration, Fuchsia's tracing system lets you attach arbitrary arguments (including integers) to trace events. But it relies on sophisticated C preprocessing (example and implementation).

Maybe we could expose individual trace points directly as macros to give maximum flexibility? For instance

#if defined(SCUDO_ENABLE_TRACING)
#include "custom_scudo_tracing.h"
#else
#define TRACE_PRIMARY_RELEASE_TO_OS_MAYBE(ClassId)
... // and so on, one empty macro (instead of a NULL-returning function) per trace point
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can work on getting better trace data after this. I would like a better way to generate the names that can happen at compile time. I'll definitely look at that for a follow-on cl.

}

// Create a trace name for the call to releaseOlderThan in the secondary.
static inline const char *GetSecondaryReleaseOlderThan() { return nullptr; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you meant GetSecondaryReleaseOlderThanTraceName (should we put all trace names in a dedicated namespace?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this name and the place it was used.

@cferris1000 cferris1000 merged commit 7628abd into llvm:main Sep 11, 2025
9 checks passed
@cferris1000 cferris1000 deleted the tracing branch November 11, 2025 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants