29 typedef duration<steady_clock::rep, steady_clock::period>
DurationType;
32 typedef std::pair<std::string, CountAndDurationType>
41 Entry(TimePointType &&S, TimePointType &&
E, std::string &&
N, std::string &&Dt)
42 : Start(
std::move(S)), End(
std::move(
E)), Name(
std::move(
N)),
43 Detail(
std::move(Dt)) {}
49 return (time_point_cast<microseconds>(Start) -
50 time_point_cast<microseconds>(StartTime))
55 return (time_point_cast<microseconds>(End) -
56 time_point_cast<microseconds>(Start))
63 : StartTime(steady_clock::
now()), ProcName(ProcName),
64 TimeTraceGranularity(TimeTraceGranularity) {}
72 assert(!Stack.empty() &&
"Must call begin() first");
73 auto &
E = Stack.back();
78 (E.getFlameGraphStartUs(StartTime) + E.getFlameGraphDurUs() >=
79 Entries.back().getFlameGraphStartUs(StartTime) +
80 Entries.back().getFlameGraphDurUs())) &&
81 "TimeProfiler scope ended earlier than previous scope");
84 DurationType Duration = E.End - E.Start;
87 if (duration_cast<microseconds>(Duration).count() >= TimeTraceGranularity)
88 Entries.emplace_back(E);
96 return Val.Name == E.Name;
98 auto &CountAndTotal = CountAndTotalPerName[E.Name];
99 CountAndTotal.first++;
100 CountAndTotal.second += Duration;
108 "All profiler sections should be ended when calling Write");
111 J.attributeBegin(
"traceEvents");
115 for (
const auto &
E : Entries) {
116 auto StartUs =
E.getFlameGraphStartUs(StartTime);
117 auto DurUs =
E.getFlameGraphDurUs();
120 J.attribute(
"pid", 1);
121 J.attribute(
"tid", 0);
122 J.attribute(
"ph",
"X");
123 J.attribute(
"ts", StartUs);
124 J.attribute(
"dur", DurUs);
125 J.attribute(
"name",
E.Name);
126 if (!
E.Detail.empty()) {
127 J.attributeObject(
"args", [&] { J.attribute(
"detail",
E.Detail); });
135 std::vector<NameAndCountAndDurationType> SortedTotals;
136 SortedTotals.reserve(CountAndTotalPerName.size());
137 for (
const auto &
E : CountAndTotalPerName)
138 SortedTotals.emplace_back(
E.getKey(),
E.getValue());
140 llvm::sort(SortedTotals.begin(), SortedTotals.end(),
143 return A.second.second >
B.second.second;
145 for (
const auto &
E : SortedTotals) {
146 auto DurUs = duration_cast<microseconds>(
E.second.second).
count();
147 auto Count = CountAndTotalPerName[
E.first].first;
150 J.attribute(
"pid", 1);
151 J.attribute(
"tid", Tid);
152 J.attribute(
"ph",
"X");
153 J.attribute(
"ts", 0);
154 J.attribute(
"dur", DurUs);
155 J.attribute(
"name",
"Total " +
E.first);
156 J.attributeObject(
"args", [&] {
157 J.attribute(
"count", int64_t(Count));
158 J.attribute(
"avg ms", int64_t(DurUs / Count / 1000));
167 J.attribute(
"cat",
"");
168 J.attribute(
"pid", 1);
169 J.attribute(
"tid", 0);
170 J.attribute(
"ts", 0);
171 J.attribute(
"ph",
"M");
172 J.attribute(
"name",
"process_name");
173 J.attributeObject(
"args", [&] { J.attribute(
"name", ProcName); });
193 assert(TimeTraceProfilerInstance ==
nullptr &&
194 "Profiler should not be initialized");
201 TimeTraceProfilerInstance =
nullptr;
205 assert(TimeTraceProfilerInstance !=
nullptr &&
206 "Profiler object can't be null");
207 TimeTraceProfilerInstance->
Write(OS);
211 if (TimeTraceProfilerInstance !=
nullptr)
212 TimeTraceProfilerInstance->
begin(Name, [&]() {
return Detail; });
217 if (TimeTraceProfilerInstance !=
nullptr)
218 TimeTraceProfilerInstance->
begin(Name, Detail);
222 if (TimeTraceProfilerInstance !=
nullptr)
223 TimeTraceProfilerInstance->
end();
void Write(raw_pwrite_stream &OS)
This class represents lattice values for constants.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringMap< CountAndDurationType > CountAndTotalPerName
std::pair< size_t, DurationType > CountAndDurationType
An efficient, type-erasing, non-owning reference to a callable.
void timeTraceProfilerEnd()
Manually end the last time section.
const TimePointType Start
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
SmallVector< Entry, 16 > Stack
std::pair< std::string, CountAndDurationType > NameAndCountAndDurationType
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName)
Initialize the time trace profiler.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
void begin(std::string Name, llvm::function_ref< std::string()> Detail)
void timeTraceProfilerCleanup()
Cleanup the time trace profiler, if it was initialized.
duration< steady_clock::rep, steady_clock::period > DurationType
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Entry(TimePointType &&S, TimePointType &&E, std::string &&N, std::string &&Dt)
TimeTraceProfiler(unsigned TimeTraceGranularity=0, StringRef ProcName="")
void timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
steady_clock::rep getFlameGraphStartUs(TimePointType StartTime) const
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
void sort(IteratorTy Start, IteratorTy End)
const TimePointType StartTime
const std::string ProcName
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const unsigned TimeTraceGranularity
void timeTraceProfilerWrite(raw_pwrite_stream &OS)
Write profiling data to output file.
TimeTraceProfiler * TimeTraceProfilerInstance
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
SmallVector< Entry, 128 > Entries
This file supports working with JSON data.
An abstract base class for streams implementations that also support a pwrite operation.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
steady_clock::rep getFlameGraphDurUs() const
time_point< steady_clock > TimePointType