[Lazarus] Command line program - CPU overused - do I need profiling?

Martin Frb lazarus at mfriebe.de
Mon Oct 4 15:19:35 CEST 2021

On 04/10/2021 10:33, Bo Berglund via lazarus wrote:
> Failed to post via Gmane so trying direct emailing instead:
> I need to figure out why my program uses 10% CPU when it is essentially idle, waiting for a client connect via TCP or for the time for a new task start to arrive.
> It is a command line program running as a service on Linux (Raspberry Pi4).
> Developed on the RPi4 with Lazarus 2.0.12 and FPC 3.2.0
> This is the main loop in the program following creatrion of the handler objects (which are idle):
>        While not (bSTerm or bSInt or bsHup) do
>        begin
>          //Eternal loop to wait for system messages
>          CheckSynchronize(5); //Timeout here instead of using sleep

I think "5" is too small a timeout. So this could keep your system busy. 
Even if it would be less than 10%, it will prevent your system from ever 
entering any idle/power saving state.

Ideally you should not need a timeout.
If any code (in a thread) sets any of the variables, it should sent a 
message to wake the CheckSyncronize.

If of course those vars are set in interrupts, then you can not send a 
wake up, because any work in interrupts is dangerous....
In that case set the timeout to at least 50. Ideally even higher.

You can have any thread checking the vars, before they do any work (or 
even while the do work).
Threads can then abort, and send a wake up.
So a longer delay, would not mean any work being done in that time.
It would only mean the app shuts down a bit later.
IMHO in that a scenario, you could probably up the timeout to 100 or 
even 200 ms.
Well I don't know how urgent you need your app to exit or reload config, 
(Assuming that no-one changes the config that should be loaded in that 
200ms, and no thread starts work on the old because they all check the 
flag.... then the delay should not hurt)

Also maybe there is a wait or select call, that would return if an 
interrupt was received. (IIRC select does that). Then your threads would 
have to use whatever you select on, in order to communicate with the 
main thread....

More information about the lazarus mailing list