Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: zero temp variable in updateMeter #21470

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions metrics/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ func NewRegisteredMeterForced(name string, r Registry) Meter {

// MeterSnapshot is a read-only copy of another Meter.
type MeterSnapshot struct {
count int64
// WARNING: The `temp` field is accessed atomically.
// On 32 bit platforms, only 64-bit aligned fields can be atomic. The struct is
// guaranteed to be so aligned, so take advantage of that. For more information,
// see https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
temp int64
count int64
rate1, rate5, rate15, rateMean float64
}

Expand Down Expand Up @@ -253,7 +257,7 @@ func (m *StandardMeter) updateSnapshot() {

func (m *StandardMeter) updateMeter() {
// should only run with write lock held on m.lock
n := atomic.LoadInt64(&m.snapshot.temp)
n := atomic.SwapInt64(&m.snapshot.temp, 0)
m.snapshot.count += n
m.a1.Update(n)
m.a5.Update(n)
Expand Down
16 changes: 16 additions & 0 deletions metrics/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ func TestMeterZero(t *testing.T) {
t.Errorf("m.Count(): 0 != %v\n", count)
}
}

func TestMeterRepeat(t *testing.T) {
m := NewMeter()
for i := 0; i < 101; i++ {
m.Mark(int64(i))
}
if count := m.Count(); count != 5050 {
t.Errorf("m.Count(): 5050 != %v\n", count)
}
for i := 0; i < 101; i++ {
m.Mark(int64(i))
}
if count := m.Count(); count != 10100 {
t.Errorf("m.Count(): 10100 != %v\n", count)
}
}