diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5862b50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.swp +.DS_Store +.idea +nohup.out +npm-debug.log +node_modules +*.pem +*.srl diff --git a/package.json b/package.json new file mode 100644 index 0000000..319cd90 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "proc", + "version": "0.0.1", + "description": "Solaris process info", + "author": "Daniel D. Shaw (http://dshaw.com)", + "main": "./build/Release/prusage", + "scripts": { + "install": "node-waf configure clean build" + } +} \ No newline at end of file diff --git a/prusage.cc b/prusage.cc new file mode 100644 index 0000000..b4ff97e --- /dev/null +++ b/prusage.cc @@ -0,0 +1,86 @@ +#include "node.h" + +#include /* getpagesize() */ +#include /* getexecname() */ +#include /* strncpy() */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64) +#define PROCFS_FILE_OFFSET_BITS_HACK 1 +#undef _FILE_OFFSET_BITS +#else +#define PROCFS_FILE_OFFSET_BITS_HACK 0 +#endif + +#include + +#if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1) +#define _FILE_OFFSET_BITS 64 +#endif + + +using namespace std; +using namespace node; +using namespace v8; + + +static Handle GetMsnd(const Arguments &args); +static Handle GetIoch(const Arguments &args); +extern "C" void init (Handle); + + +static Handle GetMsnd(const Arguments &args) { + HandleScope scope; + prusage_t prusage; + ulong_t msnd; + int fd; + + if ((fd = open("/proc/self/usage", O_RDONLY)) < 0) + return scope.Close(Integer::New(-1)); + + if (read(fd, &prusage, sizeof (prusage_t)) != sizeof (prusage_t)) { + (void) close(fd); + return scope.Close(Integer::New(-1)); + } + + msnd = (ulong_t) prusage.pr_msnd; + (void) close(fd); + + return scope.Close(Integer::New(msnd)); +} + +static Handle GetIoch(const Arguments &args) { + HandleScope scope; + prusage_t prusage; + ulong_t ioch; + int fd; + + if ((fd = open("/proc/self/usage", O_RDONLY)) < 0) + return scope.Close(Integer::New(-1)); + + if (read(fd, &prusage, sizeof (prusage_t)) != sizeof (prusage_t)) { + (void) close(fd); + return scope.Close(Integer::New(-1)); + } + + ioch = (ulong_t) prusage.pr_ioch; + (void) close(fd); + + return scope.Close(Integer::New(ioch)); +} + +extern "C" void init (Handle target) { + HandleScope scope; + NODE_SET_METHOD(target, "msnd", GetMsnd); + NODE_SET_METHOD(target, "ioch", GetIoch); +} \ No newline at end of file diff --git a/test/usage.test.js b/test/usage.test.js new file mode 100644 index 0000000..c2b1a02 --- /dev/null +++ b/test/usage.test.js @@ -0,0 +1,25 @@ +var net = require('net') + , usage = require('..'); + +var msnd = usage.msnd() + , ioch = usage.ioch(); + +console.dir(usage); + +var client; + +for (var i=0; i < 3; i++) { + client = net.connect(80, 'google.com', function() { //'connect' listener + console.log('client connected'); + client.write('world!\r\n'); + }); + client.on('data', function(data) { + //console.log(data.toString(), 'msnd', msnd, 'ioch', ioch); + console.log('msnd', msnd, 'ioch', ioch); + client.end(); + }); + client.on('end', function() { + console.log('client disconnected'); + console.log('msnd', msnd, 'ioch', ioch); + }); +} \ No newline at end of file diff --git a/wscript b/wscript new file mode 100644 index 0000000..882a095 --- /dev/null +++ b/wscript @@ -0,0 +1,17 @@ +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 = "prusage" + obj.source = "prusage.cc" \ No newline at end of file