Skip to content

Commit 3d21d87

Browse files
authored
Revert "metrics: use atomic type (ethereum#27121)"
This reverts commit 9f88fe2.
1 parent 2199b60 commit 3d21d87

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

metrics/counter.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ func NewCounter() Counter {
3838
if !Enabled {
3939
return NilCounter{}
4040
}
41-
return &StandardCounter{}
41+
return &StandardCounter{0}
4242
}
4343

4444
// NewCounterForced constructs a new StandardCounter and returns it no matter if
4545
// the global switch is enabled or not.
4646
func NewCounterForced() Counter {
47-
return &StandardCounter{}
47+
return &StandardCounter{0}
4848
}
4949

5050
// NewRegisteredCounter constructs and registers a new StandardCounter.
@@ -115,27 +115,27 @@ func (NilCounter) Snapshot() Counter { return NilCounter{} }
115115
// StandardCounter is the standard implementation of a Counter and uses the
116116
// sync/atomic package to manage a single int64 value.
117117
type StandardCounter struct {
118-
count atomic.Int64
118+
count int64
119119
}
120120

121121
// Clear sets the counter to zero.
122122
func (c *StandardCounter) Clear() {
123-
c.count.Store(0)
123+
atomic.StoreInt64(&c.count, 0)
124124
}
125125

126126
// Count returns the current count.
127127
func (c *StandardCounter) Count() int64 {
128-
return c.count.Load()
128+
return atomic.LoadInt64(&c.count)
129129
}
130130

131131
// Dec decrements the counter by the given amount.
132132
func (c *StandardCounter) Dec(i int64) {
133-
c.count.Add(-i)
133+
atomic.AddInt64(&c.count, -i)
134134
}
135135

136136
// Inc increments the counter by the given amount.
137137
func (c *StandardCounter) Inc(i int64) {
138-
c.count.Add(i)
138+
atomic.AddInt64(&c.count, i)
139139
}
140140

141141
// Snapshot returns a read-only copy of the counter.

metrics/ewma.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (NilEWMA) Update(n int64) {}
7575
// of uncounted events and processes them on each tick. It uses the
7676
// sync/atomic package to manage uncounted events.
7777
type StandardEWMA struct {
78-
uncounted atomic.Int64
78+
uncounted int64 // /!\ this should be the first member to ensure 64-bit alignment
7979
alpha float64
8080
rate float64
8181
init bool
@@ -97,8 +97,8 @@ func (a *StandardEWMA) Snapshot() EWMA {
9797
// Tick ticks the clock to update the moving average. It assumes it is called
9898
// every five seconds.
9999
func (a *StandardEWMA) Tick() {
100-
count := a.uncounted.Load()
101-
a.uncounted.Add(-count)
100+
count := atomic.LoadInt64(&a.uncounted)
101+
atomic.AddInt64(&a.uncounted, -count)
102102
instantRate := float64(count) / float64(5*time.Second)
103103
a.mutex.Lock()
104104
defer a.mutex.Unlock()
@@ -112,5 +112,5 @@ func (a *StandardEWMA) Tick() {
112112

113113
// Update adds n uncounted events.
114114
func (a *StandardEWMA) Update(n int64) {
115-
a.uncounted.Add(n)
115+
atomic.AddInt64(&a.uncounted, n)
116116
}

metrics/gauge.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewGauge() Gauge {
2525
if !Enabled {
2626
return NilGauge{}
2727
}
28-
return &StandardGauge{}
28+
return &StandardGauge{0}
2929
}
3030

3131
// NewRegisteredGauge constructs and registers a new StandardGauge.
@@ -101,7 +101,7 @@ func (NilGauge) Value() int64 { return 0 }
101101
// StandardGauge is the standard implementation of a Gauge and uses the
102102
// sync/atomic package to manage a single int64 value.
103103
type StandardGauge struct {
104-
value atomic.Int64
104+
value int64
105105
}
106106

107107
// Snapshot returns a read-only copy of the gauge.
@@ -111,22 +111,22 @@ func (g *StandardGauge) Snapshot() Gauge {
111111

112112
// Update updates the gauge's value.
113113
func (g *StandardGauge) Update(v int64) {
114-
g.value.Store(v)
114+
atomic.StoreInt64(&g.value, v)
115115
}
116116

117117
// Value returns the gauge's current value.
118118
func (g *StandardGauge) Value() int64 {
119-
return g.value.Load()
119+
return atomic.LoadInt64(&g.value)
120120
}
121121

122122
// Dec decrements the gauge's current value by the given amount.
123123
func (g *StandardGauge) Dec(i int64) {
124-
g.value.Add(-i)
124+
atomic.AddInt64(&g.value, -i)
125125
}
126126

127127
// Inc increments the gauge's current value by the given amount.
128128
func (g *StandardGauge) Inc(i int64) {
129-
g.value.Add(i)
129+
atomic.AddInt64(&g.value, i)
130130
}
131131

132132
// FunctionalGauge returns value from given function

metrics/meter.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ func NewRegisteredMeterForced(name string, r Registry) Meter {
101101

102102
// MeterSnapshot is a read-only copy of another Meter.
103103
type MeterSnapshot struct {
104-
temp atomic.Int64
104+
// WARNING: The `temp` field is accessed atomically.
105+
// On 32 bit platforms, only 64-bit aligned fields can be atomic. The struct is
106+
// guaranteed to be so aligned, so take advantage of that. For more information,
107+
// see https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
108+
temp int64
105109
count int64
106110
rate1, rate5, rate15, rateMean float64
107111
}
@@ -169,7 +173,7 @@ type StandardMeter struct {
169173
snapshot *MeterSnapshot
170174
a1, a5, a15 EWMA
171175
startTime time.Time
172-
stopped atomic.Bool
176+
stopped uint32
173177
}
174178

175179
func newStandardMeter() *StandardMeter {
@@ -184,8 +188,8 @@ func newStandardMeter() *StandardMeter {
184188

185189
// Stop stops the meter, Mark() will be a no-op if you use it after being stopped.
186190
func (m *StandardMeter) Stop() {
187-
stopped := m.stopped.Swap(true)
188-
if !stopped {
191+
stopped := atomic.SwapUint32(&m.stopped, 1)
192+
if stopped != 1 {
189193
arbiter.Lock()
190194
delete(arbiter.meters, m)
191195
arbiter.Unlock()
@@ -203,7 +207,7 @@ func (m *StandardMeter) Count() int64 {
203207

204208
// Mark records the occurrence of n events.
205209
func (m *StandardMeter) Mark(n int64) {
206-
m.snapshot.temp.Add(n)
210+
atomic.AddInt64(&m.snapshot.temp, n)
207211
}
208212

209213
// Rate1 returns the one-minute moving average rate of events per second.
@@ -237,14 +241,7 @@ func (m *StandardMeter) RateMean() float64 {
237241
// Snapshot returns a read-only copy of the meter.
238242
func (m *StandardMeter) Snapshot() Meter {
239243
m.lock.RLock()
240-
snapshot := MeterSnapshot{
241-
count: m.snapshot.count,
242-
rate1: m.snapshot.rate1,
243-
rate5: m.snapshot.rate5,
244-
rate15: m.snapshot.rate15,
245-
rateMean: m.snapshot.rateMean,
246-
}
247-
snapshot.temp.Store(m.snapshot.temp.Load())
244+
snapshot := *m.snapshot
248245
m.lock.RUnlock()
249246
return &snapshot
250247
}
@@ -260,7 +257,7 @@ func (m *StandardMeter) updateSnapshot() {
260257

261258
func (m *StandardMeter) updateMeter() {
262259
// should only run with write lock held on m.lock
263-
n := m.snapshot.temp.Swap(0)
260+
n := atomic.SwapInt64(&m.snapshot.temp, 0)
264261
m.snapshot.count += n
265262
m.a1.Update(n)
266263
m.a5.Update(n)

0 commit comments

Comments
 (0)