[TimeProfiler] Fix some style issues. NFC

Reviewed By: broadwaylamb, russell.gallop

Differential Revision: https://reviews.llvm.org/D78153
This commit is contained in:
Fangrui Song 2020-04-14 13:42:00 -07:00
parent cd5d5ce235
commit 7c13550967
1 changed files with 39 additions and 51 deletions

View File

@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/TimeProfiler.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/JSON.h"
@ -26,18 +27,14 @@
using namespace std::chrono;
using namespace llvm;
namespace {
std::mutex Mu;
static std::mutex Mu;
// List of all instances
std::vector<TimeTraceProfiler *>
ThreadTimeTraceProfilerInstances; // guarded by Mu
static std::vector<TimeTraceProfiler *>
ThreadTimeTraceProfilerInstances; // GUARDED_BY(Mu)
// Per Thread instance
LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
} // namespace
static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
namespace llvm {
TimeTraceProfiler *getTimeTraceProfilerInstance() {
TimeTraceProfiler *llvm::getTimeTraceProfilerInstance() {
return TimeTraceProfilerInstance;
}
@ -47,6 +44,7 @@ typedef std::pair<size_t, DurationType> CountAndDurationType;
typedef std::pair<std::string, CountAndDurationType>
NameAndCountAndDurationType;
namespace {
struct Entry {
const TimePointType Start;
TimePointType End;
@ -72,8 +70,9 @@ struct Entry {
.count();
}
};
} // namespace
struct TimeTraceProfiler {
struct llvm::TimeTraceProfiler {
TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
: StartTime(steady_clock::now()), ProcName(ProcName),
Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
@ -85,7 +84,7 @@ struct TimeTraceProfiler {
void end() {
assert(!Stack.empty() && "Must call begin() first");
auto &E = Stack.back();
Entry &E = Stack.back();
E.End = steady_clock::now();
// Check that end times monotonically increase.
@ -120,15 +119,14 @@ struct TimeTraceProfiler {
// Write events from this TimeTraceProfilerInstance and
// ThreadTimeTraceProfilerInstances.
void Write(raw_pwrite_stream &OS) {
void write(raw_pwrite_stream &OS) {
// Acquire Mutex as reading ThreadTimeTraceProfilerInstances.
std::lock_guard<std::mutex> Lock(Mu);
assert(Stack.empty() &&
"All profiler sections should be ended when calling Write");
assert(std::all_of(ThreadTimeTraceProfilerInstances.begin(),
ThreadTimeTraceProfilerInstances.end(),
[](const auto &TTP) { return TTP->Stack.empty(); }) &&
"All profiler sections should be ended when calling Write");
"All profiler sections should be ended when calling write");
assert(llvm::all_of(ThreadTimeTraceProfilerInstances,
[](const auto &TTP) { return TTP->Stack.empty(); }) &&
"All profiler sections should be ended when calling write");
json::OStream J(OS);
J.objectBegin();
@ -152,22 +150,18 @@ struct TimeTraceProfiler {
}
});
};
for (const auto &E : Entries) {
for (const Entry &E : Entries)
writeEvent(E, this->Tid);
}
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
for (const auto &E : TTP->Entries) {
for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
for (const Entry &E : TTP->Entries)
writeEvent(E, TTP->Tid);
}
}
// Emit totals by section name as additional "thread" events, sorted from
// longest one.
// Find highest used thread id.
uint64_t MaxTid = this->Tid;
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
MaxTid = std::max(MaxTid, TTP->Tid);
}
// Combine all CountAndTotalPerName from threads into one.
StringMap<CountAndDurationType> AllCountAndTotalPerName;
@ -178,29 +172,25 @@ struct TimeTraceProfiler {
CountAndTotal.first += Value.first;
CountAndTotal.second += Value.second;
};
for (const auto &Stat : CountAndTotalPerName) {
for (const auto &Stat : CountAndTotalPerName)
combineStat(Stat);
}
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
for (const auto &Stat : TTP->CountAndTotalPerName) {
for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
for (const auto &Stat : TTP->CountAndTotalPerName)
combineStat(Stat);
}
}
std::vector<NameAndCountAndDurationType> SortedTotals;
SortedTotals.reserve(AllCountAndTotalPerName.size());
for (const auto &Total : AllCountAndTotalPerName)
SortedTotals.emplace_back(std::string(Total.getKey()), Total.getValue());
llvm::sort(SortedTotals.begin(), SortedTotals.end(),
[](const NameAndCountAndDurationType &A,
const NameAndCountAndDurationType &B) {
return A.second.second > B.second.second;
});
llvm::sort(SortedTotals, [](const NameAndCountAndDurationType &A,
const NameAndCountAndDurationType &B) {
return A.second.second > B.second.second;
});
// Report totals on separate threads of tracing file.
uint64_t TotalTid = MaxTid + 1;
for (const auto &Total : SortedTotals) {
for (const NameAndCountAndDurationType &Total : SortedTotals) {
auto DurUs = duration_cast<microseconds>(Total.second.second).count();
auto Count = AllCountAndTotalPerName[Total.first].first;
@ -247,8 +237,8 @@ struct TimeTraceProfiler {
const unsigned TimeTraceGranularity;
};
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
StringRef ProcName) {
void llvm::timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
StringRef ProcName) {
assert(TimeTraceProfilerInstance == nullptr &&
"Profiler should not be initialized");
TimeTraceProfilerInstance = new TimeTraceProfiler(
@ -257,7 +247,7 @@ void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
// Removes all TimeTraceProfilerInstances.
// Called from main thread.
void timeTraceProfilerCleanup() {
void llvm::timeTraceProfilerCleanup() {
delete TimeTraceProfilerInstance;
std::lock_guard<std::mutex> Lock(Mu);
for (auto TTP : ThreadTimeTraceProfilerInstances)
@ -267,20 +257,20 @@ void timeTraceProfilerCleanup() {
// Finish TimeTraceProfilerInstance on a worker thread.
// This doesn't remove the instance, just moves the pointer to global vector.
void timeTraceProfilerFinishThread() {
void llvm::timeTraceProfilerFinishThread() {
std::lock_guard<std::mutex> Lock(Mu);
ThreadTimeTraceProfilerInstances.push_back(TimeTraceProfilerInstance);
TimeTraceProfilerInstance = nullptr;
}
void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
void llvm::timeTraceProfilerWrite(raw_pwrite_stream &OS) {
assert(TimeTraceProfilerInstance != nullptr &&
"Profiler object can't be null");
TimeTraceProfilerInstance->Write(OS);
TimeTraceProfilerInstance->write(OS);
}
Error timeTraceProfilerWrite(StringRef PreferredFileName,
StringRef FallbackFileName) {
Error llvm::timeTraceProfilerWrite(StringRef PreferredFileName,
StringRef FallbackFileName) {
assert(TimeTraceProfilerInstance != nullptr &&
"Profiler object can't be null");
@ -299,21 +289,19 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
return Error::success();
}
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
void llvm::timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
if (TimeTraceProfilerInstance != nullptr)
TimeTraceProfilerInstance->begin(std::string(Name),
[&]() { return std::string(Detail); });
}
void timeTraceProfilerBegin(StringRef Name,
llvm::function_ref<std::string()> Detail) {
void llvm::timeTraceProfilerBegin(StringRef Name,
llvm::function_ref<std::string()> Detail) {
if (TimeTraceProfilerInstance != nullptr)
TimeTraceProfilerInstance->begin(std::string(Name), Detail);
}
void timeTraceProfilerEnd() {
void llvm::timeTraceProfilerEnd() {
if (TimeTraceProfilerInstance != nullptr)
TimeTraceProfilerInstance->end();
}
} // namespace llvm