7

In libuv, you can end up tying up the worker threads with too much work or buggy code. Is there a simple function that can check the health of the worker threads or thread queue? It doesn't have to be 100% deterministic, after all it would be impossible to determine whether the worker thread is hanging on slow code or an infinite loop.

So any of the following heuristics would be good:

  • Number of queued items not yet worked on. If this is too large, it could mean the worker threads are busy or hung.

  • Does libuv have any thread killing mechanism where if the worker thread doesn't check back in n seconds, it gets terminated?

4

2 回答 2

1

该功能在 libuv 本身中不存在,而且我不知道有任何提供类似功能的 OSS。

就杀戮机制而言,libuv 中没有任何内容,但http://nikhilm.github.io/uvbook/threads.html#core-thread-operations建议:

一个设计良好的程序将有办法终止已经开始执行的长时间运行的工作人员。这样的工作人员可以定期检查只有主进程设置为发出终止信号的变量。

于 2014-04-18T00:45:19.317 回答
-1

如果这是针对nodejs的,那么一个简单的监控线程可以吗?我不知道获取有关事件队列内部信息的方法,但您可以将跟踪器注入事件队列以监控线程是否及时运行。(这不是通过尚未运行的线程数来衡量负载,而是通过线程是否按时运行来衡量负载。同样的事情。)

监视器线程可以重新排队自己并检查它是否至少每 10 毫秒被调用一次(或者任何允许的最大累积阻塞 ms)。由于 nodej 循环运行线程,如果监视器线程按时运行,它告诉我们所有其他线程都有机会在同一个 10 毫秒窗口内运行。类似(在节点中):

// like Date.now(), but with higher precision
// the extra precision is needed to be able to track small delays
function dateNow() {
    var t = process.hrtime();
    return (t[0] + t[1] * 1e-9) * 1000;
}

var _lastTimestamp = dateNow();   // when healthMonitor ran last, in ms
var _maxAllowedDelay = 10.0;      // max ms delay we allow for our task to run
function healthMonitor() {
    var now = dateNow();
    var delay = now - _lastTimestamp;
    if (delaly > _maxAllowedDelay) {
        console.log("healthMonitor was late:", delay, " > ", _maxAllowedDelay);
    }
    _lastTimestamp = now;
    setTimeout(healthMonitor, 1);
}

// launch the health monitor and run it forever
// note: the node process will never exit, it will have to be killed
healthMonitor();

限制警报消息并支持完全关闭是留给读者的练习。

于 2014-11-30T04:31:21.640 回答