Periodic performance loss
- Node.js Version: v14.6.3
- OS: Linux v5.6
- Scope (install, code, runtime, meta, other?): Runtime
- Module (and version) (if relevant): N/A
I've been measuring latency of traversing a tree and, eventually, toggling a GPIO. The GPIO here is not important. What I noticed is that periodically, what normally takes 2 ~ 5ms to run, jumps to 30ms+. Is there a way to verify what's happening during that time?
The behavior is very reproducible, with the very first time through the tree always takes over 20ms, then we have about 16 to 20 samples within the regular 2 ~ 5ms range, then one sample taking over 32ms, and so on.
To measure the latency, I'm using hrtime() as shown below:
let hrtime = process.hrtime();
let start = hrtime[0] * 1000000000 + hrtime[1];
tree.walk();
hrtime = process.hrtime();
let end = hrtime[0] * 1000000000 + hrtime[1];
console.log(`main: Execution took ${end - start} nS`);
Here's the snippet output of a sample run:
main: Execution took 27071049 nS
main: Execution took 5032422 nS
main: Execution took 5504323 nS
main: Execution took 4475315 nS
main: Execution took 4306362 nS
main: Execution took 4185532 nS
main: Execution took 5545781 nS
main: Execution took 3932666 nS
main: Execution took 4121701 nS
main: Execution took 4219406 nS
main: Execution took 4126451 nS
main: Execution took 4075494 nS
main: Execution took 3766712 nS
main: Execution took 3967289 nS
main: Execution took 37783035 nS
main: Execution took 5245540 nS
main: Execution took 2330800 nS
main: Execution took 2252303 nS
main: Execution took 3539886 nS
main: Execution took 2504795 nS
I tried renicing node to -20. I'm wondering if it's just garbage collection happening and stopping my application for the duration of the GC sweep, but would really like to be able to prove that's the case. Any hints are highly appreciated.
@felipebalbi You may be able to use node --trace-gc to look into when garbage collection happens and how long it takes.
@addaleax interesting, seems to match. It's odd that GC fails right when the app is booting up, though:
[5129:0x37c4790] 165 ms: Scavenge 0.9 (1.2) -> 0.6 (1.7) MB, 7.9 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
[5129:0x37c4790] 483 ms: Scavenge 1.3 (2.5) -> 1.2 (2.7) MB, 12.8 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
[5129:0x37c4790] 747 ms: Scavenge 1.7 (2.8) -> 1.5 (3.0) MB, 21.1 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
[5129:0x37c4790] 986 ms: Scavenge 2.2 (3.4) -> 2.0 (3.7) MB, 8.6 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
[5129:0x37c4790] 1196 ms: Scavenge 2.5 (3.7) -> 2.3 (3.9) MB, 8.8 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
[5129:0x37c4790] 1321 ms: Mark-sweep 2.5 (3.9) -> 1.8 (3.4) MB, 53.1 / 0.1 ms (average mu = 1.000, current mu = 1.000) external memory pressure GC in old space requested
[5129:0x37c4790] 1441 ms: Mark-sweep 1.8 (3.4) -> 1.8 (3.2) MB, 104.9 / 0.0 ms (average mu = 0.126, current mu = 0.126) external memory pressure GC in old space requested
[5129:0x37c4790] 1512 ms: Mark-sweep 1.8 (3.2) -> 1.8 (2.9) MB, 67.8 / 0.0 ms (average mu = 0.092, current mu = 0.034) external memory pressure GC in old space requested
[5129:0x37c4790] 1573 ms: Mark-sweep 1.8 (2.9) -> 1.7 (3.2) MB, 59.4 / 0.0 ms (average mu = 0.067, current mu = 0.027) external memory pressure GC in old space requested
[5129:0x37c4790] 1639 ms: Mark-sweep 1.7 (3.2) -> 1.7 (2.9) MB, 65.0 / 0.0 ms (average mu = 0.047, current mu = 0.024) external memory pressure GC in old space requested
[5129:0x37c4790] 1696 ms: Mark-sweep 1.7 (2.9) -> 1.7 (3.2) MB, 55.1 / 0.0 ms (average mu = 0.041, current mu = 0.033) external memory pressure GC in old space requested
[5129:0x37c4790] 1760 ms: Mark-sweep 1.7 (3.2) -> 1.7 (2.9) MB, 62.2 / 0.0 ms (average mu = 0.035, current mu = 0.028) external memory pressure GC in old space requested
[5129:0x37c4790] 1820 ms: Mark-sweep 1.7 (2.9) -> 1.7 (3.2) MB, 58.5 / 0.0 ms (average mu = 0.031, current mu = 0.027) external memory pressure GC in old space requested
[5129:0x37c4790] 1886 ms: Mark-sweep 1.7 (3.2) -> 1.7 (2.9) MB, 64.3 / 0.0 ms (average mu = 0.028, current mu = 0.024) external memory pressure GC in old space requested
[5129:0x37c4790] 1946 ms: Mark-sweep 1.7 (2.9) -> 1.7 (3.2) MB, 57.7 / 0.0 ms (average mu = 0.027, current mu = 0.027) external memory pressure GC in old space requested
[5129:0x37c4790] 2000 ms: Mark-sweep 1.7 (3.2) -> 1.7 (3.2) MB, 51.9 / 0.0 ms (average mu = 0.039, current mu = 0.052) external memory pressure GC in old space requested
[5129:0x37c4790] 2068 ms: Mark-sweep 1.7 (3.2) -> 1.7 (2.9) MB, 65.6 / 0.0 ms (average mu = 0.031, current mu = 0.024) external memory pressure GC in old space requested
[5129:0x37c4790] 2125 ms: Mark-sweep 1.7 (2.9) -> 1.7 (3.2) MB, 56.2 / 0.0 ms (average mu = 0.030, current mu = 0.029) external memory pressure GC in old space requested
[5129:0x37c4790] 2189 ms: Mark-sweep 1.7 (3.2) -> 1.7 (2.9) MB, 61.3 / 0.0 ms (average mu = 0.037, current mu = 0.043) external memory pressure GC in old space requested
[5129:0x37c4790] 2250 ms: Mark-sweep 1.7 (2.9) -> 1.7 (3.2) MB, 59.0 / 0.0 ms (average mu = 0.032, current mu = 0.028) external memory pressure GC in old space requested
[5129:0x37c4790] 2306 ms: Mark-sweep 1.7 (3.2) -> 1.7 (3.2) MB, 54.5 / 0.0 ms (average mu = 0.031, current mu = 0.029) external memory pressure GC in old space requested
[5129:0x37c4790] 2363 ms: Mark-sweep 1.7 (3.2) -> 1.7 (3.2) MB, 55.1 / 0.0 ms (average mu = 0.030, current mu = 0.028) external memory pressure GC in old space requested
[5129:0x37c4790] 2420 ms: Mark-sweep 1.7 (3.2) -> 1.7 (3.2) MB, 55.7 / 0.0 ms (average mu = 0.029, current mu = 0.029) external memory pressure GC in old space requested
[5129:0x37c4790] 2487 ms: Scavenge 1.7 (3.2) -> 1.7 (3.2) MB, 2.5 / 0.0 ms (average mu = 0.029, current mu = 0.029) external memory pressure
[5129:0x37c4790] 2678 ms: Mark-sweep 2.0 (3.2) -> 1.8 (3.4) MB, 60.9 / 0.1 ms (average mu = 0.629, current mu = 0.764) external memory pressure GC in old space requested
[5129:0x37c4790] 2687 ms: Scavenge 1.8 (3.4) -> 1.8 (3.9) MB, 4.3 / 0.0 ms (average mu = 0.629, current mu = 0.764) external memory pressure
[5129:0x37c4790] 2760 ms: Mark-sweep 1.8 (3.9) -> 1.8 (2.9) MB, 70.8 / 0.1 ms (average mu = 0.461, current mu = 0.141) external memory pressure GC in old space requested
[5129:0x37c4790] 2767 ms: Scavenge 1.8 (2.9) -> 1.8 (3.4) MB, 3.5 / 0.0 ms (average mu = 0.461, current mu = 0.141) external memory pressure
[5129:0x37c4790] 2830 ms: Mark-sweep 1.8 (3.4) -> 1.8 (3.2) MB, 59.7 / 0.1 ms (average mu = 0.344, current mu = 0.141) external memory pressure GC in old space requested
[5129:0x37c4790] 2833 ms: Scavenge 1.8 (3.2) -> 1.8 (3.7) MB, 1.7 / 0.0 ms (average mu = 0.344, current mu = 0.141) external memory pressure
[5129:0x37c4790] 2884 ms: Mark-sweep 1.8 (3.7) -> 1.8 (3.2) MB, 46.0 / 0.1 ms (average mu = 0.274, current mu = 0.154) external memory pressure GC in old space requested
[5129:0x37c4790] 2900 ms: Scavenge 1.8 (3.2) -> 1.8 (3.7) MB, 2.6 / 0.0 ms (average mu = 0.274, current mu = 0.154) external memory pressure
[5129:0x37c4790] 2975 ms: Mark-sweep 1.8 (3.7) -> 1.8 (2.9) MB, 69.6 / 0.1 ms (average mu = 0.254, current mu = 0.238) external memory pressure GC in old space requested
[5129:0x37c4790] 2980 ms: Scavenge 1.8 (2.9) -> 1.8 (3.4) MB, 1.1 / 0.0 ms (average mu = 0.254, current mu = 0.238) external memory pressure
[5129:0x37c4790] 3030 ms: Mark-sweep 1.8 (3.4) -> 1.8 (3.2) MB, 46.7 / 0.0 ms (average mu = 0.212, current mu = 0.147) external memory pressure GC in old space requested
[5129:0x37c4790] 3046 ms: Scavenge 1.8 (3.2) -> 1.8 (3.7) MB, 3.6 / 0.0 ms (average mu = 0.212, current mu = 0.147) external memory pressure
[5129:0x37c4790] 3107 ms: Mark-sweep 1.8 (3.7) -> 1.8 (3.2) MB, 56.9 / 0.1 ms (average mu = 0.239, current mu = 0.263) external memory pressure GC in old space requested
[5129:0x37c4790] 3111 ms: Scavenge 1.8 (3.2) -> 1.8 (3.2) MB, 1.8 / 0.0 ms (average mu = 0.239, current mu = 0.263) external memory pressure
[5129:0x37c4790] 3162 ms: Mark-sweep 1.8 (3.2) -> 1.8 (3.2) MB, 46.1 / 0.0 ms (average mu = 0.204, current mu = 0.158) external memory pressure GC in old space requested
[5129:0x37c4790] 3180 ms: Scavenge 1.8 (3.2) -> 1.8 (3.2) MB, 3.1 / 0.0 ms (average mu = 0.204, current mu = 0.158) external memory pressure
[5129:0x37c4790] 3255 ms: Mark-sweep 1.8 (3.2) -> 1.8 (2.9) MB, 70.9 / 0.1 ms (average mu = 0.223, current mu = 0.237) external memory pressure GC in old space requested
[5129:0x37c4790] 3261 ms: Scavenge 1.8 (2.9) -> 1.8 (2.9) MB, 1.4 / 0.0 ms (average mu = 0.223, current mu = 0.237) external memory pressure
[5129:0x37c4790] 3543 ms: Mark-sweep 2.2 (2.9) -> 1.9 (2.9) MB, 69.1 / 0.1 ms (average mu = 0.645, current mu = 0.760) external memory pressure GC in old space requested
[5129:0x37c4790] 3550 ms: Scavenge 1.9 (2.9) -> 1.9 (3.7) MB, 5.1 / 0.0 ms (average mu = 0.645, current mu = 0.760) external memory pressure
[5129:0x37c4790] 3621 ms: Mark-sweep 1.9 (3.7) -> 1.9 (3.4) MB, 65.2 / 0.1 ms (average mu = 0.501, current mu = 0.160) external memory pressure GC in old space requested
[5129:0x37c4790] 3625 ms: Scavenge 1.9 (3.4) -> 1.9 (3.9) MB, 2.9 / 0.1 ms (average mu = 0.501, current mu = 0.160) external memory pressure
main: Execution took 26830020 nS
main: Execution took 4937744 nS
main: Execution took 4217394 nS
main: Execution took 4177146 nS
main: Execution took 3921779 nS
main: Execution took 4114731 nS
main: Execution took 4153480 nS
main: Execution took 3857281 nS
main: Execution took 3842282 nS
main: Execution took 3971778 nS
main: Execution took 4086107 nS
main: Execution took 3951695 nS
main: Execution took 3661122 nS
main: Execution took 3829616 nS
main: Execution took 36722714 nS
[5129:0x37c4790] 11667 ms: Scavenge 2.5 (3.9) -> 2.0 (3.4) MB, 2.6 / 0.2 ms (average mu = 0.501, current mu = 0.160) allocation failure
@felipebalbi
It's odd that GC fails right when the app is booting up
Shouldn't be. GC is a reaction. The major trigger for GC would be an allocation failure, which is the principle trigger condition captured into the GC trace. Nor is it the GC that is failing! Generational GC typically hosts optimised allocation structures, but its the allocation (based on the activity directed from the application) that fails, and induces the GC cycle, i.e. its the trigger condition. During application start-up, you have lots of allocation activities occurring, because you are typically loading a lot of packages, etc. You have lots of your dependencies and sub-systems that are being initialised - not unusually concerns allocations! - to host the activity of your application, e.g. building that tree for traversal!
Can't argue with that :-) Thanks for the summary