From c843db5baa22a6b11f75a312a7bdaeb56a07f957 Mon Sep 17 00:00:00 2001 From: milen <94537774+taratorio@users.noreply.github.com> Date: Thu, 14 Nov 2024 05:38:23 +0000 Subject: [PATCH] dbg: add periodic save heap routine (#12698) --- erigon-lib/common/dbg/experiments.go | 19 +++++++++++++++++++ eth/backend.go | 1 + 2 files changed, 20 insertions(+) diff --git a/erigon-lib/common/dbg/experiments.go b/erigon-lib/common/dbg/experiments.go index f0fc9b593bf..6d9b29f4fc7 100644 --- a/erigon-lib/common/dbg/experiments.go +++ b/erigon-lib/common/dbg/experiments.go @@ -17,6 +17,7 @@ package dbg import ( + "context" "os" "path/filepath" "runtime" @@ -233,3 +234,21 @@ func SaveHeapProfileNearOOM(opts ...SaveHeapOption) { logger.Warn("[Experiment] could not write heap profile file", "err", err) } } + +func SaveHeapProfileNearOOMPeriodically(ctx context.Context, opts ...SaveHeapOption) { + if !saveHeapProfile { + return + } + + ticker := time.NewTicker(30 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + SaveHeapProfileNearOOM(opts...) + } + } +} diff --git a/eth/backend.go b/eth/backend.go index a049af4cb3a..0850a8d7018 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -503,6 +503,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger // setup periodic logging and prometheus updates go mem.LogMemStats(ctx, logger) go disk.UpdateDiskStats(ctx, logger) + go dbg.SaveHeapProfileNearOOMPeriodically(ctx, dbg.SaveHeapWithLogger(&logger)) var currentBlock *types.Block if err := backend.chainDB.View(context.Background(), func(tx kv.Tx) error {