mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
metrics: simplify float64 counter
This commit is contained in:
parent
f27df5c6a5
commit
3c3623d61e
1 changed files with 5 additions and 7 deletions
|
|
@ -36,28 +36,26 @@ type CounterFloat64Snapshot float64
|
||||||
func (c CounterFloat64Snapshot) Count() float64 { return float64(c) }
|
func (c CounterFloat64Snapshot) Count() float64 { return float64(c) }
|
||||||
|
|
||||||
// CounterFloat64 is the uses atomic to manage a single float64 value.
|
// CounterFloat64 is the uses atomic to manage a single float64 value.
|
||||||
type CounterFloat64 struct {
|
type CounterFloat64 atomic.Uint64
|
||||||
floatBits atomic.Uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear sets the counter to zero.
|
// Clear sets the counter to zero.
|
||||||
func (c *CounterFloat64) Clear() {
|
func (c *CounterFloat64) Clear() {
|
||||||
c.floatBits.Store(0)
|
(*atomic.Uint64)(c).Store(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dec decrements the counter by the given amount.
|
// Dec decrements the counter by the given amount.
|
||||||
func (c *CounterFloat64) Dec(v float64) {
|
func (c *CounterFloat64) Dec(v float64) {
|
||||||
atomicAddFloat(&c.floatBits, -v)
|
atomicAddFloat((*atomic.Uint64)(c), -v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inc increments the counter by the given amount.
|
// Inc increments the counter by the given amount.
|
||||||
func (c *CounterFloat64) Inc(v float64) {
|
func (c *CounterFloat64) Inc(v float64) {
|
||||||
atomicAddFloat(&c.floatBits, v)
|
atomicAddFloat((*atomic.Uint64)(c), v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Snapshot returns a read-only copy of the counter.
|
// Snapshot returns a read-only copy of the counter.
|
||||||
func (c *CounterFloat64) Snapshot() CounterFloat64Snapshot {
|
func (c *CounterFloat64) Snapshot() CounterFloat64Snapshot {
|
||||||
return CounterFloat64Snapshot(math.Float64frombits(c.floatBits.Load()))
|
return CounterFloat64Snapshot(math.Float64frombits((*atomic.Uint64)(c).Load()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func atomicAddFloat(fbits *atomic.Uint64, v float64) {
|
func atomicAddFloat(fbits *atomic.Uint64, v float64) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue