From 757e0c77e44317217f9eb26cc5e29d783e475abf Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Wed, 17 Apr 2024 09:56:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20provide=20a=20way=20to=20debug?= =?UTF-8?q?=20memory=20usage=20per=20query=20(#1261)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ivan Milchev --- policy/executor/internal/execution_manager.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/policy/executor/internal/execution_manager.go b/policy/executor/internal/execution_manager.go index 0ad23f6f..e6ee6abd 100644 --- a/policy/executor/internal/execution_manager.go +++ b/policy/executor/internal/execution_manager.go @@ -5,6 +5,8 @@ package internal import ( "errors" + "os" + "runtime" "sync" "time" @@ -12,6 +14,14 @@ import ( "go.mondoo.com/cnquery/v11/llx" ) +const MEM_DEBUG_ENV = "MEM_DEBUG" + +var memDebug = false + +func init() { + memDebug = os.Getenv(MEM_DEBUG_ENV) == "1" +} + type executionManager struct { runtime llx.Runtime // runQueue is the channel the execution manager will read @@ -168,6 +178,13 @@ func (em *executionManager) executeCodeBundle(codeBundle *llx.CodeBundle, props x.Run() } + if memDebug { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + log.Warn().Uint64("allocated", bToMb(m.Alloc)).Str("qrid", codeID).Msg("memory allocated after query") + } + if err != nil { return err } @@ -197,4 +214,8 @@ func (em *executionManager) executeCodeBundle(codeBundle *llx.CodeBundle, props return errOut } +func bToMb(b uint64) uint64 { + return b / 1024 / 1024 +} + var errQueryTimeout = errors.New("query execution timed out")