2012-08-05 05:09:39 +00: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 00:53:26 +00:00
|
|
|
#include "ProcessPriorityManager.h"
|
2013-01-03 23:39:25 +00:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2013-07-23 23:39:17 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2012-08-05 05:09:39 +00:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "mozilla/unused.h"
|
2013-01-05 05:03:51 +00:00
|
|
|
#include "AudioChannelService.h"
|
2012-08-05 05:09:39 +00:00
|
|
|
#include "prlog.h"
|
2013-01-26 21:14:01 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2012-08-05 05:09:39 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2013-05-17 20:17:53 +00:00
|
|
|
#include "nsIFrameLoader.h"
|
2012-08-05 05:09:39 +00:00
|
|
|
#include "nsIObserverService.h"
|
2013-01-03 23:39:25 +00:00
|
|
|
#include "StaticPtr.h"
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "nsIMozBrowserFrame.h"
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsITimer.h"
|
2013-09-23 21:30:40 +00:00
|
|
|
#include "nsIPropertyBag2.h"
|
2013-09-10 20:56:05 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-08-05 05:09:39 +00:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
// Use LOGP inside a ParticularProcessPriorityManager method; use LOG
|
|
|
|
// everywhere else. LOGP prints out information about the particular process
|
|
|
|
// priority manager.
|
2012-08-05 05:09:39 +00:00
|
|
|
//
|
2013-04-26 00:53:26 +00:00
|
|
|
// (Wow, our logging story is a huge mess.)
|
|
|
|
|
|
|
|
// #define ENABLE_LOGGING 1
|
|
|
|
|
|
|
|
#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 14:32:23 +00:00
|
|
|
"Gecko:ProcessPriorityManager", \
|
2013-04-26 00:53:26 +00:00
|
|
|
"[%schild-id=%llu, pid=%d] " fmt, \
|
|
|
|
NameWithComma().get(), \
|
|
|
|
(long long unsigned) ChildID(), Pid(), ## __VA_ARGS__)
|
|
|
|
|
|
|
|
#elif defined(ENABLE_LOGGING)
|
|
|
|
# define LOG(fmt, ...) \
|
|
|
|
printf("ProcessPriorityManager - " fmt "\n", ##__VA_ARGS__)
|
|
|
|
# define LOGP(fmt, ...) \
|
|
|
|
printf("ProcessPriorityManager[%schild-id=%llu, pid=%d] - " fmt "\n", \
|
|
|
|
NameWithComma().get(), \
|
|
|
|
(unsigned long long) ChildID(), Pid(), ##__VA_ARGS__)
|
|
|
|
|
2013-02-08 14:32:23 +00:00
|
|
|
#elif defined(PR_LOGGING)
|
2013-04-26 00:53:26 +00: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, \
|
|
|
|
("ProcessPriorityManager[%schild-id=%llu, pid=%d] - " fmt, \
|
|
|
|
NameWithComma().get(), \
|
|
|
|
(unsigned long long) ChildID(), Pid(), ##__VA_ARGS__))
|
2012-08-05 05:09:39 +00:00
|
|
|
#else
|
|
|
|
#define LOG(fmt, ...)
|
2013-04-26 00:53:26 +00:00
|
|
|
#define LOGP(fmt, ...)
|
2012-08-05 05:09:39 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::hal;
|
2012-12-19 02:37:35 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ParticularProcessPriorityManager;
|
2012-12-19 02:37:35 +00:00
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
/**
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
*
|
2013-04-26 00:53:26 +00:00
|
|
|
* ProcessPriorityManager::CurrentProcessIsForeground(), which can be called in
|
|
|
|
* any process, is handled separately, by the ProcessPriorityManagerChild
|
|
|
|
* class.
|
2012-08-05 05:09:39 +00:00
|
|
|
*/
|
2013-04-26 00:53:26 +00:00
|
|
|
class ProcessPriorityManagerImpl MOZ_FINAL
|
2012-08-05 05:09:39 +00:00
|
|
|
: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2013-01-03 23:39:25 +00:00
|
|
|
|
2013-01-26 21:14:01 +00:00
|
|
|
/**
|
2013-04-26 00:53:26 +00:00
|
|
|
* This function implements ProcessPriorityManager::SetProcessPriority.
|
2013-01-26 21:14:01 +00:00
|
|
|
*/
|
2013-04-26 00:53:26 +00:00
|
|
|
void SetProcessPriority(ContentParent* aContentParent,
|
2013-10-01 03:54:59 +00:00
|
|
|
ProcessPriority aPriority,
|
|
|
|
uint32_t aBackgroundLRU = 0);
|
2013-01-26 21:14:01 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-26 00:53:26 +00: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-26 21:14:01 +00:00
|
|
|
*/
|
2013-04-26 00:53:26 +00:00
|
|
|
void FireTestOnlyObserverNotification(const char* aTopic,
|
|
|
|
const nsACString& aData = EmptyCString());
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
/**
|
|
|
|
* Does some process, other than the one handled by aParticularManager, have
|
|
|
|
* priority FOREGROUND_HIGH?
|
|
|
|
*/
|
|
|
|
bool OtherProcessHasHighPriority(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This must be called by a ParticularProcessPriorityManager when it changes
|
|
|
|
* its priority.
|
|
|
|
*/
|
|
|
|
void NotifyProcessPriorityChanged(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager,
|
|
|
|
hal::ProcessPriority aOldPriority);
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
private:
|
|
|
|
static bool sPrefListenersRegistered;
|
|
|
|
static bool sInitialized;
|
|
|
|
static StaticRefPtr<ProcessPriorityManagerImpl> sSingleton;
|
|
|
|
|
|
|
|
static int PrefChangedCallback(const char* aPref, void* aClosure);
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl();
|
|
|
|
~ProcessPriorityManagerImpl() {}
|
|
|
|
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-09 20:27:06 +00:00
|
|
|
|
|
|
|
nsTHashtable<nsUint64HashKey> mHighPriorityChildIDs;
|
2013-04-26 00:53:26 +00: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();
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
private:
|
2013-04-26 00:53:26 +00:00
|
|
|
static StaticRefPtr<ProcessPriorityManagerChild> sSingleton;
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerChild();
|
|
|
|
~ProcessPriorityManagerChild() {}
|
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(ProcessPriorityManagerChild);
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00: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-09 20:27:06 +00:00
|
|
|
, public nsSupportsWeakReference
|
2013-04-26 00:53:26 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ParticularProcessPriorityManager(ContentParent* aContentParent);
|
|
|
|
~ParticularProcessPriorityManager();
|
|
|
|
|
|
|
|
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 03:54:59 +00:00
|
|
|
bool IsPreallocated() const;
|
2013-01-26 21:14:01 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-26 00:53:26 +00: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-26 21:14:01 +00:00
|
|
|
*/
|
2013-04-26 00:53:26 +00:00
|
|
|
const nsAutoCString& NameWithComma();
|
|
|
|
|
|
|
|
bool HasAppType(const char* aAppType);
|
2013-04-26 00:53:26 +00:00
|
|
|
bool IsExpectingSystemMessage();
|
2013-04-26 00:53:26 +00:00
|
|
|
|
|
|
|
void OnAudioChannelProcessChanged(nsISupports* aSubject);
|
|
|
|
void OnRemoteBrowserFrameShown(nsISupports* aSubject);
|
|
|
|
void OnTabParentDestroyed(nsISupports* aSubject);
|
|
|
|
void OnFrameloaderVisibleChanged(nsISupports* aSubject);
|
|
|
|
void OnChannelConnected(nsISupports* aSubject);
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessPriority CurrentPriority();
|
2013-02-27 19:28:57 +00:00
|
|
|
ProcessPriority ComputePriority();
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessCPUPriority ComputeCPUPriority();
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void ScheduleResetPriority(const char* aTimeoutPref);
|
|
|
|
void ResetPriority();
|
|
|
|
void ResetPriorityNow();
|
2013-05-09 20:27:06 +00:00
|
|
|
void ResetCPUPriorityNow();
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
/**
|
|
|
|
* This overload is equivalent to SetPriorityNow(aPriority,
|
|
|
|
* ComputeCPUPriority()).
|
|
|
|
*/
|
2013-10-01 03:54:59 +00:00
|
|
|
void SetPriorityNow(ProcessPriority aPriority,
|
|
|
|
uint32_t aBackgroundLRU = 0);
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
void SetPriorityNow(ProcessPriority aPriority,
|
2013-10-01 03:54:59 +00:00
|
|
|
ProcessCPUPriority aCPUPriority,
|
|
|
|
uint32_t aBackgroundLRU = 0);
|
2013-05-09 20:27:06 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void ShutDown();
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
private:
|
|
|
|
void FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData = EmptyCString());
|
|
|
|
|
|
|
|
void FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const char* aData = nullptr);
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
ContentParent* mContentParent;
|
|
|
|
uint64_t mChildID;
|
|
|
|
ProcessPriority mPriority;
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessCPUPriority mCPUPriority;
|
2013-04-26 00:53:26 +00:00
|
|
|
bool mHoldsCPUWakeLock;
|
2013-02-14 20:41:30 +00:00
|
|
|
bool mHoldsHighPriorityWakeLock;
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/**
|
|
|
|
* Used to implement NameWithComma().
|
|
|
|
*/
|
|
|
|
nsAutoCString mNameWithComma;
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
nsCOMPtr<nsITimer> mResetPriorityTimer;
|
|
|
|
};
|
2013-02-23 04:24:28 +00:00
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
class BackgroundProcessLRUPool MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static BackgroundProcessLRUPool* Singleton();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to remove a ContentParent from background LRU pool when
|
|
|
|
* it is destroyed or its priority changed from BACKGROUND to others.
|
|
|
|
*/
|
|
|
|
void RemoveFromBackgroundLRUPool(ContentParent* aContentParent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to add a ContentParent into background LRU pool when
|
|
|
|
* its priority changed to BACKGROUND from others.
|
|
|
|
*/
|
|
|
|
void AddIntoBackgroundLRUPool(ContentParent* aContentParent);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static StaticAutoPtr<BackgroundProcessLRUPool> sSingleton;
|
|
|
|
|
|
|
|
int32_t mLRUPoolLevels;
|
|
|
|
int32_t mLRUPoolSize;
|
|
|
|
int32_t mLRUPoolAvailableIndex;
|
|
|
|
nsTArray<ContentParent*> mLRUPool;
|
|
|
|
|
|
|
|
uint32_t CalculateLRULevel(uint32_t aBackgroundLRUPoolIndex);
|
|
|
|
|
|
|
|
nsresult UpdateAvailableIndexInLRUPool(
|
|
|
|
ContentParent* aContentParent,
|
|
|
|
int32_t aTargetIndex = -1);
|
|
|
|
|
|
|
|
void ShiftLRUPool();
|
|
|
|
|
|
|
|
void EnsureLRUPool();
|
|
|
|
|
|
|
|
BackgroundProcessLRUPool();
|
|
|
|
DISALLOW_EVIL_CONSTRUCTORS(BackgroundProcessLRUPool);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sInitialized = false;
|
|
|
|
/* static */ bool ProcessPriorityManagerImpl::sPrefListenersRegistered = false;
|
|
|
|
/* static */ StaticRefPtr<ProcessPriorityManagerImpl>
|
|
|
|
ProcessPriorityManagerImpl::sSingleton;
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(ProcessPriorityManagerImpl,
|
|
|
|
nsIObserver);
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ int
|
|
|
|
ProcessPriorityManagerImpl::PrefChangedCallback(const char* aPref,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
StaticInit();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManagerImpl::PrefsEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("dom.ipc.processPriorityManager.enabled") &&
|
|
|
|
!Preferences::GetBool("dom.ipc.tabs.disabled");
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManagerImpl::StaticInit()
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ ProcessPriorityManagerImpl*
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton()
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
if (!sSingleton) {
|
|
|
|
StaticInit();
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
return sSingleton;
|
|
|
|
}
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerImpl::ProcessPriorityManagerImpl()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
|
|
|
}
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::Init()
|
|
|
|
{
|
|
|
|
LOG("Starting up. This is the master process.");
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00: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.
|
2013-05-09 20:27:06 +00:00
|
|
|
hal::SetProcessPriority(getpid(), PROCESS_PRIORITY_MASTER,
|
|
|
|
PROCESS_CPU_PRIORITY_NORMAL);
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerImpl::Observe(
|
2012-08-05 05:09:39 +00:00
|
|
|
nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
nsDependentCString topic(aTopic);
|
|
|
|
if (topic.EqualsLiteral("ipc:content-created")) {
|
|
|
|
ObserveContentParentCreated(aSubject);
|
|
|
|
} else if (topic.EqualsLiteral("ipc:content-shutdown")) {
|
|
|
|
ObserveContentParentDestroyed(aSubject);
|
2012-08-05 05:09:39 +00:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2013-04-26 00:53:26 +00:00
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
already_AddRefed<ParticularProcessPriorityManager>
|
|
|
|
ProcessPriorityManagerImpl::GetParticularProcessPriorityManager(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm;
|
|
|
|
mParticularManagers.Get(aContentParent->ChildID(), &pppm);
|
|
|
|
if (!pppm) {
|
|
|
|
pppm = new ParticularProcessPriorityManager(aContentParent);
|
|
|
|
pppm->Init();
|
|
|
|
mParticularManagers.Put(aContentParent->ChildID(), pppm);
|
|
|
|
|
|
|
|
FireTestOnlyObserverNotification("process-created",
|
|
|
|
nsPrintfCString("%lld", aContentParent->ChildID()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return pppm.forget();
|
|
|
|
}
|
|
|
|
|
2013-02-14 20:41:30 +00:00
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerImpl::SetProcessPriority(ContentParent* aContentParent,
|
2013-10-01 03:54:59 +00:00
|
|
|
ProcessPriority aPriority,
|
|
|
|
uint32_t aBackgroundLRU)
|
2013-04-26 00:53:26 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm =
|
|
|
|
GetParticularProcessPriorityManager(aContentParent);
|
2013-10-01 03:54:59 +00:00
|
|
|
pppm->SetPriorityNow(aPriority, aBackgroundLRU);
|
2013-04-26 00:53:26 +00: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.
|
|
|
|
nsCOMPtr<nsIObserver> cp = do_QueryInterface(aContentParent);
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> pppm =
|
|
|
|
GetParticularProcessPriorityManager(static_cast<ContentParent*>(cp.get()));
|
|
|
|
}
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
EnumerateParticularProcessPriorityManagers(
|
|
|
|
const uint64_t& aKey,
|
|
|
|
nsRefPtr<ParticularProcessPriorityManager> aValue,
|
|
|
|
void* aUserData)
|
|
|
|
{
|
|
|
|
nsTArray<nsRefPtr<ParticularProcessPriorityManager> >* aArray =
|
|
|
|
static_cast<nsTArray<nsRefPtr<ParticularProcessPriorityManager> >*>(aUserData);
|
|
|
|
aArray->AppendElement(aValue);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00: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);
|
|
|
|
MOZ_ASSERT(pppm);
|
|
|
|
if (pppm) {
|
|
|
|
pppm->ShutDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
mParticularManagers.Remove(childID);
|
2013-05-09 20:27:06 +00:00
|
|
|
|
|
|
|
if (mHighPriorityChildIDs.Contains(childID)) {
|
|
|
|
mHighPriorityChildIDs.RemoveEntry(childID);
|
|
|
|
|
|
|
|
// We just lost a high-priority process; reset everyone's CPU priorities.
|
|
|
|
nsTArray<nsRefPtr<ParticularProcessPriorityManager> > pppms;
|
|
|
|
mParticularManagers.EnumerateRead(
|
|
|
|
&EnumerateParticularProcessPriorityManagers,
|
|
|
|
&pppms);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pppms.Length(); i++) {
|
|
|
|
pppms[i]->ResetCPUPriorityNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProcessPriorityManagerImpl::OtherProcessHasHighPriority(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager)
|
|
|
|
{
|
|
|
|
if (mHighPriorityChildIDs.Contains(aParticularManager->ChildID())) {
|
|
|
|
return mHighPriorityChildIDs.Count() > 1;
|
|
|
|
}
|
|
|
|
return mHighPriorityChildIDs.Count() > 0;
|
2013-04-26 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::NotifyProcessPriorityChanged(
|
|
|
|
ParticularProcessPriorityManager* aParticularManager,
|
|
|
|
ProcessPriority aOldPriority)
|
|
|
|
{
|
|
|
|
// This priority change can only affect other processes' priorities if we're
|
|
|
|
// changing to/from FOREGROUND_HIGH.
|
|
|
|
|
|
|
|
if (aOldPriority < PROCESS_PRIORITY_FOREGROUND_HIGH &&
|
|
|
|
aParticularManager->CurrentPriority() <
|
|
|
|
PROCESS_PRIORITY_FOREGROUND_HIGH) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aParticularManager->CurrentPriority() >=
|
|
|
|
PROCESS_PRIORITY_FOREGROUND_HIGH) {
|
|
|
|
mHighPriorityChildIDs.PutEntry(aParticularManager->ChildID());
|
|
|
|
} else {
|
|
|
|
mHighPriorityChildIDs.RemoveEntry(aParticularManager->ChildID());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<ParticularProcessPriorityManager> > pppms;
|
|
|
|
mParticularManagers.EnumerateRead(
|
|
|
|
&EnumerateParticularProcessPriorityManagers,
|
|
|
|
&pppms);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pppms.Length(); i++) {
|
|
|
|
if (pppms[i] != aParticularManager) {
|
|
|
|
pppms[i]->ResetCPUPriorityNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS3(ParticularProcessPriorityManager,
|
2013-04-26 00:53:26 +00:00
|
|
|
nsIObserver,
|
2013-05-09 20:27:06 +00:00
|
|
|
nsITimerCallback,
|
|
|
|
nsISupportsWeakReference);
|
2013-04-26 00:53:26 +00:00
|
|
|
|
|
|
|
ParticularProcessPriorityManager::ParticularProcessPriorityManager(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
: mContentParent(aContentParent)
|
|
|
|
, mChildID(aContentParent->ChildID())
|
|
|
|
, mPriority(PROCESS_PRIORITY_UNKNOWN)
|
2013-05-09 20:27:06 +00:00
|
|
|
, mCPUPriority(PROCESS_CPU_PRIORITY_NORMAL)
|
2013-04-26 00:53:26 +00: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-09 20:27:06 +00:00
|
|
|
os->AddObserver(this, "audio-channel-process-changed", /* ownsWeak */ true);
|
|
|
|
os->AddObserver(this, "remote-browser-frame-shown", /* ownsWeak */ true);
|
|
|
|
os->AddObserver(this, "ipc:browser-destroyed", /* ownsWeak */ true);
|
|
|
|
os->AddObserver(this, "frameloader-visible-changed", /* ownsWeak */ true);
|
2013-04-26 00:53:26 +00: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-07 20:44:25 +00: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 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
ParticularProcessPriorityManager::Notify(const WakeLockInformation& aInfo)
|
2013-02-14 20:41:30 +00:00
|
|
|
{
|
2013-05-07 20:44:25 +00:00
|
|
|
if (!mContentParent) {
|
|
|
|
// We've been shut down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-14 20:41:30 +00:00
|
|
|
bool* dest = nullptr;
|
2013-04-26 00:53:26 +00:00
|
|
|
if (aInfo.topic().EqualsLiteral("cpu")) {
|
2013-02-14 20:41:30 +00:00
|
|
|
dest = &mHoldsCPUWakeLock;
|
2013-04-26 00:53:26 +00:00
|
|
|
} else if (aInfo.topic().EqualsLiteral("high-priority")) {
|
2013-02-14 20:41:30 +00:00
|
|
|
dest = &mHoldsHighPriorityWakeLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest) {
|
2013-04-26 00:53:26 +00:00
|
|
|
bool thisProcessLocks = aInfo.lockingProcesses().Contains(ChildID());
|
2013-02-14 20:41:30 +00:00
|
|
|
if (thisProcessLocks != *dest) {
|
|
|
|
*dest = thisProcessLocks;
|
2013-04-26 00:53:26 +00:00
|
|
|
LOGP("Got wake lock changed event. "
|
|
|
|
"Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d",
|
|
|
|
mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock);
|
2013-02-14 20:41:30 +00:00
|
|
|
ResetPriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
NS_IMETHODIMP
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
if (!mContentParent) {
|
|
|
|
// We've been shut down.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCString topic(aTopic);
|
|
|
|
|
|
|
|
if (topic.EqualsLiteral("audio-channel-process-changed")) {
|
|
|
|
OnAudioChannelProcessChanged(aSubject);
|
|
|
|
} else if (topic.EqualsLiteral("remote-browser-frame-shown")) {
|
|
|
|
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 05:09:39 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00: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 03:54:59 +00:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::IsPreallocated() const
|
|
|
|
{
|
|
|
|
return mContentParent ? mContentParent->IsPreallocated() : false;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
void
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
}
|
2013-04-26 00:53:26 +00:00
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnRemoteBrowserFrameShown(nsISupports* aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFrameLoader> fl = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(fl);
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
nsCOMPtr<nsITabParent> tp;
|
|
|
|
fl->GetTabParent(getter_AddRefs(tp));
|
|
|
|
NS_ENSURE_TRUE_VOID(tp);
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
if (static_cast<TabParent*>(tp.get())->Manager() != mContentParent) {
|
2012-08-05 05:09:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
ResetPriority();
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnTabParentDestroyed(nsISupports* aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsITabParent> tp = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(tp);
|
|
|
|
|
|
|
|
if (static_cast<TabParent*>(tp.get())->Manager() != mContentParent) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-01-26 21:14:01 +00:00
|
|
|
ResetPriority();
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::OnFrameloaderVisibleChanged(nsISupports* aSubject)
|
2013-02-14 20:41:30 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
nsCOMPtr<nsIFrameLoader> fl = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE_VOID(fl);
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
nsCOMPtr<nsITabParent> tp;
|
|
|
|
fl->GetTabParent(getter_AddRefs(tp));
|
|
|
|
if (!tp) {
|
|
|
|
return;
|
2013-02-14 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
if (static_cast<TabParent*>(tp.get())->Manager() != mContentParent) {
|
|
|
|
return;
|
2013-02-14 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00: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-14 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::ResetPriority()
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-02-27 19:28:57 +00:00
|
|
|
ProcessPriority processPriority = ComputePriority();
|
2013-04-26 00:53:26 +00:00
|
|
|
if (mPriority == PROCESS_PRIORITY_UNKNOWN ||
|
|
|
|
mPriority > processPriority) {
|
2013-01-26 21:14:01 +00:00
|
|
|
ScheduleResetPriority("backgroundGracePeriodMS");
|
2013-02-27 19:28:57 +00:00
|
|
|
return;
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
2013-02-27 19:28:57 +00:00
|
|
|
|
|
|
|
SetPriorityNow(processPriority);
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::ResetPriorityNow()
|
2013-01-26 21:14:01 +00:00
|
|
|
{
|
2013-02-27 19:28:57 +00:00
|
|
|
SetPriorityNow(ComputePriority());
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::ScheduleResetPriority(const char* aTimeoutPref)
|
|
|
|
{
|
|
|
|
if (mResetPriorityTimer) {
|
|
|
|
LOGP("ScheduleResetPriority bailing; the timer is already running.");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00: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 05:09:39 +00:00
|
|
|
|
2013-02-14 20:41:29 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ParticularProcessPriorityManager::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
LOGP("Reset priority timer callback; about to ResetPriorityNow.");
|
|
|
|
ResetPriorityNow();
|
|
|
|
mResetPriorityTimer = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-14 20:41:30 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::HasAppType(const char* aAppType)
|
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
mContentParent->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); i++) {
|
|
|
|
nsAutoString appType;
|
|
|
|
static_cast<TabParent*>(browsers[i])->GetAppType(appType);
|
|
|
|
if (appType.EqualsASCII(aAppType)) {
|
|
|
|
return true;
|
2013-02-14 20:41:29 +00:00
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
return false;
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
bool
|
|
|
|
ParticularProcessPriorityManager::IsExpectingSystemMessage()
|
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
mContentParent->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); i++) {
|
|
|
|
TabParent* tp = static_cast<TabParent*>(browsers[i]);
|
2013-07-23 23:39:17 +00:00
|
|
|
nsCOMPtr<nsIMozBrowserFrame> bf = do_QueryInterface(tp->GetOwnerElement());
|
2013-04-26 00:53:26 +00:00
|
|
|
if (!bf) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bf->GetIsExpectingSystemMessage()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessPriority
|
|
|
|
ParticularProcessPriorityManager::CurrentPriority()
|
|
|
|
{
|
|
|
|
return mPriority;
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:28:57 +00:00
|
|
|
ProcessPriority
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::ComputePriority()
|
2013-02-23 04:24:28 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
if ((mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) &&
|
|
|
|
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++) {
|
|
|
|
if (static_cast<TabParent*>(browsers[i])->IsVisible()) {
|
|
|
|
isVisible = true;
|
|
|
|
break;
|
2013-02-27 19:28:57 +00:00
|
|
|
}
|
2013-04-26 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVisible) {
|
2013-09-24 08:10:20 +00:00
|
|
|
return HasAppType("keyboard") ?
|
|
|
|
PROCESS_PRIORITY_FOREGROUND_KEYBOARD :
|
|
|
|
PROCESS_PRIORITY_FOREGROUND;
|
2013-02-23 04:24:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
if ((mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) &&
|
|
|
|
IsExpectingSystemMessage()) {
|
|
|
|
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:28:57 +00:00
|
|
|
AudioChannelService* service = AudioChannelService::GetAudioChannelService();
|
2013-04-26 00:53:26 +00:00
|
|
|
if (service->ProcessContentOrNormalChannelIsActive(ChildID())) {
|
2013-02-27 19:28:57 +00:00
|
|
|
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
return HasAppType("homescreen") ?
|
2013-02-27 19:28:57 +00:00
|
|
|
PROCESS_PRIORITY_BACKGROUND_HOMESCREEN :
|
|
|
|
PROCESS_PRIORITY_BACKGROUND;
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessCPUPriority
|
|
|
|
ParticularProcessPriorityManager::ComputeCPUPriority()
|
|
|
|
{
|
|
|
|
if (mPriority >= PROCESS_PRIORITY_FOREGROUND_HIGH) {
|
|
|
|
return PROCESS_CPU_PRIORITY_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ProcessPriorityManagerImpl::GetSingleton()->
|
|
|
|
OtherProcessHasHighPriority(this) ?
|
|
|
|
PROCESS_CPU_PRIORITY_LOW :
|
|
|
|
PROCESS_CPU_PRIORITY_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::ResetCPUPriorityNow()
|
|
|
|
{
|
|
|
|
SetPriorityNow(mPriority);
|
|
|
|
}
|
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
void
|
2013-10-01 03:54:59 +00:00
|
|
|
ParticularProcessPriorityManager::SetPriorityNow(ProcessPriority aPriority,
|
|
|
|
uint32_t aBackgroundLRU)
|
2013-05-09 20:27:06 +00:00
|
|
|
{
|
2013-10-01 03:54:59 +00:00
|
|
|
SetPriorityNow(aPriority, ComputeCPUPriority(), aBackgroundLRU);
|
2013-05-09 20:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ParticularProcessPriorityManager::SetPriorityNow(ProcessPriority aPriority,
|
2013-10-01 03:54:59 +00:00
|
|
|
ProcessCPUPriority aCPUPriority,
|
|
|
|
uint32_t aBackgroundLRU)
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-02-27 19:28:57 +00:00
|
|
|
if (aPriority == PROCESS_PRIORITY_UNKNOWN) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
if (aBackgroundLRU > 0 &&
|
|
|
|
aPriority == PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
mPriority == PROCESS_PRIORITY_BACKGROUND) {
|
|
|
|
hal::SetProcessPriority(Pid(), mPriority, mCPUPriority, aBackgroundLRU);
|
|
|
|
|
|
|
|
nsPrintfCString ProcessPriorityWithBackgroundLRU("%s:%d",
|
|
|
|
ProcessPriorityToString(mPriority, mCPUPriority),
|
|
|
|
aBackgroundLRU);
|
|
|
|
|
|
|
|
FireTestOnlyObserverNotification("process-priority-with-background-LRU-set",
|
|
|
|
ProcessPriorityWithBackgroundLRU.get());
|
|
|
|
}
|
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
if (!mContentParent ||
|
|
|
|
!ProcessPriorityManagerImpl::PrefsEnabled() ||
|
|
|
|
(mPriority == aPriority && mCPUPriority == aCPUPriority)) {
|
2013-04-26 00:53:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the prefs were disabled after this ParticularProcessPriorityManager was
|
|
|
|
// created, we can at least avoid any further calls to
|
|
|
|
// hal::SetProcessPriority. Supporting dynamic enabling/disabling of the
|
|
|
|
// ProcessPriorityManager is mostly for testing.
|
|
|
|
if (!ProcessPriorityManagerImpl::PrefsEnabled()) {
|
2013-01-26 21:14:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
if (aPriority == PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
mPriority != PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
!IsPreallocated()) {
|
|
|
|
ProcessPriorityManager::AddIntoBackgroundLRUPool(mContentParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPriority != PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
mPriority == PROCESS_PRIORITY_BACKGROUND &&
|
|
|
|
!IsPreallocated()) {
|
|
|
|
ProcessPriorityManager::RemoveFromBackgroundLRUPool(mContentParent);
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
LOGP("Changing priority from %s to %s.",
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessPriorityToString(mPriority, mCPUPriority),
|
|
|
|
ProcessPriorityToString(aPriority, aCPUPriority));
|
|
|
|
|
|
|
|
ProcessPriority oldPriority = mPriority;
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
mPriority = aPriority;
|
2013-05-09 20:27:06 +00:00
|
|
|
mCPUPriority = aCPUPriority;
|
|
|
|
hal::SetProcessPriority(Pid(), mPriority, mCPUPriority);
|
2013-04-26 00:53:26 +00:00
|
|
|
|
2013-05-09 20:27:06 +00:00
|
|
|
if (oldPriority != mPriority) {
|
|
|
|
unused << mContentParent->SendNotifyProcessPriorityChanged(mPriority);
|
|
|
|
}
|
2012-10-22 19:40:19 +00:00
|
|
|
|
2013-02-27 19:28:57 +00:00
|
|
|
if (aPriority >= PROCESS_PRIORITY_FOREGROUND) {
|
2013-04-26 00:53:26 +00:00
|
|
|
unused << mContentParent->SendCancelMinimizeMemoryUsage();
|
2013-02-27 19:28:57 +00:00
|
|
|
} else {
|
2013-04-26 00:53:26 +00:00
|
|
|
unused << mContentParent->SendMinimizeMemoryUsage();
|
2012-10-22 19:40:19 +00:00
|
|
|
}
|
2013-04-26 00:53:26 +00:00
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
nsPrintfCString ProcessPriorityWithBackgroundLRU("%s:%d",
|
|
|
|
ProcessPriorityToString(mPriority, mCPUPriority),
|
|
|
|
aBackgroundLRU);
|
|
|
|
|
|
|
|
FireTestOnlyObserverNotification("process-priority-with-background-LRU-set",
|
|
|
|
ProcessPriorityWithBackgroundLRU.get());
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
FireTestOnlyObserverNotification("process-priority-set",
|
2013-05-09 20:27:06 +00:00
|
|
|
ProcessPriorityToString(mPriority, mCPUPriority));
|
|
|
|
|
|
|
|
if (oldPriority != mPriority) {
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton()->
|
|
|
|
NotifyProcessPriorityChanged(this, oldPriority);
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 21:14:01 +00:00
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::ShutDown()
|
2013-01-26 21:14:01 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
MOZ_ASSERT(mContentParent);
|
|
|
|
|
2013-05-07 20:44:25 +00:00
|
|
|
UnregisterWakeLockObserver(this);
|
|
|
|
|
2013-01-26 21:14:01 +00:00
|
|
|
if (mResetPriorityTimer) {
|
2013-04-26 00:53:26 +00:00
|
|
|
mResetPriorityTimer->Cancel();
|
|
|
|
mResetPriorityTimer = nullptr;
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
ProcessPriorityManager::RemoveFromBackgroundLRUPool(mContentParent);
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
mContentParent = nullptr;
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
void
|
|
|
|
ProcessPriorityManagerImpl::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData /* = EmptyCString() */)
|
2013-01-26 21:14:01 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00: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-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const char* aData /* = nullptr */ )
|
2013-01-26 21:14:01 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
if (!Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 21:14:01 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
nsAutoCString data;
|
|
|
|
if (aData) {
|
|
|
|
data.AppendASCII(aData);
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
FireTestOnlyObserverNotification(aTopic, data);
|
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
|
|
|
void
|
2013-04-26 00:53:26 +00:00
|
|
|
ParticularProcessPriorityManager::FireTestOnlyObserverNotification(
|
|
|
|
const char* aTopic,
|
|
|
|
const nsACString& aData /* = EmptyCString() */)
|
2012-08-05 05:09:39 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
if (!Preferences::GetBool("dom.ipc.processPriorityManager.testMode")) {
|
2012-08-05 05:09:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
nsAutoCString data(nsPrintfCString("%lld", ChildID()));
|
|
|
|
if (!aData.IsEmpty()) {
|
|
|
|
data.AppendLiteral(":");
|
|
|
|
data.Append(aData);
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00: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 09:00:56 +00:00
|
|
|
sSingleton->Init();
|
2013-04-26 00:53:26 +00:00
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ ProcessPriorityManagerChild*
|
|
|
|
ProcessPriorityManagerChild::Singleton()
|
|
|
|
{
|
|
|
|
StaticInit();
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(ProcessPriorityManagerChild,
|
|
|
|
nsIObserver)
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerChild::ProcessPriorityManagerChild()
|
|
|
|
{
|
2012-08-05 05:09:39 +00:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
2013-04-26 00:53:26 +00:00
|
|
|
mCachedPriority = PROCESS_PRIORITY_MASTER;
|
|
|
|
} else {
|
|
|
|
mCachedPriority = PROCESS_PRIORITY_UNKNOWN;
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
2013-04-26 00:53:26 +00:00
|
|
|
}
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2013-04-26 00:53:26 +00: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,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
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-03 23:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-26 00:53:26 +00:00
|
|
|
ProcessPriorityManagerChild::CurrentProcessIsForeground()
|
2013-01-03 23:39:25 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
return mCachedPriority == PROCESS_PRIORITY_UNKNOWN ||
|
|
|
|
mCachedPriority >= PROCESS_PRIORITY_FOREGROUND;
|
|
|
|
}
|
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
/* static */ StaticAutoPtr<BackgroundProcessLRUPool>
|
|
|
|
BackgroundProcessLRUPool::sSingleton;
|
|
|
|
|
|
|
|
/* static */ BackgroundProcessLRUPool*
|
|
|
|
BackgroundProcessLRUPool::Singleton()
|
|
|
|
{
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new BackgroundProcessLRUPool();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundProcessLRUPool::BackgroundProcessLRUPool()
|
|
|
|
{
|
|
|
|
EnsureLRUPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
BackgroundProcessLRUPool::CalculateLRULevel(uint32_t aBackgroundLRUPoolIndex)
|
|
|
|
{
|
|
|
|
// Set LRU level of each background process and maintain LRU buffer as below:
|
|
|
|
|
|
|
|
// Priority background : LRU0
|
|
|
|
// Priority background+1: LRU1, LRU2
|
|
|
|
// Priority background+2: LRU3, LRU4, LRU5, LRU6
|
|
|
|
// Priority background+3: LRU7, LRU8, LRU9, LRU10, LRU11, LRU12, LRU13, LRU14
|
|
|
|
// ...
|
|
|
|
// Priority background+L-1: 2^(number of background LRU pool levels - 1)
|
|
|
|
// (End of buffer)
|
|
|
|
|
|
|
|
return (uint32_t)(log((float)aBackgroundLRUPoolIndex) / log(2.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackgroundProcessLRUPool::EnsureLRUPool()
|
|
|
|
{
|
|
|
|
// We set mBackgroundLRUPoolLevels according to our pref.
|
|
|
|
// This value is used to set background process LRU pool
|
|
|
|
if (!NS_SUCCEEDED(Preferences::GetInt(
|
|
|
|
"dom.ipc.processPriorityManager.backgroundLRUPoolLevels",
|
|
|
|
&mLRUPoolLevels))) {
|
|
|
|
mLRUPoolLevels = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLRUPoolLevels <= 0) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// See bug 822325 comment 49
|
|
|
|
MOZ_ASSERT(mLRUPoolLevels <= 6);
|
|
|
|
|
|
|
|
// LRU pool size = 2 ^ (number of background LRU pool levels) - 1
|
|
|
|
mLRUPoolSize = (1 << mLRUPoolLevels) - 1;
|
|
|
|
|
|
|
|
mLRUPoolAvailableIndex = 0;
|
|
|
|
|
|
|
|
LOG("Making background LRU pool with size(%d)", mLRUPoolSize);
|
|
|
|
|
|
|
|
mLRUPool.InsertElementsAt(0, mLRUPoolSize, (ContentParent*)nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackgroundProcessLRUPool::RemoveFromBackgroundLRUPool(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
for (int32_t i = 0; i < mLRUPoolSize; i++) {
|
|
|
|
if (mLRUPool[i]) {
|
|
|
|
if (mLRUPool[i]->ChildID() == aContentParent->ChildID()) {
|
|
|
|
|
|
|
|
mLRUPool[i] = nullptr;
|
|
|
|
LOG("Remove ChildID(%llu) from LRU pool", aContentParent->ChildID());
|
|
|
|
|
|
|
|
// After we remove this ContentParent from LRU pool, we still need to
|
|
|
|
// update the available index if the index of removed one is less than
|
|
|
|
// the available index we already have.
|
|
|
|
UpdateAvailableIndexInLRUPool(aContentParent, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BackgroundProcessLRUPool::UpdateAvailableIndexInLRUPool(
|
|
|
|
ContentParent* aContentParent,
|
|
|
|
int32_t aTargetIndex)
|
|
|
|
{
|
|
|
|
// If we specify which index we want to assign to mLRUPoolAvailableIndex,
|
|
|
|
// We have to make sure the index in LRUPool doesn't point to any
|
|
|
|
// ContentParent.
|
|
|
|
if (aTargetIndex >= 0 && aTargetIndex < mLRUPoolSize &&
|
|
|
|
aTargetIndex < mLRUPoolAvailableIndex &&
|
|
|
|
!mLRUPool[aTargetIndex]) {
|
|
|
|
mLRUPoolAvailableIndex = aTargetIndex;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we didn't specify any legal aTargetIndex, then we just check
|
|
|
|
// whether current mLRUPoolAvailableIndex points to any ContentParent or not.
|
|
|
|
if (mLRUPoolAvailableIndex >= 0 && mLRUPoolAvailableIndex < mLRUPoolSize &&
|
|
|
|
!(mLRUPool[mLRUPoolAvailableIndex])) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both above way failed. So now we have to find proper value
|
|
|
|
// for mLRUPoolAvailableIndex.
|
|
|
|
// We are looking for an available index. We only shift process with
|
|
|
|
// LRU less than the available index should have, so we stop update
|
|
|
|
// mLRUPoolAvailableIndex from the for loop once we got a candidate.
|
|
|
|
mLRUPoolAvailableIndex = -1;
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < mLRUPoolSize; i++) {
|
|
|
|
if (mLRUPool[i]) {
|
|
|
|
if (mLRUPool[i]->ChildID() == aContentParent->ChildID()) {
|
|
|
|
LOG("ChildID(%llu) already in LRU pool", aContentParent->ChildID());
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (mLRUPoolAvailableIndex == -1) {
|
|
|
|
mLRUPoolAvailableIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the LRUPool is already full, mLRUPoolAvailableIndex is still -1 after
|
|
|
|
// above loop finished. We should set mLRUPoolAvailableIndex
|
|
|
|
// to mLRUPoolSize - 1 in this case. Here uses the mod operator to do it:
|
|
|
|
// New mLRUPoolAvailableIndex either equals old mLRUPoolAvailableIndex, or
|
|
|
|
// mLRUPoolSize - 1 if old mLRUPoolAvailableIndex is -1.
|
|
|
|
mLRUPoolAvailableIndex =
|
|
|
|
(mLRUPoolAvailableIndex + mLRUPoolSize) % mLRUPoolSize;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackgroundProcessLRUPool::ShiftLRUPool()
|
|
|
|
{
|
|
|
|
for (int32_t i = mLRUPoolAvailableIndex; i > 0; i--) {
|
|
|
|
mLRUPool[i] = mLRUPool[i - 1];
|
|
|
|
// Check whether i+1 is power of Two.
|
|
|
|
// If so, then it crossed a LRU group boundary and
|
|
|
|
// we need to assign its new process priority LRU.
|
|
|
|
if (!((i + 1) & i)) {
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton()->SetProcessPriority(
|
|
|
|
mLRUPool[i], PROCESS_PRIORITY_BACKGROUND, CalculateLRULevel(i + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackgroundProcessLRUPool::AddIntoBackgroundLRUPool(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
// We have to make sure that we have correct available index in LRU pool
|
|
|
|
if (!NS_SUCCEEDED(
|
|
|
|
UpdateAvailableIndexInLRUPool(aContentParent))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift the list in the pool, so we have room at index 0 for the newly added
|
|
|
|
// ContentParent
|
|
|
|
ShiftLRUPool();
|
|
|
|
|
|
|
|
mLRUPool[0] = aContentParent;
|
|
|
|
|
|
|
|
LOG("Add ChildID(%llu) into LRU pool", aContentParent->ChildID());
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
2013-01-07 08:44:22 +00:00
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::Init()
|
|
|
|
{
|
|
|
|
ProcessPriorityManagerImpl::StaticInit();
|
|
|
|
ProcessPriorityManagerChild::StaticInit();
|
2012-08-05 05:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::SetProcessPriority(ContentParent* aContentParent,
|
|
|
|
ProcessPriority aPriority)
|
2013-01-26 21:14:01 +00:00
|
|
|
{
|
2013-04-26 00:53:26 +00:00
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
|
|
|
|
ProcessPriorityManagerImpl* singleton =
|
|
|
|
ProcessPriorityManagerImpl::GetSingleton();
|
|
|
|
if (singleton) {
|
|
|
|
singleton->SetProcessPriority(aContentParent, aPriority);
|
2013-01-26 21:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 03:54:59 +00:00
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::RemoveFromBackgroundLRUPool(
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
|
|
|
|
BackgroundProcessLRUPool* singleton =
|
|
|
|
BackgroundProcessLRUPool::Singleton();
|
|
|
|
if (singleton) {
|
|
|
|
singleton->RemoveFromBackgroundLRUPool(aContentParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
ProcessPriorityManager::AddIntoBackgroundLRUPool(ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
|
|
|
|
BackgroundProcessLRUPool* singleton =
|
|
|
|
BackgroundProcessLRUPool::Singleton();
|
|
|
|
if (singleton) {
|
|
|
|
singleton->AddIntoBackgroundLRUPool(aContentParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
/* static */ bool
|
|
|
|
ProcessPriorityManager::CurrentProcessIsForeground()
|
|
|
|
{
|
|
|
|
return ProcessPriorityManagerChild::Singleton()->
|
|
|
|
CurrentProcessIsForeground();
|
|
|
|
}
|
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
} // namespace mozilla
|