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