1 //===- Timer.cpp ----------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lld/Common/Timer.h"
10 #include "lld/Common/ErrorHandler.h"
11 #include "llvm/Support/Format.h"
17 ScopedTimer::ScopedTimer(Timer
&t
) : t(&t
) {
18 startTime
= std::chrono::high_resolution_clock::now();
21 void ScopedTimer::stop() {
24 t
->addToTotal(std::chrono::high_resolution_clock::now() - startTime
);
28 ScopedTimer::~ScopedTimer() { stop(); }
30 Timer::Timer(llvm::StringRef name
) : total(0), name(std::string(name
)) {}
31 Timer::Timer(llvm::StringRef name
, Timer
&parent
)
32 : total(0), name(std::string(name
)) {
33 parent
.children
.push_back(this);
37 double totalDuration
= static_cast<double>(millis());
39 // We want to print the grand total under all the intermediate phases, so we
40 // print all children first, then print the total under that.
41 for (const auto &child
: children
)
43 child
->print(1, totalDuration
);
45 message(std::string(50, '-'));
47 print(0, millis(), false);
50 double Timer::millis() const {
51 return std::chrono::duration_cast
<std::chrono::duration
<double, std::milli
>>(
52 std::chrono::nanoseconds(total
))
56 void Timer::print(int depth
, double totalDuration
, bool recurse
) const {
57 double p
= 100.0 * millis() / totalDuration
;
60 llvm::raw_svector_ostream
stream(str
);
61 std::string s
= std::string(depth
* 2, ' ') + name
+ std::string(":");
62 stream
<< format("%-30s%7d ms (%5.1f%%)", s
.c_str(), (int)millis(), p
);
67 for (const auto &child
: children
)
69 child
->print(depth
+ 1, totalDuration
);