diff options
author | Fabricio Voznika <fvoznika@google.com> | 2019-03-11 11:46:18 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2019-03-11 11:47:30 -0700 |
commit | bc9b979b9412ad5852872c1a9bee462f73d2455e (patch) | |
tree | ca7fda751568879d4ef08aee53d3a8298e1faf55 /runsc/cmd/debug.go | |
parent | 71d53382bfb3a6f05e90e31df8f39d22c0131040 (diff) |
Add profiling commands to runsc
Example:
runsc debug --root=<dir> \
--profile-heap=/tmp/heap.prof \
--profile-cpu=/tmp/cpu.prod --profile-delay=30 \
<container ID>
PiperOrigin-RevId: 237848456
Change-Id: Icff3f20c1b157a84d0922599eaea327320dad773
Diffstat (limited to 'runsc/cmd/debug.go')
-rw-r--r-- | runsc/cmd/debug.go | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/runsc/cmd/debug.go b/runsc/cmd/debug.go index e10326754..3ee9a9b49 100644 --- a/runsc/cmd/debug.go +++ b/runsc/cmd/debug.go @@ -16,7 +16,9 @@ package cmd import ( "context" + "os" "syscall" + "time" "flag" "github.com/google/subcommands" @@ -27,9 +29,12 @@ import ( // Debug implements subcommands.Command for the "debug" command. type Debug struct { - pid int - stacks bool - signal int + pid int + stacks bool + signal int + profileHeap string + profileCPU string + profileDelay int } // Name implements subcommands.Command. @@ -51,6 +56,9 @@ func (*Debug) Usage() string { func (d *Debug) SetFlags(f *flag.FlagSet) { f.IntVar(&d.pid, "pid", 0, "sandbox process ID. Container ID is not necessary if this is set") f.BoolVar(&d.stacks, "stacks", false, "if true, dumps all sandbox stacks to the log") + f.StringVar(&d.profileHeap, "profile-heap", "", "writes heap profile to the given file.") + f.StringVar(&d.profileCPU, "profile-cpu", "", "writes CPU profile to the given file.") + f.IntVar(&d.profileDelay, "profile-delay", 5, "amount of time to wait before stoping CPU profile") f.IntVar(&d.signal, "signal", -1, "sends signal to the sandbox") } @@ -114,5 +122,35 @@ func (d *Debug) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) } log.Infof(" *** Stack dump ***\n%s", stacks) } + if d.profileCPU != "" { + f, err := os.Create(d.profileCPU) + if err != nil { + Fatalf(err.Error()) + } + defer f.Close() + + if err := c.Sandbox.StartCPUProfile(f); err != nil { + Fatalf(err.Error()) + } + log.Infof("CPU profile started for %d sec, writing to %q", d.profileDelay, d.profileCPU) + time.Sleep(time.Duration(d.profileDelay) * time.Second) + + if err := c.Sandbox.StopCPUProfile(); err != nil { + Fatalf(err.Error()) + } + log.Infof("CPU profile written to %q", d.profileCPU) + } + if d.profileHeap != "" { + f, err := os.Create(d.profileHeap) + if err != nil { + Fatalf(err.Error()) + } + defer f.Close() + + if err := c.Sandbox.HeapProfile(f); err != nil { + Fatalf(err.Error()) + } + log.Infof("Heap profile written to %q", d.profileHeap) + } return subcommands.ExitSuccess } |