Se questo è per nodejs, farebbe un semplice thread di monitoraggio? Non conosco un modo per ottenere informazioni sugli interni delle code degli eventi, ma è possibile iniettare un tracciante nella coda degli eventi per monitorare i thread in corso in modo tempestivo. (Questo misura il carico non dal numero di thread non ancora eseguiti, ma dal fatto che i thread vengano eseguiti in tempo.)
Un thread del monitor può ri-fare la coda e controllare che venga chiamato almeno ogni 10 millisecondi (o qualunque sia il massimo cumulativo di ms di blocco consentito). Poiché nodej esegue i thread round-robin, se il thread del monitor è stato eseguito in tempo, ci dice che tutti gli altri thread hanno la possibilità di essere eseguiti all'interno della stessa finestra di 10 ms. Qualcosa di simile (in nodi):
// 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 i messaggi di avviso e sostenere una chiusura pulita è un esercizio lasciato al lettore.
stai usando libuv come parte di un'app node.js o standalone? –