forked from mirrors/gecko-dev
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
241 lines
7.6 KiB
C++
241 lines
7.6 KiB
C++
/* -*- 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 "AppShutdown.h"
|
|
|
|
#ifdef XP_WIN
|
|
# include <windows.h>
|
|
#else
|
|
# include <unistd.h>
|
|
#endif
|
|
|
|
#include "GeckoProfiler.h"
|
|
#include "mozilla/CmdLineAndEnvUtils.h"
|
|
#include "mozilla/PoisonIOInterposer.h"
|
|
#include "mozilla/Printf.h"
|
|
#include "mozilla/scache/StartupCache.h"
|
|
#include "mozilla/StartupTimeline.h"
|
|
#include "mozilla/StaticPrefs_toolkit.h"
|
|
#include "mozilla/LateWriteChecks.h"
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
#include "nsAppRunner.h"
|
|
#include "nsDirectoryServiceUtils.h"
|
|
#include "nsICertStorage.h"
|
|
#include "nsThreadUtils.h"
|
|
#include "prenv.h"
|
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
|
# include "mozilla/XULStore.h"
|
|
#endif
|
|
|
|
namespace mozilla {
|
|
|
|
static ShutdownPhase sFastShutdownPhase = ShutdownPhase::NotInShutdown;
|
|
static ShutdownPhase sLateWriteChecksPhase = ShutdownPhase::NotInShutdown;
|
|
static AppShutdownMode sShutdownMode = AppShutdownMode::Normal;
|
|
static Atomic<bool, MemoryOrdering::Relaxed> sIsShuttingDown;
|
|
|
|
// These environment variable strings are all deliberately copied and leaked
|
|
// due to requirements of PR_SetEnv and similar.
|
|
static char* sSavedXulAppFile = nullptr;
|
|
#ifdef XP_WIN
|
|
static wchar_t* sSavedProfDEnvVar = nullptr;
|
|
static wchar_t* sSavedProfLDEnvVar = nullptr;
|
|
#else
|
|
static char* sSavedProfDEnvVar = nullptr;
|
|
static char* sSavedProfLDEnvVar = nullptr;
|
|
#endif
|
|
|
|
ShutdownPhase GetShutdownPhaseFromPrefValue(int32_t aPrefValue) {
|
|
switch (aPrefValue) {
|
|
case 1:
|
|
return ShutdownPhase::ShutdownPostLastCycleCollection;
|
|
case 2:
|
|
return ShutdownPhase::ShutdownThreads;
|
|
case 3:
|
|
return ShutdownPhase::Shutdown;
|
|
// NOTE: the remaining values from the ShutdownPhase enum will be added
|
|
// when we're at least reasonably confident that the world won't come
|
|
// crashing down if we do a fast shutdown at that point.
|
|
}
|
|
return ShutdownPhase::NotInShutdown;
|
|
}
|
|
|
|
bool AppShutdown::IsShuttingDown() { return sIsShuttingDown; }
|
|
|
|
void AppShutdown::SaveEnvVarsForPotentialRestart() {
|
|
const char* s = PR_GetEnv("XUL_APP_FILE");
|
|
if (s) {
|
|
sSavedXulAppFile = Smprintf("%s=%s", "XUL_APP_FILE", s).release();
|
|
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(sSavedXulAppFile);
|
|
}
|
|
}
|
|
|
|
void AppShutdown::MaybeDoRestart() {
|
|
if (sShutdownMode == AppShutdownMode::Restart) {
|
|
StopLateWriteChecks();
|
|
|
|
// Since we'll be launching our child while we're still alive, make sure
|
|
// we've unlocked the profile first, otherwise the child could hit its
|
|
// profile lock check before we've exited and thus released our lock.
|
|
UnlockProfile();
|
|
|
|
if (sSavedXulAppFile) {
|
|
PR_SetEnv(sSavedXulAppFile);
|
|
}
|
|
|
|
#ifdef XP_WIN
|
|
if (sSavedProfDEnvVar && !EnvHasValue("XRE_PROFILE_PATH")) {
|
|
SetEnvironmentVariableW(L"XRE_PROFILE_PATH", sSavedProfDEnvVar);
|
|
}
|
|
if (sSavedProfLDEnvVar && !EnvHasValue("XRE_PROFILE_LOCAL_PATH")) {
|
|
SetEnvironmentVariableW(L"XRE_PROFILE_LOCAL_PATH", sSavedProfLDEnvVar);
|
|
}
|
|
#else
|
|
if (sSavedProfDEnvVar && !EnvHasValue("XRE_PROFILE_PATH")) {
|
|
PR_SetEnv(sSavedProfDEnvVar);
|
|
}
|
|
if (sSavedProfLDEnvVar && !EnvHasValue("XRE_PROFILE_LOCAL_PATH")) {
|
|
PR_SetEnv(sSavedProfLDEnvVar);
|
|
}
|
|
#endif
|
|
|
|
LaunchChild(true);
|
|
}
|
|
}
|
|
|
|
#ifdef XP_WIN
|
|
wchar_t* CopyPathIntoNewWCString(nsIFile* aFile) {
|
|
wchar_t* result = nullptr;
|
|
nsAutoString resStr;
|
|
aFile->GetPath(resStr);
|
|
if (resStr.Length() > 0) {
|
|
result = (wchar_t*)malloc((resStr.Length() + 1) * sizeof(wchar_t));
|
|
if (result) {
|
|
wcscpy(result, resStr.get());
|
|
result[resStr.Length()] = 0;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
void AppShutdown::Init(AppShutdownMode aMode) {
|
|
if (sShutdownMode == AppShutdownMode::Normal) {
|
|
sShutdownMode = aMode;
|
|
}
|
|
|
|
// Late-write checks needs to find the profile directory, so it has to
|
|
// be initialized before services::Shutdown or (because of
|
|
// xpcshell tests replacing the service) modules being unloaded.
|
|
InitLateWriteChecks();
|
|
|
|
int32_t fastShutdownPref = StaticPrefs::toolkit_shutdown_fastShutdownStage();
|
|
sFastShutdownPhase = GetShutdownPhaseFromPrefValue(fastShutdownPref);
|
|
int32_t lateWriteChecksPref =
|
|
StaticPrefs::toolkit_shutdown_lateWriteChecksStage();
|
|
sLateWriteChecksPhase = GetShutdownPhaseFromPrefValue(lateWriteChecksPref);
|
|
|
|
// Very early shutdowns can happen before the startup cache is even
|
|
// initialized; don't bother initializing it during shutdown.
|
|
if (auto* cache = scache::StartupCache::GetSingleton()) {
|
|
cache->MaybeInitShutdownWrite();
|
|
}
|
|
}
|
|
|
|
void AppShutdown::MaybeFastShutdown(ShutdownPhase aPhase) {
|
|
// For writes which we want to ensure are recorded, we don't want to trip
|
|
// 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::GetSingleton()) {
|
|
cache->EnsureShutdownWriteComplete();
|
|
}
|
|
|
|
nsresult rv;
|
|
#ifdef MOZ_NEW_XULSTORE
|
|
rv = XULStore::Shutdown();
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "XULStore::Shutdown() failed.");
|
|
#endif
|
|
|
|
nsCOMPtr<nsICertStorage> certStorage =
|
|
do_GetService("@mozilla.org/security/certstorage;1", &rv);
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Could not get nsICertStorage");
|
|
if (NS_SUCCEEDED(rv)) {
|
|
SpinEventLoopUntil([&]() {
|
|
int32_t remainingOps;
|
|
nsresult rv = certStorage->GetRemainingOperationCount(&remainingOps);
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
"nsICertStorage::getRemainingOperationCount failed during "
|
|
"shutdown");
|
|
return NS_FAILED(rv) || remainingOps <= 0;
|
|
});
|
|
}
|
|
}
|
|
if (aPhase == sFastShutdownPhase) {
|
|
StopLateWriteChecks();
|
|
RecordShutdownEndTimeStamp();
|
|
MaybeDoRestart();
|
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
|
profiler_shutdown(IsFastShutdown::Yes);
|
|
#endif
|
|
|
|
DoImmediateExit();
|
|
} else if (aPhase == sLateWriteChecksPhase) {
|
|
#ifdef XP_MACOSX
|
|
OnlyReportDirtyWrites();
|
|
#endif /* XP_MACOSX */
|
|
BeginLateWriteChecks();
|
|
}
|
|
}
|
|
|
|
void AppShutdown::OnShutdownConfirmed() {
|
|
sIsShuttingDown = true;
|
|
// If we're restarting, we need to save environment variables correctly
|
|
// while everything is still alive to do so.
|
|
if (sShutdownMode == AppShutdownMode::Restart) {
|
|
nsCOMPtr<nsIFile> profD;
|
|
nsCOMPtr<nsIFile> profLD;
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profD));
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
|
|
getter_AddRefs(profLD));
|
|
#ifdef XP_WIN
|
|
sSavedProfDEnvVar = CopyPathIntoNewWCString(profD);
|
|
sSavedProfLDEnvVar = CopyPathIntoNewWCString(profLD);
|
|
#else
|
|
nsAutoCString profDStr;
|
|
profD->GetNativePath(profDStr);
|
|
sSavedProfDEnvVar =
|
|
Smprintf("XRE_PROFILE_PATH=%s", profDStr.get()).release();
|
|
nsAutoCString profLDStr;
|
|
profLD->GetNativePath(profLDStr);
|
|
sSavedProfLDEnvVar =
|
|
Smprintf("XRE_PROFILE_LOCAL_PATH=%s", profLDStr.get()).release();
|
|
#endif
|
|
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(sSavedProfDEnvVar);
|
|
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(sSavedProfLDEnvVar);
|
|
}
|
|
}
|
|
|
|
void AppShutdown::DoImmediateExit() {
|
|
#ifdef XP_WIN
|
|
HANDLE process = ::GetCurrentProcess();
|
|
if (::TerminateProcess(process, 0)) {
|
|
::WaitForSingleObject(process, INFINITE);
|
|
}
|
|
MOZ_CRASH("TerminateProcess failed.");
|
|
#else
|
|
_exit(0);
|
|
#endif
|
|
}
|
|
|
|
bool AppShutdown::IsRestarting() {
|
|
return sShutdownMode == AppShutdownMode::Restart;
|
|
}
|
|
|
|
} // namespace mozilla
|