Skip to content
Browse files

Modifying for use with destruct

  • Loading branch information...
1 parent 592b44e commit 30fe47c052b790ac99589ac83389ffdec453e502 @bahamas10 bahamas10 committed Jul 17, 2012
Showing with 97 additions and 107 deletions.
  1. +10 −0 lib/cred.js
  2. +58 −0 lib/lib.js
  3. +7 −58 lib/psinfo.js
  4. +17 −48 lib/usage.js
  5. +4 −1 package.json
  6. +1 −0 proc.js
View
10 lib/cred.js
@@ -0,0 +1,10 @@
+/**
+ * Parse /proc/<pid>/cred
+ */
+var parseProc = require('./lib').parseProc,
+ keys = [ 'euid', 'ruid', 'suid', 'egid', 'rgid', 'sgid', 'ngroups',
+ 'groups' ];
+
+module.exports = function(pid, callback) {
+ parseProc('cred', 'iiiiiiii', keys, [], pid, callback);
+};
View
58 lib/lib.js
@@ -0,0 +1,58 @@
+/*!
+ * parseProc
+ * MIT Licensed
+ */
+
+var fs = require('fs'),
+ destruct = require('destruct');
+
+// Read time spec of a given buffer
+function readTimespec(buf, offset) {
+ return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
+}
+
+/**
+ * Generic proc parsing function
+ *
+ * @param file file in proc to parse. ex 'usage', 'psinfo', etc.
+ * @param fmt format for binary unpacking of the file
+ * @param keys keys to map to the resultant binary unpacking operation
+ * @parama timespec_keys keys to run the readTimespec function on
+ * @param pid pid of the process, defaults to 'self'
+ * @param callback callback with the results (err, res)
+ */
+module.exports.parseProc = function(file, fmt, keys, timespec_keys, pid, callback) {
+ if (typeof pid === 'function') {
+ callback = pid;
+ pid = 'self';
+ }
+
+ fs.readFile('/proc/'+pid+'/'+file, function (err, buf) {
+ if (err) return callback(err);
+
+ // Unpack the values
+ var unpacked = destruct.unpack(fmt, buf);
+
+ // Merge the keys array with the unpacked values to make an associative array
+ var ret = {};
+ for (var i = 0; i < keys.length; i++) {
+ ret[keys[i]] = unpacked[i];
+ }
+
+ // Delete padding
+ delete ret['pad'];
+
+ // Convert time specs
+ timespec_keys.forEach(function(key) {
+ ret[key] = readTimespec(ret[key], 0);
+ });
+
+ // Check for buffers that need to be toString'd
+ keys.forEach(function(key) {
+ ret[key] = (typeof ret[key] === 'object') ? ret[key].toString() : ret[key];
+ });
+
+ // Callback
+ callback(null, ret);
+ });
+};
View
65 lib/psinfo.js
@@ -2,66 +2,15 @@
* psinfo
* MIT Licensed
* reference: http://src.illumos.org/source/xref/illumos-gate/usr/src/uts/common/sys/procfs.h#275
- *
*/
-var fs = require('fs');
-
-function readTimespec(buf, offset) {
- return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
-}
+var parseProc = require('./lib').parseProc,
+ timespec_keys = [ 'ctime', 'time', 'start' ],
+ keys = ['flag', 'nlwp', 'pid', 'ppid', 'pgid', 'sid', 'uid', 'euid', 'gid',
+ 'egid', 'addr', 'size', 'rssize', 'pad', 'ttydev', 'pctcpu', 'pctmem', 'start',
+ 'time', 'ctime', 'fname', 'psargs', 'wstat', 'argc', 'argv', 'envp',
+ 'dmodel', 'pad', 'taskid', 'projid', 'nzomb', 'poolid', 'zoneid', 'contract', 'filler'];
module.exports = function(pid, callback) {
- if (typeof pid === 'function') {
- callback = pid;
- pid = 'self';
- }
-
- fs.readFile('/proc/'+pid+'/psinfo', function (err, buf) {
- if (err) return callback(err);
-
- var data = {}, offset = 0;
- data.flag = buf.readInt32LE(offset); offset+=4; /* process flags (DEPRECATED; do not use) */
- data.nlwp = buf.readInt32LE(offset); offset+=4; /* number of active lwps in the process */
- data.pid = buf.readInt32LE(offset); offset+=4; /* unique process id */
- data.ppid = buf.readInt32LE(offset); offset+=4; /* process id of parent */
- data.pgid = buf.readInt32LE(offset); offset+=4; /* pid of process group leader */
- data.sid = buf.readInt32LE(offset); offset+=4; /* session id */
- data.uid = buf.readInt32LE(offset); offset+=4; /* real user id */
- data.euid = buf.readInt32LE(offset); offset+=4; /* effective user id */
- data.gid = buf.readInt32LE(offset); offset+=4; /* real group id */
- data.egid = buf.readInt32LE(offset); offset+=4; /* effective group id */
- data.addr = buf.readUInt32LE(offset); offset+=4; /* address of process */
- data.size = buf.readInt32LE(offset); offset+=4; /* size of process image in Kbytes */
- data.rssize = buf.readInt32LE(offset); offset+=4; /* resident set size in Kbytes */
- offset += 4; /* pad1 */
- data.ttydev = buf.readInt32LE(offset); offset+=4; /* controlling tty device (or PRNODEV) */
- /* The following percent numbers are 16-bit binary */
- /* fractions [0 .. 1] with the binary point to the */
- /* right of the high-order bit (1.0 == 0x8000) */
- data.pctcpu = buf.readUInt16LE(offset); offset+=2; /* % of recent cpu time used by all lwps */
- data.pctmem = buf.readUInt16LE(offset); offset+=2; /* % of system memory used by process */
- data.start = readTimespec(buf, offset); offset+=8; /* process start time, from the epoch */
- data.time = readTimespec(buf, offset); offset+=8; /* usr+sys cpu time for this process */
- data.ctime = readTimespec(buf, offset); offset+=8; /* usr+sys cpu time for reaped children */
- data.fname = buf.toString('ascii', offset, offset+=16) /* name of execed file */
- .split('\u0000')[0];
- data.psargs = buf.toString('ascii', offset, offset+=80) /* initial characters of arg list */
- .split('\u0000')[0];
- data.wstat = buf.readInt32LE(offset); offset+=4; /* if zombie, the wait() status */
- data.argc = buf.readInt32LE(offset); offset+=4; /* initial argument count */
- data.argv = buf.readUInt32LE(offset); offset+=4; /* address of initial argument vector */
- data.envp = buf.readUInt32LE(offset); offset+=4; /* address of initial environment vector */
- data.dmodel = buf.toString('ascii', offset, offset+=1); /* data model of the process */
- offset += 3; /* pad2 */
- data.taskid = buf.readInt32LE(offset); offset+=4; /* task id */
- data.projid = buf.readInt32LE(offset); offset+=4; /* project id */
- data.nzomb = buf.readInt32LE(offset); offset+=4; /* number of zombie lwps in the process */
- data.poolid = buf.readInt32LE(offset); offset+=4; /* pool id */
- data.zoneid = buf.readInt32LE(offset); offset+=4; /* zone id */
- data.contract = buf.readInt32LE(offset); offset+=4; /* process contract */
- offset += 1; /* filler */
-
- callback(null, data);
- });
+ parseProc('psinfo', 'iiiiiiiiiiIiiiiSSa8a8a8Z16Z80iiIIaa3iiiiii', keys, timespec_keys, pid, callback);
};
View
65 lib/usage.js
@@ -4,60 +4,29 @@
* MIT Licensed
*/
-/**
- * Module dependencies.
- */
-
-var fs = require('fs');
-
-/**
- * Exports.
- */
+var parseProc = require('./lib').parseProc,
+ timespec_keys = [ 'tstamp', 'create', 'term', 'rtime', 'utime', 'stime',
+ 'ttime', 'tftime', 'dftime', 'kftime', 'ltime', 'slptime', 'wtime',
+ 'stoptime' ],
+ keys = ['lwpid', 'count', 'tstamp', 'create', 'term', 'rtime', 'utime',
+ 'stime', 'ttime', 'tftime', 'dftime', 'kftime', 'ltime', 'slptime',
+ 'wtime', 'stoptime', 'pad', 'minf', 'majf', 'nswap', 'inblk',
+ 'outblk', 'msnd', 'mrcv', 'sigs', 'vctx', 'ictx', 'sysc', 'ioch',
+ 'pad'];
-function readTimespec(buf, offset) {
- return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
-}
-
-module.exports = function usage (pid, callback) {
+module.exports = function(pid, callback) {
if (typeof pid === 'function') {
callback = pid;
pid = 'self';
}
-
- fs.readFile('/proc/'+pid+'/usage', function (err, buf) {
+ parseProc('psinfo', 'iia8a8a8a8a8a8a8a8a8a8a8a8a8a8a48LLLLLLLLLLLLa40', keys, timespec_keys, pid, function(err, res) {
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); // 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); // user level cpu time
- data.stime = readTimespec(buf, 48); // system call cpu time
- data.ttime = readTimespec(buf, 56); // other system trap cpu time
- data.tftime = readTimespec(buf, 64); // text page fault sleep time
- data.dftime = readTimespec(buf, 72); // data page fault sleep time
- data.kftime = readTimespec(buf, 80); // kernel page fault sleep time
- data.ltime = readTimespec(buf, 88); // user lock wait sleep time
- data.slptime = readTimespec(buf, 96); // all other sleep time
- data.wtime = readTimespec(buf, 104); // wait-cpu (latency) time
- data.stoptime = readTimespec(buf, 112); // 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
- callback(null, data);
+ // divide by #lwp
+ res.create = res.create / res.count;
+ res.term = res.term / res.count;
+ res.rtime = res.rtime / res.count;
+
+ callback(null, res);
});
};
View
5 package.json
@@ -17,5 +17,8 @@
"url": "http://github.com/dshaw/proc/issues"
},
"main": "./proc",
- "bin": { "proc": "./bin/proc-cli.js" }
+ "bin": { "proc": "./bin/proc-cli.js" },
+ "dependencies": {
+ "destruct": "~ 0.0.0"
+ }
}
View
1 proc.js
@@ -11,3 +11,4 @@
exports.version = require('./package.json').version;
exports.usage = require('./lib/usage');
exports.psinfo = require('./lib/psinfo');
+exports.cred = require('./lib/cred');

0 comments on commit 30fe47c

Please sign in to comment.
Something went wrong with that request. Please try again.