CPU skyrocketing to 100% with exec node using SLEEP function < 1.0sec, but all good (1-5%) when SLEEP >= 1.0sec

Hi All,

I am facing a blocking issue with Node-RED.

I need to use an exec node which calls a function written in C++, and that contains a SLEEP function.

When the sleep is >= 1.0 sec, the Node-RED's CPU usage is all good : 1-5%.

But as soon as the sleep is < 1.0 sec, for instance 999ms, the Node-RED's CPU usage is skyrocketing to 100% and gets (obviously) Node-RED broken.

Would you be able to have a look at this, as I need a SLEEP function in my C++ code that is below 1.0 sec?

Are you running the exec node in exec mode or in spawn mode ? the latter might help.

Have you got something in node red waiting on a result from the exec or on something after the exec? If so then how have you coded that?

Hi @Colin yes, I got a split node, followed by a switch one, then that's it.

Is that a normal behavior ?

No, what happens with the CPU if you disconnect the wires on the output of the exec?

HI @bakman2 In spawn mode as I need real-time outputs and must run the script 24/7.

Are you sure that your c++ code can handle the float 0.999? if it is expecting a uint, then that value would be truncated to 0.

edit. i guess that wouldnt matter if you are passing milliseconds. can you share the c++ code ?

Nothing you do in an exec node can affect the CPU used by node red. In particular the split node does not know what you are doing in the exec. The load is either in the process in the exec node, or it is a side effect of the delay.

Are you calling the exec node regularly? If so at what rate? Is it once per second possibly? If so then with the timeout greater than that you will be starting multiple copies in parallel, and the number will increase by 1 every second.

Hi @kevinGodell

Confirmed my code can handle decimal in sleep() function because when I compile it and run it directly in the console without node-Red, it works like a charm. Even with 100ms.

I will provide you with the code later on today for you to have a look. Is that OK?

Hi @Colin

The exec node is continuously running, using spawn mode as I need real time info coming out of it in order to take decisions based on the payload.

Possibly not what you want to consider right now but ... Have you considered whether this is the right approach? If you have a C++ app that needs to run continuously and output data, seems to me as though that should be written as a service and should either output to a stream (could just be stdout which could be redirected) or better still output to MQTT or even a REST API - depending on the size, shape and speed of output.

At the moment, you have two separate processes that you have tightly coupled which will always make debugging and performance tuning harder. Better to have them decoupled.

How are you starting the exec node?

If using an Inject node can you post a screenshot of the inject node config please.

I only have a phone at the moment so can't try anything. If you add a debug node showing what is coming from the exec is there a difference between the ok sleep values and the bad values?
How often does it send a message in each case?

Normally the function is sleep(), lower case. I don't know the function you are calling. I do know the usual sleep takes an integer seconds, so if you give it a value less than 1 it will convert that to 0 and result in no delay in the loop.

As @Colin states, there is something a little odd about SLEEP. Also, there seems to be a file listed for inclusion, but it is not in the shared zip
#include "../inc/compatibility.h" Maybe that is where SLEEP is defined?

My hunch, without having further data is that the SLEEP function is not throttling at all and the message are pumping out a high rate to the split node. You should add a message counter node after the exec node just to see what rate the messages are being delivered.

What do you mean by way to late? If it is only using 5% cpu that means it is not being slowed down by processor availability.

What are the remaining 2 outputs (stderr/errorcode) reporting ?

Also put a debug node on the first output of the exec node to see if the delay is there or somewhere else.

No, I don't want a video.

1 Like

This could be due to buffering in the stdout stream between the processes.

For example, when invoking python commands with the exec node, its advisable to use the -u command line arg to disable stdout buffering.

Without having looked at your code (and being very rusty with C++), can you force a flush of the stream after writing the value?

Hi @knolleary

Thanks for the advise. I'm not sure where I need to force the flush : in the C++ code? Or in the exec node?