2012-08-05 09:09:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-08-05 09:09:39 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "ProcessPriorityManager.h"
|
2013-01-04 03:39:25 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2013-07-24 03:39:17 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "mozilla/Hal.h"
|
2015-02-25 11:37:38 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-01-27 01:14:01 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2013-05-18 00:17:53 +04:00
|
|
|
#include "nsIFrameLoader.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "nsIObserverService.h"
|
2013-01-04 03:39:25 +04:00
|
|
|
#include "StaticPtr.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "nsIMozBrowserFrame.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsITimer.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "nsIPropertyBag2.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-06-13 16:16:00 +03:00
|
|
|
#include "nsCRT.h"
|
2017-06-07 10:59:00 +03:00
|
|
|
#include "nsTHashtable.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2015-11-23 22:09:25 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
// Use LOGP inside a ParticularProcessPriorityManager method; use LOG
|
|
|
|
// everywhere else. LOGP prints out information about the particular process
|
|
|
|
// priority manager.
|
2012-08-05 09:09:39 +04:00
|
|
|
//
|
2013-04-26 04:53:26 +04:00
|
|
|
// (Wow, our logging story is a huge mess.)
|
|
|
|
|
2013-12-10 07:25:01 +04:00
|
|
|
// #define ENABLE_LOGGING 1
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
#if defined(ANDROID) && defined(ENABLE_LOGGING)
|
|
|
|
# include <android/log.h>
|
|
|
|
# define LOG(fmt, ...) \
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, \
|
|
|
|
"Gecko:ProcessPriorityManager", \
|
|
|
|
fmt, ## __VA_ARGS__)
|
|
|
|
# define LOGP(fmt, ...) \
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, \
|
2013-02-08 18:32:23 +04:00
|
|
|
"Gecko:ProcessPriorityManager", \
|
2015-02-25 11:37:38 +03:00
|
|
|
"[%schild-id=%" PRIu64 ", pid=%d] " fmt, \
|
2013-04-26 04:53:26 +04:00
|
|
|
NameWithComma().get(), \
|
2015-02-25 11:37:38 +03:00
|
|
|
static_cast<uint64_t>(ChildID()), Pid(), ## __VA_ARGS__)
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
#elif defined(ENABLE_LOGGING)
|
|
|
|
# define LOG(fmt, ...) \
|
|
|
|
printf("ProcessPriorityManager - " fmt "\n", ##__VA_ARGS__)
|
|
|
|
# define LOGP(fmt, ...) \
|
2015-02-25 11:37:38 +03:00
|
|
|
printf("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " \
|
|
|
|
fmt "\n", \
|
2013-04-26 04:53:26 +04:00
|
|
|
NameWithComma().get(), \
|
2015-02-25 11:37:38 +03:00
|
|
|
static_cast<uint64_t>(ChildID()), Pid(), ##__VA_ARGS__)
|
2015-05-14 20:13:23 +03:00
|
|
|
#else
|
2015-11-23 22:09:25 +03:00
|
|
|
static LogModule*
|
2013-04-26 04:53:26 +04:00
|
|
|
GetPPMLog()
|
|
|
|
{
|
2015-11-23 22:09:25 +03:00
|
|
|
static LazyLogModule sLog("ProcessPriorityManager");
|
2013-04-26 04:53:26 +04:00
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
# define LOG(fmt, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
|
2013-04-26 04:53:26 +04:00
|
|
|
("ProcessPriorityManager - " fmt, ##__VA_ARGS__))
|
|
|
|
# define LOGP(fmt, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetPPMLog(), LogLevel::Debug, \
|
2015-02-25 11:37:38 +03:00
|
|
|
("ProcessPriorityManager[%schild-id=%" PRIu64 ", pid=%d] - " fmt, \
|
2013-04-26 04:53:26 +04:00
|
|
|
NameWithComma().get(), \
|
2015-02-25 11:37:38 +03:00
|
|
|
static_cast<uint64_t>(ChildID()), Pid(), ##__VA_ARGS__))
|
2012-08-05 09:09:39 +04:00
|
|
|
#endif
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ParticularProcessPriorityManager;
|
2012-12-19 06:37:35 +04:00
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
/**
|
2013-04-26 04:53:26 +04:00
|
|
|
* This singleton class does the work to implement the process priority manager
|
|
|
|
* in the main process. This class may not be used in child processes. (You
|
|
|
|
* can call StaticInit, but it won't do anything, and GetSingleton() will
|
|
|
|
* return null.)
|
2012-08-05 09:09:39 +04:00
|
|
|
*
|
2014-02-17 20:45:29 +04:00
|
|
|
* ProcessPriorityManager::CurrentProcessIsForeground() and
|
|
|
|
* ProcessPriorityManager::AnyProcessHasHighPriority() which can be called in
|
|
|
|
* any process, are handled separately, by the ProcessPriorityManagerChild
|
2013-04-26 04:53:26 +04:00
|
|
|
* class.
|
2012-08-05 09:09:39 +04:00
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class ProcessPriorityManagerImpl final
|
2012-08-05 09:09:39 +04:00
|
|
|
: public nsIObserver
|
2014-05-01 17:47:26 +04:00
|
|
|
, public WakeLockObserver
|
2015-09-04 06:09:06 +03:00
|
|
|
, public nsSupportsWeakReference
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-04-26 04:53:26 +04:00
|
|
|
/**
|
|
|
|
* If we're in the main process, get the ProcessPriorityManagerImpl
|
|
|
|
* singleton. If we're in a child process, return null.
|
|
|
|
*/
|
|
|
|
static ProcessPriorityManagerImpl* GetSingleton();
|
|
|
|
|
|
|
|
static void StaticInit();
|
|
|
|
static bool PrefsEnabled();
|
2015-09-04 06:09:06 +03:00
|
|
|
static bool TestMode();
|
2012-08-05 09:09:39 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2013-01-04 03:39:25 +04:00
|
|
|
|
2013-01-27 01:14:01 +04:00
|
|
|
/**
|
2013-04-26 04:53:26 +04:00
|
|
|
* This function implements ProcessPriorityManager::SetProcessPriority.
|
2013-01-27 01:14:01 +04:00
|
|
|
*/
|
2013-04-26 04:53:26 +04:00
|
|
|
void SetProcessPriority(ContentParent* aContentParent,
|
2017-05-26 18:50:17 +03:00
|
|
|
ProcessPriority aPriority);
|
2013-01-27 01:14:01 +04:00
|
|
|
|
|
|
|
/**
|
2013-04-26 04:53:26 +04:00
|
|
|
* If a magic testing-only pref is set, notify the observer service on the
|
|
|
|
* given topic with the given data. This is used for testing
|
2013-01-27 01:14:01 +04:00
|
|
|
*/
|
2013-04-26 04:53:26 +04:00
|
|
|
void FireTestOnlyObserverNotification(const char* aTopic,
|
|
|
|
const nsACString& aData = EmptyCString());
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
/**
|
|
|
|
* This must be called by a ParticularProcessPriorityManager when it changes
|
|
|
|
* its priority.
|
|
|
|
*/
|
|
|
|
void NotifyProcessPriorityChanged(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager,
|
|
|
|
hal::ProcessPriority aOldPriority);
|
|
|
|
|
2014-05-01 17:47:26 +04:00
|
|
|
/**
|
|
|
|
* Implements WakeLockObserver, used to monitor wake lock changes in the
|
|
|
|
* main process.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Notify(const WakeLockInformation& aInfo) override;
|
2014-05-01 17:47:26 +04:00
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
void TabActivityChanged(TabParent* aTabParent, bool aIsActive);
|
|
|
|
|
2015-09-04 06:09:06 +03:00
|
|
|
/**
|
|
|
|
* Call ShutDown before destroying the ProcessPriorityManager because
|
|
|
|
* WakeLockObserver hols a strong reference to it.
|
|
|
|
*/
|
|
|
|
void ShutDown();
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
private:
|
2015-09-04 06:09:06 +03:00
|
|
|
static bool sPrefsEnabled;
|
|
|
|
static bool sRemoteTabsDisabled;
|
|
|
|
static bool sTestMode;
|
2013-04-26 04:53:26 +04:00
|
|
|
static bool sPrefListenersRegistered;
|
|
|
|
static bool sInitialized;
|
|
|
|
static StaticRefPtr<ProcessPriorityManagerImpl> sSingleton;
|
|
|
|
|
2013-12-11 03:10:01 +04:00
|
|
|
static void PrefChangedCallback(const char* aPref, void* aClosure);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
ProcessPriorityManagerImpl();
|
2014-05-01 17:47:26 +04:00
|
|
|
~ProcessPriorityManagerImpl();
|
2013-04-26 04:53:26 +04:00
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(ProcessPriorityManagerImpl);
|
|
|
|
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
already_AddRefed<ParticularProcessPriorityManager>
|
|
|
|
GetParticularProcessPriorityManager(ContentParent* aContentParent);
|
|
|
|
|
|
|
|
void ObserveContentParentCreated(nsISupports* aContentParent);
|
|
|
|
void ObserveContentParentDestroyed(nsISupports* aSubject);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsDataHashtable<nsUint64HashKey, RefPtr<ParticularProcessPriorityManager> >
|
2013-04-26 04:53:26 +04:00
|
|
|
mParticularManagers;
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
/** True if the main process is holding a high-priority wakelock */
|
2014-05-01 17:47:26 +04:00
|
|
|
bool mHighPriority;
|
2015-02-25 11:37:38 +03:00
|
|
|
|
|
|
|
/** Contains the PIDs of child processes holding high-priority wakelocks */
|
2013-05-10 00:27:06 +04:00
|
|
|
nsTHashtable<nsUint64HashKey> mHighPriorityChildIDs;
|
2013-04-26 04:53:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This singleton class implements the parts of the process priority manager
|
|
|
|
* that are available from all processes.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class ProcessPriorityManagerChild final
|
2013-04-26 04:53:26 +04:00
|
|
|
: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void StaticInit();
|
|
|
|
static ProcessPriorityManagerChild* Singleton();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
bool CurrentProcessIsForeground();
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
private:
|
2013-04-26 04:53:26 +04:00
|
|
|
static StaticRefPtr<ProcessPriorityManagerChild> sSingleton;
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerChild();
|
|
|
|
~ProcessPriorityManagerChild() {}
|
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(ProcessPriorityManagerChild);
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void Init();
|
|
|
|
|
|
|
|
hal::ProcessPriority mCachedPriority;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class manages the priority of one particular process. It is
|
|
|
|
* main-process only.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class ParticularProcessPriorityManager final
|
2013-04-26 04:53:26 +04:00
|
|
|
: public WakeLockObserver
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsITimerCallback
|
2017-07-26 21:18:20 +03:00
|
|
|
, public nsINamed
|
2013-05-10 00:27:06 +04:00
|
|
|
, public nsSupportsWeakReference
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
2014-06-23 23:56:07 +04:00
|
|
|
~ParticularProcessPriorityManager();
|
2013-04-26 04:53:26 +04:00
|
|
|
public:
|
2017-05-30 17:27:12 +03:00
|
|
|
explicit ParticularProcessPriorityManager(ContentParent* aContentParent);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Notify(const WakeLockInformation& aInfo) override;
|
2015-09-04 06:09:06 +03:00
|
|
|
static void StaticInit();
|
2013-04-26 04:53:26 +04:00
|
|
|
void Init();
|
|
|
|
|
|
|
|
int32_t Pid() const;
|
|
|
|
uint64_t ChildID() const;
|
2013-01-27 01:14:01 +04:00
|
|
|
|
|
|
|
/**
|
2013-04-26 04:53:26 +04:00
|
|
|
* Used in logging, this method returns the ContentParent's name followed by
|
|
|
|
* ", ". If we can't get the ContentParent's name for some reason, it
|
|
|
|
* returns an empty string.
|
|
|
|
*
|
|
|
|
* The reference returned here is guaranteed to be live until the next call
|
|
|
|
* to NameWithComma() or until the ParticularProcessPriorityManager is
|
|
|
|
* destroyed, whichever comes first.
|
2013-01-27 01:14:01 +04:00
|
|
|
*/
|
2013-04-26 04:53:26 +04:00
|
|
|
const nsAutoCString& NameWithComma();
|
|
|
|
|
|
|
|
void OnRemoteBrowserFrameShown(nsISupports* aSubject);
|
|
|
|
void OnTabParentDestroyed(nsISupports* aSubject);
|
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
ProcessPriority CurrentPriority();
|
2013-02-27 23:28:57 +04:00
|
|
|
ProcessPriority ComputePriority();
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-09-04 06:09:06 +03:00
|
|
|
enum TimeoutPref {
|
|
|
|
BACKGROUND_PERCEIVABLE_GRACE_PERIOD,
|
|
|
|
BACKGROUND_GRACE_PERIOD,
|
|
|
|
};
|
|
|
|
|
|
|
|
void ScheduleResetPriority(TimeoutPref aTimeoutPref);
|
2013-04-26 04:53:26 +04:00
|
|
|
void ResetPriority();
|
|
|
|
void ResetPriorityNow();
|
2017-05-26 18:50:17 +03:00
|
|
|
void SetPriorityNow(ProcessPriority aPriority);
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
void TabActivityChanged(TabParent* aTabParent, bool aIsActive);
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void ShutDown();
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("ParticularProcessPriorityManager");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
private:
|
2015-09-04 06:09:06 +03:00
|
|
|
static uint32_t sBackgroundPerceivableGracePeriodMS;
|
|
|
|
static uint32_t sBackgroundGracePeriodMS;
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData = EmptyCString());
|
|
|
|
|
|
|
|
void FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const char* aData = nullptr);
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
ContentParent* mContentParent;
|
|
|
|
uint64_t mChildID;
|
|
|
|
ProcessPriority mPriority;
|
|
|
|
bool mHoldsCPUWakeLock;
|
2013-02-15 00:41:30 +04:00
|
|
|
bool mHoldsHighPriorityWakeLock;
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/**
|
|
|
|
* Used to implement NameWithComma().
|
|
|
|
*/
|
|
|
|
nsAutoCString mNameWithComma;
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsCOMPtr<nsITimer> mResetPriorityTimer;
|
2017-06-07 10:59:00 +03:00
|
|
|
|
|
|
|
// This hashtable contains the list of active TabId for this process.
|
|
|
|
nsTHashtable<nsUint64HashKey> mActiveTabParents;
|
2013-04-26 04:53:26 +04:00
|
|
|
};
|
2013-02-23 08:24:28 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sInitialized = false;
|
2015-09-04 06:09:06 +03:00
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sPrefsEnabled = false;
|
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sRemoteTabsDisabled = true;
|
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sTestMode = false;
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sPrefListenersRegistered = false;
|
|
|
|
/* static */ StaticRefPtr<ProcessPriorityManagerImpl>
|
|
|
|
ProcessPriorityManagerImpl::sSingleton;
|
2015-09-04 06:09:06 +03:00
|
|
|
/* static */ uint32_t ParticularProcessPriorityManager::sBackgroundPerceivableGracePeriodMS = 0;
|
|
|
|
/* static */ uint32_t ParticularProcessPriorityManager::sBackgroundGracePeriodMS = 0;
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ProcessPriorityManagerImpl,
|
2015-09-04 06:09:06 +03:00
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference);
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2013-12-11 03:10:01 +04:00
|
|
|
/* static */ void
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerImpl::PrefChangedCallback(const char* aPref,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
StaticInit();
|
2015-09-04 06:09:06 +03:00
|
|
|
if (!PrefsEnabled() && sSingleton) {
|
|
|
|
sSingleton->ShutDown();
|
|
|
|
sSingleton = nullptr;
|
|
|
|
sInitialized = false;
|
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManagerImpl::PrefsEnabled()
|
|
|
|
{
|
2017-06-07 10:59:00 +03:00
|
|
|
return sPrefsEnabled && hal::SetProcessPrioritySupported() && !sRemoteTabsDisabled;
|
2015-09-04 06:09:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManagerImpl::TestMode()
|
|
|
|
{
|
|
|
|
return sTestMode;
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManagerImpl::StaticInit()
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (sInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The process priority manager is main-process only.
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
sInitialized = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-04 06:09:06 +03:00
|
|
|
if (!sPrefListenersRegistered) {
|
|
|
|
Preferences::AddBoolVarCache(&sPrefsEnabled,
|
|
|
|
"dom.ipc.processPriorityManager.enabled");
|
|
|
|
Preferences::AddBoolVarCache(&sRemoteTabsDisabled,
|
|
|
|
"dom.ipc.tabs.disabled");
|
|
|
|
Preferences::AddBoolVarCache(&sTestMode,
|
|
|
|
"dom.ipc.processPriorityManager.testMode");
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
// If IPC tabs aren't enabled at startup, don't bother with any of this.
|
|
|
|
if (!PrefsEnabled()) {
|
|
|
|
LOG("InitProcessPriorityManager bailing due to prefs.");
|
|
|
|
|
|
|
|
// Run StaticInit() again if the prefs change. We don't expect this to
|
|
|
|
// happen in normal operation, but it happens during testing.
|
|
|
|
if (!sPrefListenersRegistered) {
|
|
|
|
sPrefListenersRegistered = true;
|
|
|
|
Preferences::RegisterCallback(PrefChangedCallback,
|
|
|
|
"dom.ipc.processPriorityManager.enabled");
|
|
|
|
Preferences::RegisterCallback(PrefChangedCallback,
|
|
|
|
"dom.ipc.tabs.disabled");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sInitialized = true;
|
|
|
|
|
|
|
|
sSingleton = new ProcessPriorityManagerImpl();
|
|
|
|
sSingleton->Init();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ ProcessPriorityManagerImpl*
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton()
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!sSingleton) {
|
|
|
|
StaticInit();
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
return sSingleton;
|
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerImpl::ProcessPriorityManagerImpl()
|
2017-05-26 18:50:17 +03:00
|
|
|
: mHighPriority(false)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-05-01 17:47:26 +04:00
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl::~ProcessPriorityManagerImpl()
|
2015-09-04 06:09:06 +03:00
|
|
|
{
|
|
|
|
ShutDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::ShutDown()
|
2014-05-01 17:47:26 +04:00
|
|
|
{
|
|
|
|
UnregisterWakeLockObserver(this);
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::Init()
|
|
|
|
{
|
|
|
|
LOG("Starting up. This is the master process.");
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
// The master process's priority never changes; set it here and then forget
|
|
|
|
// about it. We'll manage only subprocesses' priorities using the process
|
|
|
|
// priority manager.
|
2015-02-26 14:43:22 +03:00
|
|
|
hal::SetProcessPriority(getpid(), PROCESS_PRIORITY_MASTER);
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
2015-09-04 06:09:06 +03:00
|
|
|
os->AddObserver(this, "ipc:content-created", /* ownsWeak */ true);
|
|
|
|
os->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerImpl::Observe(
|
2012-08-05 09:09:39 +04:00
|
|
|
nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
nsDependentCString topic(aTopic);
|
|
|
|
if (topic.EqualsLiteral("ipc:content-created")) {
|
|
|
|
ObserveContentParentCreated(aSubject);
|
|
|
|
} else if (topic.EqualsLiteral("ipc:content-shutdown")) {
|
|
|
|
ObserveContentParentDestroyed(aSubject);
|
2012-08-05 09:09:39 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
already_AddRefed<ParticularProcessPriorityManager>
|
|
|
|
ProcessPriorityManagerImpl::GetParticularProcessPriorityManager(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
2014-10-24 04:28:00 +04:00
|
|
|
uint64_t cpId = aContentParent->ChildID();
|
2017-06-14 02:54:26 +03:00
|
|
|
auto entry = mParticularManagers.LookupForAdd(cpId);
|
|
|
|
RefPtr<ParticularProcessPriorityManager> pppm = entry.OrInsert(
|
|
|
|
[aContentParent]() {
|
|
|
|
return new ParticularProcessPriorityManager(aContentParent);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
// We created a new entry.
|
2013-04-26 04:53:26 +04:00
|
|
|
pppm->Init();
|
|
|
|
FireTestOnlyObserverNotification("process-created",
|
2016-12-14 19:32:21 +03:00
|
|
|
nsPrintfCString("%" PRIu64, cpId));
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pppm.forget();
|
|
|
|
}
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerImpl::SetProcessPriority(ContentParent* aContentParent,
|
2017-05-26 18:50:17 +03:00
|
|
|
ProcessPriority aPriority)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContentParent);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ParticularProcessPriorityManager> pppm =
|
2013-04-26 04:53:26 +04:00
|
|
|
GetParticularProcessPriorityManager(aContentParent);
|
2015-02-10 06:12:26 +03:00
|
|
|
if (pppm) {
|
2017-05-26 18:50:17 +03:00
|
|
|
pppm->SetPriorityNow(aPriority);
|
2015-02-10 06:12:26 +03:00
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::ObserveContentParentCreated(
|
|
|
|
nsISupports* aContentParent)
|
|
|
|
{
|
|
|
|
// Do nothing; it's sufficient to get the PPPM. But assign to nsRefPtr so we
|
|
|
|
// don't leak the already_AddRefed object.
|
2014-06-11 09:44:03 +04:00
|
|
|
nsCOMPtr<nsIContentParent> cp = do_QueryInterface(aContentParent);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ParticularProcessPriorityManager> pppm =
|
2014-06-11 09:44:03 +04:00
|
|
|
GetParticularProcessPriorityManager(cp->AsContentParent());
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::ObserveContentParentDestroyed(nsISupports* aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(props);
|
|
|
|
|
|
|
|
uint64_t childID = CONTENT_PROCESS_ID_UNKNOWN;
|
|
|
|
props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), &childID);
|
|
|
|
NS_ENSURE_TRUE_VOID(childID != CONTENT_PROCESS_ID_UNKNOWN);
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = mParticularManagers.Lookup(childID)) {
|
|
|
|
entry.Data()->ShutDown();
|
|
|
|
mHighPriorityChildIDs.RemoveEntry(childID);
|
|
|
|
entry.Remove();
|
|
|
|
}
|
2014-05-01 17:47:26 +04:00
|
|
|
}
|
2013-05-10 00:27:06 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::NotifyProcessPriorityChanged(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager,
|
|
|
|
ProcessPriority aOldPriority)
|
|
|
|
{
|
2015-02-25 11:37:38 +03:00
|
|
|
ProcessPriority newPriority = aParticularManager->CurrentPriority();
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if (newPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH &&
|
|
|
|
aOldPriority < PROCESS_PRIORITY_FOREGROUND_HIGH) {
|
2013-05-10 00:27:06 +04:00
|
|
|
mHighPriorityChildIDs.PutEntry(aParticularManager->ChildID());
|
2015-02-25 11:37:38 +03:00
|
|
|
} else if (newPriority < PROCESS_PRIORITY_FOREGROUND_HIGH &&
|
|
|
|
aOldPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH) {
|
2013-05-10 00:27:06 +04:00
|
|
|
mHighPriorityChildIDs.RemoveEntry(aParticularManager->ChildID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 17:47:26 +04:00
|
|
|
/* virtual */ void
|
|
|
|
ProcessPriorityManagerImpl::Notify(const WakeLockInformation& aInfo)
|
|
|
|
{
|
|
|
|
/* The main process always has an ID of 0, if it is present in the wake-lock
|
|
|
|
* information then we explicitly requested a high-priority wake-lock for the
|
|
|
|
* main process. */
|
|
|
|
if (aInfo.topic().EqualsLiteral("high-priority")) {
|
|
|
|
if (aInfo.lockingProcesses().Contains((uint64_t)0)) {
|
|
|
|
mHighPriority = true;
|
|
|
|
} else {
|
|
|
|
mHighPriority = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Got wake lock changed event. "
|
|
|
|
"Now mHighPriorityParent = %d\n", mHighPriority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::TabActivityChanged(TabParent* aTabParent,
|
|
|
|
bool aIsActive)
|
|
|
|
{
|
|
|
|
ContentParent* cp = aTabParent->Manager()->AsContentParent();
|
|
|
|
RefPtr<ParticularProcessPriorityManager> pppm =
|
|
|
|
GetParticularProcessPriorityManager(cp);
|
|
|
|
if (!pppm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pppm->TabActivityChanged(aTabParent, aIsActive);
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ParticularProcessPriorityManager,
|
|
|
|
nsIObserver,
|
|
|
|
nsITimerCallback,
|
2017-07-26 21:18:20 +03:00
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsINamed);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
ParticularProcessPriorityManager::ParticularProcessPriorityManager(
|
2017-05-30 17:27:12 +03:00
|
|
|
ContentParent* aContentParent)
|
2013-04-26 04:53:26 +04:00
|
|
|
: mContentParent(aContentParent)
|
|
|
|
, mChildID(aContentParent->ChildID())
|
|
|
|
, mPriority(PROCESS_PRIORITY_UNKNOWN)
|
|
|
|
, mHoldsCPUWakeLock(false)
|
|
|
|
, mHoldsHighPriorityWakeLock(false)
|
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2013-04-26 04:53:26 +04:00
|
|
|
LOGP("Creating ParticularProcessPriorityManager.");
|
|
|
|
}
|
|
|
|
|
2015-09-04 06:09:06 +03:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::StaticInit()
|
|
|
|
{
|
|
|
|
Preferences::AddUintVarCache(&sBackgroundPerceivableGracePeriodMS,
|
|
|
|
"dom.ipc.processPriorityManager.backgroundPerceivableGracePeriodMS");
|
|
|
|
Preferences::AddUintVarCache(&sBackgroundGracePeriodMS,
|
|
|
|
"dom.ipc.processPriorityManager.backgroundGracePeriodMS");
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::Init()
|
|
|
|
{
|
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
2014-02-21 02:19:18 +04:00
|
|
|
os->AddObserver(this, "remote-browser-shown", /* ownsWeak */ true);
|
2013-05-10 00:27:06 +04:00
|
|
|
os->AddObserver(this, "ipc:browser-destroyed", /* ownsWeak */ true);
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This process may already hold the CPU lock; for example, our parent may
|
|
|
|
// have acquired it on our behalf.
|
|
|
|
WakeLockInformation info1, info2;
|
|
|
|
GetWakeLockInfo(NS_LITERAL_STRING("cpu"), &info1);
|
|
|
|
mHoldsCPUWakeLock = info1.lockingProcesses().Contains(ChildID());
|
|
|
|
|
|
|
|
GetWakeLockInfo(NS_LITERAL_STRING("high-priority"), &info2);
|
|
|
|
mHoldsHighPriorityWakeLock = info2.lockingProcesses().Contains(ChildID());
|
|
|
|
LOGP("Done starting up. mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d",
|
|
|
|
mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParticularProcessPriorityManager::~ParticularProcessPriorityManager()
|
|
|
|
{
|
|
|
|
LOGP("Destroying ParticularProcessPriorityManager.");
|
2013-05-08 00:44:25 +04:00
|
|
|
|
|
|
|
// Unregister our wake lock observer if ShutDown hasn't been called. (The
|
|
|
|
// wake lock observer takes raw refs, so we don't want to take chances here!)
|
|
|
|
// We don't call UnregisterWakeLockObserver unconditionally because the code
|
|
|
|
// will print a warning if it's called unnecessarily.
|
|
|
|
|
|
|
|
if (mContentParent) {
|
|
|
|
UnregisterWakeLockObserver(this);
|
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
ParticularProcessPriorityManager::Notify(const WakeLockInformation& aInfo)
|
2013-02-15 00:41:30 +04:00
|
|
|
{
|
2013-05-08 00:44:25 +04:00
|
|
|
if (!mContentParent) {
|
|
|
|
// We've been shut down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
bool* dest = nullptr;
|
2013-04-26 04:53:26 +04:00
|
|
|
if (aInfo.topic().EqualsLiteral("cpu")) {
|
2013-02-15 00:41:30 +04:00
|
|
|
dest = &mHoldsCPUWakeLock;
|
2013-04-26 04:53:26 +04:00
|
|
|
} else if (aInfo.topic().EqualsLiteral("high-priority")) {
|
2013-02-15 00:41:30 +04:00
|
|
|
dest = &mHoldsHighPriorityWakeLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest) {
|
2013-04-26 04:53:26 +04:00
|
|
|
bool thisProcessLocks = aInfo.lockingProcesses().Contains(ChildID());
|
2013-02-15 00:41:30 +04:00
|
|
|
if (thisProcessLocks != *dest) {
|
|
|
|
*dest = thisProcessLocks;
|
2013-04-26 04:53:26 +04:00
|
|
|
LOGP("Got wake lock changed event. "
|
|
|
|
"Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d",
|
|
|
|
mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock);
|
2013-02-15 00:41:30 +04:00
|
|
|
ResetPriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!mContentParent) {
|
|
|
|
// We've been shut down.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCString topic(aTopic);
|
|
|
|
|
2017-05-26 18:49:55 +03:00
|
|
|
if (topic.EqualsLiteral("remote-browser-shown")) {
|
2013-04-26 04:53:26 +04:00
|
|
|
OnRemoteBrowserFrameShown(aSubject);
|
|
|
|
} else if (topic.EqualsLiteral("ipc:browser-destroyed")) {
|
|
|
|
OnTabParentDestroyed(aSubject);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
uint64_t
|
|
|
|
ParticularProcessPriorityManager::ChildID() const
|
|
|
|
{
|
|
|
|
// We have to cache mContentParent->ChildID() instead of getting it from the
|
|
|
|
// ContentParent each time because after ShutDown() is called, mContentParent
|
|
|
|
// is null. If we didn't cache ChildID(), then we wouldn't be able to run
|
|
|
|
// LOGP() after ShutDown().
|
|
|
|
return mChildID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ParticularProcessPriorityManager::Pid() const
|
|
|
|
{
|
|
|
|
return mContentParent ? mContentParent->Pid() : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAutoCString&
|
|
|
|
ParticularProcessPriorityManager::NameWithComma()
|
|
|
|
{
|
|
|
|
mNameWithComma.Truncate();
|
|
|
|
if (!mContentParent) {
|
|
|
|
return mNameWithComma; // empty string
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString name;
|
|
|
|
mContentParent->FriendlyName(name);
|
|
|
|
if (name.IsEmpty()) {
|
|
|
|
return mNameWithComma; // empty string
|
|
|
|
}
|
|
|
|
|
|
|
|
mNameWithComma = NS_ConvertUTF16toUTF8(name);
|
|
|
|
mNameWithComma.AppendLiteral(", ");
|
|
|
|
return mNameWithComma;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnRemoteBrowserFrameShown(nsISupports* aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFrameLoader> fl = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(fl);
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2015-02-06 00:47:32 +03:00
|
|
|
TabParent* tp = TabParent::GetFrom(fl);
|
2013-04-26 04:53:26 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(tp);
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-02-06 00:47:32 +03:00
|
|
|
if (tp->Manager() != mContentParent) {
|
2012-08-05 09:09:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-15 04:46:26 +03:00
|
|
|
// Ignore notifications that aren't from a Browser
|
|
|
|
bool isMozBrowser;
|
|
|
|
fl->GetOwnerIsMozBrowserFrame(&isMozBrowser);
|
|
|
|
if (isMozBrowser) {
|
2015-02-10 06:12:26 +03:00
|
|
|
ResetPriority();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->RemoveObserver(this, "remote-browser-shown");
|
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnTabParentDestroyed(nsISupports* aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsITabParent> tp = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(tp);
|
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-02-06 00:47:32 +03:00
|
|
|
if (TabParent::GetFrom(tp)->Manager() != mContentParent) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
uint64_t tabId;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(tp->GetTabId(&tabId)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActiveTabParents.RemoveEntry(tabId);
|
|
|
|
|
2013-01-27 01:14:01 +04:00
|
|
|
ResetPriority();
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::ResetPriority()
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2013-02-27 23:28:57 +04:00
|
|
|
ProcessPriority processPriority = ComputePriority();
|
2013-04-26 04:53:26 +04:00
|
|
|
if (mPriority == PROCESS_PRIORITY_UNKNOWN ||
|
|
|
|
mPriority > processPriority) {
|
2014-01-16 03:17:59 +04:00
|
|
|
// Apps set at a perceivable background priority are often playing media.
|
|
|
|
// Most media will have short gaps while changing tracks between songs,
|
|
|
|
// switching videos, etc. Give these apps a longer grace period so they
|
|
|
|
// can get their next track started, if there is one, before getting
|
|
|
|
// downgraded.
|
|
|
|
if (mPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
|
2015-09-04 06:09:06 +03:00
|
|
|
ScheduleResetPriority(BACKGROUND_PERCEIVABLE_GRACE_PERIOD);
|
2014-01-16 03:17:59 +04:00
|
|
|
} else {
|
2015-09-04 06:09:06 +03:00
|
|
|
ScheduleResetPriority(BACKGROUND_GRACE_PERIOD);
|
2014-01-16 03:17:59 +04:00
|
|
|
}
|
2013-02-27 23:28:57 +04:00
|
|
|
return;
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
2013-02-27 23:28:57 +04:00
|
|
|
|
|
|
|
SetPriorityNow(processPriority);
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::ResetPriorityNow()
|
2013-01-27 01:14:01 +04:00
|
|
|
{
|
2013-02-27 23:28:57 +04:00
|
|
|
SetPriorityNow(ComputePriority());
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
2015-09-04 06:09:06 +03:00
|
|
|
ParticularProcessPriorityManager::ScheduleResetPriority(TimeoutPref aTimeoutPref)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
|
|
|
if (mResetPriorityTimer) {
|
|
|
|
LOGP("ScheduleResetPriority bailing; the timer is already running.");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2015-09-04 06:09:06 +03:00
|
|
|
uint32_t timeout = 0;
|
|
|
|
switch (aTimeoutPref) {
|
|
|
|
case BACKGROUND_PERCEIVABLE_GRACE_PERIOD:
|
|
|
|
timeout = sBackgroundPerceivableGracePeriodMS;
|
|
|
|
break;
|
|
|
|
case BACKGROUND_GRACE_PERIOD:
|
|
|
|
timeout = sBackgroundGracePeriodMS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unrecognized timeout pref");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
LOGP("Scheduling reset timer to fire in %dms.", timeout);
|
2015-12-29 13:17:13 +03:00
|
|
|
mResetPriorityTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
2013-04-26 04:53:26 +04:00
|
|
|
mResetPriorityTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ParticularProcessPriorityManager::Notify(nsITimer* aTimer)
|
|
|
|
{
|
2015-12-29 13:17:13 +03:00
|
|
|
LOGP("Reset priority timer callback; about to ResetPriorityNow.");
|
|
|
|
ResetPriorityNow();
|
|
|
|
mResetPriorityTimer = nullptr;
|
|
|
|
return NS_OK;
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
ProcessPriority
|
|
|
|
ParticularProcessPriorityManager::CurrentPriority()
|
|
|
|
{
|
|
|
|
return mPriority;
|
|
|
|
}
|
|
|
|
|
2013-02-27 23:28:57 +04:00
|
|
|
ProcessPriority
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::ComputePriority()
|
2013-02-23 08:24:28 +04:00
|
|
|
{
|
2017-06-07 10:59:00 +03:00
|
|
|
if (!mActiveTabParents.IsEmpty()) {
|
|
|
|
return PROCESS_PRIORITY_FOREGROUND;
|
|
|
|
}
|
2013-02-23 08:24:28 +04:00
|
|
|
|
2017-05-31 22:23:52 +03:00
|
|
|
if (mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:29:02 +03:00
|
|
|
return PROCESS_PRIORITY_BACKGROUND;
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
void
|
2017-05-26 18:50:17 +03:00
|
|
|
ParticularProcessPriorityManager::SetPriorityNow(ProcessPriority aPriority)
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2014-08-29 07:53:49 +04:00
|
|
|
if (aPriority == PROCESS_PRIORITY_UNKNOWN) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if (!ProcessPriorityManagerImpl::PrefsEnabled() ||
|
|
|
|
!mContentParent ||
|
2017-05-26 18:50:17 +03:00
|
|
|
mPriority == aPriority) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 18:50:17 +03:00
|
|
|
if (mPriority == aPriority) {
|
|
|
|
hal::SetProcessPriority(Pid(), mPriority);
|
2015-02-25 11:37:38 +03:00
|
|
|
return;
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
LOGP("Changing priority from %s to %s.",
|
2015-02-26 14:43:22 +03:00
|
|
|
ProcessPriorityToString(mPriority),
|
|
|
|
ProcessPriorityToString(aPriority));
|
2013-05-10 00:27:06 +04:00
|
|
|
|
|
|
|
ProcessPriority oldPriority = mPriority;
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
mPriority = aPriority;
|
2015-02-26 14:43:22 +03:00
|
|
|
hal::SetProcessPriority(Pid(), mPriority);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
if (oldPriority != mPriority) {
|
2015-02-26 14:43:22 +03:00
|
|
|
ProcessPriorityManagerImpl::GetSingleton()->
|
|
|
|
NotifyProcessPriorityChanged(this, oldPriority);
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mContentParent->SendNotifyProcessPriorityChanged(mPriority);
|
2012-10-22 23:40:19 +04:00
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
FireTestOnlyObserverNotification("process-priority-set",
|
2015-02-26 14:43:22 +03:00
|
|
|
ProcessPriorityToString(mPriority));
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::TabActivityChanged(TabParent* aTabParent,
|
|
|
|
bool aIsActive)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTabParent);
|
|
|
|
|
|
|
|
if (!aIsActive) {
|
|
|
|
mActiveTabParents.RemoveEntry(aTabParent->GetTabId());
|
|
|
|
} else {
|
|
|
|
mActiveTabParents.PutEntry(aTabParent->GetTabId());
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetPriority();
|
|
|
|
}
|
|
|
|
|
2013-01-27 01:14:01 +04:00
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::ShutDown()
|
2013-01-27 01:14:01 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
MOZ_ASSERT(mContentParent);
|
|
|
|
|
2013-05-08 00:44:25 +04:00
|
|
|
UnregisterWakeLockObserver(this);
|
|
|
|
|
2013-01-27 01:14:01 +04:00
|
|
|
if (mResetPriorityTimer) {
|
2013-04-26 04:53:26 +04:00
|
|
|
mResetPriorityTimer->Cancel();
|
|
|
|
mResetPriorityTimer = nullptr;
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
mContentParent = nullptr;
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData /* = EmptyCString() */)
|
2013-01-27 01:14:01 +04:00
|
|
|
{
|
2015-09-04 06:09:06 +03:00
|
|
|
if (!TestMode()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE_VOID(os);
|
|
|
|
|
|
|
|
nsPrintfCString topic("process-priority-manager:TEST-ONLY:%s", aTopic);
|
|
|
|
|
|
|
|
LOG("Notifying observer %s, data %s",
|
|
|
|
topic.get(), PromiseFlatCString(aData).get());
|
|
|
|
os->NotifyObservers(nullptr, topic.get(), NS_ConvertUTF8toUTF16(aData).get());
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const char* aData /* = nullptr */ )
|
2013-01-27 01:14:01 +04:00
|
|
|
{
|
2015-09-04 06:09:06 +03:00
|
|
|
if (!ProcessPriorityManagerImpl::TestMode()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsAutoCString data;
|
|
|
|
if (aData) {
|
|
|
|
data.AppendASCII(aData);
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
FireTestOnlyObserverNotification(aTopic, data);
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData /* = EmptyCString() */)
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2015-09-04 06:09:06 +03:00
|
|
|
if (!ProcessPriorityManagerImpl::TestMode()) {
|
2012-08-05 09:09:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:32:21 +03:00
|
|
|
nsAutoCString data(nsPrintfCString("%" PRIu64, ChildID()));
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!aData.IsEmpty()) {
|
2014-05-22 07:48:51 +04:00
|
|
|
data.Append(':');
|
2013-04-26 04:53:26 +04:00
|
|
|
data.Append(aData);
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
// ProcessPriorityManagerImpl::GetSingleton() is guaranteed not to return
|
|
|
|
// null, since ProcessPriorityManagerImpl is the only class which creates
|
|
|
|
// ParticularProcessPriorityManagers.
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton()->
|
|
|
|
FireTestOnlyObserverNotification(aTopic, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticRefPtr<ProcessPriorityManagerChild>
|
|
|
|
ProcessPriorityManagerChild::sSingleton;
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManagerChild::StaticInit()
|
|
|
|
{
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new ProcessPriorityManagerChild();
|
2013-07-18 13:00:56 +04:00
|
|
|
sSingleton->Init();
|
2013-04-26 04:53:26 +04:00
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ ProcessPriorityManagerChild*
|
|
|
|
ProcessPriorityManagerChild::Singleton()
|
|
|
|
{
|
|
|
|
StaticInit();
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:23:52 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ProcessPriorityManagerChild, nsIObserver)
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerChild::ProcessPriorityManagerChild()
|
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
mCachedPriority = PROCESS_PRIORITY_MASTER;
|
|
|
|
} else {
|
|
|
|
mCachedPriority = PROCESS_PRIORITY_UNKNOWN;
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerChild::Init()
|
|
|
|
{
|
|
|
|
// The process priority should only be changed in child processes; don't even
|
|
|
|
// bother listening for changes if we're in the main process.
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE_VOID(os);
|
|
|
|
os->AddObserver(this, "ipc:process-priority-changed", /* weak = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-05-31 22:23:52 +03:00
|
|
|
ProcessPriorityManagerChild::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "ipc:process-priority-changed"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(props, NS_OK);
|
|
|
|
|
|
|
|
int32_t priority = static_cast<int32_t>(PROCESS_PRIORITY_UNKNOWN);
|
|
|
|
props->GetPropertyAsInt32(NS_LITERAL_STRING("priority"), &priority);
|
|
|
|
NS_ENSURE_TRUE(ProcessPriority(priority) != PROCESS_PRIORITY_UNKNOWN, NS_OK);
|
|
|
|
|
|
|
|
mCachedPriority = static_cast<ProcessPriority>(priority);
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-01-04 03:39:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerChild::CurrentProcessIsForeground()
|
2013-01-04 03:39:25 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
return mCachedPriority == PROCESS_PRIORITY_UNKNOWN ||
|
|
|
|
mCachedPriority >= PROCESS_PRIORITY_FOREGROUND;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2013-01-07 12:44:22 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::Init()
|
|
|
|
{
|
|
|
|
ProcessPriorityManagerImpl::StaticInit();
|
|
|
|
ProcessPriorityManagerChild::StaticInit();
|
2015-09-04 06:09:06 +03:00
|
|
|
ParticularProcessPriorityManager::StaticInit();
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::SetProcessPriority(ContentParent* aContentParent,
|
|
|
|
ProcessPriority aPriority)
|
2013-01-27 01:14:01 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl* singleton =
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton();
|
|
|
|
if (singleton) {
|
|
|
|
singleton->SetProcessPriority(aContentParent, aPriority);
|
2013-01-27 01:14:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManager::CurrentProcessIsForeground()
|
|
|
|
{
|
|
|
|
return ProcessPriorityManagerChild::Singleton()->
|
|
|
|
CurrentProcessIsForeground();
|
|
|
|
}
|
|
|
|
|
2017-06-07 10:59:00 +03:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::TabActivityChanged(TabParent* aTabParent,
|
|
|
|
bool aIsActive)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTabParent);
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl* singleton =
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton();
|
|
|
|
if (!singleton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
singleton->TabActivityChanged(aTabParent, aIsActive);
|
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
} // namespace mozilla
|