diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp --- a/llvm/lib/Support/TimeProfiler.cpp +++ b/llvm/lib/Support/TimeProfiler.cpp @@ -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 - ThreadTimeTraceProfilerInstances; // guarded by Mu +static std::vector + ThreadTimeTraceProfilerInstances; // GUARDED_BY(Mu) // Per Thread instance -LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; -} // namespace - -namespace llvm { +static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; -TimeTraceProfiler *getTimeTraceProfilerInstance() { +TimeTraceProfiler *llvm::getTimeTraceProfilerInstance() { return TimeTraceProfilerInstance; } @@ -47,6 +44,7 @@ typedef std::pair NameAndCountAndDurationType; +namespace { struct Entry { const TimePointType Start; TimePointType End; @@ -72,8 +70,9 @@ .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 @@ 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 @@ // 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 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 @@ } }); }; - 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 AllCountAndTotalPerName; @@ -178,29 +172,25 @@ 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 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(Total.second.second).count(); auto Count = AllCountAndTotalPerName[Total.first].first; @@ -247,8 +237,8 @@ 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 @@ // Removes all TimeTraceProfilerInstances. // Called from main thread. -void timeTraceProfilerCleanup() { +void llvm::timeTraceProfilerCleanup() { delete TimeTraceProfilerInstance; std::lock_guard Lock(Mu); for (auto TTP : ThreadTimeTraceProfilerInstances) @@ -267,20 +257,20 @@ // 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 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 @@ 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 Detail) { +void llvm::timeTraceProfilerBegin(StringRef Name, + llvm::function_ref Detail) { if (TimeTraceProfilerInstance != nullptr) TimeTraceProfilerInstance->begin(std::string(Name), Detail); } -void timeTraceProfilerEnd() { +void llvm::timeTraceProfilerEnd() { if (TimeTraceProfilerInstance != nullptr) TimeTraceProfilerInstance->end(); } - -} // namespace llvm