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)