Libuv worker threads or queue health check?

In libuv, you can associate workflows with too much work or an error. Is there a simple function that can check the health of worker threads or thread queues? It should not be 100% deterministic, because it would be impossible to determine if the workflow is hanging on a slow code or an infinite loop.

So, any of the following heuristics will be good:

  • The number of objects in the queue that have not yet been processed. If this is too large, it may mean that the worker threads are busy or hung.

  • Does libuv have any thread killing mechanism, if the worker thread does not check after n seconds, it ends?

+7
c ++ multithreading events libuv
source share
2 answers

This function does not exist in libuv itself, and I do not know any OSS that provides something like this.

In terms of the kill mechanism, there is no one in libuv, but http://nikhilm.imtqy.com/uvbook/threads.html#core-thread-operations suggests:

A well-designed program will be able to stop work that has already begun to run. Such a worker could periodically check a variable that only the main process sets to complete the signal.

+1
source share

If this is for nodejs, will there be a simple monitor stream? I don’t know how to get information about the internals of the event queue, but you can put the tracer in the event queue to control that threads are running in a timely manner. (This does not measure the load on the number of threads that are not already running, but on the fact that the threads start on time. The same thing, sort of.)

The monitoring thread can reorder itself and verify that it is called at least every 10 milliseconds (or any possible cumulative blocking ms is allowed). Since nodej starts round-robin threads, if the monitor thread was started on time, it tells us that all the other threads got the opportunity to start in 10 ms in the same window. Something like (in node):

// 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(); 

Throttling warning messages and supporting clean shutdown is an exercise left to the reader.

-one
source share

All Articles