切片服务中对node的折腾

1 通过控制nodejs线程池大小,控制cpu资源的占用

环境:16核32g

node的线程池

第一类:v8的线程,通过--v8-pool-size=num 设置

Set V8's thread pool size which will be used to allocate background jobs.
  If set to `0` then V8 will choose an appropriate size of the thread pool based on the number of online processors.
  If the value provided is larger than V8's maximum, then the largest value will be chosen.

第二类:libuv的线程,UV_THREADPOOL_SIZE=size

Set the number of threads used in libuv's threadpool to `size` threads.

Asynchronous system APIs are used by Node.js whenever possible, but where they do not exist, libuv's threadpool is used to create asynchronous node APIs based on synchronous system APIs. Node.js APIs that use the threadpool are:

*   all `fs` APIs, other than the file watcher APIs and those that are explicitly synchronous
*   `crypto.pbkdf2()`
*   `crypto.randomBytes()`, unless it is used without a callback
*   `crypto.randomFill()`
*   `dns.lookup()`
*   all `zlib` APIs, other than those that are explicitly synchronous

Because libuv's threadpool has a fixed size, it means that if for whatever reason any of these APIs takes a long time, other (seemingly unrelated) APIs that run in libuv's threadpool will experience degraded performance. In order to mitigate this issue, one potential solution is to increase the size of libuv's threadpool by setting the `'UV_THREADPOOL_SIZE'` environment variable to a value greater than `4` (its current default value). For more information, see the [libuv threadpool documentation](http://docs.libuv.org/en/latest/threadpool.html).

不加参数启动node进程

进程启动
承载压力的时候

对比刚启动的时候,libuv的线程池扩容了4个

将libuv进程池大小设置为20个

刚启动
增加负载

对比发现,上面有21个进程处于Rsl,20个是libuv线程池的,1个是v8自己的线程池

总结:

  • 在nodejs 中如果使用libuv的uv_thread做耗时运算,则需要调整libuv的线程池
    • 如果默认设置过大,很容易造成cpu过载(这次的问题)
    • 如果默认设置过小,会浪费cpu资源
    • 如果js工程比较小,调小v8线程池
  • 对于普通js工程,可以尝试调节v8线程池数量,提高js执行速度

ps Stat 列注解

 S    Interruptible sleep (waiting for an event to complete)
 s    is a session leader
 l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
 R    running or runnable (on run queue)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容