1 //===- Timer.cpp ----------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lld/Common/Timer.h"
11 #include "lld/Common/ErrorHandler.h"
12 #include "llvm/Support/Format.h"
17 ScopedTimer::ScopedTimer(Timer &T) : T(&T) { T.start(); }
19 void ScopedTimer::stop() {
26 ScopedTimer::~ScopedTimer() { stop(); }
28 Timer::Timer(llvm::StringRef Name) : Name(Name), Parent(nullptr) {}
29 Timer::Timer(llvm::StringRef Name, Timer &Parent)
30 : Name(Name), Parent(&Parent) {}
33 if (Parent && Total.count() == 0)
34 Parent->Children.push_back(this);
35 StartTime = std::chrono::high_resolution_clock::now();
39 Total += (std::chrono::high_resolution_clock::now() - StartTime);
42 Timer &Timer::root() {
43 static Timer RootTimer("Total Link Time");
48 double TotalDuration = static_cast<double>(root().millis());
50 // We want to print the grand total under all the intermediate phases, so we
51 // print all children first, then print the total under that.
52 for (const auto &Child : Children)
53 Child->print(1, TotalDuration);
55 message(std::string(49, '-'));
57 root().print(0, root().millis(), false);
60 double Timer::millis() const {
61 return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(
66 void Timer::print(int Depth, double TotalDuration, bool Recurse) const {
67 double P = 100.0 * millis() / TotalDuration;
70 llvm::raw_svector_ostream Stream(Str);
71 std::string S = std::string(Depth * 2, ' ') + Name + std::string(":");
72 Stream << format("%-30s%5d ms (%5.1f%%)", S.c_str(), (int)millis(), P);
77 for (const auto &Child : Children)
78 Child->print(Depth + 1, TotalDuration);