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