2012-08-05 09:09:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et ft=cpp : */
|
|
|
|
/* 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"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "mozilla/unused.h"
|
2013-01-05 09:03:51 +04:00
|
|
|
#include "AudioChannelService.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "prlog.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"
|
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"
|
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__)
|
2013-04-26 04:53:26 +04:00
|
|
|
|
2013-02-08 18:32:23 +04:00
|
|
|
#elif defined(PR_LOGGING)
|
2013-04-26 04:53:26 +04:00
|
|
|
static PRLogModuleInfo*
|
|
|
|
GetPPMLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("ProcessPriorityManager");
|
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
# define LOG(fmt, ...) \
|
|
|
|
PR_LOG(GetPPMLog(), PR_LOG_DEBUG, \
|
|
|
|
("ProcessPriorityManager - " fmt, ##__VA_ARGS__))
|
|
|
|
# define LOGP(fmt, ...) \
|
|
|
|
PR_LOG(GetPPMLog(), PR_LOG_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
|
|
|
#else
|
|
|
|
#define LOG(fmt, ...)
|
2013-04-26 04:53:26 +04:00
|
|
|
#define LOGP(fmt, ...)
|
2012-08-05 09:09:39 +04:00
|
|
|
#endif
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::hal;
|
2012-12-19 06:37:35 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ParticularProcessPriorityManager;
|
2012-12-19 06:37:35 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
class ProcessLRUPool MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a new process LRU pool for the specified priority.
|
|
|
|
*/
|
|
|
|
ProcessLRUPool(ProcessPriority aPriority, uint32_t aBias);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to remove a particular process priority manager from the LRU pool
|
|
|
|
* when the associated ContentParent is destroyed or its priority changes.
|
|
|
|
*/
|
|
|
|
void Remove(ParticularProcessPriorityManager* aParticularManager);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to add a particular process priority manager into the LRU pool when
|
|
|
|
* the associated ContentParent's priority changes.
|
|
|
|
*/
|
|
|
|
void Add(ParticularProcessPriorityManager* aParticularManager);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProcessPriority mPriority;
|
|
|
|
uint32_t mLRUPoolLevels;
|
|
|
|
uint32_t mLRUPoolSize;
|
|
|
|
uint32_t mBias;
|
|
|
|
nsTArray<ParticularProcessPriorityManager*> mLRUPool;
|
|
|
|
|
|
|
|
uint32_t CalculateLRULevel(uint32_t aLRUPoolIndex);
|
|
|
|
|
|
|
|
void AdjustLRUValues(
|
|
|
|
nsTArray<ParticularProcessPriorityManager*>::index_type aStart,
|
|
|
|
bool removed);
|
|
|
|
|
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(ProcessLRUPool);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2013-04-26 04:53:26 +04:00
|
|
|
class ProcessPriorityManagerImpl MOZ_FINAL
|
2012-08-05 09:09:39 +04:00
|
|
|
: public nsIObserver
|
2014-05-01 17:47:26 +04:00
|
|
|
, public WakeLockObserver
|
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();
|
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,
|
2013-10-01 07:54:59 +04:00
|
|
|
ProcessPriority aPriority,
|
2015-02-25 11:37:38 +03:00
|
|
|
uint32_t aLRU = 0);
|
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
|
|
|
|
2014-02-17 20:45:29 +04:00
|
|
|
/**
|
|
|
|
* Does one of the child processes have priority FOREGROUND_HIGH?
|
|
|
|
*/
|
|
|
|
bool ChildProcessHasHighPriority();
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
virtual void Notify(const WakeLockInformation& aInfo) MOZ_OVERRIDE;
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
private:
|
|
|
|
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);
|
|
|
|
|
|
|
|
nsDataHashtable<nsUint64HashKey, nsRefPtr<ParticularProcessPriorityManager> >
|
|
|
|
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;
|
2015-02-25 11:37:38 +03:00
|
|
|
|
|
|
|
/** Contains a pseudo-LRU list of background processes */
|
|
|
|
ProcessLRUPool mBackgroundLRUPool;
|
|
|
|
|
|
|
|
/** Contains a pseudo-LRU list of foreground processes */
|
|
|
|
ProcessLRUPool mForegroundLRUPool;
|
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.
|
|
|
|
*/
|
|
|
|
class ProcessPriorityManagerChild MOZ_FINAL
|
|
|
|
: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void StaticInit();
|
|
|
|
static ProcessPriorityManagerChild* Singleton();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
bool CurrentProcessIsForeground();
|
2014-02-17 20:45:29 +04:00
|
|
|
bool CurrentProcessIsHighPriority();
|
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.
|
|
|
|
*/
|
|
|
|
class ParticularProcessPriorityManager MOZ_FINAL
|
|
|
|
: public WakeLockObserver
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsITimerCallback
|
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:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit ParticularProcessPriorityManager(ContentParent* aContentParent);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
|
|
|
virtual void Notify(const WakeLockInformation& aInfo) MOZ_OVERRIDE;
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
int32_t Pid() const;
|
|
|
|
uint64_t ChildID() const;
|
2013-10-01 07:54:59 +04:00
|
|
|
bool IsPreallocated() 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();
|
|
|
|
|
|
|
|
bool HasAppType(const char* aAppType);
|
2013-04-26 04:53:26 +04:00
|
|
|
bool IsExpectingSystemMessage();
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
void OnAudioChannelProcessChanged(nsISupports* aSubject);
|
|
|
|
void OnRemoteBrowserFrameShown(nsISupports* aSubject);
|
|
|
|
void OnTabParentDestroyed(nsISupports* aSubject);
|
|
|
|
void OnFrameloaderVisibleChanged(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
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void ScheduleResetPriority(const char* aTimeoutPref);
|
|
|
|
void ResetPriority();
|
|
|
|
void ResetPriorityNow();
|
2015-02-25 11:37:38 +03:00
|
|
|
void SetPriorityNow(ProcessPriority aPriority, uint32_t aLRU = 0);
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void ShutDown();
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
private:
|
|
|
|
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;
|
2015-02-25 11:37:38 +03:00
|
|
|
uint32_t mLRU;
|
2013-04-26 04:53:26 +04:00
|
|
|
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;
|
|
|
|
};
|
2013-02-23 08:24:28 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sInitialized = false;
|
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sPrefListenersRegistered = false;
|
|
|
|
/* static */ StaticRefPtr<ProcessPriorityManagerImpl>
|
|
|
|
ProcessPriorityManagerImpl::sSingleton;
|
2013-01-27 01:14:01 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ProcessPriorityManagerImpl,
|
|
|
|
nsIObserver);
|
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();
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManagerImpl::PrefsEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("dom.ipc.processPriorityManager.enabled") &&
|
|
|
|
!Preferences::GetBool("dom.ipc.tabs.disabled");
|
|
|
|
}
|
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.
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
sInitialized = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2014-05-01 17:47:26 +04:00
|
|
|
: mHighPriority(false)
|
2015-02-25 11:37:38 +03:00
|
|
|
, mBackgroundLRUPool(PROCESS_PRIORITY_BACKGROUND, 1)
|
|
|
|
, mForegroundLRUPool(PROCESS_PRIORITY_FOREGROUND, 0)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
2014-05-01 17:47:26 +04:00
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl::~ProcessPriorityManagerImpl()
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
os->AddObserver(this, "ipc:content-created", /* ownsWeak */ false);
|
|
|
|
os->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ false);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2015-02-10 06:12:26 +03:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
// Do not attempt to change the priority of the Nuwa process
|
|
|
|
if (aContentParent->IsNuwaProcess()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm;
|
2014-10-24 04:28:00 +04:00
|
|
|
uint64_t cpId = aContentParent->ChildID();
|
|
|
|
mParticularManagers.Get(cpId, &pppm);
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!pppm) {
|
|
|
|
pppm = new ParticularProcessPriorityManager(aContentParent);
|
|
|
|
pppm->Init();
|
2014-10-24 04:28:00 +04:00
|
|
|
mParticularManagers.Put(cpId, pppm);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
FireTestOnlyObserverNotification("process-created",
|
2014-10-24 04:28:00 +04:00
|
|
|
nsPrintfCString("%lld", 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,
|
2013-10-01 07:54:59 +04:00
|
|
|
ProcessPriority aPriority,
|
2015-02-25 11:37:38 +03:00
|
|
|
uint32_t aLRU)
|
2013-04-26 04:53:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm =
|
|
|
|
GetParticularProcessPriorityManager(aContentParent);
|
2015-02-10 06:12:26 +03:00
|
|
|
if (pppm) {
|
2015-02-25 11:37:38 +03:00
|
|
|
pppm->SetPriorityNow(aPriority, aLRU);
|
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);
|
2013-04-26 04:53:26 +04:00
|
|
|
nsRefPtr<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);
|
|
|
|
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm;
|
|
|
|
mParticularManagers.Get(childID, &pppm);
|
|
|
|
if (pppm) {
|
2015-02-25 11:37:38 +03:00
|
|
|
// Unconditionally remove the manager from the pools
|
|
|
|
mBackgroundLRUPool.Remove(pppm);
|
|
|
|
mForegroundLRUPool.Remove(pppm);
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
pppm->ShutDown();
|
|
|
|
|
2015-02-10 06:12:26 +03:00
|
|
|
mParticularManagers.Remove(childID);
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-02-10 06:12:26 +03:00
|
|
|
if (mHighPriorityChildIDs.Contains(childID)) {
|
|
|
|
mHighPriorityChildIDs.RemoveEntry(childID);
|
|
|
|
}
|
2014-05-01 17:47:26 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2014-02-17 20:45:29 +04:00
|
|
|
bool
|
|
|
|
ProcessPriorityManagerImpl::ChildProcessHasHighPriority( void )
|
|
|
|
{
|
|
|
|
return mHighPriorityChildIDs.Count() > 0;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
bool isPreallocated = aParticularManager->IsPreallocated();
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if (newPriority == PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
aOldPriority != PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
!isPreallocated) {
|
|
|
|
mBackgroundLRUPool.Add(aParticularManager);
|
|
|
|
} else if (newPriority != PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
aOldPriority == PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
!isPreallocated) {
|
|
|
|
mBackgroundLRUPool.Remove(aParticularManager);
|
|
|
|
}
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if (newPriority == PROCESS_PRIORITY_FOREGROUND &&
|
|
|
|
aOldPriority != PROCESS_PRIORITY_FOREGROUND) {
|
|
|
|
mForegroundLRUPool.Add(aParticularManager);
|
|
|
|
} else if (newPriority != PROCESS_PRIORITY_FOREGROUND &&
|
|
|
|
aOldPriority == PROCESS_PRIORITY_FOREGROUND) {
|
|
|
|
mForegroundLRUPool.Remove(aParticularManager);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ParticularProcessPriorityManager,
|
|
|
|
nsIObserver,
|
|
|
|
nsITimerCallback,
|
|
|
|
nsISupportsWeakReference);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
|
|
|
ParticularProcessPriorityManager::ParticularProcessPriorityManager(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
: mContentParent(aContentParent)
|
|
|
|
, mChildID(aContentParent->ChildID())
|
|
|
|
, mPriority(PROCESS_PRIORITY_UNKNOWN)
|
2015-02-25 11:37:38 +03:00
|
|
|
, mLRU(0)
|
2013-04-26 04:53:26 +04:00
|
|
|
, mHoldsCPUWakeLock(false)
|
|
|
|
, mHoldsHighPriorityWakeLock(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
|
|
|
LOGP("Creating ParticularProcessPriorityManager.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::Init()
|
|
|
|
{
|
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
2013-05-10 00:27:06 +04:00
|
|
|
os->AddObserver(this, "audio-channel-process-changed", /* ownsWeak */ true);
|
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);
|
|
|
|
os->AddObserver(this, "frameloader-visible-changed", /* 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);
|
|
|
|
|
|
|
|
if (topic.EqualsLiteral("audio-channel-process-changed")) {
|
|
|
|
OnAudioChannelProcessChanged(aSubject);
|
2014-02-21 02:19:18 +04:00
|
|
|
} else 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 if (topic.EqualsLiteral("frameloader-visible-changed")) {
|
|
|
|
OnFrameloaderVisibleChanged(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;
|
|
|
|
}
|
|
|
|
|
2013-10-01 07:54:59 +04:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::IsPreallocated() const
|
|
|
|
{
|
|
|
|
return mContentParent ? mContentParent->IsPreallocated() : false;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
ParticularProcessPriorityManager::OnAudioChannelProcessChanged(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);
|
|
|
|
if (childID == ChildID()) {
|
|
|
|
ResetPriority();
|
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
|
|
|
|
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
|
|
|
|
2014-06-11 09:44:03 +04:00
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
2015-02-06 00:47:32 +03:00
|
|
|
if (tp->Manager() != mContentParent) {
|
2012-08-05 09:09:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-10 06:12:26 +03:00
|
|
|
// Ignore notifications that aren't from a BrowserOrApp
|
|
|
|
bool isBrowserOrApp;
|
|
|
|
fl->GetOwnerIsBrowserOrAppFrame(&isBrowserOrApp);
|
|
|
|
if (isBrowserOrApp) {
|
|
|
|
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);
|
|
|
|
|
2014-06-11 09:44:03 +04:00
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
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
|
|
|
|
2013-01-27 01:14:01 +04:00
|
|
|
ResetPriority();
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnFrameloaderVisibleChanged(nsISupports* aSubject)
|
2013-02-15 00:41:30 +04:00
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
nsCOMPtr<nsIFrameLoader> fl = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(fl);
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2015-02-06 00:47:32 +03:00
|
|
|
TabParent* tp = TabParent::GetFrom(fl);
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!tp) {
|
|
|
|
return;
|
2013-02-15 00:41:30 +04:00
|
|
|
}
|
|
|
|
|
2014-06-11 09:44:03 +04:00
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
2015-02-06 00:47:32 +03:00
|
|
|
if (tp->Manager() != mContentParent) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
2013-02-15 00:41:30 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
// Most of the time when something changes in a process we call
|
|
|
|
// ResetPriority(), giving a grace period before downgrading its priority.
|
|
|
|
// But notice that here don't give a grace period: We call ResetPriorityNow()
|
|
|
|
// instead.
|
|
|
|
//
|
|
|
|
// We do this because we're reacting here to a setVisibility() call, which is
|
|
|
|
// an explicit signal from the process embedder that we should re-prioritize
|
|
|
|
// a process. If we gave a grace period in response to setVisibility()
|
|
|
|
// calls, it would be impossible for the embedder to explicitly prioritize
|
|
|
|
// processes and prevent e.g. the case where we switch which process is in
|
|
|
|
// the foreground and, during the old fg processs's grace period, it OOMs the
|
|
|
|
// new fg process.
|
|
|
|
|
|
|
|
ResetPriorityNow();
|
2013-02-15 00:41:30 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
ScheduleResetPriority("backgroundPerceivableGracePeriodMS");
|
|
|
|
} else {
|
|
|
|
ScheduleResetPriority("backgroundGracePeriodMS");
|
|
|
|
}
|
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
|
|
|
|
ParticularProcessPriorityManager::ScheduleResetPriority(const char* aTimeoutPref)
|
|
|
|
{
|
|
|
|
if (mResetPriorityTimer) {
|
|
|
|
LOGP("ScheduleResetPriority bailing; the timer is already running.");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
uint32_t timeout = Preferences::GetUint(
|
|
|
|
nsPrintfCString("dom.ipc.processPriorityManager.%s", aTimeoutPref).get());
|
|
|
|
LOGP("Scheduling reset timer to fire in %dms.", timeout);
|
|
|
|
mResetPriorityTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOGP("Reset priority timer callback; about to ResetPriorityNow.");
|
|
|
|
ResetPriorityNow();
|
|
|
|
mResetPriorityTimer = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::HasAppType(const char* aAppType)
|
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
mContentParent->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); i++) {
|
|
|
|
nsAutoString appType;
|
2015-02-06 00:47:32 +03:00
|
|
|
TabParent::GetFrom(browsers[i])->GetAppType(appType);
|
2013-04-26 04:53:26 +04:00
|
|
|
if (appType.EqualsASCII(aAppType)) {
|
|
|
|
return true;
|
2013-02-15 00:41:29 +04:00
|
|
|
}
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
return false;
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::IsExpectingSystemMessage()
|
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
mContentParent->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); i++) {
|
2015-02-06 00:47:32 +03:00
|
|
|
TabParent* tp = TabParent::GetFrom(browsers[i]);
|
2013-07-24 03:39:17 +04:00
|
|
|
nsCOMPtr<nsIMozBrowserFrame> bf = do_QueryInterface(tp->GetOwnerElement());
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!bf) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bf->GetIsExpectingSystemMessage()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-08-01 22:02:55 +04:00
|
|
|
if ((mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) &&
|
2013-04-26 04:53:26 +04:00
|
|
|
HasAppType("critical")) {
|
|
|
|
return PROCESS_PRIORITY_FOREGROUND_HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVisible = false;
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
mContentParent->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); i++) {
|
2015-02-06 00:47:32 +03:00
|
|
|
if (TabParent::GetFrom(browsers[i])->IsVisible()) {
|
2013-04-26 04:53:26 +04:00
|
|
|
isVisible = true;
|
|
|
|
break;
|
2013-02-27 23:28:57 +04:00
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVisible) {
|
2014-05-30 17:29:43 +04:00
|
|
|
return HasAppType("inputmethod") ?
|
2013-09-24 12:10:20 +04:00
|
|
|
PROCESS_PRIORITY_FOREGROUND_KEYBOARD :
|
|
|
|
PROCESS_PRIORITY_FOREGROUND;
|
2013-02-23 08:24:28 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 22:02:55 +04:00
|
|
|
if ((mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) &&
|
2013-04-26 04:53:26 +04:00
|
|
|
IsExpectingSystemMessage()) {
|
|
|
|
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
|
|
|
|
}
|
|
|
|
|
2014-08-29 10:10:16 +04:00
|
|
|
AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService();
|
2013-04-26 04:53:26 +04:00
|
|
|
if (service->ProcessContentOrNormalChannelIsActive(ChildID())) {
|
2013-02-27 23:28:57 +04:00
|
|
|
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
return HasAppType("homescreen") ?
|
2013-02-27 23:28:57 +04:00
|
|
|
PROCESS_PRIORITY_BACKGROUND_HOMESCREEN :
|
|
|
|
PROCESS_PRIORITY_BACKGROUND;
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::SetPriorityNow(ProcessPriority aPriority,
|
2015-02-25 11:37:38 +03:00
|
|
|
uint32_t aLRU)
|
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 ||
|
|
|
|
((mPriority == aPriority) && (mLRU == aLRU))) {
|
2013-04-26 04:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if ((mPriority == aPriority) && (mLRU != aLRU)) {
|
|
|
|
mLRU = aLRU;
|
|
|
|
hal::SetProcessPriority(Pid(), mPriority, aLRU);
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
nsPrintfCString processPriorityWithLRU("%s:%d",
|
|
|
|
ProcessPriorityToString(mPriority), aLRU);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
FireTestOnlyObserverNotification("process-priority-with-LRU-set",
|
|
|
|
processPriorityWithLRU.get());
|
|
|
|
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);
|
|
|
|
|
2013-05-10 00:27:06 +04:00
|
|
|
unused << mContentParent->SendNotifyProcessPriorityChanged(mPriority);
|
|
|
|
}
|
2012-10-22 23:40:19 +04:00
|
|
|
|
2014-02-12 19:50:39 +04:00
|
|
|
if (aPriority < PROCESS_PRIORITY_FOREGROUND) {
|
|
|
|
unused << mContentParent->SendFlushMemory(NS_LITERAL_STRING("low-memory"));
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
|
|
|
|
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
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
|
|
|
|
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
|
|
|
{
|
2013-04-26 04:53:26 +04:00
|
|
|
if (!Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
|
2012-08-05 09:09:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsAutoCString data(nsPrintfCString("%lld", ChildID()));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ProcessPriorityManagerChild,
|
|
|
|
nsIObserver)
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
ProcessPriorityManagerChild::ProcessPriorityManagerChild()
|
|
|
|
{
|
2012-08-05 09:09:39 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
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.
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE_VOID(os);
|
|
|
|
os->AddObserver(this, "ipc:process-priority-changed", /* weak = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ProcessPriorityManagerChild::Observe(
|
|
|
|
nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-17 20:45:29 +04:00
|
|
|
bool
|
|
|
|
ProcessPriorityManagerChild::CurrentProcessIsHighPriority()
|
|
|
|
{
|
|
|
|
return mCachedPriority == PROCESS_PRIORITY_UNKNOWN ||
|
|
|
|
mCachedPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH;
|
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
ProcessLRUPool::ProcessLRUPool(ProcessPriority aPriority, uint32_t aBias)
|
|
|
|
: mPriority(aPriority)
|
|
|
|
, mLRUPoolLevels(1)
|
|
|
|
, mBias(aBias)
|
2013-10-01 07:54:59 +04:00
|
|
|
{
|
2015-02-25 11:37:38 +03:00
|
|
|
// We set mLRUPoolLevels according to our pref.
|
2013-10-01 07:54:59 +04:00
|
|
|
// This value is used to set background process LRU pool
|
2015-02-25 11:37:38 +03:00
|
|
|
const char* str = ProcessPriorityToString(aPriority);
|
|
|
|
nsPrintfCString pref("dom.ipc.processPriorityManager.%s.LRUPoolLevels", str);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
Preferences::GetUint(pref.get(), &mLRUPoolLevels);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
|
|
|
// GonkHal defines OOM_ADJUST_MAX is 15 and b2g.js defines
|
|
|
|
// PROCESS_PRIORITY_BACKGROUND's oom_score_adj is 667 and oom_adj is 10.
|
|
|
|
// This means we can only have at most (15 -10 + 1) = 6 background LRU levels.
|
2015-02-25 11:37:38 +03:00
|
|
|
// Similarly we can have at most 4 foreground LRU levels. We should really be
|
|
|
|
// getting rid of oom_adj and just rely on oom_score_adj only which would
|
|
|
|
// lift this constraint.
|
|
|
|
MOZ_ASSERT(aPriority != PROCESS_PRIORITY_BACKGROUND || mLRUPoolLevels <= 6);
|
|
|
|
MOZ_ASSERT(aPriority != PROCESS_PRIORITY_FOREGROUND || mLRUPoolLevels <= 4);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
|
|
|
// LRU pool size = 2 ^ (number of background LRU pool levels) - 1
|
|
|
|
mLRUPoolSize = (1 << mLRUPoolLevels) - 1;
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
LOG("Making %s LRU pool with size(%d)", str, mLRUPoolSize);
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
uint32_t
|
|
|
|
ProcessLRUPool::CalculateLRULevel(uint32_t aLRU)
|
2013-10-01 07:54:59 +04:00
|
|
|
{
|
2015-02-25 11:37:38 +03:00
|
|
|
// This is used to compute the LRU adjustment for the specified LRU position.
|
|
|
|
// We use power-of-two groups with increasing adjustments that look like the
|
|
|
|
// following:
|
|
|
|
|
|
|
|
// Priority : LRU0, LRU1
|
|
|
|
// Priority+1: LRU2, LRU3
|
|
|
|
// Priority+2: LRU4, LRU5, LRU6, LRU7
|
|
|
|
// Priority+3: LRU8, LRU9, LRU10, LRU11, LRU12, LRU12, LRU13, LRU14, LRU15
|
|
|
|
// ...
|
|
|
|
// Priority+L-1: 2^(number of LRU pool levels - 1)
|
|
|
|
// (End of buffer)
|
|
|
|
|
|
|
|
// Biasing the input can be used to shift the assignment
|
|
|
|
|
|
|
|
int exp;
|
|
|
|
unused << frexp(static_cast<double>(aLRU), &exp);
|
|
|
|
uint32_t level = std::max(exp - 1, 0);
|
|
|
|
|
|
|
|
return std::min(mLRUPoolLevels - 1, level);
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
void
|
|
|
|
ProcessLRUPool::Remove(ParticularProcessPriorityManager* aParticularManager)
|
2013-10-01 07:54:59 +04:00
|
|
|
{
|
2015-02-25 11:37:38 +03:00
|
|
|
nsTArray<ParticularProcessPriorityManager*>::index_type index =
|
|
|
|
mLRUPool.IndexOf(aParticularManager);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
if (index == nsTArray<ParticularProcessPriorityManager*>::NoIndex) {
|
|
|
|
return;
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
mLRUPool.RemoveElementAt(index);
|
|
|
|
AdjustLRUValues(index, /* removed */ true);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
LOG("Remove ChildID(%" PRIu64 ") from %s LRU pool",
|
|
|
|
static_cast<uint64_t>(aParticularManager->ChildID()),
|
|
|
|
ProcessPriorityToString(mPriority));
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
/*
|
|
|
|
* Adjust the LRU values of all the processes in an LRU pool. When true the
|
|
|
|
* `removed` parameter indicates that the processes were shifted left because
|
|
|
|
* an element was removed; otherwise it means the elements were shifted right
|
|
|
|
* as an element was added.
|
|
|
|
*/
|
2013-10-01 07:54:59 +04:00
|
|
|
void
|
2015-02-25 11:37:38 +03:00
|
|
|
ProcessLRUPool::AdjustLRUValues(
|
|
|
|
nsTArray<ParticularProcessPriorityManager*>::index_type aStart,
|
|
|
|
bool removed)
|
|
|
|
{
|
|
|
|
uint32_t adj = (removed ? 1 : 0) + mBias;
|
|
|
|
|
|
|
|
for (nsTArray<ParticularProcessPriorityManager*>::index_type i = aStart;
|
|
|
|
i < mLRUPool.Length();
|
|
|
|
i++) {
|
|
|
|
/* Check whether i is a power of two. If so, then it crossed a LRU group
|
|
|
|
* boundary and we need to assign its new process priority LRU. Note that
|
|
|
|
* depending on the direction and the bias this test will pick different
|
|
|
|
* elements. */
|
|
|
|
if (((i + adj) & (i + adj - 1)) == 0) {
|
|
|
|
mLRUPool[i]->SetPriorityNow(mPriority, CalculateLRULevel(i + mBias));
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-25 11:37:38 +03:00
|
|
|
ProcessLRUPool::Add(ParticularProcessPriorityManager* aParticularManager)
|
2013-10-01 07:54:59 +04:00
|
|
|
{
|
|
|
|
// Shift the list in the pool, so we have room at index 0 for the newly added
|
2015-02-25 11:37:38 +03:00
|
|
|
// manager
|
|
|
|
mLRUPool.InsertElementAt(0, aParticularManager);
|
|
|
|
AdjustLRUValues(1, /* removed */ false);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
LOG("Add ChildID(%" PRIu64 ") into %s LRU pool",
|
|
|
|
static_cast<uint64_t>(aParticularManager->ChildID()),
|
|
|
|
ProcessPriorityToString(mPriority));
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
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();
|
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();
|
|
|
|
}
|
|
|
|
|
2014-02-17 20:45:29 +04:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManager::AnyProcessHasHighPriority()
|
|
|
|
{
|
|
|
|
ProcessPriorityManagerImpl* singleton =
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton();
|
|
|
|
|
|
|
|
if (singleton) {
|
|
|
|
return singleton->ChildProcessHasHighPriority();
|
|
|
|
} else {
|
|
|
|
return ProcessPriorityManagerChild::Singleton()->
|
|
|
|
CurrentProcessIsHighPriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
} // namespace mozilla
|