1 //===- Timer.h ----------------------------------------------*- C++ -*-===//
2 //
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
6 //
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 <atomic>
16 #include <chrono>
17 #include <map>
18 #include <memory>
19 #include <vector>
20 
21 namespace lld {
22 
23 class Timer;
24 
25 struct ScopedTimer {
26   explicit ScopedTimer(Timer &t);
27 
28   ~ScopedTimer();
29 
30   void stop();
31 
32   std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
33 
34   Timer *t = nullptr;
35 };
36 
37 class Timer {
38 public:
39   Timer(llvm::StringRef name, Timer &parent);
40 
41   static Timer &root();
42 
addToTotal(std::chrono::nanoseconds time)43   void addToTotal(std::chrono::nanoseconds time) { total += time.count(); }
44   void print();
45 
46   double millis() const;
47 
48 private:
49   explicit Timer(llvm::StringRef name);
50   void print(int depth, double totalDuration, bool recurse = true) const;
51 
52   std::atomic<std::chrono::nanoseconds::rep> total;
53   std::vector<Timer *> children;
54   std::string name;
55 };
56 
57 } // namespace lld
58 
59 #endif
60