ProfileCommon.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(SummaryEntryVector &DS, uint64_t Percentile);
  62. };
  63. class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
  64. uint64_t MaxInternalBlockCount = 0;
  65. inline void addEntryCount(uint64_t Count);
  66. inline void addInternalCount(uint64_t Count);
  67. public:
  68. InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
  69. : ProfileSummaryBuilder(std::move(Cutoffs)) {}
  70. void addRecord(const InstrProfRecord &);
  71. std::unique_ptr<ProfileSummary> getSummary();
  72. };
  73. class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
  74. public:
  75. SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
  76. : ProfileSummaryBuilder(std::move(Cutoffs)) {}
  77. void addRecord(const sampleprof::FunctionSamples &FS,
  78. bool isCallsiteSample = false);
  79. std::unique_ptr<ProfileSummary> computeSummaryForProfiles(
  80. const StringMap<sampleprof::FunctionSamples> &Profiles);
  81. std::unique_ptr<ProfileSummary> getSummary();
  82. };
  83. /// This is called when a count is seen in the profile.
  84. void ProfileSummaryBuilder::addCount(uint64_t Count) {
  85. TotalCount += Count;
  86. if (Count > MaxCount)
  87. MaxCount = Count;
  88. NumCounts++;
  89. CountFrequencies[Count]++;
  90. }
  91. } // end namespace llvm
  92. #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif