Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cpu-profile flag #2723

Merged
merged 8 commits into from Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion cmd/crowdsec/main.go
Expand Up @@ -6,6 +6,7 @@
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"

Expand Down Expand Up @@ -71,6 +72,7 @@
DisableCAPI bool
Transform string
OrderEvent bool
CpuProfile string
}

type labelsMap map[string]string
Expand Down Expand Up @@ -179,6 +181,7 @@
}

flag.StringVar(&dumpFolder, "dump-data", "", "dump parsers/buckets raw outputs")
flag.StringVar(&f.CpuProfile, "cpu-profile", "", "write cpu profile to file")
flag.Parse()
}

Expand Down Expand Up @@ -352,9 +355,24 @@
os.Exit(0)
}

if flags.CpuProfile != "" {
f, err := os.Create(flags.CpuProfile)
if err != nil {
log.Fatalf("could not create CPU profile: %s", err)
}
log.Infof("CPU profile will be written to %s", flags.CpuProfile)
if err := pprof.StartCPUProfile(f); err != nil {
f.Close()
log.Fatalf("could not start CPU profile: %s", err)
}
defer f.Close()
defer pprof.StopCPUProfile()

Check warning on line 369 in cmd/crowdsec/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/main.go#L359-L369

Added lines #L359 - L369 were not covered by tests
}

err := StartRunSvc()
if err != nil {
log.Fatal(err)
pprof.StopCPUProfile()
log.Fatal(err) //nolint:gocritic // Disable warning for the defer pprof.StopCPUProfile() call
Dismissed Show dismissed Hide dismissed
}

os.Exit(0)
Expand Down
5 changes: 5 additions & 0 deletions cmd/crowdsec/run_in_svc.go
Expand Up @@ -4,6 +4,7 @@ package main

import (
"fmt"
"runtime/pprof"

log "github.com/sirupsen/logrus"

Expand All @@ -22,6 +23,10 @@ func StartRunSvc() error {

defer trace.CatchPanic("crowdsec/StartRunSvc")

//Always try to stop CPU profiling to avoid passing flags around
//It's a noop if profiling is not enabled
defer pprof.StopCPUProfile()

if cConfig, err = LoadConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI, false); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/crowdsec/run_in_svc_windows.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"runtime/pprof"

log "github.com/sirupsen/logrus"
"golang.org/x/sys/windows/svc"
Expand All @@ -19,6 +20,10 @@ func StartRunSvc() error {

defer trace.CatchPanic("crowdsec/StartRunSvc")

//Always try to stop CPU profiling to avoid passing flags around
//It's a noop if profiling is not enabled
defer pprof.StopCPUProfile()

isRunninginService, err := svc.IsWindowsService()
if err != nil {
return fmt.Errorf("failed to determine if we are running in windows service mode: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/crowdsec/serve.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"

Expand Down Expand Up @@ -245,6 +246,10 @@ func HandleSignals(cConfig *csconfig.Config) error {

exitChan := make(chan error)

//Always try to stop CPU profiling to avoid passing flags around
//It's a noop if profiling is not enabled
defer pprof.StopCPUProfile()

go func() {
defer trace.CatchPanic("crowdsec/HandleSignals")
Loop:
Expand Down