Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:58

0001 //===-- Timer.h -------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_TIMER_H
0010 #define LLDB_UTILITY_TIMER_H
0011 
0012 #include "lldb/lldb-defines.h"
0013 #include "llvm/Support/Chrono.h"
0014 #include <atomic>
0015 #include <cstdint>
0016 
0017 namespace lldb_private {
0018 class Stream;
0019 
0020 /// \class Timer Timer.h "lldb/Utility/Timer.h"
0021 /// A timer class that simplifies common timing metrics.
0022 
0023 class Timer {
0024 public:
0025   class Category {
0026   public:
0027     explicit Category(const char *category_name);
0028     llvm::StringRef GetName() { return m_name; }
0029 
0030   private:
0031     friend class Timer;
0032     const char *m_name;
0033     std::atomic<uint64_t> m_nanos;
0034     std::atomic<uint64_t> m_nanos_total;
0035     std::atomic<uint64_t> m_count;
0036     std::atomic<Category *> m_next;
0037 
0038     Category(const Category &) = delete;
0039     const Category &operator=(const Category &) = delete;
0040   };
0041 
0042   /// Default constructor.
0043   Timer(Category &category, const char *format, ...)
0044 #if !defined(_MSC_VER)
0045   // MSVC appears to have trouble recognizing the this argument in the constructor.
0046       __attribute__((format(printf, 3, 4)))
0047 #endif
0048     ;
0049 
0050   /// Destructor
0051   ~Timer();
0052 
0053   void Dump();
0054 
0055   static void SetDisplayDepth(uint32_t depth);
0056 
0057   static void SetQuiet(bool value);
0058 
0059   static void DumpCategoryTimes(Stream &s);
0060 
0061   static void ResetCategoryTimes();
0062 
0063 protected:
0064   using TimePoint = std::chrono::steady_clock::time_point;
0065   void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
0066 
0067   Category &m_category;
0068   TimePoint m_total_start;
0069   TimePoint::duration m_child_duration{0};
0070 
0071   static std::atomic<bool> g_quiet;
0072   static std::atomic<unsigned> g_display_depth;
0073 
0074 private:
0075   Timer(const Timer &) = delete;
0076   const Timer &operator=(const Timer &) = delete;
0077 };
0078 
0079 } // namespace lldb_private
0080 
0081 // Use a format string because LLVM_PRETTY_FUNCTION might not be a string
0082 // literal.
0083 #define LLDB_SCOPED_TIMER()                                                    \
0084   static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
0085   ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
0086 #define LLDB_SCOPED_TIMERF(...)                                                \
0087   static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
0088   ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
0089 
0090 #endif // LLDB_UTILITY_TIMER_H