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

improvement(gossip,tar): Work towards improving thread management #145

Merged
merged 7 commits into from
Jun 3, 2024

Conversation

InKryption
Copy link
Contributor

Starts work towards #119 & #120.
More details in the commits.

@0xNineteen 0xNineteen added this to the Gossip milestone May 27, 2024
src/accountsdb/snapshots.zig Outdated Show resolved Hide resolved
src/gossip/service.zig Outdated Show resolved Hide resolved
src/gossip/service.zig Outdated Show resolved Hide resolved
src/gossip/service.zig Show resolved Hide resolved
src/net/socket_utils.zig Outdated Show resolved Hide resolved
src/gossip/service.zig Outdated Show resolved Hide resolved
src/gossip/service.zig Show resolved Hide resolved
src/utils/tar.zig Outdated Show resolved Hide resolved
src/utils/tar.zig Outdated Show resolved Hide resolved
* [ThreadPoolTask] Rename `done` field to `avaialable`, for better
  clarity in the contexts it's used.

* [ThreadPoolTask] Add `awaitAndAcquireFirstAvailableTask` &
  `blockUntilCompletion` functions, remove `queue` function,
  replace usages of `queue` and code which would now duplicate
  the aforementioned additions.

* [ThreadPoolTask] Add `result` field for signalling the result
  of the callback.

* [gossip] Make use of `ThreadPoolTask` instead of manually implementing
  the VerifyMessageTask Task interface details.

* [gossip] Add `GOSSIP_VERIFY_PACKET_PARALLEL_TASKS` constant, and use
  it instead of hardcoding the tasks allocated in `verifyPackets`.

* [gossip] Make `verifyPackets` and `processMessages` accept an
  a shared allocator, and make `run` provide a thread-safe allocator
  which will only be contended by a maximum of the avilable tasks
  plus the number of messages processed at the same time.

* [tar] Panic on some invalid but possible invariants.

* Constify more things that don't need to be mutable.
* Rename `SOCKET_TIMEOUT` to `SOCKET_TIMEOUT_US`, to signify its
  magnitude is in microseconds.

* Remove `recvMmsg` and simplify the timeout logic to simply setting
  it directly before the loop, and finishing the batch on the first
  `error.WouldBlock` when the batch isn't empty.

* Use `packet_batch` as an actual arraylist instead of as a weird slice.
Re-implement the logic of `run` as a function that returns the handles
which can then be joined by the caller.
Most usage sites require a bit of thought with respect to the
order of operations, so `GossipService.run` remains as a wrapper
around the new logic.
This also makes it take a `message_allocator`.
@InKryption InKryption marked this pull request as ready for review May 31, 2024 20:53
Copy link
Contributor

@0xNineteen 0xNineteen left a comment

Choose a reason for hiding this comment

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

looking like a really solid pr nice work - just a few small things

src/gossip/service.zig Show resolved Hide resolved
src/gossip/service.zig Show resolved Hide resolved
src/gossip/service.zig Show resolved Hide resolved
src/gossip/service.zig Show resolved Hide resolved
src/gossip/service.zig Outdated Show resolved Hide resolved
src/net/socket_utils.zig Outdated Show resolved Hide resolved
src/net/socket_utils.zig Show resolved Hide resolved
@InKryption
Copy link
Contributor Author

InKryption commented Jun 3, 2024

socket_utils comparative benchmark

I conducted many more runs than presented here, but those presented are representative of the general trends I observed, where performance is either equivalent or minimally better, excluding outliers where variance exceeded more than 100 in either (which occurred twice with the old code, and once with the new code).

Side note: brief testing indicated that ReleaseFast demonstrates near identical trends.

Before socket_utils changes:

❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils  
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1739       1755         43       1747



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1737       1755         56       1745



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1743       1751         12       1747



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1739       1742          2       1740



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1740       1747          8       1743

After socket_utils changes:

❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1719       1735         44       1727



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1729       1734          4       1732



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1730       1733          1       1731



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1728       1738         17       1732



❯ zig build -Doptimize=ReleaseSafe benchmark -- socket_utils
filtering benchmarks with prefix: socket_utils

Benchmark                      Iterations    Min(ms)    Max(ms)   Variance   Mean(ms)
-------------------------------------------------------------------------------------
benchmarkReadSocket(100k_msgs)          3       1721       1738         51       1728

Copy link
Contributor

@0xNineteen 0xNineteen left a comment

Choose a reason for hiding this comment

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

lgtm

@InKryption InKryption merged commit 6e0ecb7 into main Jun 3, 2024
2 checks passed
@InKryption InKryption deleted the ink/better-threads1 branch June 3, 2024 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants