svaarala / duktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint
MIT License
5.92k stars 514 forks source link

How to get memory used by a Duktape context? #2130

Open carlosabalde opened 5 years ago

carlosabalde commented 5 years ago

I've just added preliminary support for JavaScript execution inside Varnish Cache using Duktape and a Varnish Cache VMOD. That's going to be complementary to Lua support and lots of testing and benchmarking are still pending (high performance is critical in a Varnish Cache environment).

Since Lua / LuaJIT were already supported and Duktape takes a lot of inspiration from them, adding support for Duktape has been pretty simple. However, I've not been able to find the equivalent Duktape call for lua_gc(L, LUA_GCCOUNT, 0). That returns the current amount of memory (in Kbytes) used by a Lua context. Is there something similar for Duktape? It would be very handy for metrics, benchmarking, etc.

P.S.: please, add https://github.com/carlosabalde/libvmod-cfg/ to the list of projects using Duktape if you want :)

svaarala commented 5 years ago

There's currently no such binding, but it's been requested several times. It's a bit tedious but not complicated to implement, so I'll try to see if I can find the time this week to get an experimental API into master.

svaarala commented 5 years ago

Forgot to add that such an API can add together the requested allocation size for each heap object, but it cannot easily account for allocation overhead. This is one of the reasons I haven't needed such a binding myself -- I usually try to measure memory usage with e.g. valgrind massif or an instrumented custom pool allocator.

carlosabalde commented 5 years ago

Thanks a lot for the quick feedback! Of course I'm assuming allocation overhead won't be included. In any case, if the feature is not available at the moment, I'd keep it low priority.