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

maglev: Parallelize calculation of permutations #14597

Merged
merged 2 commits into from
Jan 14, 2021
Merged

Conversation

brb
Copy link
Member

@brb brb commented Jan 13, 2021

Create a number of goroutines to calculate Maglev backend permutatons.

We choose the number to be equal to the number of CPUs available, as the
calculation doesn't block and is completely CPU-bound. So, adding more
goroutines would introduce an overhead instead of an improvement.

The optimization gives ~5x improvement in time:

BEFORE OPTIMIZATION
> go test -v -check.v -check.b -check.btime 5s -check.bmem
[..]
MaglevTestSuite.BenchmarkGetMaglevTable 5 1352437020 ns/op 1049633299 B/op 9 allocs/op

AFTER OPTIMIZATION
> go test -v -check.v -check.b -check.btime 5s -check.bmem
[..]
MaglevTestSuite.BenchmarkGetMaglevTable 50 271517785 ns/op 1049633901 B/op 12 allocs/op

Signed-off-by: Chris Tarazi [email protected]
Signed-off-by: Martynas Pumputis [email protected]

Related: #14397.

Signed-off-by: Martynas Pumputis <[email protected]>
@brb brb added kind/performance There is a performance impact of this. sig/loadbalancing labels Jan 13, 2021
@brb brb requested a review from a team as a code owner January 13, 2021 07:34
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 13, 2021
@brb brb requested a review from christarazi January 13, 2021 07:35
@brb brb added the release-note/minor This PR changes functionality that users may find relevant to operating Cilium. label Jan 13, 2021
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 13, 2021
@brb brb force-pushed the pr/brb/maglev-perf branch from 3a74462 to cc90307 Compare January 13, 2021 07:38
Comment on lines +77 to +98
go func(from int) {
to := from + batchSize
if to > bCount {
to = bCount
}
for i := from; i < to; i++ {
offset, skip := getOffsetAndSkip(backends[i], m)
perm[i*int(m)] = offset % m
for j := uint64(1); j < m; j++ {
perm[i*int(m)+int(j)] = (perm[i*int(m)+int(j-1)] + skip) % m
}

}
wg.Done()
}(g)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in an offline discussion, using a semaphore here would constrain the number of goroutines as well. However, there's a mention of wanting to avoid creating a goroutine per backend, which can be expensive given a large number of backends. This would presumably put pressure on the Golang scheduler to schedule a potentially large number of goroutines, even if short lived, as well as taking up a much larger footprint of memory.

From my quick reading of the code, this seems like it's splitting the "work" into chunks and having a goroutine process the chunk. I think this is a preferable approach as it would only create up to X goroutines, rather than len(backends).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optimization gives ~5x improvement in time:

Nice! Did you also assess the overhead / time for smaller number of backends and/or M? Mainly wondering if the heuristic should be adapted to only kick in when the overhead of spawning extra go routines brings a measurable win ... otoh I don't think people would configure very small M.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With smaller Ms I got 4x.

@brb
Copy link
Member Author

brb commented Jan 13, 2021

test-me-please

@brb
Copy link
Member Author

brb commented Jan 13, 2021

retest-netnext

@brb
Copy link
Member Author

brb commented Jan 13, 2021

retest-net-next

pkg/maglev/maglev.go Show resolved Hide resolved
@brb
Copy link
Member Author

brb commented Jan 14, 2021

CI net-next hit #12511.

@brb
Copy link
Member Author

brb commented Jan 14, 2021

retest-net-next

@brb
Copy link
Member Author

brb commented Jan 14, 2021

retest-gke

Create a number of goroutines to calculate Maglev backend permutatons.

We choose the number to be equal to the number of CPUs available, as the
calculation doesn't block and is completely CPU-bound. So, adding more
goroutines would introduce an overhead instead of an improvement.

The optimization gives ~5x improvement in time:

 BEFORE OPTIMIZATION
> go test -v -check.v -check.b -check.btime 5s -check.bmem
[..]
MaglevTestSuite.BenchmarkGetMaglevTable 5 1352437020 ns/op 1049633299 B/op 9 allocs/op

AFTER OPTIMIZATION
> go test -v -check.v -check.b -check.btime 5s -check.bmem
[..]
MaglevTestSuite.BenchmarkGetMaglevTable 50 271517785 ns/op 1049633901 B/op 12 allocs/op

Signed-off-by: Chris Tarazi <[email protected]>
Signed-off-by: Martynas Pumputis <[email protected]>
@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Jan 14, 2021
@brb
Copy link
Member Author

brb commented Jan 14, 2021

All checks have passed. Pushed the comment.

@brb brb force-pushed the pr/brb/maglev-perf branch from cc90307 to 36860ae Compare January 14, 2021 08:15
@brb brb requested review from a team and aditighag January 14, 2021 08:15
@gandro gandro merged commit 72b755d into master Jan 14, 2021
@gandro gandro deleted the pr/brb/maglev-perf branch January 14, 2021 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/performance There is a performance impact of this. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/minor This PR changes functionality that users may find relevant to operating Cilium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants