dom/ipc/PreallocatedProcessManager.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set sw=2 ts=8 et ft=cpp : */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/PreallocatedProcessManager.h"
michael@0 8 #include "mozilla/ClearOnShutdown.h"
michael@0 9 #include "mozilla/Preferences.h"
michael@0 10 #include "mozilla/dom/ContentParent.h"
michael@0 11 #include "nsIPropertyBag2.h"
michael@0 12 #include "ProcessPriorityManager.h"
michael@0 13 #include "nsServiceManagerUtils.h"
michael@0 14 #include "nsCxPusher.h"
michael@0 15
michael@0 16 #ifdef MOZ_NUWA_PROCESS
michael@0 17 #include "ipc/Nuwa.h"
michael@0 18 #endif
michael@0 19
michael@0 20 // This number is fairly arbitrary ... the intention is to put off
michael@0 21 // launching another app process until the last one has finished
michael@0 22 // loading its content, to reduce CPU/memory/IO contention.
michael@0 23 #define DEFAULT_ALLOCATE_DELAY 1000
michael@0 24 #define NUWA_FORK_WAIT_DURATION_MS 2000 // 2 seconds.
michael@0 25
michael@0 26 using namespace mozilla;
michael@0 27 using namespace mozilla::hal;
michael@0 28 using namespace mozilla::dom;
michael@0 29
michael@0 30 namespace {
michael@0 31
michael@0 32 /**
michael@0 33 * This singleton class implements the static methods on
michael@0 34 * PreallocatedProcessManager.
michael@0 35 */
michael@0 36 class PreallocatedProcessManagerImpl MOZ_FINAL
michael@0 37 : public nsIObserver
michael@0 38 {
michael@0 39 public:
michael@0 40 static PreallocatedProcessManagerImpl* Singleton();
michael@0 41
michael@0 42 NS_DECL_ISUPPORTS
michael@0 43 NS_DECL_NSIOBSERVER
michael@0 44
michael@0 45 // See comments on PreallocatedProcessManager for these methods.
michael@0 46 void AllocateAfterDelay();
michael@0 47 void AllocateOnIdle();
michael@0 48 void AllocateNow();
michael@0 49 already_AddRefed<ContentParent> Take();
michael@0 50
michael@0 51 #ifdef MOZ_NUWA_PROCESS
michael@0 52 public:
michael@0 53 void ScheduleDelayedNuwaFork();
michael@0 54 void DelayedNuwaFork();
michael@0 55 void PublishSpareProcess(ContentParent* aContent);
michael@0 56 void MaybeForgetSpare(ContentParent* aContent);
michael@0 57 void OnNuwaReady();
michael@0 58 bool PreallocatedProcessReady();
michael@0 59 already_AddRefed<ContentParent> GetSpareProcess();
michael@0 60 void RunAfterPreallocatedProcessReady(nsIRunnable* aRunnable);
michael@0 61
michael@0 62 private:
michael@0 63 void NuwaFork();
michael@0 64
michael@0 65 // initialization off the critical path of app startup.
michael@0 66 CancelableTask* mPreallocateAppProcessTask;
michael@0 67
michael@0 68 // The array containing the preallocated processes. 4 as the inline storage size
michael@0 69 // should be enough so we don't need to grow the nsAutoTArray.
michael@0 70 nsAutoTArray<nsRefPtr<ContentParent>, 4> mSpareProcesses;
michael@0 71
michael@0 72 nsTArray<nsCOMPtr<nsIRunnable> > mDelayedContentParentRequests;
michael@0 73
michael@0 74 // Nuwa process is ready for creating new process.
michael@0 75 bool mIsNuwaReady;
michael@0 76 #endif
michael@0 77
michael@0 78 private:
michael@0 79 static mozilla::StaticRefPtr<PreallocatedProcessManagerImpl> sSingleton;
michael@0 80
michael@0 81 PreallocatedProcessManagerImpl();
michael@0 82 DISALLOW_EVIL_CONSTRUCTORS(PreallocatedProcessManagerImpl);
michael@0 83
michael@0 84 void Init();
michael@0 85
michael@0 86 void RereadPrefs();
michael@0 87 void Enable();
michael@0 88 void Disable();
michael@0 89
michael@0 90 void ObserveProcessShutdown(nsISupports* aSubject);
michael@0 91
michael@0 92 bool mEnabled;
michael@0 93 bool mShutdown;
michael@0 94 nsRefPtr<ContentParent> mPreallocatedAppProcess;
michael@0 95 };
michael@0 96
michael@0 97 /* static */ StaticRefPtr<PreallocatedProcessManagerImpl>
michael@0 98 PreallocatedProcessManagerImpl::sSingleton;
michael@0 99
michael@0 100 /* static */ PreallocatedProcessManagerImpl*
michael@0 101 PreallocatedProcessManagerImpl::Singleton()
michael@0 102 {
michael@0 103 if (!sSingleton) {
michael@0 104 sSingleton = new PreallocatedProcessManagerImpl();
michael@0 105 sSingleton->Init();
michael@0 106 ClearOnShutdown(&sSingleton);
michael@0 107 }
michael@0 108
michael@0 109 return sSingleton;
michael@0 110 }
michael@0 111
michael@0 112 NS_IMPL_ISUPPORTS(PreallocatedProcessManagerImpl, nsIObserver)
michael@0 113
michael@0 114 PreallocatedProcessManagerImpl::PreallocatedProcessManagerImpl()
michael@0 115 : mEnabled(false)
michael@0 116 #ifdef MOZ_NUWA_PROCESS
michael@0 117 , mPreallocateAppProcessTask(nullptr)
michael@0 118 , mIsNuwaReady(false)
michael@0 119 #endif
michael@0 120 , mShutdown(false)
michael@0 121 {}
michael@0 122
michael@0 123 void
michael@0 124 PreallocatedProcessManagerImpl::Init()
michael@0 125 {
michael@0 126 Preferences::AddStrongObserver(this, "dom.ipc.processPrelaunch.enabled");
michael@0 127 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
michael@0 128 if (os) {
michael@0 129 os->AddObserver(this, "ipc:content-shutdown",
michael@0 130 /* weakRef = */ false);
michael@0 131 os->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
michael@0 132 /* weakRef = */ false);
michael@0 133 }
michael@0 134 RereadPrefs();
michael@0 135 }
michael@0 136
michael@0 137 NS_IMETHODIMP
michael@0 138 PreallocatedProcessManagerImpl::Observe(nsISupports* aSubject,
michael@0 139 const char* aTopic,
michael@0 140 const char16_t* aData)
michael@0 141 {
michael@0 142 if (!strcmp("ipc:content-shutdown", aTopic)) {
michael@0 143 ObserveProcessShutdown(aSubject);
michael@0 144 } else if (!strcmp("nsPref:changed", aTopic)) {
michael@0 145 // The only other observer we registered was for our prefs.
michael@0 146 RereadPrefs();
michael@0 147 } else if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, aTopic)) {
michael@0 148 mShutdown = true;
michael@0 149 } else {
michael@0 150 MOZ_ASSERT(false);
michael@0 151 }
michael@0 152
michael@0 153 return NS_OK;
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 PreallocatedProcessManagerImpl::RereadPrefs()
michael@0 158 {
michael@0 159 if (Preferences::GetBool("dom.ipc.processPrelaunch.enabled")) {
michael@0 160 Enable();
michael@0 161 } else {
michael@0 162 Disable();
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 already_AddRefed<ContentParent>
michael@0 167 PreallocatedProcessManagerImpl::Take()
michael@0 168 {
michael@0 169 return mPreallocatedAppProcess.forget();
michael@0 170 }
michael@0 171
michael@0 172 void
michael@0 173 PreallocatedProcessManagerImpl::Enable()
michael@0 174 {
michael@0 175 if (mEnabled) {
michael@0 176 return;
michael@0 177 }
michael@0 178
michael@0 179 mEnabled = true;
michael@0 180 #ifdef MOZ_NUWA_PROCESS
michael@0 181 ScheduleDelayedNuwaFork();
michael@0 182 #else
michael@0 183 AllocateAfterDelay();
michael@0 184 #endif
michael@0 185 }
michael@0 186
michael@0 187 void
michael@0 188 PreallocatedProcessManagerImpl::AllocateAfterDelay()
michael@0 189 {
michael@0 190 if (!mEnabled || mPreallocatedAppProcess) {
michael@0 191 return;
michael@0 192 }
michael@0 193
michael@0 194 MessageLoop::current()->PostDelayedTask(
michael@0 195 FROM_HERE,
michael@0 196 NewRunnableMethod(this, &PreallocatedProcessManagerImpl::AllocateOnIdle),
michael@0 197 Preferences::GetUint("dom.ipc.processPrelaunch.delayMs",
michael@0 198 DEFAULT_ALLOCATE_DELAY));
michael@0 199 }
michael@0 200
michael@0 201 void
michael@0 202 PreallocatedProcessManagerImpl::AllocateOnIdle()
michael@0 203 {
michael@0 204 if (!mEnabled || mPreallocatedAppProcess) {
michael@0 205 return;
michael@0 206 }
michael@0 207
michael@0 208 MessageLoop::current()->PostIdleTask(
michael@0 209 FROM_HERE,
michael@0 210 NewRunnableMethod(this, &PreallocatedProcessManagerImpl::AllocateNow));
michael@0 211 }
michael@0 212
michael@0 213 void
michael@0 214 PreallocatedProcessManagerImpl::AllocateNow()
michael@0 215 {
michael@0 216 if (!mEnabled || mPreallocatedAppProcess) {
michael@0 217 return;
michael@0 218 }
michael@0 219
michael@0 220 mPreallocatedAppProcess = ContentParent::PreallocateAppProcess();
michael@0 221 }
michael@0 222
michael@0 223 #ifdef MOZ_NUWA_PROCESS
michael@0 224
michael@0 225 void
michael@0 226 PreallocatedProcessManagerImpl::RunAfterPreallocatedProcessReady(nsIRunnable* aRequest)
michael@0 227 {
michael@0 228 MOZ_ASSERT(NS_IsMainThread());
michael@0 229 mDelayedContentParentRequests.AppendElement(aRequest);
michael@0 230
michael@0 231 // This is an urgent NuwaFork() request. Request to fork at once.
michael@0 232 DelayedNuwaFork();
michael@0 233 }
michael@0 234
michael@0 235 void
michael@0 236 PreallocatedProcessManagerImpl::ScheduleDelayedNuwaFork()
michael@0 237 {
michael@0 238 MOZ_ASSERT(NS_IsMainThread());
michael@0 239
michael@0 240 if (mPreallocateAppProcessTask) {
michael@0 241 // Make sure there is only one request running.
michael@0 242 return;
michael@0 243 }
michael@0 244
michael@0 245 mPreallocateAppProcessTask = NewRunnableMethod(
michael@0 246 this, &PreallocatedProcessManagerImpl::DelayedNuwaFork);
michael@0 247 MessageLoop::current()->PostDelayedTask(
michael@0 248 FROM_HERE, mPreallocateAppProcessTask,
michael@0 249 Preferences::GetUint("dom.ipc.processPrelaunch.delayMs",
michael@0 250 DEFAULT_ALLOCATE_DELAY));
michael@0 251 }
michael@0 252
michael@0 253 void
michael@0 254 PreallocatedProcessManagerImpl::DelayedNuwaFork()
michael@0 255 {
michael@0 256 MOZ_ASSERT(NS_IsMainThread());
michael@0 257
michael@0 258 mPreallocateAppProcessTask = nullptr;
michael@0 259
michael@0 260 if (!mIsNuwaReady) {
michael@0 261 if (!mPreallocatedAppProcess && !mShutdown && mEnabled) {
michael@0 262 mPreallocatedAppProcess = ContentParent::RunNuwaProcess();
michael@0 263 }
michael@0 264 // else mPreallocatedAppProcess is starting. It will NuwaFork() when ready.
michael@0 265 } else if (mSpareProcesses.IsEmpty()) {
michael@0 266 NuwaFork();
michael@0 267 }
michael@0 268 }
michael@0 269
michael@0 270 /**
michael@0 271 * Get a spare ContentParent from mSpareProcesses list.
michael@0 272 */
michael@0 273 already_AddRefed<ContentParent>
michael@0 274 PreallocatedProcessManagerImpl::GetSpareProcess()
michael@0 275 {
michael@0 276 MOZ_ASSERT(NS_IsMainThread());
michael@0 277
michael@0 278 if (mSpareProcesses.IsEmpty()) {
michael@0 279 return nullptr;
michael@0 280 }
michael@0 281
michael@0 282 nsRefPtr<ContentParent> process = mSpareProcesses.LastElement();
michael@0 283 mSpareProcesses.RemoveElementAt(mSpareProcesses.Length() - 1);
michael@0 284
michael@0 285 if (mSpareProcesses.IsEmpty() && mIsNuwaReady) {
michael@0 286 NS_ASSERTION(mPreallocatedAppProcess != nullptr,
michael@0 287 "Nuwa process is not present!");
michael@0 288 ScheduleDelayedNuwaFork();
michael@0 289 }
michael@0 290
michael@0 291 return process.forget();
michael@0 292 }
michael@0 293
michael@0 294 /**
michael@0 295 * Publish a ContentParent to spare process list.
michael@0 296 */
michael@0 297 void
michael@0 298 PreallocatedProcessManagerImpl::PublishSpareProcess(ContentParent* aContent)
michael@0 299 {
michael@0 300 MOZ_ASSERT(NS_IsMainThread());
michael@0 301
michael@0 302 if (Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
michael@0 303 AutoJSContext cx;
michael@0 304 nsCOMPtr<nsIMessageBroadcaster> ppmm =
michael@0 305 do_GetService("@mozilla.org/parentprocessmessagemanager;1");
michael@0 306 nsresult rv = ppmm->BroadcastAsyncMessage(
michael@0 307 NS_LITERAL_STRING("TEST-ONLY:nuwa-add-new-process"),
michael@0 308 JS::NullHandleValue, JS::NullHandleValue, cx, 1);
michael@0 309 }
michael@0 310
michael@0 311 mSpareProcesses.AppendElement(aContent);
michael@0 312
michael@0 313 if (!mDelayedContentParentRequests.IsEmpty()) {
michael@0 314 nsCOMPtr<nsIRunnable> runnable = mDelayedContentParentRequests[0];
michael@0 315 mDelayedContentParentRequests.RemoveElementAt(0);
michael@0 316 NS_DispatchToMainThread(runnable);
michael@0 317 }
michael@0 318 }
michael@0 319
michael@0 320 void
michael@0 321 PreallocatedProcessManagerImpl::MaybeForgetSpare(ContentParent* aContent)
michael@0 322 {
michael@0 323 MOZ_ASSERT(NS_IsMainThread());
michael@0 324
michael@0 325 if (!mDelayedContentParentRequests.IsEmpty()) {
michael@0 326 if (!mPreallocateAppProcessTask) {
michael@0 327 // This NuwaFork request is urgent. Don't delay it.
michael@0 328 DelayedNuwaFork();
michael@0 329 }
michael@0 330 }
michael@0 331
michael@0 332 if (mSpareProcesses.RemoveElement(aContent)) {
michael@0 333 return;
michael@0 334 }
michael@0 335
michael@0 336 if (aContent == mPreallocatedAppProcess) {
michael@0 337 mPreallocatedAppProcess = nullptr;
michael@0 338 mIsNuwaReady = false;
michael@0 339 ScheduleDelayedNuwaFork();
michael@0 340 }
michael@0 341 }
michael@0 342
michael@0 343 void
michael@0 344 PreallocatedProcessManagerImpl::OnNuwaReady()
michael@0 345 {
michael@0 346 NS_ASSERTION(!mIsNuwaReady, "Multiple Nuwa processes created!");
michael@0 347 ProcessPriorityManager::SetProcessPriority(mPreallocatedAppProcess,
michael@0 348 hal::PROCESS_PRIORITY_MASTER);
michael@0 349 mIsNuwaReady = true;
michael@0 350 if (Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
michael@0 351 AutoJSContext cx;
michael@0 352 nsCOMPtr<nsIMessageBroadcaster> ppmm =
michael@0 353 do_GetService("@mozilla.org/parentprocessmessagemanager;1");
michael@0 354 nsresult rv = ppmm->BroadcastAsyncMessage(
michael@0 355 NS_LITERAL_STRING("TEST-ONLY:nuwa-ready"),
michael@0 356 JS::NullHandleValue, JS::NullHandleValue, cx, 1);
michael@0 357 }
michael@0 358 NuwaFork();
michael@0 359 }
michael@0 360
michael@0 361 bool
michael@0 362 PreallocatedProcessManagerImpl::PreallocatedProcessReady()
michael@0 363 {
michael@0 364 return !mSpareProcesses.IsEmpty();
michael@0 365 }
michael@0 366
michael@0 367
michael@0 368 void
michael@0 369 PreallocatedProcessManagerImpl::NuwaFork()
michael@0 370 {
michael@0 371 mPreallocatedAppProcess->SendNuwaFork();
michael@0 372 }
michael@0 373 #endif
michael@0 374
michael@0 375 void
michael@0 376 PreallocatedProcessManagerImpl::Disable()
michael@0 377 {
michael@0 378 if (!mEnabled) {
michael@0 379 return;
michael@0 380 }
michael@0 381
michael@0 382 mEnabled = false;
michael@0 383
michael@0 384 #ifdef MOZ_NUWA_PROCESS
michael@0 385 // Cancel pending fork.
michael@0 386 if (mPreallocateAppProcessTask) {
michael@0 387 mPreallocateAppProcessTask->Cancel();
michael@0 388 mPreallocateAppProcessTask = nullptr;
michael@0 389 }
michael@0 390 #endif
michael@0 391
michael@0 392 if (mPreallocatedAppProcess) {
michael@0 393 #ifdef MOZ_NUWA_PROCESS
michael@0 394 while (mSpareProcesses.Length() > 0){
michael@0 395 nsRefPtr<ContentParent> process = mSpareProcesses[0];
michael@0 396 process->Close();
michael@0 397 mSpareProcesses.RemoveElementAt(0);
michael@0 398 }
michael@0 399 mIsNuwaReady = false;
michael@0 400 #endif
michael@0 401 mPreallocatedAppProcess->Close();
michael@0 402 mPreallocatedAppProcess = nullptr;
michael@0 403 }
michael@0 404 }
michael@0 405
michael@0 406 void
michael@0 407 PreallocatedProcessManagerImpl::ObserveProcessShutdown(nsISupports* aSubject)
michael@0 408 {
michael@0 409 if (!mPreallocatedAppProcess) {
michael@0 410 return;
michael@0 411 }
michael@0 412
michael@0 413 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
michael@0 414 NS_ENSURE_TRUE_VOID(props);
michael@0 415
michael@0 416 uint64_t childID = CONTENT_PROCESS_ID_UNKNOWN;
michael@0 417 props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), &childID);
michael@0 418 NS_ENSURE_TRUE_VOID(childID != CONTENT_PROCESS_ID_UNKNOWN);
michael@0 419
michael@0 420 if (childID == mPreallocatedAppProcess->ChildID()) {
michael@0 421 mPreallocatedAppProcess = nullptr;
michael@0 422 }
michael@0 423 }
michael@0 424
michael@0 425 inline PreallocatedProcessManagerImpl* GetPPMImpl()
michael@0 426 {
michael@0 427 return PreallocatedProcessManagerImpl::Singleton();
michael@0 428 }
michael@0 429
michael@0 430 } // anonymous namespace
michael@0 431
michael@0 432 namespace mozilla {
michael@0 433
michael@0 434 /* static */ void
michael@0 435 PreallocatedProcessManager::AllocateAfterDelay()
michael@0 436 {
michael@0 437 #ifdef MOZ_NUWA_PROCESS
michael@0 438 GetPPMImpl()->ScheduleDelayedNuwaFork();
michael@0 439 #else
michael@0 440 GetPPMImpl()->AllocateAfterDelay();
michael@0 441 #endif
michael@0 442 }
michael@0 443
michael@0 444 /* static */ void
michael@0 445 PreallocatedProcessManager::AllocateOnIdle()
michael@0 446 {
michael@0 447 GetPPMImpl()->AllocateOnIdle();
michael@0 448 }
michael@0 449
michael@0 450 /* static */ void
michael@0 451 PreallocatedProcessManager::AllocateNow()
michael@0 452 {
michael@0 453 GetPPMImpl()->AllocateNow();
michael@0 454 }
michael@0 455
michael@0 456 /* static */ already_AddRefed<ContentParent>
michael@0 457 PreallocatedProcessManager::Take()
michael@0 458 {
michael@0 459 #ifdef MOZ_NUWA_PROCESS
michael@0 460 return GetPPMImpl()->GetSpareProcess();
michael@0 461 #else
michael@0 462 return GetPPMImpl()->Take();
michael@0 463 #endif
michael@0 464 }
michael@0 465
michael@0 466 #ifdef MOZ_NUWA_PROCESS
michael@0 467 /* static */ void
michael@0 468 PreallocatedProcessManager::PublishSpareProcess(ContentParent* aContent)
michael@0 469 {
michael@0 470 GetPPMImpl()->PublishSpareProcess(aContent);
michael@0 471 }
michael@0 472
michael@0 473 /* static */ void
michael@0 474 PreallocatedProcessManager::MaybeForgetSpare(ContentParent* aContent)
michael@0 475 {
michael@0 476 GetPPMImpl()->MaybeForgetSpare(aContent);
michael@0 477 }
michael@0 478
michael@0 479 /* static */ void
michael@0 480 PreallocatedProcessManager::OnNuwaReady()
michael@0 481 {
michael@0 482 GetPPMImpl()->OnNuwaReady();
michael@0 483 }
michael@0 484
michael@0 485 /*static */ bool
michael@0 486 PreallocatedProcessManager::PreallocatedProcessReady()
michael@0 487 {
michael@0 488 return GetPPMImpl()->PreallocatedProcessReady();
michael@0 489 }
michael@0 490
michael@0 491 /* static */ void
michael@0 492 PreallocatedProcessManager::RunAfterPreallocatedProcessReady(nsIRunnable* aRequest)
michael@0 493 {
michael@0 494 GetPPMImpl()->RunAfterPreallocatedProcessReady(aRequest);
michael@0 495 }
michael@0 496
michael@0 497 #endif
michael@0 498
michael@0 499 } // namespace mozilla

mercurial