[clang] [clang][unittests] Check TimeProfilerTest graphs by event order (PR #237163)
Mattéo Rizza Murgier via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 02:53:08 PST 2026
================
@@ -200,69 +205,108 @@ std::string buildTraceGraph(StringRef Json) {
continue;
Events.emplace_back(
- EventRecord{TimestampBegin, TimestampEnd, Name, Metadata});
+ EventRecord{TimestampBegin, TimestampEnd, Name, Metadata, IsInstant});
}
- // There can be nested events that are very fast, for example:
- // {"name":"EvaluateAsBooleanCondition",... ,"ts":2380,"dur":1}
- // {"name":"EvaluateAsRValue",... ,"ts":2380,"dur":1}
- // Therefore we should reverse the events list, so that events that have
- // started earlier are first in the list.
- // Then do a stable sort, we need it for the trace graph.
- std::reverse(Events.begin(), Events.end());
- llvm::stable_sort(Events, [](const auto &lhs, const auto &rhs) {
- return std::make_pair(lhs.TimestampBegin, -lhs.TimestampEnd) <
- std::make_pair(rhs.TimestampBegin, -rhs.TimestampEnd);
- });
-
- std::stringstream Stream;
- // Write a newline for better testing with multiline string literal.
- Stream << "\n";
-
- // Keep the current event stack.
- std::stack<const EventRecord *> EventStack;
- for (const auto &Event : Events) {
- // Pop every event in the stack until meeting the parent event.
- while (!EventStack.empty()) {
- bool InsideCurrentEvent =
- Event.TimestampBegin >= EventStack.top()->TimestampBegin &&
- Event.TimestampEnd <= EventStack.top()->TimestampEnd;
-
- // Presumably due to timer rounding, PerformPendingInstantiations often
- // appear to be within the timer interval of the immediately previous
- // event group. We always know these events occur at level 1 in our
- // tests, so keep popping until the stack is back at the root.
- if (InsideCurrentEvent && Event.Name == "PerformPendingInstantiations" &&
- EventStack.size() >= 2) {
- InsideCurrentEvent = false;
- }
-
- if (!InsideCurrentEvent)
- EventStack.pop();
- else
- break;
- }
- EventStack.push(&Event);
+ // Parse the expected graph: each line is indented one "| " per level.
+ struct ExpectedNode {
+ unsigned Depth;
+ std::string Label;
+ int Parent;
+ SmallVector<int, 4> Children;
+ };
+ std::vector<ExpectedNode> Nodes;
+ SmallVector<int, 8> LastAtDepth;
+ SmallVector<StringRef, 32> Lines;
+ ExpectedGraph.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
+ for (StringRef Line : Lines) {
+ unsigned Depth = 0;
+ while (Line.consume_front("| "))
+ ++Depth;
+ if (Depth > LastAtDepth.size())
+ return "expected graph skips a level";
+ LastAtDepth.resize(Depth);
+ int Parent = Depth ? LastAtDepth.back() : -1;
+ int Index = Nodes.size();
+ LastAtDepth.push_back(Index);
+ if (Parent >= 0)
+ Nodes[Parent].Children.push_back(Index);
+ Nodes.push_back({Depth, Line.str(), Parent, {}});
+ }
- // Write indentaion, name, detail, newline.
- for (size_t i = 1; i < EventStack.size(); ++i) {
- Stream << "| ";
- }
- Stream.write(Event.Name.data(), Event.Name.size());
- if (!Event.Metadata.empty()) {
- Stream << " (";
- Stream.write(Event.Metadata.data(), Event.Metadata.size());
- Stream << ")";
- }
- Stream << "\n";
+ // Timestamps are truncated to microseconds, so two sibling events that fit
+ // in the same microsecond look nested and the graph cannot be rebuilt from
+ // the timestamps alone. The order of events in the trace is exact, though:
+ // an event is written when it ends, and instant events are written right
+ // after their parent. Check the expected graph's end order against it, then
+ // that each parent contains its children and that siblings do not overlap,
+ // and print the graph with any violation marked.
+ StringSet<> InstantLabels;
+ auto Label = [](const EventRecord &E) {
+ return E.Metadata.empty() ? E.Name : E.Name + " (" + E.Metadata + ")";
+ };
+ for (const EventRecord &Event : Events)
+ if (Event.IsInstant)
+ InstantLabels.insert(Label(Event));
+ auto IsInstant = [&](int N) {
+ return InstantLabels.contains(Nodes[N].Label);
+ };
----------------
matteo-rm wrote:
Maybe we can make `IsInstant` a field of `ExpectedNode` and set it when initially parsing the graph?
https://github.com/llvm/llvm-project/pull/237163
More information about the cfe-commits
mailing list