2019-03-30 16:42:48 +08:00
|
|
|
//===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===//
|
|
|
|
//
|
2019-04-16 05:02:47 +08: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 16:42:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-04-16 05:02:47 +08:00
|
|
|
// This file implements hierarchical time profiler.
|
2019-03-30 16:42:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2019-04-09 20:18:44 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2019-04-16 05:02:47 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-04-16 14:35:07 +08:00
|
|
|
#include "llvm/Support/JSON.h"
|
2019-12-02 21:10:44 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-01-04 00:04:19 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include <algorithm>
|
2019-03-30 16:42:48 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
2020-01-04 00:04:19 +08:00
|
|
|
#include <mutex>
|
2019-03-30 16:42:48 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
2020-01-31 17:27:21 +08:00
|
|
|
using namespace llvm;
|
2019-03-30 16:42:48 +08:00
|
|
|
|
2020-01-04 00:04:19 +08:00
|
|
|
namespace {
|
|
|
|
std::mutex Mu;
|
2020-01-31 17:27:21 +08:00
|
|
|
// List of all instances
|
|
|
|
std::vector<TimeTraceProfiler *>
|
2020-01-04 00:04:19 +08:00
|
|
|
ThreadTimeTraceProfilerInstances; // guarded by Mu
|
2020-01-31 17:27:21 +08:00
|
|
|
// Per Thread instance
|
|
|
|
LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
|
2020-01-04 00:04:19 +08:00
|
|
|
} // namespace
|
|
|
|
|
2019-03-30 16:42:48 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2020-01-31 17:27:21 +08:00
|
|
|
TimeTraceProfiler *getTimeTraceProfilerInstance() {
|
|
|
|
return TimeTraceProfilerInstance;
|
|
|
|
}
|
2019-03-30 16:42:48 +08:00
|
|
|
|
|
|
|
typedef duration<steady_clock::rep, steady_clock::period> DurationType;
|
2019-09-05 17:26:04 +08:00
|
|
|
typedef time_point<steady_clock> TimePointType;
|
2019-04-09 20:18:44 +08:00
|
|
|
typedef std::pair<size_t, DurationType> CountAndDurationType;
|
|
|
|
typedef std::pair<std::string, CountAndDurationType>
|
|
|
|
NameAndCountAndDurationType;
|
2019-03-30 16:42:48 +08:00
|
|
|
|
|
|
|
struct Entry {
|
2019-11-29 00:20:59 +08:00
|
|
|
const TimePointType Start;
|
2019-09-05 17:26:04 +08:00
|
|
|
TimePointType End;
|
2019-11-29 00:20:59 +08:00
|
|
|
const std::string Name;
|
|
|
|
const std::string Detail;
|
2019-04-16 05:02:47 +08:00
|
|
|
|
2019-09-05 17:26:04 +08: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-29 00:20:59 +08:00
|
|
|
Detail(std::move(Dt)) {}
|
2019-09-05 17:26:04 +08: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 16:42:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TimeTraceProfiler {
|
2019-12-02 21:10:44 +08:00
|
|
|
TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
|
|
|
|
: StartTime(steady_clock::now()), ProcName(ProcName),
|
2020-01-04 00:04:19 +08:00
|
|
|
Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
|
2019-03-30 16:42:48 +08:00
|
|
|
|
|
|
|
void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
|
2019-09-05 17:26:04 +08:00
|
|
|
Stack.emplace_back(steady_clock::now(), TimePointType(), std::move(Name),
|
2019-04-16 05:02:47 +08:00
|
|
|
Detail());
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void end() {
|
|
|
|
assert(!Stack.empty() && "Must call begin() first");
|
|
|
|
auto &E = Stack.back();
|
2019-09-05 17:26:04 +08: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 16:42:48 +08:00
|
|
|
|
2019-08-20 06:58:26 +08:00
|
|
|
// Only include sections longer or equal to TimeTraceGranularity msec.
|
2019-09-05 17:26:04 +08:00
|
|
|
if (duration_cast<microseconds>(Duration).count() >= TimeTraceGranularity)
|
2019-03-30 16:42:48 +08: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 20:18:44 +08:00
|
|
|
auto &CountAndTotal = CountAndTotalPerName[E.Name];
|
|
|
|
CountAndTotal.first++;
|
2019-09-05 17:26:04 +08:00
|
|
|
CountAndTotal.second += Duration;
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-01-04 00:04:19 +08:00
|
|
|
// Write events from this TimeTraceProfilerInstance and
|
|
|
|
// ThreadTimeTraceProfilerInstances.
|
2019-04-16 05:02:47 +08:00
|
|
|
void Write(raw_pwrite_stream &OS) {
|
2020-01-04 00:04:19 +08:00
|
|
|
// Acquire Mutex as reading ThreadTimeTraceProfilerInstances.
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2019-03-30 16:42:48 +08:00
|
|
|
assert(Stack.empty() &&
|
|
|
|
"All profiler sections should be ended when calling Write");
|
2020-01-04 00:04:19 +08: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 20:51:42 +08:00
|
|
|
json::OStream J(OS);
|
|
|
|
J.objectBegin();
|
|
|
|
J.attributeBegin("traceEvents");
|
|
|
|
J.arrayBegin();
|
2019-03-30 16:42:48 +08:00
|
|
|
|
|
|
|
// Emit all events for the main flame graph.
|
2020-01-04 00:04:19 +08:00
|
|
|
auto writeEvent = [&](const auto &E, uint64_t Tid) {
|
2019-09-05 17:26:04 +08:00
|
|
|
auto StartUs = E.getFlameGraphStartUs(StartTime);
|
|
|
|
auto DurUs = E.getFlameGraphDurUs();
|
2019-04-16 14:35:07 +08:00
|
|
|
|
2019-04-25 20:51:42 +08:00
|
|
|
J.object([&]{
|
|
|
|
J.attribute("pid", 1);
|
2020-01-04 00:04:19 +08:00
|
|
|
J.attribute("tid", int64_t(Tid));
|
2019-04-25 20:51:42 +08:00
|
|
|
J.attribute("ph", "X");
|
|
|
|
J.attribute("ts", StartUs);
|
|
|
|
J.attribute("dur", DurUs);
|
|
|
|
J.attribute("name", E.Name);
|
2019-12-11 19:49:42 +08:00
|
|
|
if (!E.Detail.empty()) {
|
|
|
|
J.attributeObject("args", [&] { J.attribute("detail", E.Detail); });
|
|
|
|
}
|
2019-04-16 14:35:07 +08:00
|
|
|
});
|
2020-01-04 00:04:19 +08: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 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit totals by section name as additional "thread" events, sorted from
|
|
|
|
// longest one.
|
2020-01-04 00:04:19 +08: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-29 03:23:46 +08:00
|
|
|
StringRef Key = Stat.getKey();
|
2020-01-04 00:04:19 +08: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 20:18:44 +08:00
|
|
|
std::vector<NameAndCountAndDurationType> SortedTotals;
|
2020-01-04 00:04:19 +08:00
|
|
|
SortedTotals.reserve(AllCountAndTotalPerName.size());
|
|
|
|
for (const auto &Total : AllCountAndTotalPerName)
|
2020-01-29 08:01:09 +08:00
|
|
|
SortedTotals.emplace_back(std::string(Total.getKey()), Total.getValue());
|
2019-04-16 05:02:47 +08:00
|
|
|
|
|
|
|
llvm::sort(SortedTotals.begin(), SortedTotals.end(),
|
|
|
|
[](const NameAndCountAndDurationType &A,
|
|
|
|
const NameAndCountAndDurationType &B) {
|
|
|
|
return A.second.second > B.second.second;
|
|
|
|
});
|
2020-01-04 00:04:19 +08: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 14:35:07 +08:00
|
|
|
|
2019-04-25 20:51:42 +08:00
|
|
|
J.object([&]{
|
|
|
|
J.attribute("pid", 1);
|
2020-01-04 00:04:19 +08:00
|
|
|
J.attribute("tid", int64_t(TotalTid));
|
2019-04-25 20:51:42 +08:00
|
|
|
J.attribute("ph", "X");
|
|
|
|
J.attribute("ts", 0);
|
|
|
|
J.attribute("dur", DurUs);
|
2020-01-04 00:04:19 +08:00
|
|
|
J.attribute("name", "Total " + Total.first);
|
2019-04-25 20:51:42 +08:00
|
|
|
J.attributeObject("args", [&] {
|
|
|
|
J.attribute("count", int64_t(Count));
|
|
|
|
J.attribute("avg ms", int64_t(DurUs / Count / 1000));
|
|
|
|
});
|
2019-04-16 14:35:07 +08:00
|
|
|
});
|
|
|
|
|
2020-01-04 00:04:19 +08:00
|
|
|
++TotalTid;
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit metadata event with process name.
|
2019-04-25 20:51:42 +08: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 21:10:44 +08:00
|
|
|
J.attributeObject("args", [&] { J.attribute("name", ProcName); });
|
2019-04-16 14:35:07 +08:00
|
|
|
});
|
|
|
|
|
2019-04-25 20:51:42 +08:00
|
|
|
J.arrayEnd();
|
|
|
|
J.attributeEnd();
|
|
|
|
J.objectEnd();
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 05:02:47 +08:00
|
|
|
SmallVector<Entry, 16> Stack;
|
|
|
|
SmallVector<Entry, 128> Entries;
|
2019-04-09 20:18:44 +08:00
|
|
|
StringMap<CountAndDurationType> CountAndTotalPerName;
|
2019-11-29 00:20:59 +08:00
|
|
|
const TimePointType StartTime;
|
2019-12-02 21:10:44 +08:00
|
|
|
const std::string ProcName;
|
2020-01-04 00:04:19 +08:00
|
|
|
const uint64_t Tid;
|
2019-07-24 22:55:40 +08:00
|
|
|
|
|
|
|
// Minimum time granularity (in microseconds)
|
2019-11-29 00:20:59 +08:00
|
|
|
const unsigned TimeTraceGranularity;
|
2019-03-30 16:42:48 +08:00
|
|
|
};
|
|
|
|
|
2019-12-02 21:10:44 +08:00
|
|
|
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
|
|
|
|
StringRef ProcName) {
|
2019-03-30 16:42:48 +08:00
|
|
|
assert(TimeTraceProfilerInstance == nullptr &&
|
|
|
|
"Profiler should not be initialized");
|
2019-12-24 19:31:48 +08:00
|
|
|
TimeTraceProfilerInstance = new TimeTraceProfiler(
|
2019-12-02 21:10:44 +08:00
|
|
|
TimeTraceGranularity, llvm::sys::path::filename(ProcName));
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
2020-01-04 00:04:19 +08:00
|
|
|
// Removes all TimeTraceProfilerInstances.
|
|
|
|
// Called from main thread.
|
2019-03-30 16:42:48 +08:00
|
|
|
void timeTraceProfilerCleanup() {
|
2019-12-24 19:31:48 +08:00
|
|
|
delete TimeTraceProfilerInstance;
|
2020-01-04 00:04:19 +08: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 19:31:48 +08:00
|
|
|
TimeTraceProfilerInstance = nullptr;
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 05:02:47 +08:00
|
|
|
void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
|
2019-03-30 16:42:48 +08:00
|
|
|
assert(TimeTraceProfilerInstance != nullptr &&
|
|
|
|
"Profiler object can't be null");
|
|
|
|
TimeTraceProfilerInstance->Write(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
2020-01-29 03:23:46 +08:00
|
|
|
TimeTraceProfilerInstance->begin(std::string(Name),
|
|
|
|
[&]() { return std::string(Detail); });
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerBegin(StringRef Name,
|
|
|
|
llvm::function_ref<std::string()> Detail) {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
2020-01-29 03:23:46 +08:00
|
|
|
TimeTraceProfilerInstance->begin(std::string(Name), Detail);
|
2019-03-30 16:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeTraceProfilerEnd() {
|
|
|
|
if (TimeTraceProfilerInstance != nullptr)
|
|
|
|
TimeTraceProfilerInstance->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|