使用轮询系统调用等待 fds 时 CPU 使用率高

High CPU usage while using poll system call to wait on fds

本文关键字:CPU 使用率 fds 等待 系统调用      更新时间:2023-10-16

我有一个独特的问题,我的代码中使用的 linux 的轮询系统调用会得到它等待轮询的 fd,我的意思是每毫秒 POLLIN。这会导致 高 CPU 使用率 .我提供了 100 毫秒的超时,它似乎没有用。任何人都可以提出替代方案。

    for (;;) {
 ACE_Time_Value doWork(0, 20000);  
 ACE_OS::sleep(doWork);  ----------------------------> Causing low throughput, put to decrease CPU usage / On removing this we see high CPU , but throughput is achieved.
..
.
..
 if ((exitCode = fxDoWork()) < 0) {
  break;}
}
fxDoWork()
{
ACE_Time_Value selectTime;
selectTime.set(0, 100000);
..
..
..
ACE_INT32 waitResult = ACE_OS::poll(myPollfds, eventCount, &selectTime);-----------------------------> Pollin happens for every milli second/Timeout is not at all useful
..
..
..
}
===============================================================

听起来您想积累足够的数据或发生特定的超时以减少 CPU 使用率,对吗? 如果是这种情况,您可以使用 recvmmsg(): http://man7.org/linux/man-pages/man2/recvmmsg.2.html

recvmmsg() 系统调用是 recvmsg(2) 的扩展,它允许 使用单个套接字从套接字接收多条消息的调用方 系统调用。 (这对某些应用程序具有性能优势。 对 recvmsg(2) 的进一步扩展是支持在 接收操作。