Skip to content

Commit

Permalink
Bug 1627075 - OMT and OMP StartupCache access r=froydnj
Browse files Browse the repository at this point in the history
The overall goal of this patch is to make the StartupCache accessible anywhere.
There's two main pieces to that equation:

1. Allowing it to be accessed off main thread, which means modifying the
   mutex usage to ensure that all data accessed from non-main threads is
   protected.
2. Allowing it to be accessed out of the chrome process, which means passing
   a handle to a shared cache buffer down to child processes.

Number 1 is somewhat fiddly, but it's all generally straightforward work. I'll
hope that the comments and the code are sufficient to explain what's going on
there.

Number 2 has some decisions to be made:
- The first decision was to pass a handle to a frozen chunk of memory down to
  all child processes, rather than passing a handle to an actual file. There's
  two reasons for this: 1) since we want to compress the underlying file on
  disk, giving that file to child processes would mean they have to decompress
  it themselves, eating CPU time. 2) since they would have to decompress it
  themselves, they would have to allocate the memory for the decompressed
  buffers, meaning they cannot all simply share one big decompressed buffer.

  - The drawback of this decision is that we have to load and decompress the
    buffer up front, before we spawn any child processes. We attempt to
    mitigate this by keeping track of all the entries that child processes
    access, and only including those in the frozen decompressed shared buffer.

  - We base our implementation of this approach off of the shared preferences
    implementation. Hopefully I got all of the pieces to fit together
    correctly. They seem to work in local testing and on try, but I think
    they require a set of experienced eyes looking carefully at them.

- Another decision was whether to send the handles to the buffers over IPC or
  via command line. We went with the command line approach, because the startup
  cache would need to be accessed very early on in order to ensure we do not
  read from any omnijars, and we could not make that work via IPC.

  - Unfortunately this means adding another hard-coded FD, similar to
    kPrefMapFileDescriptor. It seems like at the very least we need to rope all
    of these together into one place, but I think that should be filed as a
    follow-up?

Lastly, because this patch is a bit of a monster to review - first, thank you
for looking at it, and second, the reason we're invested in this is because we
saw a >10% improvement in cold startup times on reference hardware, with a p
value less than 0.01. It's still not abundantly clear how reference hardware
numbers translate to numbers on release, and they certainly don't translate
well to Nightly numbers, but it's enough to convince me that it's worth some
effort.

Depends on D78584

Differential Revision: https://phabricator.services.mozilla.com/D77635
  • Loading branch information
squarewave committed Jul 7, 2020
1 parent 51a40f3 commit 5ff30b6
Show file tree
Hide file tree
Showing 19 changed files with 1,331 additions and 237 deletions.
18 changes: 18 additions & 0 deletions dom/ipc/ContentChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#include "mozilla/plugins/PluginInstanceParent.h"
#include "mozilla/plugins/PluginModuleParent.h"
#include "mozilla/RemoteLazyInputStreamChild.h"
#include "mozilla/scache/StartupCacheChild.h"
#include "mozilla/widget/ScreenManager.h"
#include "mozilla/widget/WidgetMessageUtils.h"
#include "nsBaseDragService.h"
Expand Down Expand Up @@ -1920,6 +1921,23 @@ mozilla::ipc::IPCResult ContentChild::RecvPScriptCacheConstructor(
return IPC_OK();
}

scache::PStartupCacheChild* ContentChild::AllocPStartupCacheChild(
const bool& wantCacheData) {
return new scache::StartupCacheChild();
}

bool ContentChild::DeallocPStartupCacheChild(
scache::PStartupCacheChild* cache) {
delete static_cast<scache::StartupCacheChild*>(cache);
return true;
}

mozilla::ipc::IPCResult ContentChild::RecvPStartupCacheConstructor(
scache::PStartupCacheChild* actor, const bool& wantCacheData) {
static_cast<scache::StartupCacheChild*>(actor)->Init(wantCacheData);
return IPC_OK();
}

PNeckoChild* ContentChild::AllocPNeckoChild() { return new NeckoChild(); }

mozilla::ipc::IPCResult ContentChild::RecvNetworkLinkTypeChange(
Expand Down
7 changes: 7 additions & 0 deletions dom/ipc/ContentChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ class ContentChild final : public PContentChild,
PScriptCacheChild*, const FileDescOrError& cacheFile,
const bool& wantCacheData) override;

PStartupCacheChild* AllocPStartupCacheChild(const bool& wantCacheData);

bool DeallocPStartupCacheChild(PStartupCacheChild*);

virtual mozilla::ipc::IPCResult RecvPStartupCacheConstructor(
PStartupCacheChild*, const bool& wantCacheData) override;

PNeckoChild* AllocPNeckoChild();

bool DeallocPNeckoChild(PNeckoChild*);
Expand Down
18 changes: 18 additions & 0 deletions dom/ipc/ContentParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "mozilla/ProcessHangMonitorIPC.h"
#include "mozilla/RDDProcessManager.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/ScriptPreloader.h"
#include "mozilla/Services.h"
#include "mozilla/Sprintf.h"
Expand Down Expand Up @@ -155,6 +156,7 @@
#include "mozilla/plugins/PluginBridge.h"
#include "mozilla/RemoteLazyInputStreamParent.h"
#include "mozilla/widget/ScreenManager.h"
#include "mozilla/scache/StartupCacheParent.h"
#include "nsAnonymousTemporaryFile.h"
#include "nsAppRunner.h"
#include "nsCExternalHandlerService.h"
Expand Down Expand Up @@ -2300,6 +2302,11 @@ bool ContentParent::BeginSubprocessLaunch(ProcessPriority aPriority) {
}
mPrefSerializer->AddSharedPrefCmdLineArgs(*mSubprocess, extraArgs);

auto startupCache = mozilla::scache::StartupCache::GetSingleton();
if (startupCache) {
startupCache->AddStartupCacheCmdLineArgs(*mSubprocess, extraArgs);
}

// Register ContentParent as an observer for changes to any pref
// whose prefix matches the empty string, i.e. all of them. The
// observation starts here in order to capture pref updates that
Expand Down Expand Up @@ -2733,6 +2740,7 @@ bool ContentParent::InitInternal(ProcessPriority aInitialPriority) {
Unused << SendRemoteType(mRemoteType);

ScriptPreloader::InitContentChild(*this);
scache::StartupCache::InitContentChild(*this);

// Initialize the message manager (and load delayed scripts) now that we
// have established communications with the child.
Expand Down Expand Up @@ -3829,6 +3837,16 @@ bool ContentParent::DeallocPScriptCacheParent(PScriptCacheParent* cache) {
return true;
}

PStartupCacheParent* ContentParent::AllocPStartupCacheParent(
const bool& wantCacheData) {
return new scache::StartupCacheParent(wantCacheData);
}

bool ContentParent::DeallocPStartupCacheParent(PStartupCacheParent* cache) {
delete static_cast<scache::StartupCacheParent*>(cache);
return true;
}

PNeckoParent* ContentParent::AllocPNeckoParent() { return new NeckoParent(); }

bool ContentParent::DeallocPNeckoParent(PNeckoParent* necko) {
Expand Down
5 changes: 5 additions & 0 deletions dom/ipc/ContentParent.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class PreallocatedProcessManagerImpl;
class BenchmarkStorageParent;

using mozilla::loader::PScriptCacheParent;
using mozilla::scache::PStartupCacheParent;

namespace embedding {
class PrintingParent;
Expand Down Expand Up @@ -926,6 +927,10 @@ class ContentParent final

bool DeallocPScriptCacheParent(PScriptCacheParent* shell);

PStartupCacheParent* AllocPStartupCacheParent(const bool& wantCacheData);

bool DeallocPStartupCacheParent(PStartupCacheParent* shell);

bool DeallocPNeckoParent(PNeckoParent* necko);

already_AddRefed<PExternalHelperAppParent> AllocPExternalHelperAppParent(
Expand Down
15 changes: 15 additions & 0 deletions dom/ipc/ContentProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
char* prefMapHandle = nullptr;
char* prefsLen = nullptr;
char* prefMapSize = nullptr;
char* scacheHandle = nullptr;
char* scacheSize = nullptr;
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
nsCOMPtr<nsIFile> profileDir;
#endif
Expand Down Expand Up @@ -127,6 +129,11 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false;
}
prefMapHandle = aArgv[i];
} else if (strcmp(aArgv[i], "-scacheHandle") == 0) {
if (++i == aArgc) {
return false;
}
scacheHandle = aArgv[i];
#endif

} else if (strcmp(aArgv[i], "-prefsLen") == 0) {
Expand All @@ -139,6 +146,11 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false;
}
prefMapSize = aArgv[i];
} else if (strcmp(aArgv[i], "-scacheSize") == 0) {
if (++i == aArgc) {
return false;
}
scacheSize = aArgv[i];
} else if (strcmp(aArgv[i], "-safeMode") == 0) {
gSafeMode = true;

Expand Down Expand Up @@ -175,6 +187,9 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
return false;
}

Unused << mozilla::scache::StartupCache::InitChildSingleton(scacheHandle,
scacheSize);

mContent.Init(IOThreadChild::message_loop(), ParentPid(), *parentBuildID,
IOThreadChild::TakeChannel(), *childID, *isForBrowser);

Expand Down
4 changes: 4 additions & 0 deletions dom/ipc/PContent.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ include protocol PVRManager;
include protocol PRemoteDecoderManager;
include protocol PProfiler;
include protocol PScriptCache;
include protocol PStartupCache;
include protocol PSessionStorageObserver;
include protocol PBenchmarkStorage;
include DOMTypes;
Expand Down Expand Up @@ -412,6 +413,7 @@ nested(upto inside_cpow) sync protocol PContent
manages PURLClassifier;
manages PURLClassifierLocal;
manages PScriptCache;
manages PStartupCache;
manages PLoginReputation;
manages PSessionStorageObserver;
manages PBenchmarkStorage;
Expand Down Expand Up @@ -539,6 +541,8 @@ child:

async PScriptCache(FileDescOrError cacheFile, bool wantCacheData);

async PStartupCache(bool wantCacheData);

async RegisterChrome(ChromePackage[] packages, SubstitutionMapping[] substitutions,
OverrideMapping[] overrides, nsCString locale, bool reset);
async RegisterChromeItem(ChromeRegistryItem item);
Expand Down
76 changes: 76 additions & 0 deletions js/xpconnect/loader/IOBuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,82 @@ class OutputBuffer {
size_t cursor_ = 0;
};

// This is similar to OutputBuffer, but with a fixed-size buffer, rather than
// a dynamically growing one. This is currently used in order to share
// StartupCache data across processes.
class PreallocatedOutputBuffer {
public:
explicit PreallocatedOutputBuffer(Range<uint8_t>& buffer) : data(buffer) {}

uint8_t* write(size_t size) {
MOZ_ASSERT(checkCapacity(size));

auto buf = &data[cursor_];
cursor_ += size;
return buf;
}

bool codeUint8(const uint8_t& val) {
if (checkCapacity(sizeof val)) {
*write(sizeof val) = val;
}
return !error_;
}

template <typename T>
bool codeUint8(const EnumSet<T>& val) {
return codeUint8(val.serialize());
}

bool codeUint16(const uint16_t& val) {
if (checkCapacity(sizeof val)) {
LittleEndian::writeUint16(write(sizeof val), val);
}
return !error_;
}

bool codeUint32(const uint32_t& val) {
if (checkCapacity(sizeof val)) {
LittleEndian::writeUint32(write(sizeof val), val);
}
return !error_;
}

bool codeString(const nsCString& str) {
uint16_t len = CheckedUint16(str.Length()).value();
if (codeUint16(len)) {
if (checkCapacity(len)) {
memcpy(write(len), str.get(), len);
}
}
return !error_;
}

bool error() { return error_; }

bool finished() { return error_ || !remainingCapacity(); }

size_t remainingCapacity() { return data.length() - cursor_; }

size_t cursor() const { return cursor_; }

const uint8_t* Get() const { return data.begin().get(); }

private:
bool checkCapacity(size_t size) {
if (size > remainingCapacity()) {
error_ = true;
}
return !error_;
}

bool error_ = false;

public:
Range<uint8_t>& data;
size_t cursor_ = 0;
};

class InputBuffer {
public:
explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {}
Expand Down
10 changes: 9 additions & 1 deletion js/xpconnect/loader/mozJSComponentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,15 @@ nsresult mozJSComponentLoader::ObjectForLocation(
// to loading the script, since we can always slow-load.

bool writeToCache = false;
StartupCache* cache = StartupCache::GetSingleton();

// Since we are intending to cache these buffers in the script preloader
// already, caching them in the StartupCache tends to be redundant. This
// ought to be addressed, but as in bug 1627075 we extended the
// StartupCache to be multi-process, we just didn't want to propagate
// this problem into yet more processes, so we pretend the StartupCache
// doesn't exist if we're not the parent process.
StartupCache* cache =
XRE_IsParentProcess() ? StartupCache::GetSingleton() : nullptr;

aInfo.EnsureResolvedURI();

Expand Down
10 changes: 9 additions & 1 deletion js/xpconnect/loader/mozJSSubScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,15 @@ nsresult mozJSSubScriptLoader::DoLoadSubScriptWithOptions(
bool ignoreCache =
options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob");

StartupCache* cache = ignoreCache ? nullptr : StartupCache::GetSingleton();
// Since we are intending to cache these buffers in the script preloader
// already, caching them in the StartupCache tends to be redundant. This
// ought to be addressed, but as in bug 1627075 we extended the
// StartupCache to be multi-process, we just didn't want to propagate
// this problem into yet more processes, so we pretend the StartupCache
// doesn't exist if we're not the parent process.
StartupCache* cache = (ignoreCache || !XRE_IsParentProcess())
? nullptr
: StartupCache::GetSingleton();

nsAutoCString cachePath;
SubscriptCachePath(cx, uri, targetObj, cachePath);
Expand Down
31 changes: 31 additions & 0 deletions startupcache/PStartupCache.ipdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8 -*- */
/* vim: set sw=4 ts=8 et tw=80 ft=cpp : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

include protocol PContent;

using class mozilla::TimeStamp from "mozilla/TimeStamp.h";
using mozilla::void_t from "ipc/IPCMessageUtils.h";

namespace mozilla {
namespace scache {

struct EntryData {
nsCString key;
// This will be an empty array if data is present in the previous
// session's cache.
uint8_t[] data;
};

protocol PStartupCache
{
manager PContent;

parent:
async __delete__(EntryData[] entries);
};

} // namespace scache
} // namespace mozilla
Loading

0 comments on commit 5ff30b6

Please sign in to comment.