Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable CPU Profiling via CLI flag #4973

Merged
merged 5 commits into from
Aug 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add cleanup func in callback
  • Loading branch information
alexanderbez committed Aug 30, 2019
commit 5cc95688b2521f3607148e177b0b6c0e2af87c24
55 changes: 32 additions & 23 deletions server/start.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package server

// DONTCOVER

import (
"fmt"
"os"
"runtime/pprof"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/tendermint/tendermint/abci/server"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/node"
Expand Down Expand Up @@ -37,25 +37,11 @@ func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
Short: "Run the full node",
RunE: func(cmd *cobra.Command, args []string) error {
if !viper.GetBool(flagWithTendermint) {
ctx.Logger.Info("Starting ABCI without Tendermint")
ctx.Logger.Info("starting ABCI without Tendermint")
return startStandAlone(ctx, appCreator)
}

if cpuProfile := viper.GetString(flagCPUProfile); cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return err
}

ctx.Logger.Info("Starting CPU profiler. Writing results to: %s\n", cpuProfile)
if err := pprof.StartCPUProfile(f); err != nil {
return err
}

defer pprof.StopCPUProfile()
}

ctx.Logger.Info("Starting ABCI with Tendermint")
ctx.Logger.Info("starting ABCI with Tendermint")

_, err := startInProcess(ctx, appCreator)
return err
Expand Down Expand Up @@ -117,7 +103,6 @@ func startStandAlone(ctx *Context, appCreator AppCreator) error {

// run forever (the node will not be returned)
select {}

}

func startInProcess(ctx *Context, appCreator AppCreator) (*node.Node, error) {
Expand All @@ -142,6 +127,7 @@ func startInProcess(ctx *Context, appCreator AppCreator) (*node.Node, error) {
}

UpgradeOldPrivValFile(cfg)

// create & start tendermint node
tmNode, err := node.NewNode(
cfg,
Expand All @@ -157,19 +143,42 @@ func startInProcess(ctx *Context, appCreator AppCreator) (*node.Node, error) {
return nil, err
}

err = tmNode.Start()
if err != nil {
if err := tmNode.Start(); err != nil {
return nil, err
}

var cpuProfileCleanup func()

if cpuProfile := viper.GetString(flagCPUProfile); cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return nil, err
}

ctx.Logger.Info("starting CPU profiler", "profile", cpuProfile)
if err := pprof.StartCPUProfile(f); err != nil {
return nil, err
}

cpuProfileCleanup = func() {
ctx.Logger.Info("stopping CPU profiler", "profile", cpuProfile)
pprof.StopCPUProfile()
f.Close()
}
}

TrapSignal(func() {
if tmNode.IsRunning() {
_ = tmNode.Stop()
}

if cpuProfileCleanup != nil {
cpuProfileCleanup()
}

ctx.Logger.Info("exiting...")
})

// run forever (the node will not be returned)
select {}
}

// DONTCOVER