Merge pull request #96019 from rikatz/96017

Kubectl - flush profiling when interrupting a long running command
This commit is contained in:
Kubernetes Prow Robot 2020-11-02 16:16:28 -08:00 committed by GitHub
commit 3c2da35b2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
@ -44,15 +45,16 @@ func initProfiling() error {
if err != nil { if err != nil {
return err return err
} }
return pprof.StartCPUProfile(f) err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
// Block and mutex profiles need a call to Set{Block,Mutex}ProfileRate to // Block and mutex profiles need a call to Set{Block,Mutex}ProfileRate to
// output anything. We choose to sample all events. // output anything. We choose to sample all events.
case "block": case "block":
runtime.SetBlockProfileRate(1) runtime.SetBlockProfileRate(1)
return nil
case "mutex": case "mutex":
runtime.SetMutexProfileFraction(1) runtime.SetMutexProfileFraction(1)
return nil
default: default:
// Check the profile name is valid. // Check the profile name is valid.
if profile := pprof.Lookup(profileName); profile == nil { if profile := pprof.Lookup(profileName); profile == nil {
@ -60,6 +62,16 @@ func initProfiling() error {
} }
} }
// If the command is interrupted before the end (ctrl-c), flush the
// profiling files
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
flushProfiling()
os.Exit(0)
}()
return nil return nil
} }