Skip to content

Commit

Permalink
core, ethdb: instrument the block and state db
Browse files Browse the repository at this point in the history
Conflicts:
	ethdb/database.go
  • Loading branch information
karalabe committed Jun 24, 2015
1 parent 7bd71fa commit 43e4a65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 14 additions & 1 deletion core/chain_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
Expand All @@ -30,6 +31,10 @@ var (
blockNumPre = []byte("block-num-")

blockInsertTimer = metrics.GetOrRegisterTimer("core/BlockInsertions", metrics.DefaultRegistry)
blockdbGetMeter = metrics.GetOrRegisterMeter("core/blockdb/Gets", metrics.DefaultRegistry)
blockdbPutMeter = metrics.GetOrRegisterMeter("core/blockdb/Puts", metrics.DefaultRegistry)
statedbGetMeter = metrics.GetOrRegisterMeter("core/statedb/Gets", metrics.DefaultRegistry)
statedbPutMeter = metrics.GetOrRegisterMeter("core/statedb/Puts", metrics.DefaultRegistry)
)

const (
Expand Down Expand Up @@ -121,7 +126,15 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow
cache: NewBlockCache(blockCacheLimit),
pow: pow,
}

// Instrument the block and state databases
if db, ok := blockDb.(*ethdb.LDBDatabase); ok {
db.GetMeter = blockdbGetMeter
db.PutMeter = blockdbPutMeter
}
if db, ok := stateDb.(*ethdb.LDBDatabase); ok {
db.GetMeter = statedbGetMeter
db.PutMeter = statedbPutMeter
}
// Check the genesis block given to the chain manager. If the genesis block mismatches block number 0
// throw an error. If no block or the same block's found continue.
if g := bc.GetBlockByNumber(0); g != nil && g.Hash() != genesis.Hash() {
Expand Down
19 changes: 14 additions & 5 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ethereum/go-ethereum/compression/rle"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/rcrowley/go-metrics"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/iterator"
Expand All @@ -13,10 +14,11 @@ import (
var OpenFileLimit = 64

type LDBDatabase struct {
// filename for reporting
fn string
// LevelDB instance
db *leveldb.DB
fn string // filename for reporting
db *leveldb.DB // LevelDB instance

GetMeter metrics.Meter // Meter for measuring the database get requests
PutMeter metrics.Meter // Meter for measuring the database put requests
}

// NewLDBDatabase returns a LevelDB wrapped object. LDBDatabase does not persist data by
Expand All @@ -43,7 +45,11 @@ func NewLDBDatabase(file string) (*LDBDatabase, error) {

// Put puts the given key / value to the queue
func (self *LDBDatabase) Put(key []byte, value []byte) error {
return self.db.Put(key, rle.Compress(value), nil)
dat := rle.Compress(value)
if self.PutMeter != nil {
self.PutMeter.Mark(int64(len(dat)))
}
return self.db.Put(key, dat, nil)
}

// Get returns the given key if it's present.
Expand All @@ -52,6 +58,9 @@ func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
if err != nil {
return nil, err
}
if self.GetMeter != nil {
self.GetMeter.Mark(int64(len(dat)))
}
return rle.Decompress(dat)
}

Expand Down

0 comments on commit 43e4a65

Please sign in to comment.