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 LLDB_UTILITY_TIMER_H 10 #define LLDB_UTILITY_TIMER_H 11 12 #include "lldb/lldb-defines.h" 13 #include "llvm/Support/Chrono.h" 14 #include <atomic> 15 #include <stdint.h> 16 17 namespace lldb_private { 18 class Stream; 19 20 /// \class Timer Timer.h "lldb/Utility/Timer.h" 21 /// A timer class that simplifies common timing metrics. 22 23 class Timer { 24 public: 25 class Category { 26 public: 27 explicit Category(const char *category_name); 28 llvm::StringRef GetName() { return m_name; } 29 30 private: 31 friend class Timer; 32 const char *m_name; 33 std::atomic<uint64_t> m_nanos; 34 std::atomic<uint64_t> m_nanos_total; 35 std::atomic<uint64_t> m_count; 36 std::atomic<Category *> m_next; 37 38 Category(const Category &) = delete; 39 const Category &operator=(const Category &) = delete; 40 }; 41 42 /// Default constructor. 43 Timer(Category &category, const char *format, ...) 44 __attribute__((format(printf, 3, 4))); 45 46 /// Destructor 47 ~Timer(); 48 49 void Dump(); 50 51 static void SetDisplayDepth(uint32_t depth); 52 53 static void SetQuiet(bool value); 54 55 static void DumpCategoryTimes(Stream *s); 56 57 static void ResetCategoryTimes(); 58 59 protected: 60 using TimePoint = std::chrono::steady_clock::time_point; 61 void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; } 62 63 Category &m_category; 64 TimePoint m_total_start; 65 TimePoint::duration m_child_duration{0}; 66 67 static std::atomic<bool> g_quiet; 68 static std::atomic<unsigned> g_display_depth; 69 70 private: 71 Timer(const Timer &) = delete; 72 const Timer &operator=(const Timer &) = delete; 73 }; 74 75 } // namespace lldb_private 76 77 #define LLDB_SCOPED_TIMER() \ 78 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \ 79 ::lldb_private::Timer _scoped_timer(_cat, LLVM_PRETTY_FUNCTION) 80 #define LLDB_SCOPED_TIMERF(...) \ 81 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \ 82 ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__) 83 84 #endif // LLDB_UTILITY_TIMER_H 85