Skip to content

Commit ceed3b5

Browse files
committed
smp-server: add periodic memory diagnostics logging
Log sizes of all in-memory data structures every 5 minutes to help identify memory growth root cause on busy servers.
1 parent f291b61 commit ceed3b5

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/Simplex/Messaging/Server.hs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ import qualified Data.X509 as X
8989
import qualified Data.X509.Validation as XV
9090
import GHC.Conc.Signal
9191
import GHC.IORef (atomicSwapIORef)
92-
import GHC.Stats (getRTSStats)
92+
import GHC.Stats (RTSStats (..), GCDetails (..), getRTSStats)
9393
import GHC.TypeLits (KnownNat)
9494
import Network.Socket (ServiceName, Socket, socketToHandle)
9595
import qualified Network.TLS as TLS
@@ -196,6 +196,7 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
196196
<> serverStatsThread_ cfg
197197
<> prometheusMetricsThread_ cfg
198198
<> controlPortThread_ cfg
199+
<> [memoryDiagThread]
199200
)
200201
`finally` stopServer s
201202
where
@@ -716,6 +717,60 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
716717
Nothing -> acc
717718
Just (_, ts) -> (cnt + 1, updateTimeBuckets ts ts' times)
718719

720+
memoryDiagThread :: M s ()
721+
memoryDiagThread = do
722+
labelMyThread "memoryDiag"
723+
Env
724+
{ ntfStore = NtfStore ntfMap,
725+
server = srv@Server {subscribers, ntfSubscribers},
726+
proxyAgent = ProxyAgent {smpAgent = pa},
727+
msgStore_ = ms
728+
} <- ask
729+
let SMPClientAgent {smpClients, smpSessions, activeServiceSubs, activeQueueSubs, pendingServiceSubs, pendingQueueSubs, smpSubWorkers} = pa
730+
liftIO $ forever $ do
731+
threadDelay 300_000_000 -- 5 minutes
732+
rts <- getRTSStats
733+
let GCDetails {gcdetails_live_bytes, gcdetails_mem_in_use_bytes} = gc rts
734+
clientCount <- IM.size <$> getServerClients srv
735+
smpQSubs <- M.size <$> getSubscribedClients (queueSubscribers subscribers)
736+
smpSSubs <- M.size <$> getSubscribedClients (serviceSubscribers subscribers)
737+
ntfQSubs <- M.size <$> getSubscribedClients (queueSubscribers ntfSubscribers)
738+
ntfSSubs <- M.size <$> getSubscribedClients (serviceSubscribers ntfSubscribers)
739+
smpPending <- IM.size <$> readTVarIO (pendingEvents subscribers)
740+
ntfPending <- IM.size <$> readTVarIO (pendingEvents ntfSubscribers)
741+
ntfStoreSize <- M.size <$> readTVarIO ntfMap
742+
paClients' <- M.size <$> readTVarIO smpClients
743+
paSessions' <- M.size <$> readTVarIO smpSessions
744+
paActSvc <- M.size <$> readTVarIO activeServiceSubs
745+
paActQ <- M.size <$> readTVarIO activeQueueSubs
746+
paPndSvc <- M.size <$> readTVarIO pendingServiceSubs
747+
paPndQ <- M.size <$> readTVarIO pendingQueueSubs
748+
paWorkers <- M.size <$> readTVarIO smpSubWorkers
749+
lc <- loadedQueueCounts $ fromMsgStore ms
750+
logInfo $
751+
"MEMORY"
752+
<> " rts_live=" <> tshow gcdetails_live_bytes
753+
<> " rts_heap=" <> tshow gcdetails_mem_in_use_bytes
754+
<> " rts_gc=" <> tshow (gcs rts)
755+
<> " clients=" <> tshow clientCount
756+
<> " smpQSubs=" <> tshow smpQSubs
757+
<> " smpSSubs=" <> tshow smpSSubs
758+
<> " ntfQSubs=" <> tshow ntfQSubs
759+
<> " ntfSSubs=" <> tshow ntfSSubs
760+
<> " smpPending=" <> tshow smpPending
761+
<> " ntfPending=" <> tshow ntfPending
762+
<> " ntfStore=" <> tshow ntfStoreSize
763+
<> " paClients=" <> tshow paClients'
764+
<> " paSessions=" <> tshow paSessions'
765+
<> " paActSvc=" <> tshow paActSvc
766+
<> " paActQ=" <> tshow paActQ
767+
<> " paPndSvc=" <> tshow paPndSvc
768+
<> " paPndQ=" <> tshow paPndQ
769+
<> " paWorkers=" <> tshow paWorkers
770+
<> " loadedQ=" <> tshow (loadedQueueCount lc)
771+
<> " loadedNtf=" <> tshow (loadedNotifierCount lc)
772+
<> " ntfLocks=" <> tshow (notifierLockCount lc)
773+
719774
runClient :: Transport c => X.CertificateChain -> C.APrivateSignKey -> TProxy c 'TServer -> c 'TServer -> M s ()
720775
runClient srvCert srvSignKey tp h = do
721776
ms <- asks msgStore

0 commit comments

Comments
 (0)