Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Timer.h ----------------------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 <chrono> |
| 16 | #include <map> |
| 17 | #include <memory> |
| 18 | |
| 19 | namespace lld { |
| 20 | |
| 21 | class Timer; |
| 22 | |
| 23 | struct ScopedTimer { |
| 24 | explicit ScopedTimer(Timer &T); |
| 25 | |
| 26 | ~ScopedTimer(); |
| 27 | |
| 28 | void stop(); |
| 29 | |
| 30 | Timer *T = nullptr; |
| 31 | }; |
| 32 | |
| 33 | class Timer { |
| 34 | public: |
| 35 | Timer(llvm::StringRef Name, Timer &Parent); |
| 36 | |
| 37 | static Timer &root(); |
| 38 | |
| 39 | void start(); |
| 40 | void stop(); |
| 41 | void print(); |
| 42 | |
| 43 | double millis() const; |
| 44 | |
| 45 | private: |
| 46 | explicit Timer(llvm::StringRef Name); |
| 47 | void print(int Depth, double TotalDuration, bool Recurse = true) const; |
| 48 | |
| 49 | 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; |
| 54 | }; |
| 55 | |
| 56 | } // namespace lld |
| 57 | |
| 58 | #endif |