#1 2020-12-11 10:19:39

sevo
Member
Registered: 2015-11-10
Posts: 27

High CPU-Load on Idle, ConsoleWaitForEnterKey (Windows Server 2019)

Hi,

on Windows Server 2019 my console-application has constant 25% CPU load, conhost.exe another 25%.
On Windows 7, everything is just fine (< 1% during idle).

I'm using the function ConsoleWaitForEnterKey.

Can you give me a hint?

Thanks in advance!

Offline

#2 2020-12-11 11:08:22

tbo
Member
Registered: 2015-04-20
Posts: 349

Re: High CPU-Load on Idle, ConsoleWaitForEnterKey (Windows Server 2019)

Just thought as a hint. I have had good experience with TSynDaemon as a starting point.

TMyServerSettings = class(TSynDaemonSettings);

TMyServerDaemon = class(TSynDaemon)
private
  FMain: TMyServerMain;
public
  constructor Create; reintroduce;
  procedure Start; override;
  procedure Stop; override;
end;
  
constructor TMyServerDaemon.Create;
begin
  inherited Create(TMyServerSettings,
    IncludeTrailingPathDelimiter(ExeVersion.ProgramFilePath),
    TFileUtils.MainDataFolder,                
    TFileUtils.GetLogFileFolder);
  ...
end;

procedure TMyServerDaemon.Start;
begin
  if FMain = Nil then
    FMain := TMyServerMain.Create;
end;

procedure TMyServerDaemon.Stop;
begin
  FreeAndNil(FMain);
end;  

// Main MyServer.dpr
begin
  with TMyServerDaemon.Create do
  try
    CommandLine(True);
  finally
    Free;
  end;
  
end.

Then I can start the server e.g. with "MyServer.exe -c".

With best regards
Thomas

Offline

#3 2021-01-12 11:25:06

sevo
Member
Registered: 2015-11-10
Posts: 27

Re: High CPU-Load on Idle, ConsoleWaitForEnterKey (Windows Server 2019)

Thanks Thomas for your hint!

I successfully used your approach with mORMotService.TSynDaemon - High CPU-Load is gone.
But i had to use ConsoleWaitForEnterKey() instead of readln() in procedure TSynDaemon.CommandLine, else the mainthread was blocked and we currently use some Timers and Services running in Mainthread.

Offline

#4 2021-01-12 11:44:44

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: High CPU-Load on Idle, ConsoleWaitForEnterKey (Windows Server 2019)

I believe that when using the Daemon all processes must be running inside the daemon.

TSynBackgroundTimer allows you to perform tasks at defined intervals.

Offline

#5 2021-01-12 14:23:47

tbo
Member
Registered: 2015-04-20
Posts: 349

Re: High CPU-Load on Idle, ConsoleWaitForEnterKey (Windows Server 2019)

macfly has already written the solution. In my source code I use it as follows:

TMyRestServer = class(TSQLRestServerDB)
protected
  procedure DownloadData(pmSender: TSynBackgroundTimer; pmEvent: TWaitResult; const pmcMsg: RawUTF8);
public  
  procedure Start;
  procedure Stop;
end;

procedure TMyRestServer.Start;
var
  timer: TSynBackgroundTimer;
begin
  timer := TimerEnable(DownloadData, FRestServiceSettings.DataUpdateInterval);
  if Assigned(timer) then
    timer.ExecuteNow(DownloadData);
end;

procedure TMyRestServer.Stop;
begin
  TimerDisable(DownloadData);
end;

With best regards
Thomas

Offline

Board footer

Powered by FluxBB