27 for (
const auto &
Block : O) {
29 auto &
B = Blocks.back();
55 H.Size = Extractor.getU32(&
Offset);
56 if (
Offset == CurrentOffset)
58 Twine(
"Error parsing block header size at offset '") +
59 Twine(CurrentOffset) +
"'",
60 std::make_error_code(std::errc::invalid_argument));
62 H.Number = Extractor.getU32(&
Offset);
63 if (
Offset == CurrentOffset)
65 Twine(
"Error parsing block header number at offset '") +
66 Twine(CurrentOffset) +
"'",
67 std::make_error_code(std::errc::invalid_argument));
69 H.Thread = Extractor.getU64(&
Offset);
70 if (
Offset == CurrentOffset)
72 Twine(
"Error parsing block header thread id at offset '") +
73 Twine(CurrentOffset) +
"'",
74 std::make_error_code(std::errc::invalid_argument));
81 std::vector<Profile::FuncID> Path;
82 auto CurrentOffset =
Offset;
85 FuncId = Extractor.getSigned(&
Offset, 4);
86 if (CurrentOffset ==
Offset)
88 Twine(
"Error parsing path at offset '") +
Twine(CurrentOffset) +
"'",
89 std::make_error_code(std::errc::invalid_argument));
91 Path.push_back(FuncId);
92 }
while (FuncId != 0);
93 return std::move(Path);
102 auto CurrentOffset =
Offset;
103 D.CallCount = Extractor.getU64(&
Offset);
104 if (CurrentOffset ==
Offset)
106 Twine(
"Error parsing call counts at offset '") +
Twine(CurrentOffset) +
108 std::make_error_code(std::errc::invalid_argument));
110 D.CumulativeLocalTime = Extractor.getU64(&
Offset);
111 if (CurrentOffset ==
Offset)
113 Twine(
"Error parsing cumulative local time at offset '") +
114 Twine(CurrentOffset) +
"'",
115 std::make_error_code(std::errc::invalid_argument));
120 if (
B.PathData.empty())
122 "Block may not have empty path data.",
123 std::make_error_code(std::errc::invalid_argument));
125 Blocks.emplace_back(std::move(
B));
130 auto It = PathIDMap.find(
P);
131 if (It == PathIDMap.end())
134 std::make_error_code(std::errc::invalid_argument));
135 std::vector<Profile::FuncID> Path;
137 Path.push_back(
Node->Func);
138 return std::move(Path);
148 auto It = RootToLeafPath.begin();
149 auto PathRoot = *It++;
151 find_if(Roots, [PathRoot](TrieNode *
N) {
return N->Func == PathRoot; });
154 TrieNode *
Node =
nullptr;
155 if (RootIt == Roots.end()) {
156 NodeStorage.emplace_back();
157 Node = &NodeStorage.back();
158 Node->Func = PathRoot;
159 Roots.push_back(
Node);
165 while (It != RootToLeafPath.end()) {
166 auto NodeFuncID = *It++;
167 auto CalleeIt =
find_if(
Node->Callees, [NodeFuncID](TrieNode *
N) {
168 return N->Func == NodeFuncID;
170 if (CalleeIt ==
Node->Callees.end()) {
171 NodeStorage.emplace_back();
172 auto NewNode = &NodeStorage.back();
173 NewNode->Func = NodeFuncID;
174 NewNode->Caller =
Node;
175 Node->Callees.push_back(NewNode);
194 using PathDataMapPtr = std::unique_ptr<PathDataMap>;
197 ThreadProfileIndexMap ThreadProfileIndex;
199 for (
const auto &
P : {std::ref(L), std::ref(R)})
200 for (
const auto &
Block :
P.get()) {
201 ThreadProfileIndexMap::iterator It;
202 std::tie(It, std::ignore) = ThreadProfileIndex.insert(
203 {
Block.Thread, std::make_unique<PathDataMap>()});
204 for (
const auto &PathAndData :
Block.PathData) {
205 auto &PathID = PathAndData.first;
206 auto &
Data = PathAndData.second;
209 PathDataMap::iterator PathDataIt;
211 std::tie(PathDataIt, Inserted) = It->second->insert({NewPathID,
Data});
213 auto &ExistingData = PathDataIt->second;
214 ExistingData.CallCount +=
Data.CallCount;
215 ExistingData.CumulativeLocalTime +=
Data.CumulativeLocalTime;
220 for (
const auto &IndexedThreadBlock : ThreadProfileIndex) {
221 PathDataVector PathAndData;
222 PathAndData.reserve(IndexedThreadBlock.second->size());
223 copy(*IndexedThreadBlock.second, std::back_inserter(PathAndData));
225 Merged.
addBlock({IndexedThreadBlock.first, std::move(PathAndData)}));
233 PathDataMap PathData;
235 for (
const auto &
P : {std::ref(L), std::ref(R)})
236 for (
const auto &
Block :
P.get())
237 for (
const auto &PathAndData :
Block.PathData) {
238 auto &PathId = PathAndData.first;
239 auto &
Data = PathAndData.second;
242 PathDataMap::iterator PathDataIt;
244 std::tie(PathDataIt, Inserted) = PathData.insert({NewPathID,
Data});
246 auto &ExistingData = PathDataIt->second;
247 ExistingData.CallCount +=
Data.CallCount;
248 ExistingData.CumulativeLocalTime +=
Data.CumulativeLocalTime;
253 PathDataVector
Block;
254 Block.reserve(PathData.size());
255 copy(PathData, std::back_inserter(
Block));
268 Twine(
"Cannot get filesize of '") + Filename +
"'", EC);
277 Twine(
"Cannot mmap profile '") + Filename +
"'", EC);
288 return HeaderOrError.takeError();
292 const auto &Header = HeaderOrError.get();
297 return PathOrError.takeError();
298 const auto &Path = PathOrError.get();
303 return DataOrError.takeError();
304 auto &
Data = DataOrError.get();
308 {{P.internPath(Path), std::move(Data)}}}))
319 Profile::FuncID FuncId;
336 for (
const auto &E :
T) {
337 auto &TSD = ThreadStacks[E.TId];
343 TSD.push_back({E.TSC, E.FuncId});
353 while (!TSD.empty()) {
354 auto Top = TSD.back();
358 std::mem_fn(&StackEntry::FuncId));
359 auto InternedPath =
P.internPath(Path);
360 auto &TPD = ThreadPathData[E.TId][InternedPath];
362 TPD.CumulativeLocalTime += FunctionLocalTime;
367 if (Top.FuncId == E.FuncId)
386 for (
const auto &ThreadPaths : ThreadPathData) {
387 const auto &TID = ThreadPaths.first;
388 const auto &PathsData = ThreadPaths.second;
389 if (
auto E =
P.addBlock({
391 std::vector<std::pair<Profile::PathID, Profile::Data>>(
392 PathsData.begin(), PathsData.end()),
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Expected< BlockHeader > readBlockHeader(DataExtractor &Extractor, uint64_t &Offset)
static Expected< Profile::Data > readData(DataExtractor &Extractor, uint64_t &Offset)
static Expected< std::vector< Profile::FuncID > > readPath(DataExtractor &Extractor, uint64_t &Offset)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This class represents a memory mapped file.
LLVM_ABI size_t size() const
@ readonly
May only access map via const_data as read only.
LLVM_ABI char * data() const
Profile instances are thread-compatible.
Profile & operator=(Profile &&O) noexcept
LLVM_ABI PathID internPath(ArrayRef< FuncID > P)
The stack represented in |P| must be in stack order (leaf to root).
LLVM_ABI Expected< std::vector< FuncID > > expandPath(PathID P) const
Provides a sequence of function IDs from a previously interned PathID.
LLVM_ABI Error addBlock(Block &&B)
Appends a fully-formed Block instance into the Profile.
A Trace object represents the records that have been loaded from XRay log files generated by instrume...
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
LLVM_ABI Profile mergeProfilesByStack(const Profile &L, const Profile &R)
This algorithm will merge two Profile instances into a single Profile instance, aggregating blocks by...
LLVM_ABI Expected< Profile > profileFromTrace(const Trace &T)
This function takes a Trace and creates a Profile instance from it.
LLVM_ABI Expected< Profile > loadProfile(StringRef Filename)
This function will attempt to load an XRay Profiling Mode profile from the provided |Filename|.
LLVM_ABI Profile mergeProfilesByThread(const Profile &L, const Profile &R)
This algorithm will merge two Profile instances into a single Profile instance, aggregating blocks by...
This is an optimization pass for GlobalISel generic memory operations.
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
auto reverse(ContainerTy &&C)
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
constexpr T AbsoluteDifference(U X, V Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
OutputIt copy(R &&Range, OutputIt Out)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
std::vector< std::pair< PathID, Data > > PathData