Bug 1795172 [wpt PR 36447] - Disallow culled inlines in repeated content., a=testonly
[gecko.git] / dom / ipc / ProcessPriorityManager.cpp
blobe373d6b3e92ead8cde9573c6ebd107f83fb89a2c
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/Telemetry.h"
22 #include "mozilla/Unused.h"
23 #include "mozilla/Logging.h"
24 #include "nsPrintfCString.h"
25 #include "nsXULAppAPI.h"
26 #include "nsFrameLoader.h"
27 #include "nsINamed.h"
28 #include "nsIObserverService.h"
29 #include "StaticPtr.h"
30 #include "nsIObserver.h"
31 #include "nsITimer.h"
32 #include "nsIPropertyBag2.h"
33 #include "nsComponentManagerUtils.h"
34 #include "nsCRT.h"
35 #include "nsTHashSet.h"
36 #include "nsQueryObject.h"
37 #include "nsTHashMap.h"
39 using namespace mozilla;
40 using namespace mozilla::dom;
41 using namespace mozilla::hal;
43 #ifdef XP_WIN
44 # include <process.h>
45 # define getpid _getpid
46 #else
47 # include <unistd.h>
48 #endif
50 #ifdef LOG
51 # undef LOG
52 #endif
54 // Use LOGP inside a ParticularProcessPriorityManager method; use LOG
55 // everywhere else. LOGP prints out information about the particular process
56 // priority manager.
58 // (Wow, our logging story is a huge mess.)
60 // #define ENABLE_LOGGING 1
62 #if defined(ANDROID) && defined(ENABLE_LOGGING)
63 # include <android/log.h>
64 # define LOG(fmt, ...) \
65 __android_log_print(ANDROID_LOG_INFO, "Gecko:ProcessPriorityManager", fmt, \
66 ##__VA_ARGS__)
67 # define LOGP(fmt, ...) \
68 __android_log_print( \
69 ANDROID_LOG_INFO, "Gecko:ProcessPriorityManager", \
70 "[%schild-id=%" PRIu64 ", pid=%d] " fmt, NameWithComma().get(), \
71 static_cast<uint64_t>(ChildID()), Pid(), ##__VA_ARGS__)
73 #elif defined(ENABLE_LOGGING)
74 # define LOG(fmt, ...) \
75 printf("ProcessPriorityManager - " fmt "\n", ##__VA_ARGS__)
76 # define LOGP(fmt, ...) \
77 printf("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " fmt \
78 "\n", \
79 NameWithComma().get(), static_cast<uint64_t>(ChildID()), Pid(), \
80 ##__VA_ARGS__)
81 #else
82 static LogModule* GetPPMLog() {
83 static LazyLogModule sLog("ProcessPriorityManager");
84 return sLog;
86 # define LOG(fmt, ...) \
87 MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
88 ("ProcessPriorityManager - " fmt, ##__VA_ARGS__))
89 # define LOGP(fmt, ...) \
90 MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
91 ("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " fmt, \
92 NameWithComma().get(), static_cast<uint64_t>(ChildID()), Pid(), \
93 ##__VA_ARGS__))
94 #endif
96 namespace geckoprofiler::markers {
97 struct SubProcessPriorityChange {
98 static constexpr Span<const char> MarkerTypeName() {
99 return MakeStringSpan("subprocessprioritychange");
101 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
102 int32_t aPid,
103 const ProfilerString8View& aPreviousPriority,
104 const ProfilerString8View& aNewPriority) {
105 aWriter.IntProperty("pid", aPid);
106 aWriter.StringProperty("Before", aPreviousPriority);
107 aWriter.StringProperty("After", aNewPriority);
109 static MarkerSchema MarkerTypeDisplay() {
110 using MS = MarkerSchema;
111 MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
112 schema.AddKeyFormat("pid", MS::Format::Integer);
113 schema.AddKeyFormat("Before", MS::Format::String);
114 schema.AddKeyFormat("After", MS::Format::String);
115 schema.SetAllLabels(
116 "priority of child {marker.data.pid}:"
117 " {marker.data.Before} -> {marker.data.After}");
118 return schema;
122 struct SubProcessPriority {
123 static constexpr Span<const char> MarkerTypeName() {
124 return MakeStringSpan("subprocesspriority");
126 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
127 int32_t aPid,
128 const ProfilerString8View& aPriority,
129 const ProfilingState& aProfilingState) {
130 aWriter.IntProperty("pid", aPid);
131 aWriter.StringProperty("Priority", aPriority);
132 aWriter.StringProperty("Marker cause",
133 ProfilerString8View::WrapNullTerminatedString(
134 ProfilingStateToString(aProfilingState)));
136 static MarkerSchema MarkerTypeDisplay() {
137 using MS = MarkerSchema;
138 MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
139 schema.AddKeyFormat("pid", MS::Format::Integer);
140 schema.AddKeyFormat("Priority", MS::Format::String);
141 schema.AddKeyFormat("Marker cause", MS::Format::String);
142 schema.SetAllLabels(
143 "priority of child {marker.data.pid}: {marker.data.Priority}");
144 return schema;
147 } // namespace geckoprofiler::markers
149 namespace {
151 class ParticularProcessPriorityManager;
154 * This singleton class does the work to implement the process priority manager
155 * in the main process. This class may not be used in child processes. (You
156 * can call StaticInit, but it won't do anything, and GetSingleton() will
157 * return null.)
159 * ProcessPriorityManager::CurrentProcessIsForeground() and
160 * ProcessPriorityManager::AnyProcessHasHighPriority() which can be called in
161 * any process, are handled separately, by the ProcessPriorityManagerChild
162 * class.
164 class ProcessPriorityManagerImpl final : public nsIObserver,
165 public nsSupportsWeakReference {
166 public:
168 * If we're in the main process, get the ProcessPriorityManagerImpl
169 * singleton. If we're in a child process, return null.
171 static ProcessPriorityManagerImpl* GetSingleton();
173 static void StaticInit();
174 static bool PrefsEnabled();
175 static void SetProcessPriorityIfEnabled(int aPid, ProcessPriority aPriority);
176 static bool TestMode();
178 NS_DECL_ISUPPORTS
179 NS_DECL_NSIOBSERVER
182 * This function implements ProcessPriorityManager::SetProcessPriority.
184 void SetProcessPriority(ContentParent* aContentParent,
185 ProcessPriority aPriority);
188 * If a magic testing-only pref is set, notify the observer service on the
189 * given topic with the given data. This is used for testing
191 void FireTestOnlyObserverNotification(const char* aTopic,
192 const nsACString& aData);
195 * This must be called by a ParticularProcessPriorityManager when it changes
196 * its priority.
198 void NotifyProcessPriorityChanged(
199 ParticularProcessPriorityManager* aParticularManager,
200 hal::ProcessPriority aOldPriority);
202 void BrowserPriorityChanged(CanonicalBrowsingContext* aBC, bool aPriority);
203 void BrowserPriorityChanged(BrowserParent* aBrowserParent, bool aPriority);
205 void ResetPriority(ContentParent* aContentParent);
207 private:
208 static bool sPrefListenersRegistered;
209 static bool sInitialized;
210 static StaticRefPtr<ProcessPriorityManagerImpl> sSingleton;
212 static void PrefChangedCallback(const char* aPref, void* aClosure);
214 ProcessPriorityManagerImpl();
215 ~ProcessPriorityManagerImpl();
216 ProcessPriorityManagerImpl(const ProcessPriorityManagerImpl&) = delete;
218 const ProcessPriorityManagerImpl& operator=(
219 const ProcessPriorityManagerImpl&) = delete;
221 void Init();
223 already_AddRefed<ParticularProcessPriorityManager>
224 GetParticularProcessPriorityManager(ContentParent* aContentParent);
226 void ObserveContentParentDestroyed(nsISupports* aSubject);
228 nsTHashMap<uint64_t, RefPtr<ParticularProcessPriorityManager> >
229 mParticularManagers;
231 /** Contains the PIDs of child processes holding high-priority wakelocks */
232 nsTHashSet<uint64_t> mHighPriorityChildIDs;
236 * This singleton class implements the parts of the process priority manager
237 * that are available from all processes.
239 class ProcessPriorityManagerChild final : public nsIObserver {
240 public:
241 static void StaticInit();
242 static ProcessPriorityManagerChild* Singleton();
244 NS_DECL_ISUPPORTS
245 NS_DECL_NSIOBSERVER
247 bool CurrentProcessIsForeground();
249 private:
250 static StaticRefPtr<ProcessPriorityManagerChild> sSingleton;
252 ProcessPriorityManagerChild();
253 ~ProcessPriorityManagerChild() = default;
254 ProcessPriorityManagerChild(const ProcessPriorityManagerChild&) = delete;
256 const ProcessPriorityManagerChild& operator=(
257 const ProcessPriorityManagerChild&) = delete;
259 void Init();
261 hal::ProcessPriority mCachedPriority;
265 * This class manages the priority of one particular process. It is
266 * main-process only.
268 class ParticularProcessPriorityManager final : public WakeLockObserver,
269 public nsITimerCallback,
270 public nsINamed,
271 public nsSupportsWeakReference {
272 ~ParticularProcessPriorityManager();
274 public:
275 explicit ParticularProcessPriorityManager(ContentParent* aContentParent);
277 NS_DECL_ISUPPORTS
278 NS_DECL_NSITIMERCALLBACK
280 virtual void Notify(const WakeLockInformation& aInfo) override;
281 void Init();
283 int32_t Pid() const;
284 uint64_t ChildID() const;
287 * Used in logging, this method returns the ContentParent's name followed by
288 * ", ". If we can't get the ContentParent's name for some reason, it
289 * returns an empty string.
291 * The reference returned here is guaranteed to be live until the next call
292 * to NameWithComma() or until the ParticularProcessPriorityManager is
293 * destroyed, whichever comes first.
295 const nsAutoCString& NameWithComma();
297 ProcessPriority CurrentPriority();
298 ProcessPriority ComputePriority();
300 enum TimeoutPref {
301 BACKGROUND_PERCEIVABLE_GRACE_PERIOD,
302 BACKGROUND_GRACE_PERIOD,
305 void ScheduleResetPriority(TimeoutPref aTimeoutPref);
306 void ResetPriority();
307 void ResetPriorityNow();
308 void SetPriorityNow(ProcessPriority aPriority);
310 void BrowserPriorityChanged(BrowserParent* aBrowserParent, bool aPriority);
312 void ShutDown();
314 NS_IMETHOD GetName(nsACString& aName) override {
315 aName.AssignLiteral("ParticularProcessPriorityManager");
316 return NS_OK;
319 private:
320 void FireTestOnlyObserverNotification(const char* aTopic, const char* aData);
322 bool IsHoldingWakeLock(const nsAString& aTopic);
324 ContentParent* mContentParent;
325 uint64_t mChildID;
326 ProcessPriority mPriority;
327 bool mHoldsCPUWakeLock;
328 bool mHoldsHighPriorityWakeLock;
329 bool mHoldsPlayingAudioWakeLock;
330 bool mHoldsPlayingVideoWakeLock;
333 * Used to implement NameWithComma().
335 nsAutoCString mNameWithComma;
337 nsCOMPtr<nsITimer> mResetPriorityTimer;
339 // This hashtable contains the list of high priority TabIds for this process.
340 nsTHashSet<uint64_t> mHighPriorityBrowserParents;
343 /* static */
344 bool ProcessPriorityManagerImpl::sInitialized = false;
345 /* static */
346 bool ProcessPriorityManagerImpl::sPrefListenersRegistered = false;
347 /* static */
348 StaticRefPtr<ProcessPriorityManagerImpl> ProcessPriorityManagerImpl::sSingleton;
350 NS_IMPL_ISUPPORTS(ProcessPriorityManagerImpl, nsIObserver,
351 nsISupportsWeakReference);
353 /* static */
354 void ProcessPriorityManagerImpl::PrefChangedCallback(const char* aPref,
355 void* aClosure) {
356 StaticInit();
357 if (!PrefsEnabled() && sSingleton) {
358 sSingleton = nullptr;
359 sInitialized = false;
363 /* static */
364 bool ProcessPriorityManagerImpl::PrefsEnabled() {
365 return StaticPrefs::dom_ipc_processPriorityManager_enabled() &&
366 !StaticPrefs::dom_ipc_tabs_disabled();
369 /* static */
370 void ProcessPriorityManagerImpl::SetProcessPriorityIfEnabled(
371 int aPid, ProcessPriority aPriority) {
372 // The preference doesn't disable the process priority manager, but only its
373 // effect. This way the IPCs still happen and can be used to collect telemetry
374 // about CPU use.
375 if (PrefsEnabled()) {
376 hal::SetProcessPriority(aPid, aPriority);
380 /* static */
381 bool ProcessPriorityManagerImpl::TestMode() {
382 return StaticPrefs::dom_ipc_processPriorityManager_testMode();
385 /* static */
386 void ProcessPriorityManagerImpl::StaticInit() {
387 if (sInitialized) {
388 return;
391 // The process priority manager is main-process only.
392 if (!XRE_IsParentProcess()) {
393 sInitialized = true;
394 return;
397 // Run StaticInit() again if the prefs change. We don't expect this to
398 // happen in normal operation, but it happens during testing.
399 if (!sPrefListenersRegistered) {
400 sPrefListenersRegistered = true;
401 Preferences::RegisterCallback(PrefChangedCallback,
402 "dom.ipc.processPriorityManager.enabled");
403 Preferences::RegisterCallback(PrefChangedCallback, "dom.ipc.tabs.disabled");
406 sInitialized = true;
408 sSingleton = new ProcessPriorityManagerImpl();
409 sSingleton->Init();
410 ClearOnShutdown(&sSingleton);
413 /* static */
414 ProcessPriorityManagerImpl* ProcessPriorityManagerImpl::GetSingleton() {
415 if (!sSingleton) {
416 StaticInit();
419 return sSingleton;
422 ProcessPriorityManagerImpl::ProcessPriorityManagerImpl() {
423 MOZ_ASSERT(XRE_IsParentProcess());
426 ProcessPriorityManagerImpl::~ProcessPriorityManagerImpl() = default;
428 void ProcessPriorityManagerImpl::Init() {
429 LOG("Starting up. This is the parent process.");
431 // The parent process's priority never changes; set it here and then forget
432 // about it. We'll manage only subprocesses' priorities using the process
433 // priority manager.
434 SetProcessPriorityIfEnabled(getpid(), PROCESS_PRIORITY_PARENT_PROCESS);
436 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
437 if (os) {
438 os->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
442 NS_IMETHODIMP
443 ProcessPriorityManagerImpl::Observe(nsISupports* aSubject, const char* aTopic,
444 const char16_t* aData) {
445 nsDependentCString topic(aTopic);
446 if (topic.EqualsLiteral("ipc:content-shutdown")) {
447 ObserveContentParentDestroyed(aSubject);
448 } else {
449 MOZ_ASSERT(false);
452 return NS_OK;
455 already_AddRefed<ParticularProcessPriorityManager>
456 ProcessPriorityManagerImpl::GetParticularProcessPriorityManager(
457 ContentParent* aContentParent) {
458 // If this content parent is already being shut down, there's no
459 // need to adjust its priority.
460 if (aContentParent->IsDead()) {
461 return nullptr;
464 const uint64_t cpId = aContentParent->ChildID();
465 return mParticularManagers.WithEntryHandle(cpId, [&](auto&& entry) {
466 if (!entry) {
467 entry.Insert(new ParticularProcessPriorityManager(aContentParent));
468 entry.Data()->Init();
470 return do_AddRef(entry.Data());
474 void ProcessPriorityManagerImpl::SetProcessPriority(
475 ContentParent* aContentParent, ProcessPriority aPriority) {
476 MOZ_ASSERT(aContentParent);
477 if (RefPtr pppm = GetParticularProcessPriorityManager(aContentParent)) {
478 pppm->SetPriorityNow(aPriority);
482 void ProcessPriorityManagerImpl::ObserveContentParentDestroyed(
483 nsISupports* aSubject) {
484 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
485 NS_ENSURE_TRUE_VOID(props);
487 uint64_t childID = CONTENT_PROCESS_ID_UNKNOWN;
488 props->GetPropertyAsUint64(u"childID"_ns, &childID);
489 NS_ENSURE_TRUE_VOID(childID != CONTENT_PROCESS_ID_UNKNOWN);
491 if (auto entry = mParticularManagers.Lookup(childID)) {
492 entry.Data()->ShutDown();
493 mHighPriorityChildIDs.Remove(childID);
494 entry.Remove();
498 void ProcessPriorityManagerImpl::NotifyProcessPriorityChanged(
499 ParticularProcessPriorityManager* aParticularManager,
500 ProcessPriority aOldPriority) {
501 ProcessPriority newPriority = aParticularManager->CurrentPriority();
503 if (newPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH &&
504 aOldPriority < PROCESS_PRIORITY_FOREGROUND_HIGH) {
505 mHighPriorityChildIDs.Insert(aParticularManager->ChildID());
506 } else if (newPriority < PROCESS_PRIORITY_FOREGROUND_HIGH &&
507 aOldPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH) {
508 mHighPriorityChildIDs.Remove(aParticularManager->ChildID());
512 static nsCString BCToString(dom::CanonicalBrowsingContext* aBC) {
513 nsCOMPtr<nsIURI> uri = aBC->GetCurrentURI();
514 return nsPrintfCString("id=%" PRIu64 " uri=%s active=%d pactive=%d",
515 aBC->Id(),
516 uri ? uri->GetSpecOrDefault().get() : "(no uri)",
517 aBC->IsActive(), aBC->IsPriorityActive());
520 void ProcessPriorityManagerImpl::BrowserPriorityChanged(
521 dom::CanonicalBrowsingContext* aBC, bool aPriority) {
522 MOZ_ASSERT(aBC->IsTop());
524 LOG("BrowserPriorityChanged(%s, %d)\n", BCToString(aBC).get(), aPriority);
526 bool alreadyActive = aBC->IsPriorityActive();
527 if (alreadyActive == aPriority) {
528 return;
531 Telemetry::ScalarAdd(
532 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_CHANGE_CONSIDERED, 1);
534 aBC->SetPriorityActive(aPriority);
536 aBC->PreOrderWalk([&](BrowsingContext* aContext) {
537 CanonicalBrowsingContext* canonical = aContext->Canonical();
538 LOG("PreOrderWalk for %p: %p -> %p, %p\n", aBC, canonical,
539 canonical->GetContentParent(), canonical->GetBrowserParent());
540 if (ContentParent* cp = canonical->GetContentParent()) {
541 if (RefPtr pppm = GetParticularProcessPriorityManager(cp)) {
542 if (auto* bp = canonical->GetBrowserParent()) {
543 pppm->BrowserPriorityChanged(bp, aPriority);
550 void ProcessPriorityManagerImpl::BrowserPriorityChanged(
551 BrowserParent* aBrowserParent, bool aPriority) {
552 LOG("BrowserPriorityChanged(bp=%p, %d)\n", aBrowserParent, aPriority);
554 if (RefPtr pppm =
555 GetParticularProcessPriorityManager(aBrowserParent->Manager())) {
556 Telemetry::ScalarAdd(
557 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_CHANGE_CONSIDERED,
559 pppm->BrowserPriorityChanged(aBrowserParent, aPriority);
563 void ProcessPriorityManagerImpl::ResetPriority(ContentParent* aContentParent) {
564 if (RefPtr pppm = GetParticularProcessPriorityManager(aContentParent)) {
565 pppm->ResetPriority();
569 NS_IMPL_ISUPPORTS(ParticularProcessPriorityManager, nsITimerCallback,
570 nsISupportsWeakReference, nsINamed);
572 ParticularProcessPriorityManager::ParticularProcessPriorityManager(
573 ContentParent* aContentParent)
574 : mContentParent(aContentParent),
575 mChildID(aContentParent->ChildID()),
576 mPriority(PROCESS_PRIORITY_UNKNOWN),
577 mHoldsCPUWakeLock(false),
578 mHoldsHighPriorityWakeLock(false),
579 mHoldsPlayingAudioWakeLock(false),
580 mHoldsPlayingVideoWakeLock(false) {
581 MOZ_ASSERT(XRE_IsParentProcess());
582 MOZ_RELEASE_ASSERT(!aContentParent->IsDead());
583 LOGP("Creating ParticularProcessPriorityManager.");
584 // Our static analysis doesn't allow capturing ref-counted pointers in
585 // lambdas, so we need to hide it in a uintptr_t. This is safe because this
586 // lambda will be destroyed in ~ParticularProcessPriorityManager().
587 uintptr_t self = reinterpret_cast<uintptr_t>(this);
588 profiler_add_state_change_callback(
589 AllProfilingStates(),
590 [self](ProfilingState aProfilingState) {
591 const ParticularProcessPriorityManager* selfPtr =
592 reinterpret_cast<const ParticularProcessPriorityManager*>(self);
593 PROFILER_MARKER("Subprocess Priority", OTHER,
594 MarkerThreadId::MainThread(), SubProcessPriority,
595 selfPtr->Pid(),
596 ProfilerString8View::WrapNullTerminatedString(
597 ProcessPriorityToString(selfPtr->mPriority)),
598 aProfilingState);
600 self);
603 void ParticularProcessPriorityManager::Init() {
604 RegisterWakeLockObserver(this);
606 // This process may already hold the CPU lock; for example, our parent may
607 // have acquired it on our behalf.
608 mHoldsCPUWakeLock = IsHoldingWakeLock(u"cpu"_ns);
609 mHoldsHighPriorityWakeLock = IsHoldingWakeLock(u"high-priority"_ns);
610 mHoldsPlayingAudioWakeLock = IsHoldingWakeLock(u"audio-playing"_ns);
611 mHoldsPlayingVideoWakeLock = IsHoldingWakeLock(u"video-playing"_ns);
613 LOGP(
614 "Done starting up. mHoldsCPUWakeLock=%d, "
615 "mHoldsHighPriorityWakeLock=%d, mHoldsPlayingAudioWakeLock=%d, "
616 "mHoldsPlayingVideoWakeLock=%d",
617 mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock, mHoldsPlayingAudioWakeLock,
618 mHoldsPlayingVideoWakeLock);
621 bool ParticularProcessPriorityManager::IsHoldingWakeLock(
622 const nsAString& aTopic) {
623 WakeLockInformation info;
624 GetWakeLockInfo(aTopic, &info);
625 return info.lockingProcesses().Contains(ChildID());
628 ParticularProcessPriorityManager::~ParticularProcessPriorityManager() {
629 LOGP("Destroying ParticularProcessPriorityManager.");
631 profiler_remove_state_change_callback(reinterpret_cast<uintptr_t>(this));
633 ShutDown();
636 /* virtual */
637 void ParticularProcessPriorityManager::Notify(
638 const WakeLockInformation& aInfo) {
639 if (!mContentParent) {
640 // We've been shut down.
641 return;
644 bool* dest = nullptr;
645 if (aInfo.topic().EqualsLiteral("cpu")) {
646 dest = &mHoldsCPUWakeLock;
647 } else if (aInfo.topic().EqualsLiteral("high-priority")) {
648 dest = &mHoldsHighPriorityWakeLock;
649 } else if (aInfo.topic().EqualsLiteral("audio-playing")) {
650 dest = &mHoldsPlayingAudioWakeLock;
651 } else if (aInfo.topic().EqualsLiteral("video-playing")) {
652 dest = &mHoldsPlayingVideoWakeLock;
655 if (dest) {
656 bool thisProcessLocks = aInfo.lockingProcesses().Contains(ChildID());
657 if (thisProcessLocks != *dest) {
658 *dest = thisProcessLocks;
659 LOGP(
660 "Got wake lock changed event. "
661 "Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d, "
662 "mHoldsPlayingAudioWakeLock=%d, mHoldsPlayingVideoWakeLock=%d",
663 mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock,
664 mHoldsPlayingAudioWakeLock, mHoldsPlayingVideoWakeLock);
665 ResetPriority();
670 uint64_t ParticularProcessPriorityManager::ChildID() const {
671 // We have to cache mContentParent->ChildID() instead of getting it from the
672 // ContentParent each time because after ShutDown() is called, mContentParent
673 // is null. If we didn't cache ChildID(), then we wouldn't be able to run
674 // LOGP() after ShutDown().
675 return mChildID;
678 int32_t ParticularProcessPriorityManager::Pid() const {
679 return mContentParent ? mContentParent->Pid() : -1;
682 const nsAutoCString& ParticularProcessPriorityManager::NameWithComma() {
683 mNameWithComma.Truncate();
684 if (!mContentParent) {
685 return mNameWithComma; // empty string
688 nsAutoString name;
689 mContentParent->FriendlyName(name);
690 if (name.IsEmpty()) {
691 return mNameWithComma; // empty string
694 CopyUTF16toUTF8(name, mNameWithComma);
695 mNameWithComma.AppendLiteral(", ");
696 return mNameWithComma;
699 void ParticularProcessPriorityManager::ResetPriority() {
700 ProcessPriority processPriority = ComputePriority();
701 if (mPriority == PROCESS_PRIORITY_UNKNOWN || mPriority > processPriority) {
702 // Apps set at a perceivable background priority are often playing media.
703 // Most media will have short gaps while changing tracks between songs,
704 // switching videos, etc. Give these apps a longer grace period so they
705 // can get their next track started, if there is one, before getting
706 // downgraded.
707 if (mPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
708 ScheduleResetPriority(BACKGROUND_PERCEIVABLE_GRACE_PERIOD);
709 } else {
710 ScheduleResetPriority(BACKGROUND_GRACE_PERIOD);
712 return;
715 SetPriorityNow(processPriority);
718 void ParticularProcessPriorityManager::ResetPriorityNow() {
719 SetPriorityNow(ComputePriority());
722 void ParticularProcessPriorityManager::ScheduleResetPriority(
723 TimeoutPref aTimeoutPref) {
724 if (mResetPriorityTimer) {
725 LOGP("ScheduleResetPriority bailing; the timer is already running.");
726 return;
729 uint32_t timeout = 0;
730 switch (aTimeoutPref) {
731 case BACKGROUND_PERCEIVABLE_GRACE_PERIOD:
732 timeout = StaticPrefs::
733 dom_ipc_processPriorityManager_backgroundPerceivableGracePeriodMS();
734 break;
735 case BACKGROUND_GRACE_PERIOD:
736 timeout =
737 StaticPrefs::dom_ipc_processPriorityManager_backgroundGracePeriodMS();
738 break;
739 default:
740 MOZ_ASSERT(false, "Unrecognized timeout pref");
741 break;
744 LOGP("Scheduling reset timer to fire in %dms.", timeout);
745 NS_NewTimerWithCallback(getter_AddRefs(mResetPriorityTimer), this, timeout,
746 nsITimer::TYPE_ONE_SHOT);
749 NS_IMETHODIMP
750 ParticularProcessPriorityManager::Notify(nsITimer* aTimer) {
751 LOGP("Reset priority timer callback; about to ResetPriorityNow.");
752 ResetPriorityNow();
753 mResetPriorityTimer = nullptr;
754 return NS_OK;
757 ProcessPriority ParticularProcessPriorityManager::CurrentPriority() {
758 return mPriority;
761 ProcessPriority ParticularProcessPriorityManager::ComputePriority() {
762 if (!mHighPriorityBrowserParents.IsEmpty() ||
763 mContentParent->GetRemoteType() == EXTENSION_REMOTE_TYPE ||
764 mHoldsPlayingAudioWakeLock) {
765 return PROCESS_PRIORITY_FOREGROUND;
768 if (mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock ||
769 mHoldsPlayingVideoWakeLock) {
770 return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
773 return PROCESS_PRIORITY_BACKGROUND;
776 void ParticularProcessPriorityManager::SetPriorityNow(
777 ProcessPriority aPriority) {
778 if (aPriority == PROCESS_PRIORITY_UNKNOWN) {
779 MOZ_ASSERT(false);
780 return;
783 LOGP("Changing priority from %s to %s (cp=%p).",
784 ProcessPriorityToString(mPriority), ProcessPriorityToString(aPriority),
785 mContentParent);
787 if (!mContentParent || mPriority == aPriority) {
788 return;
791 PROFILER_MARKER(
792 "Subprocess Priority", OTHER,
793 MarkerOptions(MarkerThreadId::MainThread(), MarkerStack::Capture()),
794 SubProcessPriorityChange, this->Pid(),
795 ProfilerString8View::WrapNullTerminatedString(
796 ProcessPriorityToString(mPriority)),
797 ProfilerString8View::WrapNullTerminatedString(
798 ProcessPriorityToString(aPriority)));
800 ProcessPriority oldPriority = mPriority;
802 mPriority = aPriority;
804 // We skip incrementing the DOM_CONTENTPROCESS_OS_PRIORITY_RAISED if we're
805 // transitioning from the PROCESS_PRIORITY_UNKNOWN level, which is where
806 // we initialize at.
807 if (oldPriority < mPriority && oldPriority != PROCESS_PRIORITY_UNKNOWN) {
808 Telemetry::ScalarAdd(
809 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_RAISED, 1);
810 } else if (oldPriority > mPriority) {
811 Telemetry::ScalarAdd(
812 Telemetry::ScalarID::DOM_CONTENTPROCESS_OS_PRIORITY_LOWERED, 1);
815 ProcessPriorityManagerImpl::SetProcessPriorityIfEnabled(Pid(), mPriority);
817 if (oldPriority != mPriority) {
818 ProcessPriorityManagerImpl::GetSingleton()->NotifyProcessPriorityChanged(
819 this, oldPriority);
821 Unused << mContentParent->SendNotifyProcessPriorityChanged(mPriority);
824 FireTestOnlyObserverNotification("process-priority-set",
825 ProcessPriorityToString(mPriority));
828 void ParticularProcessPriorityManager::BrowserPriorityChanged(
829 BrowserParent* aBrowserParent, bool aPriority) {
830 MOZ_ASSERT(aBrowserParent);
832 if (!aPriority) {
833 mHighPriorityBrowserParents.Remove(aBrowserParent->GetTabId());
834 } else {
835 mHighPriorityBrowserParents.Insert(aBrowserParent->GetTabId());
838 ResetPriority();
841 void ParticularProcessPriorityManager::ShutDown() {
842 LOGP("shutdown for %p (mContentParent %p)", this, mContentParent);
844 // Unregister our wake lock observer if ShutDown hasn't been called. (The
845 // wake lock observer takes raw refs, so we don't want to take chances here!)
846 // We don't call UnregisterWakeLockObserver unconditionally because the code
847 // will print a warning if it's called unnecessarily.
848 if (mContentParent) {
849 UnregisterWakeLockObserver(this);
852 if (mResetPriorityTimer) {
853 mResetPriorityTimer->Cancel();
854 mResetPriorityTimer = nullptr;
857 mContentParent = nullptr;
860 void ProcessPriorityManagerImpl::FireTestOnlyObserverNotification(
861 const char* aTopic, const nsACString& aData) {
862 if (!TestMode()) {
863 return;
866 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
867 NS_ENSURE_TRUE_VOID(os);
869 nsPrintfCString topic("process-priority-manager:TEST-ONLY:%s", aTopic);
871 LOG("Notifying observer %s, data %s", topic.get(),
872 PromiseFlatCString(aData).get());
873 os->NotifyObservers(nullptr, topic.get(), NS_ConvertUTF8toUTF16(aData).get());
876 void ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
877 const char* aTopic, const char* aData) {
878 MOZ_ASSERT(aData, "Pass in data");
880 if (!ProcessPriorityManagerImpl::TestMode()) {
881 return;
884 nsAutoCString data(nsPrintfCString("%" PRIu64, ChildID()));
885 data.Append(':');
886 data.AppendASCII(aData);
888 // ProcessPriorityManagerImpl::GetSingleton() is guaranteed not to return
889 // null, since ProcessPriorityManagerImpl is the only class which creates
890 // ParticularProcessPriorityManagers.
892 ProcessPriorityManagerImpl::GetSingleton()->FireTestOnlyObserverNotification(
893 aTopic, data);
896 StaticRefPtr<ProcessPriorityManagerChild>
897 ProcessPriorityManagerChild::sSingleton;
899 /* static */
900 void ProcessPriorityManagerChild::StaticInit() {
901 if (!sSingleton) {
902 sSingleton = new ProcessPriorityManagerChild();
903 sSingleton->Init();
904 ClearOnShutdown(&sSingleton);
908 /* static */
909 ProcessPriorityManagerChild* ProcessPriorityManagerChild::Singleton() {
910 StaticInit();
911 return sSingleton;
914 NS_IMPL_ISUPPORTS(ProcessPriorityManagerChild, nsIObserver)
916 ProcessPriorityManagerChild::ProcessPriorityManagerChild() {
917 if (XRE_IsParentProcess()) {
918 mCachedPriority = PROCESS_PRIORITY_PARENT_PROCESS;
919 } else {
920 mCachedPriority = PROCESS_PRIORITY_UNKNOWN;
924 void ProcessPriorityManagerChild::Init() {
925 // The process priority should only be changed in child processes; don't even
926 // bother listening for changes if we're in the main process.
927 if (!XRE_IsParentProcess()) {
928 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
929 NS_ENSURE_TRUE_VOID(os);
930 os->AddObserver(this, "ipc:process-priority-changed", /* weak = */ false);
934 NS_IMETHODIMP
935 ProcessPriorityManagerChild::Observe(nsISupports* aSubject, const char* aTopic,
936 const char16_t* aData) {
937 MOZ_ASSERT(!strcmp(aTopic, "ipc:process-priority-changed"));
939 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
940 NS_ENSURE_TRUE(props, NS_OK);
942 int32_t priority = static_cast<int32_t>(PROCESS_PRIORITY_UNKNOWN);
943 props->GetPropertyAsInt32(u"priority"_ns, &priority);
944 NS_ENSURE_TRUE(ProcessPriority(priority) != PROCESS_PRIORITY_UNKNOWN, NS_OK);
946 mCachedPriority = static_cast<ProcessPriority>(priority);
948 return NS_OK;
951 bool ProcessPriorityManagerChild::CurrentProcessIsForeground() {
952 return mCachedPriority == PROCESS_PRIORITY_UNKNOWN ||
953 mCachedPriority >= PROCESS_PRIORITY_FOREGROUND;
956 } // namespace
958 namespace mozilla {
960 /* static */
961 void ProcessPriorityManager::Init() {
962 ProcessPriorityManagerImpl::StaticInit();
963 ProcessPriorityManagerChild::StaticInit();
966 /* static */
967 void ProcessPriorityManager::SetProcessPriority(ContentParent* aContentParent,
968 ProcessPriority aPriority) {
969 MOZ_ASSERT(aContentParent);
970 MOZ_ASSERT(aContentParent->Pid() != -1);
972 ProcessPriorityManagerImpl* singleton =
973 ProcessPriorityManagerImpl::GetSingleton();
974 if (singleton) {
975 singleton->SetProcessPriority(aContentParent, aPriority);
979 /* static */
980 bool ProcessPriorityManager::CurrentProcessIsForeground() {
981 return ProcessPriorityManagerChild::Singleton()->CurrentProcessIsForeground();
984 /* static */
985 void ProcessPriorityManager::BrowserPriorityChanged(
986 CanonicalBrowsingContext* aBC, bool aPriority) {
987 if (auto* singleton = ProcessPriorityManagerImpl::GetSingleton()) {
988 singleton->BrowserPriorityChanged(aBC, aPriority);
992 /* static */
993 void ProcessPriorityManager::BrowserPriorityChanged(
994 BrowserParent* aBrowserParent, bool aPriority) {
995 MOZ_ASSERT(aBrowserParent);
997 ProcessPriorityManagerImpl* singleton =
998 ProcessPriorityManagerImpl::GetSingleton();
999 if (!singleton) {
1000 return;
1002 singleton->BrowserPriorityChanged(aBrowserParent, aPriority);
1005 /* static */
1006 void ProcessPriorityManager::RemoteBrowserFrameShown(
1007 nsFrameLoader* aFrameLoader) {
1008 ProcessPriorityManagerImpl* singleton =
1009 ProcessPriorityManagerImpl::GetSingleton();
1010 if (!singleton) {
1011 return;
1014 BrowserParent* bp = BrowserParent::GetFrom(aFrameLoader);
1015 NS_ENSURE_TRUE_VOID(bp);
1017 MOZ_ASSERT(XRE_IsParentProcess());
1019 // Ignore calls that aren't from a Browser.
1020 if (!aFrameLoader->OwnerIsMozBrowserFrame()) {
1021 return;
1024 singleton->ResetPriority(bp->Manager());
1027 } // namespace mozilla