fivdi
Posts: 155
Joined: Sun Sep 23, 2012 8:09 pm
Contact:

Node.js v0.8.11 - Installation now easy

Postby fivdi » Sat Sep 29, 2012 2:01 pm

Building Node.js v0.8.9 or lower is fairly tricky on the Raspberry Pi. Depending on the version of Node.js, various patches and/or manual edits are required. As of v0.8.10 this is no longer the case and Node.js can be built and installed as described by the Node.js installation wiki: https://github.com/joyent/node/wiki/Installation

For example, running the following six commands in a terminal will retrieve the Node.js v0.8.11 archive (wget ...), extract all files from the archive (tar ...), configure for building on the Raspberry Pi (./configure), build (make), and install Node.js (sudo make install):

wget http://nodejs.org/dist/v0.8.11/node-v0.8.11.tar.gz
tar -zxf node-v0.8.11.tar.gz
cd node-v0.8.11
./configure
make
sudo make install

You're now ready to go...

Optional Stuff

Overclocking the Raspberry Pi and running it in turbo mode reduced the build time on my system from a little over 2 hours to less than 1 hour and 20 minutes which is a fantastic improvement.

The Node.js tests can be run with the command:

make test

On my system 3 of the test failed initially:

test-child-process-fork-net2
test-eio-race
test-net-pingpong

In turbo mode test-child-process-fork-net2 can be fixed by modifying the 4th last line of test/simple/test-child-process-fork-net2.js

from:
assert.ok(timeElasped >= 190 && timeElasped <= 1000,
to:
assert.ok(timeElasped >= 190 && timeElasped <= 2000,

test-net-pingpong can be fixed by enabling ipv6 on the Raspberry Pi with the command:

modprobe ipv6

I can't figure out why test-eio-race is failing. If someone else knows please let me know.
dirkson
Posts: 5
Joined: Sat Oct 06, 2012 5:11 am

Re: Node.js v0.8.11 - Installation now easy

Postby dirkson » Sat Oct 06, 2012 5:19 am

Build worked as expected, but basic http server parsing a gpio dev sticks CPU at 98% even w/t user load. Same server under v0.6.19 behaves nicely w/ <4% during hits, and 0% when idle. v0.8.x sure would be tasty! Thoughts?

TIA,
D
fivdi
Posts: 155
Joined: Sun Sep 23, 2012 8:09 pm
Contact:

Re: Node.js v0.8.11 - Installation now easy

Postby fivdi » Sat Oct 06, 2012 9:37 am

Difficult to say without seeing some code. Sounds like setTimeout or setInterval is being used to poll GPIO pins at a high frequency. Can you post the code causing the issue? Are you using an NPM module for GPIO? If so, which one?
dirkson
Posts: 5
Joined: Sat Oct 06, 2012 5:11 am

Re: Node.js v0.8.11 - Installation now easy

Postby dirkson » Sat Oct 06, 2012 2:29 pm

I'm using the latest default everything (e.g. apt-get update; apt-get upgrade) from 9-18 wheezy. GPIO access through mod w1_gpio. Here's the code as tested on both 0.6.x via apt-get, and 0.8.11 from source:

Code: Select all


//
// rpiTemp - read DS17B20P temperature sensor using 1-wire, uses glob
//           from https://github.com/isaacs/node-glob..
//

var glob = require('glob'),
    http = require('http'),
    fs = require('fs'),
    ip = require('os').networkInterfaces()['wlan0'][0]['address'],
    rpiDev = glob.sync('/sys/bus/w1/devices/28-*/w1_slave')[0];

http.createServer(function (req, res) {
    fs.readFile(rpiDev, 'utf8', function (err, data) {
        if (err) {
            res.writeHead(500, {'ontent-Type': 'text/plain'});
            res.write(err + '\n');
            res.end();
            return;
        }
        var temp = 9 / 5 * parseFloat(data.match(/t=(.*)/g)[0].split('=')[1]) / 1000 + 32;
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write(temp.toString() + ' deg. F\n' + new Date().toString(), 'utf8');
        res.end();
    });
}).listen(1337, ip);
console.log('web server running at http://' + ip + ':1337');



TIA,
D
fivdi
Posts: 155
Joined: Sun Sep 23, 2012 8:09 pm
Contact:

Re: Node.js v0.8.11 - Installation now easy

Postby fivdi » Sat Oct 06, 2012 7:29 pm

I don't have temperature sensor to try it out but did try the following which simulates your scenario as close as I can.

Create a file called w1_slave with the following content. This simulates /sys/bus/w1/devices/28-*/w1_slave

Code: Select all

89 01 4b 46 7f ff 07 10 2d : crc=2d YES
89 01 4b 46 7f ff 07 10 2d t=24562

Modified the program to used ./w1_slave instead of /sys/bus/w1/devices/28-*/w1_slave. The modified program also uses eth0 instead of wlan0.

Code: Select all

//
// rpiTemp - read DS17B20P temperature sensor using 1-wire, uses glob
//           from https://github.com/isaacs/node-glob..
//

var glob = require('glob'),
    http = require('http'),
    fs = require('fs'),
    ip = require('os').networkInterfaces()['eth0'][0]['address'],
//    rpiDev = glob.sync('/sys/bus/w1/devices/28-*/w1_slave')[0];
    rpiDev = "./w1_slave";

http.createServer(function (req, res) {
    fs.readFile(rpiDev, 'utf8', function (err, data) {
        if (err) {
            res.writeHead(500, {'ontent-Type': 'text/plain'});
            res.write(err + '\n');
            res.end();
            return;
        }
        var temp = 9 / 5 * parseFloat(data.match(/t=(.*)/g)[0].split('=')[1]) / 1000 + 32;
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write(temp.toString() + ' deg. F\n' + new Date().toString(), 'utf8');
        res.end();
    });
}).listen(1337, ip);
console.log('web server running at http://' + ip + ':1337');

When the Web server is idle the Node process requires 0% CPU.

Navigating to the page in a browser on a different machine and holding down ctrl-r to constantly refresh the page will bring CPU usage of the Node process up to 15 to 20%

Running the following command on a different machine will bring CPU usage of the Node process up to about 40%. The command runs in 20 seconds which is about 50 requests per second. Not amazing, but ok.

Code: Select all

time for i in {1..1000}; do curl "http://<ip-address-goes-here>:1337/";done

The problem doesn't appear to be directly related to Node.
dirkson
Posts: 5
Joined: Sat Oct 06, 2012 5:11 am

Re: Node.js v0.8.11 - Installation now easy

Postby dirkson » Sat Oct 06, 2012 7:50 pm

I'll rebuild a new image and see if I can replicate the CPU spike, then test swapping out temp sensor w/ GPIP kernel module and normal file to see if there's a difference. Thanks for taking a look.

L8r,
D
fivdi
Posts: 155
Joined: Sun Sep 23, 2012 8:09 pm
Contact:

Re: Node.js v0.8.11 - Installation now easy

Postby fivdi » Sat Oct 06, 2012 8:00 pm

Good luck :)
dirkson
Posts: 5
Joined: Sat Oct 06, 2012 5:11 am

Re: Node.js v0.8.11 - Installation now easy

Postby dirkson » Sat Oct 06, 2012 10:35 pm

Figured it out -- forgot to mention one additional tidbit... I'd created the usual init.d script to start my server, and ... wait for it ... delegated the longevity management duties to my old friend node-supervisor. Commented that out and bingo -- back to expected CPU load!!! LIG, diggin v0.8.11!

I'll dig into node-supervisor a bit and see if I can see whats going on there. Its always worked great for me in my development environment (Mac), and my QA/Prod environments in the cloud.

Thanks again for the assist!

L8r,
D
dirkson
Posts: 5
Joined: Sat Oct 06, 2012 5:11 am

Re: Node.js v0.8.11 - Installation now easy

Postby dirkson » Sat Oct 06, 2012 10:48 pm

Initial test of nodejitsu/forever looks promising...

HTH,
D
varunkumar
Posts: 1
Joined: Wed Mar 20, 2013 6:52 pm

Re: Node.js v0.8.11 - Installation now easy

Postby varunkumar » Wed Mar 20, 2013 6:53 pm

michaelkoetter
Posts: 2
Joined: Tue Apr 30, 2013 7:43 am

Re: Node.js v0.8.11 - Installation now easy

Postby michaelkoetter » Tue Apr 30, 2013 7:45 am

In case you haven't seen yet, the guys at nodejs.org now provide "official" builds for the Pi:
http://nodejs.org/dist/v0.10.5/
They are not linked on the download page yet, so I don't know if they can be considered stable. Seems to work fine however :)