2012-07-11 03:12:40 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-06-30 19:39:45 +04:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-07-11 03:12:40 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-10-21 21:48:08 +04:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2014-12-31 00:52:39 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2014-01-14 20:33:31 +04:00
|
|
|
#include "mozilla/BackgroundHangMonitor.h"
|
2012-07-11 03:12:40 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2012-08-22 01:14:38 +04:00
|
|
|
#include "mozilla/ProcessedStack.h"
|
2014-12-31 00:52:39 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-10-21 22:18:27 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-12-31 00:52:39 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-10-21 21:48:08 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-11 03:12:40 +04:00
|
|
|
#include "nsStackWalk.h"
|
2014-10-21 21:48:08 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
#include "nsExceptionHandler.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS) && defined(MOZ_PROFILING) && defined(XP_WIN)
|
|
|
|
#define REPORT_CHROME_HANGS
|
|
|
|
#endif
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace HangMonitor {
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A flag which may be set from within a debugger to disable the hang
|
|
|
|
* monitor.
|
|
|
|
*/
|
|
|
|
volatile bool gDebugDisableHangMonitor = false;
|
|
|
|
|
|
|
|
const char kHangMonitorPrefName[] = "hangmonitor.timeout";
|
|
|
|
|
2014-05-09 21:55:29 +04:00
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2012-07-11 03:12:40 +04:00
|
|
|
const char kTelemetryPrefName[] = "toolkit.telemetry.enabled";
|
2014-05-09 21:55:29 +04:00
|
|
|
#endif
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
// Monitor protects gShutdown and gTimeout, but not gTimestamp which rely on
|
|
|
|
// being atomically set by the processor; synchronization doesn't really matter
|
|
|
|
// in this use case.
|
|
|
|
Monitor* gMonitor;
|
|
|
|
|
|
|
|
// The timeout preference, in seconds.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t gTimeout;
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
PRThread* gThread;
|
|
|
|
|
|
|
|
// Set when shutdown begins to signal the thread to exit immediately.
|
|
|
|
bool gShutdown;
|
|
|
|
|
|
|
|
// The timestamp of the last event notification, or PR_INTERVAL_NO_WAIT if
|
|
|
|
// we're currently not processing events.
|
2013-10-29 03:29:00 +04:00
|
|
|
Atomic<PRIntervalTime> gTimestamp(PR_INTERVAL_NO_WAIT);
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
// Main thread ID used in reporting chrome hangs under Windows
|
2013-10-11 00:42:16 +04:00
|
|
|
static HANDLE winMainThreadHandle = nullptr;
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
// Default timeout for reporting chrome hangs to Telemetry (5 seconds)
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t DEFAULT_CHROME_HANG_INTERVAL = 5;
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
// Maximum number of PCs to gather from the stack
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t MAX_CALL_STACK_PCS = 400;
|
2014-10-21 21:48:08 +04:00
|
|
|
|
|
|
|
// Chrome hang annotators
|
|
|
|
static StaticAutoPtr<std::set<Annotator*>> gAnnotators;
|
2012-07-11 03:12:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// PrefChangedFunc
|
2013-12-11 03:10:01 +04:00
|
|
|
void
|
2012-07-11 03:12:40 +04:00
|
|
|
PrefChanged(const char*, void*)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newval = Preferences::GetInt(kHangMonitorPrefName);
|
2012-07-11 03:12:40 +04:00
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
// Monitor chrome hangs on the profiling branch if Telemetry enabled
|
|
|
|
if (newval == 0) {
|
|
|
|
bool telemetryEnabled = Preferences::GetBool(kTelemetryPrefName);
|
|
|
|
if (telemetryEnabled) {
|
|
|
|
newval = DEFAULT_CHROME_HANG_INTERVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
if (newval != gTimeout) {
|
|
|
|
gTimeout = newval;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crash()
|
|
|
|
{
|
|
|
|
if (gDebugDisableHangMonitor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (::IsDebuggerPresent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("Hang"),
|
|
|
|
NS_LITERAL_CSTRING("1"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_RUNTIMEABORT("HangMonitor triggered");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-10-21 21:48:08 +04:00
|
|
|
class ChromeHangAnnotations : public HangAnnotations
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChromeHangAnnotations();
|
|
|
|
~ChromeHangAnnotations();
|
|
|
|
|
|
|
|
void AddAnnotation(const nsAString& aName, const int32_t aData) MOZ_OVERRIDE;
|
|
|
|
void AddAnnotation(const nsAString& aName, const double aData) MOZ_OVERRIDE;
|
|
|
|
void AddAnnotation(const nsAString& aName, const nsAString& aData) MOZ_OVERRIDE;
|
|
|
|
void AddAnnotation(const nsAString& aName, const nsACString& aData) MOZ_OVERRIDE;
|
|
|
|
void AddAnnotation(const nsAString& aName, const bool aData) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
|
|
|
|
bool IsEmpty() const MOZ_OVERRIDE;
|
|
|
|
bool GetEnumerator(Enumerator** aOutEnum) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
typedef std::pair<nsString, nsString> AnnotationType;
|
|
|
|
typedef std::vector<AnnotationType> VectorType;
|
|
|
|
typedef VectorType::const_iterator IteratorType;
|
|
|
|
|
|
|
|
private:
|
|
|
|
VectorType mAnnotations;
|
|
|
|
};
|
|
|
|
|
|
|
|
ChromeHangAnnotations::ChromeHangAnnotations()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ChromeHangAnnotations);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChromeHangAnnotations::~ChromeHangAnnotations()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ChromeHangAnnotations);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChromeHangAnnotations::AddAnnotation(const nsAString& aName, const int32_t aData)
|
|
|
|
{
|
|
|
|
nsString dataString;
|
|
|
|
dataString.AppendInt(aData);
|
|
|
|
AnnotationType annotation = std::make_pair(nsString(aName), dataString);
|
|
|
|
mAnnotations.push_back(annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChromeHangAnnotations::AddAnnotation(const nsAString& aName, const double aData)
|
|
|
|
{
|
|
|
|
nsString dataString;
|
|
|
|
dataString.AppendFloat(aData);
|
|
|
|
AnnotationType annotation = std::make_pair(nsString(aName), dataString);
|
|
|
|
mAnnotations.push_back(annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChromeHangAnnotations::AddAnnotation(const nsAString& aName, const nsAString& aData)
|
|
|
|
{
|
|
|
|
AnnotationType annotation = std::make_pair(nsString(aName), nsString(aData));
|
|
|
|
mAnnotations.push_back(annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChromeHangAnnotations::AddAnnotation(const nsAString& aName, const nsACString& aData)
|
|
|
|
{
|
|
|
|
nsString dataString;
|
|
|
|
AppendUTF8toUTF16(aData, dataString);
|
|
|
|
AnnotationType annotation = std::make_pair(nsString(aName), dataString);
|
|
|
|
mAnnotations.push_back(annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChromeHangAnnotations::AddAnnotation(const nsAString& aName, const bool aData)
|
|
|
|
{
|
|
|
|
nsString dataString;
|
|
|
|
dataString += aData ? NS_LITERAL_STRING("true") : NS_LITERAL_STRING("false");
|
|
|
|
AnnotationType annotation = std::make_pair(nsString(aName), dataString);
|
|
|
|
mAnnotations.push_back(annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class itself does not use synchronization but it (and its parent object)
|
|
|
|
* should be protected by mutual exclusion in some way. In Telemetry the chrome
|
|
|
|
* hang data is protected via TelemetryImpl::mHangReportsMutex.
|
|
|
|
*/
|
|
|
|
class ChromeHangAnnotationEnumerator : public HangAnnotations::Enumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChromeHangAnnotationEnumerator(const ChromeHangAnnotations::VectorType& aAnnotations);
|
|
|
|
~ChromeHangAnnotationEnumerator();
|
|
|
|
|
|
|
|
virtual bool Next(nsAString& aOutName, nsAString& aOutValue);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ChromeHangAnnotations::IteratorType mIterator;
|
|
|
|
ChromeHangAnnotations::IteratorType mEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
ChromeHangAnnotationEnumerator::ChromeHangAnnotationEnumerator(
|
|
|
|
const ChromeHangAnnotations::VectorType& aAnnotations)
|
|
|
|
: mIterator(aAnnotations.begin())
|
|
|
|
, mEnd(aAnnotations.end())
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ChromeHangAnnotationEnumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChromeHangAnnotationEnumerator::~ChromeHangAnnotationEnumerator()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ChromeHangAnnotationEnumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ChromeHangAnnotationEnumerator::Next(nsAString& aOutName, nsAString& aOutValue)
|
|
|
|
{
|
|
|
|
aOutName.Truncate();
|
|
|
|
aOutValue.Truncate();
|
|
|
|
if (mIterator == mEnd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aOutName = mIterator->first;
|
|
|
|
aOutValue = mIterator->second;
|
|
|
|
++mIterator;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ChromeHangAnnotations::IsEmpty() const
|
|
|
|
{
|
|
|
|
return mAnnotations.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ChromeHangAnnotations::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t result = sizeof(mAnnotations) +
|
|
|
|
mAnnotations.capacity() * sizeof(AnnotationType);
|
|
|
|
for (IteratorType i = mAnnotations.begin(), e = mAnnotations.end(); i != e;
|
|
|
|
++i) {
|
|
|
|
result += i->first.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
result += i->second.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ChromeHangAnnotations::GetEnumerator(HangAnnotations::Enumerator** aOutEnum)
|
|
|
|
{
|
|
|
|
if (!aOutEnum) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aOutEnum = nullptr;
|
|
|
|
if (mAnnotations.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aOutEnum = new ChromeHangAnnotationEnumerator(mAnnotations);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-11 03:12:40 +04:00
|
|
|
static void
|
2014-09-11 08:47:01 +04:00
|
|
|
ChromeStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP, void* aClosure)
|
2012-07-11 03:12:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aClosure);
|
2014-05-27 11:15:35 +04:00
|
|
|
std::vector<uintptr_t>* stack =
|
2012-08-22 01:14:38 +04:00
|
|
|
static_cast<std::vector<uintptr_t>*>(aClosure);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (stack->size() == MAX_CALL_STACK_PCS) {
|
2012-08-22 01:14:38 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-08-22 01:14:38 +04:00
|
|
|
MOZ_ASSERT(stack->size() < MAX_CALL_STACK_PCS);
|
|
|
|
stack->push_back(reinterpret_cast<uintptr_t>(aPC));
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-27 11:15:35 +04:00
|
|
|
GetChromeHangReport(Telemetry::ProcessedStack& aStack,
|
|
|
|
int32_t& aSystemUptime,
|
|
|
|
int32_t& aFirefoxUptime)
|
2012-07-11 03:12:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(winMainThreadHandle);
|
|
|
|
|
|
|
|
// The thread we're about to suspend might have the alloc lock
|
|
|
|
// so allocate ahead of time
|
2012-08-22 01:14:38 +04:00
|
|
|
std::vector<uintptr_t> rawStack;
|
|
|
|
rawStack.reserve(MAX_CALL_STACK_PCS);
|
2012-07-11 03:12:40 +04:00
|
|
|
DWORD ret = ::SuspendThread(winMainThreadHandle);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (ret == -1) {
|
2012-07-11 03:12:40 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-12-21 09:31:57 +04:00
|
|
|
NS_StackWalk(ChromeStackWalker, /* skipFrames */ 0, /* maxFrames */ 0,
|
|
|
|
reinterpret_cast<void*>(&rawStack),
|
2012-12-08 09:15:21 +04:00
|
|
|
reinterpret_cast<uintptr_t>(winMainThreadHandle), nullptr);
|
2012-07-11 03:12:40 +04:00
|
|
|
ret = ::ResumeThread(winMainThreadHandle);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (ret == -1) {
|
2012-07-11 03:12:40 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-11-20 18:45:14 +04:00
|
|
|
aStack = Telemetry::GetStackAndModules(rawStack);
|
2014-01-28 01:47:00 +04:00
|
|
|
|
|
|
|
// Record system uptime (in minutes) at the time of the hang
|
|
|
|
aSystemUptime = ((GetTickCount() / 1000) - (gTimeout * 2)) / 60;
|
|
|
|
|
|
|
|
// Record Firefox uptime (in minutes) at the time of the hang
|
|
|
|
bool error;
|
|
|
|
TimeStamp processCreation = TimeStamp::ProcessCreation(error);
|
|
|
|
if (!error) {
|
|
|
|
TimeDuration td = TimeStamp::Now() - processCreation;
|
|
|
|
aFirefoxUptime = (static_cast<int32_t>(td.ToSeconds()) - (gTimeout * 2)) / 60;
|
|
|
|
} else {
|
|
|
|
aFirefoxUptime = -1;
|
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
2014-10-21 21:48:08 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
ChromeHangAnnotatorCallout(ChromeHangAnnotations& aAnnotations)
|
|
|
|
{
|
|
|
|
gMonitor->AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(gAnnotators);
|
|
|
|
if (!gAnnotators) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (std::set<Annotator*>::iterator i = gAnnotators->begin(),
|
|
|
|
e = gAnnotators->end();
|
|
|
|
i != e; ++i) {
|
|
|
|
(*i)->AnnotateHang(aAnnotations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 03:12:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadMain(void*)
|
|
|
|
{
|
2012-06-12 21:06:20 +04:00
|
|
|
PR_SetCurrentThreadName("Hang Monitor");
|
|
|
|
|
2012-07-11 03:12:40 +04:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
|
|
|
|
// In order to avoid issues with the hang monitor incorrectly triggering
|
|
|
|
// during a general system stop such as sleeping, the monitor thread must
|
|
|
|
// run twice to trigger hang protection.
|
|
|
|
PRIntervalTime lastTimestamp = 0;
|
|
|
|
int waitCount = 0;
|
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2012-08-22 01:14:38 +04:00
|
|
|
Telemetry::ProcessedStack stack;
|
2014-01-28 01:47:00 +04:00
|
|
|
int32_t systemUptime = -1;
|
|
|
|
int32_t firefoxUptime = -1;
|
2014-12-31 00:52:39 +03:00
|
|
|
auto annotations = MakeUnique<ChromeHangAnnotations>();
|
2012-07-11 03:12:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (gShutdown) {
|
|
|
|
return; // Exit the thread
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid rereading the volatile value in this loop
|
|
|
|
PRIntervalTime timestamp = gTimestamp;
|
|
|
|
|
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
|
|
|
|
|
|
|
if (timestamp != PR_INTERVAL_NO_WAIT &&
|
|
|
|
now < timestamp) {
|
|
|
|
// 32-bit overflow, reset for another waiting period
|
|
|
|
timestamp = 1; // lowest legal PRInterval value
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timestamp != PR_INTERVAL_NO_WAIT &&
|
|
|
|
timestamp == lastTimestamp &&
|
|
|
|
gTimeout > 0) {
|
|
|
|
++waitCount;
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-01-28 01:47:00 +04:00
|
|
|
// Capture the chrome-hang stack + Firefox & system uptimes after
|
|
|
|
// the minimum hang duration has been reached (not when the hang ends)
|
|
|
|
if (waitCount == 2) {
|
|
|
|
GetChromeHangReport(stack, systemUptime, firefoxUptime);
|
2014-10-21 21:48:08 +04:00
|
|
|
ChromeHangAnnotatorCallout(*annotations);
|
2014-01-28 01:47:00 +04:00
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
#else
|
2014-01-28 01:47:00 +04:00
|
|
|
// This is the crash-on-hang feature.
|
|
|
|
// See bug 867313 for the quirk in the waitCount comparison
|
|
|
|
if (waitCount >= 2) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t delay =
|
|
|
|
int32_t(PR_IntervalToSeconds(now - timestamp));
|
2013-05-01 02:39:16 +04:00
|
|
|
if (delay >= gTimeout) {
|
2012-07-11 03:12:40 +04:00
|
|
|
MonitorAutoUnlock unlock(*gMonitor);
|
|
|
|
Crash();
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 01:47:00 +04:00
|
|
|
#endif
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2012-07-11 03:12:40 +04:00
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
if (waitCount >= 2) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t hangDuration = PR_IntervalToSeconds(now - lastTimestamp);
|
2014-10-21 21:48:08 +04:00
|
|
|
Telemetry::RecordChromeHang(hangDuration, stack, systemUptime,
|
2014-12-31 00:52:39 +03:00
|
|
|
firefoxUptime, Move(annotations));
|
2012-08-22 01:14:38 +04:00
|
|
|
stack.Clear();
|
2014-12-31 00:52:39 +03:00
|
|
|
annotations = MakeUnique<ChromeHangAnnotations>();
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
waitCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntervalTime timeout;
|
|
|
|
if (gTimeout <= 0) {
|
|
|
|
timeout = PR_INTERVAL_NO_TIMEOUT;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2012-07-11 03:12:40 +04:00
|
|
|
timeout = PR_MillisecondsToInterval(gTimeout * 500);
|
|
|
|
}
|
|
|
|
lock.Wait(timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Startup()
|
|
|
|
{
|
|
|
|
// The hang detector only runs in chrome processes. If you change this,
|
|
|
|
// you must also deal with the threadsafety of AnnotateCrashReport in
|
|
|
|
// non-chrome processes!
|
2014-05-27 11:15:35 +04:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
2012-07-11 03:12:40 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(!gMonitor, "Hang monitor already initialized");
|
2012-07-11 03:12:40 +04:00
|
|
|
gMonitor = new Monitor("HangMonitor");
|
|
|
|
|
2013-10-11 00:42:16 +04:00
|
|
|
Preferences::RegisterCallback(PrefChanged, kHangMonitorPrefName, nullptr);
|
|
|
|
PrefChanged(nullptr, nullptr);
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2013-10-11 00:42:16 +04:00
|
|
|
Preferences::RegisterCallback(PrefChanged, kTelemetryPrefName, nullptr);
|
2012-07-11 03:12:40 +04:00
|
|
|
winMainThreadHandle =
|
|
|
|
OpenThread(THREAD_ALL_ACCESS, FALSE, GetCurrentThreadId());
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!winMainThreadHandle) {
|
2012-07-11 03:12:40 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-10-21 21:48:08 +04:00
|
|
|
gAnnotators = new std::set<Annotator*>();
|
2012-07-11 03:12:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Don't actually start measuring hangs until we hit the main event loop.
|
|
|
|
// This potentially misses a small class of really early startup hangs,
|
|
|
|
// but avoids dealing with some xpcshell tests and other situations which
|
|
|
|
// start XPCOM but don't ever start the event loop.
|
|
|
|
Suspend();
|
|
|
|
|
|
|
|
gThread = PR_CreateThread(PR_USER_THREAD,
|
|
|
|
ThreadMain,
|
2013-10-11 00:42:16 +04:00
|
|
|
nullptr, PR_PRIORITY_LOW, PR_GLOBAL_THREAD,
|
2012-07-11 03:12:40 +04:00
|
|
|
PR_JOINABLE_THREAD, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Shutdown()
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
2012-07-11 03:12:40 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(gMonitor, "Hang monitor not started");
|
2012-07-11 03:12:40 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
// Scope the lock we're going to delete later
|
2012-07-11 03:12:40 +04:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
gShutdown = true;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// thread creation could theoretically fail
|
|
|
|
if (gThread) {
|
|
|
|
PR_JoinThread(gThread);
|
2013-10-11 00:42:16 +04:00
|
|
|
gThread = nullptr;
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete gMonitor;
|
2013-10-11 00:42:16 +04:00
|
|
|
gMonitor = nullptr;
|
2014-10-21 21:48:08 +04:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
// gAnnotators is a StaticAutoPtr, so we just need to null it out.
|
|
|
|
gAnnotators = nullptr;
|
|
|
|
#endif
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsUIMessageWaiting()
|
|
|
|
{
|
|
|
|
#ifndef XP_WIN
|
|
|
|
return false;
|
|
|
|
#else
|
2014-05-27 11:15:35 +04:00
|
|
|
#define NS_WM_IMEFIRST WM_IME_SETCONTEXT
|
|
|
|
#define NS_WM_IMELAST WM_IME_KEYUP
|
2012-07-11 03:12:40 +04:00
|
|
|
BOOL haveUIMessageWaiting = FALSE;
|
|
|
|
MSG msg;
|
2014-05-27 11:15:35 +04:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, WM_KEYFIRST,
|
2012-07-11 03:12:40 +04:00
|
|
|
WM_IME_KEYLAST, PM_NOREMOVE);
|
2013-10-11 00:42:16 +04:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, NS_WM_IMEFIRST,
|
2012-07-11 03:12:40 +04:00
|
|
|
NS_WM_IMELAST, PM_NOREMOVE);
|
2013-10-11 00:42:16 +04:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, WM_MOUSEFIRST,
|
2012-07-11 03:12:40 +04:00
|
|
|
WM_MOUSELAST, PM_NOREMOVE);
|
|
|
|
return haveUIMessageWaiting;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 11:15:35 +04:00
|
|
|
NotifyActivity(ActivityType aActivityType)
|
2012-07-11 03:12:40 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"HangMonitor::Notify called from off the main thread.");
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
// Determine the activity type more specifically
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aActivityType == kGeneralActivity) {
|
|
|
|
aActivityType = IsUIMessageWaiting() ? kActivityUIAVail :
|
|
|
|
kActivityNoUIAVail;
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the cumulative amount of lag time since the last UI message
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t cumulativeUILagMS = 0;
|
2014-05-27 11:15:35 +04:00
|
|
|
switch (aActivityType) {
|
|
|
|
case kActivityNoUIAVail:
|
|
|
|
cumulativeUILagMS = 0;
|
|
|
|
break;
|
|
|
|
case kActivityUIAVail:
|
|
|
|
case kUIActivity:
|
|
|
|
if (gTimestamp != PR_INTERVAL_NO_WAIT) {
|
|
|
|
cumulativeUILagMS += PR_IntervalToMilliseconds(PR_IntervalNow() -
|
|
|
|
gTimestamp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a locked activity because PRTimeStamp is a 32-bit quantity
|
|
|
|
// which can be read/written atomically, and we don't want to pay locking
|
|
|
|
// penalties here.
|
|
|
|
gTimestamp = PR_IntervalNow();
|
|
|
|
|
|
|
|
// If we have UI activity we should reset the timer and report it if it is
|
|
|
|
// significant enough.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aActivityType == kUIActivity) {
|
2012-07-11 03:12:40 +04:00
|
|
|
// The minimum amount of lag time that we should report for telemetry data.
|
|
|
|
// Mozilla's UI responsiveness goal is 50ms
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kUIResponsivenessThresholdMS = 50;
|
2012-07-11 03:12:40 +04:00
|
|
|
if (cumulativeUILagMS > kUIResponsivenessThresholdMS) {
|
|
|
|
mozilla::Telemetry::Accumulate(mozilla::Telemetry::EVENTLOOP_UI_LAG_EXP_MS,
|
|
|
|
cumulativeUILagMS);
|
|
|
|
}
|
|
|
|
cumulativeUILagMS = 0;
|
|
|
|
}
|
2014-01-14 20:33:31 +04:00
|
|
|
|
|
|
|
if (gThread && !gShutdown) {
|
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Suspend()
|
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"HangMonitor::Suspend called from off the main thread.");
|
2012-07-11 03:12:40 +04:00
|
|
|
|
|
|
|
// Because gTimestamp changes this resets the wait count.
|
|
|
|
gTimestamp = PR_INTERVAL_NO_WAIT;
|
2014-01-14 20:33:31 +04:00
|
|
|
|
|
|
|
if (gThread && !gShutdown) {
|
|
|
|
mozilla::BackgroundHangMonitor().NotifyWait();
|
|
|
|
}
|
2012-07-11 03:12:40 +04:00
|
|
|
}
|
|
|
|
|
2014-10-21 21:48:08 +04:00
|
|
|
void
|
|
|
|
RegisterAnnotator(Annotator& aAnnotator)
|
|
|
|
{
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-10-22 22:39:27 +04:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 21:48:08 +04:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
MOZ_ASSERT(gAnnotators);
|
|
|
|
gAnnotators->insert(&aAnnotator);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterAnnotator(Annotator& aAnnotator)
|
|
|
|
{
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-10-22 22:39:27 +04:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 21:48:08 +04:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
MOZ_ASSERT(gAnnotators);
|
|
|
|
gAnnotators->erase(&aAnnotator);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
} // namespace HangMonitor
|
|
|
|
} // namespace mozilla
|