cmd, ethdb, vendor: print iostats

This commit is contained in:
Kurkó Mihály 2018-03-02 16:02:53 +02:00
parent 49bcb5fbd5
commit 54ac541f36
4 changed files with 103 additions and 1 deletions

View file

@ -225,6 +225,11 @@ func importChain(ctx *cli.Context) error {
utils.Fatalf("Failed to read database stats: %v", err) utils.Fatalf("Failed to read database stats: %v", err)
} }
fmt.Println(stats) fmt.Println(stats)
iostats, err := db.LDB().GetProperty("leveldb.iostats")
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(iostats)
fmt.Printf("Trie cache misses: %d\n", trie.CacheMisses()) fmt.Printf("Trie cache misses: %d\n", trie.CacheMisses())
fmt.Printf("Trie cache unloads: %d\n\n", trie.CacheUnloads()) fmt.Printf("Trie cache unloads: %d\n\n", trie.CacheUnloads())
@ -254,6 +259,11 @@ func importChain(ctx *cli.Context) error {
utils.Fatalf("Failed to read database stats: %v", err) utils.Fatalf("Failed to read database stats: %v", err)
} }
fmt.Println(stats) fmt.Println(stats)
iostats, err = db.LDB().GetProperty("leveldb.iostats")
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(iostats)
return nil return nil
} }

View file

@ -22,6 +22,7 @@ import (
"sync" "sync"
"time" "time"
"fmt"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
@ -221,6 +222,13 @@ func (db *LDBDatabase) meter(refresh time.Duration) {
db.log.Error("Failed to read database stats", "err", err) db.log.Error("Failed to read database stats", "err", err)
return return
} }
iostats, err := db.db.GetProperty("leveldb.iostats")
if err != nil {
db.log.Error("Failed to read database iostats", "err", err)
return
}
fmt.Println(iostats)
// Find the compaction table, skip the header // Find the compaction table, skip the header
lines := strings.Split(stats, "\n") lines := strings.Split(stats, "\n")
for len(lines) > 0 && strings.TrimSpace(lines[0]) != "Compactions" { for len(lines) > 0 && strings.TrimSpace(lines[0]) != "Compactions" {

View file

@ -168,7 +168,7 @@ func openDB(s *session) (*DB, error) {
// The returned DB instance is safe for concurrent use. // The returned DB instance is safe for concurrent use.
// The DB must be closed after use, by calling Close method. // The DB must be closed after use, by calling Close method.
func Open(stor storage.Storage, o *opt.Options) (db *DB, err error) { func Open(stor storage.Storage, o *opt.Options) (db *DB, err error) {
s, err := newSession(stor, o) s, err := newSession(storage.IOCounterWrapper(stor), o)
if err != nil { if err != nil {
return return
} }
@ -906,6 +906,8 @@ func (db *DB) GetSnapshot() (*Snapshot, error) {
// Returns the number of files at level 'n'. // Returns the number of files at level 'n'.
// leveldb.stats // leveldb.stats
// Returns statistics of the underlying DB. // Returns statistics of the underlying DB.
// leveldb.iostats
// Returns statistics of effective disk read and write.
// leveldb.writedelay // leveldb.writedelay
// Returns cumulative write delay caused by compaction. // Returns cumulative write delay caused by compaction.
// leveldb.sstables // leveldb.sstables
@ -959,6 +961,12 @@ func (db *DB) GetProperty(name string) (value string, err error) {
level, len(tables), float64(tables.size())/1048576.0, duration.Seconds(), level, len(tables), float64(tables.size())/1048576.0, duration.Seconds(),
float64(read)/1048576.0, float64(write)/1048576.0) float64(read)/1048576.0, float64(write)/1048576.0)
} }
case p == "iostats":
var r, w float64
if s, ok := db.s.stor.(storage.IOCounter); ok {
r, w = float64(s.Reads())/1048576.0, float64(s.Writes())/1048576.0
}
value = fmt.Sprintf("Read(MB): %13.5f Write(MB): %13.5f", r, w)
case p == "writedelay": case p == "writedelay":
writeDelayN, writeDelay := atomic.LoadInt32(&db.cWriteDelayN), time.Duration(atomic.LoadInt64(&db.cWriteDelay)) writeDelayN, writeDelay := atomic.LoadInt32(&db.cWriteDelayN), time.Duration(atomic.LoadInt64(&db.cWriteDelay))
value = fmt.Sprintf("DelayN:%d Delay:%s", writeDelayN, writeDelay) value = fmt.Sprintf("DelayN:%d Delay:%s", writeDelayN, writeDelay)

View file

@ -11,6 +11,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"sync/atomic"
) )
// FileType represent a file type. // FileType represent a file type.
@ -177,3 +178,78 @@ type Storage interface {
// called after the storage has been closed. // called after the storage has been closed.
Close() error Close() error
} }
// IOCounter collects read and write statistics.
type IOCounter interface {
// Reads returns the cumulative number of read bytes of the underlying storage.
Reads() uint64
// Writes returns the cumulative number of written bytes of the underlying storage.
Writes() uint64
}
type ioCounter struct {
Storage
read uint64
write uint64
}
func (c *ioCounter) Open(fd FileDesc) (Reader, error) {
r, err := c.Storage.Open(fd)
return &meteredReader{r, c}, err
}
func (c *ioCounter) Create(fd FileDesc) (Writer, error) {
w, err := c.Storage.Create(fd)
return &meteredWriter{w, c}, err
}
func (c *ioCounter) Reads() uint64 {
return atomic.LoadUint64(&c.read)
}
func (c *ioCounter) Writes() uint64 {
return atomic.LoadUint64(&c.write)
}
// AddRead increases the number of read bytes by n.
func (c *ioCounter) AddRead(n uint64) uint64 {
return atomic.AddUint64(&c.read, n)
}
// AddWrite increases the number of written bytes by n.
func (c *ioCounter) AddWrite(n uint64) uint64 {
return atomic.AddUint64(&c.write, n)
}
// IOCounterWrapper returns the given storage wrapped by ioCounter.
func IOCounterWrapper(s Storage) Storage {
return &ioCounter{s, 0, 0}
}
type meteredReader struct {
Reader
c *ioCounter
}
func (r *meteredReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
r.c.AddRead(uint64(n))
return n, err
}
func (r *meteredReader) ReadAt(p []byte, off int64) (n int, err error) {
n, err = r.Reader.ReadAt(p, off)
r.c.AddRead(uint64(n))
return n, err
}
type meteredWriter struct {
Writer
c *ioCounter
}
func (w *meteredWriter) Write(p []byte) (n int, err error) {
n, err = w.Writer.Write(p)
w.c.AddWrite(uint64(n))
return n, err
}