Bug 1535361 - Let stream transport service idle with 5 threads instead of only one to not create/kill threads in quick bursts; add option to thread pool to shorten the idle timeout progressively with number of idle thread to save memory, r=dragana

Differential Revision: https://phabricator.services.mozilla.com/D23845

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Honza Bambas 2019-03-19 14:35:12 +00:00
parent 44f895e1f6
commit 87dbb69b5b
4 changed files with 37 additions and 4 deletions

View File

@ -182,7 +182,8 @@ nsresult nsStreamTransportService::Init() {
// Configure the pool
mPool->SetName(NS_LITERAL_CSTRING("StreamTrans"));
mPool->SetThreadLimit(25);
mPool->SetIdleThreadLimit(1);
mPool->SetIdleThreadLimit(5);
mPool->SetIdleThreadTimeoutRegressive(true);
mPool->SetIdleThreadTimeout(PR_SecondsToInterval(30));
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();

View File

@ -68,6 +68,14 @@ interface nsIThreadPool : nsIEventTarget
*/
attribute unsigned long idleThreadTimeout;
/**
* If set to true the idle timeout will be calculated as idleThreadTimeout
* divideded by the number of idle threads at the moment. This may help
* save memory allocations but still keep reasonable amount of idle threads.
* Default is false, use |idleThreadTimeout| for all threads.
*/
attribute boolean idleThreadTimeoutRegressive;
/**
* Get/set the number of bytes reserved for the stack of all threads in
* the pool. By default this is nsIThreadManager::DEFAULT_STACK_SIZE.

View File

@ -51,7 +51,8 @@ nsThreadPool::nsThreadPool()
mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT),
mIdleCount(0),
mStackSize(nsIThreadManager::DEFAULT_STACK_SIZE),
mShutdown(false) {
mShutdown(false),
mRegressiveMaxIdleTime(false) {
LOG(("THRD-P(%p) constructor!!!\n", this));
}
@ -182,8 +183,10 @@ nsThreadPool::Run() {
event = mEvents.GetEvent(nullptr, lock);
if (!event) {
TimeStamp now = TimeStamp::Now();
TimeDuration timeout =
TimeDuration::FromMilliseconds(mIdleThreadTimeout);
uint32_t idleTimeoutDivider =
(mIdleCount && mRegressiveMaxIdleTime) ? mIdleCount : 1;
TimeDuration timeout = TimeDuration::FromMilliseconds(
static_cast<double>(mIdleThreadTimeout) / idleTimeoutDivider);
// If we are shutting down, then don't keep any idle threads
if (mShutdown) {
@ -511,6 +514,26 @@ nsThreadPool::SetIdleThreadTimeout(uint32_t aValue) {
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadTimeoutRegressive(bool* aValue) {
*aValue = mRegressiveMaxIdleTime;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadTimeoutRegressive(bool aValue) {
MutexAutoLock lock(mMutex);
bool oldRegressive = mRegressiveMaxIdleTime;
mRegressiveMaxIdleTime = aValue;
// Would setting regressive timeout effect idle threads?
if (mRegressiveMaxIdleTime > oldRegressive && mIdleCount > 1) {
mEventsAvailable
.NotifyAll(); // wake up threads so they observe this change
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetThreadStackSize(uint32_t* aValue) {
MutexAutoLock lock(mMutex);

View File

@ -46,6 +46,7 @@ class nsThreadPool final : public nsIThreadPool, public nsIRunnable {
uint32_t mStackSize;
nsCOMPtr<nsIThreadPoolListener> mListener;
bool mShutdown;
bool mRegressiveMaxIdleTime;
nsCString mName;
nsThreadPoolNaming mThreadNaming;
};