Idle ROS nodes using >100% CPU
i've built/written a couple of my own ROS nodes. They are connected like this:
create_data_node mqtt_bridge =>|broker|=> mqtt_bridge => processing_node
master1/ pc1 master2/pc2
create_data_node
does just what you would expect - creating data and then publishing it onto the mqtt_bridge, which in return will publish them onto the broker. From there another mqtt_bridge
(on another pc) received the data and published it to processing_node
which in return does some processing. The workflow itself has to be that way, works out when data is created and is not part of this question.
Now: The moment i shut down create_data_node
everything on master2/pc2 should enter idle mode, since no data is put in and no callback are called. Yet the CPU usage for this particular node is > 100% and furthermore rosout
is using ~150% as well.
On processing_node
i'm simply creating a ros::Subscriber
and then using ros::spin()
, just as follows (stripped example):
int main(int argc, char** argv) {
ros::init(argc, argv, DEFAULT_NODENAME);
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe<messages::testmessage>("topic", 1000, callback);
ros::spin();
return 0;
}
As far as i understand ros::spin()
(in contrary to while(1) { ros::spinOnce();}
should not do an idle wait.
So my question simply is - What causes this behaviour and what can i do to prevent this? Thanks in advance!
Edit: Reducing the subscriber queue to 100 resulted in 60% CPU and further reducing to 10 in 50% CPU usage.
have you tried with a queue of 1 for you subscriber?
Just to keep things connected: #q298364.
@GuillaumeB see my edit.
Is the process in your callback a long and heavy one? What kind of message are you using (simple Float32 or huge images)?
There are no messages incoming and thus the callback isn't even executed.
It tried in my side with a queue of 2000 msg and if I publish msg I can reach 50% of cpu usage but not before sending any msg (0.7%). I would suspect that messages are stacked in your queue and even once you kill the publisher node it is still processing.
again, the nodes are in idle mode since there no messages incoming. none are published and thus there should barely be any cpu usage at all
But as @GuillaumeB has said, the buffer may still contain messages long after the publisher node has been shutdown. Could you modify your code so that the processing_node logs to the screen when it processes a messages, then you could tell for sure.