blob: 95e811a2f9f03c00df22bd702a48f675b8d3ed5f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Timer.h ----------------------------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_COMMON_TIMER_H
10#define LLD_COMMON_TIMER_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/StringRef.h"
14#include <assert.h>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020015#include <atomic>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016#include <chrono>
17#include <map>
18#include <memory>
19
20namespace lld {
21
22class Timer;
23
24struct ScopedTimer {
Andrew Walbran3d2c1972020-04-07 12:24:26 +010025 explicit ScopedTimer(Timer &t);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026
27 ~ScopedTimer();
28
29 void stop();
30
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031 std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
32
Andrew Walbran3d2c1972020-04-07 12:24:26 +010033 Timer *t = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034};
35
36class Timer {
37public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010038 Timer(llvm::StringRef name, Timer &parent);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010039
40 static Timer &root();
41
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020042 void addToTotal(std::chrono::nanoseconds time) { total += time.count(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043 void print();
44
45 double millis() const;
46
47private:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048 explicit Timer(llvm::StringRef name);
49 void print(int depth, double totalDuration, bool recurse = true) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020051 std::atomic<std::chrono::nanoseconds::rep> total;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010052 std::vector<Timer *> children;
53 std::string name;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054};
55
56} // namespace lld
57
58#endif