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

Support dag cache config #291

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type Config struct {
Estimatefee bool `long:"estimatefee" description:"Enable estimate fee"`

AcctMode bool `long:"acctmode" description:"Enable support account system mode"`

DAGCacheSize uint64 `long:"dagcachesize" description:"DAG block cache size"`
BlockDataCacheSize uint64 `long:"bdcachesize" description:"Block data cache size"`
}

func (c *Config) GetMinningAddrs() []types.Address {
Expand Down
4 changes: 4 additions & 0 deletions core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ type Config struct {

// data dir
DataDir string

DAGCacheSize uint64
BlockDataCacheSize uint64
}

// BestState houses information about the current best block and other info
Expand Down Expand Up @@ -364,6 +367,7 @@ func New(config *Config) (*BlockChain, error) {
b.bd = meerdag.New(config.DAGType, b.CalcWeight,
1.0/float64(par.TargetTimePerBlock/time.Second), b.db, b.getBlockData)
b.bd.SetTipsDisLimit(int64(par.CoinbaseMaturity))
b.bd.SetCacheSize(config.DAGCacheSize, config.BlockDataCacheSize)
// Initialize the chain state from the passed database. When the db
// does not yet contain any chain state, both it and the chain state
// will be initialized to contain only the genesis block.
Expand Down
10 changes: 5 additions & 5 deletions core/json/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ type AcctInfo struct {
}

type MeerDAGInfoResult struct {
Name string `json:"name"`
Total uint `json:"total"`
BlockCacheSize uint `json:"bcachesize"`
BlockCacheRate float64 `json:"bcacherate"`
BlockDataCacheSize int `json:"bdcachesize"`
Name string `json:"name"`
Total uint `json:"total"`
BlockCacheSize string `json:"bcachesize"`
BlockCacheRate string `json:"bcacherate"`
BlockDataCacheSize string `json:"bdcachesize"`
}
43 changes: 33 additions & 10 deletions meerdag/blockpersist.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func (bd *MeerDAG) GetBlockData(ib IBlock) IBlockData {

func (bd *MeerDAG) updateBlockDataCache() {
cacheSize := bd.GetBlockDataCacheSize()
if cacheSize <= MinBlockDataCache {
if cacheSize <= bd.minBDCacheSize {
return
}
maxLife := params.ActiveNetParams.TargetTimePerBlock
startTime := time.Now()
mainHeight := bd.GetMainChainTip().GetHeight()
need := cacheSize - MinBlockDataCache
need := cacheSize - bd.minBDCacheSize
blockDataCache := map[uint]time.Time{}
bd.blockDataLock.Lock()
const waitTime = time.Second * 2
Expand All @@ -105,7 +105,7 @@ func (bd *MeerDAG) updateBlockDataCache() {
}
ib := bd.GetBlockById(k)
if ib != nil {
if math.Abs(float64(mainHeight)-float64(ib.GetHeight())) <= float64(MinBlockDataCache) {
if math.Abs(float64(mainHeight)-float64(ib.GetHeight())) <= float64(bd.minBDCacheSize) {
continue
}
ib.SetData(nil)
Expand All @@ -129,10 +129,16 @@ func (bd *MeerDAG) LoadBlockDataSet(sets *IdSet) {
}
}

func (bd *MeerDAG) GetBlockDataCacheSize() int {
func (bd *MeerDAG) GetBlockDataCacheSize() uint64 {
bd.blockDataLock.Lock()
defer bd.blockDataLock.Unlock()
return len(bd.blockDataCache)
return uint64(len(bd.blockDataCache))
}

func (bd *MeerDAG) GetMinBlockDataCacheSize() uint64 {
bd.blockDataLock.Lock()
defer bd.blockDataLock.Unlock()
return bd.minBDCacheSize
}

func (bd *MeerDAG) loadBlock(id uint) (IBlock, error) {
Expand Down Expand Up @@ -217,19 +223,25 @@ func (bd *MeerDAG) getChildren(ib IBlock) *IdSet {
return children
}

func (bd *MeerDAG) GetBlockCacheSize() int {
func (bd *MeerDAG) GetBlockCacheSize() uint64 {
bd.stateLock.Lock()
defer bd.stateLock.Unlock()
return uint64(len(bd.blocks))
}

func (bd *MeerDAG) GetMinBlockCacheSize() uint64 {
bd.stateLock.Lock()
defer bd.stateLock.Unlock()
return len(bd.blocks)
return bd.minCacheSize
}

func (bd *MeerDAG) updateBlockCache() {
cacheSize := bd.GetBlockCacheSize()
if cacheSize <= MinBlockPruneSize {
if cacheSize <= bd.minCacheSize {
return
}
mainTip := bd.GetMainChainTip()
need := cacheSize - MinBlockPruneSize
need := cacheSize - bd.minCacheSize
deletes := []IBlock{}

bd.stateLock.Lock()
Expand All @@ -242,7 +254,7 @@ func (bd *MeerDAG) updateBlockCache() {
bd.commitBlock.Has(k) {
continue
}
if v.GetHeight()+MinBlockPruneSize < mainTip.GetHeight() {
if v.GetHeight()+uint(bd.minCacheSize) < mainTip.GetHeight() {
deletes = append(deletes, v)
}
//
Expand Down Expand Up @@ -285,3 +297,14 @@ func (bd *MeerDAG) unloadBlock(ib IBlock) {
}

}

func (bd *MeerDAG) SetCacheSize(dag uint64, data uint64) {
bd.stateLock.Lock()
defer bd.stateLock.Unlock()
if dag > MinBlockPruneSize {
bd.minCacheSize = dag
}
if data > MinBlockDataCache {
bd.minBDCacheSize = data
}
}
5 changes: 5 additions & 0 deletions meerdag/meerdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ type MeerDAG struct {

wg sync.WaitGroup
quit chan struct{}

minCacheSize uint64
minBDCacheSize uint64
}

// Acquire the name of DAG instance
Expand Down Expand Up @@ -1395,6 +1398,8 @@ func New(dagType string, calcWeight CalcWeight, blockRate float64, db database.D
md := &MeerDAG{
quit: make(chan struct{}),
blockDataCache: map[uint]time.Time{},
minCacheSize: MinBlockPruneSize,
minBDCacheSize: MinBlockDataCache,
}
md.init(dagType, calcWeight, blockRate, db, getBlockData)
return md
Expand Down
6 changes: 3 additions & 3 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ func (api *PublicBlockChainAPI) GetMeerDAGInfo() (interface{}, error) {
md := api.node.GetBlockManager().GetChain().BlockDAG()
mdr.Name = md.GetName()
mdr.Total = md.GetBlockTotal()
mdr.BlockCacheSize = uint(md.GetBlockCacheSize())
mdr.BlockCacheRate = float64(mdr.BlockCacheSize) / float64(mdr.Total)
mdr.BlockDataCacheSize = md.GetBlockDataCacheSize()
mdr.BlockCacheSize = fmt.Sprintf("%d / %d", md.GetBlockCacheSize(), md.GetMinBlockCacheSize())
mdr.BlockCacheRate = fmt.Sprintf("%.2f%%", float64(md.GetBlockCacheSize())/float64(mdr.Total)*100)
mdr.BlockDataCacheSize = fmt.Sprintf("%d / %d", md.GetBlockDataCacheSize(), md.GetMinBlockDataCacheSize())
return mdr, nil
}

Expand Down
22 changes: 12 additions & 10 deletions services/blkmgr/blkmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@ func NewBlockManager(ntmgr consensus.Notify, indexManager blockchain.IndexManage
// Create a new block chain instance with the appropriate configuration.
var err error
bm.chain, err = blockchain.New(&blockchain.Config{
DB: db,
Interrupt: interrupt,
ChainParams: par,
TimeSource: timeSource,
Events: events,
SigCache: sigCache,
IndexManager: indexManager,
DAGType: cfg.DAGType,
CacheInvalidTx: cfg.CacheInvalidTx,
DataDir: cfg.DataDir,
DB: db,
Interrupt: interrupt,
ChainParams: par,
TimeSource: timeSource,
Events: events,
SigCache: sigCache,
IndexManager: indexManager,
DAGType: cfg.DAGType,
CacheInvalidTx: cfg.CacheInvalidTx,
DataDir: cfg.DataDir,
DAGCacheSize: cfg.DAGCacheSize,
BlockDataCacheSize: cfg.BlockDataCacheSize,
})
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions services/common/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,16 @@ var (
Usage: "Enable support account system mode",
Destination: &cfg.AcctMode,
},
&cli.Uint64Flag{
Name: "dagcachesize",
Usage: "DAG block cache size",
Destination: &cfg.DAGCacheSize,
},
&cli.Uint64Flag{
Name: "bdcachesize",
Usage: "Block data cache size",
Destination: &cfg.BlockDataCacheSize,
},
}
)

Expand Down