Profile.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //===- Profile.h - XRay Profile Abstraction -------------------------------===//
  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. // Defines the XRay Profile class representing the latency profile generated by
  10. // XRay's profiling mode.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_XRAY_PROFILE_H
  14. #define LLVM_XRAY_PROFILE_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Support/Error.h"
  19. #include <list>
  20. #include <utility>
  21. #include <vector>
  22. namespace llvm {
  23. namespace xray {
  24. class Profile;
  25. // We forward declare the Trace type for turning a Trace into a Profile.
  26. class Trace;
  27. /// This function will attempt to load an XRay Profiling Mode profile from the
  28. /// provided |Filename|.
  29. ///
  30. /// For any errors encountered in the loading of the profile data from
  31. /// |Filename|, this function will return an Error condition appropriately.
  32. Expected<Profile> loadProfile(StringRef Filename);
  33. /// This algorithm will merge two Profile instances into a single Profile
  34. /// instance, aggregating blocks by Thread ID.
  35. Profile mergeProfilesByThread(const Profile &L, const Profile &R);
  36. /// This algorithm will merge two Profile instances into a single Profile
  37. /// instance, aggregating blocks by function call stack.
  38. Profile mergeProfilesByStack(const Profile &L, const Profile &R);
  39. /// This function takes a Trace and creates a Profile instance from it.
  40. Expected<Profile> profileFromTrace(const Trace &T);
  41. /// Profile instances are thread-compatible.
  42. class Profile {
  43. public:
  44. using ThreadID = uint64_t;
  45. using PathID = unsigned;
  46. using FuncID = int32_t;
  47. struct Data {
  48. uint64_t CallCount;
  49. uint64_t CumulativeLocalTime;
  50. };
  51. struct Block {
  52. ThreadID Thread;
  53. std::vector<std::pair<PathID, Data>> PathData;
  54. };
  55. /// Provides a sequence of function IDs from a previously interned PathID.
  56. ///
  57. /// Returns an error if |P| had not been interned before into the Profile.
  58. ///
  59. Expected<std::vector<FuncID>> expandPath(PathID P) const;
  60. /// The stack represented in |P| must be in stack order (leaf to root). This
  61. /// will always return the same PathID for |P| that has the same sequence.
  62. PathID internPath(ArrayRef<FuncID> P);
  63. /// Appends a fully-formed Block instance into the Profile.
  64. ///
  65. /// Returns an error condition in the following cases:
  66. ///
  67. /// - The PathData component of the Block is empty
  68. ///
  69. Error addBlock(Block &&B);
  70. Profile() = default;
  71. ~Profile() = default;
  72. Profile(Profile &&O) noexcept
  73. : Blocks(std::move(O.Blocks)), NodeStorage(std::move(O.NodeStorage)),
  74. Roots(std::move(O.Roots)), PathIDMap(std::move(O.PathIDMap)),
  75. NextID(O.NextID) {}
  76. Profile &operator=(Profile &&O) noexcept {
  77. Blocks = std::move(O.Blocks);
  78. NodeStorage = std::move(O.NodeStorage);
  79. Roots = std::move(O.Roots);
  80. PathIDMap = std::move(O.PathIDMap);
  81. NextID = O.NextID;
  82. return *this;
  83. }
  84. Profile(const Profile &);
  85. Profile &operator=(const Profile &);
  86. friend void swap(Profile &L, Profile &R) {
  87. using std::swap;
  88. swap(L.Blocks, R.Blocks);
  89. swap(L.NodeStorage, R.NodeStorage);
  90. swap(L.Roots, R.Roots);
  91. swap(L.PathIDMap, R.PathIDMap);
  92. swap(L.NextID, R.NextID);
  93. }
  94. private:
  95. using BlockList = std::list<Block>;
  96. struct TrieNode {
  97. FuncID Func = 0;
  98. std::vector<TrieNode *> Callees{};
  99. TrieNode *Caller = nullptr;
  100. PathID ID = 0;
  101. };
  102. // List of blocks associated with a Profile.
  103. BlockList Blocks;
  104. // List of TrieNode elements we've seen.
  105. std::list<TrieNode> NodeStorage;
  106. // List of call stack roots.
  107. SmallVector<TrieNode *, 4> Roots;
  108. // Reverse mapping between a PathID to a TrieNode*.
  109. DenseMap<PathID, TrieNode *> PathIDMap;
  110. // Used to identify paths.
  111. PathID NextID = 1;
  112. public:
  113. using const_iterator = BlockList::const_iterator;
  114. const_iterator begin() const { return Blocks.begin(); }
  115. const_iterator end() const { return Blocks.end(); }
  116. bool empty() const { return Blocks.empty(); }
  117. };
  118. } // namespace xray
  119. } // namespace llvm
  120. #endif