blob: 4a298b04a30b55540fafb21e70ce9647334c1793 [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>
15#include <chrono>
16#include <map>
17#include <memory>
18
19namespace lld {
20
21class Timer;
22
23struct ScopedTimer {
Andrew Walbran3d2c1972020-04-07 12:24:26 +010024 explicit ScopedTimer(Timer &t);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025
26 ~ScopedTimer();
27
28 void stop();
29
Andrew Walbran3d2c1972020-04-07 12:24:26 +010030 Timer *t = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031};
32
33class Timer {
34public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010035 Timer(llvm::StringRef name, Timer &parent);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036
37 static Timer &root();
38
39 void start();
40 void stop();
41 void print();
42
43 double millis() const;
44
45private:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010046 explicit Timer(llvm::StringRef name);
47 void print(int depth, double totalDuration, bool recurse = true) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048
Andrew Walbran3d2c1972020-04-07 12:24:26 +010049 std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
50 std::chrono::nanoseconds total;
51 std::vector<Timer *> children;
52 std::string name;
53 Timer *parent;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054};
55
56} // namespace lld
57
58#endif