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 @@ -26,18 +26,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 +static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; -namespace llvm { - -TimeTraceProfiler *getTimeTraceProfilerInstance() { +TimeTraceProfiler *llvm::getTimeTraceProfilerInstance() { return TimeTraceProfilerInstance; } @@ -47,6 +43,7 @@ typedef std::pair NameAndCountAndDurationType; +namespace { struct Entry { const TimePointType Start; TimePointType End; @@ -72,8 +69,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) {} @@ -120,15 +118,15 @@ // 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"); + "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"); 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,14 +172,11 @@ 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()); @@ -247,8 +238,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 +248,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 +258,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 +290,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