2012-07-10 16:12:40 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-06-30 08:39:45 -07:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-07-10 16:12:40 -07: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 10:48:08 -07:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2014-01-14 08:33:31 -08:00
|
|
|
#include "mozilla/BackgroundHangMonitor.h"
|
2012-07-10 16:12:40 -07:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2012-08-21 14:14:38 -07:00
|
|
|
#include "mozilla/ProcessedStack.h"
|
2013-10-28 16:29:00 -07:00
|
|
|
#include "mozilla/Atomics.h"
|
2014-10-21 11:18:27 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-10-21 10:48:08 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2012-07-10 16:12:40 -07:00
|
|
|
#include "nsStackWalk.h"
|
2014-10-21 10:48:08 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2012-07-10 16:12:40 -07: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 00:15:35 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace HangMonitor {
|
2012-07-10 16:12:40 -07: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 10:55:29 -07:00
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2012-07-10 16:12:40 -07:00
|
|
|
const char kTelemetryPrefName[] = "toolkit.telemetry.enabled";
|
2014-05-09 10:55:29 -07:00
|
|
|
#endif
|
2012-07-10 16:12:40 -07: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 08:56:38 -07:00
|
|
|
int32_t gTimeout;
|
2012-07-10 16:12:40 -07: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-28 16:29:00 -07:00
|
|
|
Atomic<PRIntervalTime> gTimestamp(PR_INTERVAL_NO_WAIT);
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
// Main thread ID used in reporting chrome hangs under Windows
|
2013-10-10 13:42:16 -07:00
|
|
|
static HANDLE winMainThreadHandle = nullptr;
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
// Default timeout for reporting chrome hangs to Telemetry (5 seconds)
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t DEFAULT_CHROME_HANG_INTERVAL = 5;
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
// Maximum number of PCs to gather from the stack
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t MAX_CALL_STACK_PCS = 400;
|
2014-10-21 10:48:08 -07:00
|
|
|
|
|
|
|
// Chrome hang annotators
|
|
|
|
static StaticAutoPtr<std::set<Annotator*>> gAnnotators;
|
2012-07-10 16:12:40 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// PrefChangedFunc
|
2013-12-10 15:10:01 -08:00
|
|
|
void
|
2012-07-10 16:12:40 -07:00
|
|
|
PrefChanged(const char*, void*)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t newval = Preferences::GetInt(kHangMonitorPrefName);
|
2012-07-10 16:12:40 -07: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 10:48:08 -07: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-10 16:12:40 -07:00
|
|
|
static void
|
2014-09-10 21:47:01 -07:00
|
|
|
ChromeStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP, void* aClosure)
|
2012-07-10 16:12:40 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aClosure);
|
2014-05-27 00:15:35 -07:00
|
|
|
std::vector<uintptr_t>* stack =
|
2012-08-21 14:14:38 -07:00
|
|
|
static_cast<std::vector<uintptr_t>*>(aClosure);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (stack->size() == MAX_CALL_STACK_PCS) {
|
2012-08-21 14:14:38 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-08-21 14:14:38 -07:00
|
|
|
MOZ_ASSERT(stack->size() < MAX_CALL_STACK_PCS);
|
|
|
|
stack->push_back(reinterpret_cast<uintptr_t>(aPC));
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-27 00:15:35 -07:00
|
|
|
GetChromeHangReport(Telemetry::ProcessedStack& aStack,
|
|
|
|
int32_t& aSystemUptime,
|
|
|
|
int32_t& aFirefoxUptime)
|
2012-07-10 16:12:40 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(winMainThreadHandle);
|
|
|
|
|
|
|
|
// The thread we're about to suspend might have the alloc lock
|
|
|
|
// so allocate ahead of time
|
2012-08-21 14:14:38 -07:00
|
|
|
std::vector<uintptr_t> rawStack;
|
|
|
|
rawStack.reserve(MAX_CALL_STACK_PCS);
|
2012-07-10 16:12:40 -07:00
|
|
|
DWORD ret = ::SuspendThread(winMainThreadHandle);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (ret == -1) {
|
2012-07-10 16:12:40 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-12-20 21:31:57 -08:00
|
|
|
NS_StackWalk(ChromeStackWalker, /* skipFrames */ 0, /* maxFrames */ 0,
|
|
|
|
reinterpret_cast<void*>(&rawStack),
|
2012-12-07 21:15:21 -08:00
|
|
|
reinterpret_cast<uintptr_t>(winMainThreadHandle), nullptr);
|
2012-07-10 16:12:40 -07:00
|
|
|
ret = ::ResumeThread(winMainThreadHandle);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (ret == -1) {
|
2012-07-10 16:12:40 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-11-20 06:45:14 -08:00
|
|
|
aStack = Telemetry::GetStackAndModules(rawStack);
|
2014-01-27 13:47:00 -08: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-10 16:12:40 -07:00
|
|
|
}
|
2014-10-21 10:48:08 -07: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-10 16:12:40 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadMain(void*)
|
|
|
|
{
|
2012-06-12 10:06:20 -07:00
|
|
|
PR_SetCurrentThreadName("Hang Monitor");
|
|
|
|
|
2012-07-10 16:12:40 -07: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-21 14:14:38 -07:00
|
|
|
Telemetry::ProcessedStack stack;
|
2014-01-27 13:47:00 -08:00
|
|
|
int32_t systemUptime = -1;
|
|
|
|
int32_t firefoxUptime = -1;
|
2014-10-21 10:48:08 -07:00
|
|
|
nsAutoPtr<ChromeHangAnnotations> annotations = new ChromeHangAnnotations();
|
2012-07-10 16:12:40 -07: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-27 13:47:00 -08: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 10:48:08 -07:00
|
|
|
ChromeHangAnnotatorCallout(*annotations);
|
2014-01-27 13:47:00 -08:00
|
|
|
}
|
2012-07-10 16:12:40 -07:00
|
|
|
#else
|
2014-01-27 13:47:00 -08:00
|
|
|
// This is the crash-on-hang feature.
|
|
|
|
// See bug 867313 for the quirk in the waitCount comparison
|
|
|
|
if (waitCount >= 2) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t delay =
|
|
|
|
int32_t(PR_IntervalToSeconds(now - timestamp));
|
2013-04-30 15:39:16 -07:00
|
|
|
if (delay >= gTimeout) {
|
2012-07-10 16:12:40 -07:00
|
|
|
MonitorAutoUnlock unlock(*gMonitor);
|
|
|
|
Crash();
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 13:47:00 -08:00
|
|
|
#endif
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2012-07-10 16:12:40 -07:00
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
if (waitCount >= 2) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t hangDuration = PR_IntervalToSeconds(now - lastTimestamp);
|
2014-10-21 10:48:08 -07:00
|
|
|
Telemetry::RecordChromeHang(hangDuration, stack, systemUptime,
|
|
|
|
firefoxUptime, annotations->IsEmpty() ?
|
|
|
|
nullptr : annotations.forget());
|
2012-08-21 14:14:38 -07:00
|
|
|
stack.Clear();
|
2014-10-21 10:48:08 -07:00
|
|
|
annotations = new ChromeHangAnnotations();
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
waitCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntervalTime timeout;
|
|
|
|
if (gTimeout <= 0) {
|
|
|
|
timeout = PR_INTERVAL_NO_TIMEOUT;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2012-07-10 16:12:40 -07: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 00:15:35 -07:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
2012-07-10 16:12:40 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-07-10 16:12:40 -07:00
|
|
|
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(!gMonitor, "Hang monitor already initialized");
|
2012-07-10 16:12:40 -07:00
|
|
|
gMonitor = new Monitor("HangMonitor");
|
|
|
|
|
2013-10-10 13:42:16 -07:00
|
|
|
Preferences::RegisterCallback(PrefChanged, kHangMonitorPrefName, nullptr);
|
|
|
|
PrefChanged(nullptr, nullptr);
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2013-10-10 13:42:16 -07:00
|
|
|
Preferences::RegisterCallback(PrefChanged, kTelemetryPrefName, nullptr);
|
2012-07-10 16:12:40 -07:00
|
|
|
winMainThreadHandle =
|
|
|
|
OpenThread(THREAD_ALL_ACCESS, FALSE, GetCurrentThreadId());
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!winMainThreadHandle) {
|
2012-07-10 16:12:40 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-10-21 10:48:08 -07:00
|
|
|
gAnnotators = new std::set<Annotator*>();
|
2012-07-10 16:12:40 -07: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-10 13:42:16 -07:00
|
|
|
nullptr, PR_PRIORITY_LOW, PR_GLOBAL_THREAD,
|
2012-07-10 16:12:40 -07:00
|
|
|
PR_JOINABLE_THREAD, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Shutdown()
|
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
2012-07-10 16:12:40 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-07-10 16:12:40 -07:00
|
|
|
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(gMonitor, "Hang monitor not started");
|
2012-07-10 16:12:40 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
{
|
|
|
|
// Scope the lock we're going to delete later
|
2012-07-10 16:12:40 -07:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
gShutdown = true;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// thread creation could theoretically fail
|
|
|
|
if (gThread) {
|
|
|
|
PR_JoinThread(gThread);
|
2013-10-10 13:42:16 -07:00
|
|
|
gThread = nullptr;
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
delete gMonitor;
|
2013-10-10 13:42:16 -07:00
|
|
|
gMonitor = nullptr;
|
2014-10-21 10:48:08 -07:00
|
|
|
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
|
|
|
// gAnnotators is a StaticAutoPtr, so we just need to null it out.
|
|
|
|
gAnnotators = nullptr;
|
|
|
|
#endif
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsUIMessageWaiting()
|
|
|
|
{
|
|
|
|
#ifndef XP_WIN
|
|
|
|
return false;
|
|
|
|
#else
|
2014-05-27 00:15:35 -07:00
|
|
|
#define NS_WM_IMEFIRST WM_IME_SETCONTEXT
|
|
|
|
#define NS_WM_IMELAST WM_IME_KEYUP
|
2012-07-10 16:12:40 -07:00
|
|
|
BOOL haveUIMessageWaiting = FALSE;
|
|
|
|
MSG msg;
|
2014-05-27 00:15:35 -07:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, WM_KEYFIRST,
|
2012-07-10 16:12:40 -07:00
|
|
|
WM_IME_KEYLAST, PM_NOREMOVE);
|
2013-10-10 13:42:16 -07:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, NS_WM_IMEFIRST,
|
2012-07-10 16:12:40 -07:00
|
|
|
NS_WM_IMELAST, PM_NOREMOVE);
|
2013-10-10 13:42:16 -07:00
|
|
|
haveUIMessageWaiting |= ::PeekMessageW(&msg, nullptr, WM_MOUSEFIRST,
|
2012-07-10 16:12:40 -07:00
|
|
|
WM_MOUSELAST, PM_NOREMOVE);
|
|
|
|
return haveUIMessageWaiting;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
NotifyActivity(ActivityType aActivityType)
|
2012-07-10 16:12:40 -07:00
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"HangMonitor::Notify called from off the main thread.");
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
// Determine the activity type more specifically
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aActivityType == kGeneralActivity) {
|
|
|
|
aActivityType = IsUIMessageWaiting() ? kActivityUIAVail :
|
|
|
|
kActivityNoUIAVail;
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the cumulative amount of lag time since the last UI message
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t cumulativeUILagMS = 0;
|
2014-05-27 00:15:35 -07: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-10 16:12:40 -07: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 00:15:35 -07:00
|
|
|
if (aActivityType == kUIActivity) {
|
2012-07-10 16:12:40 -07:00
|
|
|
// The minimum amount of lag time that we should report for telemetry data.
|
|
|
|
// Mozilla's UI responsiveness goal is 50ms
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t kUIResponsivenessThresholdMS = 50;
|
2012-07-10 16:12:40 -07:00
|
|
|
if (cumulativeUILagMS > kUIResponsivenessThresholdMS) {
|
|
|
|
mozilla::Telemetry::Accumulate(mozilla::Telemetry::EVENTLOOP_UI_LAG_EXP_MS,
|
|
|
|
cumulativeUILagMS);
|
|
|
|
}
|
|
|
|
cumulativeUILagMS = 0;
|
|
|
|
}
|
2014-01-14 08:33:31 -08:00
|
|
|
|
|
|
|
if (gThread && !gShutdown) {
|
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
|
|
|
}
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Suspend()
|
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"HangMonitor::Suspend called from off the main thread.");
|
2012-07-10 16:12:40 -07:00
|
|
|
|
|
|
|
// Because gTimestamp changes this resets the wait count.
|
|
|
|
gTimestamp = PR_INTERVAL_NO_WAIT;
|
2014-01-14 08:33:31 -08:00
|
|
|
|
|
|
|
if (gThread && !gShutdown) {
|
|
|
|
mozilla::BackgroundHangMonitor().NotifyWait();
|
|
|
|
}
|
2012-07-10 16:12:40 -07:00
|
|
|
}
|
|
|
|
|
2014-10-21 10:48:08 -07:00
|
|
|
void
|
|
|
|
RegisterAnnotator(Annotator& aAnnotator)
|
|
|
|
{
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-10-22 11:39:27 -07:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 10:48:08 -07:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
MOZ_ASSERT(gAnnotators);
|
|
|
|
gAnnotators->insert(&aAnnotator);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterAnnotator(Annotator& aAnnotator)
|
|
|
|
{
|
|
|
|
#ifdef REPORT_CHROME_HANGS
|
2014-10-22 11:39:27 -07:00
|
|
|
if (GeckoProcessType_Default != XRE_GetProcessType()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 10:48:08 -07:00
|
|
|
MonitorAutoLock lock(*gMonitor);
|
|
|
|
MOZ_ASSERT(gAnnotators);
|
|
|
|
gAnnotators->erase(&aAnnotator);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
} // namespace HangMonitor
|
|
|
|
} // namespace mozilla
|