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

Massive performance improvements and cleaner shutdown #23

Closed
wants to merge 5 commits into from

Conversation

botbench
Copy link

@botbench botbench commented Apr 1, 2025

Tight while() loop in main() now uses rclpp::Rate to reduce CPU usage due to busy waiting.
Node now catches both SIGINT and TERM and passes off shutdown to handler.
CPU usage has gone from 100% of one core to around 1-3%.

Tight while() loop in main() now uses rclpp::Rate to reduce CPU usage due to busy waiting.

Added handler to allow node to shutdown more gracefully.

Signed-off-by: Xander Soldaat <[email protected]>
Node now catches both SIGINT and TERM and passes off shutdown to handler.
bool has been replaced with std::atomic_bool
Improved performance and better shutdown
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @botbench, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses performance issues and improves the shutdown process of the laser scan publisher node. It introduces a rclcpp::Rate to control the loop frequency, reducing CPU usage. Additionally, it implements signal handling for SIGINT and SIGTERM to ensure a graceful shutdown. A cleanup function is also registered to be called on shutdown.

Highlights

  • Performance Improvement: Implements rclcpp::Rate to regulate the main loop's frequency, significantly reducing CPU usage from 100% to 1-3%.
  • Graceful Shutdown: Adds signal handling for SIGINT and SIGTERM signals, triggering a cleanup function and proper shutdown of the node.
  • Cleanup Function: Registers a cleanup function (node_cleanup) using rclcpp::on_shutdown to perform necessary actions before the node terminates.

Changelog

  • src/main.cpp
    • Introduces shutdown_requested atomic boolean to control the main loop's execution.
    • Adds signal_handler function to catch SIGINT and SIGTERM signals and initiate shutdown.
    • Implements node_cleanup function to perform cleanup tasks before shutdown.
    • Registers signal_handler for SIGINT and SIGTERM signals.
    • Registers node_cleanup function to be executed on shutdown.
    • Replaces the tight while loop with a loop that uses rclcpp::Rate to control the frequency.
    • Adds a check for shutdown_requested in the main loop condition.
    • Closes the command port (cmd_port.Close()) after the loop finishes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A node's gentle sleep,
No longer a CPU hog,
Signals bring it rest.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance improvements by reducing CPU usage and implementing a cleaner shutdown process. The changes include using rclcpp::Rate to control the loop frequency and handling SIGINT and SIGTERM signals for graceful shutdown. Overall, the changes seem well-structured and address the stated goals effectively.

Summary of Findings

  • Signal Handler and Shutdown: The signal handler and shutdown mechanism are well-implemented, ensuring a graceful shutdown of the node upon receiving SIGINT or SIGTERM signals. This prevents abrupt termination and allows for proper resource cleanup.
  • Rate Limiting: The use of rclcpp::Rate effectively reduces CPU usage by preventing a tight loop. Setting the rate to 40Hz seems reasonable for most cases, but it might be beneficial to allow this value to be configurable via a parameter.
  • Resource Cleanup: The addition of cmd_port.Close() ensures that the serial port is properly closed before the node terminates, preventing potential resource leaks or conflicts.

Merge Readiness

The pull request is well-structured and addresses the performance and shutdown issues effectively. The changes significantly reduce CPU usage and provide a cleaner shutdown process. I recommend merging this pull request after addressing the comments below. I am unable to directly approve the pull request, and users should have others review and approve this code before merging.

botbench added 2 commits April 1, 2025 17:45
The command line option 'poll_rate_hz' has been added. It allows the driver to poll at the requested rate.
Removed the to_right_hand function parameter to remove compiler warning.
@GyuH13
Copy link
Contributor

GyuH13 commented Apr 7, 2025

@botbench
Thank you for your contribution. We will discuss the relevant part and make modifications as necessary before applying it. This PR will be closed. Thank you!

@GyuH13 GyuH13 closed this Apr 7, 2025
@botbench
Copy link
Author

botbench commented Apr 7, 2025

Great! I am working on some other improvements to the driver. I'll post that pull request soon.

@GyuH13
Copy link
Contributor

GyuH13 commented Apr 11, 2025

Hello @botbench
#27 (comment)
Thank you for your contribution!
By adding sleep to the infinite while loop, we were able to reduce the CPU usage from 100% to around 5%.
I’ve added your PR as a reference in the ChangeLog. Thanks again!

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.

2 participants