Bug 1627075 - OMT and OMP StartupCache access r=froydnj

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
This commit is contained in:
Doug Thayer 2020-07-07 17:03:28 +00:00
Родитель 2046625fac
Коммит 4d6b5a1f91
19 изменённых файлов: 1333 добавлений и 239 удалений

Просмотреть файл

@ -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"
@ -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(

Просмотреть файл

@ -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*);

Просмотреть файл

@ -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"
@ -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"
@ -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
@ -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.
@ -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) {

Просмотреть файл

@ -86,6 +86,7 @@ class PreallocatedProcessManagerImpl;
class BenchmarkStorageParent;
using mozilla::loader::PScriptCacheParent;
using mozilla::scache::PStartupCacheParent;
namespace embedding {
class PrintingParent;
@ -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(

Просмотреть файл

@ -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
@ -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) {
@ -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;
@ -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);

Просмотреть файл

@ -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;
@ -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;
@ -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);

Просмотреть файл

@ -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) {}

Просмотреть файл

@ -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();

Просмотреть файл

@ -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);

Просмотреть файл

@ -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

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -22,10 +22,13 @@
#include "mozilla/Attributes.h"
#include "mozilla/AutoMemMap.h"
#include "mozilla/Compression.h"
#include "mozilla/EnumSet.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Mutex.h"
#include "mozilla/Omnijar.h"
#include "mozilla/Result.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
/**
* The StartupCache is a persistent cache of simple key-value pairs,
@ -78,47 +81,153 @@
*/
namespace mozilla {
namespace dom {
class ContentParent;
}
namespace ipc {
class GeckoChildProcessHost;
} // namespace ipc
namespace scache {
class StartupCacheChild;
#ifdef XP_UNIX
// Please see bug 1440207 about improving the problem of random fixed FDs,
// which the addition of the below constant exacerbates.
static const int kStartupCacheFd = 11;
#endif
// We use INT_MAX here just to simplify the sorting - we want to push
// unrequested entries to the back, and have requested entries in the order
// they came in.
static const int kStartupCacheEntryNotRequested = INT_MAX;
static const int kStartupcacheEntryNotInSharedData = -1;
// StartupCache entries can be backed by a buffer which they allocate as
// soon as they are requested, into which they decompress the contents out
// of the memory mapped file, *or* they can be backed by a contiguous buffer
// which we allocate up front and decompress into, in order to share it with
// child processes. This class is a helper class to hold a buffer which the
// entry itself may or may not own.
//
// Side note: it may be appropriate for StartupCache entries to never own
// their underlying buffers. We explicitly work to ensure that anything the
// StartupCache returns to a caller survives for the lifetime of the
// application, so it may be preferable to have a set of large contiguous
// buffers which we allocate on demand, and fill up with cache entry contents,
// but at that point we're basically implementing our own hacky pseudo-malloc,
// for relatively uncertain performance gains. For the time being, we just
// keep the existing model unchanged.
class MaybeOwnedCharPtr {
public:
char* mPtr;
bool mOwned;
~MaybeOwnedCharPtr() {
if (mOwned) {
delete[] mPtr;
}
}
MaybeOwnedCharPtr(const MaybeOwnedCharPtr& other);
MaybeOwnedCharPtr& operator=(const MaybeOwnedCharPtr& other);
MaybeOwnedCharPtr(MaybeOwnedCharPtr&& other)
: mPtr(std::exchange(other.mPtr, nullptr)),
mOwned(std::exchange(other.mOwned, false)) {}
MaybeOwnedCharPtr& operator=(MaybeOwnedCharPtr&& other) {
std::swap(mPtr, other.mPtr);
std::swap(mOwned, other.mOwned);
return *this;
}
MaybeOwnedCharPtr& operator=(decltype(nullptr)) {
mPtr = nullptr;
mOwned = false;
return *this;
}
explicit operator bool() const { return !!mPtr; }
char* get() { return mPtr; }
explicit MaybeOwnedCharPtr(char* aBytes) : mPtr(aBytes), mOwned(false) {}
explicit MaybeOwnedCharPtr(UniquePtr<char[]>&& aBytes)
: mPtr(aBytes.release()), mOwned(true) {}
explicit MaybeOwnedCharPtr(size_t size)
: mPtr(new char[size]), mOwned(true) {}
};
enum class StartupCacheEntryFlags {
Shared,
RequestedByChild,
AddedThisSession,
};
struct StartupCacheEntry {
UniquePtr<char[]> mData;
MaybeOwnedCharPtr mData;
uint32_t mOffset;
uint32_t mCompressedSize;
uint32_t mUncompressedSize;
int32_t mSharedDataOffset;
int32_t mHeaderOffsetInFile;
int32_t mRequestedOrder;
bool mRequested;
EnumSet<StartupCacheEntryFlags> mFlags;
MOZ_IMPLICIT StartupCacheEntry(uint32_t aOffset, uint32_t aCompressedSize,
uint32_t aUncompressedSize)
uint32_t aUncompressedSize,
EnumSet<StartupCacheEntryFlags> aFlags)
: mData(nullptr),
mOffset(aOffset),
mCompressedSize(aCompressedSize),
mUncompressedSize(aUncompressedSize),
mSharedDataOffset(kStartupcacheEntryNotInSharedData),
mHeaderOffsetInFile(0),
mRequestedOrder(0),
mRequested(false) {}
mRequestedOrder(kStartupCacheEntryNotRequested),
mFlags(aFlags) {}
StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength,
int32_t aRequestedOrder)
int32_t aRequestedOrder,
EnumSet<StartupCacheEntryFlags> aFlags)
: mData(std::move(aData)),
mOffset(0),
mCompressedSize(0),
mUncompressedSize(aLength),
mSharedDataOffset(kStartupcacheEntryNotInSharedData),
mHeaderOffsetInFile(0),
mRequestedOrder(0),
mRequested(true) {}
mRequestedOrder(aRequestedOrder),
mFlags(aFlags) {}
struct Comparator {
using Value = std::pair<const nsCString*, StartupCacheEntry*>;
bool Equals(const Value& a, const Value& b) const {
return a.second->mRequestedOrder == b.second->mRequestedOrder;
// This is a bit ugly. Here and below, just note that we want entries
// with the RequestedByChild flag to be sorted before any other entries,
// because we're going to want to decompress them and send them down to
// child processes pretty early during startup.
return a.second->mFlags.contains(
StartupCacheEntryFlags::RequestedByChild) ==
b.second->mFlags.contains(
StartupCacheEntryFlags::RequestedByChild) &&
a.second->mRequestedOrder == b.second->mRequestedOrder;
}
bool LessThan(const Value& a, const Value& b) const {
return a.second->mRequestedOrder < b.second->mRequestedOrder;
bool requestedByChildA =
a.second->mFlags.contains(StartupCacheEntryFlags::RequestedByChild);
bool requestedByChildB =
b.second->mFlags.contains(StartupCacheEntryFlags::RequestedByChild);
if (requestedByChildA == requestedByChildB) {
return a.second->mRequestedOrder < b.second->mRequestedOrder;
} else {
return requestedByChildA;
}
}
};
};
@ -131,10 +240,26 @@ class StartupCacheListener final : public nsIObserver {
NS_DECL_NSIOBSERVER
};
// This mirrors a bit of logic in the script preloader. Basically, there's
// certainly some overhead in child processes sending us lists of requested
// startup cache items, so we want to limit that. Accordingly, we only
// request to be notified of requested cache items for the first occurrence
// of each process type, enumerated below.
enum class ProcessType : uint8_t {
Uninitialized,
Parent,
Web,
Extension,
PrivilegedAbout,
};
class StartupCache : public nsIMemoryReporter {
friend class StartupCacheListener;
friend class StartupCacheChild;
public:
using Table = HashMap<nsCString, StartupCacheEntry>;
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
@ -147,11 +272,10 @@ class StartupCache : public nsIMemoryReporter {
nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length);
// Stores a buffer. Caller yields ownership.
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf,
uint32_t length);
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf, uint32_t length,
bool isFromChildProcess = false);
// Removes the cache file.
void InvalidateCache(bool memoryOnly = false);
void InvalidateCache();
// For use during shutdown - this will write the startupcache's data
// to disk if the timer hasn't already gone off.
@ -169,7 +293,7 @@ class StartupCache : public nsIMemoryReporter {
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
nsIObjectOutputStream** outStream);
static StartupCache* GetSingletonNoInit();
static ProcessType GetChildProcessType(const nsAString& remoteType);
static StartupCache* GetSingleton();
// This will get the StartupCache up and running to get cached entries, but
@ -182,14 +306,22 @@ class StartupCache : public nsIMemoryReporter {
// requirements of the startup cache are met.
static nsresult FullyInitSingleton();
static nsresult InitChildSingleton(char* aScacheHandleStr,
char* aScacheSizeStr);
static void DeleteSingleton();
static void InitContentChild(dom::ContentParent& parent);
void AddStartupCacheCmdLineArgs(ipc::GeckoChildProcessHost& procHost,
std::vector<std::string>& aExtraOpts);
nsresult ParseStartupCacheCmdLineArgs(char* aScacheHandleStr,
char* aScacheSizeStr);
// This measures all the heap memory used by the StartupCache, i.e. it
// excludes the mapping.
size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
bool ShouldCompactCache();
nsresult ResetStartupWriteTimerCheckingReadCount();
nsresult ResetStartupWriteTimer();
bool StartupWriteComplete();
@ -202,6 +334,13 @@ class StartupCache : public nsIMemoryReporter {
Result<Ok, nsresult> LoadArchive();
nsresult PartialInit(nsIFile* aProfileLocalDir);
nsresult FullyInit();
nsresult InitChild(StartupCacheChild* cacheChild);
// Removes the cache file.
void InvalidateCacheImpl(bool memoryOnly = false);
nsresult ResetStartupWriteTimerCheckingReadCount();
nsresult ResetStartupWriteTimerImpl();
// Returns a file pointer for the cache file with the given name in the
// current profile.
@ -213,36 +352,69 @@ class StartupCache : public nsIMemoryReporter {
// Writes the cache to disk
Result<Ok, nsresult> WriteToDisk();
Result<Ok, nsresult> DecompressEntry(StartupCacheEntry& aEntry);
Result<Ok, nsresult> LoadEntriesOffDisk();
Result<Ok, nsresult> LoadEntriesFromSharedMemory();
void WaitOnPrefetchThread();
void StartPrefetchMemoryThread();
static void WriteTimeout(nsITimer* aTimer, void* aClosure);
static void SendEntriesTimeout(nsITimer* aTimer, void* aClosure);
void MaybeWriteOffMainThread();
static void ThreadedPrefetch(void* aClosure);
HashMap<nsCString, StartupCacheEntry> mTable;
EnumSet<ProcessType> mInitializedProcesses{};
nsCString mContentStartupFinishedTopic;
Table mTable;
// owns references to the contents of tables which have been invalidated.
// In theory grows forever if the cache is continually filled and then
// invalidated, but this should not happen in practice.
nsTArray<decltype(mTable)> mOldTables;
nsCOMPtr<nsIFile> mFile;
loader::AutoMemMap mCacheData;
Mutex mTableLock;
loader::AutoMemMap mSharedData;
UniqueFileHandle mSharedDataHandle;
// This lock must protect a few members of the StartupCache. Essentially,
// we want to protect everything accessed by GetBuffer and PutBuffer. This
// includes:
// - mTable
// - mCacheData
// - mDecompressionContext
// - mCurTableReferenced
// - mOldTables
// - mWrittenOnce
// - gIgnoreDiskCache
// - mFile
// - mWriteTimer
// - mStartupWriteInitiated
mutable Mutex mLock;
nsCOMPtr<nsIObserverService> mObserverService;
RefPtr<StartupCacheListener> mListener;
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsITimer> mWriteTimer;
nsCOMPtr<nsITimer> mSendEntriesTimer;
Atomic<bool> mDirty;
Atomic<bool> mWrittenOnce;
bool mCurTableReferenced;
bool mLoaded;
bool mFullyInitialized;
uint32_t mRequestedCount;
uint32_t mPrefetchSize;
uint32_t mSharedDataSize;
size_t mCacheEntriesBaseOffset;
static StaticRefPtr<StartupCache> gStartupCache;
static bool gShutdownInitiated;
static bool gIgnoreDiskCache;
static bool gFoundDiskCacheOnInit;
Atomic<StartupCacheChild*> mChildActor;
PRThread* mPrefetchThread;
UniquePtr<Compression::LZ4FrameDecompressionContext> mDecompressionContext;
#ifdef DEBUG

Просмотреть файл

@ -0,0 +1,64 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/scache/StartupCacheChild.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace scache {
void StartupCacheChild::Init(bool wantCacheData) {
mWantCacheData = wantCacheData;
auto* cache = StartupCache::GetSingleton();
if (cache) {
Unused << cache->InitChild(wantCacheData ? this : nullptr);
}
if (!wantCacheData) {
// If the parent process isn't expecting any cache data from us, we're
// done.
Send__delete__(this, AutoTArray<EntryData, 0>());
}
}
void StartupCacheChild::SendEntriesAndFinalize(StartupCache::Table& entries) {
MOZ_RELEASE_ASSERT(mWantCacheData);
nsTArray<EntryData> dataArray;
for (auto iter = entries.iter(); !iter.done(); iter.next()) {
const auto& key = iter.get().key();
auto& value = iter.get().value();
if (!value.mData ||
value.mRequestedOrder == kStartupCacheEntryNotRequested) {
continue;
}
auto data = dataArray.AppendElement();
data->key() = key;
if (value.mFlags.contains(StartupCacheEntryFlags::AddedThisSession)) {
data->data().AppendElements(
reinterpret_cast<const uint8_t*>(value.mData.get()),
value.mUncompressedSize);
}
}
mWantCacheData = false;
Send__delete__(this, dataArray);
}
void StartupCacheChild::ActorDestroy(ActorDestroyReason aWhy) {
auto* cache = StartupCache::GetSingleton();
if (cache) {
cache->mChildActor = nullptr;
}
}
} // namespace scache
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,43 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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/. */
#ifndef StartupCacheChild_h
#define StartupCacheChild_h
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/PStartupCacheChild.h"
#include "mozilla/scache/PStartupCacheParent.h"
namespace mozilla {
namespace ipc {
class FileDescriptor;
}
namespace scache {
using mozilla::ipc::FileDescriptor;
class StartupCacheChild final : public PStartupCacheChild {
friend class mozilla::scache::StartupCache;
friend class mozilla::scache::StartupCacheListener;
public:
StartupCacheChild() = default;
void Init(bool wantCacheData);
protected:
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
void SendEntriesAndFinalize(StartupCache::Table& entries);
private:
bool mWantCacheData = false;
};
} // namespace scache
} // namespace mozilla
#endif // StartupCacheChild_h

Просмотреть файл

@ -0,0 +1,38 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/scache/StartupCacheParent.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace scache {
IPCResult StartupCacheParent::Recv__delete__(nsTArray<EntryData>&& entries) {
if (!mWantCacheData && entries.Length()) {
return IPC_FAIL(this, "UnexpectedScriptData");
}
mWantCacheData = false;
if (entries.Length()) {
auto* cache = StartupCache::GetSingleton();
for (auto& entry : entries) {
auto buffer = MakeUnique<char[]>(entry.data().Length());
memcpy(buffer.get(), entry.data().Elements(), entry.data().Length());
cache->PutBuffer(entry.key().get(), std::move(buffer),
entry.data().Length(), /* isFromChildProcess:*/ true);
}
}
return IPC_OK();
}
void StartupCacheParent::ActorDestroy(ActorDestroyReason aWhy) {}
} // namespace scache
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,37 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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/. */
#ifndef StartupCacheParent_h
#define StartupCacheParent_h
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/PStartupCacheParent.h"
namespace mozilla {
namespace scache {
using mozilla::ipc::IPCResult;
class StartupCacheParent final : public PStartupCacheParent {
friend class PStartupCacheParent;
public:
explicit StartupCacheParent(bool wantCacheData)
: mWantCacheData(wantCacheData) {}
protected:
IPCResult Recv__delete__(nsTArray<EntryData>&& entries);
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
private:
bool mWantCacheData;
};
} // namespace scache
} // namespace mozilla
#endif // StartupCacheParent_h

Просмотреть файл

@ -12,12 +12,16 @@ BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
EXPORTS.mozilla.scache += [
'StartupCache.h',
'StartupCacheChild.h',
'StartupCacheParent.h',
'StartupCacheUtils.h',
]
UNIFIED_SOURCES += [
'StartupCache.cpp',
'StartupCacheChild.cpp',
'StartupCacheInfo.cpp',
'StartupCacheParent.cpp',
'StartupCacheUtils.cpp',
]
@ -31,4 +35,10 @@ XPIDL_SOURCES += [
'nsIStartupCacheInfo.idl',
]
IPDL_SOURCES += [
'PStartupCache.ipdl',
]
include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul'

Просмотреть файл

@ -66,6 +66,11 @@ TestStartupCache::TestStartupCache() {
// We intentionally leak `env` here because it is required by PR_SetEnv
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env);
#endif
if (!StartupCache::GetSingleton()) {
StartupCache::PartialInitSingleton(nullptr);
StartupCache::FullyInitSingleton();
}
StartupCache::GetSingleton()->InvalidateCache();
}
TestStartupCache::~TestStartupCache() {

Просмотреть файл

@ -142,7 +142,7 @@ void AppShutdown::Init(AppShutdownMode aMode) {
// Very early shutdowns can happen before the startup cache is even
// initialized; don't bother initializing it during shutdown.
if (auto* cache = scache::StartupCache::GetSingletonNoInit()) {
if (auto* cache = scache::StartupCache::GetSingleton()) {
cache->MaybeInitShutdownWrite();
}
}
@ -152,7 +152,7 @@ void AppShutdown::MaybeFastShutdown(ShutdownPhase aPhase) {
// the late write checking code. Anything that writes to disk and which
// we don't want to skip should be listed out explicitly in this section.
if (aPhase == sFastShutdownPhase || aPhase == sLateWriteChecksPhase) {
if (auto* cache = scache::StartupCache::GetSingletonNoInit()) {
if (auto* cache = scache::StartupCache::GetSingleton()) {
cache->EnsureShutdownWriteComplete();
}