Skip to content

Commit

Permalink
Revert "metrics: refactor metrics (ethereum#28035)"
Browse files Browse the repository at this point in the history
This reverts commit 135d2d1.
  • Loading branch information
devopsbo3 authored Nov 10, 2023
1 parent 6d5bfae commit 12b9cd2
Show file tree
Hide file tree
Showing 50 changed files with 2,032 additions and 1,035 deletions.
10 changes: 6 additions & 4 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,12 @@ func (s *StateDB) deleteStorage(addr common.Address, addrHash common.Hash, root
slotDeletionSkip.Inc(1)
}
n := int64(len(slots))

slotDeletionMaxCount.UpdateIfGt(int64(len(slots)))
slotDeletionMaxSize.UpdateIfGt(int64(size))

if n > slotDeletionMaxCount.Value() {
slotDeletionMaxCount.Update(n)
}
if int64(size) > slotDeletionMaxSize.Value() {
slotDeletionMaxSize.Update(int64(size))
}
slotDeletionTimer.UpdateSince(start)
slotDeletionCount.Mark(n)
slotDeletionSize.Mark(int64(size))
Expand Down
72 changes: 52 additions & 20 deletions metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
"sync/atomic"
)

type CounterSnapshot interface {
Count() int64
}

// Counters hold an int64 value that can be incremented and decremented.
type Counter interface {
Clear()
Count() int64
Dec(int64)
Inc(int64)
Snapshot() CounterSnapshot
Snapshot() Counter
}

// GetOrRegisterCounter returns an existing Counter or constructs and registers
Expand Down Expand Up @@ -41,13 +38,13 @@ func NewCounter() Counter {
if !Enabled {
return NilCounter{}
}
return new(StandardCounter)
return &StandardCounter{}
}

// NewCounterForced constructs a new StandardCounter and returns it no matter if
// the global switch is enabled or not.
func NewCounterForced() Counter {
return new(StandardCounter)
return &StandardCounter{}
}

// NewRegisteredCounter constructs and registers a new StandardCounter.
Expand All @@ -73,40 +70,75 @@ func NewRegisteredCounterForced(name string, r Registry) Counter {
return c
}

// counterSnapshot is a read-only copy of another Counter.
type counterSnapshot int64
// CounterSnapshot is a read-only copy of another Counter.
type CounterSnapshot int64

// Clear panics.
func (CounterSnapshot) Clear() {
panic("Clear called on a CounterSnapshot")
}

// Count returns the count at the time the snapshot was taken.
func (c counterSnapshot) Count() int64 { return int64(c) }
func (c CounterSnapshot) Count() int64 { return int64(c) }

// Dec panics.
func (CounterSnapshot) Dec(int64) {
panic("Dec called on a CounterSnapshot")
}

// Inc panics.
func (CounterSnapshot) Inc(int64) {
panic("Inc called on a CounterSnapshot")
}

// Snapshot returns the snapshot.
func (c CounterSnapshot) Snapshot() Counter { return c }

// NilCounter is a no-op Counter.
type NilCounter struct{}

func (NilCounter) Clear() {}
func (NilCounter) Dec(i int64) {}
func (NilCounter) Inc(i int64) {}
func (NilCounter) Snapshot() CounterSnapshot { return (*emptySnapshot)(nil) }
// Clear is a no-op.
func (NilCounter) Clear() {}

// Count is a no-op.
func (NilCounter) Count() int64 { return 0 }

// Dec is a no-op.
func (NilCounter) Dec(i int64) {}

// Inc is a no-op.
func (NilCounter) Inc(i int64) {}

// Snapshot is a no-op.
func (NilCounter) Snapshot() Counter { return NilCounter{} }

// StandardCounter is the standard implementation of a Counter and uses the
// sync/atomic package to manage a single int64 value.
type StandardCounter atomic.Int64
type StandardCounter struct {
count atomic.Int64
}

// Clear sets the counter to zero.
func (c *StandardCounter) Clear() {
(*atomic.Int64)(c).Store(0)
c.count.Store(0)
}

// Count returns the current count.
func (c *StandardCounter) Count() int64 {
return c.count.Load()
}

// Dec decrements the counter by the given amount.
func (c *StandardCounter) Dec(i int64) {
(*atomic.Int64)(c).Add(-i)
c.count.Add(-i)
}

// Inc increments the counter by the given amount.
func (c *StandardCounter) Inc(i int64) {
(*atomic.Int64)(c).Add(i)
c.count.Add(i)
}

// Snapshot returns a read-only copy of the counter.
func (c *StandardCounter) Snapshot() CounterSnapshot {
return counterSnapshot((*atomic.Int64)(c).Load())
func (c *StandardCounter) Snapshot() Counter {
return CounterSnapshot(c.Count())
}
61 changes: 45 additions & 16 deletions metrics/counter_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"sync/atomic"
)

type CounterFloat64Snapshot interface {
Count() float64
}

// CounterFloat64 holds a float64 value that can be incremented and decremented.
type CounterFloat64 interface {
Clear()
Count() float64
Dec(float64)
Inc(float64)
Snapshot() CounterFloat64Snapshot
Snapshot() CounterFloat64
}

// GetOrRegisterCounterFloat64 returns an existing CounterFloat64 or constructs and registers
Expand Down Expand Up @@ -74,19 +71,47 @@ func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 {
return c
}

// counterFloat64Snapshot is a read-only copy of another CounterFloat64.
type counterFloat64Snapshot float64
// CounterFloat64Snapshot is a read-only copy of another CounterFloat64.
type CounterFloat64Snapshot float64

// Clear panics.
func (CounterFloat64Snapshot) Clear() {
panic("Clear called on a CounterFloat64Snapshot")
}

// Count returns the value at the time the snapshot was taken.
func (c counterFloat64Snapshot) Count() float64 { return float64(c) }
func (c CounterFloat64Snapshot) Count() float64 { return float64(c) }

// Dec panics.
func (CounterFloat64Snapshot) Dec(float64) {
panic("Dec called on a CounterFloat64Snapshot")
}

// Inc panics.
func (CounterFloat64Snapshot) Inc(float64) {
panic("Inc called on a CounterFloat64Snapshot")
}

// Snapshot returns the snapshot.
func (c CounterFloat64Snapshot) Snapshot() CounterFloat64 { return c }

// NilCounterFloat64 is a no-op CounterFloat64.
type NilCounterFloat64 struct{}

func (NilCounterFloat64) Clear() {}
func (NilCounterFloat64) Count() float64 { return 0.0 }
func (NilCounterFloat64) Dec(i float64) {}
func (NilCounterFloat64) Inc(i float64) {}
func (NilCounterFloat64) Snapshot() CounterFloat64Snapshot { return NilCounterFloat64{} }
// Clear is a no-op.
func (NilCounterFloat64) Clear() {}

// Count is a no-op.
func (NilCounterFloat64) Count() float64 { return 0.0 }

// Dec is a no-op.
func (NilCounterFloat64) Dec(i float64) {}

// Inc is a no-op.
func (NilCounterFloat64) Inc(i float64) {}

// Snapshot is a no-op.
func (NilCounterFloat64) Snapshot() CounterFloat64 { return NilCounterFloat64{} }

// StandardCounterFloat64 is the standard implementation of a CounterFloat64 and uses the
// atomic to manage a single float64 value.
Expand All @@ -99,6 +124,11 @@ func (c *StandardCounterFloat64) Clear() {
c.floatBits.Store(0)
}

// Count returns the current value.
func (c *StandardCounterFloat64) Count() float64 {
return math.Float64frombits(c.floatBits.Load())
}

// Dec decrements the counter by the given amount.
func (c *StandardCounterFloat64) Dec(v float64) {
atomicAddFloat(&c.floatBits, -v)
Expand All @@ -110,9 +140,8 @@ func (c *StandardCounterFloat64) Inc(v float64) {
}

// Snapshot returns a read-only copy of the counter.
func (c *StandardCounterFloat64) Snapshot() CounterFloat64Snapshot {
v := math.Float64frombits(c.floatBits.Load())
return counterFloat64Snapshot(v)
func (c *StandardCounterFloat64) Snapshot() CounterFloat64 {
return CounterFloat64Snapshot(c.Count())
}

func atomicAddFloat(fbits *atomic.Uint64, v float64) {
Expand Down
16 changes: 8 additions & 8 deletions metrics/counter_float_64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func BenchmarkCounterFloat64Parallel(b *testing.B) {
}()
}
wg.Wait()
if have, want := c.Snapshot().Count(), 10.0*float64(b.N); have != want {
if have, want := c.Count(), 10.0*float64(b.N); have != want {
b.Fatalf("have %f want %f", have, want)
}
}
Expand All @@ -36,39 +36,39 @@ func TestCounterFloat64Clear(t *testing.T) {
c := NewCounterFloat64()
c.Inc(1.0)
c.Clear()
if count := c.Snapshot().Count(); count != 0 {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestCounterFloat64Dec1(t *testing.T) {
c := NewCounterFloat64()
c.Dec(1.0)
if count := c.Snapshot().Count(); count != -1.0 {
if count := c.Count(); count != -1.0 {
t.Errorf("c.Count(): -1.0 != %v\n", count)
}
}

func TestCounterFloat64Dec2(t *testing.T) {
c := NewCounterFloat64()
c.Dec(2.0)
if count := c.Snapshot().Count(); count != -2.0 {
if count := c.Count(); count != -2.0 {
t.Errorf("c.Count(): -2.0 != %v\n", count)
}
}

func TestCounterFloat64Inc1(t *testing.T) {
c := NewCounterFloat64()
c.Inc(1.0)
if count := c.Snapshot().Count(); count != 1.0 {
if count := c.Count(); count != 1.0 {
t.Errorf("c.Count(): 1.0 != %v\n", count)
}
}

func TestCounterFloat64Inc2(t *testing.T) {
c := NewCounterFloat64()
c.Inc(2.0)
if count := c.Snapshot().Count(); count != 2.0 {
if count := c.Count(); count != 2.0 {
t.Errorf("c.Count(): 2.0 != %v\n", count)
}
}
Expand All @@ -85,15 +85,15 @@ func TestCounterFloat64Snapshot(t *testing.T) {

func TestCounterFloat64Zero(t *testing.T) {
c := NewCounterFloat64()
if count := c.Snapshot().Count(); count != 0 {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestGetOrRegisterCounterFloat64(t *testing.T) {
r := NewRegistry()
NewRegisteredCounterFloat64("foo", r).Inc(47.0)
if c := GetOrRegisterCounterFloat64("foo", r).Snapshot(); c.Count() != 47.0 {
if c := GetOrRegisterCounterFloat64("foo", r); c.Count() != 47.0 {
t.Fatal(c)
}
}
14 changes: 7 additions & 7 deletions metrics/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@ func TestCounterClear(t *testing.T) {
c := NewCounter()
c.Inc(1)
c.Clear()
if count := c.Snapshot().Count(); count != 0 {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestCounterDec1(t *testing.T) {
c := NewCounter()
c.Dec(1)
if count := c.Snapshot().Count(); count != -1 {
if count := c.Count(); count != -1 {
t.Errorf("c.Count(): -1 != %v\n", count)
}
}

func TestCounterDec2(t *testing.T) {
c := NewCounter()
c.Dec(2)
if count := c.Snapshot().Count(); count != -2 {
if count := c.Count(); count != -2 {
t.Errorf("c.Count(): -2 != %v\n", count)
}
}

func TestCounterInc1(t *testing.T) {
c := NewCounter()
c.Inc(1)
if count := c.Snapshot().Count(); count != 1 {
if count := c.Count(); count != 1 {
t.Errorf("c.Count(): 1 != %v\n", count)
}
}

func TestCounterInc2(t *testing.T) {
c := NewCounter()
c.Inc(2)
if count := c.Snapshot().Count(); count != 2 {
if count := c.Count(); count != 2 {
t.Errorf("c.Count(): 2 != %v\n", count)
}
}
Expand All @@ -63,15 +63,15 @@ func TestCounterSnapshot(t *testing.T) {

func TestCounterZero(t *testing.T) {
c := NewCounter()
if count := c.Snapshot().Count(); count != 0 {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestGetOrRegisterCounter(t *testing.T) {
r := NewRegistry()
NewRegisteredCounter("foo", r).Inc(47)
if c := GetOrRegisterCounter("foo", r).Snapshot(); c.Count() != 47 {
if c := GetOrRegisterCounter("foo", r); c.Count() != 47 {
t.Fatal(c)
}
}
4 changes: 4 additions & 0 deletions metrics/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package metrics

const epsilon = 0.0000000000000001
const epsilonPercentile = .00000000001
Loading

0 comments on commit 12b9cd2

Please sign in to comment.