github.com/ethereum/go-ethereum@v1.14.3/metrics/gauge.go (about)

     1  package metrics
     2  
     3  import "sync/atomic"
     4  
     5  // GaugeSnapshot contains a readonly int64.
     6  type GaugeSnapshot interface {
     7  	Value() int64
     8  }
     9  
    10  // Gauge holds an int64 value that can be set arbitrarily.
    11  type Gauge interface {
    12  	Snapshot() GaugeSnapshot
    13  	Update(int64)
    14  	UpdateIfGt(int64)
    15  	Dec(int64)
    16  	Inc(int64)
    17  }
    18  
    19  // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
    20  // new StandardGauge.
    21  func GetOrRegisterGauge(name string, r Registry) Gauge {
    22  	if nil == r {
    23  		r = DefaultRegistry
    24  	}
    25  	return r.GetOrRegister(name, NewGauge).(Gauge)
    26  }
    27  
    28  // NewGauge constructs a new StandardGauge.
    29  func NewGauge() Gauge {
    30  	if !Enabled {
    31  		return NilGauge{}
    32  	}
    33  	return &StandardGauge{}
    34  }
    35  
    36  // NewRegisteredGauge constructs and registers a new StandardGauge.
    37  func NewRegisteredGauge(name string, r Registry) Gauge {
    38  	c := NewGauge()
    39  	if nil == r {
    40  		r = DefaultRegistry
    41  	}
    42  	r.Register(name, c)
    43  	return c
    44  }
    45  
    46  // gaugeSnapshot is a read-only copy of another Gauge.
    47  type gaugeSnapshot int64
    48  
    49  // Value returns the value at the time the snapshot was taken.
    50  func (g gaugeSnapshot) Value() int64 { return int64(g) }
    51  
    52  // NilGauge is a no-op Gauge.
    53  type NilGauge struct{}
    54  
    55  func (NilGauge) Snapshot() GaugeSnapshot { return (*emptySnapshot)(nil) }
    56  func (NilGauge) Update(v int64)          {}
    57  func (NilGauge) UpdateIfGt(v int64)      {}
    58  func (NilGauge) Dec(i int64)             {}
    59  func (NilGauge) Inc(i int64)             {}
    60  
    61  // StandardGauge is the standard implementation of a Gauge and uses the
    62  // sync/atomic package to manage a single int64 value.
    63  type StandardGauge struct {
    64  	value atomic.Int64
    65  }
    66  
    67  // Snapshot returns a read-only copy of the gauge.
    68  func (g *StandardGauge) Snapshot() GaugeSnapshot {
    69  	return gaugeSnapshot(g.value.Load())
    70  }
    71  
    72  // Update updates the gauge's value.
    73  func (g *StandardGauge) Update(v int64) {
    74  	g.value.Store(v)
    75  }
    76  
    77  // UpdateIfGt updates the gauge's value if v is larger then the current value.
    78  func (g *StandardGauge) UpdateIfGt(v int64) {
    79  	for {
    80  		exist := g.value.Load()
    81  		if exist >= v {
    82  			break
    83  		}
    84  		if g.value.CompareAndSwap(exist, v) {
    85  			break
    86  		}
    87  	}
    88  }
    89  
    90  // Dec decrements the gauge's current value by the given amount.
    91  func (g *StandardGauge) Dec(i int64) {
    92  	g.value.Add(-i)
    93  }
    94  
    95  // Inc increments the gauge's current value by the given amount.
    96  func (g *StandardGauge) Inc(i int64) {
    97  	g.value.Add(i)
    98  }