[fpc-pascal] My Linux service application consumes 10% CPU when idling - why?

Bo Berglund bo.berglund at gmail.com
Sun Oct 17 00:43:51 CEST 2021


On Sat, 16 Oct 2021 18:16:57 -0400, Travis Siegel via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:

>
>On 10/16/2021 6:06 PM, Bo Berglund via fpc-pascal wrote:
>> Meanwhile the strace outputs stuff like this to the screen:
>> strace: Process 6360 attached
>> strace: Process 6361 attached
>> strace: Process 6362 attached
>> strace: Process 6363 attached
>> strace: Process 6364 attached
>> strace: Process 6365 attached
>> strace: Process 6366 attached
>> strace: Process 6367 attached
>> strace: Process 6368 attached
>> strace: Process 6369 attached
>> strace: Process 6370 attached
>> strace: Process 6371 attached
>> strace: Process 6372 attached
>>
>> I don't understand what this means either....
>>
>It looks like your application is spawning additional processes. If it's 
>supposed to do that, then all is well.  If it isn't supposed to do that 
>when it's idling, then you have some troubleshooting to do. :)

I found a howto-page for strace so I could modify the call to have strace
produce a summary after running for the set time (3 seconds here).
Easier read but does not give me much to work with.

timeout --signal=2 3s strace -f -c -p 488 -o tracelog2.log

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 46.47    0.348353         414       841           nanosleep
 27.67    0.207437       17286        12           _newselect
 16.17    0.121220         211       572        77 futex
  2.54    0.019020          89       213           munmap
  1.77    0.013281          62       213           mmap2
  1.63    0.012250          43       284       284 ioctl
  0.73    0.005485          77        71           madvise
  0.55    0.004137          29       142           gettimeofday
  0.53    0.003958          27       142           clock_gettime
  0.51    0.003858          54        71           rt_sigprocmask
  0.37    0.002748          38        71           clone
  0.36    0.002688          37        71           sched_setscheduler
  0.31    0.002315          32        71           set_robust_list
  0.30    0.002262          31        71           ugetrlimit
  0.07    0.000548         182         3         1 restart_syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.749560                  2848       362 total

I don't really see how this helps either, though....

As a reminder here is the main application's loop:

      While not (bSTerm or bSInt or bsHup) do
      begin
        //Here is where the server runs as defined elsewhere
        //Eternal loop to wait for system messages
        Sleep(5); //To not hog the CPU
        CheckSynchronize(); //No timeout here since sleep is used
        //CheckSynchronize(5); //Timeout here instead of using sleep
        if CheckRestartReq then
        begin
          Debug_Writeln('Restart timeout reached - exiting');
          FLogServ.StdLog('Restart timeout reached - exiting');
          break;
          //FServerStartTick := GetTickCount64; //Reset timeout for now
        end;
      end;

And CheckRestartReq implementation:
Note that FRemoteTaskServer.AutoRestartInterval is 0 here.
So the function CheckRestartReq should always return immediately with a false
result.

function CheckRestartReq: boolean;
var
  NextStart: TDateTime;
begin
  Result := false;
  if FRemoteTaskServer.AutoRestartInterval < 120 then exit; //Not configured for
automatic restart (min time 2 min)
  if (GetTickCount64 - FServerStartTick) <
(FRemoteTaskServer.AutoRestartInterval * 1000) then exit; //Not reached set
restart time yet
  if FRemoteTaskServer.TaskRunning then exit; //Cannot restart when task is
running
  if FSuperStingSocketServer.ClientCount > 0 then exit; //Cannot restart when
client connected

  NextStart := FRemoteTaskServer.NextTaskStartTime; //Get next time a task will
start
  if NextStart = 0 then  //No task scheduled to start so OK to restart server
  begin
    Result := true;
    exit;
  end;
  if (NextStart - Now) > (2.0 / 60 / 24) then  Result := true; // More than 2
minutes until next task start, so OK to exit
end;


-- 
Bo Berglund
Developer in Sweden



More information about the fpc-pascal mailing list