Ignore:
Timestamp:
Sep 20, 2018, 4:46:43 PM (7 years ago)
Author:
[email protected]
Message:

Add functions to measure memory footprint to JSC
https://bugs.webkit.org/show_bug.cgi?id=189768

Reviewed by Saam Barati.

Rolling this back in.

Provide system memory metrics for the current process to aid in memory reduction measurement and
tuning using native JS tests.

  • jsc.cpp:

(MemoryFootprint::now):
(MemoryFootprint::resetPeak):
(GlobalObject::finishCreation):
(JSCMemoryFootprint::JSCMemoryFootprint):
(JSCMemoryFootprint::createStructure):
(JSCMemoryFootprint::create):
(JSCMemoryFootprint::finishCreation):
(JSCMemoryFootprint::addProperty):
(functionResetMemoryPeak):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/jsc.cpp

    r236260 r236293  
    128128#endif
    129129
     130#if __has_include(<WebKitAdditions/MemoryFootprint.h>)
     131#include <WebKitAdditions/MemoryFootprint.h>
     132#else
     133struct MemoryFootprint {
     134    uint64_t current;
     135    uint64_t peak;
     136   
     137    static MemoryFootprint now()
     138    {
     139        return { 0L, 0L };
     140    }
     141   
     142    static void resetPeak()
     143    {
     144    }
     145};
     146#endif
     147
    130148#if !defined(PATH_MAX)
    131149#define PATH_MAX 4096
     
    268286static EncodedJSValue JSC_HOST_CALL functionForceGCSlowPaths(ExecState*);
    269287static EncodedJSValue JSC_HOST_CALL functionHeapSize(ExecState*);
     288static EncodedJSValue JSC_HOST_CALL functionCreateMemoryFootprint(ExecState*);
     289static EncodedJSValue JSC_HOST_CALL functionResetMemoryPeak(ExecState*);
    270290static EncodedJSValue JSC_HOST_CALL functionAddressOf(ExecState*);
    271291static EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*);
     
    485505        addFunction(vm, "forceGCSlowPaths", functionForceGCSlowPaths, 0);
    486506        addFunction(vm, "gcHeapSize", functionHeapSize, 0);
     507        addFunction(vm, "MemoryFootprint", functionCreateMemoryFootprint, 0);
     508        addFunction(vm, "resetMemoryPeak", functionResetMemoryPeak, 0);
    487509        addFunction(vm, "addressOf", functionAddressOf, 1);
    488510        addFunction(vm, "version", functionVersion, 1);
     
    11801202}
    11811203
     1204class JSCMemoryFootprint : public JSDestructibleObject {
     1205    using Base = JSDestructibleObject;
     1206public:
     1207    JSCMemoryFootprint(VM& vm, Structure* structure)
     1208        : Base(vm, structure)
     1209    { }
     1210
     1211    static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
     1212    {
     1213        return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
     1214    }
     1215
     1216    static JSCMemoryFootprint* create(VM& vm, JSGlobalObject* globalObject)
     1217    {
     1218        Structure* structure = createStructure(vm, globalObject, jsNull());
     1219        JSCMemoryFootprint* footprint = new (NotNull, allocateCell<JSCMemoryFootprint>(vm.heap, sizeof(JSCMemoryFootprint))) JSCMemoryFootprint(vm, structure);
     1220        footprint->finishCreation(vm);
     1221        return footprint;
     1222    }
     1223
     1224    void finishCreation(VM& vm)
     1225    {
     1226        Base::finishCreation(vm);
     1227
     1228        auto addProperty = [&] (VM& vm, const char* name, JSValue value) {
     1229            JSCMemoryFootprint::addProperty(vm, name, value);
     1230        };
     1231
     1232        MemoryFootprint footprint = MemoryFootprint::now();
     1233
     1234        // Report sizes in KBytes so that values up to GB are still integers.
     1235        addProperty(vm, "current", jsNumber(footprint.current / 1024));
     1236        addProperty(vm, "peak", jsNumber(footprint.peak / 1024));
     1237    }
     1238
     1239    DECLARE_INFO;
     1240
     1241private:
     1242    void addProperty(VM& vm, const char* name, JSValue value)
     1243    {
     1244        Identifier identifier = Identifier::fromString(&vm, name);
     1245        putDirect(vm, identifier, value);
     1246    }
     1247};
     1248
     1249const ClassInfo JSCMemoryFootprint::s_info = { "MemoryFootprint", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCMemoryFootprint) };
     1250
     1251EncodedJSValue JSC_HOST_CALL functionCreateMemoryFootprint(ExecState* exec)
     1252{
     1253    VM& vm = exec->vm();
     1254    JSLockHolder lock(vm);
     1255    return JSValue::encode(JSCMemoryFootprint::create(vm, exec->lexicalGlobalObject()));
     1256}
     1257
     1258EncodedJSValue JSC_HOST_CALL functionResetMemoryPeak(ExecState*)
     1259{
     1260    MemoryFootprint::resetPeak();
     1261    return JSValue::encode(jsUndefined());
     1262}
     1263
    11821264// This function is not generally very helpful in 64-bit code as the tag and payload
    11831265// share a register. But in 32-bit JITed code the tag may not be checked if an
Note: See TracChangeset for help on using the changeset viewer.