From 26991986100237dd096f97db2e24323539baef25 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Tue, 25 Apr 2017 11:21:37 +0530 Subject: [PATCH] Add flag --cpu-profile to enable pprof To collect CPU profile information added a flag `--cpu-profile` which is a path to file where this collected information will be dumped. Fixes #464 Signed-off-by: Suraj Deshmukh --- cmd/ocid/main.go | 15 +++++++++++++++ docs/ocid.8.md | 3 +++ 2 files changed, 18 insertions(+) diff --git a/cmd/ocid/main.go b/cmd/ocid/main.go index 086a62d1..e94b3d72 100644 --- a/cmd/ocid/main.go +++ b/cmd/ocid/main.go @@ -9,6 +9,8 @@ import ( "strings" "syscall" + "runtime/pprof" + "github.com/Sirupsen/logrus" "github.com/containers/storage/pkg/reexec" "github.com/kubernetes-incubator/cri-o/server" @@ -210,6 +212,10 @@ func main() { Name: "cni-plugin-dir", Usage: "CNI plugin binaries directory", }, + cli.StringFlag{ + Name: "cpu-profile", + Usage: "set the CPU profile file path", + }, } sort.Sort(cli.FlagsByName(app.Flags)) @@ -258,6 +264,15 @@ func main() { } app.Action = func(c *cli.Context) error { + if cp := c.GlobalString("cpu-profile"); cp != "" { + f, err := os.Create(cp) + if err != nil { + return fmt.Errorf("invalid --cpu-profile value %q", err) + } + pprof.StartCPUProfile(f) + defer pprof.StopCPUProfile() + } + config := c.App.Metadata["config"].(*server.Config) if !config.SELinux { diff --git a/docs/ocid.8.md b/docs/ocid.8.md index 89001e87..bebef93d 100644 --- a/docs/ocid.8.md +++ b/docs/ocid.8.md @@ -110,6 +110,9 @@ ocid is meant to provide an integration path between OCI conformant runtimes and **--cni-plugin-dir**="" CNI plugin binaries directory (default: "/opt/cni/bin/") +**--cpu-profile** + Set the CPU profile file path + **--version, -v** Print the version