Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ProfileCommon.h - Common profiling APIs. -----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/Support/Error.h" |
| 21 | #include <algorithm> |
| 22 | #include <cstdint> |
| 23 | #include <functional> |
| 24 | #include <map> |
| 25 | #include <memory> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | namespace sampleprof { |
| 31 | |
| 32 | class FunctionSamples; |
| 33 | |
| 34 | } // end namespace sampleprof |
| 35 | |
| 36 | inline const char *getHotSectionPrefix() { return ".hot"; } |
| 37 | inline const char *getUnlikelySectionPrefix() { return ".unlikely"; } |
| 38 | |
| 39 | class ProfileSummaryBuilder { |
| 40 | private: |
| 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 | |
| 47 | protected: |
| 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 | |
| 62 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | /// A vector of useful cutoff values for detailed summary. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 64 | static const ArrayRef<uint32_t> DefaultCutoffs; |
| 65 | }; |
| 66 | |
| 67 | class InstrProfSummaryBuilder final : public ProfileSummaryBuilder { |
| 68 | uint64_t MaxInternalBlockCount = 0; |
| 69 | |
| 70 | inline void addEntryCount(uint64_t Count); |
| 71 | inline void addInternalCount(uint64_t Count); |
| 72 | |
| 73 | public: |
| 74 | InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs) |
| 75 | : ProfileSummaryBuilder(std::move(Cutoffs)) {} |
| 76 | |
| 77 | void addRecord(const InstrProfRecord &); |
| 78 | std::unique_ptr<ProfileSummary> getSummary(); |
| 79 | }; |
| 80 | |
| 81 | class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder { |
| 82 | public: |
| 83 | SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs) |
| 84 | : ProfileSummaryBuilder(std::move(Cutoffs)) {} |
| 85 | |
| 86 | void addRecord(const sampleprof::FunctionSamples &FS); |
| 87 | std::unique_ptr<ProfileSummary> getSummary(); |
| 88 | }; |
| 89 | |
| 90 | /// This is called when a count is seen in the profile. |
| 91 | void ProfileSummaryBuilder::addCount(uint64_t Count) { |
| 92 | TotalCount += Count; |
| 93 | if (Count > MaxCount) |
| 94 | MaxCount = Count; |
| 95 | NumCounts++; |
| 96 | CountFrequencies[Count]++; |
| 97 | } |
| 98 | |
| 99 | } // end namespace llvm |
| 100 | |
| 101 | #endif // LLVM_PROFILEDATA_PROFILECOMMON_H |