-
Notifications
You must be signed in to change notification settings - Fork 12
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
Enable customization of metrics calculation #26
Open
ArtemVasilevMIPT
wants to merge
12
commits into
libp2p:master
Choose a base branch
from
ArtemVasilevMIPT:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bb21ae9
Initial changes
978bf57
Added interfaces
5659dc2
Fixed interfaces
1070bec
Tests passed
2feb4eb
Refactored sweeper
cc7dc35
Removed debug comments
d148d36
Merge branch 'master' of github.com:libp2p/go-flow-metrics
b590479
Refactored float comparison
617790b
Modified .gitignore, go.mod and mock_clock_test.go
abc0ba3
Simplified interface, added time argument to Update()
2ba877f
Added compatibility with libp2p
f6487e6
Returned 'Mark' method
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,46 @@ package flow | |
|
||
import ( | ||
"fmt" | ||
"math" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
// IdleRate the rate at which we declare a meter idle (and stop tracking it | ||
// until it's re-registered). | ||
// | ||
// The default ensures that 1 event every ~30s will keep the meter from going | ||
// idle. | ||
var IdleRate = 1e-13 | ||
|
||
// Alpha for EWMA of 1s | ||
var alpha = 1 - math.Exp(-1.0) | ||
|
||
// Snapshot is a rate/total snapshot. | ||
type Snapshot struct { | ||
Rate float64 | ||
Total uint64 | ||
LastUpdate time.Time | ||
} | ||
|
||
type MeterInterface interface { | ||
String() string | ||
Mark(count uint64) | ||
Snapshot() Snapshot | ||
Reset() | ||
Update(tdiff time.Duration) | ||
IsIdle() bool | ||
SetIdle() | ||
SetActive() | ||
} | ||
|
||
// NewMeter returns a new Meter with the correct idle time. | ||
// | ||
// While zero-value Meters can be used, their "last update" time will start at | ||
// the program start instead of when the meter was created. | ||
func NewMeter() *Meter { | ||
func NewMeter() MeterInterface { | ||
return &Meter{ | ||
fresh: true, | ||
snapshot: Snapshot{ | ||
LastUpdate: cl.Now(), | ||
}, | ||
|
@@ -38,6 +61,8 @@ type Meter struct { | |
|
||
// Take lock. | ||
snapshot Snapshot | ||
|
||
fresh bool | ||
} | ||
|
||
// Mark updates the total. | ||
|
@@ -69,3 +94,85 @@ func (m *Meter) Reset() { | |
func (m *Meter) String() string { | ||
return m.Snapshot().String() | ||
} | ||
|
||
func (m *Meter) Update(tdiff time.Duration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please pass |
||
if !m.fresh { | ||
timeMultiplier := float64(time.Second) / float64(tdiff) | ||
total := m.accumulator.Load() | ||
diff := total - m.snapshot.Total | ||
instant := timeMultiplier * float64(diff) | ||
|
||
if diff > 0 { | ||
m.snapshot.LastUpdate = cl.Now() | ||
} | ||
|
||
if m.snapshot.Rate == 0 { | ||
m.snapshot.Rate = instant | ||
} else { | ||
m.snapshot.Rate += alpha * (instant - m.snapshot.Rate) | ||
} | ||
m.snapshot.Total = total | ||
|
||
// This is equivalent to one zeros, then one, then 30 zeros. | ||
// We'll consider that to be "idle". | ||
if m.snapshot.Rate > IdleRate { | ||
return | ||
} | ||
|
||
// Ok, so we are idle... | ||
// Mark this as idle by zeroing the accumulator. | ||
swappedTotal := m.accumulator.Swap(0) | ||
// So..., are we really idle? | ||
if swappedTotal > total { | ||
// Not so idle... | ||
// Now we need to make sure this gets re-registered. | ||
|
||
// First, add back what we removed. If we can do this | ||
// fast enough, we can put it back before anyone | ||
// notices. | ||
currentTotal := m.accumulator.Add(swappedTotal) | ||
|
||
// Did we make it? | ||
if currentTotal == swappedTotal { | ||
// Yes! Nobody noticed, move along. | ||
return | ||
} | ||
// No. Someone noticed and will (or has) put back into | ||
// the registration channel. | ||
// | ||
// Remove the snapshot total, it'll get added back on | ||
// registration. | ||
// | ||
// `^uint64(total - 1)` is the two's complement of | ||
// `total`. It's the "correct" way to subtract | ||
// atomically in go. | ||
m.accumulator.Add(^uint64(m.snapshot.Total - 1)) | ||
} else { | ||
m.SetIdle() | ||
} | ||
} else { | ||
// Re-add the total to all the newly active accumulators and set the snapshot to the total. | ||
// 1. We don't do this on register to avoid having to take the snapshot lock. | ||
// 2. We skip calculating the bandwidth for this round so we get an _accurate_ bandwidth calculation. | ||
total := m.accumulator.Add(m.snapshot.Total) | ||
if total > m.snapshot.Total { | ||
m.snapshot.LastUpdate = cl.Now() | ||
} | ||
m.snapshot.Total = total | ||
m.fresh = false | ||
} | ||
} | ||
|
||
func (m *Meter) IsIdle() bool { | ||
return !m.registered | ||
} | ||
|
||
func (m *Meter) SetIdle() { | ||
m.snapshot.Rate = 0 | ||
m.registered = false | ||
m.fresh = true | ||
} | ||
|
||
func (m *Meter) SetActive() { | ||
m.registered = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mockclocktest | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
flow "github.com/libp2p/go-flow-metrics" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
var cl = clock.NewMock() | ||
|
||
func init() { | ||
flow.SetClock(cl) | ||
} | ||
|
||
func TestBasic(t *testing.T) { | ||
m := new(flow.Meter) | ||
for i := 0; i < 300; i++ { | ||
m.Mark(1000) | ||
cl.Add(40 * time.Millisecond) | ||
} | ||
if rate := m.Snapshot().Rate; approxEq(rate, 25000, 1) { | ||
t.Errorf("expected rate 25000, got %f", rate) | ||
} | ||
|
||
for i := 0; i < 200; i++ { | ||
m.Mark(200) | ||
cl.Add(40 * time.Millisecond) | ||
} | ||
|
||
// Adjusts | ||
if rate := m.Snapshot().Rate; approxEq(rate, 5017.776503840969, 0.0001) { | ||
t.Errorf("expected rate 5017.776503840969, got %f", rate) | ||
} | ||
|
||
// Let it settle. | ||
cl.Add(2 * time.Second) | ||
if total := m.Snapshot().Total; total != 340000 { | ||
t.Errorf("expected total 3400000, got %d", total) | ||
} | ||
} | ||
|
||
func approxEq(a, b, err float64) bool { | ||
return math.Abs(a-b) < err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd drop all of these from this interface and focus on what the sweeper requires. Different meter implementations will need different interfaces (e.g., different data in their snapshots).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't drop
Snapshot()
because it's used inMeterRegistry.walkIdle()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran some tests and discovered that my changes made the library incompatible with the current version of libp2p. Turns out that
BandwidthCounter
from libp2p uses methodMeterRegistry.ForEach()
. In my version this method acceptsfunc(string, MeterInterface)
but originally it acceptsfunc(string, *Meter)
. I plan to explicitly specify the right version ofMeterRegistry.ForEach()
for compatibility with libp2p. Do you have better suggestions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Stebalien Gentle reminder, can you, please, check my last commit and say if it's fine with you?