blob: 6bb5825339ae145b10add71846420801ddb505ed [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ProfileCommon.h - Common profiling APIs. -----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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/Support/Error.h"
21#include <algorithm>
22#include <cstdint>
23#include <functional>
24#include <map>
25#include <memory>
26#include <vector>
27
28namespace llvm {
29
30namespace sampleprof {
31
32class FunctionSamples;
33
34} // end namespace sampleprof
35
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020036inline const char *getHotSectionPrefix() { return "hot"; }
37inline const char *getUnlikelySectionPrefix() { return "unlikely"; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038
39class ProfileSummaryBuilder {
40private:
41 /// We keep track of the number of times a count (block count or samples)
42 /// appears in the profile. The map is kept sorted in the descending order of
43 /// counts.
44 std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
45 std::vector<uint32_t> DetailedSummaryCutoffs;
46
47protected:
48 SummaryEntryVector DetailedSummary;
49 uint64_t TotalCount = 0;
50 uint64_t MaxCount = 0;
51 uint64_t MaxFunctionCount = 0;
52 uint32_t NumCounts = 0;
53 uint32_t NumFunctions = 0;
54
55 ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
56 : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
57 ~ProfileSummaryBuilder() = default;
58
59 inline void addCount(uint64_t Count);
60 void computeDetailedSummary();
61
62public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063 /// A vector of useful cutoff values for detailed summary.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064 static const ArrayRef<uint32_t> DefaultCutoffs;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020065
66 /// Find the summary entry for a desired percentile of counts.
67 static const ProfileSummaryEntry &
68 getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069};
70
71class 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
77public:
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
85class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
86public:
87 SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
88 : ProfileSummaryBuilder(std::move(Cutoffs)) {}
89
Andrew Walbran3d2c1972020-04-07 12:24:26 +010090 void addRecord(const sampleprof::FunctionSamples &FS,
91 bool isCallsiteSample = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 std::unique_ptr<ProfileSummary> getSummary();
93};
94
95/// This is called when a count is seen in the profile.
96void ProfileSummaryBuilder::addCount(uint64_t Count) {
97 TotalCount += Count;
98 if (Count > MaxCount)
99 MaxCount = Count;
100 NumCounts++;
101 CountFrequencies[Count]++;
102}
103
104} // end namespace llvm
105
106#endif // LLVM_PROFILEDATA_PROFILECOMMON_H