Bug 1634779 - pt 2. Partially revert Bug 1603006 r=kmag
[gecko.git] / dom / ipc / PreallocatedProcessManager.cpp
blob2baf5690d68af7677ab3dab99a0239aaf81e8c80
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/PreallocatedProcessManager.h"
8 #include "mozilla/ClearOnShutdown.h"
9 #include "mozilla/Preferences.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/dom/ContentParent.h"
12 #include "mozilla/dom/ScriptSettings.h"
13 #include "mozilla/StaticPrefs_dom.h"
14 #include "nsIPropertyBag2.h"
15 #include "ProcessPriorityManager.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsIXULRuntime.h"
19 using namespace mozilla::hal;
20 using namespace mozilla::dom;
22 namespace mozilla {
23 /**
24 * This singleton class implements the static methods on
25 * PreallocatedProcessManager.
27 class PreallocatedProcessManagerImpl final : public nsIObserver {
28 public:
29 static PreallocatedProcessManagerImpl* Singleton();
31 NS_DECL_ISUPPORTS
32 NS_DECL_NSIOBSERVER
34 // See comments on PreallocatedProcessManager for these methods.
35 void AddBlocker(ContentParent* aParent);
36 void RemoveBlocker(ContentParent* aParent);
37 already_AddRefed<ContentParent> Take();
38 bool Provide(ContentParent* aParent);
40 private:
41 static const char* const kObserverTopics[];
43 static mozilla::StaticRefPtr<PreallocatedProcessManagerImpl> sSingleton;
45 PreallocatedProcessManagerImpl();
46 ~PreallocatedProcessManagerImpl();
47 DISALLOW_EVIL_CONSTRUCTORS(PreallocatedProcessManagerImpl);
49 void Init();
51 bool CanAllocate();
52 void AllocateAfterDelay();
53 void AllocateOnIdle();
54 void AllocateNow();
56 void RereadPrefs();
57 void Enable();
58 void Disable();
59 void CloseProcess();
61 void ObserveProcessShutdown(nsISupports* aSubject);
63 bool mEnabled;
64 bool mShutdown;
65 bool mLaunchInProgress;
66 RefPtr<ContentParent> mPreallocatedProcess;
67 nsTHashtable<nsUint64HashKey> mBlockers;
69 bool IsEmpty() const { return !mPreallocatedProcess && !mLaunchInProgress; }
72 const char* const PreallocatedProcessManagerImpl::kObserverTopics[] = {
73 "ipc:content-shutdown",
74 "memory-pressure",
75 "profile-change-teardown",
76 NS_XPCOM_SHUTDOWN_OBSERVER_ID,
79 /* static */
80 StaticRefPtr<PreallocatedProcessManagerImpl>
81 PreallocatedProcessManagerImpl::sSingleton;
83 /* static */
84 PreallocatedProcessManagerImpl* PreallocatedProcessManagerImpl::Singleton() {
85 MOZ_ASSERT(NS_IsMainThread());
86 if (!sSingleton) {
87 sSingleton = new PreallocatedProcessManagerImpl();
88 sSingleton->Init();
89 ClearOnShutdown(&sSingleton);
92 return sSingleton;
95 NS_IMPL_ISUPPORTS(PreallocatedProcessManagerImpl, nsIObserver)
97 PreallocatedProcessManagerImpl::PreallocatedProcessManagerImpl()
98 : mEnabled(false), mShutdown(false), mLaunchInProgress(false) {}
100 PreallocatedProcessManagerImpl::~PreallocatedProcessManagerImpl() {
101 // This shouldn't happen, because the promise callbacks should
102 // hold strong references, but let't make absolutely sure:
103 MOZ_RELEASE_ASSERT(!mLaunchInProgress);
106 void PreallocatedProcessManagerImpl::Init() {
107 Preferences::AddStrongObserver(this, "dom.ipc.processPrelaunch.enabled");
108 // We have to respect processCount at all time. This is especially important
109 // for testing.
110 Preferences::AddStrongObserver(this, "dom.ipc.processCount");
111 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
112 MOZ_ASSERT(os);
113 for (auto topic : kObserverTopics) {
114 os->AddObserver(this, topic, /* ownsWeak */ false);
116 RereadPrefs();
119 NS_IMETHODIMP
120 PreallocatedProcessManagerImpl::Observe(nsISupports* aSubject,
121 const char* aTopic,
122 const char16_t* aData) {
123 if (!strcmp("ipc:content-shutdown", aTopic)) {
124 ObserveProcessShutdown(aSubject);
125 } else if (!strcmp("nsPref:changed", aTopic)) {
126 // The only other observer we registered was for our prefs.
127 RereadPrefs();
128 } else if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, aTopic) ||
129 !strcmp("profile-change-teardown", aTopic)) {
130 Preferences::RemoveObserver(this, "dom.ipc.processPrelaunch.enabled");
131 Preferences::RemoveObserver(this, "dom.ipc.processCount");
132 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
133 MOZ_ASSERT(os);
134 for (auto topic : kObserverTopics) {
135 os->RemoveObserver(this, topic);
137 // Let's prevent any new preallocated processes from starting. ContentParent
138 // will handle the shutdown of the existing process and the
139 // mPreallocatedProcess reference will be cleared by the ClearOnShutdown of
140 // the manager singleton.
141 mShutdown = true;
142 } else if (!strcmp("memory-pressure", aTopic)) {
143 CloseProcess();
144 } else {
145 MOZ_ASSERT_UNREACHABLE("Unknown topic");
148 return NS_OK;
151 void PreallocatedProcessManagerImpl::RereadPrefs() {
152 if (mozilla::BrowserTabsRemoteAutostart() &&
153 Preferences::GetBool("dom.ipc.processPrelaunch.enabled")) {
154 Enable();
155 } else {
156 Disable();
159 if (ContentParent::IsMaxProcessCountReached(
160 NS_LITERAL_STRING(DEFAULT_REMOTE_TYPE))) {
161 CloseProcess();
165 already_AddRefed<ContentParent> PreallocatedProcessManagerImpl::Take() {
166 if (!mEnabled || mShutdown) {
167 return nullptr;
170 if (mPreallocatedProcess) {
171 // The preallocated process is taken. Let's try to start up a new one soon.
172 ProcessPriorityManager::SetProcessPriority(mPreallocatedProcess,
173 PROCESS_PRIORITY_FOREGROUND);
174 AllocateOnIdle();
177 return mPreallocatedProcess.forget();
180 bool PreallocatedProcessManagerImpl::Provide(ContentParent* aParent) {
181 // This will take the already-running process even if there's a
182 // launch in progress; if that process hasn't been taken by the
183 // time the launch completes, the new process will be shut down.
184 if (mEnabled && !mShutdown && !mPreallocatedProcess) {
185 mPreallocatedProcess = aParent;
188 // We might get a call from both NotifyTabDestroying and NotifyTabDestroyed
189 // with the same ContentParent. Returning true here for both calls is
190 // important to avoid the cached process to be destroyed.
191 return aParent == mPreallocatedProcess;
194 void PreallocatedProcessManagerImpl::Enable() {
195 if (mEnabled) {
196 return;
199 mEnabled = true;
200 AllocateAfterDelay();
203 void PreallocatedProcessManagerImpl::AddBlocker(ContentParent* aParent) {
204 uint64_t childID = aParent->ChildID();
205 MOZ_ASSERT(!mBlockers.Contains(childID));
206 mBlockers.PutEntry(childID);
209 void PreallocatedProcessManagerImpl::RemoveBlocker(ContentParent* aParent) {
210 uint64_t childID = aParent->ChildID();
211 // This used to assert that the blocker existed, but preallocated
212 // processes aren't blockers anymore because it's not useful and
213 // interferes with async launch, and it's simpler if content
214 // processes don't need to remember whether they were preallocated.
215 // (And preallocated processes can't AddBlocker when taken, because
216 // it's possible for a short-lived process to be recycled through
217 // Provide() and Take() before reaching RecvFirstIdle.)
218 mBlockers.RemoveEntry(childID);
219 if (IsEmpty() && mBlockers.IsEmpty()) {
220 AllocateAfterDelay();
224 bool PreallocatedProcessManagerImpl::CanAllocate() {
225 return mEnabled && mBlockers.IsEmpty() && IsEmpty() && !mShutdown &&
226 !ContentParent::IsMaxProcessCountReached(
227 NS_LITERAL_STRING(DEFAULT_REMOTE_TYPE));
230 void PreallocatedProcessManagerImpl::AllocateAfterDelay() {
231 if (!mEnabled) {
232 return;
235 NS_DelayedDispatchToCurrentThread(
236 NewRunnableMethod("PreallocatedProcessManagerImpl::AllocateOnIdle", this,
237 &PreallocatedProcessManagerImpl::AllocateOnIdle),
238 StaticPrefs::dom_ipc_processPrelaunch_delayMs());
241 void PreallocatedProcessManagerImpl::AllocateOnIdle() {
242 if (!mEnabled) {
243 return;
246 NS_DispatchToCurrentThreadQueue(
247 NewRunnableMethod("PreallocatedProcessManagerImpl::AllocateNow", this,
248 &PreallocatedProcessManagerImpl::AllocateNow),
249 EventQueuePriority::Idle);
252 void PreallocatedProcessManagerImpl::AllocateNow() {
253 if (!CanAllocate()) {
254 if (mEnabled && !mShutdown && IsEmpty() && !mBlockers.IsEmpty()) {
255 // If it's too early to allocate a process let's retry later.
256 AllocateAfterDelay();
258 return;
261 RefPtr<PreallocatedProcessManagerImpl> self(this);
262 mLaunchInProgress = true;
264 ContentParent::PreallocateProcess()->Then(
265 GetCurrentThreadSerialEventTarget(), __func__,
267 [self, this](const RefPtr<ContentParent>& process) {
268 mLaunchInProgress = false;
269 if (CanAllocate()) {
270 mPreallocatedProcess = process;
271 } else {
272 process->ShutDownProcess(ContentParent::SEND_SHUTDOWN_MESSAGE);
276 [self, this](ContentParent::LaunchError err) {
277 mLaunchInProgress = false;
281 void PreallocatedProcessManagerImpl::Disable() {
282 if (!mEnabled) {
283 return;
286 mEnabled = false;
287 CloseProcess();
290 void PreallocatedProcessManagerImpl::CloseProcess() {
291 if (mPreallocatedProcess) {
292 mPreallocatedProcess->ShutDownProcess(ContentParent::SEND_SHUTDOWN_MESSAGE);
293 mPreallocatedProcess = nullptr;
297 void PreallocatedProcessManagerImpl::ObserveProcessShutdown(
298 nsISupports* aSubject) {
299 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
300 NS_ENSURE_TRUE_VOID(props);
302 uint64_t childID = CONTENT_PROCESS_ID_UNKNOWN;
303 props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), &childID);
304 NS_ENSURE_TRUE_VOID(childID != CONTENT_PROCESS_ID_UNKNOWN);
306 if (mPreallocatedProcess && childID == mPreallocatedProcess->ChildID()) {
307 mPreallocatedProcess = nullptr;
310 mBlockers.RemoveEntry(childID);
313 inline PreallocatedProcessManagerImpl* GetPPMImpl() {
314 return PreallocatedProcessManagerImpl::Singleton();
317 /* static */
318 void PreallocatedProcessManager::AddBlocker(ContentParent* aParent) {
319 GetPPMImpl()->AddBlocker(aParent);
322 /* static */
323 void PreallocatedProcessManager::RemoveBlocker(ContentParent* aParent) {
324 GetPPMImpl()->RemoveBlocker(aParent);
327 /* static */
328 already_AddRefed<ContentParent> PreallocatedProcessManager::Take() {
329 return GetPPMImpl()->Take();
332 /* static */
333 bool PreallocatedProcessManager::Provide(ContentParent* aParent) {
334 return GetPPMImpl()->Provide(aParent);
337 } // namespace mozilla