Skip to content

Commit 08c01fe

Browse files
committed
db: improve file cache metrics, fix 32-bit platform adjustment
The file cache metrics now keep track of the table and blob file counts separately. We fix the 32-bit memory size adjustment for tests and make it easy (and mandatory) to keep up to date. Fixes #4831
1 parent acc4f6d commit 08c01fe

File tree

8 files changed

+73
-54
lines changed

8 files changed

+73
-54
lines changed

file_cache.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import (
3434
"github.com/cockroachdb/redact"
3535
)
3636

37+
// FileCacheMetrics contains metrics for the file cache. Note that the file
38+
// cache is normally shared between all the stores on a node.
39+
type FileCacheMetrics struct {
40+
// The number of bytes inuse by the cache.
41+
Size int64
42+
TableCount int64
43+
BlobFileCount int64
44+
Hits int64
45+
Misses int64
46+
}
47+
3748
var emptyIter = &errorIter{err: nil}
3849
var emptyKeyspanIter = &errorKeyspanIter{err: nil}
3950

@@ -290,7 +301,7 @@ func (h *fileCacheHandle) SSTStatsCollector() *block.CategoryStatsCollector {
290301
// Metrics returns metrics for the file cache. Note that the CacheMetrics track
291302
// the global cache which is shared between multiple handles (stores). The
292303
// FilterMetrics are per-handle.
293-
func (h *fileCacheHandle) Metrics() (CacheMetrics, FilterMetrics) {
304+
func (h *fileCacheHandle) Metrics() (FileCacheMetrics, FilterMetrics) {
294305
m := h.fileCache.c.Metrics()
295306

296307
// The generic cache maintains a count of entries, but it doesn't know which
@@ -301,10 +312,11 @@ func (h *fileCacheHandle) Metrics() (CacheMetrics, FilterMetrics) {
301312
countSSTables := h.fileCache.counts.sstables.Load()
302313
countBlobFiles := h.fileCache.counts.blobFiles.Load()
303314

304-
cm := CacheMetrics{
305-
Hits: m.Hits,
306-
Misses: m.Misses,
307-
Count: countSSTables + countBlobFiles,
315+
cm := FileCacheMetrics{
316+
TableCount: countSSTables,
317+
BlobFileCount: countBlobFiles,
318+
Hits: m.Hits,
319+
Misses: m.Misses,
308320
Size: m.Size + countSSTables*int64(unsafe.Sizeof(sstable.Reader{})) +
309321
countBlobFiles*int64(unsafe.Sizeof(blob.FileReader{})),
310322
}

metrics.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"math"
1010
"time"
11+
"unsafe"
1112

1213
"github.com/cockroachdb/pebble/internal/base"
1314
"github.com/cockroachdb/pebble/internal/cache"
@@ -17,6 +18,7 @@ import (
1718
"github.com/cockroachdb/pebble/objstorage/objstorageprovider/sharedcache"
1819
"github.com/cockroachdb/pebble/record"
1920
"github.com/cockroachdb/pebble/sstable"
21+
"github.com/cockroachdb/pebble/sstable/blob"
2022
"github.com/cockroachdb/pebble/sstable/block"
2123
"github.com/cockroachdb/pebble/wal"
2224
"github.com/cockroachdb/redact"
@@ -441,7 +443,7 @@ type Metrics struct {
441443
}
442444
}
443445

444-
FileCache CacheMetrics
446+
FileCache FileCacheMetrics
445447

446448
// Count of the number of open sstable iterators.
447449
TableIters int64
@@ -811,15 +813,16 @@ func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
811813
}
812814
w.Printf("\n")
813815

814-
formatCacheMetrics := func(m *CacheMetrics, name redact.SafeString) {
815-
w.Printf("%s: %s entries (%s) hit rate: %.1f%%\n",
816-
name,
817-
humanize.Count.Int64(m.Count),
818-
humanize.Bytes.Int64(m.Size),
819-
redact.Safe(hitRate(m.Hits, m.Misses)))
820-
}
821-
formatCacheMetrics(&m.BlockCache, "Block cache")
822-
formatCacheMetrics(&m.FileCache, "Table cache")
816+
w.Printf("Block cache: %s entries (%s) hit rate: %.1f%%\n",
817+
humanize.Count.Int64(m.BlockCache.Count),
818+
humanize.Bytes.Int64(m.BlockCache.Size),
819+
redact.Safe(hitRate(m.BlockCache.Hits, m.BlockCache.Misses)))
820+
821+
w.Printf("File cache: %s tables, %s blobfiles (%s) hit rate: %.1f%%\n",
822+
humanize.Count.Int64(m.FileCache.TableCount),
823+
humanize.Count.Int64(m.FileCache.BlobFileCount),
824+
humanize.Bytes.Int64(m.FileCache.Size),
825+
redact.Safe(hitRate(m.FileCache.Hits, m.FileCache.Misses)))
823826

824827
formatSharedCacheMetrics := func(w redact.SafePrinter, m *SecondaryCacheMetrics, name redact.SafeString) {
825828
w.Printf("%s: %s entries (%s) hit rate: %.1f%%\n",
@@ -882,17 +885,20 @@ func percent(numerator, denominator int64) float64 {
882885
// provide a platform-independent result for tests.
883886
func (m *Metrics) StringForTests() string {
884887
mCopy := *m
885-
if math.MaxInt == math.MaxInt32 {
886-
// README: This is the difference in Sizeof(sstable.Reader{})) + Sizeof(blob.FileReader{})
887-
// between 64 and 32 bit platforms. See Metrics() in file_cache.go for more details.
888-
// This magic number must be updated if the sstable.Reader or blob.FileReader struct changes.
889-
// On 64-bit platforms, the size of the sstable.Reader struct is 616 bytes.
890-
// On 32-bit platforms, the size of the sstable.Reader struct is 496 bytes.
891-
// On 64-bit platforms, the size of the blob.FileReader struct is 88 bytes.
892-
// On 32-bit platforms, the size of the blob.FileReader struct is 56 bytes.
893-
// The difference is 616 - 496 + 88 - 56 = 152 bytes.
894-
const tableCacheSizeAdjustment = 152
895-
mCopy.FileCache.Size += mCopy.FileCache.Count * tableCacheSizeAdjustment
888+
889+
// We recalculate the file cache size using the 64-bit sizes, and we ignore
890+
// the genericcache metadata size which is harder to adjust.
891+
const sstableReaderSize64bit = 280
892+
const blobFileReaderSize64bit = 96
893+
mCopy.FileCache.Size = mCopy.FileCache.TableCount*sstableReaderSize64bit + mCopy.FileCache.BlobFileCount*blobFileReaderSize64bit
894+
if math.MaxInt == math.MaxInt64 {
895+
// Verify the 64-bit sizes, so they are kept updated.
896+
if sstableReaderSize64bit != unsafe.Sizeof(sstable.Reader{}) {
897+
panic(fmt.Sprintf("sstableReaderSize64bit should be updated to %d", unsafe.Sizeof(sstable.Reader{})))
898+
}
899+
if blobFileReaderSize64bit != unsafe.Sizeof(blob.FileReader{}) {
900+
panic(fmt.Sprintf("blobFileReaderSize64bit should be updated to %d", unsafe.Sizeof(blob.FileReader{})))
901+
}
896902
}
897903
// Don't show cgo memory statistics as they can vary based on architecture,
898904
// invariants tag, etc.

metrics_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func exampleMetrics() Metrics {
7676
m.Table.BackingTableSize = 2 << 20
7777
m.Table.ZombieCount = 16
7878
m.FileCache.Size = 17
79-
m.FileCache.Count = 18
79+
m.FileCache.TableCount = 180
80+
m.FileCache.BlobFileCount = 181
8081
m.FileCache.Hits = 19
8182
m.FileCache.Misses = 20
8283
m.TableIters = 21
@@ -369,7 +370,7 @@ func TestMetrics(t *testing.T) {
369370
// Some subset of cases show non-determinism in cache hits/misses.
370371
if td.HasArg("zero-cache-hits-misses") {
371372
// Avoid non-determinism.
372-
m.FileCache = cache.Metrics{}
373+
m.FileCache = FileCacheMetrics{}
373374
m.BlockCache = cache.Metrics{}
374375
// Empirically, the unknown stats are also non-deterministic.
375376
if len(m.CategoryStats) > 0 && m.CategoryStats[0].Category == block.CategoryUnknown {

testdata/compaction/value_separation

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Local tables size: 864B
160160
Compression types: snappy: 1
161161
Table stats: all loaded
162162
Block cache: 4 entries (1.6KB) hit rate: 70.3%
163-
Table cache: 2 entries (808B) hit rate: 82.2%
163+
File cache: 1 tables, 1 blobfiles (376B) hit rate: 82.2%
164164
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
165165
Snapshots: 0 earliest seq num: 0
166166
Table iters: 0

testdata/event_listener

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ Local tables size: 2.2KB
260260
Compression types: snappy: 3
261261
Table stats: initial load in progress
262262
Block cache: 2 entries (784B) hit rate: 0.0%
263-
Table cache: 0 entries (0B) hit rate: 50.0%
263+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 50.0%
264264
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
265265
Snapshots: 0 earliest seq num: 0
266266
Table iters: 0
@@ -365,7 +365,7 @@ Local tables size: 4.4KB
365365
Compression types: snappy: 6
366366
Table stats: initial load in progress
367367
Block cache: 6 entries (2.3KB) hit rate: 0.0%
368-
Table cache: 0 entries (0B) hit rate: 50.0%
368+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 50.0%
369369
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
370370
Snapshots: 0 earliest seq num: 0
371371
Table iters: 0

testdata/ingest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Local tables size: 569B
5555
Compression types: snappy: 1
5656
Table stats: all loaded
5757
Block cache: 3 entries (1.1KB) hit rate: 15.4%
58-
Table cache: 1 entries (496B) hit rate: 50.0%
58+
File cache: 1 tables, 0 blobfiles (280B) hit rate: 50.0%
5959
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
6060
Snapshots: 0 earliest seq num: 0
6161
Table iters: 0

testdata/metrics

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Local tables size: 28B
2424
Compression types:
2525
Table stats: 31
2626
Block cache: 2 entries (1B) hit rate: 42.9%
27-
Table cache: 18 entries (17B) hit rate: 48.7%
27+
File cache: 180 tables, 181 blobfiles (17B) hit rate: 48.7%
2828
Range key sets: 123 Tombstones: 456 Total missized tombstones encountered: 789
2929
Snapshots: 4 earliest seq num: 1024
3030
Table iters: 21
@@ -78,7 +78,7 @@ Local tables size: 755B
7878
Compression types: snappy: 1
7979
Table stats: all loaded
8080
Block cache: 2 entries (795B) hit rate: 0.0%
81-
Table cache: 1 entries (496B) hit rate: 0.0%
81+
File cache: 1 tables, 0 blobfiles (280B) hit rate: 0.0%
8282
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
8383
Snapshots: 0 earliest seq num: 0
8484
Table iters: 1
@@ -143,7 +143,7 @@ Local tables size: 1.5KB
143143
Compression types: snappy: 2
144144
Table stats: all loaded
145145
Block cache: 2 entries (795B) hit rate: 33.3%
146-
Table cache: 2 entries (992B) hit rate: 66.7%
146+
File cache: 2 tables, 0 blobfiles (560B) hit rate: 66.7%
147147
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
148148
Snapshots: 0 earliest seq num: 0
149149
Table iters: 2
@@ -191,7 +191,7 @@ Local tables size: 1.5KB
191191
Compression types: snappy: 2
192192
Table stats: all loaded
193193
Block cache: 2 entries (795B) hit rate: 33.3%
194-
Table cache: 2 entries (992B) hit rate: 66.7%
194+
File cache: 2 tables, 0 blobfiles (560B) hit rate: 66.7%
195195
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
196196
Snapshots: 0 earliest seq num: 0
197197
Table iters: 2
@@ -236,7 +236,7 @@ Local tables size: 1.5KB
236236
Compression types: snappy: 2
237237
Table stats: all loaded
238238
Block cache: 2 entries (795B) hit rate: 33.3%
239-
Table cache: 1 entries (496B) hit rate: 66.7%
239+
File cache: 1 tables, 0 blobfiles (280B) hit rate: 66.7%
240240
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
241241
Snapshots: 0 earliest seq num: 0
242242
Table iters: 1
@@ -284,7 +284,7 @@ Local tables size: 1.5KB
284284
Compression types: snappy: 2
285285
Table stats: all loaded
286286
Block cache: 0 entries (0B) hit rate: 33.3%
287-
Table cache: 0 entries (0B) hit rate: 66.7%
287+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 66.7%
288288
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
289289
Snapshots: 0 earliest seq num: 0
290290
Table iters: 0
@@ -371,7 +371,7 @@ Local tables size: 7.1KB
371371
Compression types: snappy: 9
372372
Table stats: all loaded
373373
Block cache: 0 entries (0B) hit rate: 33.3%
374-
Table cache: 0 entries (0B) hit rate: 66.7%
374+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 66.7%
375375
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
376376
Snapshots: 0 earliest seq num: 0
377377
Table iters: 0
@@ -443,7 +443,7 @@ Local tables size: 7.1KB
443443
Compression types: snappy: 9
444444
Table stats: all loaded
445445
Block cache: 0 entries (0B) hit rate: 10.0%
446-
Table cache: 0 entries (0B) hit rate: 55.0%
446+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 55.0%
447447
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
448448
Snapshots: 0 earliest seq num: 0
449449
Table iters: 0
@@ -566,7 +566,7 @@ Local tables size: 12KB
566566
Compression types: snappy: 15
567567
Table stats: all loaded
568568
Block cache: 6 entries (2.3KB) hit rate: 7.3%
569-
Table cache: 0 entries (0B) hit rate: 55.0%
569+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 55.0%
570570
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
571571
Snapshots: 0 earliest seq num: 0
572572
Table iters: 0
@@ -651,7 +651,7 @@ Local tables size: 17KB
651651
Compression types: snappy: 22
652652
Table stats: all loaded
653653
Block cache: 6 entries (2.3KB) hit rate: 7.3%
654-
Table cache: 0 entries (0B) hit rate: 55.0%
654+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 55.0%
655655
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
656656
Snapshots: 0 earliest seq num: 0
657657
Table iters: 0
@@ -748,7 +748,7 @@ Local tables size: 16KB
748748
Compression types: snappy: 21
749749
Table stats: all loaded
750750
Block cache: 0 entries (0B) hit rate: 0.0%
751-
Table cache: 0 entries (0B) hit rate: 0.0%
751+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
752752
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
753753
Snapshots: 0 earliest seq num: 0
754754
Table iters: 0
@@ -889,7 +889,7 @@ Local tables size: 14KB
889889
Compression types: snappy: 18
890890
Table stats: all loaded
891891
Block cache: 0 entries (0B) hit rate: 0.0%
892-
Table cache: 0 entries (0B) hit rate: 0.0%
892+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
893893
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
894894
Snapshots: 0 earliest seq num: 0
895895
Table iters: 0
@@ -947,7 +947,7 @@ Local tables size: 2.2KB
947947
Compression types: snappy: 3
948948
Table stats: all loaded
949949
Block cache: 0 entries (0B) hit rate: 0.0%
950-
Table cache: 0 entries (0B) hit rate: 0.0%
950+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
951951
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
952952
Snapshots: 0 earliest seq num: 0
953953
Table iters: 0
@@ -991,7 +991,7 @@ Local tables size: 0B
991991
Compression types: snappy: 3
992992
Table stats: all loaded
993993
Block cache: 0 entries (0B) hit rate: 0.0%
994-
Table cache: 0 entries (0B) hit rate: 50.0%
994+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 50.0%
995995
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
996996
Snapshots: 0 earliest seq num: 0
997997
Table iters: 0
@@ -1051,7 +1051,7 @@ Local tables size: 0B
10511051
Compression types: snappy: 4
10521052
Table stats: all loaded
10531053
Block cache: 2 entries (787B) hit rate: 0.0%
1054-
Table cache: 0 entries (0B) hit rate: 50.0%
1054+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 50.0%
10551055
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
10561056
Snapshots: 0 earliest seq num: 0
10571057
Table iters: 0
@@ -1103,7 +1103,7 @@ Local tables size: 755B
11031103
Compression types: snappy: 5
11041104
Table stats: all loaded
11051105
Block cache: 2 entries (787B) hit rate: 0.0%
1106-
Table cache: 0 entries (0B) hit rate: 50.0%
1106+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 50.0%
11071107
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
11081108
Snapshots: 0 earliest seq num: 0
11091109
Table iters: 0
@@ -1144,7 +1144,7 @@ Local tables size: 755B
11441144
Compression types: snappy: 5
11451145
Table stats: all loaded
11461146
Block cache: 0 entries (0B) hit rate: 0.0%
1147-
Table cache: 0 entries (0B) hit rate: 0.0%
1147+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
11481148
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
11491149
Snapshots: 0 earliest seq num: 0
11501150
Table iters: 0
@@ -1188,7 +1188,7 @@ Local tables size: 758B
11881188
Compression types: snappy: 3
11891189
Table stats: all loaded
11901190
Block cache: 0 entries (0B) hit rate: 0.0%
1191-
Table cache: 0 entries (0B) hit rate: 0.0%
1191+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
11921192
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
11931193
Snapshots: 0 earliest seq num: 0
11941194
Table iters: 0
@@ -1271,7 +1271,7 @@ Compression types: snappy: 5
12711271
Garbage: point-deletions 502B range-deletions 1.5KB
12721272
Table stats: all loaded
12731273
Block cache: 2 entries (774B) hit rate: 0.0%
1274-
Table cache: 2 entries (992B) hit rate: 0.0%
1274+
File cache: 2 tables, 0 blobfiles (560B) hit rate: 0.0%
12751275
Range key sets: 0 Tombstones: 3 Total missized tombstones encountered: 0
12761276
Snapshots: 0 earliest seq num: 0
12771277
Table iters: 0
@@ -1313,7 +1313,7 @@ Compression types: snappy: 5
13131313
Garbage: point-deletions 502B range-deletions 1.5KB
13141314
Table stats: all loaded
13151315
Block cache: 2 entries (774B) hit rate: 0.0%
1316-
Table cache: 2 entries (992B) hit rate: 0.0%
1316+
File cache: 2 tables, 0 blobfiles (560B) hit rate: 0.0%
13171317
Range key sets: 0 Tombstones: 3 Total missized tombstones encountered: 0
13181318
Snapshots: 0 earliest seq num: 0
13191319
Table iters: 0

tool/testdata/db_lsm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Local tables size: 709B
3434
Compression types: unknown: 1
3535
Table stats: <redacted>
3636
Block cache: 0 entries (0B) hit rate: 0.0%
37-
Table cache: 0 entries (0B) hit rate: 0.0%
37+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
3838
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
3939
Snapshots: 0 earliest seq num: 0
4040
Table iters: 0
@@ -70,7 +70,7 @@ Local tables size: 709B
7070
Compression types: unknown: 1
7171
Table stats: <redacted>
7272
Block cache: 0 entries (0B) hit rate: 0.0%
73-
Table cache: 0 entries (0B) hit rate: 0.0%
73+
File cache: 0 tables, 0 blobfiles (0B) hit rate: 0.0%
7474
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
7575
Snapshots: 0 earliest seq num: 0
7676
Table iters: 0

0 commit comments

Comments
 (0)