llvm.org GIT mirror llvm / 233963b
Revert r326723: Make STATISTIC() values available programmatically Despite building cleanly on my machine in three separate configs, it's failing on pretty much all bots due to missing includes among other things. Investigating. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326726 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Sanders 2 years ago
6 changed file(s) with 7 addition(s) and 154 deletion(s). Raw diff Collapse all Expand all
880880 set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE)
881881 endfunction()
882882 get_compile_definitions()
883
884 # The default for LLVM_ENABLE_STATS depends on whether NDEBUG is defined or not.
885 # LLVM_ENABLE_ASSERTIONS controls that so re-use it as the default.
886 option(LLVM_ENABLE_STATS "Enable statistics collection" ${LLVM_ENABLE_ASSERTIONS})
2525 #ifndef LLVM_ADT_STATISTIC_H
2626 #define LLVM_ADT_STATISTIC_H
2727
28 #include "llvm/Config/llvm-config.h"
2928 #include "llvm/Support/Compiler.h"
3029 #include
3130 #include
3433
3534 class raw_ostream;
3635 class raw_fd_ostream;
37 class StringRef;
3836
3937 class Statistic {
4038 public:
6159 // Allow use of this class as the value itself.
6260 operator unsigned() const { return getValue(); }
6361
64 #if LLVM_ENABLE_STATS
62 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
6563 const Statistic &operator=(unsigned Val) {
6664 Value.store(Val, std::memory_order_relaxed);
6765 return init();
143141
144142 void updateMax(unsigned V) {}
145143
146 #endif // LLVM_ENABLE_STATS
144 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
147145
148146 protected:
149147 Statistic &init() {
181179 /// PrintStatisticsJSON().
182180 void PrintStatisticsJSON(raw_ostream &OS);
183181
184 /// \brief Get the statistics. This can be used to look up the value of
185 /// statistics without needing to parse JSON.
186 ///
187 /// This function does not prevent statistics being updated by other threads
188 /// during it's execution. It will return the value at the point that it is
189 /// read. However, it will prevent new statistics from registering until it
190 /// completes.
191 const std::vector> GetStatistics();
192
193182 } // end namespace llvm
194183
195184 #endif // LLVM_ADT_STATISTIC_H
7676 /* LLVM version string */
7777 #define LLVM_VERSION_STRING "${PACKAGE_VERSION}"
7878
79 /* Whether LLVM records statistics for use with GetStatistics(),
80 * PrintStatistics() or PrintStatisticsJSON()
81 */
82 #cmakedefine01 LLVM_ENABLE_STATS
83
8479 #endif
5151 static bool PrintOnExit;
5252
5353 namespace {
54 /// This class is used in a ManagedStatic so that it is created on demand (when
55 /// the first statistic is bumped) and destroyed only when llvm_shutdown is
56 /// called. We print statistics from the destructor.
57 /// This class is also used to look up statistic values from applications that
58 /// use LLVM.
54 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
55 /// on demand (when the first statistic is bumped) and destroyed only when
56 /// llvm_shutdown is called. We print statistics from the destructor.
5957 class StatisticInfo {
6058 std::vector Stats;
61
6259 friend void llvm::PrintStatistics();
6360 friend void llvm::PrintStatistics(raw_ostream &OS);
6461 friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
6663 /// Sort statistics by debugtype,name,description.
6764 void sort();
6865 public:
69 using const_iterator = std::vector::const_iterator;
70
7166 StatisticInfo();
7267 ~StatisticInfo();
7368
7469 void addStatistic(const Statistic *S) {
7570 Stats.push_back(S);
7671 }
77
78 const_iterator begin() const { return Stats.begin(); }
79 const_iterator end() const { return Stats.end(); }
80 iterator_range statistics() const {
81 return {begin(), end()};
82 }
8372 };
84 } // end anonymous namespace
73 }
8574
8675 static ManagedStatic StatInfo;
8776 static ManagedStatic > StatLock;
190179 }
191180
192181 void llvm::PrintStatistics() {
193 #if LLVM_ENABLE_STATS
182 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
194183 StatisticInfo &Stats = *StatInfo;
195184
196185 // Statistics not enabled?
215204 }
216205 #endif
217206 }
218
219 const std::vector> llvm::GetStatistics() {
220 sys::SmartScopedLock Reader(*StatLock);
221 std::vector> ReturnStats;
222
223 for (const auto &Stat : StatInfo->statistics())
224 ReturnStats.emplace_back(Stat->getName(), Stat->getValue());
225 return ReturnStats;
226 }
5555 SparseBitVectorTest.cpp
5656 SparseMultiSetTest.cpp
5757 SparseSetTest.cpp
58 StatisticTest.cpp
5958 StringExtrasTest.cpp
6059 StringMapTest.cpp
6160 StringRefTest.cpp
+0
-106
unittests/ADT/StatisticTest.cpp less more
None //===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/Statistic.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
12 using namespace llvm;
13
14 namespace {
15 #define DEBUG_TYPE "unittest"
16 STATISTIC(Counter, "Counts things");
17 STATISTIC(Counter2, "Counts other things");
18
19 TEST(StatisticTest, Count) {
20 EnableStatistics();
21
22 Counter = 0;
23 EXPECT_EQ(Counter, 0u);
24 Counter++;
25 Counter++;
26 #if LLVM_ENABLE_STATS
27 EXPECT_EQ(Counter, 2u);
28 #else
29 EXPECT_EQ(Counter, 0u);
30 #endif
31 }
32
33 TEST(StatisticTest, Assign) {
34 EnableStatistics();
35
36 Counter = 2;
37 #if LLVM_ENABLE_STATS
38 EXPECT_EQ(Counter, 2u);
39 #else
40 EXPECT_EQ(Counter, 0u);
41 #endif
42 }
43
44 TEST(StatisticTest, API) {
45 EnableStatistics();
46
47 Counter = 0;
48 EXPECT_EQ(Counter, 0u);
49 Counter++;
50 Counter++;
51 #if LLVM_ENABLE_STATS
52 EXPECT_EQ(Counter, 2u);
53 #else
54 EXPECT_EQ(Counter, 0u);
55 #endif
56
57 #if LLVM_ENABLE_STATS
58 const auto Range1 = GetStatistics();
59 EXPECT_NE(Range1.begin(), Range1.end());
60 EXPECT_EQ(Range1.begin() + 1, Range1.end());
61
62 Optional> S1;
63 Optional> S2;
64 for (const auto &S : Range1) {
65 if (std::string(S.first) == "Counter")
66 S1 = S;
67 if (std::string(S.first) == "Counter2")
68 S2 = S;
69 }
70
71 EXPECT_NE(S1.hasValue(), false);
72 EXPECT_EQ(S2.hasValue(), false);
73
74 // Counter2 will be registered when it's first touched.
75 Counter2++;
76
77 const auto Range2 = GetStatistics();
78 EXPECT_NE(Range2.begin(), Range2.end());
79 EXPECT_EQ(Range2.begin() + 2, Range2.end());
80
81 S1 = None;
82 S2 = None;
83 for (const auto &S : Range2) {
84 if (std::string(S.first) == "Counter")
85 S1 = S;
86 if (std::string(S.first) == "Counter2")
87 S2 = S;
88 }
89
90 EXPECT_NE(S1.hasValue(), false);
91 EXPECT_NE(S2.hasValue(), false);
92
93 EXPECT_EQ(S1->first, "Counter");
94 EXPECT_EQ(S1->second, 2u);
95
96 EXPECT_EQ(S2->first, "Counter2");
97 EXPECT_EQ(S2->second, 1u);
98 #else
99 Counter2++;
100 auto &Range = GetStatistics();
101 EXPECT_EQ(Range.begin(), Range.end());
102 #endif
103 }
104
105 } // end anonymous namespace