xref: /aosp_15_r20/external/llvm/lib/Support/Statistic.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- Statistic.cpp - Easy way to expose stats information --------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the 'Statistic' class, which is designed to be an easy
11*9880d681SAndroid Build Coastguard Worker // way to expose various success metrics from passes.  These statistics are
12*9880d681SAndroid Build Coastguard Worker // printed at the end of a run, when the -stats command line option is enabled
13*9880d681SAndroid Build Coastguard Worker // on the command line.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // This is useful for reporting information like the number of instructions
16*9880d681SAndroid Build Coastguard Worker // simplified, optimized or removed by various transformations, like this:
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
19*9880d681SAndroid Build Coastguard Worker //
20*9880d681SAndroid Build Coastguard Worker // Later, in the code: ++NumInstEliminated;
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Compiler.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Mutex.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
33*9880d681SAndroid Build Coastguard Worker #include <algorithm>
34*9880d681SAndroid Build Coastguard Worker #include <cstring>
35*9880d681SAndroid Build Coastguard Worker using namespace llvm;
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker /// -stats - Command line option to cause transformations to emit stats about
38*9880d681SAndroid Build Coastguard Worker /// what they did.
39*9880d681SAndroid Build Coastguard Worker ///
40*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
41*9880d681SAndroid Build Coastguard Worker Enabled(
42*9880d681SAndroid Build Coastguard Worker     "stats",
43*9880d681SAndroid Build Coastguard Worker     cl::desc("Enable statistics output from program (available with Asserts)"));
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> StatsAsJSON("stats-json",
47*9880d681SAndroid Build Coastguard Worker                                  cl::desc("Display statistics as json data"));
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker namespace {
50*9880d681SAndroid Build Coastguard Worker /// StatisticInfo - This class is used in a ManagedStatic so that it is created
51*9880d681SAndroid Build Coastguard Worker /// on demand (when the first statistic is bumped) and destroyed only when
52*9880d681SAndroid Build Coastguard Worker /// llvm_shutdown is called.  We print statistics from the destructor.
53*9880d681SAndroid Build Coastguard Worker class StatisticInfo {
54*9880d681SAndroid Build Coastguard Worker   std::vector<const Statistic*> Stats;
55*9880d681SAndroid Build Coastguard Worker   friend void llvm::PrintStatistics();
56*9880d681SAndroid Build Coastguard Worker   friend void llvm::PrintStatistics(raw_ostream &OS);
57*9880d681SAndroid Build Coastguard Worker   friend void llvm::PrintStatisticsJSON(raw_ostream &OS);
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker   /// Sort statistics by debugtype,name,description.
60*9880d681SAndroid Build Coastguard Worker   void sort();
61*9880d681SAndroid Build Coastguard Worker public:
62*9880d681SAndroid Build Coastguard Worker   ~StatisticInfo();
63*9880d681SAndroid Build Coastguard Worker 
addStatistic(const Statistic * S)64*9880d681SAndroid Build Coastguard Worker   void addStatistic(const Statistic *S) {
65*9880d681SAndroid Build Coastguard Worker     Stats.push_back(S);
66*9880d681SAndroid Build Coastguard Worker   }
67*9880d681SAndroid Build Coastguard Worker };
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker static ManagedStatic<StatisticInfo> StatInfo;
71*9880d681SAndroid Build Coastguard Worker static ManagedStatic<sys::SmartMutex<true> > StatLock;
72*9880d681SAndroid Build Coastguard Worker 
73*9880d681SAndroid Build Coastguard Worker /// RegisterStatistic - The first time a statistic is bumped, this method is
74*9880d681SAndroid Build Coastguard Worker /// called.
RegisterStatistic()75*9880d681SAndroid Build Coastguard Worker void Statistic::RegisterStatistic() {
76*9880d681SAndroid Build Coastguard Worker   // If stats are enabled, inform StatInfo that this statistic should be
77*9880d681SAndroid Build Coastguard Worker   // printed.
78*9880d681SAndroid Build Coastguard Worker   sys::SmartScopedLock<true> Writer(*StatLock);
79*9880d681SAndroid Build Coastguard Worker   if (!Initialized) {
80*9880d681SAndroid Build Coastguard Worker     if (Enabled)
81*9880d681SAndroid Build Coastguard Worker       StatInfo->addStatistic(this);
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker     TsanHappensBefore(this);
84*9880d681SAndroid Build Coastguard Worker     sys::MemoryFence();
85*9880d681SAndroid Build Coastguard Worker     // Remember we have been registered.
86*9880d681SAndroid Build Coastguard Worker     TsanIgnoreWritesBegin();
87*9880d681SAndroid Build Coastguard Worker     Initialized = true;
88*9880d681SAndroid Build Coastguard Worker     TsanIgnoreWritesEnd();
89*9880d681SAndroid Build Coastguard Worker   }
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker // Print information when destroyed, iff command line option is specified.
~StatisticInfo()93*9880d681SAndroid Build Coastguard Worker StatisticInfo::~StatisticInfo() {
94*9880d681SAndroid Build Coastguard Worker   llvm::PrintStatistics();
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker 
EnableStatistics()97*9880d681SAndroid Build Coastguard Worker void llvm::EnableStatistics() {
98*9880d681SAndroid Build Coastguard Worker   Enabled.setValue(true);
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker 
AreStatisticsEnabled()101*9880d681SAndroid Build Coastguard Worker bool llvm::AreStatisticsEnabled() {
102*9880d681SAndroid Build Coastguard Worker   return Enabled;
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker 
sort()105*9880d681SAndroid Build Coastguard Worker void StatisticInfo::sort() {
106*9880d681SAndroid Build Coastguard Worker   std::stable_sort(Stats.begin(), Stats.end(),
107*9880d681SAndroid Build Coastguard Worker                    [](const Statistic *LHS, const Statistic *RHS) {
108*9880d681SAndroid Build Coastguard Worker     if (int Cmp = std::strcmp(LHS->getDebugType(), RHS->getDebugType()))
109*9880d681SAndroid Build Coastguard Worker       return Cmp < 0;
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker     if (int Cmp = std::strcmp(LHS->getName(), RHS->getName()))
112*9880d681SAndroid Build Coastguard Worker       return Cmp < 0;
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker     return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
115*9880d681SAndroid Build Coastguard Worker   });
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker 
PrintStatistics(raw_ostream & OS)118*9880d681SAndroid Build Coastguard Worker void llvm::PrintStatistics(raw_ostream &OS) {
119*9880d681SAndroid Build Coastguard Worker   StatisticInfo &Stats = *StatInfo;
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   // Figure out how long the biggest Value and Name fields are.
122*9880d681SAndroid Build Coastguard Worker   unsigned MaxDebugTypeLen = 0, MaxValLen = 0;
123*9880d681SAndroid Build Coastguard Worker   for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
124*9880d681SAndroid Build Coastguard Worker     MaxValLen = std::max(MaxValLen,
125*9880d681SAndroid Build Coastguard Worker                          (unsigned)utostr(Stats.Stats[i]->getValue()).size());
126*9880d681SAndroid Build Coastguard Worker     MaxDebugTypeLen = std::max(MaxDebugTypeLen,
127*9880d681SAndroid Build Coastguard Worker                          (unsigned)std::strlen(Stats.Stats[i]->getDebugType()));
128*9880d681SAndroid Build Coastguard Worker   }
129*9880d681SAndroid Build Coastguard Worker 
130*9880d681SAndroid Build Coastguard Worker   Stats.sort();
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   // Print out the statistics header...
133*9880d681SAndroid Build Coastguard Worker   OS << "===" << std::string(73, '-') << "===\n"
134*9880d681SAndroid Build Coastguard Worker      << "                          ... Statistics Collected ...\n"
135*9880d681SAndroid Build Coastguard Worker      << "===" << std::string(73, '-') << "===\n\n";
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   // Print all of the statistics.
138*9880d681SAndroid Build Coastguard Worker   for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i)
139*9880d681SAndroid Build Coastguard Worker     OS << format("%*u %-*s - %s\n",
140*9880d681SAndroid Build Coastguard Worker                  MaxValLen, Stats.Stats[i]->getValue(),
141*9880d681SAndroid Build Coastguard Worker                  MaxDebugTypeLen, Stats.Stats[i]->getDebugType(),
142*9880d681SAndroid Build Coastguard Worker                  Stats.Stats[i]->getDesc());
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   OS << '\n';  // Flush the output stream.
145*9880d681SAndroid Build Coastguard Worker   OS.flush();
146*9880d681SAndroid Build Coastguard Worker }
147*9880d681SAndroid Build Coastguard Worker 
write_json_string_escaped(raw_ostream & OS,const char * string)148*9880d681SAndroid Build Coastguard Worker static void write_json_string_escaped(raw_ostream &OS, const char *string) {
149*9880d681SAndroid Build Coastguard Worker   // Out current usage should not need any escaping. Keep it simple and just
150*9880d681SAndroid Build Coastguard Worker   // check that the input is pure ASCII without special characers.
151*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
152*9880d681SAndroid Build Coastguard Worker   for (const unsigned char *c = (const unsigned char*)string; *c != '\0'; ++c) {
153*9880d681SAndroid Build Coastguard Worker     assert(*c != '\\' && *c != '\"' && *c >= 0x20 && *c < 0x80);
154*9880d681SAndroid Build Coastguard Worker   }
155*9880d681SAndroid Build Coastguard Worker #endif
156*9880d681SAndroid Build Coastguard Worker   OS << string;
157*9880d681SAndroid Build Coastguard Worker }
158*9880d681SAndroid Build Coastguard Worker 
PrintStatisticsJSON(raw_ostream & OS)159*9880d681SAndroid Build Coastguard Worker void llvm::PrintStatisticsJSON(raw_ostream &OS) {
160*9880d681SAndroid Build Coastguard Worker   StatisticInfo &Stats = *StatInfo;
161*9880d681SAndroid Build Coastguard Worker 
162*9880d681SAndroid Build Coastguard Worker   Stats.sort();
163*9880d681SAndroid Build Coastguard Worker 
164*9880d681SAndroid Build Coastguard Worker   // Print all of the statistics.
165*9880d681SAndroid Build Coastguard Worker   OS << "{\n";
166*9880d681SAndroid Build Coastguard Worker   const char *delim = "";
167*9880d681SAndroid Build Coastguard Worker   for (const Statistic *Stat : Stats.Stats) {
168*9880d681SAndroid Build Coastguard Worker     OS << delim;
169*9880d681SAndroid Build Coastguard Worker     OS << "\t\"";
170*9880d681SAndroid Build Coastguard Worker     write_json_string_escaped(OS, Stat->getDebugType());
171*9880d681SAndroid Build Coastguard Worker     OS << '.';
172*9880d681SAndroid Build Coastguard Worker     write_json_string_escaped(OS, Stat->getName());
173*9880d681SAndroid Build Coastguard Worker     OS << "\": " << Stat->getValue();
174*9880d681SAndroid Build Coastguard Worker     delim = ",\n";
175*9880d681SAndroid Build Coastguard Worker   }
176*9880d681SAndroid Build Coastguard Worker   OS << "\n}\n";
177*9880d681SAndroid Build Coastguard Worker   OS.flush();
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker 
PrintStatistics()180*9880d681SAndroid Build Coastguard Worker void llvm::PrintStatistics() {
181*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
182*9880d681SAndroid Build Coastguard Worker   StatisticInfo &Stats = *StatInfo;
183*9880d681SAndroid Build Coastguard Worker 
184*9880d681SAndroid Build Coastguard Worker   // Statistics not enabled?
185*9880d681SAndroid Build Coastguard Worker   if (Stats.Stats.empty()) return;
186*9880d681SAndroid Build Coastguard Worker 
187*9880d681SAndroid Build Coastguard Worker   // Get the stream to write to.
188*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
189*9880d681SAndroid Build Coastguard Worker   if (StatsAsJSON)
190*9880d681SAndroid Build Coastguard Worker     PrintStatisticsJSON(*OutStream);
191*9880d681SAndroid Build Coastguard Worker   else
192*9880d681SAndroid Build Coastguard Worker     PrintStatistics(*OutStream);
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker #else
195*9880d681SAndroid Build Coastguard Worker   // Check if the -stats option is set instead of checking
196*9880d681SAndroid Build Coastguard Worker   // !Stats.Stats.empty().  In release builds, Statistics operators
197*9880d681SAndroid Build Coastguard Worker   // do nothing, so stats are never Registered.
198*9880d681SAndroid Build Coastguard Worker   if (Enabled) {
199*9880d681SAndroid Build Coastguard Worker     // Get the stream to write to.
200*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
201*9880d681SAndroid Build Coastguard Worker     (*OutStream) << "Statistics are disabled.  "
202*9880d681SAndroid Build Coastguard Worker                  << "Build with asserts or with -DLLVM_ENABLE_STATS\n";
203*9880d681SAndroid Build Coastguard Worker   }
204*9880d681SAndroid Build Coastguard Worker #endif
205*9880d681SAndroid Build Coastguard Worker }
206