Skip to content

Commit 630dacc

Browse files
committed
feat: add pprof file flag for memory profiling
Signed-off-by: Evsyukov Denis <[email protected]>
1 parent 35310d8 commit 630dacc

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

cmd/dmt/main.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ package main
1818

1919
import (
2020
"context"
21+
"errors"
2122
"os"
23+
"runtime"
24+
"runtime/pprof"
2225

2326
"github.com/fatih/color"
2427

28+
"github.com/deckhouse/dmt/internal/flags"
29+
"github.com/deckhouse/dmt/internal/fsutils"
2530
"github.com/deckhouse/dmt/internal/logger"
2631
"github.com/deckhouse/dmt/internal/manager"
2732
"github.com/deckhouse/dmt/internal/metrics"
@@ -32,7 +37,32 @@ func main() {
3237
execute()
3338
}
3439

35-
func runLint(dir string) {
40+
func runLint(dir string) error {
41+
if flags.PprofFile != "" {
42+
logger.InfoF("Profiling enabled. Profile file: %s", flags.PprofFile)
43+
defer func() {
44+
pproFile, err := fsutils.ExpandDir(flags.PprofFile)
45+
if err != nil {
46+
logger.ErrorF("could not get current working directory: %s", err)
47+
return
48+
}
49+
logger.InfoF("Writing memory profile to %s", pproFile)
50+
f, err := os.Create(pproFile)
51+
if err != nil {
52+
logger.ErrorF("could not create memory profile: %s", err)
53+
return
54+
}
55+
defer f.Close()
56+
runtime.GC()
57+
// Lookup("allocs") creates a profile similar to go test -memprofile.
58+
// Alternatively, use Lookup("heap") for a profile
59+
// that has inuse_space as the default index.
60+
if err := pprof.Lookup("allocs").WriteTo(f, 0); err != nil {
61+
logger.ErrorF("could not write memory profile: %s", err)
62+
return
63+
}
64+
}()
65+
}
3666
color.NoColor = false
3767
logger.InfoF("DMT version: %s", version)
3868
logger.InfoF("Dir: %v", dir)
@@ -56,6 +86,8 @@ func runLint(dir string) {
5686
metricsClient.Send(context.Background())
5787

5888
if mng.HasCriticalErrors() {
59-
os.Exit(1)
89+
return errors.New("critical errors found")
6090
}
91+
92+
return nil
6193
}

cmd/dmt/root.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,7 @@ func lintCmdFunc(_ *cobra.Command, args []string) {
9393
return
9494
}
9595

96-
runLint(dir)
96+
if err := runLint(dir); err != nil {
97+
os.Exit(1)
98+
}
9799
}

internal/flags/flags.go

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
PrintVersion bool
3535
Version string
3636
ValuesFile string
37+
PprofFile string
3738
)
3839

3940
func InitDefaultFlagSet() *pflag.FlagSet {
@@ -52,6 +53,7 @@ func InitLintFlagSet() *pflag.FlagSet {
5253
lint.StringVar(&LinterName, "linter", "", "linter name to run")
5354
lint.StringVarP(&LogLevel, "log-level", "l", "INFO", "log-level [DEBUG | INFO | WARN | ERROR]")
5455
lint.StringVarP(&ValuesFile, "values-file", "f", "", "path to values.yaml file with override values")
56+
lint.StringVarP(&PprofFile, "pprof-file", "r", "", "path to pprof file")
5557

5658
return lint
5759
}

0 commit comments

Comments
 (0)