Skip to content

Commit

Permalink
op-service/pprof: Support profiling to file (ethereum-optimism#6739) (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomArtamonov authored Jan 9, 2024
1 parent 4250b6e commit 1b5413b
Show file tree
Hide file tree
Showing 24 changed files with 440 additions and 230 deletions.
2 changes: 1 addition & 1 deletion op-batcher/batcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ethereum-optimism/optimism/op-batcher/flags"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
Expand Down
4 changes: 2 additions & 2 deletions op-batcher/batcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ethereum-optimism/optimism/op-batcher/flags"
"github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/stretchr/testify/require"
Expand All @@ -30,7 +30,7 @@ func validBatcherConfig() batcher.CLIConfig {
TxMgrConfig: txmgr.NewCLIConfig("fake", txmgr.DefaultBatcherFlagValues),
LogConfig: log.DefaultCLIConfig(),
MetricsConfig: metrics.DefaultCLIConfig(),
PprofConfig: pprof.DefaultCLIConfig(),
PprofConfig: oppprof.DefaultCLIConfig(),
// The compressor config is not checked in config.Check()
RPC: rpc.DefaultCLIConfig(),
}
Expand Down
40 changes: 20 additions & 20 deletions op-batcher/batcher/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"
_ "net/http/pprof"
"strconv"
"strings"
"sync/atomic"
"time"
Expand All @@ -24,7 +22,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/httputil"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
Expand Down Expand Up @@ -62,13 +60,12 @@ type BatcherService struct {

Version string

pprofSrv *httputil.HTTPServer
metricsSrv *httputil.HTTPServer
rpcServer *oprpc.Server
pprofService *oppprof.Service
metricsSrv *httputil.HTTPServer
rpcServer *oprpc.Server

balanceMetricer io.Closer

stopped atomic.Bool
stopped atomic.Bool

NotSubmittingOnStart bool
}
Expand Down Expand Up @@ -111,7 +108,7 @@ func (bs *BatcherService) initFromCLIConfig(ctx context.Context, version string,
return fmt.Errorf("failed to start metrics server: %w", err)
}
if err := bs.initPProf(cfg); err != nil {
return fmt.Errorf("failed to start pprof server: %w", err)
return fmt.Errorf("failed to init profiling: %w", err)
}
bs.initDriver()
if err := bs.initRPCServer(cfg); err != nil {
Expand Down Expand Up @@ -216,16 +213,19 @@ func (bs *BatcherService) initTxManager(cfg *CLIConfig) error {
}

func (bs *BatcherService) initPProf(cfg *CLIConfig) error {
if !cfg.PprofConfig.Enabled {
return nil
}
log.Debug("starting pprof server", "addr", net.JoinHostPort(cfg.PprofConfig.ListenAddr, strconv.Itoa(cfg.PprofConfig.ListenPort)))
srv, err := oppprof.StartServer(cfg.PprofConfig.ListenAddr, cfg.PprofConfig.ListenPort)
if err != nil {
return err
bs.pprofService = oppprof.New(
cfg.PprofConfig.ListenEnabled,
cfg.PprofConfig.ListenAddr,
cfg.PprofConfig.ListenPort,
cfg.PprofConfig.ProfileType,
cfg.PprofConfig.ProfileDir,
cfg.PprofConfig.ProfileFilename,
)

if err := bs.pprofService.Start(); err != nil {
return fmt.Errorf("failed to start pprof service: %w", err)
}
bs.pprofSrv = srv
log.Info("started pprof server", "addr", srv.Addr())

return nil
}

Expand Down Expand Up @@ -326,8 +326,8 @@ func (bs *BatcherService) Stop(ctx context.Context) error {
result = errors.Join(result, fmt.Errorf("failed to stop RPC server: %w", err))
}
}
if bs.pprofSrv != nil {
if err := bs.pprofSrv.Stop(ctx); err != nil {
if bs.pprofService != nil {
if err := bs.pprofService.Stop(ctx); err != nil {
result = errors.Join(result, fmt.Errorf("failed to stop PProf server: %w", err))
}
}
Expand Down
2 changes: 1 addition & 1 deletion op-batcher/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
opservice "github.com/ethereum-optimism/optimism/op-service"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
Expand Down
2 changes: 1 addition & 1 deletion op-challenger/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/ethereum-optimism/optimism/op-node/chaincfg"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)

Expand Down
2 changes: 1 addition & 1 deletion op-challenger/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
openum "github.com/ethereum-optimism/optimism/op-service/enum"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)

Expand Down
51 changes: 27 additions & 24 deletions op-challenger/game/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/httputil"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
Expand All @@ -46,8 +46,8 @@ type Service struct {
l1Client *ethclient.Client
pollClient client.RPC

pprofSrv *httputil.HTTPServer
metricsSrv *httputil.HTTPServer
pprofService *oppprof.Service
metricsSrv *httputil.HTTPServer

balanceMetricer io.Closer

Expand All @@ -71,28 +71,28 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*Se

func (s *Service) initFromConfig(ctx context.Context, cfg *config.Config) error {
if err := s.initTxManager(cfg); err != nil {
return err
return fmt.Errorf("failed to init tx manager: %w", err)
}
if err := s.initL1Client(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init l1 client: %w", err)
}
if err := s.initRollupClient(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init rollup client: %w", err)
}
if err := s.initPollClient(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init poll client: %w", err)
}
if err := s.initPProfServer(&cfg.PprofConfig); err != nil {
return err
if err := s.initPProf(&cfg.PprofConfig); err != nil {
return fmt.Errorf("failed to init profiling: %w", err)
}
if err := s.initMetricsServer(&cfg.MetricsConfig); err != nil {
return err
return fmt.Errorf("failed to init metrics server: %w", err)
}
if err := s.initGameLoader(cfg); err != nil {
return err
return fmt.Errorf("failed to init game loader: %w", err)
}
if err := s.initScheduler(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init scheduler: %w", err)
}

s.initMonitor(cfg)
Expand Down Expand Up @@ -129,17 +129,20 @@ func (s *Service) initPollClient(ctx context.Context, cfg *config.Config) error
return nil
}

func (s *Service) initPProfServer(cfg *oppprof.CLIConfig) error {
if !cfg.Enabled {
return nil
}
s.logger.Debug("starting pprof", "addr", cfg.ListenAddr, "port", cfg.ListenPort)
pprofSrv, err := oppprof.StartServer(cfg.ListenAddr, cfg.ListenPort)
if err != nil {
return fmt.Errorf("failed to start pprof server: %w", err)
func (s *Service) initPProf(cfg *oppprof.CLIConfig) error {
s.pprofService = oppprof.New(
cfg.ListenEnabled,
cfg.ListenAddr,
cfg.ListenPort,
cfg.ProfileType,
cfg.ProfileDir,
cfg.ProfileFilename,
)

if err := s.pprofService.Start(); err != nil {
return fmt.Errorf("failed to start pprof service: %w", err)
}
s.pprofSrv = pprofSrv
s.logger.Info("started pprof server", "addr", pprofSrv.Addr())

return nil
}

Expand Down Expand Up @@ -231,8 +234,8 @@ func (s *Service) Stop(ctx context.Context) error {
if s.faultGamesCloser != nil {
s.faultGamesCloser()
}
if s.pprofSrv != nil {
if err := s.pprofSrv.Stop(ctx); err != nil {
if s.pprofService != nil {
if err := s.pprofService.Stop(ctx); err != nil {
result = errors.Join(result, fmt.Errorf("failed to close pprof server: %w", err))
}
}
Expand Down
2 changes: 1 addition & 1 deletion op-conductor/conductor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
)

Expand Down
2 changes: 1 addition & 1 deletion op-conductor/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
opflags "github.com/ethereum-optimism/optimism/op-service/flags"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
)

Expand Down
2 changes: 1 addition & 1 deletion op-e2e/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/eth"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum-optimism/optimism/op-service/sources"
"github.com/ethereum-optimism/optimism/op-service/testlog"
Expand Down
2 changes: 1 addition & 1 deletion op-heartbeat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/ethereum-optimism/optimism/op-heartbeat/flags"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/urfave/cli/v2"
)

Expand Down
28 changes: 16 additions & 12 deletions op-heartbeat/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/httputil"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
)

const (
Expand Down Expand Up @@ -60,13 +60,14 @@ func Main(version string) func(ctx *cli.Context) error {
}

type HeartbeatService struct {
pprof, metrics, http *httputil.HTTPServer
metrics, http *httputil.HTTPServer
pprofService *oppprof.Service
}

func (hs *HeartbeatService) Stop(ctx context.Context) error {
var result error
if hs.pprof != nil {
result = errors.Join(result, hs.pprof.Stop(ctx))
if hs.pprofService != nil {
result = errors.Join(result, hs.pprofService.Stop(ctx))
}
if hs.metrics != nil {
result = errors.Join(result, hs.metrics.Stop(ctx))
Expand All @@ -93,14 +94,17 @@ func Start(ctx context.Context, l log.Logger, cfg Config, version string) (*Hear
}

pprofCfg := cfg.Pprof
if pprofCfg.Enabled {
l.Debug("starting pprof", "addr", pprofCfg.ListenAddr, "port", pprofCfg.ListenPort)
pprofSrv, err := oppprof.StartServer(pprofCfg.ListenAddr, pprofCfg.ListenPort)
if err != nil {
return nil, errors.Join(fmt.Errorf("failed to start pprof server: %w", err), hs.Stop(ctx))
}
l.Info("started pprof server", "addr", pprofSrv.Addr())
hs.pprof = pprofSrv
hs.pprofService = oppprof.New(
pprofCfg.ListenEnabled,
pprofCfg.ListenAddr,
pprofCfg.ListenPort,
pprofCfg.ProfileType,
pprofCfg.ProfileDir,
pprofCfg.ProfileFilename,
)

if err := hs.pprofService.Start(); err != nil {
return nil, fmt.Errorf("failed to start pprof service: %w", err)
}

metrics := NewMetrics(registry)
Expand Down
22 changes: 2 additions & 20 deletions op-node/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
openum "github.com/ethereum-optimism/optimism/op-service/enum"
opflags "github.com/ethereum-optimism/optimism/op-service/flags"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/sources"
)

Expand Down Expand Up @@ -176,23 +177,6 @@ var (
Value: 7300,
EnvVars: prefixEnvVars("METRICS_PORT"),
}
PprofEnabledFlag = &cli.BoolFlag{
Name: "pprof.enabled",
Usage: "Enable the pprof server",
EnvVars: prefixEnvVars("PPROF_ENABLED"),
}
PprofAddrFlag = &cli.StringFlag{
Name: "pprof.addr",
Usage: "pprof listening address",
Value: "0.0.0.0", // TODO(CLI-4159): Switch to 127.0.0.1
EnvVars: prefixEnvVars("PPROF_ADDR"),
}
PprofPortFlag = &cli.IntFlag{
Name: "pprof.port",
Usage: "pprof listening port",
Value: 6060,
EnvVars: prefixEnvVars("PPROF_PORT"),
}
SnapshotLog = &cli.StringFlag{
Name: "snapshotlog.file",
Usage: "Path to the snapshot log file",
Expand Down Expand Up @@ -289,9 +273,6 @@ var optionalFlags = []cli.Flag{
MetricsEnabledFlag,
MetricsAddrFlag,
MetricsPortFlag,
PprofEnabledFlag,
PprofAddrFlag,
PprofPortFlag,
SnapshotLog,
HeartbeatEnabledFlag,
HeartbeatMonikerFlag,
Expand All @@ -317,6 +298,7 @@ func init() {
DeprecatedFlags = append(DeprecatedFlags, deprecatedP2PFlags(EnvVarPrefix)...)
optionalFlags = append(optionalFlags, P2PFlags(EnvVarPrefix)...)
optionalFlags = append(optionalFlags, oplog.CLIFlags(EnvVarPrefix)...)
optionalFlags = append(optionalFlags, oppprof.CLIFlags(EnvVarPrefix)...)
optionalFlags = append(optionalFlags, DeprecatedFlags...)
optionalFlags = append(optionalFlags, opflags.CLIFlags(EnvVarPrefix)...)
Flags = append(requiredFlags, optionalFlags...)
Expand Down
2 changes: 1 addition & 1 deletion op-node/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-node/rollup/sync"
oppprof "github.com/ethereum-optimism/optimism/op-service/pprof"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum/go-ethereum/log"
)

Expand Down
Loading

0 comments on commit 1b5413b

Please sign in to comment.