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