Skip to content

Commit 77bf6ef

Browse files
s7v7nislandsmmsqe
authored andcommittedDec 7, 2023
metrics: use atomic type (ethereum#27121)
1 parent 4ebf734 commit 77bf6ef

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed
 

‎metrics/counter.go

+7-7
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{0}
41+
return &StandardCounter{}
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{0}
47+
return &StandardCounter{}
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 int64
118+
count atomic.Int64
119119
}
120120

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

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

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

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

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

‎metrics/ewma.go

+4-4
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 int64 // /!\ this should be the first member to ensure 64-bit alignment
78+
uncounted atomic.Int64
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 := atomic.LoadInt64(&a.uncounted)
101-
atomic.AddInt64(&a.uncounted, -count)
100+
count := a.uncounted.Load()
101+
a.uncounted.Add(-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-
atomic.AddInt64(&a.uncounted, n)
115+
a.uncounted.Add(n)
116116
}

‎metrics/gauge.go

+6-6
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{0}
28+
return &StandardGauge{}
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 int64
104+
value atomic.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-
atomic.StoreInt64(&g.value, v)
114+
g.value.Store(v)
115115
}
116116

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

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

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

132132
// FunctionalGauge returns value from given function

‎metrics/meter.go

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

102102
// MeterSnapshot is a read-only copy of another Meter.
103103
type MeterSnapshot struct {
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
104+
temp atomic.Int64
109105
count int64
110106
rate1, rate5, rate15, rateMean float64
111107
}
@@ -173,7 +169,7 @@ type StandardMeter struct {
173169
snapshot *MeterSnapshot
174170
a1, a5, a15 EWMA
175171
startTime time.Time
176-
stopped uint32
172+
stopped atomic.Bool
177173
}
178174

179175
func newStandardMeter() *StandardMeter {
@@ -188,8 +184,8 @@ func newStandardMeter() *StandardMeter {
188184

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

208204
// Mark records the occurrence of n events.
209205
func (m *StandardMeter) Mark(n int64) {
210-
atomic.AddInt64(&m.snapshot.temp, n)
206+
m.snapshot.temp.Add(n)
211207
}
212208

213209
// Rate1 returns the one-minute moving average rate of events per second.
@@ -241,7 +237,14 @@ func (m *StandardMeter) RateMean() float64 {
241237
// Snapshot returns a read-only copy of the meter.
242238
func (m *StandardMeter) Snapshot() Meter {
243239
m.lock.RLock()
244-
snapshot := *m.snapshot
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())
245248
m.lock.RUnlock()
246249
return &snapshot
247250
}
@@ -257,7 +260,7 @@ func (m *StandardMeter) updateSnapshot() {
257260

258261
func (m *StandardMeter) updateMeter() {
259262
// should only run with write lock held on m.lock
260-
n := atomic.SwapInt64(&m.snapshot.temp, 0)
263+
n := m.snapshot.temp.Swap(0)
261264
m.snapshot.count += n
262265
m.a1.Update(n)
263266
m.a5.Update(n)

0 commit comments

Comments
 (0)
Please sign in to comment.