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/ADT/SmallString.h"
12 #include "llvm/Support/Format.h"
18 ScopedTimer::ScopedTimer(Timer
&t
) : t(&t
) {
19 startTime
= std::chrono::high_resolution_clock::now();
22 void ScopedTimer::stop() {
25 t
->addToTotal(std::chrono::high_resolution_clock::now() - startTime
);
29 ScopedTimer::~ScopedTimer() { stop(); }
31 Timer::Timer(llvm::StringRef name
) : total(0), name(std::string(name
)) {}
32 Timer::Timer(llvm::StringRef name
, Timer
&parent
)
33 : total(0), name(std::string(name
)) {
34 parent
.children
.push_back(this);
38 double totalDuration
= static_cast<double>(millis());
40 // We want to print the grand total under all the intermediate phases, so we
41 // print all children first, then print the total under that.
42 for (const auto &child
: children
)
44 child
->print(1, totalDuration
);
46 message(std::string(50, '-'));
48 print(0, millis(), false);
51 double Timer::millis() const {
52 return std::chrono::duration_cast
<std::chrono::duration
<double, std::milli
>>(
53 std::chrono::nanoseconds(total
))
57 void Timer::print(int depth
, double totalDuration
, bool recurse
) const {
58 double p
= 100.0 * millis() / totalDuration
;
61 llvm::raw_svector_ostream
stream(str
);
62 std::string s
= std::string(depth
* 2, ' ') + name
+ std::string(":");
63 stream
<< format("%-30s%7d ms (%5.1f%%)", s
.c_str(), (int)millis(), p
);
68 for (const auto &child
: children
)
70 child
->print(depth
+ 1, totalDuration
);