28 for (
const auto &
Block : O) {
30 auto &
B = Blocks.back();
51static Expected<BlockHeader> readBlockHeader(DataExtractor &Extractor,
55 H.Size = Extractor.getU32(&
Offset);
56 if (
Offset == CurrentOffset)
57 return make_error<StringError>(
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)
64 return make_error<StringError>(
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)
71 return make_error<StringError>(
72 Twine(
"Error parsing block header thread id at offset '") +
73 Twine(CurrentOffset) +
"'",
74 std::make_error_code(std::errc::invalid_argument));
78static Expected<std::vector<Profile::FuncID>> readPath(DataExtractor &Extractor,
81 std::vector<Profile::FuncID>
Path;
82 auto CurrentOffset =
Offset;
86 if (CurrentOffset ==
Offset)
87 return make_error<StringError>(
88 Twine(
"Error parsing path at offset '") + Twine(CurrentOffset) +
"'",
89 std::make_error_code(std::errc::invalid_argument));
93 return std::move(Path);
96static Expected<Profile::Data> readData(DataExtractor &Extractor,
102 auto CurrentOffset =
Offset;
103 D.CallCount = Extractor.getU64(&
Offset);
104 if (CurrentOffset ==
Offset)
105 return make_error<StringError>(
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)
112 return make_error<StringError>(
113 Twine(
"Error parsing cumulative local time at offset '") +
114 Twine(CurrentOffset) +
"'",
115 std::make_error_code(std::errc::invalid_argument));
122 if (
B.PathData.empty())
123 return make_error<StringError>(
124 "Block may not have empty path data.",
125 std::make_error_code(std::errc::invalid_argument));
127 Blocks.emplace_back(std::move(
B));
132 auto It = PathIDMap.find(
P);
133 if (It == PathIDMap.end())
134 return make_error<StringError>(
136 std::make_error_code(std::errc::invalid_argument));
137 std::vector<Profile::FuncID> Path;
139 Path.push_back(
Node->Func);
140 return std::move(Path);
150 auto It = RootToLeafPath.begin();
151 auto PathRoot = *It++;
153 find_if(Roots, [PathRoot](TrieNode *
N) {
return N->Func == PathRoot; });
156 TrieNode *
Node =
nullptr;
157 if (RootIt == Roots.
end()) {
158 NodeStorage.emplace_back();
159 Node = &NodeStorage.back();
160 Node->Func = PathRoot;
167 while (It != RootToLeafPath.end()) {
168 auto NodeFuncID = *It++;
169 auto CalleeIt =
find_if(
Node->Callees, [NodeFuncID](TrieNode *
N) {
170 return N->Func == NodeFuncID;
172 if (CalleeIt ==
Node->Callees.end()) {
173 NodeStorage.emplace_back();
174 auto NewNode = &NodeStorage.back();
175 NewNode->Func = NodeFuncID;
176 NewNode->Caller =
Node;
177 Node->Callees.push_back(NewNode);
196 using PathDataMapPtr = std::unique_ptr<PathDataMap>;
199 ThreadProfileIndexMap ThreadProfileIndex;
201 for (
const auto &
P : {std::ref(L), std::ref(R)})
202 for (
const auto &
Block :
P.get()) {
203 ThreadProfileIndexMap::iterator It;
204 std::tie(It, std::ignore) = ThreadProfileIndex.
insert(
205 {
Block.Thread, std::make_unique<PathDataMap>()});
206 for (
const auto &PathAndData :
Block.PathData) {
207 auto &PathID = PathAndData.first;
208 auto &
Data = PathAndData.second;
211 PathDataMap::iterator PathDataIt;
213 std::tie(PathDataIt, Inserted) = It->second->insert({NewPathID,
Data});
215 auto &ExistingData = PathDataIt->second;
216 ExistingData.CallCount +=
Data.CallCount;
217 ExistingData.CumulativeLocalTime +=
Data.CumulativeLocalTime;
222 for (
const auto &IndexedThreadBlock : ThreadProfileIndex) {
223 PathDataVector PathAndData;
224 PathAndData.reserve(IndexedThreadBlock.second->size());
225 copy(*IndexedThreadBlock.second, std::back_inserter(PathAndData));
227 Merged.
addBlock({IndexedThreadBlock.first, std::move(PathAndData)}));
235 PathDataMap PathData;
237 for (
const auto &
P : {std::ref(L), std::ref(R)})
238 for (
const auto &
Block :
P.get())
239 for (
const auto &PathAndData :
Block.PathData) {
240 auto &PathId = PathAndData.first;
241 auto &
Data = PathAndData.second;
244 PathDataMap::iterator PathDataIt;
246 std::tie(PathDataIt, Inserted) = PathData.insert({NewPathID,
Data});
248 auto &ExistingData = PathDataIt->second;
249 ExistingData.CallCount +=
Data.CallCount;
250 ExistingData.CumulativeLocalTime +=
Data.CumulativeLocalTime;
255 PathDataVector
Block;
256 Block.reserve(PathData.size());
257 copy(PathData, std::back_inserter(
Block));
269 return make_error<StringError>(
270 Twine(
"Cannot get filesize of '") + Filename +
"'", EC);
278 return make_error<StringError>(
279 Twine(
"Cannot mmap profile '") + Filename +
"'", EC);
288 auto HeaderOrError = readBlockHeader(Extractor,
Offset);
290 return HeaderOrError.takeError();
294 const auto &Header = HeaderOrError.get();
297 auto PathOrError = readPath(Extractor,
Offset);
299 return PathOrError.takeError();
300 const auto &Path = PathOrError.get();
303 auto DataOrError = readData(Extractor,
Offset);
305 return DataOrError.takeError();
306 auto &
Data = DataOrError.get();
310 {{P.internPath(Path), std::move(Data)}}}))
338 for (
const auto &
E :
T) {
339 auto &TSD = ThreadStacks[
E.TId];
341 case RecordTypes::ENTER:
342 case RecordTypes::ENTER_ARG:
345 TSD.push_back({
E.TSC,
E.FuncId});
348 case RecordTypes::EXIT:
349 case RecordTypes::TAIL_EXIT:
355 while (!TSD.empty()) {
356 auto Top = TSD.back();
360 std::mem_fn(&StackEntry::FuncId));
361 auto InternedPath =
P.internPath(Path);
362 auto &TPD = ThreadPathData[
E.TId][InternedPath];
364 TPD.CumulativeLocalTime += FunctionLocalTime;
369 if (Top.FuncId ==
E.FuncId)
378 case RecordTypes::CUSTOM_EVENT:
379 case RecordTypes::TYPED_EVENT:
388 for (
const auto &ThreadPaths : ThreadPathData) {
389 const auto &TID = ThreadPaths.first;
390 const auto &PathsData = ThreadPaths.second;
391 if (
auto E =
P.addBlock({
393 std::vector<std::pair<Profile::PathID, Profile::Data>>(
394 PathsData.begin(), PathsData.end()),
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
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.
void push_back(const T &Elt)
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.
@ readonly
May only access map via const_data as read only.
Profile instances are thread-compatible.
Profile & operator=(Profile &&O) noexcept
PathID internPath(ArrayRef< FuncID > P)
The stack represented in |P| must be in stack order (leaf to root).
Expected< std::vector< FuncID > > expandPath(PathID P) const
Provides a sequence of function IDs from a previously interned PathID.
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...
std::error_code closeFile(file_t &F)
Close the file object.
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.
Profile mergeProfilesByStack(const Profile &L, const Profile &R)
This algorithm will merge two Profile instances into a single Profile instance, aggregating blocks by...
Expected< Profile > profileFromTrace(const Trace &T)
This function takes a Trace and creates a Profile instance from it.
Expected< Profile > loadProfile(StringRef Filename)
This function will attempt to load an XRay Profiling Mode profile from the provided |Filename|.
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)
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
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