Issue #21 - Remove Telemetry plumbing and fix build.

Note this won't give working applications. Requires FE changes and
additional js module changes (next part).
This commit is contained in:
Moonchild 2021-10-29 11:02:27 +00:00 committed by roytam1
commit 8d800b1cb0
205 changed files with 45 additions and 40519 deletions

View file

@ -10,13 +10,10 @@
#include "mozilla/Move.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/ThreadHangStats.h"
#include "mozilla/ThreadLocal.h"
#include "prinrval.h"
#include "prthread.h"
#include "ThreadStackHelper.h"
#include "nsIObserverService.h"
#include "nsIObserver.h"
#include "mozilla/Services.h"
@ -30,7 +27,7 @@
#define BHR_BETA_MOD 1;
// Maximum depth of the call stack in the reported thread hangs. This value represents
// the 99.9th percentile of the thread hangs stack depths reported by Telemetry.
// the 99.9th percentile of the thread hangs stack depths reported.
static const size_t kMaxThreadHangStackDepth = 30;
// An utility comparator function used by std::unique to collapse "(* script)" entries in
@ -174,12 +171,6 @@ public:
bool mWaiting;
// Is the thread dedicated to a single BackgroundHangMonitor
BackgroundHangMonitor::ThreadType mThreadType;
// Platform-specific helper to get hang stacks
ThreadStackHelper mStackHelper;
// Stack of current hang
Telemetry::HangStack mHangStack;
// Statistics for telemetry
Telemetry::ThreadHangStats mStats;
// Annotations for the current hang
UniquePtr<HangMonitor::HangAnnotations> mAnnotations;
// Annotators registered for this thread
@ -190,10 +181,6 @@ public:
uint32_t aMaxTimeoutMs,
BackgroundHangMonitor::ThreadType aThreadType = BackgroundHangMonitor::THREAD_SHARED);
// Report a hang; aManager->mLock IS locked
Telemetry::HangHistogram& ReportHang(PRIntervalTime aHangTime);
// Report a permanent hang; aManager->mLock IS locked
void ReportPermaHang();
// Called by BackgroundHangMonitor::NotifyActivity
void NotifyActivity()
{
@ -325,14 +312,12 @@ BackgroundHangManager::RunMonitorThread()
// Skip subsequent iterations and tolerate a race on mWaiting here
currentThread->mWaiting = true;
currentThread->mHanging = false;
currentThread->ReportPermaHang();
continue;
}
if (MOZ_LIKELY(!currentThread->mHanging)) {
if (MOZ_UNLIKELY(hangTime >= currentThread->mTimeout)) {
// A hang started
currentThread->mStackHelper.GetStack(currentThread->mHangStack);
currentThread->mHangStart = interval;
currentThread->mHanging = true;
currentThread->mAnnotations =
@ -341,7 +326,6 @@ BackgroundHangManager::RunMonitorThread()
} else {
if (MOZ_LIKELY(interval != currentThread->mHangStart)) {
// A hang ended
currentThread->ReportHang(intervalNow - currentThread->mHangStart);
currentThread->mHanging = false;
}
}
@ -390,7 +374,6 @@ BackgroundHangThread::BackgroundHangThread(const char* aName,
, mHanging(false)
, mWaiting(true)
, mThreadType(aThreadType)
, mStats(aName)
{
if (sTlsKeyInitialized && IsShared()) {
sTlsKey.set(this);
@ -416,69 +399,6 @@ BackgroundHangThread::~BackgroundHangThread()
if (sTlsKeyInitialized && IsShared()) {
sTlsKey.set(nullptr);
}
// Move our copy of ThreadHangStats to Telemetry storage
Telemetry::RecordThreadHangStats(mStats);
}
Telemetry::HangHistogram&
BackgroundHangThread::ReportHang(PRIntervalTime aHangTime)
{
// Recovered from a hang; called on the monitor thread
// mManager->mLock IS locked
// Remove unwanted "js::RunScript" frame from the stack
for (size_t i = 0; i < mHangStack.length(); ) {
const char** f = mHangStack.begin() + i;
if (!mHangStack.IsInBuffer(*f) && !strcmp(*f, "js::RunScript")) {
mHangStack.erase(f);
} else {
i++;
}
}
// Collapse duplicated "(chrome script)" and "(content script)" entries in the stack.
auto it = std::unique(mHangStack.begin(), mHangStack.end(), StackScriptEntriesCollapser);
mHangStack.erase(it, mHangStack.end());
// Limit the depth of the reported stack if greater than our limit. Only keep its
// last entries, since the most recent frames are at the end of the vector.
if (mHangStack.length() > kMaxThreadHangStackDepth) {
const int elementsToRemove = mHangStack.length() - kMaxThreadHangStackDepth;
// Replace the oldest frame with a known label so that we can tell this stack
// was limited.
mHangStack[0] = "(reduced stack)";
mHangStack.erase(mHangStack.begin() + 1, mHangStack.begin() + elementsToRemove);
}
Telemetry::HangHistogram newHistogram(Move(mHangStack));
for (Telemetry::HangHistogram* oldHistogram = mStats.mHangs.begin();
oldHistogram != mStats.mHangs.end(); oldHistogram++) {
if (newHistogram == *oldHistogram) {
// New histogram matches old one
oldHistogram->Add(aHangTime, Move(mAnnotations));
return *oldHistogram;
}
}
// Add new histogram
newHistogram.Add(aHangTime, Move(mAnnotations));
if (!mStats.mHangs.append(Move(newHistogram))) {
MOZ_CRASH();
}
return mStats.mHangs.back();
}
void
BackgroundHangThread::ReportPermaHang()
{
// Permanently hanged; called on the monitor thread
// mManager->mLock IS locked
Telemetry::HangHistogram& hang = ReportHang(mMaxTimeout);
Telemetry::HangStack& stack = hang.GetNativeStack();
if (stack.empty()) {
mStackHelper.GetNativeStack(stack);
}
}
MOZ_ALWAYS_INLINE void
@ -493,7 +413,6 @@ BackgroundHangThread::Update()
mManager->Wakeup();
} else {
PRIntervalTime duration = intervalNow - mInterval;
mStats.mActivity.Add(duration);
if (MOZ_UNLIKELY(duration >= mTimeout)) {
/* Wake up the manager thread to tell it that a hang ended */
mManager->Wakeup();
@ -552,19 +471,12 @@ BackgroundHangMonitor::IsDisabled() {
bool
BackgroundHangMonitor::DisableOnBeta() {
nsAdoptingCString clientID = Preferences::GetCString("toolkit.telemetry.cachedClientID");
bool telemetryEnabled = Preferences::GetBool("toolkit.telemetry.enabled");
if (!telemetryEnabled || !clientID || BackgroundHangMonitor::ShouldDisableOnBeta(clientID)) {
if (XRE_IsParentProcess()) {
BackgroundHangMonitor::Shutdown();
} else {
BackgroundHangManager::sDisabled = true;
}
return true;
if (XRE_IsParentProcess()) {
BackgroundHangMonitor::Shutdown();
} else {
BackgroundHangManager::sDisabled = true;
}
return false;
return true;
}
void
@ -575,7 +487,6 @@ BackgroundHangMonitor::Startup()
if (!strcmp(NS_STRINGIFY(MOZ_UPDATE_CHANNEL), "beta")) {
if (XRE_IsParentProcess()) { // cached ClientID hasn't been read yet
ThreadStackHelper::Startup();
BackgroundHangThread::Startup();
BackgroundHangManager::sInstance = new BackgroundHangManager();
@ -589,7 +500,6 @@ BackgroundHangMonitor::Startup()
}
}
ThreadStackHelper::Startup();
BackgroundHangThread::Startup();
BackgroundHangManager::sInstance = new BackgroundHangManager();
#endif
@ -610,7 +520,6 @@ BackgroundHangMonitor::Shutdown()
we don't want to hold the lock when it's being destroyed. */
BackgroundHangManager::sInstance->Shutdown();
BackgroundHangManager::sInstance = nullptr;
ThreadStackHelper::Shutdown();
BackgroundHangManager::sDisabled = true;
#endif
}
@ -652,11 +561,8 @@ BackgroundHangMonitor::NotifyActivity()
"This thread is not initialized for hang monitoring");
return;
}
if (Telemetry::CanRecordExtended()) {
mThread->NotifyActivity();
}
#endif
// STUB
}
void
@ -668,11 +574,8 @@ BackgroundHangMonitor::NotifyWait()
"This thread is not initialized for hang monitoring");
return;
}
if (Telemetry::CanRecordExtended()) {
mThread->NotifyWait();
}
#endif
// STUB
}
bool
@ -719,15 +622,4 @@ BackgroundHangMonitor::ThreadHangStatsIterator::ThreadHangStatsIterator()
#endif
}
Telemetry::ThreadHangStats*
BackgroundHangMonitor::ThreadHangStatsIterator::GetNext()
{
if (!mThread) {
return nullptr;
}
Telemetry::ThreadHangStats* stats = &mThread->mStats;
mThread = mThread->getNext();
return stats;
}
} // namespace mozilla

View file

@ -9,8 +9,6 @@
#include "mozilla/BackgroundHangMonitor.h"
#include "mozilla/Monitor.h"
#include "mozilla/Preferences.h"
#include "mozilla/ProcessedStack.h"
#include "mozilla/Telemetry.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "nsReadableUtils.h"

View file

@ -1,225 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "ThreadStackHelper.h"
#include "MainThreadUtils.h"
#include "nsJSPrincipals.h"
#include "nsScriptSecurityManager.h"
#include "jsfriendapi.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Move.h"
#include "mozilla/Scoped.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/MemoryChecking.h"
#include "mozilla/Sprintf.h"
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wshadow"
#endif
#if defined(MOZ_VALGRIND)
# include <valgrind/valgrind.h>
#endif
#include <string.h>
#include <vector>
#include <cstdlib>
#ifdef XP_LINUX
#include <ucontext.h>
#include <unistd.h>
#include <sys/syscall.h>
#endif
#ifdef __GNUC__
# pragma GCC diagnostic pop // -Wshadow
#endif
#if defined(XP_LINUX)
#include <pthread.h>
#endif
namespace mozilla {
void
ThreadStackHelper::Startup()
{
#if defined(XP_LINUX)
MOZ_ASSERT(NS_IsMainThread());
if (!sInitialized) {
// TODO: centralize signal number allocation
sFillStackSignum = SIGRTMIN + 4;
if (sFillStackSignum > SIGRTMAX) {
// Leave uninitialized
MOZ_ASSERT(false);
return;
}
struct sigaction sigact = {};
sigact.sa_sigaction = FillStackHandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO | SA_RESTART;
MOZ_ALWAYS_TRUE(!::sigaction(sFillStackSignum, &sigact, nullptr));
}
sInitialized++;
#endif
}
void
ThreadStackHelper::Shutdown()
{
#if defined(XP_LINUX)
MOZ_ASSERT(NS_IsMainThread());
if (sInitialized == 1) {
struct sigaction sigact = {};
sigact.sa_handler = SIG_DFL;
MOZ_ALWAYS_TRUE(!::sigaction(sFillStackSignum, &sigact, nullptr));
}
sInitialized--;
#endif
}
ThreadStackHelper::ThreadStackHelper()
: mStackToFill(nullptr)
{
#if defined(XP_LINUX)
MOZ_ALWAYS_TRUE(!::sem_init(&mSem, 0, 0));
mThreadID = ::syscall(SYS_gettid);
#elif defined(XP_WIN)
mInitialized = !!::DuplicateHandle(
::GetCurrentProcess(), ::GetCurrentThread(),
::GetCurrentProcess(), &mThreadID,
THREAD_SUSPEND_RESUME
, FALSE, 0);
MOZ_ASSERT(mInitialized);
#endif
}
ThreadStackHelper::~ThreadStackHelper()
{
#if defined(XP_LINUX)
MOZ_ALWAYS_TRUE(!::sem_destroy(&mSem));
#elif defined(XP_WIN)
if (mInitialized) {
MOZ_ALWAYS_TRUE(!!::CloseHandle(mThreadID));
}
#endif
}
namespace {
template<typename T>
class ScopedSetPtr
{
private:
T*& mPtr;
public:
ScopedSetPtr(T*& p, T* val) : mPtr(p) { mPtr = val; }
~ScopedSetPtr() { mPtr = nullptr; }
};
} // namespace
void
ThreadStackHelper::GetStack(Stack& aStack)
{
// Always run PrepareStackBuffer first to clear aStack
if (!PrepareStackBuffer(aStack)) {
// Skip and return empty aStack
return;
}
ScopedSetPtr<Stack> stackPtr(mStackToFill, &aStack);
#if defined(XP_LINUX)
if (!sInitialized) {
MOZ_ASSERT(false);
return;
}
siginfo_t uinfo = {};
uinfo.si_signo = sFillStackSignum;
uinfo.si_code = SI_QUEUE;
uinfo.si_pid = getpid();
uinfo.si_uid = getuid();
uinfo.si_value.sival_ptr = this;
if (::syscall(SYS_rt_tgsigqueueinfo, uinfo.si_pid,
mThreadID, sFillStackSignum, &uinfo)) {
// rt_tgsigqueueinfo was added in Linux 2.6.31.
// Could have failed because the syscall did not exist.
return;
}
MOZ_ALWAYS_TRUE(!::sem_wait(&mSem));
#elif defined(XP_WIN)
if (!mInitialized) {
MOZ_ASSERT(false);
return;
}
if (::SuspendThread(mThreadID) == DWORD(-1)) {
MOZ_ASSERT(false);
return;
}
// SuspendThread is asynchronous, so the thread may still be running. Use
// GetThreadContext to ensure it's really suspended.
// See https://blogs.msdn.microsoft.com/oldnewthing/20150205-00/?p=44743.
CONTEXT context;
context.ContextFlags = CONTEXT_CONTROL;
if (::GetThreadContext(mThreadID, &context)) {
FillStackBuffer();
FillThreadContext();
}
MOZ_ALWAYS_TRUE(::ResumeThread(mThreadID) != DWORD(-1));
#endif
}
void
ThreadStackHelper::GetNativeStack(Stack& aStack)
{
/*** STUB ***/
}
#ifdef XP_LINUX
int ThreadStackHelper::sInitialized;
int ThreadStackHelper::sFillStackSignum;
void
ThreadStackHelper::FillStackHandler(int aSignal, siginfo_t* aInfo,
void* aContext)
{
ThreadStackHelper* const helper =
reinterpret_cast<ThreadStackHelper*>(aInfo->si_value.sival_ptr);
helper->FillStackBuffer();
helper->FillThreadContext(aContext);
::sem_post(&helper->mSem);
}
#endif // XP_LINUX
bool
ThreadStackHelper::PrepareStackBuffer(Stack& aStack)
{
// Return false to skip getting the stack and return an empty stack
aStack.clear();
return false;
}
void
ThreadStackHelper::FillStackBuffer()
{
MOZ_ASSERT(mStackToFill->empty());
/*** STUB ***/
}
MOZ_ASAN_BLACKLIST void
ThreadStackHelper::FillThreadContext(void* aContext)
{
/*** STUB ***/
}
} // namespace mozilla

View file

@ -1,100 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef mozilla_ThreadStackHelper_h
#define mozilla_ThreadStackHelper_h
#include "mozilla/ThreadHangStats.h"
#include "GeckoProfiler.h"
#include <stddef.h>
#if defined(XP_LINUX)
#include <signal.h>
#include <semaphore.h>
#include <sys/types.h>
#elif defined(XP_WIN)
#include <windows.h>
#endif
namespace mozilla {
/**
* ThreadStackHelper is used to retrieve the profiler pseudo-stack of a
* thread, as an alternative of using the profiler to take a profile.
* The target thread first declares an ThreadStackHelper instance;
* then another thread can call ThreadStackHelper::GetStack to retrieve
* the pseudo-stack of the target thread at that instant.
*
* Only non-copying labels are included in the stack, which means labels
* with custom text and markers are not included.
*/
class ThreadStackHelper
{
public:
typedef Telemetry::HangStack Stack;
private:
Stack* mStackToFill;
bool PrepareStackBuffer(Stack& aStack);
void FillStackBuffer();
void FillThreadContext(void* aContext = nullptr);
public:
/**
* Initialize ThreadStackHelper. Must be called from main thread.
*/
static void Startup();
/**
* Uninitialize ThreadStackHelper. Must be called from main thread.
*/
static void Shutdown();
/**
* Create a ThreadStackHelper instance targeting the current thread.
*/
ThreadStackHelper();
~ThreadStackHelper();
/**
* Retrieve the current pseudostack of the thread associated
* with this ThreadStackHelper.
*
* @param aStack Stack instance to be filled.
*/
void GetStack(Stack& aStack);
/**
* Retrieve the current native stack of the thread associated
* with this ThreadStackHelper.
*
* @param aNativeStack Stack instance to be filled.
*/
void GetNativeStack(Stack& aStack);
#if defined(XP_LINUX)
private:
static int sInitialized;
static int sFillStackSignum;
static void FillStackHandler(int aSignal, siginfo_t* aInfo, void* aContext);
sem_t mSem;
pid_t mThreadID;
#elif defined(XP_WIN)
private:
bool mInitialized;
HANDLE mThreadID;
#endif
};
} // namespace mozilla
#endif // mozilla_ThreadStackHelper_h

View file

@ -62,7 +62,6 @@ UNIFIED_SOURCES += [
'nsTimerImpl.cpp',
'SharedThreadPool.cpp',
'TaskQueue.cpp',
'ThreadStackHelper.cpp',
'ThrottledEventQueue.cpp',
'TimerThread.cpp',
]