xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/Support/Timer.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLVM_SUPPORT_TIMER_H
100b57cec5SDimitry Andric #define LLVM_SUPPORT_TIMER_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
130b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
140b57cec5SDimitry Andric #include "llvm/Support/DataTypes.h"
150b57cec5SDimitry Andric #include <cassert>
1604eeddc0SDimitry Andric #include <memory>
170b57cec5SDimitry Andric #include <string>
180b57cec5SDimitry Andric #include <vector>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric class TimerGroup;
230b57cec5SDimitry Andric class raw_ostream;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric class TimeRecord {
26*06c3fb27SDimitry Andric   double WallTime = 0.0;             ///< Wall clock time elapsed in seconds.
27*06c3fb27SDimitry Andric   double UserTime = 0.0;             ///< User time elapsed.
28*06c3fb27SDimitry Andric   double SystemTime = 0.0;           ///< System time elapsed.
29*06c3fb27SDimitry Andric   ssize_t MemUsed = 0;               ///< Memory allocated (in bytes).
30*06c3fb27SDimitry Andric   uint64_t InstructionsExecuted = 0; ///< Number of instructions executed
310b57cec5SDimitry Andric public:
32*06c3fb27SDimitry Andric   TimeRecord() = default;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   /// Get the current time and memory usage.  If Start is true we get the memory
350b57cec5SDimitry Andric   /// usage before the time, otherwise we get time before memory usage.  This
360b57cec5SDimitry Andric   /// matters if the time to get the memory usage is significant and shouldn't
370b57cec5SDimitry Andric   /// be counted as part of a duration.
380b57cec5SDimitry Andric   static TimeRecord getCurrentTime(bool Start = true);
390b57cec5SDimitry Andric 
getProcessTime()400b57cec5SDimitry Andric   double getProcessTime() const { return UserTime + SystemTime; }
getUserTime()410b57cec5SDimitry Andric   double getUserTime() const { return UserTime; }
getSystemTime()420b57cec5SDimitry Andric   double getSystemTime() const { return SystemTime; }
getWallTime()430b57cec5SDimitry Andric   double getWallTime() const { return WallTime; }
getMemUsed()440b57cec5SDimitry Andric   ssize_t getMemUsed() const { return MemUsed; }
getInstructionsExecuted()45fe6060f1SDimitry Andric   uint64_t getInstructionsExecuted() const { return InstructionsExecuted; }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   bool operator<(const TimeRecord &T) const {
480b57cec5SDimitry Andric     // Sort by Wall Time elapsed, as it is the only thing really accurate
490b57cec5SDimitry Andric     return WallTime < T.WallTime;
500b57cec5SDimitry Andric   }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   void operator+=(const TimeRecord &RHS) {
530b57cec5SDimitry Andric     WallTime += RHS.WallTime;
540b57cec5SDimitry Andric     UserTime += RHS.UserTime;
550b57cec5SDimitry Andric     SystemTime += RHS.SystemTime;
560b57cec5SDimitry Andric     MemUsed += RHS.MemUsed;
57fe6060f1SDimitry Andric     InstructionsExecuted += RHS.InstructionsExecuted;
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric   void operator-=(const TimeRecord &RHS) {
600b57cec5SDimitry Andric     WallTime -= RHS.WallTime;
610b57cec5SDimitry Andric     UserTime -= RHS.UserTime;
620b57cec5SDimitry Andric     SystemTime -= RHS.SystemTime;
630b57cec5SDimitry Andric     MemUsed -= RHS.MemUsed;
64fe6060f1SDimitry Andric     InstructionsExecuted -= RHS.InstructionsExecuted;
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   /// Print the current time record to \p OS, with a breakdown showing
680b57cec5SDimitry Andric   /// contributions to the \p Total time record.
690b57cec5SDimitry Andric   void print(const TimeRecord &Total, raw_ostream &OS) const;
700b57cec5SDimitry Andric };
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric /// This class is used to track the amount of time spent between invocations of
730b57cec5SDimitry Andric /// its startTimer()/stopTimer() methods.  Given appropriate OS support it can
740b57cec5SDimitry Andric /// also keep track of the RSS of the program at various points.  By default,
750b57cec5SDimitry Andric /// the Timer will print the amount of time it has captured to standard error
760b57cec5SDimitry Andric /// when the last timer is destroyed, otherwise it is printed when its
770b57cec5SDimitry Andric /// TimerGroup is destroyed.  Timers do not print their information if they are
780b57cec5SDimitry Andric /// never started.
790b57cec5SDimitry Andric class Timer {
800b57cec5SDimitry Andric   TimeRecord Time;          ///< The total time captured.
810b57cec5SDimitry Andric   TimeRecord StartTime;     ///< The time startTimer() was last called.
820b57cec5SDimitry Andric   std::string Name;         ///< The name of this time variable.
830b57cec5SDimitry Andric   std::string Description;  ///< Description of this time variable.
84480093f4SDimitry Andric   bool Running = false;     ///< Is the timer currently running?
85480093f4SDimitry Andric   bool Triggered = false;   ///< Has the timer ever been triggered?
860b57cec5SDimitry Andric   TimerGroup *TG = nullptr; ///< The TimerGroup this Timer is in.
870b57cec5SDimitry Andric 
88480093f4SDimitry Andric   Timer **Prev = nullptr;   ///< Pointer to \p Next of previous timer in group.
89480093f4SDimitry Andric   Timer *Next = nullptr;    ///< Next timer in the group.
900b57cec5SDimitry Andric public:
Timer(StringRef TimerName,StringRef TimerDescription)91480093f4SDimitry Andric   explicit Timer(StringRef TimerName, StringRef TimerDescription) {
92480093f4SDimitry Andric     init(TimerName, TimerDescription);
930b57cec5SDimitry Andric   }
Timer(StringRef TimerName,StringRef TimerDescription,TimerGroup & tg)94480093f4SDimitry Andric   Timer(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg) {
95480093f4SDimitry Andric     init(TimerName, TimerDescription, tg);
960b57cec5SDimitry Andric   }
Timer(const Timer & RHS)970b57cec5SDimitry Andric   Timer(const Timer &RHS) {
980b57cec5SDimitry Andric     assert(!RHS.TG && "Can only copy uninitialized timers");
990b57cec5SDimitry Andric   }
1000b57cec5SDimitry Andric   const Timer &operator=(const Timer &T) {
1010b57cec5SDimitry Andric     assert(!TG && !T.TG && "Can only assign uninit timers");
1020b57cec5SDimitry Andric     return *this;
1030b57cec5SDimitry Andric   }
1040b57cec5SDimitry Andric   ~Timer();
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   /// Create an uninitialized timer, client must use 'init'.
1071fd87a68SDimitry Andric   explicit Timer() = default;
108480093f4SDimitry Andric   void init(StringRef TimerName, StringRef TimerDescription);
109480093f4SDimitry Andric   void init(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg);
1100b57cec5SDimitry Andric 
getName()1110b57cec5SDimitry Andric   const std::string &getName() const { return Name; }
getDescription()1120b57cec5SDimitry Andric   const std::string &getDescription() const { return Description; }
isInitialized()1130b57cec5SDimitry Andric   bool isInitialized() const { return TG != nullptr; }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   /// Check if the timer is currently running.
isRunning()1160b57cec5SDimitry Andric   bool isRunning() const { return Running; }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   /// Check if startTimer() has ever been called on this timer.
hasTriggered()1190b57cec5SDimitry Andric   bool hasTriggered() const { return Triggered; }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   /// Start the timer running.  Time between calls to startTimer/stopTimer is
1220b57cec5SDimitry Andric   /// counted by the Timer class.  Note that these calls must be correctly
1230b57cec5SDimitry Andric   /// paired.
1240b57cec5SDimitry Andric   void startTimer();
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   /// Stop the timer.
1270b57cec5SDimitry Andric   void stopTimer();
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   /// Clear the timer state.
1300b57cec5SDimitry Andric   void clear();
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   /// Return the duration for which this timer has been running.
getTotalTime()1330b57cec5SDimitry Andric   TimeRecord getTotalTime() const { return Time; }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric private:
1360b57cec5SDimitry Andric   friend class TimerGroup;
1370b57cec5SDimitry Andric };
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric /// The TimeRegion class is used as a helper class to call the startTimer() and
1400b57cec5SDimitry Andric /// stopTimer() methods of the Timer class.  When the object is constructed, it
1410b57cec5SDimitry Andric /// starts the timer specified as its argument.  When it is destroyed, it stops
1420b57cec5SDimitry Andric /// the relevant timer.  This makes it easy to time a region of code.
1430b57cec5SDimitry Andric class TimeRegion {
1440b57cec5SDimitry Andric   Timer *T;
1450b57cec5SDimitry Andric   TimeRegion(const TimeRegion &) = delete;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric public:
TimeRegion(Timer & t)1480b57cec5SDimitry Andric   explicit TimeRegion(Timer &t) : T(&t) {
1490b57cec5SDimitry Andric     T->startTimer();
1500b57cec5SDimitry Andric   }
TimeRegion(Timer * t)1510b57cec5SDimitry Andric   explicit TimeRegion(Timer *t) : T(t) {
1520b57cec5SDimitry Andric     if (T) T->startTimer();
1530b57cec5SDimitry Andric   }
~TimeRegion()1540b57cec5SDimitry Andric   ~TimeRegion() {
1550b57cec5SDimitry Andric     if (T) T->stopTimer();
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric };
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric /// This class is basically a combination of TimeRegion and Timer.  It allows
1600b57cec5SDimitry Andric /// you to declare a new timer, AND specify the region to time, all in one
1610b57cec5SDimitry Andric /// statement.  All timers with the same name are merged.  This is primarily
1620b57cec5SDimitry Andric /// used for debugging and for hunting performance problems.
1630b57cec5SDimitry Andric struct NamedRegionTimer : public TimeRegion {
1640b57cec5SDimitry Andric   explicit NamedRegionTimer(StringRef Name, StringRef Description,
1650b57cec5SDimitry Andric                             StringRef GroupName,
1660b57cec5SDimitry Andric                             StringRef GroupDescription, bool Enabled = true);
1670b57cec5SDimitry Andric };
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric /// The TimerGroup class is used to group together related timers into a single
1700b57cec5SDimitry Andric /// report that is printed when the TimerGroup is destroyed.  It is illegal to
1710b57cec5SDimitry Andric /// destroy a TimerGroup object before all of the Timers in it are gone.  A
1720b57cec5SDimitry Andric /// TimerGroup can be specified for a newly created timer in its constructor.
1730b57cec5SDimitry Andric class TimerGroup {
1740b57cec5SDimitry Andric   struct PrintRecord {
1750b57cec5SDimitry Andric     TimeRecord Time;
1760b57cec5SDimitry Andric     std::string Name;
1770b57cec5SDimitry Andric     std::string Description;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric     PrintRecord(const PrintRecord &Other) = default;
180480093f4SDimitry Andric     PrintRecord &operator=(const PrintRecord &Other) = default;
PrintRecordPrintRecord1810b57cec5SDimitry Andric     PrintRecord(const TimeRecord &Time, const std::string &Name,
1820b57cec5SDimitry Andric                 const std::string &Description)
1830b57cec5SDimitry Andric       : Time(Time), Name(Name), Description(Description) {}
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric     bool operator <(const PrintRecord &Other) const {
1860b57cec5SDimitry Andric       return Time < Other.Time;
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric   };
1890b57cec5SDimitry Andric   std::string Name;
1900b57cec5SDimitry Andric   std::string Description;
1910b57cec5SDimitry Andric   Timer *FirstTimer = nullptr; ///< First timer in the group.
1920b57cec5SDimitry Andric   std::vector<PrintRecord> TimersToPrint;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   TimerGroup **Prev; ///< Pointer to Next field of previous timergroup in list.
1950b57cec5SDimitry Andric   TimerGroup *Next;  ///< Pointer to next timergroup in list.
1960b57cec5SDimitry Andric   TimerGroup(const TimerGroup &TG) = delete;
1970b57cec5SDimitry Andric   void operator=(const TimerGroup &TG) = delete;
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric public:
2000b57cec5SDimitry Andric   explicit TimerGroup(StringRef Name, StringRef Description);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   explicit TimerGroup(StringRef Name, StringRef Description,
2030b57cec5SDimitry Andric                       const StringMap<TimeRecord> &Records);
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   ~TimerGroup();
2060b57cec5SDimitry Andric 
setName(StringRef NewName,StringRef NewDescription)2070b57cec5SDimitry Andric   void setName(StringRef NewName, StringRef NewDescription) {
2080b57cec5SDimitry Andric     Name.assign(NewName.begin(), NewName.end());
2090b57cec5SDimitry Andric     Description.assign(NewDescription.begin(), NewDescription.end());
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   /// Print any started timers in this group, optionally resetting timers after
2130b57cec5SDimitry Andric   /// printing them.
2140b57cec5SDimitry Andric   void print(raw_ostream &OS, bool ResetAfterPrint = false);
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   /// Clear all timers in this group.
2170b57cec5SDimitry Andric   void clear();
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   /// This static method prints all timers.
2200b57cec5SDimitry Andric   static void printAll(raw_ostream &OS);
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   /// Clear out all timers. This is mostly used to disable automatic
2230b57cec5SDimitry Andric   /// printing on shutdown, when timers have already been printed explicitly
2240b57cec5SDimitry Andric   /// using \c printAll or \c printJSONValues.
2250b57cec5SDimitry Andric   static void clearAll();
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   const char *printJSONValues(raw_ostream &OS, const char *delim);
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   /// Prints all timers as JSON key/value pairs.
2300b57cec5SDimitry Andric   static const char *printAllJSONValues(raw_ostream &OS, const char *delim);
2310b57cec5SDimitry Andric 
232bdd1243dSDimitry Andric   /// Ensure global objects required for statistics printing are initialized.
233bdd1243dSDimitry Andric   /// This function is used by the Statistic code to ensure correct order of
234bdd1243dSDimitry Andric   /// global constructors and destructors.
235bdd1243dSDimitry Andric   static void constructForStatistics();
236cd675bb6SDimitry Andric 
237cd675bb6SDimitry Andric   /// This makes the default group unmanaged, and lets the user manage the
238cd675bb6SDimitry Andric   /// group's lifetime.
239cd675bb6SDimitry Andric   static std::unique_ptr<TimerGroup> aquireDefaultGroup();
240cd675bb6SDimitry Andric 
2410b57cec5SDimitry Andric private:
2420b57cec5SDimitry Andric   friend class Timer;
2430b57cec5SDimitry Andric   friend void PrintStatisticsJSON(raw_ostream &OS);
2440b57cec5SDimitry Andric   void addTimer(Timer &T);
2450b57cec5SDimitry Andric   void removeTimer(Timer &T);
2460b57cec5SDimitry Andric   void prepareToPrintList(bool reset_time = false);
2470b57cec5SDimitry Andric   void PrintQueuedTimers(raw_ostream &OS);
2480b57cec5SDimitry Andric   void printJSONValue(raw_ostream &OS, const PrintRecord &R,
2490b57cec5SDimitry Andric                       const char *suffix, double Value);
2500b57cec5SDimitry Andric };
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric } // end namespace llvm
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric #endif
255