xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ProfileData/ProfileCommon.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/Compiler.h"
22 #include "llvm/Support/Error.h"
23 #include <algorithm>
24 #include <cstdint>
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <vector>
29 
30 namespace llvm {
31 
32 LLVM_ABI extern cl::opt<bool> UseContextLessSummary;
33 LLVM_ABI extern cl::opt<int> ProfileSummaryCutoffHot;
34 LLVM_ABI extern cl::opt<int> ProfileSummaryCutoffCold;
35 LLVM_ABI extern cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold;
36 LLVM_ABI extern cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold;
37 LLVM_ABI extern cl::opt<uint64_t> ProfileSummaryHotCount;
38 LLVM_ABI extern cl::opt<uint64_t> ProfileSummaryColdCount;
39 
40 namespace sampleprof {
41 
42 class FunctionSamples;
43 
44 } // end namespace sampleprof
45 
46 class ProfileSummaryBuilder {
47 private:
48   /// We keep track of the number of times a count (block count or samples)
49   /// appears in the profile. The map is kept sorted in the descending order of
50   /// counts.
51   std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
52   std::vector<uint32_t> DetailedSummaryCutoffs;
53 
54 protected:
55   SummaryEntryVector DetailedSummary;
56   uint64_t TotalCount = 0;
57   uint64_t MaxCount = 0;
58   uint64_t MaxFunctionCount = 0;
59   uint32_t NumCounts = 0;
60   uint32_t NumFunctions = 0;
61 
ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)62   ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
63       : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
64   ~ProfileSummaryBuilder() = default;
65 
66   inline void addCount(uint64_t Count);
67   LLVM_ABI void computeDetailedSummary();
68 
69 public:
70   /// A vector of useful cutoff values for detailed summary.
71   LLVM_ABI static const ArrayRef<uint32_t> DefaultCutoffs;
72 
73   /// Find the summary entry for a desired percentile of counts.
74   LLVM_ABI static const ProfileSummaryEntry &
75   getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile);
76   LLVM_ABI static uint64_t getHotCountThreshold(const SummaryEntryVector &DS);
77   LLVM_ABI static uint64_t getColdCountThreshold(const SummaryEntryVector &DS);
78 };
79 
80 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
81   uint64_t MaxInternalBlockCount = 0;
82 
83 public:
InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)84   InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
85       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
86 
87   LLVM_ABI void addEntryCount(uint64_t Count);
88   LLVM_ABI void addInternalCount(uint64_t Count);
89 
90   LLVM_ABI void addRecord(const InstrProfRecord &);
91   LLVM_ABI std::unique_ptr<ProfileSummary> getSummary();
92 };
93 
94 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
95 public:
SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)96   SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
97       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
98 
99   LLVM_ABI void addRecord(const sampleprof::FunctionSamples &FS,
100                           bool isCallsiteSample = false);
101   LLVM_ABI std::unique_ptr<ProfileSummary>
102   computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles);
103   LLVM_ABI std::unique_ptr<ProfileSummary> getSummary();
104 };
105 
106 /// This is called when a count is seen in the profile.
addCount(uint64_t Count)107 void ProfileSummaryBuilder::addCount(uint64_t Count) {
108   TotalCount += Count;
109   if (Count > MaxCount)
110     MaxCount = Count;
111   NumCounts++;
112   CountFrequencies[Count]++;
113 }
114 
115 } // end namespace llvm
116 
117 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
118