Skip to content
Browse files

add process.getMemoryUsage()

  • Loading branch information...
1 parent 6cacff8 commit 8c216549b2bb7c96d4b6fcd6e6dd52d0c7dc1f23 @gfx committed Aug 9, 2013
Showing with 35 additions and 5 deletions.
  1. +2 −1 .gitignore
  2. +3 −0 .gitmodules
  3. +5 −3 Makefile
  4. +1 −0 libuv
  5. +24 −1 main.c
View
3 .gitignore
@@ -2,4 +2,5 @@
*.swp
nore
*.jsx.js
-
+lib/
+include/
View
3 .gitmodules
@@ -0,0 +1,3 @@
+[submodule "libuv"]
+ path = libuv
+ url = https://github.com/joyent/libuv.git
View
8 Makefile
@@ -1,5 +1,5 @@
-OPTIMIZE := -O4
+OPTIMIZE := -O2
all: jshello jsxhello
@@ -9,6 +9,8 @@ jshello: nore
jsxhello: nore
JSX_RUNJS=./nore jsx --run hello.jsx foo bar
-nore: main.c
- $(CC) $(OPTIMIZE) -Wall -Wextra -g -framework JavaScriptCore -o $@ $<
+nore: main.c lib/libuv.a
+ $(CC) $(OPTIMIZE) -Iinclude -Wall -Wextra -g -framework JavaScriptCore -framework CoreFoundation -framework CoreServices -o $@ $< lib/libuv.a
+lib/libuv.a:
+ prefix=$$PWD ; cd libuv && sh autogen.sh && ./configure --prefix=$$prefix && make install
1 libuv
@@ -0,0 +1 @@
+Subproject commit e4c1483b48c4d3eb6e1692aed8c2e183386d2c0a
View
25 main.c
@@ -5,6 +5,9 @@
#include <unistd.h>
#include <JavaScriptCore/JavaScriptCore.h>
+
+#include "uv.h"
+
#define UNUSED __attribute__((unused))
const char* const CommandTitle = "nore";
@@ -75,7 +78,7 @@ printToStderrFunc(
}
static JSValueRef
-setTimeoutFunc(
+setTimeoutFunc( // TODO use an actual event loop like UV
JSContextRef ctx,
JSObjectRef jobj UNUSED,
JSObjectRef jobjThis,
@@ -127,6 +130,25 @@ setFunc(JSContextRef ctx, JSObjectRef obj, const char* name, JSObjectCallAsFunct
JSStringRelease(jstrName);
}
+static JSValueRef
+getMemoryUsage(
+ JSContextRef ctx,
+ JSObjectRef jobj UNUSED,
+ JSObjectRef jobjThis UNUSED,
+ size_t argLen UNUSED,
+ const JSValueRef args[] UNUSED,
+ JSValueRef* exception) {
+
+ size_t rss = 0;
+ int const err UNUSED = uv_resident_set_memory(&rss); // see node/src/node.cc
+
+ JSObjectRef const retval = JSObjectMake(ctx, NULL, NULL);
+ setProperty(ctx, retval, "rss", JSValueToObject(ctx, JSValueMakeNumber(ctx, rss), exception));
+
+ return retval;
+}
+
+
static void
skipShebang(FILE* const fp) {
int c = fgetc(fp);
@@ -164,6 +186,7 @@ setupJSGlobals(JSContextRef ctx, JSObjectRef jobjGlobal, int argc, const char**
setProperty(ctx, jsProcess, "env", JSObjectMake(ctx, NULL, NULL));
setProperty(ctx, jsProcess, "title", makeJSObjectFromCString(ctx, CommandTitle));
setProperty(ctx, jsProcess, "version", makeJSObjectFromCString(ctx, CommandVersion));
+ setFunc(ctx, jsProcess, "memoryUsage", getMemoryUsage);
// process.argv
JSValueRef jvals[argc];

0 comments on commit 8c21654

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