Bug 1765226 - Make ProfilerIOInterposeObserver a static object - r=florian

This removes some memory operations, and extends the observer's life.

Differential Revision: https://phabricator.services.mozilla.com/D144176
This commit is contained in:
Gerald Squelart 2022-04-20 23:39:21 +00:00
parent d58e1b92f7
commit c1b8cfb651
3 changed files with 36 additions and 20 deletions

View File

@ -501,7 +501,6 @@ class CorePS {
// is worthwhile:
// - CorePS::mRegisteredPages itself (its elements' children are
// measured above)
// - CorePS::mInterposeObserver
#if defined(USE_LUL_STACKWALK)
if (lul::LUL* lulPtr = sInstance->mLul; lulPtr) {
@ -736,6 +735,12 @@ class ActivePS {
return aFeatures;
}
bool ShouldInterposeIOs() {
return ProfilerFeature::HasMainThreadIO(mFeatures) ||
ProfilerFeature::HasFileIO(mFeatures) ||
ProfilerFeature::HasFileIOAll(mFeatures);
}
ActivePS(
PSLockRef aLock, PowerOfTwo32 aCapacity, double aInterval,
uint32_t aFeatures, const char** aFilters, uint32_t aFilterCount,
@ -767,11 +772,6 @@ class ActivePS {
// unlocks gPSMutex.
mSamplerThread(
NewSamplerThread(aLock, mGeneration, aInterval, aFeatures)),
mInterposeObserver((ProfilerFeature::HasMainThreadIO(aFeatures) ||
ProfilerFeature::HasFileIO(aFeatures) ||
ProfilerFeature::HasFileIOAll(aFeatures))
? new ProfilerIOInterposeObserver()
: nullptr),
mIsPaused(false),
mIsSamplingPaused(false) {
// Deep copy and lower-case aFilters.
@ -785,20 +785,29 @@ class ActivePS {
}
#if !defined(RELEASE_OR_BETA)
if (mInterposeObserver) {
if (ShouldInterposeIOs()) {
// We need to register the observer on the main thread, because we want
// to observe IO that happens on the main thread.
// IOInterposer needs to be initialized before calling
// IOInterposer::Register or our observer will be silently dropped.
if (NS_IsMainThread()) {
IOInterposer::Init();
IOInterposer::Register(IOInterposeObserver::OpAll, mInterposeObserver);
IOInterposer::Register(IOInterposeObserver::OpAll,
&ProfilerIOInterposeObserver::GetInstance());
} else {
RefPtr<ProfilerIOInterposeObserver> observer = mInterposeObserver;
NS_DispatchToMainThread(
NS_NewRunnableFunction("ActivePS::ActivePS", [=]() {
NS_NewRunnableFunction("ActivePS::ActivePS", []() {
// Note: This could theoretically happen after ActivePS gets
// destroyed, but it's ok:
// - The Observer always checks that the profiler is (still)
// active before doing its work.
// - The destruction should happen on the same thread as this
// construction, so the un-registration will also be dispatched
// and queued on the main thread, and run after this.
IOInterposer::Init();
IOInterposer::Register(IOInterposeObserver::OpAll, observer);
IOInterposer::Register(
IOInterposeObserver::OpAll,
&ProfilerIOInterposeObserver::GetInstance());
}));
}
}
@ -810,17 +819,18 @@ class ActivePS {
!mMaybeProcessCPUCounter,
"mMaybeProcessCPUCounter should have been deleted before ~ActivePS()");
#if !defined(RELEASE_OR_BETA)
if (mInterposeObserver) {
if (ShouldInterposeIOs()) {
// We need to unregister the observer on the main thread, because that's
// where we've registered it.
if (NS_IsMainThread()) {
IOInterposer::Unregister(IOInterposeObserver::OpAll,
mInterposeObserver);
&ProfilerIOInterposeObserver::GetInstance());
} else {
RefPtr<ProfilerIOInterposeObserver> observer = mInterposeObserver;
NS_DispatchToMainThread(
NS_NewRunnableFunction("ActivePS::~ActivePS", [=]() {
IOInterposer::Unregister(IOInterposeObserver::OpAll, observer);
NS_NewRunnableFunction("ActivePS::~ActivePS", []() {
IOInterposer::Unregister(
IOInterposeObserver::OpAll,
&ProfilerIOInterposeObserver::GetInstance());
}));
}
}
@ -1414,9 +1424,6 @@ class ActivePS {
// can destroy it.
SamplerThread* const mSamplerThread;
// The interposer that records main thread I/O.
RefPtr<ProfilerIOInterposeObserver> mInterposeObserver;
// Is the profiler fully paused?
bool mIsPaused;

View File

@ -7,6 +7,12 @@
using namespace mozilla;
/* static */
ProfilerIOInterposeObserver& ProfilerIOInterposeObserver::GetInstance() {
static ProfilerIOInterposeObserver sProfilerIOInterposeObserver;
return sProfilerIOInterposeObserver;
}
namespace geckoprofiler::markers {
struct FileIOMarker {
static constexpr Span<const char> MarkerTypeName() {

View File

@ -18,9 +18,12 @@ class ProfilerIOInterposeObserver final : public IOInterposeObserver {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ProfilerIOInterposeObserver)
public:
static ProfilerIOInterposeObserver& GetInstance();
virtual void Observe(Observation& aObservation) override;
protected:
private:
ProfilerIOInterposeObserver() = default;
virtual ~ProfilerIOInterposeObserver() {}
};