node-supervisor icon indicating copy to clipboard operation
node-supervisor copied to clipboard

CPU spike for 2500 watched files in v0.8.8

Open nemtsov opened this issue 14 years ago • 44 comments

When watching a large number of files (a bit more than 2500 in my specific case) with supervisor -e 'js|mu' app the CPU spikes to 60% on MBP 2.2Ghz. I repeated the same with forever --spinSleepTime 10 --watch --watchDirectory . start app.js and the CPU remained roughly at 0%.

nemtsov avatar Aug 24 '12 20:08 nemtsov

What OS are you using?

iangreenleaf avatar Aug 24 '12 20:08 iangreenleaf

OS X 10.8.1 (on a MacBook Pro Early 2011, 2.2 GHz Intel Core i7, 8 GB 1333 MHz DDR3, 750 GB SATA)

nemtsov avatar Aug 24 '12 20:08 nemtsov

Thanks. It's probably the file-watching code that's to blame, since it is recursing through and watching every individual file. I'm still wanting to revisit that code - I've just been dragging my feet to see what shakes out of watchFile and watch in core.

iangreenleaf avatar Aug 24 '12 20:08 iangreenleaf

Same thing happened for me with run.js, was suspicious but I let it go and instead fixed my ignoring of files in gitignore, most notably the node modules folder which generally has tons of files in it.

Would be healthy to figure out what changed in core that makes it less efficient than before, I think.

DTrejo avatar Sep 16 '12 16:09 DTrejo

specifying limited files/dirs to monitor via -w helps, got cpu usage down to ~20% from ~60%.

arieljake avatar Oct 08 '12 21:10 arieljake

iangreenleaf,

I've just been dragging my feet to see what shakes out of watchFile if you point me in the right direction, i can have a look

arieljake avatar Oct 08 '12 21:10 arieljake

I look into the watch vs. watchFile stuff periodically, and always end up hitting some sticking point or area of uncertainty. The requirements for The One Solution To Rule Them All are:

  • Cross-platform compatible
  • Uses inotify and other OS hooks when available (falls back to polling otherwise)
  • File change notification can provide the name of the file
  • Watches directories recursively (or else doesn't have performance problems)
  • Is not deprecated in Node core

I've also been wanting to check in with the core team to see what their future plans are for the fs module - if they're planning to keep both of those in the API, or if one is on the way out. The docs at least look better now than the last time I checked.

iangreenleaf avatar Oct 08 '12 21:10 iangreenleaf

If you want inotify etc, then use fs.watch. If you want filename on notifications, then you're not going to get that (on all platforms) by calling fs.watch() on a directory. File-watching is one part of node where we've just had to accept that true platform independence is not really achievable.

isaacs avatar Oct 08 '12 21:10 isaacs

Re: future plans, fs is very stable. You can rely on it.

isaacs avatar Oct 08 '12 21:10 isaacs

If you want inotify etc, then use fs.watch. If you want filename on notifications, then you're not going to get that (on all platforms) by calling fs.watch() on a directory. File-watching is one part of node where we've just had to accept that true platform independence is not really achievable.

I suspected that might be the answer. So by my reckoning, moving to fs.watch would solve the performance issues but would leave Mac users without the -e option (AFAIK that's the only feature that truly depends on the filename).

I'm also wondering if filename support could be patched on inelegantly in JS land by manually checking mtimes or something - that kind of solution would be good enough for the scale we're using it. Probably worth looking to see if someone has implemented this somewhere.

iangreenleaf avatar Oct 08 '12 21:10 iangreenleaf

up does something similar to what's suggested in their watch implementation. They pre-process the files to watch, and then use fs.watch on each file. That implementation doesn't seem to cause any performance issues on OSX. If you're pre-processing the list of files to watch, it seems like you'd still be able to filter out by --extension or --ignore folders as it currently does.

selfcontained avatar Oct 09 '12 03:10 selfcontained

Interesting. I ran into limitations before with watch having a bound on the number of files it could handle (see #34). I'm testing again and not seeing that limitation any more, so perhaps that option is worth revisiting.

iangreenleaf avatar Oct 09 '12 06:10 iangreenleaf

On my fork I switched it to just use watch() and it's been running smoothly without any cpu overhead. Granted, I'm only running on OSX, so I'm oblivious to what any of the issues on Windows would be.

selfcontained avatar Oct 09 '12 15:10 selfcontained

I tried @bmharris fork and I got some improvements on OS X 10.7.5. For some reason the CPU usage was showing some really weird values (130% !)

PID   COMMAND      %CPU  TIME     #TH  #WQ  #POR #MRE RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP PPID STATE    UID
3245  node         130.9 00:13.76 6/3  0    42   143  14M+   12M    23M+   95M    3014M  3245 311  running  501

With mbharris fork, this went down to around 30%:

PID   COMMAND      %CPU  TIME     #TH  #WQ  #POR #MRE RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP PPID STATE    UID
3369  node         33.6 00:22.58 6     0    42   145  20M    12M    29M    92M    3007M  3369 311  sleeping 501  11372   385    175      86       5832606+ 333      111483

This is still a lot, though, for ~350 files

$ find . -name '*.js' | wc
     356     356   21662

gregerolsson avatar Oct 23 '12 19:10 gregerolsson

Thanks for the benchmark. I agree, that's better but still not ideal. I'm planning to start putting together a new major release that's 0.8 only, so I'll use that opportunity to work on this. On Oct 23, 2012 12:12 PM, "gregerolsson" [email protected] wrote:

I tried @bmharris https://github.com/bmharris fork and I got some improvements on OS X 10.7.5. For some reason the CPU usage was showing some really weird values (130% !)

PID COMMAND %CPU TIME #TH #WQ #POR #MRE RPRVT RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID 3245 node 130.9 00:13.76 6/3 0 42 143 14M+ 12M 23M+ 95M 3014M 3245 311 running 501

With mbharris fork, this went down to around 30%:

PID COMMAND %CPU TIME #TH #WQ #POR #MRE RPRVT RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID 3369 node 33.6 00:22.58 6 0 42 145 20M 12M 29M 92M 3007M 3369 311 sleeping 501 11372 385 175 86 5832606+ 333 111483

This is still a lot, though, for ~350 files

$ find . -name '*.js' | wc 356 356 21662

— Reply to this email directly or view it on GitHubhttps://github.com/isaacs/node-supervisor/issues/71#issuecomment-9714251.

iangreenleaf avatar Oct 23 '12 20:10 iangreenleaf

Any further update / workaround for this issue?

Tried @bmharris and the spike were a bit lower, but still there significantly.

chakrit avatar Jan 14 '13 06:01 chakrit

For anyone else having this issue, I have had more success with Python's watchdog which have a very similar feature set to node-supervisor.

$ easy_install pip
$ pip install watchdog

It'll install a watchmedo command for you, check watchmedo auto-restart --help for docs.

I have it aliased as supervise so it is just one command away:

$ alias supervise='watchmedo auto-restart -d . -p "*.node;*.js;*.coffee"'
$ supervise node server.js

... which works perfectly without any noticable CPU spike at all.

Also works great with build tools like cake make or grunt directly. Example for a grunt lint on supervise:

$ supervise grunt lint # re-lint on every file changes.

chakrit avatar Jan 14 '13 06:01 chakrit

@chakrit watchdog is a stable tool. No CPU overcharged anymore. Many thanks.

Lu79 avatar Feb 20 '13 12:02 Lu79

watchmedo needed just one more parameter to scan my subdirectories, --recursive, so my alias looks like this:

alias supervise='watchmedo auto-restart -d . --recursive -p "*.js;*.node;*.coffee"'

Awesome tip @chakrit really made a difference for me! /cc @Lu79

thanpolas avatar Feb 25 '13 17:02 thanpolas

Wow am I glad I found this thread. Great tips from both @thanpolas and @chakrit. I experienced this problem today on Win7 x32 with Node 0.8. For any Windows users battling this same problem, install Python, easy_install (setuptools), and pip as suggested above. Next create a blank file at %Windir%\System32\supervise.bat. Put the following command in that batch file:

watchmedo auto-restart -d . --recursive -p "*.js;*.node;*.coffee" %1 %2

EDIT: ^^ Incorrect code here. See my comment below.

Then to actually supervise your app you would run the same command that @chakrit listed above:

supervise node server.js

jimthedev avatar Mar 06 '13 04:03 jimthedev