From e2171a0ca07b38932a3478dea0dfd2d9fb97b83f Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Tue, 1 Mar 2016 16:10:38 +0900 Subject: [PATCH] cli: add an options to enable profiling $ gobgp -r 10000 monitor global rib this will enable profiling through tcp port 10000 $ go tool pprof 'http://localhost:10000/debug/pprof/profile?seconds=10' Signed-off-by: ISHIDA Wataru --- gobgp/cmd/root.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gobgp/cmd/root.go b/gobgp/cmd/root.go index 58028881..dcd69d8b 100644 --- a/gobgp/cmd/root.go +++ b/gobgp/cmd/root.go @@ -16,8 +16,12 @@ package cmd import ( + "fmt" api "github.com/osrg/gobgp/api" "github.com/spf13/cobra" + "net/http" + _ "net/http/pprof" + "os" ) var globalOpts struct { @@ -28,6 +32,7 @@ var globalOpts struct { Json bool GenCmpl bool BashCmplFile string + PprofPort int } var cmds []string @@ -38,6 +43,15 @@ func NewRootCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "gobgp", PersistentPreRun: func(cmd *cobra.Command, args []string) { + if globalOpts.PprofPort > 0 { + go func() { + if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", globalOpts.PprofPort), nil); err != nil { + fmt.Println(err) + os.Exit(1) + } + }() + } + if !globalOpts.GenCmpl { conn := connGrpc() client = api.NewGobgpApiClient(conn) @@ -59,6 +73,7 @@ func NewRootCmd() *cobra.Command { rootCmd.PersistentFlags().BoolVarP(&globalOpts.Quiet, "quiet", "q", false, "use quiet") rootCmd.PersistentFlags().BoolVarP(&globalOpts.GenCmpl, "gen-cmpl", "c", false, "generate completion file") rootCmd.PersistentFlags().StringVarP(&globalOpts.BashCmplFile, "bash-cmpl-file", "", "gobgp-completion.bash", "bash cmpl filename") + rootCmd.PersistentFlags().IntVarP(&globalOpts.PprofPort, "pprof-port", "r", 0, "pprof port") globalCmd := NewGlobalCmd() neighborCmd := NewNeighborCmd()