Skip to content

Commit

Permalink
Merge pull request #4 from dshaw/proc-psinfo
Browse files Browse the repository at this point in the history
Adding psinfo
  • Loading branch information
bahamas10 committed Jul 9, 2012
2 parents bfdb071 + 7acbaaa commit 592b44e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 12 deletions.
82 changes: 73 additions & 9 deletions Readme.md
Expand Up @@ -8,15 +8,28 @@ This was written for Voxer to use on Joyent. It has not been tested outside of S

## usage

Expose `/proc/self/usage`
Expose elements of `/proc`

## Usage usage (Oh, so meta.)
### Usage usage (Oh, so meta.)

var proc = require('proc');
``` js
var proc = require('proc');

proc.usage(function (err, usage_obj) {
console.dir(usage_obj);
});
```

### Usage psinfo

``` js
var proc = require('proc');

proc.psinfo(function (err, psinfo_t) {
console.dir(psinfo_t);
});
```

proc.usage(function (err, usage_obj) {
console.dir(usage_obj);
});

## CLI install

Expand All @@ -32,7 +45,8 @@ This will produce a JSON string, so it works great with something like [jsontool

## Underlying data structure

<pre>
### usage
``` c
/*
* Resource usage.
*/
Expand Down Expand Up @@ -68,7 +82,57 @@ typedef struct prusage {
ulong_t pr_ioch; /* chars read and written */
ulong_t filler[10]; /* filler for future expansion */
} prusage_t;
</pre>
```

### psinfo
``` c
/*
* process ps(1) information file. /proc/<pid>/psinfo
*/
#define PRARGSZ 80 /* number of chars of arguments */
typedef struct psinfo {
int pr_flag; /* process flags (DEPRECATED; do not use) */
int pr_nlwp; /* number of active lwps in the process */
pid_t pr_pid; /* unique process id */
pid_t pr_ppid; /* process id of parent */
pid_t pr_pgid; /* pid of process group leader */
pid_t pr_sid; /* session id */
uid_t pr_uid; /* real user id */
uid_t pr_euid; /* effective user id */
gid_t pr_gid; /* real group id */
gid_t pr_egid; /* effective group id */
uintptr_t pr_addr; /* address of process */
size_t pr_size; /* size of process image in Kbytes */
size_t pr_rssize; /* resident set size in Kbytes */
size_t pr_pad1;
dev_t pr_ttydev; /* 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) */
ushort_t pr_pctcpu; /* % of recent cpu time used by all lwps */
ushort_t pr_pctmem; /* % of system memory used by process */
timestruc_t pr_start; /* process start time, from the epoch */
timestruc_t pr_time; /* usr+sys cpu time for this process */
timestruc_t pr_ctime; /* usr+sys cpu time for reaped children */
char pr_fname[PRFNSZ]; /* name of execed file */
char pr_psargs[PRARGSZ]; /* initial characters of arg list */
int pr_wstat; /* if zombie, the wait() status */
int pr_argc; /* initial argument count */
uintptr_t pr_argv; /* address of initial argument vector */
uintptr_t pr_envp; /* address of initial environment vector */
char pr_dmodel; /* data model of the process */
char pr_pad2[3];
taskid_t pr_taskid; /* task id */
projid_t pr_projid; /* project id */
int pr_nzomb; /* number of zombie lwps in the process */
poolid_t pr_poolid; /* pool id */
zoneid_t pr_zoneid; /* zone id */
id_t pr_contract; /* process contract */
int pr_filler[1]; /* reserved for future use */
lwpsinfo_t pr_lwp; /* information for representative lwp */
} psinfo_t;
```


## License

Expand All @@ -93,4 +157,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions bin/proc-cli.js
Expand Up @@ -32,6 +32,7 @@ function help (cmd) {
, ''
, 'examples:'
, ' proc usage 10008'
, ' proc psinfo'
, ' proc version'
].join('\n')
}
Expand Down
67 changes: 67 additions & 0 deletions lib/psinfo.js
@@ -0,0 +1,67 @@
/*!
* 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);
}

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);
});
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"name": "proc",
"version": "0.2.2",
"description": "Expose system state via the /proc file system.",
"keywords": ["proc", "prusage", "ptime", "smartos", "solaris"],
"keywords": ["illumos", "proc", "prusage", "ptime", "smartos", "solaris"],
"author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)",
"contributors": [
"Matt Ranney",
Expand All @@ -18,4 +18,4 @@
},
"main": "./proc",
"bin": { "proc": "./bin/proc-cli.js" }
}
}
3 changes: 2 additions & 1 deletion proc.js
Expand Up @@ -9,4 +9,5 @@
*/

exports.version = require('./package.json').version;
exports.usage = require('./lib/usage');
exports.usage = require('./lib/usage');
exports.psinfo = require('./lib/psinfo');

0 comments on commit 592b44e

Please sign in to comment.