aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bucket.go26
-rw-r--r--cmd/bolt/stats.go17
-rw-r--r--cmd/bolt/stats_test.go43
3 files changed, 70 insertions, 16 deletions
diff --git a/bucket.go b/bucket.go
index 4d45b38..96218f1 100644
--- a/bucket.go
+++ b/bucket.go
@@ -371,23 +371,36 @@ func (b *Bucket) Stats() BucketStats {
b.forEachPage(func(p *page, depth int) {
if (p.flags & leafPageFlag) != 0 {
s.KeyN += int(p.count)
+ // used totals the used bytes for the page
used := pageHeaderSize
if p.count != 0 {
+ // If page has any elements, add all element headers.
used += leafPageElementSize * int(p.count-1)
+ // Add all element key, value sizes.
+ // The computation takes advantage of the fact that the position
+ // of the last element's key/value equals to the total of the sizes
+ // of all previous elements' keys and values.
+ // It also includes the last element's header.
lastElement := p.leafPageElement(p.count - 1)
used += int(lastElement.pos + lastElement.ksize + lastElement.vsize)
}
if b.root == 0 {
+ // For inlined bucket just update the inline stats
s.InlineBucketInuse += used
} else {
+ // For non-inlined bucket update all the leaf stats
s.LeafPageN++
s.LeafInuse += used
s.LeafOverflowN += int(p.overflow)
- // Collect stats from sub-buckets
+ // Collect stats from sub-buckets.
+ // Do that by iterating over all element headers
+ // looking for the ones with the bucketLeafFlag.
for i := uint16(0); i < p.count; i++ {
e := p.leafPageElement(i)
if (e.flags & bucketLeafFlag) != 0 {
+ // For any bucket element, open the element value
+ // and recursively call Stats on the contained bucket.
subStats.Add(b.openBucket(e.value()).Stats())
}
}
@@ -395,21 +408,28 @@ func (b *Bucket) Stats() BucketStats {
} else if (p.flags & branchPageFlag) != 0 {
s.BranchPageN++
lastElement := p.branchPageElement(p.count - 1)
+ // used totals the used bytes for the page
+ // Add header and all element headers.
used := pageHeaderSize + (branchPageElementSize * int(p.count-1))
+ // Add size of all keys and values.
+ // Again, use the fact that last element's position equals to
+ // the total of key, value sizes of all previous elements.
used += int(lastElement.pos + lastElement.ksize)
s.BranchInuse += used
s.BranchOverflowN += int(p.overflow)
}
-
+ // Keep track of maximum page depth.
if depth+1 > s.Depth {
s.Depth = (depth + 1)
}
})
+ // Alloc stats can be computed from page counts and pageSize.
s.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize
s.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize
- // add the max depth of sub-buckets to get total nested depth
+ // Add the max depth of sub-buckets to get total nested depth.
s.Depth += subStats.Depth
+ // Add the stats for all sub-buckets
s.Add(subStats)
return s
}
diff --git a/cmd/bolt/stats.go b/cmd/bolt/stats.go
index 6968273..54a0f44 100644
--- a/cmd/bolt/stats.go
+++ b/cmd/bolt/stats.go
@@ -46,17 +46,26 @@ func Stats(path, prefix string) {
println("Page size utilization")
printf("\tBytes allocated for physical branch pages: %d\n", s.BranchAlloc)
- percentage := int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc))
+ var percentage int
+ if s.BranchAlloc != 0 {
+ percentage = int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc))
+ }
printf("\tBytes actually used for branch data: %d (%d%%)\n", s.BranchInuse, percentage)
printf("\tBytes allocated for physical leaf pages: %d\n", s.LeafAlloc)
- percentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc))
+ percentage = 0
+ if s.LeafAlloc != 0 {
+ percentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc))
+ }
printf("\tBytes actually used for leaf data: %d (%d%%)\n", s.LeafInuse, percentage)
- println("Bucket statistics ")
+ println("Bucket statistics")
printf("\tTotal number of buckets: %d\n", s.BucketN)
percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN))
printf("\tTotal number on inlined buckets: %d (%d%%)\n", s.InlineBucketN, percentage)
- percentage = int(float32(s.InlineBucketInuse) * 100.0 / float32(s.LeafInuse))
+ percentage = 0
+ if s.LeafInuse != 0 {
+ percentage = int(float32(s.InlineBucketInuse) * 100.0 / float32(s.LeafInuse))
+ }
printf("\tBytes used for inlined buckets: %d (%d%%)\n", s.InlineBucketInuse, percentage)
return nil
diff --git a/cmd/bolt/stats_test.go b/cmd/bolt/stats_test.go
index 9662f09..2ad5d51 100644
--- a/cmd/bolt/stats_test.go
+++ b/cmd/bolt/stats_test.go
@@ -1,6 +1,8 @@
package main_test
import (
+ "os"
+ "strconv"
"testing"
"github.com/boltdb/bolt"
@@ -9,12 +11,31 @@ import (
)
func TestStats(t *testing.T) {
+ if os.Getpagesize() != 4096 {
+ t.Skip()
+ }
SetTestMode(true)
open(func(db *bolt.DB, path string) {
db.Update(func(tx *bolt.Tx) error {
- tx.CreateBucket([]byte("foo"))
- tx.CreateBucket([]byte("bar"))
- tx.CreateBucket([]byte("baz"))
+ b, err := tx.CreateBucket([]byte("foo"))
+ if err != nil {
+ return err
+ }
+ for i := 0; i < 10; i++ {
+ b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
+ }
+ b, err = tx.CreateBucket([]byte("bar"))
+ if err != nil {
+ return err
+ }
+ for i := 0; i < 100; i++ {
+ b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
+ }
+ b, err = tx.CreateBucket([]byte("baz"))
+ if err != nil {
+ return err
+ }
+ b.Put([]byte("key"), []byte("value"))
return nil
})
db.Close()
@@ -23,15 +44,19 @@ func TestStats(t *testing.T) {
"Page count statistics\n"+
"\tNumber of logical branch pages: 0\n"+
"\tNumber of physical branch overflow pages: 0\n"+
- "\tNumber of logical leaf pages: 2\n"+
+ "\tNumber of logical leaf pages: 1\n"+
"\tNumber of physical leaf overflow pages: 0\n"+
"Tree statistics\n"+
- "\tNumber of keys/value pairs: 0\n"+
- "\tNumber of levels in B+tree: 0\n"+
+ "\tNumber of keys/value pairs: 101\n"+
+ "\tNumber of levels in B+tree: 1\n"+
"Page size utilization\n"+
"\tBytes allocated for physical branch pages: 0\n"+
- "\tBytes actually used for branch data: 0\n"+
- "\tBytes allocated for physical leaf pages: 8192\n"+
- "\tBytes actually used for leaf data: 0", output)
+ "\tBytes actually used for branch data: 0 (0%)\n"+
+ "\tBytes allocated for physical leaf pages: 4096\n"+
+ "\tBytes actually used for leaf data: 1996 (48%)\n"+
+ "Bucket statistics\n"+
+ "\tTotal number of buckets: 2\n"+
+ "\tTotal number on inlined buckets: 1 (50%)\n"+
+ "\tBytes used for inlined buckets: 40 (2%)", output)
})
}