Permalink
Browse files

Major refactoring thanks to @mranney.

  • Loading branch information...
1 parent 039e572 commit 97c1b6a463d5dd1d82b8a4d46e67f353697f8cb8 @dshaw committed Mar 29, 2012
Showing with 73 additions and 68 deletions.
  1. +10 −0 History.md
  2. +2 −5 package.json
  3. +1 −46 proc.js
  4. +60 −0 usage.js
  5. +0 −17 wscript
View
@@ -1,4 +1,14 @@
+0.2.0 / 2012-03-28
+==================
+
+ * Evaluate the prusage struct in pure JavaScript and don't block the event loop. (@mranney)
+
+0.1.0 / 2012-03-28
+==================
+
+ * First full full release.
+
0.0.3 / 2012-03-28
==================
View
@@ -1,10 +1,7 @@
{
"name": "proc",
- "version": "0.1.0",
+ "version": "0.2.0",
"description": "Expose system state via the /proc file system.",
"author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)",
- "main": "./proc",
- "scripts": {
- "install": "node-waf configure clean build"
- }
+ "main": "./proc"
}
View
@@ -8,49 +8,4 @@
* Exports.
*/
-function read_ts(buf, offset) {
- return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
-}
-
-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
- obj.create = read_ts(buf, 16) / obj.count; // process/lwp creation time stamp
- obj.term = read_ts(buf, 24) / obj.count; // process/lwp termination time stamp
- obj.rtime = read_ts(buf, 32) / obj.count; // total lwp real (elapsed) time
- obj.utime = read_ts(buf, 40) / obj.count; // user level cpu time
- obj.stime = read_ts(buf, 48) / obj.count; // system call cpu time
- obj.ttime = read_ts(buf, 56) / obj.count; // other system trap cpu time
- obj.tftime = read_ts(buf, 64) / obj.count; // text page fault sleep time
- obj.dftime = read_ts(buf, 72) / obj.count; // data page fault sleep time
- obj.kftime = read_ts(buf, 80) / obj.count; // kernel page fault sleep time
- obj.ltime = read_ts(buf, 88) / obj.count; // user lock wait sleep time
- obj.slptime = read_ts(buf, 96) / obj.count; // all other sleep time
- obj.wtime = read_ts(buf, 104) / obj.count; // wait-cpu (latency) time
- obj.stoptime = read_ts(buf, 112) / obj.count; // stopped time
- // filler for future expansion
- obj.minf = buf.readUInt32LE(168); // minor page faults
- obj.majf = buf.readUInt32LE(172); // major page faults
- obj.nswap = buf.readUInt32LE(176); // swaps
- obj.inblk = buf.readUInt32LE(180); // input blocks
- obj.outblk = buf.readUInt32LE(184); // output blocks
- obj.msnd = buf.readUInt32LE(188); // messages sent
- obj.mrcv = buf.readUInt32LE(192); // messages received
- obj.sigs = buf.readUInt32LE(196); // signals received
- obj.vctx = buf.readUInt32LE(200); // voluntary context switches
- obj.ictx = buf.readUInt32LE(204); // involuntary context switches
- obj.sysc = buf.readUInt32LE(208); // system calls
- obj.ioch = buf.readUInt32LE(212); // chars read and written
- // filler for future expansion
-
- callback(null, obj);
- });
-};
+exports.usage = require('./usage');
View
@@ -0,0 +1,60 @@
+/*!
+ * proc
+ * Copyright(c) 2012 Daniel D. Shaw <dshaw@dshaw.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var fs = require("fs");
+
+/**
+ * Exports.
+ */
+
+function readTimespec(buf, offset) {
+ return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
+}
+
+module.exports = function usage(callback) {
+ fs.readFile("/proc/self/usage", function (err, buf) {
+ if (err) return callback(err);
+
+ var data = {};
+ data.lwpid = buf.readUInt32LE(0); // lwp id. 0: process or defunct
+ data.count = buf.readUInt32LE(4); // number of contributing lwps
+ data.tstamp = readTimespec(buf, 8) / data.count; // current time stamp
+ data.create = readTimespec(buf, 16) / data.count; // process/lwp creation time stamp
+ data.term = readTimespec(buf, 24) / data.count; // process/lwp termination time stamp
+ data.rtime = readTimespec(buf, 32) / data.count; // total lwp real (elapsed) time
+ data.utime = readTimespec(buf, 40) / data.count; // user level cpu time
+ data.stime = readTimespec(buf, 48) / data.count; // system call cpu time
+ data.ttime = readTimespec(buf, 56) / data.count; // other system trap cpu time
+ data.tftime = readTimespec(buf, 64) / data.count; // text page fault sleep time
+ data.dftime = readTimespec(buf, 72) / data.count; // data page fault sleep time
+ data.kftime = readTimespec(buf, 80) / data.count; // kernel page fault sleep time
+ data.ltime = readTimespec(buf, 88) / data.count; // user lock wait sleep time
+ data.slptime = readTimespec(buf, 96) / data.count; // all other sleep time
+ data.wtime = readTimespec(buf, 104) / data.count; // wait-cpu (latency) time
+ data.stoptime = readTimespec(buf, 112) / data.count; // stopped time
+ // filler for future expansion
+ data.minf = buf.readUInt32LE(168); // minor page faults
+ data.majf = buf.readUInt32LE(172); // major page faults
+ data.nswap = buf.readUInt32LE(176); // swaps
+ data.inblk = buf.readUInt32LE(180); // input blocks
+ data.outblk = buf.readUInt32LE(184); // output blocks
+ data.msnd = buf.readUInt32LE(188); // messages sent
+ data.mrcv = buf.readUInt32LE(192); // messages received
+ data.sigs = buf.readUInt32LE(196); // signals received
+ data.vctx = buf.readUInt32LE(200); // voluntary context switches
+ data.ictx = buf.readUInt32LE(204); // involuntary context switches
+ data.sysc = buf.readUInt32LE(208); // system calls
+ data.ioch = buf.readUInt32LE(212); // chars read and written
+ // filler for future expansion
+
+ console.log(data)
+ callback(null, data);
+ });
+};
View
@@ -1,17 +0,0 @@
-srcdir = "."
-blddir = "build"
-VERSION = "0.0.1"
-
-def set_options(opt):
- opt.tool_options("compiler_cxx")
-
-def configure(conf):
- conf.check_tool("compiler_cxx")
- conf.check_tool("node_addon")
-
-def build(bld):
- obj = bld.new_task_gen("cxx", "shlib", "node_addon")
- # without this, eio_custom doesn't keep a ref to the req->data
- obj.cxxflags = ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"]
- obj.target = "usage"
- obj.source = "prusage.cc"

0 comments on commit 97c1b6a

Please sign in to comment.