1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "MainThreadIdlePeriod.h" 8 9 #include "mozilla/Maybe.h" 10 #include "mozilla/Preferences.h" 11 #include "nsRefreshDriver.h" 12 #include "nsThreadUtils.h" 13 14 // The amount of idle time (milliseconds) reserved for a long idle period. 15 static const double kLongIdlePeriodMS = 50.0; 16 17 // The minimum amount of time (milliseconds) required for an idle period to be 18 // scheduled on the main thread. N.B. layout.idle_period.time_limit adds 19 // padding at the end of the idle period, which makes the point in time that we 20 // expect to become busy again be: 21 // now + kMinIdlePeriodMS + layout.idle_period.time_limit 22 static const double kMinIdlePeriodMS = 3.0; 23 24 static const uint32_t kMaxTimerThreadBound = 5; // milliseconds 25 static const uint32_t kMaxTimerThreadBoundClamp = 15; // milliseconds 26 27 namespace mozilla { 28 29 NS_IMETHODIMP GetIdlePeriodHint(TimeStamp * aIdleDeadline)30MainThreadIdlePeriod::GetIdlePeriodHint(TimeStamp* aIdleDeadline) { 31 MOZ_ASSERT(NS_IsMainThread()); 32 MOZ_ASSERT(aIdleDeadline); 33 34 TimeStamp now = TimeStamp::Now(); 35 TimeStamp currentGuess = 36 now + TimeDuration::FromMilliseconds(kLongIdlePeriodMS); 37 38 currentGuess = nsRefreshDriver::GetIdleDeadlineHint(currentGuess); 39 currentGuess = NS_GetTimerDeadlineHintOnCurrentThread(currentGuess, 40 kMaxTimerThreadBound); 41 42 // If the idle period is too small, then just return a null time 43 // to indicate we are busy. Otherwise return the actual deadline. 44 TimeDuration minIdlePeriod = TimeDuration::FromMilliseconds(kMinIdlePeriodMS); 45 bool busySoon = currentGuess.IsNull() || 46 (now >= (currentGuess - minIdlePeriod)) || 47 currentGuess < mLastIdleDeadline; 48 49 if (!busySoon) { 50 *aIdleDeadline = mLastIdleDeadline = currentGuess; 51 } 52 53 return NS_OK; 54 } 55 GetLongIdlePeriod()56/* static */ float MainThreadIdlePeriod::GetLongIdlePeriod() { 57 return kLongIdlePeriodMS; 58 } 59 60 } // namespace mozilla 61