mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #96019 from rikatz/96017
Kubectl - flush profiling when interrupting a long running command
This commit is contained in:
commit
3c2da35b2a
@ -19,6 +19,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
|
||||
@ -44,15 +45,16 @@ func initProfiling() error {
|
||||
if err != nil {
|
||||
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
|
||||
// output anything. We choose to sample all events.
|
||||
case "block":
|
||||
runtime.SetBlockProfileRate(1)
|
||||
return nil
|
||||
case "mutex":
|
||||
runtime.SetMutexProfileFraction(1)
|
||||
return nil
|
||||
default:
|
||||
// Check the profile name is valid.
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user