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"
28 #include "nsIObserverService.h"
29 #include "StaticPtr.h"
30 #include "nsIObserver.h"
32 #include "nsIPropertyBag2.h"
33 #include "nsComponentManagerUtils.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
;
45 # define getpid _getpid
54 // Use LOGP inside a ParticularProcessPriorityManager method; use LOG
55 // everywhere else. LOGP prints out information about the particular process
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, \
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 \
79 NameWithComma().get(), static_cast<uint64_t>(ChildID()), Pid(), \
82 static LogModule
* GetPPMLog() {
83 static LazyLogModule
sLog("ProcessPriorityManager");
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(), \
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
,
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
);
116 "priority of child {marker.data.pid}:"
117 " {marker.data.Before} -> {marker.data.After}");
122 struct SubProcessPriority
{
123 static constexpr Span
<const char> MarkerTypeName() {
124 return MakeStringSpan("subprocesspriority");
126 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter
& aWriter
,
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
);
143 "priority of child {marker.data.pid}: {marker.data.Priority}");
147 } // namespace geckoprofiler::markers
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
159 * ProcessPriorityManager::CurrentProcessIsForeground() and
160 * ProcessPriorityManager::AnyProcessHasHighPriority() which can be called in
161 * any process, are handled separately, by the ProcessPriorityManagerChild
164 class ProcessPriorityManagerImpl final
: public nsIObserver
,
165 public nsSupportsWeakReference
{
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();
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
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
);
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;
223 already_AddRefed
<ParticularProcessPriorityManager
>
224 GetParticularProcessPriorityManager(ContentParent
* aContentParent
);
226 void ObserveContentParentDestroyed(nsISupports
* aSubject
);
228 nsTHashMap
<uint64_t, RefPtr
<ParticularProcessPriorityManager
> >
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
{
241 static void StaticInit();
242 static ProcessPriorityManagerChild
* Singleton();
247 bool CurrentProcessIsForeground();
250 static StaticRefPtr
<ProcessPriorityManagerChild
> sSingleton
;
252 ProcessPriorityManagerChild();
253 ~ProcessPriorityManagerChild() = default;
254 ProcessPriorityManagerChild(const ProcessPriorityManagerChild
&) = delete;
256 const ProcessPriorityManagerChild
& operator=(
257 const ProcessPriorityManagerChild
&) = delete;
261 hal::ProcessPriority mCachedPriority
;
265 * This class manages the priority of one particular process. It is
268 class ParticularProcessPriorityManager final
: public WakeLockObserver
,
269 public nsITimerCallback
,
271 public nsSupportsWeakReference
{
272 ~ParticularProcessPriorityManager();
275 explicit ParticularProcessPriorityManager(ContentParent
* aContentParent
);
278 NS_DECL_NSITIMERCALLBACK
280 virtual void Notify(const WakeLockInformation
& aInfo
) override
;
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();
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
);
314 NS_IMETHOD
GetName(nsACString
& aName
) override
{
315 aName
.AssignLiteral("ParticularProcessPriorityManager");
320 void FireTestOnlyObserverNotification(const char* aTopic
, const char* aData
);
322 bool IsHoldingWakeLock(const nsAString
& aTopic
);
324 ContentParent
* mContentParent
;
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
;
344 bool ProcessPriorityManagerImpl::sInitialized
= false;
346 bool ProcessPriorityManagerImpl::sPrefListenersRegistered
= false;
348 StaticRefPtr
<ProcessPriorityManagerImpl
> ProcessPriorityManagerImpl::sSingleton
;
350 NS_IMPL_ISUPPORTS(ProcessPriorityManagerImpl
, nsIObserver
,
351 nsISupportsWeakReference
);
354 void ProcessPriorityManagerImpl::PrefChangedCallback(const char* aPref
,
357 if (!PrefsEnabled() && sSingleton
) {
358 sSingleton
= nullptr;
359 sInitialized
= false;
364 bool ProcessPriorityManagerImpl::PrefsEnabled() {
365 return StaticPrefs::dom_ipc_processPriorityManager_enabled() &&
366 !StaticPrefs::dom_ipc_tabs_disabled();
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
375 if (PrefsEnabled()) {
376 hal::SetProcessPriority(aPid
, aPriority
);
381 bool ProcessPriorityManagerImpl::TestMode() {
382 return StaticPrefs::dom_ipc_processPriorityManager_testMode();
386 void ProcessPriorityManagerImpl::StaticInit() {
391 // The process priority manager is main-process only.
392 if (!XRE_IsParentProcess()) {
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");
408 sSingleton
= new ProcessPriorityManagerImpl();
410 ClearOnShutdown(&sSingleton
);
414 ProcessPriorityManagerImpl
* ProcessPriorityManagerImpl::GetSingleton() {
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
434 SetProcessPriorityIfEnabled(getpid(), PROCESS_PRIORITY_PARENT_PROCESS
);
436 nsCOMPtr
<nsIObserverService
> os
= services::GetObserverService();
438 os
->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
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
);
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()) {
464 const uint64_t cpId
= aContentParent
->ChildID();
465 return mParticularManagers
.WithEntryHandle(cpId
, [&](auto&& 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
);
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",
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
) {
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
);
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
,
596 ProfilerString8View::WrapNullTerminatedString(
597 ProcessPriorityToString(selfPtr
->mPriority
)),
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
);
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));
637 void ParticularProcessPriorityManager::Notify(
638 const WakeLockInformation
& aInfo
) {
639 if (!mContentParent
) {
640 // We've been shut down.
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
;
656 bool thisProcessLocks
= aInfo
.lockingProcesses().Contains(ChildID());
657 if (thisProcessLocks
!= *dest
) {
658 *dest
= thisProcessLocks
;
660 "Got wake lock changed event. "
661 "Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d, "
662 "mHoldsPlayingAudioWakeLock=%d, mHoldsPlayingVideoWakeLock=%d",
663 mHoldsCPUWakeLock
, mHoldsHighPriorityWakeLock
,
664 mHoldsPlayingAudioWakeLock
, mHoldsPlayingVideoWakeLock
);
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().
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
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
707 if (mPriority
== PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE
) {
708 ScheduleResetPriority(BACKGROUND_PERCEIVABLE_GRACE_PERIOD
);
710 ScheduleResetPriority(BACKGROUND_GRACE_PERIOD
);
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.");
729 uint32_t timeout
= 0;
730 switch (aTimeoutPref
) {
731 case BACKGROUND_PERCEIVABLE_GRACE_PERIOD
:
732 timeout
= StaticPrefs::
733 dom_ipc_processPriorityManager_backgroundPerceivableGracePeriodMS();
735 case BACKGROUND_GRACE_PERIOD
:
737 StaticPrefs::dom_ipc_processPriorityManager_backgroundGracePeriodMS();
740 MOZ_ASSERT(false, "Unrecognized timeout pref");
744 LOGP("Scheduling reset timer to fire in %dms.", timeout
);
745 NS_NewTimerWithCallback(getter_AddRefs(mResetPriorityTimer
), this, timeout
,
746 nsITimer::TYPE_ONE_SHOT
);
750 ParticularProcessPriorityManager::Notify(nsITimer
* aTimer
) {
751 LOGP("Reset priority timer callback; about to ResetPriorityNow.");
753 mResetPriorityTimer
= nullptr;
757 ProcessPriority
ParticularProcessPriorityManager::CurrentPriority() {
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
) {
783 LOGP("Changing priority from %s to %s (cp=%p).",
784 ProcessPriorityToString(mPriority
), ProcessPriorityToString(aPriority
),
787 if (!mContentParent
|| mPriority
== aPriority
) {
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
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(
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
);
833 mHighPriorityBrowserParents
.Remove(aBrowserParent
->GetTabId());
835 mHighPriorityBrowserParents
.Insert(aBrowserParent
->GetTabId());
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
) {
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()) {
884 nsAutoCString
data(nsPrintfCString("%" PRIu64
, ChildID()));
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(
896 StaticRefPtr
<ProcessPriorityManagerChild
>
897 ProcessPriorityManagerChild::sSingleton
;
900 void ProcessPriorityManagerChild::StaticInit() {
902 sSingleton
= new ProcessPriorityManagerChild();
904 ClearOnShutdown(&sSingleton
);
909 ProcessPriorityManagerChild
* ProcessPriorityManagerChild::Singleton() {
914 NS_IMPL_ISUPPORTS(ProcessPriorityManagerChild
, nsIObserver
)
916 ProcessPriorityManagerChild::ProcessPriorityManagerChild() {
917 if (XRE_IsParentProcess()) {
918 mCachedPriority
= PROCESS_PRIORITY_PARENT_PROCESS
;
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);
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
);
951 bool ProcessPriorityManagerChild::CurrentProcessIsForeground() {
952 return mCachedPriority
== PROCESS_PRIORITY_UNKNOWN
||
953 mCachedPriority
>= PROCESS_PRIORITY_FOREGROUND
;
961 void ProcessPriorityManager::Init() {
962 ProcessPriorityManagerImpl::StaticInit();
963 ProcessPriorityManagerChild::StaticInit();
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();
975 singleton
->SetProcessPriority(aContentParent
, aPriority
);
980 bool ProcessPriorityManager::CurrentProcessIsForeground() {
981 return ProcessPriorityManagerChild::Singleton()->CurrentProcessIsForeground();
985 void ProcessPriorityManager::BrowserPriorityChanged(
986 CanonicalBrowsingContext
* aBC
, bool aPriority
) {
987 if (auto* singleton
= ProcessPriorityManagerImpl::GetSingleton()) {
988 singleton
->BrowserPriorityChanged(aBC
, aPriority
);
993 void ProcessPriorityManager::BrowserPriorityChanged(
994 BrowserParent
* aBrowserParent
, bool aPriority
) {
995 MOZ_ASSERT(aBrowserParent
);
997 ProcessPriorityManagerImpl
* singleton
=
998 ProcessPriorityManagerImpl::GetSingleton();
1002 singleton
->BrowserPriorityChanged(aBrowserParent
, aPriority
);
1006 void ProcessPriorityManager::RemoteBrowserFrameShown(
1007 nsFrameLoader
* aFrameLoader
) {
1008 ProcessPriorityManagerImpl
* singleton
=
1009 ProcessPriorityManagerImpl::GetSingleton();
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()) {
1024 singleton
->ResetPriority(bp
->Manager());
1027 } // namespace mozilla