I'll show you what I've done to train until now, so you may better understand what I'm looking for in case I'm not very clear ^^
function hello() {
console.log('.');
}
function fibo(n, cb) {
if (n === 0) {
process.nextTick(cb.bind(null, 0));
} else if (n === 1) {
process.nextTick(cb.bind(null, 1));
} else {
var n1, n2;
process.nextTick(fibo.bind(null, n-1, function(n){ n1 = n; next(); }));
process.nextTick(fibo.bind(null, n-2, function(n){ n2 = n; next(); }));
function next() {
if (typeof n1 === 'undefined' || typeof n2 === 'undefined') {
return;
}
process.nextTick(cb.bind(null, n1+n2));
}
}
}
var interval = setInterval(hello, 100);
fibo(24, function (n) {
clearInterval(interval);
console.log(n);
});
It produces following output, where I can see how the event loop is blocked, and the whole point is to detect who is blocking it (here it's obviously fibo() but I train for less obvious cases ;))
$ time node fibo.js ] 9:43
.
.
.
.
.
46368
node fibo.js 0,95s user 0,06s system 99% cpu 1,021 total
nprof produces the following output:
Statistical profiling result from v8.log, (914 ticks, 0 unaccounted, 0 excluded).
…
[Bottom up (heavy) profile]:
Note: percentage shows a share of a particular caller in the total
amount of its parent calls.
Callers occupying less than 2.0% are not shown.
ticks parent name
741 81.1% /usr/local/bin/node
187 25.2% LazyCompile: bind native v8natives.js:1456
143 76.5% LazyCompile: *fibo /home/nchambrier/Bureau/fibo.js:5
143 100.0% Function: ~d native v8natives.js:1480
142 99.3% LazyCompile: *startup.processNextTick.process._tickCallback node.js:185
44 23.5% Function: ~next /home/nchambrier/Bureau/fibo.js:14
30 68.2% Function: ~process.nextTick.fibo.bind.n2 /home/nchambrier/Bureau/fibo.js:12
30 100.0% Function: ~d native v8natives.js:1480
30 100.0% LazyCompile: *startup.processNextTick.process._tickCallback node.js:185
14 31.8% Function: ~<anonymous> /home/nchambrier/Bureau/fibo.js:13
14 100.0% Function: ~d native v8natives.js:1480
14 100.0% LazyCompile: *startup.processNextTick.process._tickCallback node.js:185
…
I feel like the bold line is interesting, and I'm tempted to interpret it as "fibo() takes 76.5% of the whole CPU", but it's not so simple.
I'll give another try to valgrind, just in case.