Path: blob/main/contrib/llvm-project/lld/Common/Timer.cpp
34879 views
//===- Timer.cpp ----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lld/Common/Timer.h"9#include "lld/Common/ErrorHandler.h"10#include "llvm/ADT/SmallString.h"11#include "llvm/Support/Format.h"12#include <ratio>1314using namespace lld;15using namespace llvm;1617ScopedTimer::ScopedTimer(Timer &t) : t(&t) {18startTime = std::chrono::high_resolution_clock::now();19}2021void ScopedTimer::stop() {22if (!t)23return;24t->addToTotal(std::chrono::high_resolution_clock::now() - startTime);25t = nullptr;26}2728ScopedTimer::~ScopedTimer() { stop(); }2930Timer::Timer(llvm::StringRef name) : total(0), name(std::string(name)) {}31Timer::Timer(llvm::StringRef name, Timer &parent)32: total(0), name(std::string(name)) {33parent.children.push_back(this);34}3536void Timer::print() {37double totalDuration = static_cast<double>(millis());3839// We want to print the grand total under all the intermediate phases, so we40// print all children first, then print the total under that.41for (const auto &child : children)42if (child->total > 0)43child->print(1, totalDuration);4445message(std::string(50, '-'));4647print(0, millis(), false);48}4950double Timer::millis() const {51return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(52std::chrono::nanoseconds(total))53.count();54}5556void Timer::print(int depth, double totalDuration, bool recurse) const {57double p = 100.0 * millis() / totalDuration;5859SmallString<32> str;60llvm::raw_svector_ostream stream(str);61std::string s = std::string(depth * 2, ' ') + name + std::string(":");62stream << format("%-30s%7d ms (%5.1f%%)", s.c_str(), (int)millis(), p);6364message(str);6566if (recurse) {67for (const auto &child : children)68if (child->total > 0)69child->print(depth + 1, totalDuration);70}71}727374