epoll_wait() 消耗太多CPU

Wen*_*.Wu 5 linux epoll strace

我的 epoll_wait() 消耗了太多 CPU,一个简单的 strace 显示:

    strace -c -f -p 3655
    Process 3655 attached with 5 threads
    ^CProcess 3655 detached
    Process 3656 detached
    Process 3657 detached
    Process 3658 detached
    Process 3659 detached
    % time     seconds  usecs/call     calls    errors syscall
    ------ ----------- ----------- --------- --------- ----------------
     64.43   32.228205      596819        54           epoll_wait
     35.31   17.661939          17   1063312    195547 futex
      0.26    0.131803           0    448140           gettimeofday
      0.00    0.000090           3        27           write
      0.00    0.000050           1        54           epoll_ctl
      0.00    0.000000           0        54        27 read
      0.00    0.000000           0         9           sendmsg
      0.00    0.000000           0        90        54 recvmsg
    ------ ----------- ----------- --------- --------- ----------------
    100.00   50.022087               1511740    195628 total
Run Code Online (Sandbox Code Playgroud)

只有 54 个 epoll_wait() 调用,但 epoll_wait() 的 usecs/call 是 596819,为什么?

cni*_*tar 3

在这种情况下,报告的时间strace有点误导。

strace(1)告诉我们:

-c 计算每个系统调用的时间、调用和错误,并在程序退出时报告摘要。在 Linux 上,这尝试显示 独立于挂钟时间的系统时间(在内核中运行所花费的 CPU 时间) 。

但我怀疑它实际计算的只是从被epoll_wait调用到返回的时间。这并不意味着您的进程位于"running"内核中。它更有可能正在睡觉,这对于……来说是很自然的事情epoll_wait