2013-03-07 02:29:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
|
|
|
#include "TimerThread.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2013-08-22 19:14:42 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-09-22 03:13:51 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2015-06-04 01:25:57 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2015-06-16 05:57:19 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracerImpl.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
2004-05-11 13:38:50 +04:00
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#ifndef getpid
|
|
|
|
#define getpid _getpid
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
using mozilla::Atomic;
|
2015-06-04 01:25:57 +03:00
|
|
|
using mozilla::LogLevel;
|
2010-07-15 17:59:24 +04:00
|
|
|
using mozilla::TimeDuration;
|
|
|
|
using mozilla::TimeStamp;
|
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
static Atomic<int32_t> gGenerator;
|
2012-07-30 18:20:58 +04:00
|
|
|
static TimerThread* gThread = nullptr;
|
2002-04-14 04:27:16 +04:00
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
// This module prints info about the precision of timers.
|
2015-10-19 22:50:14 +03:00
|
|
|
static mozilla::LazyLogModule sTimerLog("nsTimerImpl");
|
|
|
|
|
|
|
|
mozilla::LogModule*
|
2012-10-30 03:32:10 +04:00
|
|
|
GetTimerLog()
|
|
|
|
{
|
2015-10-19 22:50:14 +03:00
|
|
|
return sTimerLog;
|
2012-10-30 03:32:10 +04:00
|
|
|
}
|
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
// This module prints info about which timers are firing, which is useful for
|
|
|
|
// wakeups for the purposes of power profiling. Set the following environment
|
|
|
|
// variable before starting the browser.
|
|
|
|
//
|
2016-05-26 01:26:10 +03:00
|
|
|
// MOZ_LOG=TimerFirings:4
|
2015-09-10 10:50:51 +03:00
|
|
|
//
|
|
|
|
// Then a line will be printed for every timer that fires. The name used for a
|
|
|
|
// |CallbackType::Function| timer depends on the circumstances.
|
|
|
|
//
|
|
|
|
// - If it was explicitly named (e.g. it was initialized with
|
|
|
|
// InitWithNamedFuncCallback()) then that explicit name will be shown.
|
|
|
|
//
|
|
|
|
// - Otherwise, if we are on a platform that supports function name lookup
|
2015-09-22 03:13:51 +03:00
|
|
|
// (Mac or Linux) then the looked-up name will be shown with a
|
|
|
|
// "[from dladdr]" annotation. On Mac the looked-up name will be immediately
|
|
|
|
// useful. On Linux it'll need post-processing with
|
|
|
|
// tools/rb/fix_linux_stack.py.
|
2015-09-10 10:50:51 +03:00
|
|
|
//
|
|
|
|
// - Otherwise, no name will be printed. If many timers hit this case then
|
|
|
|
// you'll need to re-run the workload on a Mac to find out which timers they
|
|
|
|
// are, and then give them explicit names.
|
|
|
|
//
|
|
|
|
// If you redirect this output to a file called "out", you can then
|
|
|
|
// post-process it with a command something like the following.
|
|
|
|
//
|
|
|
|
// cat out | grep timer | sort | uniq -c | sort -r -n
|
|
|
|
//
|
|
|
|
// This will show how often each unique line appears, with the most common ones
|
|
|
|
// first.
|
|
|
|
//
|
2015-09-17 07:49:24 +03:00
|
|
|
// More detailed docs are here:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Performance/TimerFirings_logging
|
|
|
|
//
|
2015-10-19 22:50:14 +03:00
|
|
|
static mozilla::LazyLogModule sTimerFiringsLog("TimerFirings");
|
|
|
|
|
|
|
|
mozilla::LogModule*
|
2015-09-10 10:50:51 +03:00
|
|
|
GetTimerFiringsLog()
|
|
|
|
{
|
2015-10-19 22:50:14 +03:00
|
|
|
return sTimerFiringsLog;
|
2015-09-10 10:50:51 +03:00
|
|
|
}
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
double nsTimerImpl::sDeltaSumSquared = 0;
|
|
|
|
double nsTimerImpl::sDeltaSum = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
double nsTimerImpl::sDeltaNum = 0;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
myNS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
|
2014-05-27 11:15:35 +04:00
|
|
|
double* meanResult, double* stdDevResult)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
|
|
|
double mean = 0.0, var = 0.0, stdDev = 0.0;
|
|
|
|
if (n > 0.0 && sumOfValues >= 0) {
|
|
|
|
mean = sumOfValues / n;
|
|
|
|
double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (temp < 0.0 || n <= 1) {
|
2001-12-16 09:13:17 +03:00
|
|
|
var = 0.0;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2001-12-16 09:13:17 +03:00
|
|
|
var = temp / (n * (n - 1));
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
// for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
|
|
|
|
stdDev = var != 0.0 ? sqrt(var) : 0.0;
|
|
|
|
}
|
|
|
|
*meanResult = mean;
|
|
|
|
*stdDevResult = stdDev;
|
|
|
|
}
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsTimer, nsITimer)
|
|
|
|
NS_IMPL_ADDREF(nsTimer)
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2016-07-20 23:16:40 +03:00
|
|
|
nsTimer::Release(void)
|
2002-03-08 23:11:49 +03:00
|
|
|
{
|
2016-07-20 23:16:40 +03:00
|
|
|
nsrefcnt count = --mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, count, "nsTimer");
|
|
|
|
|
|
|
|
if (count == 1) {
|
|
|
|
// Last ref, held by nsTimerImpl. Make sure the cycle is broken.
|
|
|
|
// If there is a nsTimerEvent in a queue for this timer, the nsTimer will
|
|
|
|
// live until that event pops, otherwise the nsTimerImpl will go away and
|
|
|
|
// the nsTimer along with it.
|
|
|
|
mImpl->Neuter();
|
|
|
|
mImpl = nullptr;
|
|
|
|
} else if (count == 0) {
|
2010-07-05 13:42:18 +04:00
|
|
|
delete this;
|
2002-03-08 23:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
nsTimerImpl::nsTimerImpl(nsITimer* aTimer) :
|
2012-07-30 18:20:58 +04:00
|
|
|
mClosure(nullptr),
|
2015-09-10 10:50:51 +03:00
|
|
|
mName(nsTimerImpl::Nothing),
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType(CallbackType::Unknown),
|
2002-12-04 03:57:20 +03:00
|
|
|
mGeneration(0),
|
2016-07-20 23:16:40 +03:00
|
|
|
mDelay(0),
|
|
|
|
mITimer(aTimer)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
// XXXbsmedberg: shouldn't this be in Init()?
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallback.c = nullptr;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTimerImpl::~nsTimerImpl()
|
|
|
|
{
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2004-05-11 13:38:50 +04:00
|
|
|
//static
|
|
|
|
nsresult
|
|
|
|
nsTimerImpl::Startup()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
gThread = new TimerThread();
|
|
|
|
|
|
|
|
NS_ADDREF(gThread);
|
|
|
|
rv = gThread->InitLocks();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(gThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
|
|
|
nsTimerImpl::Shutdown()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2002-01-15 04:20:47 +03:00
|
|
|
double mean = 0, stddev = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
myNS_MeanAndStdDev(sDeltaNum, sDeltaSum, sDeltaSumSquared, &mean, &stddev);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("sDeltaNum = %f, sDeltaSum = %f, sDeltaSumSquared = %f\n",
|
|
|
|
sDeltaNum, sDeltaSum, sDeltaSumSquared));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("mean: %fms, stddev: %fms\n", mean, stddev));
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!gThread) {
|
2001-12-16 09:13:17 +03:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
gThread->Shutdown();
|
2002-02-18 03:10:55 +03:00
|
|
|
NS_RELEASE(gThread);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsresult
|
2015-09-15 01:57:17 +03:00
|
|
|
nsTimerImpl::InitCommon(uint32_t aDelay, uint32_t aType)
|
2002-12-04 03:57:20 +03:00
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!gThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-04-23 21:24:20 +04:00
|
|
|
if (!mEventTarget) {
|
|
|
|
NS_ERROR("mEventTarget is NULL");
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2004-05-11 13:38:50 +04:00
|
|
|
|
|
|
|
rv = gThread->Init();
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-05-11 13:38:50 +04:00
|
|
|
|
2015-07-28 18:10:54 +03:00
|
|
|
gThread->RemoveTimer(this);
|
2011-11-03 07:46:08 +04:00
|
|
|
mTimeout = TimeStamp();
|
2013-08-22 19:14:42 +04:00
|
|
|
mGeneration = gGenerator++;
|
2002-12-04 03:57:20 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mType = (uint8_t)aType;
|
2002-12-04 03:57:20 +03:00
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
|
|
|
return gThread->AddTimer(this);
|
|
|
|
}
|
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
nsresult
|
|
|
|
nsTimerImpl::InitWithFuncCallbackCommon(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType,
|
|
|
|
Name aName)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aFunc)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Function;
|
2002-12-04 03:57:20 +03:00
|
|
|
mCallback.c = aFunc;
|
2001-12-16 09:13:17 +03:00
|
|
|
mClosure = aClosure;
|
2015-09-10 10:50:51 +03:00
|
|
|
mName = aName;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-09-15 01:57:17 +03:00
|
|
|
return InitCommon(aDelay, aType);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2015-09-10 10:50:51 +03:00
|
|
|
nsTimerImpl::InitWithFuncCallback(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType)
|
|
|
|
{
|
|
|
|
Name name(nsTimerImpl::Nothing);
|
|
|
|
return InitWithFuncCallbackCommon(aFunc, aClosure, aDelay, aType, name);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2015-09-10 10:50:51 +03:00
|
|
|
nsTimerImpl::InitWithNamedFuncCallback(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType,
|
|
|
|
const char* aNameString)
|
|
|
|
{
|
|
|
|
Name name(aNameString);
|
|
|
|
return InitWithFuncCallbackCommon(aFunc, aClosure, aDelay, aType, name);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2015-09-10 10:50:51 +03:00
|
|
|
nsTimerImpl::InitWithNameableFuncCallback(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType,
|
|
|
|
nsTimerNameCallbackFunc aNameFunc)
|
|
|
|
{
|
|
|
|
Name name(aNameFunc);
|
|
|
|
return InitWithFuncCallbackCommon(aFunc, aClosure, aDelay, aType, name);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::InitWithCallback(nsITimerCallback* aCallback,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aCallback)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-11-02 03:46:37 +03:00
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Interface;
|
2001-12-16 09:13:17 +03:00
|
|
|
mCallback.i = aCallback;
|
2001-12-17 09:59:55 +03:00
|
|
|
NS_ADDREF(mCallback.i);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-09-15 01:57:17 +03:00
|
|
|
return InitCommon(aDelay, aType);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::Init(nsIObserver* aObserver, uint32_t aDelay, uint32_t aType)
|
2002-02-20 07:01:34 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-11-02 03:46:37 +03:00
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Observer;
|
2002-02-20 07:01:34 +03:00
|
|
|
mCallback.o = aObserver;
|
|
|
|
NS_ADDREF(mCallback.o);
|
|
|
|
|
2015-09-15 01:57:17 +03:00
|
|
|
return InitCommon(aDelay, aType);
|
2002-02-20 07:01:34 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::Cancel()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (gThread) {
|
2001-12-31 07:04:22 +03:00
|
|
|
gThread->RemoveTimer(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-02-20 07:01:34 +03:00
|
|
|
|
2007-10-24 23:11:41 +04:00
|
|
|
ReleaseCallback();
|
|
|
|
|
2002-02-20 07:01:34 +03:00
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
void
|
|
|
|
nsTimerImpl::Neuter()
|
|
|
|
{
|
|
|
|
if (gThread) {
|
2016-10-04 18:23:39 +03:00
|
|
|
gThread->RemoveTimer(this, true);
|
2016-07-20 23:16:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If invoked on the target thread, guarantees that the timer doesn't pop.
|
|
|
|
// If invoked anywhere else, it might prevent the timer from popping, but
|
2016-10-04 18:23:39 +03:00
|
|
|
// no guarantees. In any case, the above RemoveTimer call will prevent it
|
|
|
|
// from being rescheduled.
|
2016-07-20 23:16:40 +03:00
|
|
|
++mGeneration;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::SetDelay(uint32_t aDelay)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2016-07-20 23:16:40 +03:00
|
|
|
if (mCallbackType == CallbackType::Unknown && mType == nsITimer::TYPE_ONE_SHOT) {
|
2009-02-03 17:42:21 +03:00
|
|
|
// This may happen if someone tries to re-use a one-shot timer
|
|
|
|
// by re-setting delay instead of reinitializing the timer.
|
|
|
|
NS_ERROR("nsITimer->SetDelay() called when the "
|
|
|
|
"one-shot timer is not set up.");
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:07:38 +03:00
|
|
|
bool reAdd = false;
|
|
|
|
if (gThread) {
|
|
|
|
reAdd = NS_SUCCEEDED(gThread->RemoveTimer(this));
|
|
|
|
}
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
2016-08-05 18:07:38 +03:00
|
|
|
if (reAdd) {
|
|
|
|
gThread->AddTimer(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-09-07 09:38:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::GetDelay(uint32_t* aDelay)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aDelay = mDelay;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::SetType(uint32_t aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
mType = (uint8_t)aType;
|
2001-12-16 09:13:17 +03:00
|
|
|
// XXX if this is called, we should change the actual type.. this could effect
|
2002-04-14 04:27:16 +04:00
|
|
|
// repeating timers. we need to ensure in Fire() that if mType has changed
|
2001-12-16 09:13:17 +03:00
|
|
|
// during the callback that we don't end up with the timer in the queue twice.
|
2002-09-07 09:38:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::GetType(uint32_t* aType)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::GetClosure(void** aClosure)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aClosure = mClosure;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::GetCallback(nsITimerCallback** aCallback)
|
2005-05-05 00:22:32 +04:00
|
|
|
{
|
2015-04-20 21:08:57 +03:00
|
|
|
if (mCallbackType == CallbackType::Interface) {
|
2005-05-05 00:22:32 +04:00
|
|
|
NS_IF_ADDREF(*aCallback = mCallback.i);
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aCallback = nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-05-05 00:22:32 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::GetTarget(nsIEventTarget** aTarget)
|
2008-07-24 21:20:33 +04:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aTarget = mEventTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-10 06:08:43 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTimerImpl::SetTarget(nsIEventTarget* aTarget)
|
2008-07-24 21:20:33 +04:00
|
|
|
{
|
2015-04-20 21:08:57 +03:00
|
|
|
if (NS_WARN_IF(mCallbackType != CallbackType::Unknown)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2008-07-24 21:20:33 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aTarget) {
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = aTarget;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2008-07-24 21:20:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
|
|
|
nsTimerImpl::Fire()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2016-08-05 21:39:34 +03:00
|
|
|
if (mCallbackType == CallbackType::Unknown) {
|
2001-12-17 09:59:55 +03:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-17 09:59:55 +03:00
|
|
|
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("Timer", "Fire",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::OTHER);
|
2011-12-03 02:05:33 +04:00
|
|
|
|
2013-08-20 23:22:27 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeDuration a = now - mStart; // actual delay in intervals
|
|
|
|
TimeDuration b = TimeDuration::FromMilliseconds(mDelay); // expected delay in intervals
|
|
|
|
TimeDuration delta = (a > b) ? a - b : b - a;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t d = delta.ToMilliseconds(); // delta in ms
|
2002-01-15 04:20:47 +03:00
|
|
|
sDeltaSum += d;
|
|
|
|
sDeltaSumSquared += double(d) * double(d);
|
2002-03-08 23:11:49 +03:00
|
|
|
sDeltaNum++;
|
2002-01-15 04:20:47 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] expected delay time %4ums\n", this, mDelay));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] actual delay time %fms\n", this,
|
|
|
|
a.ToMilliseconds()));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] (mType is %d) -------\n", this, mType));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] delta %4dms\n",
|
|
|
|
this, (a > b) ? (int32_t)d : -(int32_t)d));
|
2002-01-15 04:20:47 +03:00
|
|
|
|
|
|
|
mStart = mStart2;
|
2010-07-22 20:00:26 +04:00
|
|
|
mStart2 = TimeStamp();
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerFiringsLog(), LogLevel::Debug)) {
|
2016-08-05 20:50:00 +03:00
|
|
|
LogFiring(mCallbackType, mCallback);
|
2015-09-10 10:50:51 +03:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:50:00 +03:00
|
|
|
int32_t oldGeneration = mGeneration;
|
|
|
|
|
|
|
|
switch (mCallbackType) {
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Function:
|
2016-08-05 20:50:00 +03:00
|
|
|
mCallback.c(mITimer, mClosure);
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Interface:
|
2016-08-05 20:50:00 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsITimerCallback> keepalive(mCallback.i);
|
|
|
|
keepalive->Notify(mITimer);
|
|
|
|
}
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Observer:
|
2016-08-05 20:50:00 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserver> keepalive(mCallback.o);
|
|
|
|
keepalive->Observe(mITimer,
|
|
|
|
NS_TIMER_CALLBACK_TOPIC,
|
|
|
|
nullptr);
|
|
|
|
}
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2014-05-27 11:15:35 +04:00
|
|
|
default:
|
|
|
|
;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:50:00 +03:00
|
|
|
if (oldGeneration == mGeneration && mCallbackType != CallbackType::Unknown) {
|
|
|
|
// Timer has not been re-init or canceled; clear out one-shot timers, and
|
|
|
|
// re-schedule repeating timers.
|
|
|
|
if (IsRepeating()) {
|
|
|
|
if (mType == nsITimer::TYPE_REPEATING_SLACK) {
|
|
|
|
SetDelayInternal(mDelay);
|
|
|
|
} else {
|
|
|
|
SetDelayInternal(mDelay, mTimeout);
|
|
|
|
}
|
|
|
|
if (gThread) {
|
|
|
|
gThread->AddTimer(this);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ReleaseCallback();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2007-10-24 23:11:41 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2015-05-07 19:43:37 +03:00
|
|
|
("[this=%p] Took %fms to fire timer callback\n",
|
|
|
|
this, (TimeStamp::Now() - now).ToMilliseconds()));
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2016-03-07 22:00:41 +03:00
|
|
|
#if defined(HAVE_DLADDR) && defined(HAVE___CXA_DEMANGLE)
|
2015-09-22 03:13:51 +03:00
|
|
|
#define USE_DLADDR 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_DLADDR
|
2015-09-10 10:50:51 +03:00
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// See the big comment above GetTimerFiringsLog() to understand this code.
|
|
|
|
void
|
|
|
|
nsTimerImpl::LogFiring(CallbackType aCallbackType, CallbackUnion aCallback)
|
|
|
|
{
|
|
|
|
const char* typeStr;
|
|
|
|
switch (mType) {
|
2016-07-20 23:16:40 +03:00
|
|
|
case nsITimer::TYPE_ONE_SHOT: typeStr = "ONE_SHOT"; break;
|
|
|
|
case nsITimer::TYPE_REPEATING_SLACK: typeStr = "SLACK "; break;
|
|
|
|
case nsITimer::TYPE_REPEATING_PRECISE: /* fall through */
|
|
|
|
case nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP: typeStr = "PRECISE "; break;
|
2015-09-10 10:50:51 +03:00
|
|
|
default: MOZ_CRASH("bad type");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aCallbackType) {
|
|
|
|
case CallbackType::Function: {
|
|
|
|
bool needToFreeName = false;
|
|
|
|
const char* annotation = "";
|
|
|
|
const char* name;
|
|
|
|
static const size_t buflen = 1024;
|
|
|
|
char buf[buflen];
|
|
|
|
|
|
|
|
if (mName.is<NameString>()) {
|
|
|
|
name = mName.as<NameString>();
|
|
|
|
|
|
|
|
} else if (mName.is<NameFunc>()) {
|
2017-01-19 02:37:30 +03:00
|
|
|
mName.as<NameFunc>()(mITimer, /* aAnonymize = */ false, mClosure, buf, buflen);
|
2015-09-10 10:50:51 +03:00
|
|
|
name = buf;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mName.is<NameNothing>());
|
2015-09-22 03:13:51 +03:00
|
|
|
#ifdef USE_DLADDR
|
2015-09-10 10:50:51 +03:00
|
|
|
annotation = "[from dladdr] ";
|
|
|
|
|
|
|
|
Dl_info info;
|
2015-09-22 03:13:51 +03:00
|
|
|
void* addr = reinterpret_cast<void*>(aCallback.c);
|
|
|
|
if (dladdr(addr, &info) == 0) {
|
2015-09-10 10:50:51 +03:00
|
|
|
name = "???[dladdr: failed]";
|
|
|
|
|
2015-09-22 03:13:51 +03:00
|
|
|
} else if (info.dli_sname) {
|
2015-09-10 10:50:51 +03:00
|
|
|
int status;
|
|
|
|
name = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
|
|
|
|
if (status == 0) {
|
|
|
|
// Success. Because we didn't pass in a buffer to __cxa_demangle it
|
|
|
|
// allocates its own one with malloc() which we must free() later.
|
|
|
|
MOZ_ASSERT(name);
|
|
|
|
needToFreeName = true;
|
|
|
|
} else if (status == -1) {
|
|
|
|
name = "???[__cxa_demangle: OOM]";
|
|
|
|
} else if (status == -2) {
|
|
|
|
name = "???[__cxa_demangle: invalid mangled name]";
|
|
|
|
} else if (status == -3) {
|
|
|
|
name = "???[__cxa_demangle: invalid argument]";
|
|
|
|
} else {
|
|
|
|
name = "???[__cxa_demangle: unexpected status value]";
|
|
|
|
}
|
2015-09-22 03:13:51 +03:00
|
|
|
|
|
|
|
} else if (info.dli_fname) {
|
|
|
|
// The "#0: " prefix is necessary for fix_linux_stack.py to interpret
|
|
|
|
// this string as something to convert.
|
|
|
|
snprintf(buf, buflen, "#0: ???[%s +0x%" PRIxPTR "]\n",
|
|
|
|
info.dli_fname, uintptr_t(addr) - uintptr_t(info.dli_fbase));
|
|
|
|
name = buf;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
name = "???[dladdr: no symbol or shared object obtained]";
|
2015-09-10 10:50:51 +03:00
|
|
|
}
|
|
|
|
#else
|
2015-09-22 03:13:51 +03:00
|
|
|
name = "???[dladdr is unimplemented or doesn't work well on this OS]";
|
2015-09-10 10:50:51 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_LOG(GetTimerFiringsLog(), LogLevel::Debug,
|
|
|
|
("[%d] fn timer (%s %5d ms): %s%s\n",
|
|
|
|
getpid(), typeStr, mDelay, annotation, name));
|
|
|
|
|
|
|
|
if (needToFreeName) {
|
|
|
|
free(const_cast<char*>(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CallbackType::Interface: {
|
|
|
|
MOZ_LOG(GetTimerFiringsLog(), LogLevel::Debug,
|
|
|
|
("[%d] iface timer (%s %5d ms): %p\n",
|
|
|
|
getpid(), typeStr, mDelay, aCallback.i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CallbackType::Observer: {
|
|
|
|
MOZ_LOG(GetTimerFiringsLog(), LogLevel::Debug,
|
|
|
|
("[%d] obs timer (%s %5d ms): %p\n",
|
|
|
|
getpid(), typeStr, mDelay, aCallback.o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CallbackType::Unknown:
|
|
|
|
default: {
|
|
|
|
MOZ_LOG(GetTimerFiringsLog(), LogLevel::Debug,
|
|
|
|
("[%d] ??? timer (%s, %5d ms)\n",
|
|
|
|
getpid(), typeStr, mDelay));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
2016-08-05 20:50:00 +03:00
|
|
|
nsTimerImpl::SetDelayInternal(uint32_t aDelay, TimeStamp aBase)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeDuration delayInterval = TimeDuration::FromMilliseconds(aDelay);
|
2002-04-26 01:07:54 +04:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
mDelay = aDelay;
|
|
|
|
|
2016-08-05 20:50:00 +03:00
|
|
|
mTimeout = aBase;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-04-26 01:07:54 +04:00
|
|
|
mTimeout += delayInterval;
|
2002-03-08 23:11:49 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mStart.IsNull()) {
|
2016-08-05 20:50:00 +03:00
|
|
|
mStart = aBase;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2016-08-05 20:50:00 +03:00
|
|
|
mStart2 = aBase;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
2002-01-24 09:37:38 +03:00
|
|
|
|
2016-11-23 07:40:13 +03:00
|
|
|
void
|
|
|
|
nsTimerImpl::GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
switch (mCallbackType) {
|
|
|
|
case CallbackType::Function:
|
|
|
|
if (mName.is<NameString>()) {
|
|
|
|
aName.Assign(mName.as<NameString>());
|
|
|
|
} else if (mName.is<NameFunc>()) {
|
|
|
|
static const size_t buflen = 1024;
|
|
|
|
char buf[buflen];
|
2017-01-19 02:37:30 +03:00
|
|
|
mName.as<NameFunc>()(mITimer, /* aAnonymize = */ true, mClosure, buf, buflen);
|
2016-11-23 07:40:13 +03:00
|
|
|
aName.Assign(buf);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mName.is<NameNothing>());
|
2017-01-19 01:01:05 +03:00
|
|
|
aName.AssignLiteral("Anonymous callback timer");
|
2016-11-23 07:40:13 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallbackType::Interface:
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(mCallback.i)) {
|
|
|
|
named->GetName(aName);
|
2017-01-19 01:01:05 +03:00
|
|
|
} else {
|
|
|
|
aName.AssignLiteral("Anonymous interface timer");
|
2016-11-23 07:40:13 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallbackType::Observer:
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(mCallback.o)) {
|
|
|
|
named->GetName(aName);
|
2017-01-19 01:01:05 +03:00
|
|
|
} else {
|
|
|
|
aName.AssignLiteral("Anonymous observer timer");
|
2016-11-23 07:40:13 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallbackType::Unknown:
|
2017-01-19 01:01:05 +03:00
|
|
|
aName.AssignLiteral("Anonymous timer");
|
2016-11-23 07:40:13 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
nsTimer::~nsTimer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-23 02:02:59 +04:00
|
|
|
size_t
|
2016-07-20 23:16:40 +03:00
|
|
|
nsTimer::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2014-06-23 02:02:59 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this);
|
|
|
|
}
|
2015-06-16 05:57:19 +03:00
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
/* static */ const nsTimerImpl::NameNothing nsTimerImpl::Nothing = 0;
|
|
|
|
|
2015-06-16 05:57:19 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
void
|
|
|
|
nsTimerImpl::GetTLSTraceInfo()
|
|
|
|
{
|
|
|
|
mTracedTask.GetTLSTraceInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
TracedTaskCommon
|
|
|
|
nsTimerImpl::GetTracedTask()
|
|
|
|
{
|
|
|
|
return mTracedTask;
|
|
|
|
}
|
2016-07-20 23:16:40 +03:00
|
|
|
|
2015-06-16 05:57:19 +03:00
|
|
|
#endif
|
|
|
|
|