diff --git a/cmd/lavad/main.go b/cmd/lavad/main.go index a0227fa012..cc37032859 100644 --- a/cmd/lavad/main.go +++ b/cmd/lavad/main.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" + _ "net/http/pprof" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" @@ -97,6 +99,23 @@ func main() { utils.LavaFormatFatal("failed to read log level flag", err, nil) } utils.LoggingLevel(logLevel) + + // check if the command includes --pprof-address + pprofAddressFlagUsed := cmd.Flags().Lookup("pprof-address").Changed + if pprofAddressFlagUsed { + // get pprof server ip address (default value: "") + pprofServerAddress, err := cmd.Flags().GetString("pprof-address") + if err != nil { + utils.LavaFormatFatal("failed to read pprof address flag", err, nil) + } + + // start pprof HTTP server + err = performance.StartPprofServer(pprofServerAddress) + if err != nil { + return utils.LavaFormatError("failed to start pprof HTTP server", err, nil) + } + } + relayer.PortalServer(ctx, clientCtx, listenAddr, chainID, apiInterface, cmd.Flags()) return nil @@ -147,6 +166,7 @@ func main() { cmdTestClient.MarkFlagRequired(flags.FlagFrom) cmdTestClient.Flags().Bool("secure", false, "secure sends reliability on every message") cmdPortalServer.Flags().Bool("secure", false, "secure sends reliability on every message") + cmdPortalServer.Flags().String(performance.PprofAddressFlagName, "", "pprof server address, used for code profiling") cmdPortalServer.Flags().String(performance.CacheFlagName, "", "address for a cache server to improve performance") cmdServer.Flags().String(performance.CacheFlagName, "", "address for a cache server to improve performance") rootCmd.AddCommand(cmdServer) diff --git a/relayer/performance/pprofServer.go b/relayer/performance/pprofServer.go new file mode 100644 index 0000000000..92e69e5f5d --- /dev/null +++ b/relayer/performance/pprofServer.go @@ -0,0 +1,33 @@ +package performance + +import ( + "fmt" + + "github.com/gofiber/fiber/v2" + fiberpprof "github.com/gofiber/fiber/v2/middleware/pprof" + "github.com/lavanet/lava/utils" +) + +const ( + PprofAddressFlagName = "pprof-address" +) + +func StartPprofServer(addr string) error { + // Set up the Fiber app + app := fiber.New() + + // Let the fiber HTTP server use pprof + app.Use(fiberpprof.New()) + + // Start the HTTP server in a goroutine + go func() { + fmt.Printf("Starting server on %s\n", addr) + if err := app.Listen(addr); err != nil { + fmt.Printf("Error starting pprof HTTP server: %s\n", err) + } + }() + + utils.LavaFormatInfo("start pprof HTTP server", &map[string]string{"IPAddress": addr}) + + return nil +}