gpt4 book ai didi

node.js - NodeJS CPU 一次达到 100% 一个 CPU

转载 作者:IT老高 更新时间:2023-10-28 23:17:33 26 4
gpt4 key购买 nike

我有一个用 NodeJS 编写的 SOCKS5 代理服务器。我正在利用原生 netdgram 库来打开 TCP 和 UDP 套接字。

它可以正常工作大约 2 天,并且所有 CPU 最高都在 30% 左右。在没有重新启动的 2 天后,一个 CPU 飙升至 100%。之后,所有 CPU 轮流运行,并保持 100% 一次一个 CPU。

CPU Spikes

这是 CPU 峰值的 7 天图表: enter image description here

我正在使用集群创建实例,例如:

for (let i = 0; i < Os.cpus().length; i++) {
Cluster.fork();
}

这是 strace 在 cpu 为 100% 时的输出:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
99.76 0.294432 79 3733 epoll_pwait
0.10 0.000299 0 3724 24 futex
0.08 0.000250 0 3459 15 rt_sigreturn
0.03 0.000087 0 8699 write
0.01 0.000023 0 190 190 connect
0.01 0.000017 0 3212 38 read
0.00 0.000014 0 420 close
0.00 0.000008 0 612 180 recvmsg
0.00 0.000000 0 34 mmap
0.00 0.000000 0 16 ioctl
0.00 0.000000 0 190 socket
0.00 0.000000 0 111 sendmsg
0.00 0.000000 0 190 bind
0.00 0.000000 0 482 getsockname
0.00 0.000000 0 218 getpeername
0.00 0.000000 0 238 setsockopt
0.00 0.000000 0 432 getsockopt
0.00 0.000000 0 3259 104 epoll_ctl
------ ----------- ----------- --------- --------- ----------------
100.00 0.295130 29219 551 total

和 Node 配置文件结果(重了):

[Bottom up (heavy) profile]:
Note: percentage shows a share of a particular caller in the total
amount of its parent calls.
Callers occupying less than 1.0% are not shown.

ticks parent name
1722861 81.0% syscall

28897 1.4% UNKNOWN

由于我只使用 native 库,我的大部分代码实际上是在 C++ 而不是 JS 上运行的。所以我必须做的任何调试都在 v8 引擎中。以下是 Node 分析器的摘要(针对语言):

 [Summary]:
ticks total nonlib name
92087 4.3% 4.5% JavaScript
1937348 91.1% 94.1% C++
15594 0.7% 0.8% GC
68976 3.2% Shared libraries
28897 1.4% Unaccounted

我怀疑它可能是正在运行的垃圾收集器。但是我增加了 Node 的堆大小,内存似乎在范围内。我真的不知道如何调试它,因为每次迭代大约需要 2 天。

有人遇到过类似的问题并成功调试了吗?我可以使用我能得到的任何帮助。

最佳答案

在您的问题中,没有足够的信息来重现您的案例。操作系统、Node.js 版本、您的代码实现等可能是导致此类行为的原因。

有可以解决或避免此类问题的最佳实践列表:

  1. 使用情况pm2作为您的 Node.js 应用程序的主管。
  2. 在生产环境中调试您的 Node.js 应用程序。为了那个原因:
    • 检查您与产品服务器的 ssh 连接
    • 使用 ssh -N -L 9229:127.0.0.1:9229 root@your-remove-host 将调试端口绑定(bind)到 localhost
    • 通过命令kill -SIGUSR1 <nodejs pid>开始调试
    • 打开 chrome://inspect在您的 Chrome 中或为 Node.js 使用任何其他调试器
  3. 在投入生产之前:

关于node.js - NodeJS CPU 一次达到 100% 一个 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com