node icon indicating copy to clipboard operation
node copied to clipboard

degrading performance after using child_process

Open dannymarsland opened this issue 10 years ago • 44 comments

After running a child process ( using exec or spawn ) I have found that the performance of my node.js application decreases by a factor of 10. Below is a contrived example and output.

var exec = require('child_process').exec;

function runExpensiveOperation(times) {
  while(times > 0) {
    console.time('expensiveOperation');
    var str = 'lorem';
    for ( var i=0;i< 10000000; i++) {
      // string concatenation
      str = str.length < 1000 ? str + str : '';
      // math operation
      i * i * i;
    }
    console.timeEnd('expensiveOperation');
    times--;
  }
}

console.log('PRE EXEC');
runExpensiveOperation(10);
exec('echo "hello"');
console.log('POST EXEC');
runExpensiveOperation(10);

Output:

PRE EXEC
expensiveOperation: 66.458ms
expensiveOperation: 65.735ms
expensiveOperation: 69.237ms
expensiveOperation: 65.269ms
expensiveOperation: 69.133ms
expensiveOperation: 65.639ms
expensiveOperation: 67.944ms
expensiveOperation: 63.595ms
expensiveOperation: 64.153ms
expensiveOperation: 65.093ms
POST EXEC
expensiveOperation: 715.861ms
expensiveOperation: 739.671ms
expensiveOperation: 714.546ms
expensiveOperation: 714.845ms
expensiveOperation: 745.719ms
expensiveOperation: 743.240ms
expensiveOperation: 716.481ms
expensiveOperation: 732.916ms
expensiveOperation: 736.576ms
expensiveOperation: 742.416ms

In addition, this problem only occurs if the string concatenation AND math operation are run in the expensiveOperation - if either are commented out then there is no issue.

  • Version: 5.8.0
  • Platform: Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64 ( Macbook Air OS X El Capitan )
  • Subsystem: child_process

dannymarsland avatar Mar 10 '16 02:03 dannymarsland

@matthewloring the issue still occurs when ignoring the process output

dannymarsland avatar Mar 13 '16 11:03 dannymarsland

Is it possible that the affected machines have AVX enabled and the unaffected machines don't?

bnoordhuis avatar Mar 14 '16 17:03 bnoordhuis

My machine (unaffected) has AVX/AVX2 support. @bnoordhuis How would I determine whether it is enabled?

matthewloring avatar Mar 14 '16 17:03 matthewloring

@matthewloring What (if anything) does sysctl -a | grep machdep.cpu.features | grep AVX print? AVX1 is probably harmless, AVX2 probably isn't.

bnoordhuis avatar Mar 14 '16 18:03 bnoordhuis

Maybe make that sysctl -a | grep machdep.cpu | grep -i avx, there can be more than one sysctl.

EDIT: Or even just sysctl -a | grep -i avx.

bnoordhuis avatar Mar 14 '16 18:03 bnoordhuis

AVX1.0 and AVX2 both appear to be present.

Full Output:

machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
machdep.cpu.leaf7_features: SMEP ERMS RDWRFSGS TSC_THREAD_OFFSET BMI1 AVX2 BMI2 INVPCID FPU_CSDS

matthewloring avatar Mar 14 '16 18:03 matthewloring

Jeremiah@Jeremiahs-MacBook-Pro ~/D/node> sysctl -a | grep -i avx
hw.optional.avx1_0: 1
hw.optional.avx2_0: 0
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC POPCNT AES PCID XSAVE OSXSAVE TSCTMR AVX1.0 RDRAND F16C

Edit: Looks like I only have AXV1?

Fishrock123 avatar Mar 14 '16 18:03 Fishrock123

hmmm both are enabled for me, and I was unable to reproduce the problem on this machine:

$ sysctl -a | grep -i avx
hw.optional.avx1_0: 1
hw.optional.avx2_0: 1
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
machdep.cpu.leaf7_features: SMEP ERMS RDWRFSGS TSC_THREAD_OFFSET BMI1 HLE AVX2 BMI2 INVPCID RTM FPU_CSDS

evanlucas avatar Mar 14 '16 18:03 evanlucas

My (affected) machine only has AVX1, as well:

hw.optional.avx1_0: 1
hw.optional.avx2_0: 0

machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC POPCNT AES PCID XSAVE OSXSAVE TSCTMR AVX1.0 RDRAND F16C
machdep.cpu.leaf7_features: SMEP ERMS RDWRFSGS
machdep.cpu.extfeatures: SYSCALL XD EM64T LAHF RDTSCP TSCI

okabsd avatar Mar 14 '16 22:03 okabsd

Okay, then it's probably not AVX-related. Some versions of OS X 10.9 and 10.10 suffer from performance degradation when mixing AVX and UNIX signals, but I think (although I'm not 100% sure) that's only with AVX2. V8 started emitting AVX instructions in 4.2 so that seemed almost too much of a coincidence.

bnoordhuis avatar Mar 15 '16 08:03 bnoordhuis

I don't have AVX2 too and affected.

sysctl -a | grep -i avx                                                                                                                $
hw.optional.avx1_0: 1
hw.optional.avx2_0: 0
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC POPCNT AES PCID XSAVE OSXSAVE TSCTMR AVX1.0

Isn't that everyone that don't have AVX2 affected? Isn't it like binary is compiled to use those instructions enabled, but problem happen on CPUs that don't have those?

S-YOU avatar Mar 15 '16 11:03 S-YOU

Folks that can reproduce this, can you try with the --noenable-avx flag and see if that changes anything?

ofrobots avatar Mar 15 '16 15:03 ofrobots

@ofrobots yes, that fixes it. Also, this is fixed somewhere in master, so it must indeed be a v8 bug.

Fishrock123 avatar Mar 15 '16 16:03 Fishrock123

Tentatively marking this lts-watch to keep an eye on it just in case we need to fix this in v4 also

jasnell avatar Mar 15 '16 16:03 jasnell

Perhaps this is related? https://codereview.chromium.org/878063002 V8 issue: https://bugs.chromium.org/p/v8/issues/detail?id=3846

targos avatar Mar 15 '16 16:03 targos

@ofrobots yes it fixes the issue for me too. @ofrobots what are the side effects of using --noenable-avx ?

dannymarsland avatar Mar 15 '16 19:03 dannymarsland

--noenable-avx tells V8 to avoid generating AVX instructions. The JIT compilers behave as if you are running on a processor that doesn't support AVX instructions. Using this option may reduce performance if your workload is sensitive to this feature.

Would folks mind trying the following nightly builds:

  • https://nodejs.org/download/nightly/v6.0.0-nightly201603024c724dd439/ – Mar02 – should have V8 4.8
  • https://nodejs.org/download/nightly/v6.0.0-nightly20160119df4d209ad5/ – Jan19 – should have V8 4.7

This will help narrow down when this got fixed in V8.

ofrobots avatar Mar 15 '16 22:03 ofrobots

@ofrobots Some data, with and without --noenable-avx. Appears to be fixed in the Mar02 build.

Jan19:

Oka at XI in ~/D/node-v6.0.0-nightly20160119df4d209ad5
↪ ./node --version
v6.0.0-pre
Oka at XI in ~/D/node-v6.0.0-nightly20160119df4d209ad5               
↪ ./node ~/osx.js
PRE EXEC
expensiveOperation: 56.145ms
expensiveOperation: 48.590ms
expensiveOperation: 45.105ms
expensiveOperation: 46.103ms
expensiveOperation: 46.482ms
expensiveOperation: 44.786ms
expensiveOperation: 44.023ms
expensiveOperation: 44.009ms
expensiveOperation: 50.380ms
expensiveOperation: 44.237ms
POST EXEC
expensiveOperation: 550.309ms
expensiveOperation: 552.331ms
expensiveOperation: 551.128ms
expensiveOperation: 547.652ms
expensiveOperation: 548.428ms
expensiveOperation: 545.683ms
expensiveOperation: 545.651ms
expensiveOperation: 551.198ms
expensiveOperation: 548.487ms
expensiveOperation: 550.837ms
Oka at XI in ~/D/node-v6.0.0-nightly20160119df4d209ad5               
↪ ./node --noenable-avx ~/osx.js
PRE EXEC
expensiveOperation: 53.805ms
expensiveOperation: 45.973ms
expensiveOperation: 43.450ms
expensiveOperation: 47.712ms
expensiveOperation: 48.413ms
expensiveOperation: 45.503ms
expensiveOperation: 46.244ms
expensiveOperation: 48.230ms
expensiveOperation: 48.879ms
expensiveOperation: 48.132ms
POST EXEC
expensiveOperation: 50.016ms
expensiveOperation: 47.327ms
expensiveOperation: 73.101ms
expensiveOperation: 46.448ms
expensiveOperation: 47.300ms
expensiveOperation: 49.577ms
expensiveOperation: 45.852ms
expensiveOperation: 49.045ms
expensiveOperation: 49.020ms
expensiveOperation: 45.724ms

Mar02:

Oka at XI in ~/D/node-v6.0.0-nightly201603024c724dd439
↪ ./node --version
v6.0.0-pre
Oka at XI in ~/D/node-v6.0.0-nightly201603024c724dd439               
↪ ./node ~/osx.js
PRE EXEC
expensiveOperation: 52.884ms
expensiveOperation: 46.866ms
expensiveOperation: 46.788ms
expensiveOperation: 45.380ms
expensiveOperation: 49.307ms
expensiveOperation: 42.706ms
expensiveOperation: 45.491ms
expensiveOperation: 48.018ms
expensiveOperation: 46.618ms
expensiveOperation: 49.353ms
POST EXEC
expensiveOperation: 46.085ms
expensiveOperation: 50.011ms
expensiveOperation: 45.186ms
expensiveOperation: 51.053ms
expensiveOperation: 45.617ms
expensiveOperation: 45.710ms
expensiveOperation: 43.065ms
expensiveOperation: 49.217ms
expensiveOperation: 46.529ms
expensiveOperation: 50.416ms
Oka at XI in ~/D/node-v6.0.0-nightly201603024c724dd439               
↪ ./node --noenable-avx ~/osx.js
PRE EXEC
expensiveOperation: 53.757ms
expensiveOperation: 47.107ms
expensiveOperation: 48.006ms
expensiveOperation: 49.681ms
expensiveOperation: 48.921ms
expensiveOperation: 49.080ms
expensiveOperation: 49.982ms
expensiveOperation: 53.690ms
expensiveOperation: 48.214ms
expensiveOperation: 49.141ms
POST EXEC
expensiveOperation: 44.735ms
expensiveOperation: 46.640ms
expensiveOperation: 48.634ms
expensiveOperation: 48.363ms
expensiveOperation: 48.377ms
expensiveOperation: 47.957ms
expensiveOperation: 48.650ms
expensiveOperation: 48.903ms
expensiveOperation: 47.977ms
expensiveOperation: 47.929ms

okabsd avatar Mar 16 '16 04:03 okabsd

@Okahyphen thanks a lot for the data.

Here's the V8 bug corresponding to this issue: https://bugs.chromium.org/p/v8/issues/detail?id=4406. This is a mac specific issue in how signals interact with applications that use a mix of AVX and SSE instructions (although I would be interested in knowing if anyone on BSDs, on similar hardware, is also affected).

There are quite a few CLs attached to that bug that will need to be backported if we want to fix this in v4.x and v5.x. A simpler work-around might be to force the use of --noenable-avx on mac on AVX1 hardware.

ofrobots avatar Mar 16 '16 18:03 ofrobots

--noenable_avx seems like a reasonable approach, although I suppose it might be hampered by proper bug detection. You don't want to disable it on unaffected machines.

(Although performance on OS X is of less concern because it's not what people deploy their production software on.)

bnoordhuis avatar Mar 20 '16 19:03 bnoordhuis