From a09751e7791ec6fb9a48969b868caeb1fe56003c Mon Sep 17 00:00:00 2001 From: Daniel Sanders Date: Mon, 5 Mar 2018 19:38:16 +0000 Subject: Re-commit: Make STATISTIC() values available programmatically Summary: It can be useful for tools to be able to retrieve the values of variables declared via STATISTIC() directly without having to emit them and parse them back. Use cases include: * Needing to report specific statistics to a test harness * Wanting to post-process statistics. For example, to produce a percentage of functions that were fully selected by GlobalISel Make this possible by adding llvm::GetStatistics() which returns an iterator_range that can be used to inspect the statistics that have been touched during execution. When statistics are disabled (NDEBUG and not LLVM_ENABLE_STATISTICS) this method will return an empty range. This patch doesn't address the effect of multiple compilations within the same process. In such situations, the statistics will be cumulative for all compilations up to the GetStatistics() call. Reviewers: qcolombet, rtereshin, aditya_nandakumar, bogner Reviewed By: rtereshin, bogner Subscribers: llvm-commits, mgorny Differential Revision: https://reviews.llvm.org/D43901 This re-commit fixes a missing include of which it seems clang didn't mind but G++ and MSVC objected to. It seems that, clang was ok with std::vector only being forward declared at the point of use since it was fully defined eventually but G++/MSVC both rejected it at the point of use. llvm-svn: 326738 --- llvm/lib/Support/Statistic.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'llvm/lib/Support/Statistic.cpp') diff --git a/llvm/lib/Support/Statistic.cpp b/llvm/lib/Support/Statistic.cpp index 370274d..67b0716 100644 --- a/llvm/lib/Support/Statistic.cpp +++ b/llvm/lib/Support/Statistic.cpp @@ -52,11 +52,14 @@ static bool Enabled; static bool PrintOnExit; namespace { -/// StatisticInfo - This class is used in a ManagedStatic so that it is created -/// on demand (when the first statistic is bumped) and destroyed only when -/// llvm_shutdown is called. We print statistics from the destructor. +/// This class is used in a ManagedStatic so that it is created on demand (when +/// the first statistic is bumped) and destroyed only when llvm_shutdown is +/// called. We print statistics from the destructor. +/// This class is also used to look up statistic values from applications that +/// use LLVM. class StatisticInfo { std::vector Stats; + friend void llvm::PrintStatistics(); friend void llvm::PrintStatistics(raw_ostream &OS); friend void llvm::PrintStatisticsJSON(raw_ostream &OS); @@ -64,14 +67,22 @@ class StatisticInfo { /// Sort statistics by debugtype,name,description. void sort(); public: + using const_iterator = std::vector::const_iterator; + StatisticInfo(); ~StatisticInfo(); void addStatistic(const Statistic *S) { Stats.push_back(S); } + + const_iterator begin() const { return Stats.begin(); } + const_iterator end() const { return Stats.end(); } + iterator_range statistics() const { + return {begin(), end()}; + } }; -} +} // end anonymous namespace static ManagedStatic StatInfo; static ManagedStatic > StatLock; @@ -180,7 +191,7 @@ void llvm::PrintStatisticsJSON(raw_ostream &OS) { } void llvm::PrintStatistics() { -#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) +#if LLVM_ENABLE_STATS StatisticInfo &Stats = *StatInfo; // Statistics not enabled? @@ -205,3 +216,12 @@ void llvm::PrintStatistics() { } #endif } + +const std::vector> llvm::GetStatistics() { + sys::SmartScopedLock Reader(*StatLock); + std::vector> ReturnStats; + + for (const auto &Stat : StatInfo->statistics()) + ReturnStats.emplace_back(Stat->getName(), Stat->getValue()); + return ReturnStats; +} -- cgit v1.1