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