Skip to content

Commit

Permalink
add cpu profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushaga14 committed Dec 2, 2024
1 parent c71c912 commit 694e843
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ebpf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func run() {
doProfiling := false
trafficUtils.InitVar("AKTO_DEBUG_MEM_PROFILING", &doProfiling)

doProfilingCpu := false
trafficUtils.InitVar("AKTO_DEBUG_CPU_PROFILING", &doProfilingCpu)

if doProfiling {
ticker := time.NewTicker(time.Minute) // Create a ticker to trigger every minute
defer ticker.Stop()
Expand All @@ -194,6 +197,15 @@ func run() {
}
}

if doProfilingCpu {
ticker := time.NewTicker(time.Minute) // Create a ticker to trigger every minute
defer ticker.Stop()

for range ticker.C {
captureCpuProfile() // Capture memory profile every time the ticker ticks
}
}

sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
log.Println("Sniffer is ready")
Expand All @@ -207,3 +219,16 @@ func captureMemoryProfile() {

pprof.WriteHeapProfile(f) // Write memory profile
}

func captureCpuProfile() {
f, err := os.Create("cpu.prof")
if err != nil {
panic("could not create CPU profile: " + err.Error())
}
defer f.Close()

if err := pprof.StartCPUProfile(f); err != nil {
panic("could not start CPU profile: " + err.Error())
}
defer pprof.StopCPUProfile()
}

0 comments on commit 694e843

Please sign in to comment.