summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2025-03-02 22:18:36 +0100
committerTom Smeding <tom@tomsmeding.com>2025-03-02 22:18:36 +0100
commitdccb5f2a0e92a568961e60e3e2ba3dfb4316c663 (patch)
treec22f7923c74e0de97cb0444cf6d29b25a654bbb8
parent52184258f8eef227c743726770e984202e101919 (diff)
Compile.Exec: Show number of loaded kernels in debug prints
-rw-r--r--src/Compile/Exec.hs11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/Compile/Exec.hs b/src/Compile/Exec.hs
index 7c5cb79..487ed8a 100644
--- a/src/Compile/Exec.hs
+++ b/src/Compile/Exec.hs
@@ -14,6 +14,7 @@ import Foreign.Ptr (FunPtr)
import System.Directory (removeDirectoryRecursive)
import System.Environment (lookupEnv)
import System.IO (hPutStrLn, stderr)
+import System.IO.Unsafe (unsafePerformIO)
import System.Posix.DynamicLinker
import System.Posix.Temp (mkdtemp)
import System.Process (readProcess)
@@ -31,14 +32,16 @@ buildKernel csource funnames = do
let args = ["-O3", "-march=native", "-shared", "-fPIC", "-std=c99", "-x", "c", "-o", outso, "-", "-Wall", "-Wextra", "-Wno-unused-parameter"]
_ <- readProcess "gcc" args csource
- hPutStrLn stderr $ "[chad] loading kernel " ++ path
+ numLoaded <- atomicModifyIORef' numLoadedCounter (\n -> (n+1, n+1))
+ hPutStrLn stderr $ "[chad] loading kernel " ++ path ++ " (" ++ show numLoaded ++ " total)"
dl <- dlopen outso [RTLD_LAZY, RTLD_LOCAL]
removeDirectoryRecursive path -- we keep a reference anyway because we have the file open now
ptrs <- Map.fromList <$> sequence [(name,) <$> dlsym dl name | name <- funnames]
ref <- newIORef ptrs
- _ <- mkWeakIORef ref (do hPutStrLn stderr $ "[chad] unloading kernel " ++ path
+ _ <- mkWeakIORef ref (do numLeft <- atomicModifyIORef' numLoadedCounter (\n -> (n-1, n-1))
+ hPutStrLn stderr $ "[chad] unloading kernel " ++ path ++ " (" ++ show numLeft ++ " left)"
dlclose dl)
return (KernelLib ref)
@@ -57,3 +60,7 @@ getTempDir =
lookupEnv "TMPDIR" >>= \case
Just s | not (null s) -> return s
_ -> return "/tmp"
+
+{-# NOINLINE numLoadedCounter #-}
+numLoadedCounter :: IORef Int
+numLoadedCounter = unsafePerformIO $ newIORef 0