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