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 namespace sampleprof {
32 
33 class FunctionSamples;
34 
35 } // end namespace sampleprof
36 
37 inline const char *getHotSectionPrefix() { return "hot"; }
38 inline const char *getUnlikelySectionPrefix() { return "unlikely"; }
39 
40 class ProfileSummaryBuilder {
41 private:
42   /// We keep track of the number of times a count (block count or samples)
43   /// appears in the profile. The map is kept sorted in the descending order of
44   /// counts.
45   std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
46   std::vector<uint32_t> DetailedSummaryCutoffs;
47 
48 protected:
49   SummaryEntryVector DetailedSummary;
50   uint64_t TotalCount = 0;
51   uint64_t MaxCount = 0;
52   uint64_t MaxFunctionCount = 0;
53   uint32_t NumCounts = 0;
54   uint32_t NumFunctions = 0;
55 
56   ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
57       : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
58   ~ProfileSummaryBuilder() = default;
59 
60   inline void addCount(uint64_t Count);
61   void computeDetailedSummary();
62 
63 public:
64   /// A vector of useful cutoff values for detailed summary.
65   static const ArrayRef<uint32_t> DefaultCutoffs;
66 
67   /// Find the summary entry for a desired percentile of counts.
68   static const ProfileSummaryEntry &
69   getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile);
70 };
71 
72 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
73   uint64_t MaxInternalBlockCount = 0;
74 
75   inline void addEntryCount(uint64_t Count);
76   inline void addInternalCount(uint64_t Count);
77 
78 public:
79   InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
80       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
81 
82   void addRecord(const InstrProfRecord &);
83   std::unique_ptr<ProfileSummary> getSummary();
84 };
85 
86 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
87 public:
88   SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
89       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
90 
91   void addRecord(const sampleprof::FunctionSamples &FS,
92                  bool isCallsiteSample = false);
93   std::unique_ptr<ProfileSummary> computeSummaryForProfiles(
94       const StringMap<sampleprof::FunctionSamples> &Profiles);
95   std::unique_ptr<ProfileSummary> getSummary();
96 };
97 
98 /// This is called when a count is seen in the profile.
99 void ProfileSummaryBuilder::addCount(uint64_t Count) {
100   TotalCount += Count;
101   if (Count > MaxCount)
102     MaxCount = Count;
103   NumCounts++;
104   CountFrequencies[Count]++;
105 }
106 
107 } // end namespace llvm
108 
109 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
110