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