Skip to content

Commit

Permalink
🔨 provide a way to debug memory usage per query (#1261)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <ivan@mondoo.com>
  • Loading branch information
imilchev authored Apr 17, 2024
1 parent 849ce7f commit 757e0c7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions policy/executor/internal/execution_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ package internal

import (
"errors"
"os"
"runtime"
"sync"
"time"

"github.com/rs/zerolog/log"
"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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")

0 comments on commit 757e0c7

Please sign in to comment.