Profile.h 4.4 KB

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