diff --git a/bolt/src/DataAggregator.cpp b/bolt/src/DataAggregator.cpp index 98e7954fb0d7..3c0b97c0262b 100644 --- a/bolt/src/DataAggregator.cpp +++ b/bolt/src/DataAggregator.cpp @@ -1395,6 +1395,11 @@ std::error_code DataAggregator::printLBRHeatMap() { } HM.print(opts::HeatmapFile); + if (opts::HeatmapFile == "-") { + HM.printCDF(opts::HeatmapFile); + } else { + HM.printCDF(opts::HeatmapFile + ".csv"); + } return std::error_code(); } diff --git a/bolt/src/Heatmap.cpp b/bolt/src/Heatmap.cpp index 5761c90db2c6..4dbae4642a82 100644 --- a/bolt/src/Heatmap.cpp +++ b/bolt/src/Heatmap.cpp @@ -17,7 +17,9 @@ #include "llvm/Support/Format.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" +#include #include +#include #define DEBUG_TYPE "bolt-heatmap" @@ -237,5 +239,43 @@ void Heatmap::print(raw_ostream &OS) const { } } +void Heatmap::printCDF(StringRef FileName) const { + std::error_code EC; + raw_fd_ostream OS(FileName, EC, sys::fs::OpenFlags::F_None); + if (EC) { + errs() << "error opening output file: " << EC.message() << '\n'; + exit(1); + } + printCDF(OS); +} + +void Heatmap::printCDF(raw_ostream &OS) const { + uint64_t NumTotalCounts{0}; + std::vector Counts; + + for (const auto &KV : Map) { + Counts.push_back(KV.second); + NumTotalCounts += KV.second; + } + + std::sort(Counts.begin(), Counts.end(), std::greater()); + + double RatioLeftInKB = (1.0 * BucketSize) / 1024; + assert(NumTotalCounts > 0 && + "total number of heatmap buckets should be greater than 0"); + double RatioRightInPercent = 100.0 / NumTotalCounts; + uint64_t RunningCount{0}; + + OS << "Bucket counts, Size (KB), CDF (%)\n"; + for (uint64_t I = 0; I < Counts.size(); I++) { + RunningCount += Counts[I]; + OS << format("%llu", (I + 1)) << ", " + << format("%.4f", RatioLeftInKB * (I + 1)) << ", " + << format("%.4f", RatioRightInPercent * (RunningCount)) << "\n"; + } + + Counts.clear(); +} + } // namespace bolt } // namespace llvm diff --git a/bolt/src/Heatmap.h b/bolt/src/Heatmap.h index 9ed48186e265..3cc3205f4bac 100644 --- a/bolt/src/Heatmap.h +++ b/bolt/src/Heatmap.h @@ -64,6 +64,10 @@ public: void print(raw_ostream &OS) const; + void printCDF(StringRef FileName) const; + + void printCDF(raw_ostream &OS) const; + size_t size() const { return Map.size(); }