@@ -284,32 +284,31 @@ void Heatmap::printCDF(raw_ostream &OS) const {
284284 Counts.clear ();
285285}
286286
287- void Heatmap::printSectionHotness (const Heatmap::SectionStatsMap &Stats,
288- StringRef FileName) const {
287+ void Heatmap::printSectionHotness (StringRef FileName) const {
289288 std::error_code EC;
290289 raw_fd_ostream OS (FileName, EC, sys::fs::OpenFlags::OF_None);
291290 if (EC) {
292291 errs () << " error opening output file: " << EC.message () << ' \n ' ;
293292 exit (1 );
294293 }
295- printSectionHotness (Stats, OS);
294+ printSectionHotness (OS);
296295}
297296
298- StringMap<Heatmap::SectionStats> Heatmap::computeSectionStats ( ) const {
297+ void Heatmap::printSectionHotness (raw_ostream &OS ) const {
299298 uint64_t NumTotalCounts = 0 ;
300- StringMap<SectionStats> Stat;
299+ StringMap<uint64_t > SectionHotness;
300+ StringMap<uint64_t > SectionUtilization;
301301 unsigned TextSectionIndex = 0 ;
302302
303303 if (TextSections.empty ())
304- return Stat ;
304+ return ;
305305
306306 uint64_t UnmappedHotness = 0 ;
307307 auto RecordUnmappedBucket = [&](uint64_t Address, uint64_t Frequency) {
308- if (opts::Verbosity >= 1 )
309- errs () << " Couldn't map the address bucket ["
310- << formatv (" {0:x}, {1:x}" , Address, Address + BucketSize)
311- << " ] containing " << Frequency
312- << " samples to a text section in the binary.\n " ;
308+ errs () << " Couldn't map the address bucket [0x" << Twine::utohexstr (Address)
309+ << " , 0x" << Twine::utohexstr (Address + BucketSize)
310+ << " ] containing " << Frequency
311+ << " samples to a text section in the binary." ;
313312 UnmappedHotness += Frequency;
314313 };
315314
@@ -326,48 +325,30 @@ StringMap<Heatmap::SectionStats> Heatmap::computeSectionStats() const {
326325 RecordUnmappedBucket (Address, Count);
327326 continue ;
328327 }
329- SectionStats &SecStats = Stat[ TextSections[TextSectionIndex].Name ] ;
330- ++SecStats. Buckets ;
331- SecStats. Samples += Count ;
328+ StringRef Name = TextSections[TextSectionIndex].Name ;
329+ SectionHotness[Name] += Count ;
330+ ++SectionUtilization[Name] ;
332331 }
333- Stat[" [total]" ] = SectionStats{NumTotalCounts, Map.size ()};
334- if (UnmappedHotness)
335- Stat[" [unmapped]" ] = SectionStats{UnmappedHotness, 0 };
336-
337- return Stat;
338- }
339332
340- void Heatmap::printSectionHotness (const StringMap<SectionStats> &Stats,
341- raw_ostream &OS) const {
342- if (TextSections.empty ())
343- return ;
333+ auto getNumBuckets = [&](uint64_t Begin, uint64_t End) {
334+ return End / BucketSize + !!(End % BucketSize) - Begin / BucketSize;
335+ };
344336
345- auto TotalIt = Stats.find (" [total]" );
346- assert (TotalIt != Stats.end () && " Malformed SectionStatsMap" );
347- const uint64_t NumTotalCounts = TotalIt->second .Samples ;
348337 assert (NumTotalCounts > 0 &&
349338 " total number of heatmap buckets should be greater than 0" );
350339
351340 OS << " Section Name, Begin Address, End Address, Percentage Hotness, "
352341 << " Utilization Pct\n " ;
353342 for (const auto [Name, Begin, End] : TextSections) {
354- uint64_t Samples = 0 ;
355- uint64_t Buckets = 0 ;
356- auto SectionIt = Stats.find (Name);
357- if (SectionIt != Stats.end ()) {
358- Samples = SectionIt->second .Samples ;
359- Buckets = SectionIt->second .Buckets ;
360- }
361- const float RelHotness = 100 . * Samples / NumTotalCounts;
362- const float BucketUtilization = 100 . * Buckets / getNumBuckets (Begin, End);
343+ const float RelHotness = 100 . * SectionHotness[Name] / NumTotalCounts;
344+ const float BucketUtilization =
345+ 100 . * SectionUtilization[Name] / getNumBuckets (Begin, End);
363346 OS << formatv (" {0}, {1:x}, {2:x}, {3:f4}, {4:f4}\n " , Name, Begin, End,
364347 RelHotness, BucketUtilization);
365348 }
366- auto UnmappedIt = Stats.find (" [unmapped]" );
367- if (UnmappedIt == Stats.end ())
368- return ;
369- const float UnmappedPct = 100 . * UnmappedIt->second .Samples / NumTotalCounts;
370- OS << formatv (" [unmapped], 0x0, 0x0, {0:f4}, 0\n " , UnmappedPct);
349+ if (UnmappedHotness > 0 )
350+ OS << formatv (" [unmapped], 0x0, 0x0, {0:f4}, 0\n " ,
351+ 100.0 * UnmappedHotness / NumTotalCounts);
371352}
372353} // namespace bolt
373354} // namespace llvm
0 commit comments