17574: add -pprof flag to service commands.
[arvados.git] / lib / service / cmd.go
index 9ca24312582060d42f6ed878f600c780ae9d5872..40db4f9c7c7f80f744ab3c44da874794d925c9c1 100644 (file)
@@ -12,6 +12,7 @@ import (
        "io"
        "net"
        "net/http"
+       _ "net/http/pprof"
        "net/url"
        "os"
        "strings"
@@ -70,6 +71,7 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        loader := config.NewLoader(stdin, log)
        loader.SetupFlags(flags)
        versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
+       pprofAddr := flags.String("pprof", "", "Serve Go profile data at `[addr]:port`")
        err = flags.Parse(args)
        if err == flag.ErrHelp {
                err = nil
@@ -80,6 +82,12 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
                return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
        }
 
+       if *pprofAddr != "" {
+               go func() {
+                       log.Println(http.ListenAndServe(*pprofAddr, nil))
+               }()
+       }
+
        if strings.HasSuffix(prog, "controller") {
                // Some config-loader checks try to make API calls via
                // controller. Those can't be expected to work if this