2019-03-30 09:42:48 +01:00
|
|
|
//===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===//
|
|
|
|
//
|
2019-04-15 23:02:47 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-30 09:42:48 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-04-15 23:02:47 +02:00
|
|
|
// This file implements hierarchical time profiler.
|
2019-03-30 09:42:48 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2019-04-09 14:18:44 +02:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2019-04-15 23:02:47 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-04-16 08:35:07 +02:00
|
|
|
#include "llvm/Support/JSON.h"
|
2019-12-02 14:10:44 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-01-03 17:04:19 +01:00
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include <algorithm>
|
2019-03-30 09:42:48 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
2020-01-03 17:04:19 +01:00
|
|
|
#include <mutex>
|
2019-03-30 09:42:48 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
2020-01-31 10:27:21 +01:00
|
|
|
using namespace llvm;
|
2019-03-30 09:42:48 +01:00
|
|
|
|
2020-01-03 17:04:19 +01:00
|
|
|
namespace {
|
|
|
|
std::mutex Mu;
|
2020-01-31 10:27:21 +01:00
|
|
|
// List of all instances
|
|
|
|
std::vector<TimeTraceProfiler *>
|
2020-01-03 17:04:19 +01:00
|
|
|
ThreadTimeTraceProfilerInstances; // guarded by Mu
|
2020-01-31 10:27:21 +01:00
|
|
|
// Per Thread instance
|
|
|
|
LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
|
2020-01-03 17:04:19 +01:00
|
|
|
} // namespace
|
|
|
|
|
2019-03-30 09:42:48 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2020-01-31 10:27:21 +01:00
|
|
|
TimeTraceProfiler *getTimeTraceProfilerInstance() {
|
|
|
|
return TimeTraceProfilerInstance;
|
|
|
|
}
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
typedef duration<steady_clock::rep, steady_clock::period> DurationType;
|
2019-09-05 11:26:04 +02:00
|
|
|
typedef time_point<steady_clock> TimePointType;
|
2019-04-09 14:18:44 +02:00
|
|
|
typedef std::pair<size_t, DurationType> CountAndDurationType;
|
|
|
|
typedef std::pair<std::string, CountAndDurationType>
|
|
|
|
NameAndCountAndDurationType;
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
struct Entry {
|
2019-11-28 17:20:59 +01:00
|
|
|
const TimePointType Start;
|
2019-09-05 11:26:04 +02:00
|
|
|
TimePointType End;
|
2019-11-28 17:20:59 +01:00
|
|
|
const std::string Name;
|
|
|
|
const std::string Detail;
|
2019-04-15 23:02:47 +02:00
|
|
|
|
2019-09-05 11:26:04 +02:00
|
|
|
Entry(TimePointType &&S, TimePointType &&E, std::string &&N, std::string &&Dt)
|
|
|
|
: Start(std::move(S)), End(std::move(E)), Name(std::move(N)),
|
2019-11-28 17:20:59 +01:00
|
|
|
Detail(std::move(Dt)) {}
|
2019-09-05 11:26:04 +02:00
|
|
|
|
|
|
|
// Calculate timings for FlameGraph. Cast time points to microsecond precision
|
|
|
|
// rather than casting duration. This avoid truncation issues causing inner
|
|
|
|
// scopes overruning outer scopes.
|
|
|
|
steady_clock::rep getFlameGraphStartUs(TimePointType StartTime) const {
|
|
|
|
return (time_point_cast<microseconds>(Start) -
|
|
|
|
time_point_cast<microseconds>(StartTime))
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
steady_clock::rep getFlameGraphDurUs() const {
|
|
|
|
return (time_point_cast<microseconds>(End) -
|
|
|
|
time_point_cast<microseconds>(Start))
|
|
|
|
.count();
|
|
|
|
}
|
2019-03-30 09:42:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TimeTraceProfiler {
|
2019-12-02 14:10:44 +01:00
|
|
|
TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
|
|
|
|
: StartTime(steady_clock::now()), ProcName(ProcName),
|
2020-01-03 17:04:19 +01:00
|
|
|
Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
|
2019-09-05 11:26:04 +02:00
|
|
|
Stack.emplace_back(steady_clock::now(), TimePointType(), std::move(Name),
|
2019-04-15 23:02:47 +02:00
|
|
|
Detail());
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void end() {
|
|
|
|
assert(!Stack.empty() && "Must call begin() first");
|
|
|
|
auto &E = Stack.back();
|
2019-09-05 11:26:04 +02:00
|
|
|
E.End = steady_clock::now();
|
|
|
|
|
|
|
|
// Check that end times monotonically increase.
|
|
|
|
assert((Entries.empty() ||
|
|
|
|
(E.getFlameGraphStartUs(StartTime) + E.getFlameGraphDurUs() >=
|
|
|
|
Entries.back().getFlameGraphStartUs(StartTime) +
|
|
|
|
Entries.back().getFlameGraphDurUs())) &&
|
|
|
|
"TimeProfiler scope ended earlier than previous scope");
|
|
|
|
|
|
|
|
// Calculate duration at full precision for overall counts.
|
|
|
|
DurationType Duration = E.End - E.Start;
|
2019-03-30 09:42:48 +01:00
|
|
|
|
2019-08-20 00:58:26 +02:00
|
|
|
// Only include sections longer or equal to TimeTraceGranularity msec.
|
2019-09-05 11:26:04 +02:00
|
|
|
if (duration_cast<microseconds>(Duration).count() >= TimeTraceGranularity)
|
2019-03-30 09:42:48 +01:00
|
|
|
Entries.emplace_back(E);
|
|
|
|
|
|
|
|
// Track total time taken by each "name", but only the topmost levels of
|
|
|
|
// them; e.g. if there's a template instantiation that instantiates other
|
|
|
|
// templates from within, we only want to add the topmost one. "topmost"
|
|
|
|
// happens to be the ones that don't have any currently open entries above
|
|
|
|
// itself.
|
|
|
|
if (std::find_if(++Stack.rbegin(), Stack.rend(), [&](const Entry &Val) {
|
|
|
|
return Val.Name == E.Name;
|
|
|
|
}) == Stack.rend()) {
|
2019-04-09 14:18:44 +02:00
|
|
|
auto &CountAndTotal = CountAndTotalPerName[E.Name];
|
|
|
|
CountAndTotal.first++;
|
2019-09-05 11:26:04 +02:00
|
|
|
CountAndTotal.second += Duration;
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:04:19 +01:00
|
|
|
// Write events from this TimeTraceProfilerInstance and
|
|
|
|
// ThreadTimeTraceProfilerInstances.
|
2019-04-15 23:02:47 +02:00
|
|
|
void Write(raw_pwrite_stream &OS) {
|
2020-01-03 17:04:19 +01:00
|
|
|
// Acquire Mutex as reading ThreadTimeTraceProfilerInstances.
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2019-03-30 09:42:48 +01:00
|
|
|
assert(Stack.empty() &&
|
|
|
|
"All profiler sections should be ended when calling Write");
|
2020-01-03 17:04:19 +01:00
|
|
|
assert(std::all_of(ThreadTimeTraceProfilerInstances.begin(),
|
|
|
|
ThreadTimeTraceProfilerInstances.end(),
|
|
|
|
[](const auto &TTP) { return TTP->Stack.empty(); }) &&
|
|
|
|
"All profiler sections should be ended when calling Write");
|
|
|
|
|
2019-04-25 14:51:42 +02:00
|
|
|
json::OStream J(OS);
|
|
|
|
J.objectBegin();
|
|
|
|
J.attributeBegin("traceEvents");
|
|
|
|
J.arrayBegin();
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
// Emit all events for the main flame graph.
|
2020-01-03 17:04:19 +01:00
|
|
|
auto writeEvent = [&](const auto &E, uint64_t Tid) {
|
2019-09-05 11:26:04 +02:00
|
|
|
auto StartUs = E.getFlameGraphStartUs(StartTime);
|
|
|
|
auto DurUs = E.getFlameGraphDurUs();
|
2019-04-16 08:35:07 +02:00
|
|
|
|
2019-04-25 14:51:42 +02:00
|
|
|
J.object([&]{
|
|
|
|
J.attribute("pid", 1);
|
2020-01-03 17:04:19 +01:00
|
|
|
J.attribute("tid", int64_t(Tid));
|
2019-04-25 14:51:42 +02:00
|
|
|
J.attribute("ph", "X");
|
|
|
|
J.attribute("ts", StartUs);
|
|
|
|
J.attribute("dur", DurUs);
|
|
|
|
J.attribute("name", E.Name);
|
2019-12-11 12:49:42 +01:00
|
|
|
if (!E.Detail.empty()) {
|
|
|
|
J.attributeObject("args", [&] { J.attribute("detail", E.Detail); });
|
|
|
|
}
|
2019-04-16 08:35:07 +02:00
|
|
|
});
|
2020-01-03 17:04:19 +01:00
|
|
|
};
|
|
|
|
for (const auto &E : Entries) {
|
|
|
|
writeEvent(E, this->Tid);
|
|
|
|
}
|
|
|
|
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
|
|
|
|
for (const auto &E : TTP->Entries) {
|
|
|
|
writeEvent(E, TTP->Tid);
|
|
|
|
}
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit totals by section name as additional "thread" events, sorted from
|
|
|
|
// longest one.
|
2020-01-03 17:04:19 +01:00
|
|
|
// Find highest used thread id.
|
|
|
|
uint64_t MaxTid = this->Tid;
|
|
|
|
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
|
|
|
|
MaxTid = std::max(MaxTid, TTP->Tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine all CountAndTotalPerName from threads into one.
|
|
|
|
StringMap<CountAndDurationType> AllCountAndTotalPerName;
|
|
|
|
auto combineStat = [&](const auto &Stat) {
|
2020-01-28 20:23:46 +01:00
|
|
|
StringRef Key = Stat.getKey();
|
2020-01-03 17:04:19 +01:00
|
|
|
auto Value = Stat.getValue();
|
|
|
|
auto &CountAndTotal = AllCountAndTotalPerName[Key];
|
|
|
|
CountAndTotal.first += Value.first;
|
|
|
|
CountAndTotal.second += Value.second;
|
|
|
|
};
|
|
|
|
for (const auto &Stat : CountAndTotalPerName) {
|
|
|
|
combineStat(Stat);
|
|
|
|
}
|
|
|
|
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
|
|
|
|
for (const auto &Stat : TTP->CountAndTotalPerName) {
|
|
|
|
combineStat(Stat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:18:44 +02:00
|
|
|
std::vector<NameAndCountAndDurationType> SortedTotals;
|
2020-01-03 17:04:19 +01:00
|
|
|
SortedTotals.reserve(AllCountAndTotalPerName.size());
|
|
|
|
for (const auto &Total : AllCountAndTotalPerName)
|
2020-01-29 01:01:09 +01:00
|
|
|
SortedTotals.emplace_back(std::string(Total.getKey()), Total.getValue());
|
2019-04-15 23:02:47 +02:00
|
|
|
|
|
|
|
llvm::sort(SortedTotals.begin(), SortedTotals.end(),
|
|
|
|
[](const NameAndCountAndDurationType &A,
|
|
|
|
const NameAndCountAndDurationType &B) {
|
|
|
|
return A.second.second > B.second.second;
|
|
|
|
});
|
2020-01-03 17:04:19 +01:00
|
|
|
|
|
|
|
// Report totals on separate threads of tracing file.
|
|
|
|
uint64_t TotalTid = MaxTid + 1;
|
|
|
|
for (const auto &Total : SortedTotals) {
|
|
|
|
auto DurUs = duration_cast<microseconds>(Total.second.second).count();
|
|
|
|
auto Count = AllCountAndTotalPerName[Total.first].first;
|
2019-04-16 08:35:07 +02:00
|
|
|
|
2019-04-25 14:51:42 +02:00
|
|
|
J.object([&]{
|
|
|
|
J.attribute("pid", 1);
|
2020-01-03 17:04:19 +01:00
|
|
|
J.attribute("tid", int64_t(TotalTid));
|
2019-04-25 14:51:42 +02:00
|
|
|
J.attribute("ph", "X");
|
|
|
|
J.attribute("ts", 0);
|
|
|
|
J.attribute("dur", DurUs);
|
2020-01-03 17:04:19 +01:00
|
|
|
J.attribute("name", "Total " + Total.first);
|
2019-04-25 14:51:42 +02:00
|
|
|
J.attributeObject("args", [&] {
|
|
|
|
J.attribute("count", int64_t(Count));
|
|
|
|
J.attribute("avg ms", int64_t(DurUs / Count / 1000));
|
|
|
|
});
|
2019-04-16 08:35:07 +02:00
|
|
|
});
|
|
|
|
|
2020-01-03 17:04:19 +01:00
|
|
|
++TotalTid;
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit metadata event with process name.
|
2019-04-25 14:51:42 +02:00
|
|
|
J.object([&] {
|
|
|
|
J.attribute("cat", "");
|
|
|
|
J.attribute("pid", 1);
|
|
|
|
J.attribute("tid", 0);
|
|
|
|
J.attribute("ts", 0);
|
|
|
|
J.attribute("ph", "M");
|
|
|
|
J.attribute("name", "process_name");
|
2019-12-02 14:10:44 +01:00
|
|
|
J.attributeObject("args", [&] { J.attribute("name", ProcName); });
|
2019-04-16 08:35:07 +02:00
|
|
|
});
|
|
|
|
|
2019-04-25 14:51:42 +02:00
|
|
|
J.arrayEnd();
|
|
|
|
J.attributeEnd();
|
|
|
|
J.objectEnd();
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:02:47 +02:00
|
|
|
SmallVector<Entry, 16> Stack;
|
|
|
|
SmallVector<Entry, 128> Entries;
|
2019-04-09 14:18:44 +02:00
|
|
|
StringMap<CountAndDurationType> CountAndTotalPerName;
|
2019-11-28 17:20:59 +01:00
|
|
|
const TimePointType StartTime;
|
2019-12-02 14:10:44 +01:00
|
|
|
const std::string ProcName;
|
2020-01-03 17:04:19 +01:00
|
|
|
const uint64_t Tid;
|
2019-07-24 16:55:40 +02:00
|
|
|
|
|
|
|
// Minimum time granularity (in microseconds)
|
2019-11-28 17:20:59 +01:00
|
|
|
const unsigned TimeTraceGranularity;
|
2019-03-30 09:42:48 +01:00
|
|
|
};
|
|
|
|
|
2019-12-02 14:10:44 +01:00
|
|
|
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
|
|
|
|
StringRef ProcName) {
|
2019-03-30 09:42:48 +01:00
|
|
|
assert(TimeTraceProfilerInstance == nullptr &&
|
|
|
|
"Profiler should not be initialized");
|
2019-12-24 12:31:48 +01:00
|
|
|
TimeTraceProfilerInstance = new TimeTraceProfiler(
|
2019-12-02 14:10:44 +01:00
|
|
|
TimeTraceGranularity, llvm::sys::path::filename(ProcName));
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:04:19 +01:00
|
|
|
// Removes all TimeTraceProfilerInstances.
|
|
|
|
// Called from main thread.
|
2019-03-30 09:42:48 +01:00
|
|
|
void timeTraceProfilerCleanup() {
|
2019-12-24 12:31:48 +01:00
|
|
|
delete TimeTraceProfilerInstance;
|
2020-01-03 17:04:19 +01:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
for (auto TTP : ThreadTimeTraceProfilerInstances)
|
|
|
|
delete TTP;
|
|
|
|
ThreadTimeTraceProfilerInstances.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish TimeTraceProfilerInstance on a worker thread.
|
|
|
|
// This doesn't remove the instance, just moves the pointer to global vector.
|
|
|
|
void timeTraceProfilerFinishThread() {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
ThreadTimeTraceProfilerInstances.push_back(TimeTraceProfilerInstance);
|
2019-12-24 12:31:48 +01:00
|
|
|
TimeTraceProfilerInstance = nullptr;
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:02:47 +02:00
|
|
|
void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
|
2019-03-30 09:42:48 +01:00
|
|
|
assert(TimeTraceProfilerInstance != nullptr &&
|
|
|
|
"Profiler object can't be null");
|
|
|
|
TimeTraceProfilerInstance->Write(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
2020-01-28 20:23:46 +01:00
|
|
|
TimeTraceProfilerInstance->begin(std::string(Name),
|
|
|
|
[&]() { return std::string(Detail); });
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerBegin(StringRef Name,
|
|
|
|
llvm::function_ref<std::string()> Detail) {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
2020-01-28 20:23:46 +01:00
|
|
|
TimeTraceProfilerInstance->begin(std::string(Name), Detail);
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerEnd() {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
|
|
|
TimeTraceProfilerInstance->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|