Skip to content

[JSB] skip publish on no subscription #1609

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

saikishor
Copy link
Member

might help with #1409
@firesurfer can you check if this helps?

Copy link

codecov bot commented Mar 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.20%. Comparing base (7c6a938) to head (2911d34).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1609      +/-   ##
==========================================
+ Coverage   85.16%   85.20%   +0.03%     
==========================================
  Files         126      126              
  Lines       11915    11922       +7     
  Branches      997      997              
==========================================
+ Hits        10148    10158      +10     
+ Misses       1454     1452       -2     
+ Partials      313      312       -1     
Flag Coverage Δ
unittests 85.20% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
..._state_broadcaster/src/joint_state_broadcaster.cpp 89.88% <100.00%> (+0.43%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@firesurfer
Copy link
Contributor

@saikishor I don't think this will help with the other issue as there are subscribers on the joint state topic - I also explained in the other issue why I think this happens in our setup.

Also a small comment from my side on this patch. I am not sure how ROS2 actually determines the amount of subscribers of a topic. I could imagine that this might be a rather expensive operation. It might be cheaper just to publish the data.

@saikishor
Copy link
Member Author

@saikishor I don't think this will help with the other issue as there are subscribers on the joint state topic - I also explained in the other issue why I think this happens in our setup.

I thought the overhead might come from the dynamic_joint_states.

Also a small comment from my side on this patch. I am not sure how ROS2 actually determines the amount of subscribers of a topic. I could imagine that this might be a rather expensive operation. It might be cheaper just to publish the data.

I don't think it's expensive operation, I have used in RT loops and didn't find any major jitters

@firesurfer
Copy link
Contributor

I thought the overhead might come from the dynamic_joint_states.

I see. I can hopefully give it a try next week if our setup is free.

@@ -351,7 +351,9 @@ controller_interface::return_type JointStateBroadcaster::update(
state_interface.get_value());
}

if (realtime_joint_state_publisher_ && realtime_joint_state_publisher_->trylock())
if (
realtime_joint_state_publisher_ && joint_state_publisher_->get_subscription_count() > 0u &&
Copy link

Choose a reason for hiding this comment

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

looks good. Are the rmw and implementations thereof doing this implicitly already?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe yes

Copy link

Choose a reason for hiding this comment

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

meaning subscription count is polled outside the RT loop where it is definitely safe, right? To my best understanding, the publish operation just appends a buffer

Copy link
Member Author

@saikishor saikishor Apr 4, 2025

Choose a reason for hiding this comment

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

Yes! That's what I understood

@bmagyar
Copy link
Member

bmagyar commented Mar 31, 2025

So we wait for @firesurfer 's feedback?

@saikishor
Copy link
Member Author

So we wait for @firesurfer 's feedback?

Yeah, let's wait for his feedback just in case.

@firesurfer
Copy link
Contributor

Hey guys. Unfortunately I wasn't able to test this patch on our setup.

Given that this patch does not make things worse:

I don't think it's expensive operation, I have used in RT loops and didn't find any major jitters

I would say feel free to merge it.

@mhubii
Copy link

mhubii commented Apr 3, 2025

If it's not broken, don't fix it. Might make sense to benchmark this before modifying such a sensitive line of code based on empirical insights

@mhubii
Copy link

mhubii commented Apr 3, 2025

wasn't the initial issue closed?

@saikishor
Copy link
Member Author

If it's not broken, don't fix it. Might make sense to benchmark this before modifying such a sensitive line of code based on empirical insights

@mhubii Will you be able to test it on your setup?

@mhubii
Copy link

mhubii commented Apr 4, 2025

essentially run a mock JSB with and without modification, spawn subscriptions + monitor CPU load and cycle time / jitter each across all available middlewares?

wonder whether this could be done using something like Google benchmark on an abstract level

@bmagyar
Copy link
Member

bmagyar commented Apr 10, 2025

Change of heart, I am with @mhubii on this. Instead of benchmarking though I think we should look closer at the JSB with a profiler first. The changes suggested on this PR do not fix the underlying issue, only avoid them when there aren't subscribers which will lead to a massive performance change when there is a subscriber. Let's fix the root cause instead.

Copy link
Member

@bmagyar bmagyar left a comment

Choose a reason for hiding this comment

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

I suggest profiling first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

High CPU load due to joint state broadcaster
4 participants