From fd34660d2499d07a8e1fec58edee05a3c481f40c Mon Sep 17 00:00:00 2001 From: Andrew Johnston Date: Wed, 6 Jan 2021 04:34:38 +0000 Subject: [PATCH] move memoryUsage() to core --- Makefile | 8 +++---- just.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ just.h | 4 ++++ just.js | 2 +- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index cb79fc0b2..725780241 100644 --- a/Makefile +++ b/Makefile @@ -108,11 +108,11 @@ runtime-debug: modules deps/v8/libv8_monolith.a ## build debug version of runtim make main-debug runtime-static: modules deps/v8/libv8_monolith.a ## build dynamic runtime - make MODULE=net module + make MODULE=net module-static make MODULE=sys module-static - make MODULE=epoll module - make MODULE=vm module - make MODULE=fs module + make MODULE=epoll module-static + make MODULE=vm module-static + make MODULE=fs module-static make main-static runtime-static-debug: modules deps/v8/libv8_monolith.a ## build debug version of runtime diff --git a/just.cc b/just.cc index 67c1d6699..ee6d50270 100644 --- a/just.cc +++ b/just.cc @@ -340,6 +340,76 @@ void just::Builtin(const FunctionCallbackInfo &args) { NewStringType::kNormal, b->size).ToLocalChecked()); } +ssize_t just::process_memory_usage() { + char buf[1024]; + const char* s = NULL; + ssize_t n = 0; + unsigned long val = 0; + int fd = 0; + int i = 0; + do { + fd = open("/proc/thread-self/stat", O_RDONLY); + } while (fd == -1 && errno == EINTR); + if (fd == -1) return (ssize_t)errno; + do + n = read(fd, buf, sizeof(buf) - 1); + while (n == -1 && errno == EINTR); + close(fd); + if (n == -1) + return (ssize_t)errno; + buf[n] = '\0'; + s = strchr(buf, ' '); + if (s == NULL) + goto err; + s += 1; + if (*s != '(') + goto err; + s = strchr(s, ')'); + if (s == NULL) + goto err; + for (i = 1; i <= 22; i++) { + s = strchr(s + 1, ' '); + if (s == NULL) + goto err; + } + errno = 0; + val = strtoul(s, NULL, 10); + if (errno != 0) + goto err; + return val * (unsigned long)getpagesize(); +err: + return 0; +} + + +void just::MemoryUsage(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + HandleScope handleScope(isolate); + ssize_t rss = just::process_memory_usage(); + HeapStatistics v8_heap_stats; + isolate->GetHeapStatistics(&v8_heap_stats); + Local array = args[0].As(); + Local ab = array->Buffer(); + std::shared_ptr backing = ab->GetBackingStore(); + // todo: why is this double? + uint64_t *fields = static_cast(backing->Data()); + fields[0] = rss; + fields[1] = v8_heap_stats.total_heap_size(); + fields[2] = v8_heap_stats.used_heap_size(); + fields[3] = v8_heap_stats.external_memory(); + fields[4] = v8_heap_stats.does_zap_garbage(); + fields[5] = v8_heap_stats.heap_size_limit(); + fields[6] = v8_heap_stats.malloced_memory(); + fields[7] = v8_heap_stats.number_of_detached_contexts(); + fields[8] = v8_heap_stats.number_of_native_contexts(); + fields[9] = v8_heap_stats.peak_malloced_memory(); + fields[10] = v8_heap_stats.total_available_size(); + fields[11] = v8_heap_stats.total_heap_size_executable(); + fields[12] = v8_heap_stats.total_physical_size(); + fields[13] = isolate->AdjustAmountOfExternalAllocatedMemory(0); + args.GetReturnValue().Set(array); +} + void just::Init(Isolate* isolate, Local target) { Local version = ObjectTemplate::New(isolate); SET_VALUE(isolate, version, "just", String::NewFromUtf8Literal(isolate, @@ -366,5 +436,6 @@ void just::Init(Isolate* isolate, Local target) { SET_METHOD(isolate, target, "error", Error); SET_METHOD(isolate, target, "load", Load); SET_METHOD(isolate, target, "builtin", Builtin); + SET_METHOD(isolate, target, "memoryUsage", MemoryUsage); SET_MODULE(isolate, target, "version", version); } diff --git a/just.h b/just.h index 919f58434..5ee630609 100644 --- a/just.h +++ b/just.h @@ -58,6 +58,8 @@ using v8::PromiseRejectEvent; using v8::Uint32Array; using v8::BigUint64Array; +ssize_t process_memory_usage(); + typedef void *(*register_plugin)(); struct builtin { unsigned int size; @@ -83,6 +85,8 @@ void FreeMemory(void* buf, size_t length, void* data); void UnwrapMemory(void* buf, size_t length, void* data); void FreeMappedMemory(void* buf, size_t length, void* data); +void MemoryUsage(const FunctionCallbackInfo &args); + void SET_METHOD(Isolate *isolate, Local recv, const char *name, FunctionCallback callback); void SET_MODULE(Isolate *isolate, Local diff --git a/just.js b/just.js index 8b4f6220d..033bb6101 100644 --- a/just.js +++ b/just.js @@ -306,7 +306,7 @@ function main () { just.require.cache = cache just.waitForInspector = false - just.memoryUsage = wrapMemoryUsage(just.sys.memoryUsage) + just.memoryUsage = wrapMemoryUsage(just.memoryUsage) just.cpuUsage = wrapCpuUsage(just.sys.cpuUsage) just.rUsage = wrapgetrUsage(just.sys.getrUsage) just.heapUsage = wrapHeapUsage(just.sys.heapUsage)