1 //===- ProfileCommon.h - Common profiling APIs. -----------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains data structures and functions common to both instrumented 10 // and sample profiling. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_PROFILEDATA_PROFILECOMMON_H 15 #define LLVM_PROFILEDATA_PROFILECOMMON_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/IR/ProfileSummary.h" 19 #include "llvm/ProfileData/InstrProf.h" 20 #include "llvm/ProfileData/SampleProf.h" 21 #include "llvm/Support/Error.h" 22 #include <algorithm> 23 #include <cstdint> 24 #include <functional> 25 #include <map> 26 #include <memory> 27 #include <vector> 28 29 namespace llvm { 30 31 extern cl::opt<bool> UseContextLessSummary; 32 extern cl::opt<int> ProfileSummaryCutoffHot; 33 extern cl::opt<int> ProfileSummaryCutoffCold; 34 extern cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold; 35 extern cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold; 36 extern cl::opt<uint64_t> ProfileSummaryHotCount; 37 extern cl::opt<uint64_t> ProfileSummaryColdCount; 38 39 namespace sampleprof { 40 41 class FunctionSamples; 42 43 } // end namespace sampleprof 44 45 class ProfileSummaryBuilder { 46 private: 47 /// We keep track of the number of times a count (block count or samples) 48 /// appears in the profile. The map is kept sorted in the descending order of 49 /// counts. 50 std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies; 51 std::vector<uint32_t> DetailedSummaryCutoffs; 52 53 protected: 54 SummaryEntryVector DetailedSummary; 55 uint64_t TotalCount = 0; 56 uint64_t MaxCount = 0; 57 uint64_t MaxFunctionCount = 0; 58 uint32_t NumCounts = 0; 59 uint32_t NumFunctions = 0; 60 61 ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs) 62 : DetailedSummaryCutoffs(std::move(Cutoffs)) {} 63 ~ProfileSummaryBuilder() = default; 64 65 inline void addCount(uint64_t Count); 66 void computeDetailedSummary(); 67 68 public: 69 /// A vector of useful cutoff values for detailed summary. 70 static const ArrayRef<uint32_t> DefaultCutoffs; 71 72 /// Find the summary entry for a desired percentile of counts. 73 static const ProfileSummaryEntry & 74 getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile); 75 static uint64_t getHotCountThreshold(const SummaryEntryVector &DS); 76 static uint64_t getColdCountThreshold(const SummaryEntryVector &DS); 77 }; 78 79 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder { 80 uint64_t MaxInternalBlockCount = 0; 81 82 inline void addEntryCount(uint64_t Count); 83 inline void addInternalCount(uint64_t Count); 84 85 public: 86 InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs) 87 : ProfileSummaryBuilder(std::move(Cutoffs)) {} 88 89 void addRecord(const InstrProfRecord &); 90 std::unique_ptr<ProfileSummary> getSummary(); 91 }; 92 93 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder { 94 public: 95 SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs) 96 : ProfileSummaryBuilder(std::move(Cutoffs)) {} 97 98 void addRecord(const sampleprof::FunctionSamples &FS, 99 bool isCallsiteSample = false); 100 std::unique_ptr<ProfileSummary> 101 computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles); 102 std::unique_ptr<ProfileSummary> getSummary(); 103 }; 104 105 /// This is called when a count is seen in the profile. 106 void ProfileSummaryBuilder::addCount(uint64_t Count) { 107 TotalCount += Count; 108 if (Count > MaxCount) 109 MaxCount = Count; 110 NumCounts++; 111 CountFrequencies[Count]++; 112 } 113 114 } // end namespace llvm 115 116 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H 117