NodeJS CPU一次最多可达到100%一个CPU [英] NodeJS CPU spikes to 100% one CPU at a time

查看:684
本文介绍了NodeJS CPU一次最多可达到100%一个CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用NodeJS编写的SOCKS5代理服务器。
我正在使用本机 net dgram 库打开TCP和UDP套接字。

I have a SOCKS5 Proxy server that I wrote in NodeJS. I am utilizing the native net and dgram libraries to open TCP and UDP sockets.

它可以正常工作约2天,并且所有CPU的最大值约为30%。 2天无重启后,一个CPU峰值达到100%。之后,所有CPU都会轮流使用,一次只能保持100%一个CPU。

It's working fine for around 2 days and all the CPUs are around 30% max. After 2 days with no restarts, one CPU spikes to 100%. After that, all CPUs take turns and stay at 100% one CPU at a time.

这是7天的CPU高峰图表:
< img src = https://i.stack.imgur.com/vHaQI.png alt =在此处输入图片描述>

Here is a 7 day chart of the CPU spikes:

我是使用Cluster创建实例,例如:

I am using Cluster to create instances such as:

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

这是当cpu处于100%时strace的输出:

This is the output of strace while the cpu is at 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

和节点概要文件结果(重载):

And the node profile result (heavy up):

[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

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

Since I only use the native libraries most of my code actually runs on C++ and not JS. So any debugging that I have to do is in v8 engine. Here is a summary of node profiler (for language):

 [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天。

I was suspecting that it might be the garbage collector that was running. But I have increased the heap size of Node and the memory seems to be within range. I don't really know how to debug it since each iteration takes around 2 days.

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

Anyone had a similar issue and had success debugging it? I can use any help I can get.

推荐答案

几个月前,我们意识到在同一容器上运行的另一项服务一直在跟踪打开的套接字导致了此问题。该服务是一个较旧的版本,过了一会儿,它在跟踪套接字时使CPU异常运行。将服务升级到最新版本可以解决cpu问题。

A few months ago we realized that another service that was running on the same box that was keeping track of open sockets was causing the issue. This service was an older version and after a while it was spiking the cpu when tracking the sockets. Upgrading the service to the latest version solved the cpu issues.

经验教训:有时候不是您,而是他们

Lesson learned: Sometimes it is not you, it is them

这篇关于NodeJS CPU一次最多可达到100%一个CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆