Permalink
Browse files

Add error handling and callback.

  • Loading branch information...
1 parent 40f32b5 commit 3d51c871a575cdd72e2ed09106b3a1f5582021be @mranney mranney committed Mar 29, 2012
Showing with 10 additions and 7 deletions.
  1. +3 −4 Readme.md
  2. +7 −3 proc.js
View
@@ -14,10 +14,9 @@ Expose `/proc/self/usage`
var proc = require('proc');
- setTimeout(function () {
- var usage = proc.usage();
- console.log(usage);
- }, 2500);
+ proc.usage(function (err, usage_obj) {
+ console.dir(usage_obj);
+ });
## Underlying data structure
View
10 proc.js
@@ -12,10 +12,14 @@ function read_ts(buf, offset) {
return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
}
-exports.usage = function usage2() {
+exports.usage = function usage(callback) {
var fs = require("fs"), file, obj = {};
file = fs.readFile("/proc/self/usage", function (err, buf) {
+ if (err) {
+ return callback(err);
+ }
+
obj.lwpid = buf.readUInt32LE(0); // lwp id. 0: process or defunct
obj.count = buf.readUInt32LE(4); // number of contributing lwps
obj.tstamp = read_ts(buf, 8) / obj.count; // current time stamp
@@ -46,7 +50,7 @@ exports.usage = function usage2() {
obj.sysc = buf.readUInt32LE(208); // system calls
obj.ioch = buf.readUInt32LE(212); // chars read and written
// filler for future expansion
- console.log(require("util").inspect(buf.slice(208)));
- console.dir(obj);
+
+ callback(null, obj);
});
};

0 comments on commit 3d51c87

Please sign in to comment.