add freelist stats to db stats

pull/34/head
Martin Kobetic 2014-06-17 18:40:56 +00:00
parent 41aa602f27
commit c105316292
4 changed files with 29 additions and 11 deletions

22
db.go
View File

@ -427,16 +427,12 @@ func (db *DB) removeTx(tx *Tx) {
break break
} }
} }
n := len(db.txs)
// Unlock the meta pages. // Unlock the meta pages.
db.metalock.Unlock() db.metalock.Unlock()
// Merge statistics. // Merge statistics.
db.statlock.Lock() db.mergeStats(&tx.stats)
db.stats.OpenTxN = n
db.stats.TxStats.add(&tx.stats)
db.statlock.Unlock()
} }
// Update executes a function within the context of a read-write managed transaction. // Update executes a function within the context of a read-write managed transaction.
@ -554,10 +550,24 @@ func (db *DB) allocate(count int) (*page, error) {
return p, nil return p, nil
} }
// mergeStats updates db stats in thread-safe manner.
func (db *DB) mergeStats(txStats *TxStats) {
db.statlock.Lock()
db.stats.FreelistN = db.freelist.count()
db.stats.FreelistAlloc = db.freelist.size()
db.stats.OpenTxN = len(db.txs)
db.stats.TxStats.add(txStats)
db.statlock.Unlock()
}
// Stats represents statistics about the database. // Stats represents statistics about the database.
type Stats struct { type Stats struct {
// Freelist stats
FreelistN int // total number of pages on the freelist
FreelistAlloc int // total bytes used by the freelist and the pages on it
// Transaction stats // Transaction stats
TxN int // total number of completed read transactions TxN int // total number of started read transactions
OpenTxN int // number of currently open read transactions OpenTxN int // number of currently open read transactions
TxStats TxStats // global, ongoing stats. TxStats TxStats // global, ongoing stats.

View File

@ -253,7 +253,8 @@ func TestDB_Stats(t *testing.T) {
return err return err
}) })
stats := db.Stats() stats := db.Stats()
assert.Equal(t, 2, stats.TxStats.PageCount) assert.Equal(t, 2, stats.TxStats.PageCount, "PageCount")
assert.Equal(t, 2, stats.FreelistN, "FreelistN %d", db.freelist.count())
}) })
} }

View File

@ -22,7 +22,16 @@ type freelist struct {
// size returns the size of the page after serialization. // size returns the size of the page after serialization.
func (f *freelist) size() int { func (f *freelist) size() int {
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * len(f.all())) return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * f.count())
}
// count returns count of pages on the freelist
func (f *freelist) count() int {
var count = len(f.ids)
for _, list := range f.pending {
count += len(list)
}
return count
} }
// all returns a list of all free ids and all pending ids in one sorted list. // all returns a list of all free ids and all pending ids in one sorted list.

4
tx.go
View File

@ -236,9 +236,7 @@ func (tx *Tx) close() {
tx.db.rwlock.Unlock() tx.db.rwlock.Unlock()
// Merge statistics. // Merge statistics.
tx.db.statlock.Lock() tx.db.mergeStats(&tx.stats)
tx.db.stats.TxStats.add(&tx.stats)
tx.db.statlock.Unlock()
} else { } else {
tx.db.removeTx(tx) tx.db.removeTx(tx)
} }