Ignore:
Timestamp:
Dec 13, 2018, 8:05:41 PM (6 years ago)
Author:
[email protected]
Message:

The JSC shell should listen for memory pressure events and respond to them
https://bugs.webkit.org/show_bug.cgi?id=192647

Reviewed by Keith Miller.

Source/JavaScriptCore:

We want the JSC shell to behave more like the WebContent process when
it comes to running performance tests. One way to make the shell
more like this is to have it respond to memory pressure events in
a similar way as the WebContent process. This makes it easier to run
benchmarks like JetStream2 on the CLI on iOS.

  • jsc.cpp:

(jscmain):

  • runtime/VM.cpp:

(JSC::VM::drainMicrotasks):

  • runtime/VM.h:

(JSC::VM::setOnEachMicrotaskTick):

Source/WTF:

  • wtf/MemoryPressureHandler.cpp:

(WTF::MemoryPressureHandler::MemoryPressureHandler):
(WTF::MemoryPressureHandler::setDispatchQueue):
Make it so that we can customize which dispatch queue memory pressure
events get handled on.

  • wtf/MemoryPressureHandler.h:

(WTF::MemoryPressureHandler::setShouldLogMemoryMemoryPressureEvents):
Make it so that we can disable logging that happens on each memory
pressure event.

  • wtf/cocoa/MemoryPressureHandlerCocoa.mm:

(WTF::MemoryPressureHandler::install):
(WTF::MemoryPressureHandler::uninstall):
(WTF::MemoryPressureHandler::holdOff):

File:
1 edited

Legend:

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

    r239187 r239195  
    8181#include <thread>
    8282#include <type_traits>
     83#include <wtf/Box.h>
    8384#include <wtf/CommaPrinter.h>
    8485#include <wtf/MainThread.h>
     86#include <wtf/MemoryPressureHandler.h>
    8587#include <wtf/MonotonicTime.h>
    8688#include <wtf/NeverDestroyed.h>
     
    28722874    Gigacage::disableDisablingPrimitiveGigacageIfShouldBeEnabled();
    28732875
     2876#if PLATFORM(COCOA)
     2877    auto& memoryPressureHandler = MemoryPressureHandler::singleton();
     2878    {
     2879        dispatch_queue_t queue = dispatch_queue_create("jsc shell memory pressure handler", DISPATCH_QUEUE_SERIAL);
     2880        memoryPressureHandler.setDispatchQueue(queue);
     2881        dispatch_release(queue);
     2882    }
     2883    Box<Critical> memoryPressureCriticalState = Box<Critical>::create(Critical::No);
     2884    Box<Synchronous> memoryPressureSynchronousState = Box<Synchronous>::create(Synchronous::No);
     2885    memoryPressureHandler.setLowMemoryHandler([=] (Critical critical, Synchronous synchronous) {
     2886        // We set these racily with respect to reading them from the JS execution thread.
     2887        *memoryPressureCriticalState = critical;
     2888        *memoryPressureSynchronousState = synchronous;
     2889    });
     2890    memoryPressureHandler.setShouldLogMemoryMemoryPressureEvents(false);
     2891    memoryPressureHandler.install();
     2892
     2893    auto onEachMicrotaskTick = [&] (VM& vm) {
     2894        if (*memoryPressureCriticalState == Critical::No)
     2895            return;
     2896
     2897        *memoryPressureCriticalState = Critical::No;
     2898        bool isSynchronous = *memoryPressureSynchronousState == Synchronous::Yes;
     2899
     2900        WTF::releaseFastMallocFreeMemory();
     2901        vm.deleteAllCode(DeleteAllCodeIfNotCollecting);
     2902
     2903        if (!vm.heap.isCurrentThreadBusy()) {
     2904            if (isSynchronous) {
     2905                vm.heap.collectNow(Sync, CollectionScope::Full);
     2906                WTF::releaseFastMallocFreeMemory();
     2907            } else
     2908                vm.heap.collectNowFullIfNotDoneRecently(Async);
     2909        }
     2910    };
     2911#endif
     2912
    28742913    int result = runJSC(
    28752914        options, false,
    2876         [&] (VM&, GlobalObject* globalObject, bool& success) {
     2915        [&] (VM& vm, GlobalObject* globalObject, bool& success) {
     2916#if PLATFORM(COCOA)
     2917            vm.setOnEachMicrotaskTick(WTFMove(onEachMicrotaskTick));
     2918#endif
    28772919            runWithOptions(globalObject, options, success);
    28782920        });
Note: See TracChangeset for help on using the changeset viewer.