From d328d173fa54c35b78b1eef5edba797e1fa03fd4 Mon Sep 17 00:00:00 2001
From: Luite Stegeman <stegeman@gmail.com>
Date: Sun, 20 Oct 2024 20:31:27 +0000
Subject: [PATCH] Add requestTickyCounterSamples to GHC.Internal.Profiling

This allows the user to request ticky counters to be written to
the eventlog at specific times.

See #24645
---
 libraries/ghc-internal/src/GHC/Internal/Profiling.hs | 10 ++++++++++
 rts/RtsSymbols.c                                     |  1 +
 rts/Ticky.c                                          |  9 +++++++++
 rts/include/rts/Ticky.h                              |  2 ++
 4 files changed, 22 insertions(+)

diff --git a/libraries/ghc-internal/src/GHC/Internal/Profiling.hs b/libraries/ghc-internal/src/GHC/Internal/Profiling.hs
index 16f685155328..766a44b968ed 100644
--- a/libraries/ghc-internal/src/GHC/Internal/Profiling.hs
+++ b/libraries/ghc-internal/src/GHC/Internal/Profiling.hs
@@ -9,6 +9,8 @@ module GHC.Internal.Profiling ( -- * Cost Centre Profiling
                      , startHeapProfTimer
                      , stopHeapProfTimer
                      , requestHeapCensus
+                       -- * Ticky counters (eventlog)
+                     , requestTickyCounterSamples
                      )where
 
 import GHC.Internal.Base
@@ -51,3 +53,11 @@ foreign import ccall startHeapProfTimer :: IO ()
 -- @since base-4.16.0.0
 foreign import ccall stopHeapProfTimer :: IO ()
 
+-- | Request ticky counter samples to be written to the eventlog.
+--
+-- Note: This won't do anything unless you have specified RTS options on
+-- the command line to log ticky samples to the eventlog.
+--
+-- @since base-4.20.0.0
+
+foreign import ccall requestTickyCounterSamples :: IO ()
diff --git a/rts/RtsSymbols.c b/rts/RtsSymbols.c
index d4ce9acb9432..a447f126bd09 100644
--- a/rts/RtsSymbols.c
+++ b/rts/RtsSymbols.c
@@ -922,6 +922,7 @@ extern char **environ;
       SymI_HasProto(stopProfTimer)                                      \
       SymI_HasProto(startHeapProfTimer)                                 \
       SymI_HasProto(stopHeapProfTimer)                                  \
+      SymI_HasProto(requestTickyCounterSamples)                         \
       SymI_HasProto(setUserEra)                                         \
       SymI_HasProto(incrementUserEra)                                   \
       SymI_HasProto(getUserEra)                                         \
diff --git a/rts/Ticky.c b/rts/Ticky.c
index 65d52c670813..111bb19607fb 100644
--- a/rts/Ticky.c
+++ b/rts/Ticky.c
@@ -418,3 +418,12 @@ void emitTickyCounterSamples(void)
 }
 
 #endif /* TICKY_TICKY */
+
+void requestTickyCounterSamples(void)
+{
+#if defined(TICKY_TICKY) && defined(TRACING)
+    if (RtsFlags.TraceFlags.ticky) {
+        emitTickyCounterSamples();
+    }
+#endif
+}
diff --git a/rts/include/rts/Ticky.h b/rts/include/rts/Ticky.h
index 4d58c8e63afd..4b04d474dcd8 100644
--- a/rts/include/rts/Ticky.h
+++ b/rts/include/rts/Ticky.h
@@ -32,3 +32,5 @@ typedef struct _StgEntCounter {
     StgInt      allocs;         /* number of allocations by this fun */
     struct _StgEntCounter *link;/* link to chain them all together */
 } StgEntCounter;
+
+void requestTickyCounterSamples(void);
-- 
GitLab