diff options
| author | Ben Johnson <benbjohnson@yahoo.com> | 2014-06-17 13:34:15 -0600 |
|---|---|---|
| committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-06-17 13:34:15 -0600 |
| commit | f8365b732eb3756973d2fd6e12cf9ad6f246a786 (patch) | |
| tree | 9a7368d7811e4f68343636542e13e1e4da6c1f90 | |
| parent | Document key/value lifecycle. (diff) | |
| parent | drop mergeStats and move freelist stats update to Tx (diff) | |
| download | dedo-f8365b732eb3756973d2fd6e12cf9ad6f246a786.tar.gz dedo-f8365b732eb3756973d2fd6e12cf9ad6f246a786.tar.xz | |
Merge pull request #197 from Shopify/freelist_stats
Add freelist stats to db stats
| -rw-r--r-- | db.go | 6 | ||||
| -rw-r--r-- | db_test.go | 3 | ||||
| -rw-r--r-- | freelist.go | 11 | ||||
| -rw-r--r-- | tx.go | 6 |
4 files changed, 23 insertions, 3 deletions
@@ -556,8 +556,12 @@ func (db *DB) allocate(count int) (*page, error) { // Stats represents statistics about the database. 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 - TxN int // total number of completed read transactions + TxN int // total number of started read transactions OpenTxN int // number of currently open read transactions TxStats TxStats // global, ongoing stats. @@ -253,7 +253,8 @@ func TestDB_Stats(t *testing.T) { return err }) 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()) }) } diff --git a/freelist.go b/freelist.go index e27f80a..3551113 100644 --- a/freelist.go +++ b/freelist.go @@ -22,7 +22,16 @@ type freelist struct { // size returns the size of the page after serialization. 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. @@ -232,11 +232,17 @@ func (tx *Tx) rollback() { func (tx *Tx) close() { if tx.writable { + // Grab freelist stats. + var freelistN = tx.db.freelist.count() + var freelistAlloc = tx.db.freelist.size() + // Remove writer lock. tx.db.rwlock.Unlock() // Merge statistics. tx.db.statlock.Lock() + tx.db.stats.FreelistN = freelistN + tx.db.stats.FreelistAlloc = freelistAlloc tx.db.stats.TxStats.add(&tx.stats) tx.db.statlock.Unlock() } else { |
