├── .npmignore ├── index.js ├── .gitignore ├── binding.gyp ├── Makefile ├── .travis.yml ├── examples ├── example.js └── express.js ├── README.md ├── package.json ├── tests └── test.js ├── LICENSE └── getrusage.cc /.npmignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .git 3 | build 4 | .lock-wscript 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = require('bindings')('getrusage.node') 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .lock-wscript 3 | *.Makefile 4 | *.target.gyp.mk 5 | out 6 | node_modules 7 | gyp-mac-tool 8 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | 'targets': [ 3 | { 4 | 'target_name': 'getrusage', 5 | 'include_dirs': [ 6 | "" 32 | } 33 | ], 34 | "bugs": { 35 | "url": "https://github.com/davglass/node-getrusage/issues" 36 | }, 37 | "license": "BSD", 38 | "repositories": [ 39 | { 40 | "type": "git", 41 | "url": "git://github.com/davglass/node-getrusage.git" 42 | } 43 | ], 44 | "main": "./index.js", 45 | "dependencies": { 46 | "bindings": "*", 47 | "nan": "^2.0.9" 48 | }, 49 | "devDependencies": { 50 | "vows": "*" 51 | }, 52 | "scripts": { 53 | "install": "make build", 54 | "test": "./node_modules/.bin/vows --spec ./tests/*.js" 55 | }, 56 | "engines": { 57 | "node": ">=0.6.0" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | var vows = require('vows'), 2 | assert = require('assert'), 3 | proc = require('../'), 4 | waste = function() { 5 | for (var i =0; i < 900000; i++) { 6 | //Nothing here 7 | } 8 | }; 9 | 10 | vows.describe('Test Loading and Bindings').addBatch({ 11 | 'Should be loaded': { 12 | topic: function () { 13 | return proc 14 | }, 15 | 'should have 3 methods': function (topic) { 16 | assert.isFunction(topic.usage); 17 | assert.isFunction(topic.getcputime); 18 | assert.isFunction(topic.getsystemtime); 19 | }, 20 | 'proc.usage() should return an object': function(topic) { 21 | var usage = topic.usage(); 22 | var keys = [ 'utime','stime','maxrss','ixrss','idrss','isrss','minflt','majflt','nswap','inblock','oublock','msgsnd','msgrcv','nsignals','nvcsw','nivcsw']; 23 | assert.isObject(usage); 24 | assert.deepEqual(Object.keys(usage), keys); 25 | }, 26 | 'proc.getcputime() should return a number': function(topic) { 27 | waste(); 28 | var time = topic.getcputime(); 29 | assert.isNumber(time); 30 | assert.isTrue((time > 0)); 31 | }, 32 | 'proc.getsystemtime() should return a number': function(topic) { 33 | waste(); 34 | var time = topic.getsystemtime(); 35 | assert.isNumber(time); 36 | assert.isTrue((time > 0)); 37 | } 38 | } 39 | }).export(module); 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2007, Dav Glass . 4 | All rights reserved. 5 | 6 | Redistribution and use of this software in source and binary forms, with or without modification, are 7 | permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above 10 | copyright notice, this list of conditions and the 11 | following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above 14 | copyright notice, this list of conditions and the 15 | following disclaimer in the documentation and/or other 16 | materials provided with the distribution. 17 | 18 | * The name of Dav Glass may not be used to endorse or promote products 19 | derived from this software without specific prior 20 | written permission of Dav Glass. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 23 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 28 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | -------------------------------------------------------------------------------- /getrusage.cc: -------------------------------------------------------------------------------- 1 | /* 2 | */ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace Nan; 15 | 16 | 17 | NAN_METHOD(GetCPUTime) { 18 | struct rusage ru; 19 | getrusage(RUSAGE_SELF, &ru); 20 | 21 | info.GetReturnValue().Set(New( 22 | (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec * 1e-6 + 23 | (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec * 1e-6)); 24 | } 25 | 26 | NAN_METHOD(GetSystemTime) { 27 | struct rusage ru; 28 | getrusage(RUSAGE_SELF, &ru); 29 | 30 | info.GetReturnValue().Set(New( 31 | (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec * 1e-6)); 32 | } 33 | 34 | static v8::Handle timevalToNumber(struct timeval &tim) { 35 | return New((double)tim.tv_sec + (double)tim.tv_usec * 1e-6); 36 | } 37 | 38 | NAN_METHOD(GetUsage) { 39 | EscapableHandleScope scope; 40 | 41 | struct rusage ru; 42 | getrusage(RUSAGE_SELF, &ru); 43 | 44 | v8::Local output = New(); 45 | 46 | #define FIELD(name, conv) \ 47 | Set(output, New(#name).ToLocalChecked(), conv(ru.ru_##name)) 48 | 49 | FIELD(utime, timevalToNumber); /* user time used */ 50 | FIELD(stime, timevalToNumber); /* system time used */ 51 | FIELD(maxrss, New); /* maximum resident set size */ 52 | FIELD(ixrss, New); /* integral shared memory size */ 53 | FIELD(idrss, New); /* integral unshared data size */ 54 | FIELD(isrss, New); /* integral unshared stack size */ 55 | FIELD(minflt, New); /* page reclaims */ 56 | FIELD(majflt, New); /* page faults */ 57 | FIELD(nswap, New); /* swaps */ 58 | FIELD(inblock, New); /* block input operations */ 59 | FIELD(oublock, New); /* block output operations */ 60 | FIELD(msgsnd, New); /* messages sent */ 61 | FIELD(msgrcv, New); /* messages received */ 62 | FIELD(nsignals, New); /* signals received */ 63 | FIELD(nvcsw, New); /* voluntary context switches */ 64 | FIELD(nivcsw, New); /* involuntary context switches */ 65 | 66 | #undef FIELD 67 | 68 | info.GetReturnValue().Set(scope.Escape(output)); 69 | } 70 | 71 | NAN_MODULE_INIT(Init) { 72 | SetMethod(target, "usage", GetUsage); 73 | SetMethod(target, "getcputime", GetCPUTime); 74 | SetMethod(target, "getsystemtime", GetSystemTime); 75 | } 76 | 77 | NODE_MODULE(getrusage, Init) 78 | --------------------------------------------------------------------------------