diff --git a/daemons/compute/server/main.go b/daemons/compute/server/main.go index 2c26a5fa..8e20de78 100644 --- a/daemons/compute/server/main.go +++ b/daemons/compute/server/main.go @@ -34,6 +34,9 @@ import ( "github.com/takama/daemon" "google.golang.org/grpc" + "net/http" + _ "net/http/pprof" + pb "github.com/NearNodeFlash/nnf-dm/daemons/compute/client-go/api" "github.com/NearNodeFlash/nnf-dm/daemons/compute/server/auth" @@ -101,9 +104,24 @@ func (service *Service) Manage() (msg string, err error) { return fmt.Sprintf("Failed to set permissions on socket %s", *socketAddr), err } - // Enable CPU profiling with pprof - if len(options.CpuProfile) > 0 { - filename := options.CpuProfile + "-" + time.Now().UTC().Format(time.RFC3339) + // Print out tunable parameters + stdlog.Printf("GOMAXPROCS: %s\n", os.Getenv("GOMAXPROCS")) + stdlog.Printf("GOGC: %s\n", os.Getenv("GOGC")) + stdlog.Printf("GOMEMLIMIT: %s\n", os.Getenv("GOMEMLIMIT")) + + // Enable HTTP tracing. See https://pkg.go.dev/net/http/pprof for more details. + if options.Tracing { + go func() { + url := "localhost:6061" + stdlog.Printf("HTTP Tracing enabled at %s\n", url) + stdlog.Printf("HTTP Tracing output: %s", http.ListenAndServe(url, nil)) + }() + } + + // Enable CPU profiling with pprof. Daemon must be stopped for contents to be written to the + // file. See https://pkg.go.dev/runtime/pprof for more details. + if options.CpuProfile { + filename := fmt.Sprintf("/tmp/nnf-dm-cpu-%s.prof", time.Now().UTC().Format(time.RFC3339)) f, err := os.Create(filename) if err != nil { return fmt.Sprintf("could not create CPU profile"), err diff --git a/daemons/compute/server/servers/server.go b/daemons/compute/server/servers/server.go index 6c2afc4f..ae879e51 100644 --- a/daemons/compute/server/servers/server.go +++ b/daemons/compute/server/servers/server.go @@ -35,7 +35,8 @@ type ServerOptions struct { name string nodeName string sysConfig string - CpuProfile string + CpuProfile bool + Tracing bool simulated bool k8sQPS int @@ -50,7 +51,8 @@ func GetOptions() (*ServerOptions, error) { nodeName: os.Getenv("NNF_NODE_NAME"), tokenFile: os.Getenv("NNF_DATA_MOVEMENT_SERVICE_TOKEN_FILE"), certFile: os.Getenv("NNF_DATA_MOVEMENT_SERVICE_CERT_FILE"), - CpuProfile: os.Getenv("NNF_DATA_MOVEMENT_SERVICE_CPU_PROFILE"), + CpuProfile: false, + Tracing: false, simulated: false, // These options adjust the client-side rate-limiting for k8s. The new defaults are 50 and @@ -70,8 +72,10 @@ func GetOptions() (*ServerOptions, error) { flag.BoolVar(&opts.simulated, "simulated", opts.simulated, "Run in simulation mode where no requests are sent to the server") flag.IntVar(&opts.k8sQPS, "kubernetes-qps", opts.k8sQPS, "Kubernetes client queries per second (QPS)") flag.IntVar(&opts.k8sBurst, "kubernetes-burst", opts.k8sBurst, "Kubernetes client additional concurrent calls above QPS") - flag.StringVar(&opts.CpuProfile, "cpu-profile", opts.CpuProfile, - "Enable and dump CPU profiling data to this file after daemon is stopped. Timestamp is added to end of filename.") + flag.BoolVar(&opts.CpuProfile, "cpu-profile", opts.CpuProfile, + "Enable and dump CPU profiling data to `/tmp/nnf-dm-cpu-.prof`. Daemon must be stopped to dump profile.") + flag.BoolVar(&opts.Tracing, "tracing", opts.Tracing, + "Enable tracing via HTTP server") flag.Parse() return &opts, nil }