⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 279495 in webkit


Ignore:
Timestamp:
Jul 1, 2021, 8:14:26 PM (5 years ago)
Author:
Chris Dumez
Message:

[macOS] Suspend WebProcesses that are in the process cache
​https://bugs.webkit.org/show_bug.cgi?id=227269

Reviewed by Geoffrey Garen.

Suspend WebProcesses that are in the process cache on macOS to make sure they use no CPU.

  • UIProcess/WebProcessProxy.cpp:

(WebKit::WebProcessProxy::setIsInProcessCache):
(WebKit::WebProcessProxy::platformSuspendProcess):
(WebKit::WebProcessProxy::platformResumeProcess):

  • UIProcess/WebProcessProxy.h:
  • UIProcess/mac/WebProcessProxyMac.mm:

(WebKit::WebProcessProxy::platformSuspendProcess):
(WebKit::WebProcessProxy::platformResumeProcess):

  • WebProcess/WebProcess.cpp:

(WebKit::WebProcess::setIsInProcessCache):

  • WebProcess/WebProcess.h:
  • WebProcess/WebProcess.messages.in:
Location:
trunk/Source
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/workers/service/context/SWContextManager.cpp

    r278253 r279495  
    144144            completionHandler();
    145145
     146        if (m_pendingServiceWorkerTerminationRequests.isEmpty()) {
     147            for (auto& handler : std::exchange(m_whenTerminationRequestsAreDoneHandlers, { }))
     148                handler();
     149        }
     150
    146151        // Spin the runloop before releasing the worker thread proxy, as there would otherwise be
    147152        // a race towards its destruction.
    … …  
    150155}
    151156
     157void SWContextManager::whenTerminationRequestsAreDone(CompletionHandler<void()>&& completionHandler)
     158{
     159    if (m_pendingServiceWorkerTerminationRequests.isEmpty())
     160        return completionHandler();
     161
     162    m_whenTerminationRequestsAreDoneHandlers.append(WTFMove(completionHandler));
     163}
     164
    152165void SWContextManager::forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>& apply)
    153166{
  • trunk/Source/WebCore/workers/service/context/SWContextManager.h

    r278253 r279495  
    108108    static constexpr Seconds syncWorkerTerminationTimeout { 100_ms }; // Only used by layout tests.
    109109
     110    WEBCORE_EXPORT void whenTerminationRequestsAreDone(CompletionHandler<void()>&&);
     111
    110112private:
    111113    SWContextManager() = default;
    … …  
    129131    };
    130132    HashMap<ServiceWorkerIdentifier, std::unique_ptr<ServiceWorkerTerminationRequest>> m_pendingServiceWorkerTerminationRequests;
     133    Vector<CompletionHandler<void()>> m_whenTerminationRequestsAreDoneHandlers;
    131134};
    132135
  • trunk/Source/WebKit/ChangeLog

    r279494 r279495  
     12021-07-01  Chris Dumez  <cdumez@apple.com>
     2
     3        [macOS] Suspend WebProcesses that are in the process cache
     4        https://bugs.webkit.org/show_bug.cgi?id=227269
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Suspend WebProcesses that are in the process cache on macOS to make sure they use no CPU.
     9
     10        * UIProcess/WebProcessProxy.cpp:
     11        (WebKit::WebProcessProxy::setIsInProcessCache):
     12        (WebKit::WebProcessProxy::platformSuspendProcess):
     13        (WebKit::WebProcessProxy::platformResumeProcess):
     14        * UIProcess/WebProcessProxy.h:
     15        * UIProcess/mac/WebProcessProxyMac.mm:
     16        (WebKit::WebProcessProxy::platformSuspendProcess):
     17        (WebKit::WebProcessProxy::platformResumeProcess):
     18        * WebProcess/WebProcess.cpp:
     19        (WebKit::WebProcess::setIsInProcessCache):
     20        * WebProcess/WebProcess.h:
     21        * WebProcess/WebProcess.messages.in:
     22
    1232021-07-01  Alex Christensen  <achristensen@webkit.org>
    224
  • trunk/Source/WebKit/UIProcess/WebProcessProxy.cpp

    r279411 r279495  
    290290    m_isInProcessCache = value;
    291291
    292     send(Messages::WebProcess::SetIsInProcessCache(m_isInProcessCache), 0);
     292    if (!m_isInProcessCache)
     293        platformResumeProcess();
     294
     295    sendWithAsyncReply(Messages::WebProcess::SetIsInProcessCache(m_isInProcessCache), [weakThis = makeWeakPtr(*this), isEnteringProcessCache = value]() mutable {
     296        if (isEnteringProcessCache && weakThis && weakThis->m_isInProcessCache)
     297            weakThis->platformSuspendProcess();
     298    });
    293299
    294300    if (m_isInProcessCache) {
    … …  
    20042010}
    20052011
     2012#if !PLATFORM(MAC)
     2013void WebProcessProxy::platformSuspendProcess()
     2014{
     2015}
     2016
     2017void WebProcessProxy::platformResumeProcess()
     2018{
     2019}
     2020#endif
     2021
    20062022} // namespace WebKit
    20072023
  • trunk/Source/WebKit/UIProcess/WebProcessProxy.h

    r279411 r279495  
    472472    void processDidTerminateOrFailedToLaunch(ProcessTerminationReason);
    473473
     474    void platformSuspendProcess();
     475    void platformResumeProcess();
     476
    474477    // IPC::Connection::Client
    475478    friend class WebConnectionToWebProcess;
  • trunk/Source/WebKit/UIProcess/mac/WebProcessProxyMac.mm

    r279411 r279495  
    3232#import "WKFullKeyboardAccessWatcher.h"
    3333#import <Kernel/kern/cs_blobs.h>
     34#import <signal.h>
    3435#import <wtf/ProcessPrivilege.h>
    3536#import <wtf/spi/cocoa/SecuritySPI.h>
    … …  
    7980}
    8081
     82void WebProcessProxy::platformSuspendProcess()
     83{
     84    RELEASE_LOG(Process, "%p - [PID=%i] WebProcessProxy::platformSuspendProcess", this, processIdentifier());
     85    ALLOW_DEPRECATED_DECLARATIONS_BEGIN
     86    if (auto* connection = this->connection())
     87        xpc_connection_kill(connection->xpcConnection(), SIGSTOP);
     88    ALLOW_DEPRECATED_DECLARATIONS_END
     89}
     90
     91void WebProcessProxy::platformResumeProcess()
     92{
     93    RELEASE_LOG(Process, "%p - [PID=%i] WebProcessProxy::platformResumeProcess", this, processIdentifier());
     94    ALLOW_DEPRECATED_DECLARATIONS_BEGIN
     95    if (auto* connection = this->connection())
     96        xpc_connection_kill(connection->xpcConnection(), SIGCONT);
     97    ALLOW_DEPRECATED_DECLARATIONS_END
     98}
     99
    81100} // namespace WebKit
    82101
  • trunk/Source/WebKit/WebProcess/WebProcess.cpp

    r279477 r279495  
    615615}
    616616
    617 void WebProcess::setIsInProcessCache(bool isInProcessCache)
     617void WebProcess::setIsInProcessCache(bool isInProcessCache, CompletionHandler<void()>&& completionHandler)
    618618{
    619619#if PLATFORM(COCOA)
    … …  
    630630    UNUSED_PARAM(isInProcessCache);
    631631#endif
     632
     633#if ENABLE(SERVICE_WORKER)
     634    if (isInProcessCache) {
     635        // When calling the completion handler, the parent process may suspend us. We do not want to get suspended until pending requests to
     636        // stop service workers are processed because this would be wasteful from a memory standpoint. Also, service worker termination
     637        // requests have a timeout and this timeout would be reached if we suspended while it is being processed.
     638        SWContextManager::singleton().whenTerminationRequestsAreDone(WTFMove(completionHandler));
     639        return;
     640    }
     641#endif
     642
     643    completionHandler();
    632644}
    633645
  • trunk/Source/WebKit/WebProcess/WebProcess.h

    r279411 r279495  
    421421
    422422    void setHasSuspendedPageProxy(bool);
    423     void setIsInProcessCache(bool);
     423    void setIsInProcessCache(bool, CompletionHandler<void()>&&);
    424424    void markIsNoLongerPrewarmed();
    425425
  • trunk/Source/WebKit/WebProcess/WebProcess.messages.in

    r279411 r279495  
    106106
    107107    SetHasSuspendedPageProxy(bool hasSuspendedPageProxy);
    108     SetIsInProcessCache(bool isInProcessCache)
     108    SetIsInProcessCache(bool isInProcessCache) -> () Async
    109109    MarkIsNoLongerPrewarmed()
    110110    GetActivePagesOriginsForTesting() -> (Vector<String> activeOrigins) Async
Note: See TracChangeset for help on using the changeset viewer.