Skip to content

Commit 67a862d

Browse files
hadvprpehrjl493456442
authored
cmd/geth, ethdb/pebble: improve database statistic (#29948)
* cmd/geth, ethdb/pebble: polish method naming and code comment * implement db stat for pebble * cmd, core, ethdb, internal, trie: remove db property selector * cmd, core, ethdb: fix function description --------- Co-authored-by: prpeh <[email protected]> Co-authored-by: Gary Rong <[email protected]>
1 parent 7cf6a63 commit 67a862d

File tree

11 files changed

+71
-40
lines changed

11 files changed

+71
-40
lines changed

cmd/geth/chaincmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func importChain(ctx *cli.Context) error {
336336
fmt.Printf("Import done in %v.\n\n", time.Since(start))
337337

338338
// Output pre-compaction stats mostly to see the import trashing
339-
showLeveldbStats(db)
339+
showDBStats(db)
340340

341341
// Print the memory statistics used by the importing
342342
mem := new(runtime.MemStats)
@@ -359,7 +359,7 @@ func importChain(ctx *cli.Context) error {
359359
}
360360
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))
361361

362-
showLeveldbStats(db)
362+
showDBStats(db)
363363
return importErr
364364
}
365365

cmd/geth/dbcmd.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,17 +407,13 @@ func checkStateContent(ctx *cli.Context) error {
407407
return nil
408408
}
409409

410-
func showLeveldbStats(db ethdb.KeyValueStater) {
411-
if stats, err := db.Stat("leveldb.stats"); err != nil {
410+
func showDBStats(db ethdb.KeyValueStater) {
411+
stats, err := db.Stat()
412+
if err != nil {
412413
log.Warn("Failed to read database stats", "error", err)
413-
} else {
414-
fmt.Println(stats)
415-
}
416-
if ioStats, err := db.Stat("leveldb.iostats"); err != nil {
417-
log.Warn("Failed to read database iostats", "error", err)
418-
} else {
419-
fmt.Println(ioStats)
414+
return
420415
}
416+
fmt.Println(stats)
421417
}
422418

423419
func dbStats(ctx *cli.Context) error {
@@ -427,7 +423,7 @@ func dbStats(ctx *cli.Context) error {
427423
db := utils.MakeChainDatabase(ctx, stack, true)
428424
defer db.Close()
429425

430-
showLeveldbStats(db)
426+
showDBStats(db)
431427
return nil
432428
}
433429

@@ -439,15 +435,15 @@ func dbCompact(ctx *cli.Context) error {
439435
defer db.Close()
440436

441437
log.Info("Stats before compaction")
442-
showLeveldbStats(db)
438+
showDBStats(db)
443439

444440
log.Info("Triggering compaction")
445441
if err := db.Compact(nil, nil); err != nil {
446442
log.Info("Compact err", "error", err)
447443
return err
448444
}
449445
log.Info("Stats after compaction")
450-
showLeveldbStats(db)
446+
showDBStats(db)
451447
return nil
452448
}
453449

core/rawdb/table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
147147
}
148148
}
149149

150-
// Stat returns a particular internal stat of the database.
151-
func (t *table) Stat(property string) (string, error) {
152-
return t.db.Stat(property)
150+
// Stat returns the statistic data of the database.
151+
func (t *table) Stat() (string, error) {
152+
return t.db.Stat()
153153
}
154154

155155
// Compact flattens the underlying data store for the given key range. In essence,

ethdb/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type KeyValueWriter interface {
3939

4040
// KeyValueStater wraps the Stat method of a backing data store.
4141
type KeyValueStater interface {
42-
// Stat returns a particular internal stat of the database.
43-
Stat(property string) (string, error)
42+
// Stat returns the statistic data of the database.
43+
Stat() (string, error)
4444
}
4545

4646
// Compacter wraps the Compact method of a backing data store.

ethdb/leveldb/leveldb.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package leveldb
2222

2323
import (
2424
"fmt"
25-
"strings"
2625
"sync"
2726
"time"
2827

@@ -244,14 +243,53 @@ func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
244243
return &snapshot{db: snap}, nil
245244
}
246245

247-
// Stat returns a particular internal stat of the database.
248-
func (db *Database) Stat(property string) (string, error) {
249-
if property == "" {
250-
property = "leveldb.stats"
251-
} else if !strings.HasPrefix(property, "leveldb.") {
252-
property = "leveldb." + property
246+
// Stat returns the statistic data of the database.
247+
func (db *Database) Stat() (string, error) {
248+
var stats leveldb.DBStats
249+
if err := db.db.Stats(&stats); err != nil {
250+
return "", err
253251
}
254-
return db.db.GetProperty(property)
252+
var (
253+
message string
254+
totalRead int64
255+
totalWrite int64
256+
totalSize int64
257+
totalTables int
258+
totalDuration time.Duration
259+
)
260+
if len(stats.LevelSizes) > 0 {
261+
message += " Level | Tables | Size(MB) | Time(sec) | Read(MB) | Write(MB)\n" +
262+
"-------+------------+---------------+---------------+---------------+---------------\n"
263+
for level, size := range stats.LevelSizes {
264+
read := stats.LevelRead[level]
265+
write := stats.LevelWrite[level]
266+
duration := stats.LevelDurations[level]
267+
tables := stats.LevelTablesCounts[level]
268+
269+
if tables == 0 && duration == 0 {
270+
continue
271+
}
272+
totalTables += tables
273+
totalSize += size
274+
totalRead += read
275+
totalWrite += write
276+
totalDuration += duration
277+
message += fmt.Sprintf(" %3d | %10d | %13.5f | %13.5f | %13.5f | %13.5f\n",
278+
level, tables, float64(size)/1048576.0, duration.Seconds(),
279+
float64(read)/1048576.0, float64(write)/1048576.0)
280+
}
281+
message += "-------+------------+---------------+---------------+---------------+---------------\n"
282+
message += fmt.Sprintf(" Total | %10d | %13.5f | %13.5f | %13.5f | %13.5f\n",
283+
totalTables, float64(totalSize)/1048576.0, totalDuration.Seconds(),
284+
float64(totalRead)/1048576.0, float64(totalWrite)/1048576.0)
285+
message += "-------+------------+---------------+---------------+---------------+---------------\n\n"
286+
}
287+
message += fmt.Sprintf("Read(MB):%.5f Write(MB):%.5f\n", float64(stats.IORead)/1048576.0, float64(stats.IOWrite)/1048576.0)
288+
message += fmt.Sprintf("BlockCache(MB):%.5f FileCache:%d\n", float64(stats.BlockCacheSize)/1048576.0, stats.OpenedTablesCount)
289+
message += fmt.Sprintf("MemoryCompaction:%d Level0Compaction:%d NonLevel0Compaction:%d SeekCompaction:%d\n", stats.MemComp, stats.Level0Comp, stats.NonLevel0Comp, stats.SeekComp)
290+
message += fmt.Sprintf("WriteDelayCount:%d WriteDelayDuration:%s Paused:%t\n", stats.WriteDelayCount, common.PrettyDuration(stats.WriteDelayDuration), stats.WritePaused)
291+
message += fmt.Sprintf("Snapshots:%d Iterators:%d\n", stats.AliveSnapshots, stats.AliveIterators)
292+
return message, nil
255293
}
256294

257295
// Compact flattens the underlying data store for the given key range. In essence,

ethdb/memorydb/memorydb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
182182
return newSnapshot(db), nil
183183
}
184184

185-
// Stat returns a particular internal stat of the database.
186-
func (db *Database) Stat(property string) (string, error) {
187-
return "", errors.New("unknown property")
185+
// Stat returns the statistic data of the database.
186+
func (db *Database) Stat() (string, error) {
187+
return "", nil
188188
}
189189

190190
// Compact is not supported on a memory database, but there's no need either as

ethdb/pebble/pebble.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,8 @@ func upperBound(prefix []byte) (limit []byte) {
416416
}
417417

418418
// Stat returns the internal metrics of Pebble in a text format. It's a developer
419-
// method to read everything there is to read independent of Pebble version.
420-
//
421-
// The property is unused in Pebble as there's only one thing to retrieve.
422-
func (d *Database) Stat(property string) (string, error) {
419+
// method to read everything there is to read, independent of Pebble version.
420+
func (d *Database) Stat() (string, error) {
423421
return d.db.Metrics().String(), nil
424422
}
425423

ethdb/remotedb/remotedb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
126126
panic("not supported")
127127
}
128128

129-
func (db *Database) Stat(property string) (string, error) {
130-
panic("not supported")
129+
func (db *Database) Stat() (string, error) {
130+
return "", nil
131131
}
132132

133133
func (db *Database) AncientDatadir() (string, error) {

internal/ethapi/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,8 +2108,8 @@ func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, err
21082108
}
21092109

21102110
// ChaindbProperty returns leveldb properties of the key-value database.
2111-
func (api *DebugAPI) ChaindbProperty(property string) (string, error) {
2112-
return api.b.ChainDb().Stat(property)
2111+
func (api *DebugAPI) ChaindbProperty() (string, error) {
2112+
return api.b.ChainDb().Stat()
21132113
}
21142114

21152115
// ChaindbCompact flattens the entire key-value database into a single level,

internal/web3ext/web3ext.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ web3._extend({
263263
new web3._extend.Method({
264264
name: 'chaindbProperty',
265265
call: 'debug_chaindbProperty',
266-
params: 1,
267266
outputFormatter: console.log
268267
}),
269268
new web3._extend.Method({

0 commit comments

Comments
 (0)