ProfileCommon.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ProfileCommon.h - Common profiling APIs. -----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains data structures and functions common to both instrumented
  15. // and sample profiling.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_PROFILEDATA_PROFILECOMMON_H
  19. #define LLVM_PROFILEDATA_PROFILECOMMON_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/IR/ProfileSummary.h"
  22. #include "llvm/ProfileData/InstrProf.h"
  23. #include "llvm/ProfileData/SampleProf.h"
  24. #include "llvm/Support/Error.h"
  25. #include <algorithm>
  26. #include <cstdint>
  27. #include <functional>
  28. #include <map>
  29. #include <memory>
  30. #include <vector>
  31. namespace llvm {
  32. namespace sampleprof {
  33. class FunctionSamples;
  34. } // end namespace sampleprof
  35. inline const char *getHotSectionPrefix() { return "hot"; }
  36. inline const char *getUnlikelySectionPrefix() { return "unlikely"; }
  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. protected:
  45. SummaryEntryVector DetailedSummary;
  46. uint64_t TotalCount = 0;
  47. uint64_t MaxCount = 0;
  48. uint64_t MaxFunctionCount = 0;
  49. uint32_t NumCounts = 0;
  50. uint32_t NumFunctions = 0;
  51. ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
  52. : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
  53. ~ProfileSummaryBuilder() = default;
  54. inline void addCount(uint64_t Count);
  55. void computeDetailedSummary();
  56. public:
  57. /// A vector of useful cutoff values for detailed summary.
  58. static const ArrayRef<uint32_t> DefaultCutoffs;
  59. /// Find the summary entry for a desired percentile of counts.
  60. static const ProfileSummaryEntry &
  61. getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile);
  62. static uint64_t getHotCountThreshold(const SummaryEntryVector &DS);
  63. static uint64_t getColdCountThreshold(const SummaryEntryVector &DS);
  64. };
  65. class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
  66. uint64_t MaxInternalBlockCount = 0;
  67. inline void addEntryCount(uint64_t Count);
  68. inline void addInternalCount(uint64_t Count);
  69. public:
  70. InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
  71. : ProfileSummaryBuilder(std::move(Cutoffs)) {}
  72. void addRecord(const InstrProfRecord &);
  73. std::unique_ptr<ProfileSummary> getSummary();
  74. };
  75. class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
  76. public:
  77. SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
  78. : ProfileSummaryBuilder(std::move(Cutoffs)) {}
  79. void addRecord(const sampleprof::FunctionSamples &FS,
  80. bool isCallsiteSample = false);
  81. std::unique_ptr<ProfileSummary>
  82. computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles);
  83. std::unique_ptr<ProfileSummary> getSummary();
  84. };
  85. /// This is called when a count is seen in the profile.
  86. void ProfileSummaryBuilder::addCount(uint64_t Count) {
  87. TotalCount += Count;
  88. if (Count > MaxCount)
  89. MaxCount = Count;
  90. NumCounts++;
  91. CountFrequencies[Count]++;
  92. }
  93. } // end namespace llvm
  94. #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif