Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% CPU utilization #7

Closed
mattiash opened this issue Nov 20, 2012 · 5 comments
Closed

100% CPU utilization #7

mattiash opened this issue Nov 20, 2012 · 5 comments

Comments

@mattiash
Copy link

For even the simplest programs using 'gpio', node uses 100% CPU on my Raspberry PI with Raspbian. For example:

var gpio = require('gpio');

var gpio4 = gpio.export( 4, 
                         { direction: "out", 
                           ready: function() { 
                               gpio4.set();
                               console.log( "4 exported" ) } } );

setInterval( function() { console.log("ping"); }, 5000 );

started with

sudo node test.js

results in 100% CPU usage. Am I doing something wrong? How can I debug this?

@EugenePikalov
Copy link

I have the same issue. It is because of fast check changes in FileWatcher. You need to change node_modules/gpio/gpio.js line 62
interval = 1;
to
interval = 100;

And CPU will utilization be 5%

@vicary
Copy link

vicary commented Nov 26, 2012

+1 @EugenePikalov thanks for that, was dived into the C code for some time with no luck.

@EnotionZ
Copy link
Owner

You would think it's fine checking the contents of a file each ms, especially when the file contains only 1 byte. But yeah, I never intended for that to be a permanent solution. I rolled out the custom filewatcher because node fs's watcher looks for metadata changes on the file and doesn't work properly when we manually change the gpio header via hardware.

I used interval = 1 because I wanted to get close to realtime. Doesn't make sense to drive a robot that responds too slowly. But then again, no one in their right mind would write an autonomous robot in JavaScript. For now, I think I'll add the ability to set the FileWatcher interval in the API and default it to 100ms. Thanks Eugene for pointing this out.

I'll look at some low level tools to catch file changes without having to read the file itself; should significantly reduce cpu usage even at small intervals (a coworker of mine mentioned inotify). I'll keep this issue open until I can figure that out. If you guys have any ideas, I'd love to hear it!

@vicary
Copy link

vicary commented Dec 6, 2012

I smell kernel drivers here, linux has it's controversial ground on hardware triggers (a.k.a. interrupt).

@EnotionZ
Copy link
Owner

EnotionZ commented Apr 5, 2013

I'm gonna close this issue. With e306269 the default interval is now 100 and there's an option to set the interval when exporting a pin.

@EnotionZ EnotionZ closed this as completed Apr 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants