Home | History | Annotate | Download | only in ProfileData
      1 //===- ProfileCommon.h - Common profiling APIs. -----------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains data structures and functions common to both instrumented
     11 // and sample profiling.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_PROFILEDATA_PROFILECOMMON_H
     16 #define LLVM_PROFILEDATA_PROFILECOMMON_H
     17 
     18 #include "llvm/ADT/ArrayRef.h"
     19 #include "llvm/IR/ProfileSummary.h"
     20 #include "llvm/Support/Error.h"
     21 #include <algorithm>
     22 #include <cstdint>
     23 #include <functional>
     24 #include <map>
     25 #include <memory>
     26 #include <vector>
     27 
     28 namespace llvm {
     29 
     30 struct InstrProfRecord;
     31 
     32 namespace sampleprof {
     33 
     34 class FunctionSamples;
     35 
     36 } // end namespace sampleprof
     37 
     38 inline const char *getHotSectionPrefix() { return ".hot"; }
     39 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
     40 
     41 class ProfileSummaryBuilder {
     42 private:
     43   /// We keep track of the number of times a count (block count or samples)
     44   /// appears in the profile. The map is kept sorted in the descending order of
     45   /// counts.
     46   std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
     47   std::vector<uint32_t> DetailedSummaryCutoffs;
     48 
     49 protected:
     50   SummaryEntryVector DetailedSummary;
     51   uint64_t TotalCount = 0;
     52   uint64_t MaxCount = 0;
     53   uint64_t MaxFunctionCount = 0;
     54   uint32_t NumCounts = 0;
     55   uint32_t NumFunctions = 0;
     56 
     57   ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
     58       : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
     59   ~ProfileSummaryBuilder() = default;
     60 
     61   inline void addCount(uint64_t Count);
     62   void computeDetailedSummary();
     63 
     64 public:
     65   /// \brief A vector of useful cutoff values for detailed summary.
     66   static const ArrayRef<uint32_t> DefaultCutoffs;
     67 };
     68 
     69 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
     70   uint64_t MaxInternalBlockCount = 0;
     71 
     72   inline void addEntryCount(uint64_t Count);
     73   inline void addInternalCount(uint64_t Count);
     74 
     75 public:
     76   InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
     77       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
     78 
     79   void addRecord(const InstrProfRecord &);
     80   std::unique_ptr<ProfileSummary> getSummary();
     81 };
     82 
     83 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
     84 public:
     85   SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
     86       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
     87 
     88   void addRecord(const sampleprof::FunctionSamples &FS);
     89   std::unique_ptr<ProfileSummary> getSummary();
     90 };
     91 
     92 /// This is called when a count is seen in the profile.
     93 void ProfileSummaryBuilder::addCount(uint64_t Count) {
     94   TotalCount += Count;
     95   if (Count > MaxCount)
     96     MaxCount = Count;
     97   NumCounts++;
     98   CountFrequencies[Count]++;
     99 }
    100 
    101 } // end namespace llvm
    102 
    103 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
    104