mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +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 (
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user