From 74485a5d46f98ec317d13f126a796e79d8c49ee4 Mon Sep 17 00:00:00 2001 From: Gerald Squelart Date: Thu, 22 Aug 2019 00:33:11 +0000 Subject: [PATCH] Bug 1575453 - AUTO_PROFILER_STATS (off by default) - r=gregtatum AUTO_PROFILER_STATS(name) can be used to time a {block}. Statistics are gathered in a function-static variable, and printf'd when the program ends. Differential Revision: https://phabricator.services.mozilla.com/D42825 --HG-- extra : moz-landing-system : lando --- mozglue/baseprofiler/public/BaseProfiler.h | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/mozglue/baseprofiler/public/BaseProfiler.h b/mozglue/baseprofiler/public/BaseProfiler.h index 335d307dacf8..b86eb417061b 100644 --- a/mozglue/baseprofiler/public/BaseProfiler.h +++ b/mozglue/baseprofiler/public/BaseProfiler.h @@ -78,6 +78,8 @@ # define AUTO_BASE_PROFILER_TEXT_MARKER_DOCSHELL_CAUSE( \ markerName, text, categoryPair, docShell, cause) +# define AUTO_PROFILER_STATS(name) + #else // !MOZ_BASE_PROFILER # include "BaseProfilingStack.h" @@ -528,6 +530,62 @@ struct ProfilerBufferInfo { // buffer is being written to, and how much data is visible. MFBT_API Maybe profiler_get_buffer_info(); +// Uncomment the following line to display profiler runtime statistics at +// shutdown. +// # define PROFILER_RUNTIME_STATS + +# ifdef PROFILER_RUNTIME_STATS +struct StaticBaseProfilerStats { + explicit StaticBaseProfilerStats(const char* aName) : mName(aName) {} + ~StaticBaseProfilerStats() { + long long n = static_cast(mNumberDurations); + if (n != 0) { + long long sumNs = static_cast(mSumDurationsNs); + printf("Profiler stats `%s`: %lld ns / %lld = %lld ns\n", mName, sumNs, n, + sumNs / n); + } else { + printf("Profiler stats `%s`: (nothing)\n", mName); + } + } + Atomic mSumDurationsNs{0}; + Atomic mNumberDurations{0}; + + private: + const char* mName; +}; + +class MOZ_RAII AutoProfilerStats { + public: + explicit AutoProfilerStats( + StaticBaseProfilerStats& aStats MOZ_GUARD_OBJECT_NOTIFIER_PARAM) + : mStats(aStats), mStart(TimeStamp::NowUnfuzzed()) { + MOZ_GUARD_OBJECT_NOTIFIER_INIT; + } + + ~AutoProfilerStats() { + mStats.mSumDurationsNs += int64_t( + (TimeStamp::NowUnfuzzed() - mStart).ToMicroseconds() * 1000 + 0.5); + ++mStats.mNumberDurations; + } + + private: + MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER + + StaticBaseProfilerStats& mStats; + TimeStamp mStart; +}; + +# define AUTO_PROFILER_STATS(name) \ + static ::mozilla::baseprofiler::StaticBaseProfilerStats sStat##name( \ + #name); \ + ::mozilla::baseprofiler::AutoProfilerStats autoStat##name(sStat##name); + +# else // PROFILER_RUNTIME_STATS + +# define AUTO_PROFILER_STATS(name) + +# endif // PROFILER_RUNTIME_STATS else + //--------------------------------------------------------------------------- // Put profiling data into the profiler (labels and markers) //---------------------------------------------------------------------------