Backed out changeset 1d9301697aa0 (bug 1887752) for causing failures on browser_all_f...
[gecko.git] / dom / ipc / ProcessPriorityManager.cpp
blob10fca879a658004b26a29fcca6950408e1f66996
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 "ProcessPriorityManager.h"
8 #include "mozilla/ClearOnShutdown.h"
9 #include "mozilla/dom/CanonicalBrowsingContext.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/BrowserHost.h"
13 #include "mozilla/dom/BrowserParent.h"
14 #include "mozilla/Hal.h"
15 #include "mozilla/IntegerPrintfMacros.h"
16 #include "mozilla/Preferences.h"
17 #include "mozilla/ProfilerMarkers.h"
18 #include "mozilla/ProfilerState.h"
19 #include "mozilla/Services.h"
20 #include "mozilla/StaticPrefs_dom.h"
21 #include "mozilla/StaticPrefs_threads.h"
22 #include "mozilla/Telemetry.h"
23 #include "mozilla/Unused.h"
24 #include "mozilla/Logging.h"
25 #include "nsPrintfCString.h"
26 #include "nsXULAppAPI.h"
27 #include "nsFrameLoader.h"
28 #include "nsINamed.h"
29 #include "nsIObserverService.h"
30 #include "StaticPtr.h"
31 #include "nsIObserver.h"
32 #include "nsITimer.h"
33 #include "nsIPropertyBag2.h"
34 #include "nsComponentManagerUtils.h"
35 #include "nsCRT.h"
36 #include "nsTHashSet.h"
37 #include "nsQueryObject.h"
38 #include "nsTHashMap.h"
40 using namespace mozilla;
41 using namespace mozilla::dom;
42 using namespace mozilla::hal;
44 #ifdef XP_WIN
45 # include <process.h>
46 # define getpid _getpid
47 #else
48 # include <unistd.h>
49 #endif
51 #ifdef LOG
52 # undef LOG
53 #endif
55 // Use LOGP inside a ParticularProcessPriorityManager method; use LOG
56 // everywhere else. LOGP prints out information about the particular process
57 // priority manager.
59 // (Wow, our logging story is a huge mess.)
61 // #define ENABLE_LOGGING 1
63 #if defined(ANDROID) && defined(ENABLE_LOGGING)
64 # include <android/log.h>
65 # define LOG(fmt, ...) \
66 __android_log_print(ANDROID_LOG_INFO, "Gecko:ProcessPriorityManager", fmt, \
67 ##__VA_ARGS__)
68 # define LOGP(fmt, ...) \
69 __android_log_print( \
70 ANDROID_LOG_INFO, "Gecko:ProcessPriorityManager", \
71 "[%schild-id=%" PRIu64 ", pid=%d] " fmt, NameWithComma().get(), \
72 static_cast<uint64_t>(ChildID()), Pid(), ##__VA_ARGS__)
74 #elif defined(ENABLE_LOGGING)
75 # define LOG(fmt, ...) \
76 printf("ProcessPriorityManager - " fmt "\n", ##__VA_ARGS__)
77 # define LOGP(fmt, ...) \
78 printf("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " fmt \
79 "\n", \
80 NameWithComma().get(), static_cast<uint64_t>(ChildID()), Pid(), \
81 ##__VA_ARGS__)
82 #else
83 static LogModule* GetPPMLog() {
84 static LazyLogModule sLog("ProcessPriorityManager");
85 return sLog;
87 # define LOG(fmt, ...) \
88 MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
89 ("ProcessPriorityManager - " fmt, ##__VA_ARGS__))
90 # define LOGP(fmt, ...) \
91 MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
92 ("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " fmt, \
93 NameWithComma().get(), static_cast<uint64_t>(ChildID()), Pid(), \
94 ##__VA_ARGS__))
95 #endif
97 namespace geckoprofiler::markers {
98 struct SubProcessPriorityChange {
99 static constexpr Span<const char> MarkerTypeName() {
100 return MakeStringSpan("subprocessprioritychange");
102 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
103 int32_t aPid,
104 const ProfilerString8View& aPreviousPriority,
105 const ProfilerString8View& aNewPriority) {
106 aWriter.IntProperty("pid", aPid);
107 aWriter.StringProperty("Before", aPreviousPriority);
108 aWriter.StringProperty("After", aNewPriority);
110 static MarkerSchema MarkerTypeDisplay() {
111 using MS = MarkerSchema;
112 MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
113 schema.AddKeyFormat("pid", MS::Format::Integer);
114 schema.AddKeyFormat("Before", MS::Format::String);
115 schema.AddKeyFormat("After", MS::Format::String);
116 schema.SetAllLabels(
117 "priority of child {marker.data.pid}:"
118 " {marker.data.Before} -> {marker.data.After}");
119 return schema;
123 struct SubProcessPriority {
124 static constexpr Span<const char> MarkerTypeName() {
125 return MakeStringSpan("subprocesspriority");
127 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
128 int32_t aPid,
129 const ProfilerString8View& aPriority,
130 const ProfilingState& aProfilingState) {
131 aWriter.IntProperty("pid", aPid);
132 aWriter.StringProperty("Priority", aPriority);
133 aWriter.StringProperty("Marker cause",
134 ProfilerString8View::WrapNullTerminatedString(
135 ProfilingStateToString(aProfilingState)));
137 static MarkerSchema MarkerTypeDisplay() {
138 using MS = MarkerSchema;
139 MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
140 schema.AddKeyFormat("pid", MS::Format::Integer);
141 schema.AddKeyFormat("Priority", MS::Format::String);
142 schema.AddKeyFormat("Marker cause", MS::Format::String);
143 schema.SetAllLabels(
144 "priority of child {marker.data.pid}: {marker.data.Priority}");
145 return schema;
148 } // namespace geckoprofiler::markers
150 namespace {
152 class ParticularProcessPriorityManager;
155 * This singleton class does the work to implement the process priority manager
156 * in the main process. This class may not be used in child processes. (You
157 * can call StaticInit, but it won't do anything, and GetSingleton() will
158 * return null.)
160 * ProcessPriorityManager::CurrentProcessIsForeground() and
161 * ProcessPriorityManager::AnyProcessHasHighPriority() which can be called in
162 * any process, are handled separately, by the ProcessPriorityManagerChild
163 * class.
165 class ProcessPriorityManagerImpl final : public nsIObserver,
166 public nsSupportsWeakReference {
167 public:
169 * If we're in the main process, get the ProcessPriorityManagerImpl
170 * singleton. If we're in a child process, return null.
172 static ProcessPriorityManagerImpl* GetSingleton();
174 static void StaticInit();
175 static bool PrefsEnabled();
176 static void SetProcessPriorityIfEnabled(int aPid, ProcessPriority aPriority);
177 static bool TestMode();
179 NS_DECL_ISUPPORTS
180 NS_DECL_NSIOBSERVER
183 * This function implements ProcessPriorityManager::SetProcessPriority.
185 void SetProcessPriority(ContentParent* aContentParent,
186 ProcessPriority aPriority);
189 * If a magic testing-only pref is set, notify the observer service on the
190 * given topic with the given data. This is used for testing
192 void FireTestOnlyObserverNotification(const char* aTopic,
193 const nsACString& aData);
196 * This must be called by a ParticularProcessPriorityManager when it changes
197 * its priority.
199 void NotifyProcessPriorityChanged(
200 ParticularProcessPriorityManager* aParticularManager,
201 hal::ProcessPriority aOldPriority);
203 void BrowserPriorityChanged(CanonicalBrowsingContext* aBC, bool aPriority);
204 void BrowserPriorityChanged(BrowserParent* aBrowserParent, bool aPriority);
206 private:
207 static bool sPrefListenersRegistered;
208 static bool sInitialized;
209 static StaticRefPtr<ProcessPriorityManagerImpl> sSingleton;
211 static void PrefChangedCallback(const char* aPref, void* aClosure);
213 ProcessPriorityManagerImpl();
214 ~ProcessPriorityManagerImpl();
215 ProcessPriorityManagerImpl(const ProcessPriorityManagerImpl&) = delete;
217 const ProcessPriorityManagerImpl& operator=(
218 const ProcessPriorityManagerImpl&) = delete;
220 void Init();
222 already_AddRefed<ParticularProcessPriorityManager>
223 GetParticularProcessPriorityManager(ContentParent* aContentParent);
225 void ObserveContentParentDestroyed(nsISupports* aSubject);
227 nsTHashMap<uint64_t, RefPtr<ParticularProcessPriorityManager> >
228 mParticularManagers;
230 /** Contains the PIDs of child processes holding high-priority wakelocks */
231 nsTHashSet<uint64_t> mHighPriorityChildIDs;
235 * This singleton class implements the parts of the process priority manager
236 * that are available from all processes.
238 class ProcessPriorityManagerChild final : public nsIObserver {
239 public:
240 static void StaticInit();
241 static ProcessPriorityManagerChild* Singleton();
243 NS_DECL_ISUPPORTS
244 NS_DECL_NSIOBSERVER
246 bool CurrentProcessIsForeground();
248 private:
249 static StaticRefPtr<ProcessPriorityManagerChild> sSingleton;
251 ProcessPriorityManagerChild();
252 ~ProcessPriorityManagerChild() = default;
253 ProcessPriorityManagerChild(const ProcessPriorityManagerChild&) = delete;
255 const ProcessPriorityManagerChild& operator=(
256 const ProcessPriorityManagerChild&) = delete;
258 void Init();
260 hal::ProcessPriority mCachedPriority;
264 * This class manages the priority of one particular process. It is
265 * main-process only.
267 class ParticularProcessPriorityManager final : public WakeLockObserver,
268 public nsITimerCallback,
269 public nsINamed,
270 public nsSupportsWeakReference {
271 ~ParticularProcessPriorityManager();
273 public:
274 explicit ParticularProcessPriorityManager(ContentParent* aContentParent);
276 NS_DECL_ISUPPORTS
277 NS_DECL_NSITIMERCALLBACK
279 virtual void Notify(const WakeLockInformation& aInfo) override;
280 void Init();
282 int32_t Pid() const;
283 uint64_t ChildID() const;
286 * Used in logging, this method returns the ContentParent's name followed by
287 * ", ". If we can't get the ContentParent's name for some reason, it
288 * returns an empty string.
290 * The reference returned here is guaranteed to be live until the next call
291 * to NameWithComma() or until the ParticularProcessPriorityManager is
292 * destroyed, whichever comes first.
294 const nsAutoCString& NameWithComma();
296 ProcessPriority CurrentPriority();
297 ProcessPriority ComputePriority();
299 enum TimeoutPref {
300 BACKGROUND_PERCEIVABLE_GRACE_PERIOD,
301 BACKGROUND_GRACE_PERIOD,
304 void ScheduleResetPriority(TimeoutPref aTimeoutPref);
305 void ResetPriority();
306 void ResetPriorityNow();
307 void SetPriorityNow(ProcessPriority aPriority);
309 void BrowserPriorityChanged(BrowserParent* aBrowserParent, bool aPriority);
311 void ShutDown();
313 NS_IMETHOD GetName(nsACString& aName) override {
314 aName.AssignLiteral("ParticularProcessPriorityManager");
315 return NS_OK;
318 private:
319 void FireTestOnlyObserverNotification(const char* aTopic, const char* aData);
321 bool IsHoldingWakeLock(const nsAString& aTopic);
323 ContentParent* mContentParent;
324 uint64_t mChildID;
325 ProcessPriority mPriority;
326 bool mHoldsCPUWakeLock;
327 bool mHoldsHighPriorityWakeLock;
328 bool mHoldsPlayingAudioWakeLock;
329 bool mHoldsPlayingVideoWakeLock;
332 * Used to implement NameWithComma().
334 nsAutoCString mNameWithComma;
336 nsCOMPtr<nsITimer> mResetPriorityTimer;
338 // This hashtable contains the list of high priority TabIds for this process.
339 nsTHashSet<uint64_t> mHighPriorityBrowserParents;
342 /* static */
343 bool ProcessPriorityManagerImpl::sInitialized = false;
344 /* static */
345 bool ProcessPriorityManagerImpl::sPrefListenersRegistered = false;
346 /* static */
347 StaticRefPtr<ProcessPriorityManagerImpl> ProcessPriorityManagerImpl::sSingleton;
349 NS_IMPL_ISUPPORTS(ProcessPriorityManagerImpl, nsIObserver,
350 nsISupportsWeakReference);
352 /* static */
353 void ProcessPriorityManagerImpl::PrefChangedCallback(const char* aPref,
354 void* aClosure) {
355 StaticInit();
356 if (!PrefsEnabled() && sSingleton) {
357 sSingleton = nullptr;
358 sInitialized = false;
362 /* static */
363 bool ProcessPriorityManagerImpl::PrefsEnabled() {
364 return StaticPrefs::dom_ipc_processPriorityManager_enabled();
367 /* static */
368 void ProcessPriorityManagerImpl::SetProcessPriorityIfEnabled(
369 int aPid, ProcessPriority aPriority) {
370 // The preference doesn't disable the process priority manager, but only its
371 // effect. This way the IPCs still happen and can be used to collect telemetry
372 // about CPU use.
373 if (PrefsEnabled()) {
374 hal::SetProcessPriority(aPid, aPriority);
378 /* static */
379 bool ProcessPriorityManagerImpl::TestMode() {
380 return StaticPrefs::dom_ipc_processPriorityManager_testMode();
383 /* static */
384 void ProcessPriorityManagerImpl::StaticInit() {
385 if (sInitialized) {
386 return;
389 // The process priority manager is main-process only.
390 if (!XRE_IsParentProcess()) {
391 sInitialized = true;
392 return;
395 // Run StaticInit() again if the pref changes. We don't expect this to
396 // happen in normal operation, but it happens during testing.
397 if (!sPrefListenersRegistered) {
398 sPrefListenersRegistered = true;
399 Preferences::RegisterCallback(PrefChangedCallback,
400 "dom.ipc.processPriorityManager.enabled");
403 sInitialized = true;
405 sSingleton = new ProcessPriorityManagerImpl();
406 sSingleton->Init();
407 ClearOnShutdown(&sSingleton);
410 /* static */
411 ProcessPriorityManagerImpl* ProcessPriorityManagerImpl::GetSingleton() {
412 if (!sSingleton) {
413 StaticInit();
416 return sSingleton;
419 ProcessPriorityManagerImpl::ProcessPriorityManagerImpl() {
420 MOZ_ASSERT(XRE_IsParentProcess());
423 ProcessPriorityManagerImpl::~ProcessPriorityManagerImpl() = default;
425 void ProcessPriorityManagerImpl::Init() {
426 LOG("Starting up. This is the parent process.");
428 // The parent process's priority never changes; set it here and then forget
429 // about it. We'll manage only subprocesses' priorities using the process
430 // priority manager.
431 SetProcessPriorityIfEnabled(getpid(), PROCESS_PRIORITY_PARENT_PROCESS);
433 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
434 if (os) {
435 os->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
439 NS_IMETHODIMP
440 ProcessPriorityManagerImpl::Observe(nsISupports* aSubject, const char* aTopic,
441 const char16_t* aData) {
442 nsDependentCString topic(aTopic);
443 if (topic.EqualsLiteral("ipc:content-shutdown")) {
444 ObserveContentParentDestroyed(aSubject);
445 } else {
446 MOZ_ASSERT(false);
449 return NS_OK;
452 already_AddRefed<ParticularProcessPriorityManager>
453 ProcessPriorityManagerImpl::GetParticularProcessPriorityManager(
454 ContentParent* aContentParent) {
455 // If this content parent is already being shut down, there's no
456 // need to adjust its priority.
457 if (aContentParent->IsDead()) {
458 return nullptr;
461 const uint64_t cpId = aContentParent->ChildID();
462 return mParticularManagers.WithEntryHandle(cpId, [&](auto&& entry) {
463 if (!entry) {
464 entry.Insert(new ParticularProcessPriorityManager(aContentParent));
465 entry.Data()->Init();
467 return do_AddRef(entry.Data());
471 void ProcessPriorityManagerImpl::SetProcessPriority(
472 ContentParent* aContentParent, ProcessPriority aPriority) {
473 MOZ_ASSERT(aContentParent);
474 if (RefPtr pppm = GetParticularProcessPriorityManager(aContentParent)) {
475 pppm->SetPriorityNow(aPriority);
479 void ProcessPriorityManagerImpl::ObserveContentParentDestroyed(
480 nsISupports* aSubject) {
481 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
482 NS_ENSURE_TRUE_VOID(props);
484 uint64_t childID = CONTENT_PROCESS_ID_UNKNOWN;
485 props->GetPropertyAsUint64(u"childID"_ns, &childID);
486 NS_ENSURE_TRUE_VOID(childID != CONTENT_PROCESS_ID_UNKNOWN);
488 if (auto entry = mParticularManagers.Lookup(childID)) {
489 entry.Data()->ShutDown();
490 mHighPriorityChildIDs.Remove(childID);
491 entry.Remove();
495 void ProcessPriorityManagerImpl::NotifyProcessPriorityChanged(
496 ParticularProcessPriorityManager* aParticularManager,
497 ProcessPriority aOldPriority) {
498 ProcessPriority newPriority = aParticularManager->CurrentPriority();
500 if (newPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH &&
501 aOldPriority < PROCESS_PRIORITY_FOREGROUND_HIGH) {
502 mHighPriorityChildIDs.Insert(aParticularManager->ChildID());
503 } else if (newPriority < PROCESS_PRIORITY_FOREGROUND_HIGH &&
504 aOldPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH) {
505 mHighPriorityChildIDs.Remove(aParticularManager->ChildID());
509 static nsCString BCToString(dom::CanonicalBrowsingContext* aBC) {
510 nsCOMPtr<nsIURI> uri = aBC->GetCurrentURI();
511 return nsPrintfCString("id=%" PRIu64 " uri=%s active=%d pactive=%d",
512 aBC->Id(),
513 uri ? uri->GetSpecOrDefault().get() : "(no uri)",
514 aBC->IsActive(), aBC->IsPriorityActive());
517 void ProcessPriorityManagerImpl::BrowserPriorityChanged(
518 dom::CanonicalBrowsingContext* aBC, bool aPriority) {
519 MOZ_ASSERT(aBC->IsTop());
521 LOG("BrowserPriorityChanged(%s, %d)\n", BCToString(aBC).get(), aPriority);
523 bool alreadyActive = aBC->IsPriorityActive();
524 if (alreadyActive == aPriority) {
525 return;
528 Telemetry::ScalarAdd(
529 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_CHANGE_CONSIDERED, 1);
531 aBC->SetPriorityActive(aPriority);
533 aBC->PreOrderWalk([&](BrowsingContext* aContext) {
534 CanonicalBrowsingContext* canonical = aContext->Canonical();
535 LOG("PreOrderWalk for %p: %p -> %p, %p\n", aBC, canonical,
536 canonical->GetContentParent(), canonical->GetBrowserParent());
537 if (ContentParent* cp = canonical->GetContentParent()) {
538 if (RefPtr pppm = GetParticularProcessPriorityManager(cp)) {
539 if (auto* bp = canonical->GetBrowserParent()) {
540 pppm->BrowserPriorityChanged(bp, aPriority);
547 void ProcessPriorityManagerImpl::BrowserPriorityChanged(
548 BrowserParent* aBrowserParent, bool aPriority) {
549 LOG("BrowserPriorityChanged(bp=%p, %d)\n", aBrowserParent, aPriority);
551 if (RefPtr pppm =
552 GetParticularProcessPriorityManager(aBrowserParent->Manager())) {
553 Telemetry::ScalarAdd(
554 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_CHANGE_CONSIDERED,
556 pppm->BrowserPriorityChanged(aBrowserParent, aPriority);
560 NS_IMPL_ISUPPORTS(ParticularProcessPriorityManager, nsITimerCallback,
561 nsISupportsWeakReference, nsINamed);
563 ParticularProcessPriorityManager::ParticularProcessPriorityManager(
564 ContentParent* aContentParent)
565 : mContentParent(aContentParent),
566 mChildID(aContentParent->ChildID()),
567 mPriority(PROCESS_PRIORITY_UNKNOWN),
568 mHoldsCPUWakeLock(false),
569 mHoldsHighPriorityWakeLock(false),
570 mHoldsPlayingAudioWakeLock(false),
571 mHoldsPlayingVideoWakeLock(false) {
572 MOZ_ASSERT(XRE_IsParentProcess());
573 MOZ_RELEASE_ASSERT(!aContentParent->IsDead());
574 LOGP("Creating ParticularProcessPriorityManager.");
575 // Our static analysis doesn't allow capturing ref-counted pointers in
576 // lambdas, so we need to hide it in a uintptr_t. This is safe because this
577 // lambda will be destroyed in ~ParticularProcessPriorityManager().
578 uintptr_t self = reinterpret_cast<uintptr_t>(this);
579 profiler_add_state_change_callback(
580 AllProfilingStates(),
581 [self](ProfilingState aProfilingState) {
582 const ParticularProcessPriorityManager* selfPtr =
583 reinterpret_cast<const ParticularProcessPriorityManager*>(self);
584 PROFILER_MARKER("Subprocess Priority", OTHER,
585 MarkerThreadId::MainThread(), SubProcessPriority,
586 selfPtr->Pid(),
587 ProfilerString8View::WrapNullTerminatedString(
588 ProcessPriorityToString(selfPtr->mPriority)),
589 aProfilingState);
591 self);
594 void ParticularProcessPriorityManager::Init() {
595 RegisterWakeLockObserver(this);
597 // This process may already hold the CPU lock; for example, our parent may
598 // have acquired it on our behalf.
599 mHoldsCPUWakeLock = IsHoldingWakeLock(u"cpu"_ns);
600 mHoldsHighPriorityWakeLock = IsHoldingWakeLock(u"high-priority"_ns);
601 mHoldsPlayingAudioWakeLock = IsHoldingWakeLock(u"audio-playing"_ns);
602 mHoldsPlayingVideoWakeLock = IsHoldingWakeLock(u"video-playing"_ns);
604 LOGP(
605 "Done starting up. mHoldsCPUWakeLock=%d, "
606 "mHoldsHighPriorityWakeLock=%d, mHoldsPlayingAudioWakeLock=%d, "
607 "mHoldsPlayingVideoWakeLock=%d",
608 mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock, mHoldsPlayingAudioWakeLock,
609 mHoldsPlayingVideoWakeLock);
612 bool ParticularProcessPriorityManager::IsHoldingWakeLock(
613 const nsAString& aTopic) {
614 WakeLockInformation info;
615 GetWakeLockInfo(aTopic, &info);
616 return info.lockingProcesses().Contains(ChildID());
619 ParticularProcessPriorityManager::~ParticularProcessPriorityManager() {
620 LOGP("Destroying ParticularProcessPriorityManager.");
622 profiler_remove_state_change_callback(reinterpret_cast<uintptr_t>(this));
624 ShutDown();
627 /* virtual */
628 void ParticularProcessPriorityManager::Notify(
629 const WakeLockInformation& aInfo) {
630 if (!mContentParent) {
631 // We've been shut down.
632 return;
635 bool* dest = nullptr;
636 if (aInfo.topic().EqualsLiteral("cpu")) {
637 dest = &mHoldsCPUWakeLock;
638 } else if (aInfo.topic().EqualsLiteral("high-priority")) {
639 dest = &mHoldsHighPriorityWakeLock;
640 } else if (aInfo.topic().EqualsLiteral("audio-playing")) {
641 dest = &mHoldsPlayingAudioWakeLock;
642 } else if (aInfo.topic().EqualsLiteral("video-playing")) {
643 dest = &mHoldsPlayingVideoWakeLock;
646 if (dest) {
647 bool thisProcessLocks = aInfo.lockingProcesses().Contains(ChildID());
648 if (thisProcessLocks != *dest) {
649 *dest = thisProcessLocks;
650 LOGP(
651 "Got wake lock changed event. "
652 "Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d, "
653 "mHoldsPlayingAudioWakeLock=%d, mHoldsPlayingVideoWakeLock=%d",
654 mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock,
655 mHoldsPlayingAudioWakeLock, mHoldsPlayingVideoWakeLock);
656 ResetPriority();
661 uint64_t ParticularProcessPriorityManager::ChildID() const {
662 // We have to cache mContentParent->ChildID() instead of getting it from the
663 // ContentParent each time because after ShutDown() is called, mContentParent
664 // is null. If we didn't cache ChildID(), then we wouldn't be able to run
665 // LOGP() after ShutDown().
666 return mChildID;
669 int32_t ParticularProcessPriorityManager::Pid() const {
670 return mContentParent ? mContentParent->Pid() : -1;
673 const nsAutoCString& ParticularProcessPriorityManager::NameWithComma() {
674 mNameWithComma.Truncate();
675 if (!mContentParent) {
676 return mNameWithComma; // empty string
679 nsAutoString name;
680 mContentParent->FriendlyName(name);
681 if (name.IsEmpty()) {
682 return mNameWithComma; // empty string
685 CopyUTF16toUTF8(name, mNameWithComma);
686 mNameWithComma.AppendLiteral(", ");
687 return mNameWithComma;
690 void ParticularProcessPriorityManager::ResetPriority() {
691 ProcessPriority processPriority = ComputePriority();
692 if (mPriority == PROCESS_PRIORITY_UNKNOWN || mPriority > processPriority) {
693 // Apps set at a perceivable background priority are often playing media.
694 // Most media will have short gaps while changing tracks between songs,
695 // switching videos, etc. Give these apps a longer grace period so they
696 // can get their next track started, if there is one, before getting
697 // downgraded.
698 if (mPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
699 ScheduleResetPriority(BACKGROUND_PERCEIVABLE_GRACE_PERIOD);
700 } else {
701 ScheduleResetPriority(BACKGROUND_GRACE_PERIOD);
703 return;
706 SetPriorityNow(processPriority);
709 void ParticularProcessPriorityManager::ResetPriorityNow() {
710 SetPriorityNow(ComputePriority());
713 void ParticularProcessPriorityManager::ScheduleResetPriority(
714 TimeoutPref aTimeoutPref) {
715 if (mResetPriorityTimer) {
716 LOGP("ScheduleResetPriority bailing; the timer is already running.");
717 return;
720 uint32_t timeout = 0;
721 switch (aTimeoutPref) {
722 case BACKGROUND_PERCEIVABLE_GRACE_PERIOD:
723 timeout = StaticPrefs::
724 dom_ipc_processPriorityManager_backgroundPerceivableGracePeriodMS();
725 break;
726 case BACKGROUND_GRACE_PERIOD:
727 timeout =
728 StaticPrefs::dom_ipc_processPriorityManager_backgroundGracePeriodMS();
729 break;
730 default:
731 MOZ_ASSERT(false, "Unrecognized timeout pref");
732 break;
735 LOGP("Scheduling reset timer to fire in %dms.", timeout);
736 NS_NewTimerWithCallback(getter_AddRefs(mResetPriorityTimer), this, timeout,
737 nsITimer::TYPE_ONE_SHOT);
740 NS_IMETHODIMP
741 ParticularProcessPriorityManager::Notify(nsITimer* aTimer) {
742 LOGP("Reset priority timer callback; about to ResetPriorityNow.");
743 ResetPriorityNow();
744 mResetPriorityTimer = nullptr;
745 return NS_OK;
748 ProcessPriority ParticularProcessPriorityManager::CurrentPriority() {
749 return mPriority;
752 ProcessPriority ParticularProcessPriorityManager::ComputePriority() {
753 if (!mHighPriorityBrowserParents.IsEmpty() ||
754 mContentParent->GetRemoteType() == EXTENSION_REMOTE_TYPE ||
755 mHoldsPlayingAudioWakeLock) {
756 return PROCESS_PRIORITY_FOREGROUND;
759 if (mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock ||
760 mHoldsPlayingVideoWakeLock) {
761 return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
764 return PROCESS_PRIORITY_BACKGROUND;
767 #ifdef XP_MACOSX
768 // Method used for setting QoS levels on background main threads.
769 static bool PriorityUsesLowPowerMainThread(
770 const hal::ProcessPriority& aPriority) {
771 return aPriority == hal::PROCESS_PRIORITY_BACKGROUND ||
772 aPriority == hal::PROCESS_PRIORITY_PREALLOC;
774 // Method reduces redundancy in pref check while addressing the edge case
775 // where a pref is flipped to false during active browser use.
776 static bool PrefsUseLowPriorityThreads() {
777 return StaticPrefs::threads_use_low_power_enabled() &&
778 StaticPrefs::threads_lower_mainthread_priority_in_background_enabled();
780 #endif
782 void ParticularProcessPriorityManager::SetPriorityNow(
783 ProcessPriority aPriority) {
784 if (aPriority == PROCESS_PRIORITY_UNKNOWN) {
785 MOZ_ASSERT(false);
786 return;
789 LOGP("Changing priority from %s to %s (cp=%p).",
790 ProcessPriorityToString(mPriority), ProcessPriorityToString(aPriority),
791 mContentParent);
793 if (!mContentParent || mPriority == aPriority) {
794 return;
797 PROFILER_MARKER(
798 "Subprocess Priority", OTHER,
799 MarkerOptions(MarkerThreadId::MainThread(), MarkerStack::Capture()),
800 SubProcessPriorityChange, this->Pid(),
801 ProfilerString8View::WrapNullTerminatedString(
802 ProcessPriorityToString(mPriority)),
803 ProfilerString8View::WrapNullTerminatedString(
804 ProcessPriorityToString(aPriority)));
806 ProcessPriority oldPriority = mPriority;
808 mPriority = aPriority;
810 // We skip incrementing the DOM_CONTENTPROCESS_OS_PRIORITY_RAISED if we're
811 // transitioning from the PROCESS_PRIORITY_UNKNOWN level, which is where
812 // we initialize at.
813 if (oldPriority < mPriority && oldPriority != PROCESS_PRIORITY_UNKNOWN) {
814 Telemetry::ScalarAdd(
815 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_RAISED, 1);
816 } else if (oldPriority > mPriority) {
817 Telemetry::ScalarAdd(
818 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_LOWERED, 1);
821 ProcessPriorityManagerImpl::SetProcessPriorityIfEnabled(Pid(), mPriority);
823 if (oldPriority != mPriority) {
824 ProcessPriorityManagerImpl::GetSingleton()->NotifyProcessPriorityChanged(
825 this, oldPriority);
827 #ifdef XP_MACOSX
828 // In cases where we have low-power threads enabled (such as on MacOS) we
829 // can go ahead and put the main thread in the background here. If the new
830 // priority is the background priority, we can tell the OS to put the main
831 // thread on low-power cores. Alternately, if we are changing from the
832 // background to a higher priority, we change the main thread back to its
833 // normal state.
834 // During shutdown, we will manually set the priority to the highest
835 // possible and disallow any additional priority changes.
837 // The messages for this will be relayed using the ProcessHangMonitor such
838 // that the priority can be raised even if the main thread is unresponsive.
839 if (!mContentParent->IsShuttingDown() &&
840 PriorityUsesLowPowerMainThread(mPriority) !=
841 PriorityUsesLowPowerMainThread(oldPriority)) {
842 if (PriorityUsesLowPowerMainThread(mPriority) &&
843 PrefsUseLowPriorityThreads()) {
844 mContentParent->SetMainThreadQoSPriority(nsIThread::QOS_PRIORITY_LOW);
845 } else if (PriorityUsesLowPowerMainThread(oldPriority)) {
846 // In the event that the user changes prefs while tabs are in the
847 // background, we still want to have the ability to put the main thread
848 // back in the foreground to keep tabs from being stuck in the
849 // background priority.
850 mContentParent->SetMainThreadQoSPriority(
851 nsIThread::QOS_PRIORITY_NORMAL);
854 #endif
856 Unused << mContentParent->SendNotifyProcessPriorityChanged(mPriority);
859 FireTestOnlyObserverNotification("process-priority-set",
860 ProcessPriorityToString(mPriority));
863 void ParticularProcessPriorityManager::BrowserPriorityChanged(
864 BrowserParent* aBrowserParent, bool aPriority) {
865 MOZ_ASSERT(aBrowserParent);
867 if (!aPriority) {
868 mHighPriorityBrowserParents.Remove(aBrowserParent->GetTabId());
869 } else {
870 mHighPriorityBrowserParents.Insert(aBrowserParent->GetTabId());
873 ResetPriority();
876 void ParticularProcessPriorityManager::ShutDown() {
877 LOGP("shutdown for %p (mContentParent %p)", this, mContentParent);
879 // Unregister our wake lock observer if ShutDown hasn't been called. (The
880 // wake lock observer takes raw refs, so we don't want to take chances here!)
881 // We don't call UnregisterWakeLockObserver unconditionally because the code
882 // will print a warning if it's called unnecessarily.
883 if (mContentParent) {
884 UnregisterWakeLockObserver(this);
887 if (mResetPriorityTimer) {
888 mResetPriorityTimer->Cancel();
889 mResetPriorityTimer = nullptr;
892 mContentParent = nullptr;
895 void ProcessPriorityManagerImpl::FireTestOnlyObserverNotification(
896 const char* aTopic, const nsACString& aData) {
897 if (!TestMode()) {
898 return;
901 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
902 NS_ENSURE_TRUE_VOID(os);
904 nsPrintfCString topic("process-priority-manager:TEST-ONLY:%s", aTopic);
906 LOG("Notifying observer %s, data %s", topic.get(),
907 PromiseFlatCString(aData).get());
908 os->NotifyObservers(nullptr, topic.get(), NS_ConvertUTF8toUTF16(aData).get());
911 void ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
912 const char* aTopic, const char* aData) {
913 MOZ_ASSERT(aData, "Pass in data");
915 if (!ProcessPriorityManagerImpl::TestMode()) {
916 return;
919 nsAutoCString data(nsPrintfCString("%" PRIu64, ChildID()));
920 data.Append(':');
921 data.AppendASCII(aData);
923 // ProcessPriorityManagerImpl::GetSingleton() is guaranteed not to return
924 // null, since ProcessPriorityManagerImpl is the only class which creates
925 // ParticularProcessPriorityManagers.
927 ProcessPriorityManagerImpl::GetSingleton()->FireTestOnlyObserverNotification(
928 aTopic, data);
931 StaticRefPtr<ProcessPriorityManagerChild>
932 ProcessPriorityManagerChild::sSingleton;
934 /* static */
935 void ProcessPriorityManagerChild::StaticInit() {
936 if (!sSingleton) {
937 sSingleton = new ProcessPriorityManagerChild();
938 sSingleton->Init();
939 ClearOnShutdown(&sSingleton);
943 /* static */
944 ProcessPriorityManagerChild* ProcessPriorityManagerChild::Singleton() {
945 StaticInit();
946 return sSingleton;
949 NS_IMPL_ISUPPORTS(ProcessPriorityManagerChild, nsIObserver)
951 ProcessPriorityManagerChild::ProcessPriorityManagerChild() {
952 if (XRE_IsParentProcess()) {
953 mCachedPriority = PROCESS_PRIORITY_PARENT_PROCESS;
954 } else {
955 mCachedPriority = PROCESS_PRIORITY_UNKNOWN;
959 void ProcessPriorityManagerChild::Init() {
960 // The process priority should only be changed in child processes; don't even
961 // bother listening for changes if we're in the main process.
962 if (!XRE_IsParentProcess()) {
963 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
964 NS_ENSURE_TRUE_VOID(os);
965 os->AddObserver(this, "ipc:process-priority-changed", /* weak = */ false);
969 NS_IMETHODIMP
970 ProcessPriorityManagerChild::Observe(nsISupports* aSubject, const char* aTopic,
971 const char16_t* aData) {
972 MOZ_ASSERT(!strcmp(aTopic, "ipc:process-priority-changed"));
974 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
975 NS_ENSURE_TRUE(props, NS_OK);
977 int32_t priority = static_cast<int32_t>(PROCESS_PRIORITY_UNKNOWN);
978 props->GetPropertyAsInt32(u"priority"_ns, &priority);
979 NS_ENSURE_TRUE(ProcessPriority(priority) != PROCESS_PRIORITY_UNKNOWN, NS_OK);
981 mCachedPriority = static_cast<ProcessPriority>(priority);
983 return NS_OK;
986 bool ProcessPriorityManagerChild::CurrentProcessIsForeground() {
987 return mCachedPriority == PROCESS_PRIORITY_UNKNOWN ||
988 mCachedPriority >= PROCESS_PRIORITY_FOREGROUND;
991 } // namespace
993 namespace mozilla {
995 /* static */
996 void ProcessPriorityManager::Init() {
997 ProcessPriorityManagerImpl::StaticInit();
998 ProcessPriorityManagerChild::StaticInit();
1001 /* static */
1002 void ProcessPriorityManager::SetProcessPriority(ContentParent* aContentParent,
1003 ProcessPriority aPriority) {
1004 MOZ_ASSERT(aContentParent);
1005 MOZ_ASSERT(aContentParent->Pid() != -1);
1007 ProcessPriorityManagerImpl* singleton =
1008 ProcessPriorityManagerImpl::GetSingleton();
1009 if (singleton) {
1010 singleton->SetProcessPriority(aContentParent, aPriority);
1014 /* static */
1015 bool ProcessPriorityManager::CurrentProcessIsForeground() {
1016 return ProcessPriorityManagerChild::Singleton()->CurrentProcessIsForeground();
1019 /* static */
1020 void ProcessPriorityManager::BrowserPriorityChanged(
1021 CanonicalBrowsingContext* aBC, bool aPriority) {
1022 if (auto* singleton = ProcessPriorityManagerImpl::GetSingleton()) {
1023 singleton->BrowserPriorityChanged(aBC, aPriority);
1027 /* static */
1028 void ProcessPriorityManager::BrowserPriorityChanged(
1029 BrowserParent* aBrowserParent, bool aPriority) {
1030 MOZ_ASSERT(aBrowserParent);
1032 ProcessPriorityManagerImpl* singleton =
1033 ProcessPriorityManagerImpl::GetSingleton();
1034 if (!singleton) {
1035 return;
1037 singleton->BrowserPriorityChanged(aBrowserParent, aPriority);
1040 } // namespace mozilla