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

feat(server/v2): enable pprof #22284

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
40 changes: 35 additions & 5 deletions server/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"strings"
"syscall"

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

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
)

// Execute executes the root command of an application.
Expand Down Expand Up @@ -127,11 +129,9 @@ func createStartCommand[T transaction.Tx](
}
}()

if err := server.Start(ctx); err != nil {
return err
}

return nil
return wrapCPUProfile(l, v, func() error {
return server.Start(ctx)
})
},
}

Expand All @@ -143,6 +143,36 @@ func createStartCommand[T transaction.Tx](
return cmd
}

// wrapCPUProfile starts CPU profiling, if enabled, and executes the provided
// callbackFn in a separate goroutine, then will wait for that callback to return.
func wrapCPUProfile(logger log.Logger, v *viper.Viper, callbackFn func() error) error {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
cpuProfileFile := v.GetString(FlagCPUProfiling)
if len(cpuProfileFile) == 0 {
// if cpu profiling is not enabled, just run the callback
return callbackFn()
}

f, err := os.Create(cpuProfileFile)
if err != nil {
return err
}
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

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

defer func() {
logger.Info("stopping CPU profiler", "profile", cpuProfileFile)
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
logger.Info("failed to close cpu-profile file", "profile", cpuProfileFile, "err", err.Error())
}
}()

return callbackFn()
}

// configHandle writes the default config to the home directory if it does not exist and sets the server context
func configHandle[T transaction.Tx](s *Server[T], cmd *cobra.Command) error {
home, err := cmd.Flags().GetString(FlagHome)
Expand Down
5 changes: 4 additions & 1 deletion server/v2/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ func prefix(f string) string {
return fmt.Sprintf("%s.%s", serverName, f)
}

var FlagMinGasPrices = prefix("minimum-gas-prices")
var (
FlagMinGasPrices = prefix("minimum-gas-prices")
FlagCPUProfiling = prefix("cpu-profile")
)

const (
// FlagHome specifies the home directory flag.
Expand Down
2 changes: 2 additions & 0 deletions server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func (s *Server[T]) Configs() map[string]any {
func (s *Server[T]) StartCmdFlags() *pflag.FlagSet {
flags := pflag.NewFlagSet(s.Name(), pflag.ExitOnError)
flags.String(FlagMinGasPrices, "", "Minimum gas prices to accept for transactions; Any fee in a tx must meet this minimum (e.g. 0.01photino;0.0001stake)")
flags.String(FlagCPUProfiling, "", "Enable CPU profiling and write to the specified file")

return flags
}

Expand Down
Loading