Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Statistic.cpp - Easy way to expose stats information --------------===// |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements the 'Statistic' class, which is designed to be an easy |
| 4 | // way to expose various success metrics from passes. These statistics are |
| 5 | // printed at the end of a run, when the -stats command line option is enabled |
| 6 | // on the command line. |
| 7 | // |
| 8 | // This is useful for reporting information like the number of instructions |
| 9 | // simplified, optimized or removed by various transformations, like this: |
| 10 | // |
| 11 | // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed"); |
| 12 | // |
| 13 | // Later, in the code: ++NumInstEliminated; |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 17 | #include "Support/Statistic.h" |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 18 | #include "Support/CommandLine.h" |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 19 | #include <sstream> |
Chris Lattner | 038e05a | 2003-08-01 22:15:41 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 21 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 22 | // GetLibSupportInfoOutputFile - Return a file stream to print our output on... |
| 23 | std::ostream *GetLibSupportInfoOutputFile(); |
| 24 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 25 | unsigned StatisticBase::NumStats = 0; |
| 26 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 27 | // -stats - Command line option to cause transformations to emit stats about |
| 28 | // what they did. |
| 29 | // |
| 30 | static cl::opt<bool> |
| 31 | Enabled("stats", cl::desc("Enable statistics output from program")); |
| 32 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 33 | struct StatRecord { |
| 34 | std::string Value; |
| 35 | const char *Name, *Desc; |
| 36 | |
| 37 | StatRecord(const std::string V, const char *N, const char *D) |
| 38 | : Value(V), Name(N), Desc(D) {} |
| 39 | |
| 40 | bool operator<(const StatRecord &SR) const { |
| 41 | return std::strcmp(Name, SR.Name) < 0; |
| 42 | } |
| 43 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 44 | void print(unsigned ValFieldSize, unsigned NameFieldSize, |
| 45 | std::ostream &OS) { |
| 46 | OS << std::string(ValFieldSize-Value.length(), ' ') |
| 47 | << Value << " " << Name |
| 48 | << std::string(NameFieldSize-std::strlen(Name), ' ') |
| 49 | << " - " << Desc << "\n"; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | static std::vector<StatRecord> *AccumStats = 0; |
| 54 | |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 55 | // Print information when destroyed, iff command line option is specified |
| 56 | void StatisticBase::destroy() const { |
| 57 | if (Enabled && hasSomeData()) { |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 58 | if (AccumStats == 0) |
| 59 | AccumStats = new std::vector<StatRecord>(); |
| 60 | |
| 61 | std::ostringstream Out; |
| 62 | printValue(Out); |
| 63 | AccumStats->push_back(StatRecord(Out.str(), Name, Desc)); |
| 64 | } |
| 65 | |
| 66 | if (--NumStats == 0 && AccumStats) { |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 67 | std::ostream *OutStream = GetLibSupportInfoOutputFile(); |
| 68 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 69 | // Figure out how long the biggest Value and Name fields are... |
| 70 | unsigned MaxNameLen = 0, MaxValLen = 0; |
| 71 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) { |
Anand Shukla | c1d4d79 | 2002-10-04 23:56:18 +0000 | [diff] [blame] | 72 | MaxValLen = std::max(MaxValLen, |
| 73 | (unsigned)(*AccumStats)[i].Value.length()); |
| 74 | MaxNameLen = std::max(MaxNameLen, |
| 75 | (unsigned)std::strlen((*AccumStats)[i].Name)); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Sort the fields... |
| 79 | std::stable_sort(AccumStats->begin(), AccumStats->end()); |
| 80 | |
| 81 | // Print out the statistics header... |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 82 | *OutStream << "===" << std::string(73, '-') << "===\n" |
| 83 | << " ... Statistics Collected ...\n" |
| 84 | << "===" << std::string(73, '-') << "===\n\n"; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 85 | |
| 86 | // Print all of the statistics accumulated... |
| 87 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 88 | (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 89 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 90 | *OutStream << std::endl; // Flush the output stream... |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 91 | |
| 92 | // Free all accumulated statistics... |
| 93 | delete AccumStats; |
| 94 | AccumStats = 0; |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 95 | if (OutStream != &std::cerr && OutStream != &std::cout) |
| 96 | delete OutStream; // Close the file... |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 97 | } |
| 98 | } |