Skip to content

Commit

Permalink
Merge PR: auto dump pprof when cpu is high (#1018)
Browse files Browse the repository at this point in the history
* add auto dump pprof

* add auto dump pprof

* add auto dump pprof

* add auto dump pprof

* add auto dump pprof

* add auto dump pprof: rm param

* add auto dump pprof: add mem param

* add auto dump pprof: GoroutineDump

* add auto dump pprof: lrp

* add auto dump pprof

Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Zhong Qiu <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2021
1 parent 66d268c commit 64d3411
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func RegisterDynamicConfig() {
// set the dynamic config
oecConfig := GetOecConfig()
tmconfig.SetDynamicConfig(oecConfig)

//download pprof
PprofDownload()
}

func (c *OecConfig) loadFromConfig() {
Expand Down
81 changes: 81 additions & 0 deletions app/config/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package config

import (
"github.com/mosn/holmes"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
tmos "github.com/tendermint/tendermint/libs/os"
"path"
)

type PporfConfig struct {
autoDump bool
collectInterval string
coolDown string
dumpPath string
cpuTriggerPercentMin int
cpuTriggerPercentDiff int
cpuTriggerPercentAbs int
memTriggerPercentMin int
memTriggerPercentDiff int
memTriggerPercentAbs int
}

const (
FlagPprofAutoDump = "pprof-auto-dump"
FlagPprofCpuTriggerPercentMin = "pprof-cpu-trigger-percent-min"
FlagPprofCpuTriggerPercentDiff = "pprof-cpu-trigger-percent-diff"
FlagPprofCpuTriggerPercentAbs = "pprof-cpu-trigger-percent-abs"
FlagPprofMemTriggerPercentMin = "pprof-mem-trigger-percent-min"
FlagPprofMemTriggerPercentDiff = "pprof-mem-trigger-percent-diff"
FlagPprofMemTriggerPercentAbs = "pprof-mem-trigger-percent-abs"
)

// PprofDownload auto dump pprof
func PprofDownload() {
c := LoadPprofFromConfig()
if !c.autoDump {
return
}

h, err := holmes.New(
holmes.WithCollectInterval(c.collectInterval),
holmes.WithCoolDown(c.coolDown),
holmes.WithDumpPath(c.dumpPath),
holmes.WithCPUDump(c.cpuTriggerPercentMin, c.cpuTriggerPercentDiff, c.cpuTriggerPercentAbs),
holmes.WithMemDump(c.memTriggerPercentMin, c.memTriggerPercentDiff, c.memTriggerPercentAbs),
holmes.WithGoroutineDump(2000, 50, 5000),
holmes.WithBinaryDump(),
)
if err != nil {
tmos.Exit(err.Error())
}
h.EnableCPUDump().EnableMemDump().EnableGoroutineDump()
// start the metrics collect and dump loop
h.Start()
}

func LoadPprofFromConfig() *PporfConfig {
autoDump := viper.GetBool(FlagPprofAutoDump)
dumpPath := path.Join(viper.GetString(cli.HomeFlag), "pprof")
cpuTriggerPercentMin := viper.GetInt(FlagPprofCpuTriggerPercentMin)
cpuTriggerPercentDiff := viper.GetInt(FlagPprofCpuTriggerPercentDiff)
cpuTriggerPercentAbs := viper.GetInt(FlagPprofCpuTriggerPercentAbs)
memTriggerPercentMin := viper.GetInt(FlagPprofMemTriggerPercentMin)
memTriggerPercentDiff := viper.GetInt(FlagPprofMemTriggerPercentDiff)
memTriggerPercentAbs := viper.GetInt(FlagPprofMemTriggerPercentAbs)

c := &PporfConfig{
autoDump: autoDump,
collectInterval: "5s",
coolDown: "3m",
dumpPath: dumpPath,
cpuTriggerPercentMin: cpuTriggerPercentMin,
cpuTriggerPercentDiff: cpuTriggerPercentDiff,
cpuTriggerPercentAbs: cpuTriggerPercentAbs,
memTriggerPercentMin: memTriggerPercentMin,
memTriggerPercentDiff: memTriggerPercentDiff,
memTriggerPercentAbs: memTriggerPercentAbs,
}
return c
}
8 changes: 8 additions & 0 deletions cmd/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ func RegisterAppFlag(cmd *cobra.Command) {

cmd.Flags().Bool(config.FlagEnableDynamic, false, "Enable dynamic configuration for nodes")
cmd.Flags().String(config.FlagApollo, "", "Apollo connection config(IP|AppID|NamespaceName) for dynamic configuration")

cmd.Flags().Bool(config.FlagPprofAutoDump, false, "Enable auto dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentMin, 45, "TriggerPercentMin of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentDiff, 50, "TriggerPercentDiff of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentAbs, 50, "TriggerPercentAbs of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentMin, 70, "TriggerPercentMin of mem to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentDiff, 50, "TriggerPercentDiff of mem to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentAbs, 75, "TriggerPercentAbs of cpu mem dump pprof")
}
12 changes: 10 additions & 2 deletions cmd/exchaind/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/okex/exchain/app/config"
"github.com/tendermint/tendermint/state"
"log"
"net/http"
Expand Down Expand Up @@ -56,6 +57,13 @@ func replayCmd(ctx *server.Context) *cobra.Command {
cmd.Flags().BoolVarP(&state.IgnoreSmbCheck, "ignore-smb", "i", false, "ignore state machine broken")
cmd.Flags().String(server.FlagPruning, storetypes.PruningOptionNothing, "Pruning strategy (default|nothing|everything|custom)")
cmd.Flags().Uint64(server.FlagHaltHeight, 0, "Block height at which to gracefully halt the chain and shutdown the node")
cmd.Flags().Bool(config.FlagPprofAutoDump, false, "Enable auto dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentMin, 45, "TriggerPercentMin of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentDiff, 50, "TriggerPercentDiff of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofCpuTriggerPercentAbs, 50, "TriggerPercentAbs of cpu to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentMin, 70, "TriggerPercentMin of mem to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentDiff, 50, "TriggerPercentDiff of mem to dump pprof")
cmd.Flags().Int(config.FlagPprofMemTriggerPercentAbs, 75, "TriggerPercentAbs of cpu mem dump pprof")
return cmd
}

Expand Down Expand Up @@ -91,7 +99,7 @@ func replayBlock(ctx *server.Context, originDataDir string) {
startBlockHeight := currentBlockHeight + 1
//doReplay(ctx, state, stateStoreDB, proxyApp, originDataDir, startBlockHeight)
haltBlockHeight := viper.GetInt64(server.FlagHaltHeight)
doReplay(ctx, state, stateStoreDB, proxyApp, originDataDir, startBlockHeight,haltBlockHeight)
doReplay(ctx, state, stateStoreDB, proxyApp, originDataDir, startBlockHeight, haltBlockHeight)
}

// panic if error is not nil
Expand Down Expand Up @@ -165,7 +173,7 @@ func initChain(state sm.State, stateDB dbm.DB, genDoc *types.GenesisDoc, proxyAp
}

func doReplay(ctx *server.Context, state sm.State, stateStoreDB dbm.DB,
proxyApp proxy.AppConns, originDataDir string, startBlockHeight int64,haltBlockHeight int64){
proxyApp proxy.AppConns, originDataDir string, startBlockHeight int64, haltBlockHeight int64) {
originBlockStoreDB, err := openDB(blockStoreDB, originDataDir)
panicError(err)
originBlockStore := store.NewBlockStore(originBlockStoreDB)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/json-iterator/go v1.1.9
github.com/kr/pretty v0.2.0 // indirect
github.com/miguelmota/go-ethereum-hdwallet v0.0.0-20210614093730-56a4d342a6ff
github.com/mosn/holmes v0.0.0-20210830110104-685dc05437bf
github.com/nacos-group/nacos-sdk-go v1.0.0
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/pkg/errors v0.9.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mosn/holmes v0.0.0-20210830110104-685dc05437bf h1:VfHb81w0uu11UCYRHZloSb5DHBoGyOwqs5LL981vemc=
github.com/mosn/holmes v0.0.0-20210830110104-685dc05437bf/go.mod h1:nzlLOLX+7+4VrlTn9kcZb+JBaOQtdtvCEQ4iqBAl5co=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/mtibben/percent v0.2.1 h1:5gssi8Nqo8QU/r2pynCm+hBQHpkB/uNK7BJCFogWdzs=
github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ibNBTZrns=
Expand Down Expand Up @@ -730,6 +732,7 @@ github.com/segmentio/kafka-go v0.2.2 h1:KIUln5unPisRL2yyAkZsDR/coiymN9Djunv6JKGQ
github.com/segmentio/kafka-go v0.2.2/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
Expand Down

0 comments on commit 64d3411

Please sign in to comment.