LCOV - code coverage report
Current view: top level - pebble - metrics.go (source / functions) Hit Total Coverage
Test: 2024-07-22 08:17Z 72c3f550 - meta test only.lcov Lines: 35 284 12.3 %
Date: 2024-07-22 08:18:19 Functions: 0 0 -

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

Generated by: LCOV version 1.14