diff options
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/Statistic.cpp | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/llvm/lib/Support/Statistic.cpp b/llvm/lib/Support/Statistic.cpp index 370274dc299..67b07162b9a 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<const Statistic*> 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 Statistic *>::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<const_iterator> statistics() const { + return {begin(), end()}; + } }; -} +} // end anonymous namespace static ManagedStatic<StatisticInfo> StatInfo; static ManagedStatic<sys::SmartMutex<true> > 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<std::pair<StringRef, unsigned>> llvm::GetStatistics() { + sys::SmartScopedLock<true> Reader(*StatLock); + std::vector<std::pair<StringRef, unsigned>> ReturnStats; + + for (const auto &Stat : StatInfo->statistics()) + ReturnStats.emplace_back(Stat->getName(), Stat->getValue()); + return ReturnStats; +} |