Line data Source code
1 : // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2 : // of this source code is governed by a BSD-style license that can be found in
3 : // the LICENSE file.
4 :
5 : package pebble
6 :
7 : import (
8 : "fmt"
9 : "math"
10 : "time"
11 :
12 : "github.com/cockroachdb/pebble/internal/base"
13 : "github.com/cockroachdb/pebble/internal/cache"
14 : "github.com/cockroachdb/pebble/internal/humanize"
15 : "github.com/cockroachdb/pebble/objstorage/objstorageprovider/sharedcache"
16 : "github.com/cockroachdb/pebble/record"
17 : "github.com/cockroachdb/pebble/sstable"
18 : "github.com/cockroachdb/redact"
19 : "github.com/prometheus/client_golang/prometheus"
20 : )
21 :
22 : // CacheMetrics holds metrics for the block and table cache.
23 : type CacheMetrics = cache.Metrics
24 :
25 : // FilterMetrics holds metrics for the filter policy
26 : type FilterMetrics = sstable.FilterMetrics
27 :
28 : // ThroughputMetric is a cumulative throughput metric. See the detailed
29 : // comment in base.
30 : type ThroughputMetric = base.ThroughputMetric
31 :
32 : // SecondaryCacheMetrics holds metrics for the persistent secondary cache
33 : // that caches commonly accessed blocks from blob storage on a local
34 : // file system.
35 : type SecondaryCacheMetrics = sharedcache.Metrics
36 :
37 : // LevelMetrics holds per-level metrics such as the number of files and total
38 : // size of the files, and compaction related metrics.
39 : type LevelMetrics struct {
40 : // The number of sublevels within the level. The sublevel count corresponds
41 : // to the read amplification for the level. An empty level will have a
42 : // sublevel count of 0, implying no read amplification. Only L0 will have
43 : // a sublevel count other than 0 or 1.
44 : Sublevels int32
45 : // The total number of files in the level.
46 : NumFiles int64
47 : // The total size in bytes of the files in the level.
48 : Size int64
49 : // The level's compaction score. This is the compensatedScoreRatio in the
50 : // candidateLevelInfo.
51 : Score float64
52 : // The number of incoming bytes from other levels read during
53 : // compactions. This excludes bytes moved and bytes ingested. For L0 this is
54 : // the bytes written to the WAL.
55 : BytesIn uint64
56 : // The number of bytes ingested. The sibling metric for tables is
57 : // TablesIngested.
58 : BytesIngested uint64
59 : // The number of bytes moved into the level by a "move" compaction. The
60 : // sibling metric for tables is TablesMoved.
61 : BytesMoved uint64
62 : // The number of bytes read for compactions at the level. This includes bytes
63 : // read from other levels (BytesIn), as well as bytes read for the level.
64 : BytesRead uint64
65 : // The number of bytes written during compactions. The sibling
66 : // metric for tables is TablesCompacted. This metric may be summed
67 : // with BytesFlushed to compute the total bytes written for the level.
68 : BytesCompacted uint64
69 : // The number of bytes written during flushes. The sibling
70 : // metrics for tables is TablesFlushed. This metric is always
71 : // zero for all levels other than L0.
72 : BytesFlushed uint64
73 : // The number of sstables compacted to this level.
74 : TablesCompacted uint64
75 : // The number of sstables flushed to this level.
76 : TablesFlushed uint64
77 : // The number of sstables ingested into the level.
78 : TablesIngested uint64
79 : // The number of sstables moved to this level by a "move" compaction.
80 : TablesMoved uint64
81 :
82 : MultiLevel struct {
83 : // BytesInTop are the total bytes in a multilevel compaction coming from the top level.
84 : BytesInTop uint64
85 :
86 : // BytesIn, exclusively for multiLevel compactions.
87 : BytesIn uint64
88 :
89 : // BytesRead, exclusively for multilevel compactions.
90 : BytesRead uint64
91 : }
92 :
93 : // Additional contains misc additional metrics that are not always printed.
94 : Additional struct {
95 : // The sum of Properties.ValueBlocksSize for all the sstables in this
96 : // level. Printed by LevelMetrics.format iff there is at least one level
97 : // with a non-zero value.
98 : ValueBlocksSize uint64
99 : // Cumulative metrics about bytes written to data blocks and value blocks,
100 : // via compactions (except move compactions) or flushes. Not printed by
101 : // LevelMetrics.format, but are available to sophisticated clients.
102 : BytesWrittenDataBlocks uint64
103 : BytesWrittenValueBlocks uint64
104 : }
105 : }
106 :
107 : // Add updates the counter metrics for the level.
108 1 : func (m *LevelMetrics) Add(u *LevelMetrics) {
109 1 : m.NumFiles += u.NumFiles
110 1 : m.Size += u.Size
111 1 : m.BytesIn += u.BytesIn
112 1 : m.BytesIngested += u.BytesIngested
113 1 : m.BytesMoved += u.BytesMoved
114 1 : m.BytesRead += u.BytesRead
115 1 : m.BytesCompacted += u.BytesCompacted
116 1 : m.BytesFlushed += u.BytesFlushed
117 1 : m.TablesCompacted += u.TablesCompacted
118 1 : m.TablesFlushed += u.TablesFlushed
119 1 : m.TablesIngested += u.TablesIngested
120 1 : m.TablesMoved += u.TablesMoved
121 1 : m.MultiLevel.BytesInTop += u.MultiLevel.BytesInTop
122 1 : m.MultiLevel.BytesRead += u.MultiLevel.BytesRead
123 1 : m.MultiLevel.BytesIn += u.MultiLevel.BytesIn
124 1 : m.Additional.BytesWrittenDataBlocks += u.Additional.BytesWrittenDataBlocks
125 1 : m.Additional.BytesWrittenValueBlocks += u.Additional.BytesWrittenValueBlocks
126 1 : m.Additional.ValueBlocksSize += u.Additional.ValueBlocksSize
127 1 : }
128 :
129 : // WriteAmp computes the write amplification for compactions at this
130 : // level. Computed as (BytesFlushed + BytesCompacted) / BytesIn.
131 0 : func (m *LevelMetrics) WriteAmp() float64 {
132 0 : if m.BytesIn == 0 {
133 0 : return 0
134 0 : }
135 0 : return float64(m.BytesFlushed+m.BytesCompacted) / float64(m.BytesIn)
136 : }
137 :
138 : // Metrics holds metrics for various subsystems of the DB such as the Cache,
139 : // Compactions, WAL, and per-Level metrics.
140 : //
141 : // TODO(peter): The testing of these metrics is relatively weak. There should
142 : // be testing that performs various operations on a DB and verifies that the
143 : // metrics reflect those operations.
144 : type Metrics struct {
145 : BlockCache CacheMetrics
146 :
147 : Compact struct {
148 : // The total number of compactions, and per-compaction type counts.
149 : Count int64
150 : DefaultCount int64
151 : DeleteOnlyCount int64
152 : ElisionOnlyCount int64
153 : MoveCount int64
154 : ReadCount int64
155 : RewriteCount int64
156 : MultiLevelCount int64
157 : CounterLevelCount int64
158 : // An estimate of the number of bytes that need to be compacted for the LSM
159 : // to reach a stable state.
160 : EstimatedDebt uint64
161 : // Number of bytes present in sstables being written by in-progress
162 : // compactions. This value will be zero if there are no in-progress
163 : // compactions.
164 : InProgressBytes int64
165 : // Number of compactions that are in-progress.
166 : NumInProgress int64
167 : // MarkedFiles is a count of files that are marked for
168 : // compaction. Such files are compacted in a rewrite compaction
169 : // when no other compactions are picked.
170 : MarkedFiles int
171 : // Duration records the cumulative duration of all compactions since the
172 : // database was opened.
173 : Duration time.Duration
174 : }
175 :
176 : Ingest struct {
177 : // The total number of ingestions
178 : Count uint64
179 : }
180 :
181 : Flush struct {
182 : // The total number of flushes.
183 : Count int64
184 : WriteThroughput ThroughputMetric
185 : // Number of flushes that are in-progress. In the current implementation
186 : // this will always be zero or one.
187 : NumInProgress int64
188 : // AsIngestCount is a monotonically increasing counter of flush operations
189 : // handling ingested tables.
190 : AsIngestCount uint64
191 : // AsIngestCount is a monotonically increasing counter of tables ingested as
192 : // flushables.
193 : AsIngestTableCount uint64
194 : // AsIngestBytes is a monotonically increasing counter of the bytes flushed
195 : // for flushables that originated as ingestion operations.
196 : AsIngestBytes uint64
197 : }
198 :
199 : Filter FilterMetrics
200 :
201 : Levels [numLevels]LevelMetrics
202 :
203 : MemTable struct {
204 : // The number of bytes allocated by memtables and large (flushable)
205 : // batches.
206 : Size uint64
207 : // The count of memtables.
208 : Count int64
209 : // The number of bytes present in zombie memtables which are no longer
210 : // referenced by the current DB state. An unbounded number of memtables
211 : // may be zombie if they're still in use by an iterator. One additional
212 : // memtable may be zombie if it's no longer in use and waiting to be
213 : // recycled.
214 : ZombieSize uint64
215 : // The count of zombie memtables.
216 : ZombieCount int64
217 : }
218 :
219 : Keys struct {
220 : // The approximate count of internal range key set keys in the database.
221 : RangeKeySetsCount uint64
222 : // The approximate count of internal tombstones (DEL, SINGLEDEL and
223 : // RANGEDEL key kinds) within the database.
224 : TombstoneCount uint64
225 : // A cumulative total number of missized DELSIZED keys encountered by
226 : // compactions since the database was opened.
227 : MissizedTombstonesCount uint64
228 : }
229 :
230 : Snapshots struct {
231 : // The number of currently open snapshots.
232 : Count int
233 : // The sequence number of the earliest, currently open snapshot.
234 : EarliestSeqNum uint64
235 : // A running tally of keys written to sstables during flushes or
236 : // compactions that would've been elided if it weren't for open
237 : // snapshots.
238 : PinnedKeys uint64
239 : // A running cumulative sum of the size of keys and values written to
240 : // sstables during flushes or compactions that would've been elided if
241 : // it weren't for open snapshots.
242 : PinnedSize uint64
243 : }
244 :
245 : Table struct {
246 : // The number of bytes present in obsolete tables which are no longer
247 : // referenced by the current DB state or any open iterators.
248 : ObsoleteSize uint64
249 : // The count of obsolete tables.
250 : ObsoleteCount int64
251 : // The number of bytes present in zombie tables which are no longer
252 : // referenced by the current DB state but are still in use by an iterator.
253 : ZombieSize uint64
254 : // The count of zombie tables.
255 : ZombieCount int64
256 : }
257 :
258 : TableCache CacheMetrics
259 :
260 : // Count of the number of open sstable iterators.
261 : TableIters int64
262 : // Uptime is the total time since this DB was opened.
263 : Uptime time.Duration
264 :
265 : WAL struct {
266 : // Number of live WAL files.
267 : Files int64
268 : // Number of obsolete WAL files.
269 : ObsoleteFiles int64
270 : // Physical size of the obsolete WAL files.
271 : ObsoletePhysicalSize uint64
272 : // Size of the live data in the WAL files. Note that with WAL file
273 : // recycling this is less than the actual on-disk size of the WAL files.
274 : Size uint64
275 : // Physical size of the WAL files on-disk. With WAL file recycling,
276 : // this is greater than the live data in WAL files.
277 : PhysicalSize uint64
278 : // Number of logical bytes written to the WAL.
279 : BytesIn uint64
280 : // Number of bytes written to the WAL.
281 : BytesWritten uint64
282 : }
283 :
284 : LogWriter struct {
285 : FsyncLatency prometheus.Histogram
286 : record.LogWriterMetrics
287 : }
288 :
289 : SecondaryCacheMetrics SecondaryCacheMetrics
290 :
291 : private struct {
292 : optionsFileSize uint64
293 : manifestFileSize uint64
294 : }
295 : }
296 :
297 : var (
298 : // FsyncLatencyBuckets are prometheus histogram buckets suitable for a histogram
299 : // that records latencies for fsyncs.
300 : FsyncLatencyBuckets = append(
301 : prometheus.LinearBuckets(0.0, float64(time.Microsecond*100), 50),
302 : prometheus.ExponentialBucketsRange(float64(time.Millisecond*5), float64(10*time.Second), 50)...,
303 : )
304 :
305 : // SecondaryCacheIOBuckets exported to enable exporting from package pebble to
306 : // enable exporting metrics with below buckets in CRDB.
307 : SecondaryCacheIOBuckets = sharedcache.IOBuckets
308 : // SecondaryCacheChannelWriteBuckets exported to enable exporting from package
309 : // pebble to enable exporting metrics with below buckets in CRDB.
310 : SecondaryCacheChannelWriteBuckets = sharedcache.ChannelWriteBuckets
311 : )
312 :
313 : // DiskSpaceUsage returns the total disk space used by the database in bytes,
314 : // including live and obsolete files.
315 0 : func (m *Metrics) DiskSpaceUsage() uint64 {
316 0 : var usageBytes uint64
317 0 : usageBytes += m.WAL.PhysicalSize
318 0 : usageBytes += m.WAL.ObsoletePhysicalSize
319 0 : for _, lm := range m.Levels {
320 0 : usageBytes += uint64(lm.Size)
321 0 : }
322 0 : usageBytes += m.Table.ObsoleteSize
323 0 : usageBytes += m.Table.ZombieSize
324 0 : usageBytes += m.private.optionsFileSize
325 0 : usageBytes += m.private.manifestFileSize
326 0 : usageBytes += uint64(m.Compact.InProgressBytes)
327 0 : return usageBytes
328 : }
329 :
330 : // ReadAmp returns the current read amplification of the database.
331 : // It's computed as the number of sublevels in L0 + the number of non-empty
332 : // levels below L0.
333 0 : func (m *Metrics) ReadAmp() int {
334 0 : var ramp int32
335 0 : for _, l := range m.Levels {
336 0 : ramp += l.Sublevels
337 0 : }
338 0 : return int(ramp)
339 : }
340 :
341 : // Total returns the sum of the per-level metrics and WAL metrics.
342 1 : func (m *Metrics) Total() LevelMetrics {
343 1 : var total LevelMetrics
344 1 : for level := 0; level < numLevels; level++ {
345 1 : l := &m.Levels[level]
346 1 : total.Add(l)
347 1 : total.Sublevels += l.Sublevels
348 1 : }
349 : // Compute total bytes-in as the bytes written to the WAL + bytes ingested.
350 1 : total.BytesIn = m.WAL.BytesWritten + total.BytesIngested
351 1 : // Add the total bytes-in to the total bytes-flushed. This is to account for
352 1 : // the bytes written to the log and bytes written externally and then
353 1 : // ingested.
354 1 : total.BytesFlushed += total.BytesIn
355 1 : return total
356 : }
357 :
358 : // String pretty-prints the metrics as below:
359 : //
360 : // | | | | ingested | moved | written | | amp
361 : // level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w
362 : // ------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------
363 : // 0 | 101 102B 0B | 103.0 | 104B | 112 104B | 113 106B | 221 217B | 107B | 1 2.1
364 : // 1 | 201 202B 0B | 203.0 | 204B | 212 204B | 213 206B | 421 417B | 207B | 2 2.0
365 : // 2 | 301 302B 0B | 303.0 | 304B | 312 304B | 313 306B | 621 617B | 307B | 3 2.0
366 : // 3 | 401 402B 0B | 403.0 | 404B | 412 404B | 413 406B | 821 817B | 407B | 4 2.0
367 : // 4 | 501 502B 0B | 503.0 | 504B | 512 504B | 513 506B | 1.0K 1017B | 507B | 5 2.0
368 : // 5 | 601 602B 0B | 603.0 | 604B | 612 604B | 613 606B | 1.2K 1.2KB | 607B | 6 2.0
369 : // 6 | 701 702B 0B | - | 704B | 712 704B | 713 706B | 1.4K 1.4KB | 707B | 7 2.0
370 : // total | 2.8K 2.7KB 0B | - | 2.8KB | 2.9K 2.8KB | 2.9K 2.8KB | 5.7K 8.4KB | 2.8KB | 28 3.0
371 : // -----------------------------------------------------------------------------------------------------------
372 : // WAL: 22 files (24B) in: 25B written: 26B (4% overhead)
373 : // Flushes: 8
374 : // Compactions: 5 estimated debt: 6B in progress: 2 (7B)
375 : // default: 27 delete: 28 elision: 29 move: 30 read: 31 rewrite: 32 multi-level: 33
376 : // MemTables: 12 (11B) zombie: 14 (13B)
377 : // Zombie tables: 16 (15B)
378 : // Block cache: 2 entries (1B) hit rate: 42.9%
379 : // Table cache: 18 entries (17B) hit rate: 48.7%
380 : // Secondary cache: 40 entries (40B) hit rate: 49.9%
381 : // Snapshots: 4 earliest seq num: 1024
382 : // Table iters: 21
383 : // Filter utility: 47.4%
384 : // Ingestions: 27 as flushable: 36 (34B in 35 tables)
385 0 : func (m *Metrics) String() string {
386 0 : return redact.StringWithoutMarkers(m)
387 0 : }
388 :
389 : var _ redact.SafeFormatter = &Metrics{}
390 :
391 : // SafeFormat implements redact.SafeFormatter.
392 0 : func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
393 0 : // NB: Pebble does not make any assumptions as to which Go primitive types
394 0 : // have been registered as safe with redact.RegisterSafeType and does not
395 0 : // register any types itself. Some of the calls to `redact.Safe`, etc are
396 0 : // superfluous in the context of CockroachDB, which registers all the Go
397 0 : // numeric types as safe.
398 0 :
399 0 : // TODO(jackson): There are a few places where we use redact.SafeValue
400 0 : // instead of redact.RedactableString. This is necessary because of a bug
401 0 : // whereby formatting a redact.RedactableString argument does not respect
402 0 : // width specifiers. When the issue is fixed, we can convert these to
403 0 : // RedactableStrings. https://github.com/cockroachdb/redact/issues/17
404 0 :
405 0 : multiExists := m.Compact.MultiLevelCount > 0
406 0 : appendIfMulti := func(line redact.SafeString) {
407 0 : if multiExists {
408 0 : w.SafeString(line)
409 0 : }
410 : }
411 0 : newline := func() {
412 0 : w.SafeString("\n")
413 0 : }
414 :
415 0 : w.SafeString(" | | | | ingested | moved | written | | amp")
416 0 : appendIfMulti(" | multilevel")
417 0 : newline()
418 0 : w.SafeString("level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w")
419 0 : appendIfMulti(" | top in read")
420 0 : newline()
421 0 : w.SafeString("------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------")
422 0 : appendIfMulti("-+------------------")
423 0 : newline()
424 0 :
425 0 : // formatRow prints out a row of the table.
426 0 : formatRow := func(m *LevelMetrics, score float64) {
427 0 : scoreStr := "-"
428 0 : if !math.IsNaN(score) {
429 0 : // Try to keep the string no longer than 5 characters.
430 0 : switch {
431 0 : case score < 99.995:
432 0 : scoreStr = fmt.Sprintf("%.2f", score)
433 0 : case score < 999.95:
434 0 : scoreStr = fmt.Sprintf("%.1f", score)
435 0 : default:
436 0 : scoreStr = fmt.Sprintf("%.0f", score)
437 : }
438 : }
439 0 : var wampStr string
440 0 : if wamp := m.WriteAmp(); wamp > 99.5 {
441 0 : wampStr = fmt.Sprintf("%.0f", wamp)
442 0 : } else {
443 0 : wampStr = fmt.Sprintf("%.1f", wamp)
444 0 : }
445 :
446 0 : w.Printf("| %5s %6s %6s | %5s | %5s | %5s %6s | %5s %6s | %5s %6s | %5s | %3d %4s",
447 0 : humanize.Count.Int64(m.NumFiles),
448 0 : humanize.Bytes.Int64(m.Size),
449 0 : humanize.Bytes.Uint64(m.Additional.ValueBlocksSize),
450 0 : redact.Safe(scoreStr),
451 0 : humanize.Bytes.Uint64(m.BytesIn),
452 0 : humanize.Count.Uint64(m.TablesIngested),
453 0 : humanize.Bytes.Uint64(m.BytesIngested),
454 0 : humanize.Count.Uint64(m.TablesMoved),
455 0 : humanize.Bytes.Uint64(m.BytesMoved),
456 0 : humanize.Count.Uint64(m.TablesFlushed+m.TablesCompacted),
457 0 : humanize.Bytes.Uint64(m.BytesFlushed+m.BytesCompacted),
458 0 : humanize.Bytes.Uint64(m.BytesRead),
459 0 : redact.Safe(m.Sublevels),
460 0 : redact.Safe(wampStr))
461 0 :
462 0 : if multiExists {
463 0 : w.Printf(" | %5s %5s %5s",
464 0 : humanize.Bytes.Uint64(m.MultiLevel.BytesInTop),
465 0 : humanize.Bytes.Uint64(m.MultiLevel.BytesIn),
466 0 : humanize.Bytes.Uint64(m.MultiLevel.BytesRead))
467 0 : }
468 0 : newline()
469 : }
470 :
471 0 : var total LevelMetrics
472 0 : for level := 0; level < numLevels; level++ {
473 0 : l := &m.Levels[level]
474 0 : w.Printf("%5d ", redact.Safe(level))
475 0 :
476 0 : // Format the score.
477 0 : score := math.NaN()
478 0 : if level < numLevels-1 {
479 0 : score = l.Score
480 0 : }
481 0 : formatRow(l, score)
482 0 : total.Add(l)
483 0 : total.Sublevels += l.Sublevels
484 : }
485 : // Compute total bytes-in as the bytes written to the WAL + bytes ingested.
486 0 : total.BytesIn = m.WAL.BytesWritten + total.BytesIngested
487 0 : // Add the total bytes-in to the total bytes-flushed. This is to account for
488 0 : // the bytes written to the log and bytes written externally and then
489 0 : // ingested.
490 0 : total.BytesFlushed += total.BytesIn
491 0 : w.SafeString("total ")
492 0 : formatRow(&total, math.NaN())
493 0 :
494 0 : w.SafeString("-----------------------------------------------------------------------------------------------------------")
495 0 : appendIfMulti("--------------------")
496 0 : newline()
497 0 : w.Printf("WAL: %d files (%s) in: %s written: %s (%.0f%% overhead)\n",
498 0 : redact.Safe(m.WAL.Files),
499 0 : humanize.Bytes.Uint64(m.WAL.Size),
500 0 : humanize.Bytes.Uint64(m.WAL.BytesIn),
501 0 : humanize.Bytes.Uint64(m.WAL.BytesWritten),
502 0 : redact.Safe(percent(int64(m.WAL.BytesWritten)-int64(m.WAL.BytesIn), int64(m.WAL.BytesIn))))
503 0 :
504 0 : w.Printf("Flushes: %d\n", redact.Safe(m.Flush.Count))
505 0 :
506 0 : w.Printf("Compactions: %d estimated debt: %s in progress: %d (%s)\n",
507 0 : redact.Safe(m.Compact.Count),
508 0 : humanize.Bytes.Uint64(m.Compact.EstimatedDebt),
509 0 : redact.Safe(m.Compact.NumInProgress),
510 0 : humanize.Bytes.Int64(m.Compact.InProgressBytes))
511 0 :
512 0 : w.Printf(" default: %d delete: %d elision: %d move: %d read: %d rewrite: %d multi-level: %d\n",
513 0 : redact.Safe(m.Compact.DefaultCount),
514 0 : redact.Safe(m.Compact.DeleteOnlyCount),
515 0 : redact.Safe(m.Compact.ElisionOnlyCount),
516 0 : redact.Safe(m.Compact.MoveCount),
517 0 : redact.Safe(m.Compact.ReadCount),
518 0 : redact.Safe(m.Compact.RewriteCount),
519 0 : redact.Safe(m.Compact.MultiLevelCount))
520 0 :
521 0 : w.Printf("MemTables: %d (%s) zombie: %d (%s)\n",
522 0 : redact.Safe(m.MemTable.Count),
523 0 : humanize.Bytes.Uint64(m.MemTable.Size),
524 0 : redact.Safe(m.MemTable.ZombieCount),
525 0 : humanize.Bytes.Uint64(m.MemTable.ZombieSize))
526 0 :
527 0 : w.Printf("Zombie tables: %d (%s)\n",
528 0 : redact.Safe(m.Table.ZombieCount),
529 0 : humanize.Bytes.Uint64(m.Table.ZombieSize))
530 0 :
531 0 : formatCacheMetrics := func(m *CacheMetrics, name redact.SafeString) {
532 0 : w.Printf("%s: %s entries (%s) hit rate: %.1f%%\n",
533 0 : name,
534 0 : humanize.Count.Int64(m.Count),
535 0 : humanize.Bytes.Int64(m.Size),
536 0 : redact.Safe(hitRate(m.Hits, m.Misses)))
537 0 : }
538 0 : formatCacheMetrics(&m.BlockCache, "Block cache")
539 0 : formatCacheMetrics(&m.TableCache, "Table cache")
540 0 :
541 0 : formatSharedCacheMetrics := func(w redact.SafePrinter, m *SecondaryCacheMetrics, name redact.SafeString) {
542 0 : w.Printf("%s: %s entries (%s) hit rate: %.1f%%\n",
543 0 : name,
544 0 : humanize.Count.Int64(m.Count),
545 0 : humanize.Bytes.Int64(m.Size),
546 0 : redact.Safe(hitRate(m.ReadsWithFullHit, m.ReadsWithPartialHit+m.ReadsWithNoHit)))
547 0 : }
548 0 : formatSharedCacheMetrics(w, &m.SecondaryCacheMetrics, "Secondary cache")
549 0 :
550 0 : w.Printf("Snapshots: %d earliest seq num: %d\n",
551 0 : redact.Safe(m.Snapshots.Count),
552 0 : redact.Safe(m.Snapshots.EarliestSeqNum))
553 0 :
554 0 : w.Printf("Table iters: %d\n", redact.Safe(m.TableIters))
555 0 : w.Printf("Filter utility: %.1f%%\n", redact.Safe(hitRate(m.Filter.Hits, m.Filter.Misses)))
556 0 : w.Printf("Ingestions: %d as flushable: %d (%s in %d tables)\n",
557 0 : redact.Safe(m.Ingest.Count),
558 0 : redact.Safe(m.Flush.AsIngestCount),
559 0 : humanize.Bytes.Uint64(m.Flush.AsIngestBytes),
560 0 : redact.Safe(m.Flush.AsIngestTableCount))
561 : }
562 :
563 0 : func hitRate(hits, misses int64) float64 {
564 0 : return percent(hits, hits+misses)
565 0 : }
566 :
567 0 : func percent(numerator, denominator int64) float64 {
568 0 : if denominator == 0 {
569 0 : return 0
570 0 : }
571 0 : return 100 * float64(numerator) / float64(denominator)
572 : }
573 :
574 : // StringForTests is identical to m.String() on 64-bit platforms. It is used to
575 : // provide a platform-independent result for tests.
576 0 : func (m *Metrics) StringForTests() string {
577 0 : mCopy := *m
578 0 : if math.MaxInt == math.MaxInt32 {
579 0 : // This is the difference in Sizeof(sstable.Reader{})) between 64 and 32 bit
580 0 : // platforms.
581 0 : const tableCacheSizeAdjustment = 212
582 0 : mCopy.TableCache.Size += mCopy.TableCache.Count * tableCacheSizeAdjustment
583 0 : }
584 0 : return redact.StringWithoutMarkers(&mCopy)
585 : }
|