]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/Common/Timer.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / Common / Timer.cpp
1 //===- Timer.cpp ----------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lld/Common/Timer.h"
11 #include "lld/Common/ErrorHandler.h"
12 #include "llvm/Support/Format.h"
13
14 using namespace lld;
15 using namespace llvm;
16
17 ScopedTimer::ScopedTimer(Timer &T) : T(&T) { T.start(); }
18
19 void ScopedTimer::stop() {
20   if (!T)
21     return;
22   T->stop();
23   T = nullptr;
24 }
25
26 ScopedTimer::~ScopedTimer() { stop(); }
27
28 Timer::Timer(llvm::StringRef Name) : Name(Name), Parent(nullptr) {}
29 Timer::Timer(llvm::StringRef Name, Timer &Parent)
30     : Name(Name), Parent(&Parent) {}
31
32 void Timer::start() {
33   if (Parent && Total.count() == 0)
34     Parent->Children.push_back(this);
35   StartTime = std::chrono::high_resolution_clock::now();
36 }
37
38 void Timer::stop() {
39   Total += (std::chrono::high_resolution_clock::now() - StartTime);
40 }
41
42 Timer &Timer::root() {
43   static Timer RootTimer("Total Link Time");
44   return RootTimer;
45 }
46
47 void Timer::print() {
48   double TotalDuration = static_cast<double>(root().millis());
49
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);
54
55   message(std::string(49, '-'));
56
57   root().print(0, root().millis(), false);
58 }
59
60 double Timer::millis() const {
61   return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(
62              Total)
63       .count();
64 }
65
66 void Timer::print(int Depth, double TotalDuration, bool Recurse) const {
67   double P = 100.0 * millis() / TotalDuration;
68
69   SmallString<32> Str;
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);
73
74   message(Str);
75
76   if (Recurse) {
77     for (const auto &Child : Children)
78       Child->print(Depth + 1, TotalDuration);
79   }
80 }