Skip to content

Commit

Permalink
fixed excessive CPU consumption of async server
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Aug 31, 2023
1 parent 5073a0f commit 828117b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/mormot.commit.inc
@@ -1 +1 @@
'2.1.5810'
'2.1.5811'
37 changes: 13 additions & 24 deletions src/net/mormot.net.async.pas
Expand Up @@ -1815,22 +1815,16 @@ procedure TAsyncConnectionsThread.Execute;
(fOwner.fThreadClients.Count > 0) and
(InterlockedDecrement(fOwner.fThreadClients.Count) >= 0) do
fOwner.ThreadClientsConnect;
ms := 0;
ms := 1000;
case fProcess of
atpReadSingle:
if fOwner.fClientsEpoll then
ms := 100 // for quick shutdown
else
ms := 1000;
atpReadPoll:
if fOwner.fClientsEpoll then
ms := 1100; // efficient epoll_wait(ms) API call
ms := 100; // for quick shutdown
end;
// main TAsyncConnections read/write process
while not Terminated and
(fOwner.fClients <> nil) and
(fOwner.fClients.fRead <> nil) do
begin
case fProcess of
atpReadSingle:
// a single thread to rule them all: polling, reading and processing
Expand All @@ -1845,30 +1839,31 @@ procedure TAsyncConnectionsThread.Execute;
new := fOwner.fClients.fRead.PollForPendingEvents(ms);
if Terminated then
break;
if (new = 0) and
(fOwner.fClients.fRead.fPending.Count <> 0) then
new := 1;
fEvent.ResetEvent;
fWaitForReadPending := true; // should be set before wakeup
fWaitForReadPending := true; // to be set before wakeup
if new <> 0 then
fOwner.ThreadPollingWakeup(new);
new := fOwner.ThreadPollingWakeup(new);
// wait for the sub-threads to wake up this one
if Terminated then
break;
if (fEvent.IsEventFD and
(fOwner.fThreadPollingAwakeCount > 2)) or
((fOwner.fClients.fRead.fPending.Count = 0) and
(fOwner.fClients.fRead.Count = 0)) then
begin
// 1) avoid poll(eventfd) syscall on heavy loaded server
// 2) there is no connection any more: wait for next accept
begin
fWaitForReadPending := true; // better safe than sorry
fEvent.WaitForEver;
end
else if fOwner.fClientsEpoll then
// not needed by select/poll TPollSocketAbstract.WaitForModified
// which actually waits up to the ms period
else
begin
// there are some threads working: wait for the first idle
// always release current thread to avoid CPU burning
fWaitForReadPending := true;
fEvent.WaitFor(20);
fEvent.WaitFor(1);
end;
end;
atpReadPending:
Expand All @@ -1891,7 +1886,6 @@ procedure TAsyncConnectionsThread.Execute;
raise EAsyncConnections.CreateUtf8('%.Execute: unexpected fProcess=%',
[self, ord(fProcess)]);
end;
end;
fOwner.DoLog(sllInfo, 'Execute: done %', [fName], self);
except
on E: Exception do
Expand Down Expand Up @@ -2220,8 +2214,6 @@ function TAsyncConnections.ThreadClientsConnect: TAsyncConnection;
// thread is supposed to handle in its loop - default value is computed as
// (ThreadPoolCount / CpuCount) * 8 so should scale depending on the actual HW
// - on Linux, waking up threads is done via efficient blocking eventfd()
// - on Windows, acoThreadSmooting is forced because without it the async
// server seems unstable and consumes too much CPU in its R0 thread

function TAsyncConnections.ThreadPollingWakeup(Events: integer): PtrInt;
var
Expand Down Expand Up @@ -2253,7 +2245,7 @@ function TAsyncConnections.ThreadPollingWakeup(Events: integer): PtrInt;
// exactly wake up one thread per needed event
if t.fWaitForReadPending then
begin
// this thread is currently idle and can be used
// this thread is currently idle and can be activated
t.fThreadPollingLastWakeUpCount := 0;
t.fThreadPollingLastWakeUpTix := 0;
t.fWaitForReadPending := false; // acquire this thread
Expand Down Expand Up @@ -2291,7 +2283,7 @@ function TAsyncConnections.ThreadPollingWakeup(Events: integer): PtrInt;
end;
// notify threads outside fThreadPollingWakeupSafe
for i := 0 to result - 1 do
fThreads[ndx[i]].fEvent.SetEvent; // on Linux, will write eventfd()
fThreads[ndx[i]].fEvent.SetEvent; // on Linux, uses eventfd()
end;

procedure TAsyncConnections.DoLog(Level: TSynLogInfo; const TextFmt: RawUtf8;
Expand Down Expand Up @@ -3646,9 +3638,6 @@ constructor THttpAsyncServer.Create(const aPort: RawUtf8; const OnStart,
//include(aco, acoWritePollOnly);
if hsoEnableTls in ProcessOptions then
include(aco, acoEnableTls);
{$ifdef OSWINDOWS}
include(ProcessOptions, hsoThreadSmooting); // seems unstable without it
{$endif OSWINDOWS}
if hsoThreadSmooting in ProcessOptions then
include(aco, acoThreadSmooting)
else // our thread smooting algorithm excludes CPU affinity
Expand Down

0 comments on commit 828117b

Please sign in to comment.