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

Reduce TX/RX buffers allocation #1749

Merged
merged 7 commits into from
Feb 3, 2025
Merged

Reduce TX/RX buffers allocation #1749

merged 7 commits into from
Feb 3, 2025

Conversation

Mallets
Copy link
Member

@Mallets Mallets commented Jan 30, 2025

This PR:

  1. Lazily allocates the TX batches upon link creation;
  2. Adopts a less-aggressive policy for allocating excessive memory for RX buffers.

This allows to reduce the number of allocated batches that are not utilised.
In general it should improve the overall memory consumption when many links are established.

Validation

Target

AMD Ryzen 7 5800X 8-Core Processor
32GB of RAM

Config

Queue configuration (queue.conf.json5)

{
  transport: {
    link: {
      tx: {
        queue: {
          size: {
            control: 1,
            real_time: 1,
            interactive_high: 1,
            interactive_low: 1,
            data_high: 2,
            data: 4,
            data_low: 4,
            background: 4,
          },
          allocation: {
	    // mode: "init",
            mode: "lazy",
          },
        },
      },
    },
  },
}

Test

Run zenohd:

./target/release/zenohd --no-multicast-scouting -l tcp/localhost:7447 -c queue.conf.json5

Run 100 z_sub:

for i in `seq 1 100`; do (./target/release/examples/z_sub --no-multicast-scouting -l tcp/localhost:0 -e tcp/localhost:7447 -c queue.conf.json5 &); done

Run 100 z_pub:

for i in `seq 1 100`; do (./target/release/examples/z_pub --no-multicast-scouting -l tcp/localhost:0 -e tcp/localhost:7447 -c queue.conf.json5 &); done

Results

Baseline memory usage: 3.45G.
Memory mode:

  • Init: 14.9G/31.3G11.45G in total (56.97M per process)
  • Lazy: 10.2G/31.3G6.75G in total (33.58M per process)

In case of z_sub / z_pub the gain is 4.7G less memory.

Copy link

PR missing one of the required labels: {'new feature', 'documentation', 'enhancement', 'bug', 'dependencies', 'internal', 'breaking-change'}

@Mallets Mallets added internal Changes not included in the changelog enhancement Existing things could work better labels Jan 30, 2025
@Mallets Mallets marked this pull request as ready for review January 31, 2025 10:01
@Mallets Mallets requested a review from wyfo January 31, 2025 16:35
@Mallets Mallets changed the title Lazy allocation of batches in transmission pipeline Reduce TX/RX buffers allocation Feb 3, 2025
@wyfo
Copy link
Contributor

wyfo commented Feb 3, 2025

In pipeline.rs of main branch, if we insert the following line between lines 268 and 269:

None => break WBatch::new_ephemeral(self.batch_config),

the throughput is then increased by 1Mmsg/s for 8B messages on my computer (MacBook Air M3). The effect of this line is that each batch is freshly allocated instead of going through the recycling pipeline.
It tells us two things:

  • the current default batch count (4) may not be optimized to reach the best throughput
  • amortized allocations may not be so costly

If we then modify the code to limit the number of concurrent allocated batches to the one of the config, still keeping the allocation on demand behavior, we obtain this time a small throughput reduction, around 100kmsg/s or less, so 1.6% of 6.3Mmsg/s. This should be the cost of allocating on demand, so not very high regarding throughput. On the other hand, it gives a fully elastic memory consumption, that could decrease the total memory footprint.

This allocation on demand behavior could even be customized to start with a smaller batch, for example 1kB, and only increase the size if in case of demanding throughput/big payloads.

@@ -475,6 +475,12 @@
/// The maximum time limit (in ms) a message should be retained for batching when back-pressure happens.
time_limit: 1,
},
allocation: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a result that shows that the first allocation has a sensible effect, which would justify the need of this configuration addition?

@Mallets
Copy link
Member Author

Mallets commented Feb 3, 2025

I agree that 4 may not be ideal for the default number of batches.
I'm also thinking that reducing the default number of batches to 2 with a lazily memory allocation would make the best default. However, I'd prefer leaving changing the default parameters to a separate PR.

Regarding the sensible effects on the first allocation, I didn't observer any.
But for the sake of not introducing changes to the default behaviour, I added the configuration support.

I can do a new PR for changing the defaults on top of this one for better traceability.

@wyfo
Copy link
Contributor

wyfo commented Feb 3, 2025

If there is no sensible effect, I would be against adding more stuff to the configuration.

@Mallets
Copy link
Member Author

Mallets commented Feb 3, 2025

I see your point, but the lack of sensible effect is the effect we measured on our machines.
It doesn't mean no sensible effect will be the norm to every user on every target platform.

@Mallets
Copy link
Member Author

Mallets commented Feb 3, 2025

#1751 changes the default configuration and targets this PR branch.

@wyfo
Copy link
Contributor

wyfo commented Feb 3, 2025

You decide then.

@Mallets Mallets merged commit 6360eb6 into main Feb 3, 2025
26 checks passed
@Mallets Mallets deleted the feat/lazy_batching branch February 3, 2025 11:34
JEnoch added a commit to ZettaScaleLabs/rmw_zenoh that referenced this pull request Feb 4, 2025
Yadunund added a commit to ros2/rmw_zenoh that referenced this pull request Feb 4, 2025
* Bump: zenoh-cpp=bd4d741 zenoh-c=15d56e1 zenoh=e17de41

* Config: add new gossip target setting (eclipse-zenoh/zenoh#1678)

* Config: add new interests timeout setting (eclipse-zenoh/zenoh#1710)

* Config: copy updates from eclipse-zenoh/zenoh#1712

* Config: copy updates from eclipse-zenoh/zenoh#1722

* Config: copy updates from eclipse-zenoh/zenoh#1749

* Config: copy updates from eclipse-zenoh/zenoh#1751

* fix: use the commit 15d56e1 to include the allocation feature

* Bump zenoh-c=5fce7fb zenoh=e4ea6f0 (for eclipse-zenoh/zenoh#1754 fixing a routing issue in 1.2.0)

* Update documentation for commits and undo style changes

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: yuanyuyuan <[email protected]>
Co-authored-by: Yadunund <[email protected]>
mergify bot pushed a commit to ros2/rmw_zenoh that referenced this pull request Feb 4, 2025
* Bump: zenoh-cpp=bd4d741 zenoh-c=15d56e1 zenoh=e17de41

* Config: add new gossip target setting (eclipse-zenoh/zenoh#1678)

* Config: add new interests timeout setting (eclipse-zenoh/zenoh#1710)

* Config: copy updates from eclipse-zenoh/zenoh#1712

* Config: copy updates from eclipse-zenoh/zenoh#1722

* Config: copy updates from eclipse-zenoh/zenoh#1749

* Config: copy updates from eclipse-zenoh/zenoh#1751

* fix: use the commit 15d56e1 to include the allocation feature

* Bump zenoh-c=5fce7fb zenoh=e4ea6f0 (for eclipse-zenoh/zenoh#1754 fixing a routing issue in 1.2.0)

* Update documentation for commits and undo style changes

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: yuanyuyuan <[email protected]>
Co-authored-by: Yadunund <[email protected]>
(cherry picked from commit d322d6f)
mergify bot pushed a commit to ros2/rmw_zenoh that referenced this pull request Feb 4, 2025
* Bump: zenoh-cpp=bd4d741 zenoh-c=15d56e1 zenoh=e17de41

* Config: add new gossip target setting (eclipse-zenoh/zenoh#1678)

* Config: add new interests timeout setting (eclipse-zenoh/zenoh#1710)

* Config: copy updates from eclipse-zenoh/zenoh#1712

* Config: copy updates from eclipse-zenoh/zenoh#1722

* Config: copy updates from eclipse-zenoh/zenoh#1749

* Config: copy updates from eclipse-zenoh/zenoh#1751

* fix: use the commit 15d56e1 to include the allocation feature

* Bump zenoh-c=5fce7fb zenoh=e4ea6f0 (for eclipse-zenoh/zenoh#1754 fixing a routing issue in 1.2.0)

* Update documentation for commits and undo style changes

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: yuanyuyuan <[email protected]>
Co-authored-by: Yadunund <[email protected]>
(cherry picked from commit d322d6f)
Yadunund pushed a commit to ros2/rmw_zenoh that referenced this pull request Feb 4, 2025
* Bump: zenoh-cpp=bd4d741 zenoh-c=15d56e1 zenoh=e17de41

* Config: add new gossip target setting (eclipse-zenoh/zenoh#1678)

* Config: add new interests timeout setting (eclipse-zenoh/zenoh#1710)

* Config: copy updates from eclipse-zenoh/zenoh#1712

* Config: copy updates from eclipse-zenoh/zenoh#1722

* Config: copy updates from eclipse-zenoh/zenoh#1749

* Config: copy updates from eclipse-zenoh/zenoh#1751

* fix: use the commit 15d56e1 to include the allocation feature

* Bump zenoh-c=5fce7fb zenoh=e4ea6f0 (for eclipse-zenoh/zenoh#1754 fixing a routing issue in 1.2.0)

* Update documentation for commits and undo style changes

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: yuanyuyuan <[email protected]>
Co-authored-by: Yadunund <[email protected]>
(cherry picked from commit d322d6f)

Co-authored-by: Julien Enoch <[email protected]>
Yadunund pushed a commit to ros2/rmw_zenoh that referenced this pull request Feb 4, 2025
* Bump: zenoh-cpp=bd4d741 zenoh-c=15d56e1 zenoh=e17de41

* Config: add new gossip target setting (eclipse-zenoh/zenoh#1678)

* Config: add new interests timeout setting (eclipse-zenoh/zenoh#1710)

* Config: copy updates from eclipse-zenoh/zenoh#1712

* Config: copy updates from eclipse-zenoh/zenoh#1722

* Config: copy updates from eclipse-zenoh/zenoh#1749

* Config: copy updates from eclipse-zenoh/zenoh#1751

* fix: use the commit 15d56e1 to include the allocation feature

* Bump zenoh-c=5fce7fb zenoh=e4ea6f0 (for eclipse-zenoh/zenoh#1754 fixing a routing issue in 1.2.0)

* Update documentation for commits and undo style changes

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: yuanyuyuan <[email protected]>
Co-authored-by: Yadunund <[email protected]>
(cherry picked from commit d322d6f)

Co-authored-by: Julien Enoch <[email protected]>
wyfo added a commit to ZettaScaleLabs/zenoh that referenced this pull request Feb 10, 2025
wyfo added a commit to ZettaScaleLabs/zenoh that referenced this pull request Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Existing things could work better internal Changes not included in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants