Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 14 | pmbaty | 1 | //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- 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 LLVM_SUPPORT_TIMER_H |
||
| 10 | #define LLVM_SUPPORT_TIMER_H |
||
| 11 | |||
| 12 | #include "llvm/ADT/StringMap.h" |
||
| 13 | #include "llvm/ADT/StringRef.h" |
||
| 14 | #include "llvm/Support/DataTypes.h" |
||
| 15 | #include <cassert> |
||
| 16 | #include <memory> |
||
| 17 | #include <string> |
||
| 18 | #include <vector> |
||
| 19 | |||
| 20 | namespace llvm { |
||
| 21 | |||
| 22 | class TimerGroup; |
||
| 23 | class raw_ostream; |
||
| 24 | |||
| 25 | class TimeRecord { |
||
| 26 | double WallTime; ///< Wall clock time elapsed in seconds. |
||
| 27 | double UserTime; ///< User time elapsed. |
||
| 28 | double SystemTime; ///< System time elapsed. |
||
| 29 | ssize_t MemUsed; ///< Memory allocated (in bytes). |
||
| 30 | uint64_t InstructionsExecuted; ///< Number of instructions executed |
||
| 31 | public: |
||
| 32 | TimeRecord() |
||
| 33 | : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0), |
||
| 34 | InstructionsExecuted(0) {} |
||
| 35 | |||
| 36 | /// Get the current time and memory usage. If Start is true we get the memory |
||
| 37 | /// usage before the time, otherwise we get time before memory usage. This |
||
| 38 | /// matters if the time to get the memory usage is significant and shouldn't |
||
| 39 | /// be counted as part of a duration. |
||
| 40 | static TimeRecord getCurrentTime(bool Start = true); |
||
| 41 | |||
| 42 | double getProcessTime() const { return UserTime + SystemTime; } |
||
| 43 | double getUserTime() const { return UserTime; } |
||
| 44 | double getSystemTime() const { return SystemTime; } |
||
| 45 | double getWallTime() const { return WallTime; } |
||
| 46 | ssize_t getMemUsed() const { return MemUsed; } |
||
| 47 | uint64_t getInstructionsExecuted() const { return InstructionsExecuted; } |
||
| 48 | |||
| 49 | bool operator<(const TimeRecord &T) const { |
||
| 50 | // Sort by Wall Time elapsed, as it is the only thing really accurate |
||
| 51 | return WallTime < T.WallTime; |
||
| 52 | } |
||
| 53 | |||
| 54 | void operator+=(const TimeRecord &RHS) { |
||
| 55 | WallTime += RHS.WallTime; |
||
| 56 | UserTime += RHS.UserTime; |
||
| 57 | SystemTime += RHS.SystemTime; |
||
| 58 | MemUsed += RHS.MemUsed; |
||
| 59 | InstructionsExecuted += RHS.InstructionsExecuted; |
||
| 60 | } |
||
| 61 | void operator-=(const TimeRecord &RHS) { |
||
| 62 | WallTime -= RHS.WallTime; |
||
| 63 | UserTime -= RHS.UserTime; |
||
| 64 | SystemTime -= RHS.SystemTime; |
||
| 65 | MemUsed -= RHS.MemUsed; |
||
| 66 | InstructionsExecuted -= RHS.InstructionsExecuted; |
||
| 67 | } |
||
| 68 | |||
| 69 | /// Print the current time record to \p OS, with a breakdown showing |
||
| 70 | /// contributions to the \p Total time record. |
||
| 71 | void print(const TimeRecord &Total, raw_ostream &OS) const; |
||
| 72 | }; |
||
| 73 | |||
| 74 | /// This class is used to track the amount of time spent between invocations of |
||
| 75 | /// its startTimer()/stopTimer() methods. Given appropriate OS support it can |
||
| 76 | /// also keep track of the RSS of the program at various points. By default, |
||
| 77 | /// the Timer will print the amount of time it has captured to standard error |
||
| 78 | /// when the last timer is destroyed, otherwise it is printed when its |
||
| 79 | /// TimerGroup is destroyed. Timers do not print their information if they are |
||
| 80 | /// never started. |
||
| 81 | class Timer { |
||
| 82 | TimeRecord Time; ///< The total time captured. |
||
| 83 | TimeRecord StartTime; ///< The time startTimer() was last called. |
||
| 84 | std::string Name; ///< The name of this time variable. |
||
| 85 | std::string Description; ///< Description of this time variable. |
||
| 86 | bool Running = false; ///< Is the timer currently running? |
||
| 87 | bool Triggered = false; ///< Has the timer ever been triggered? |
||
| 88 | TimerGroup *TG = nullptr; ///< The TimerGroup this Timer is in. |
||
| 89 | |||
| 90 | Timer **Prev = nullptr; ///< Pointer to \p Next of previous timer in group. |
||
| 91 | Timer *Next = nullptr; ///< Next timer in the group. |
||
| 92 | public: |
||
| 93 | explicit Timer(StringRef TimerName, StringRef TimerDescription) { |
||
| 94 | init(TimerName, TimerDescription); |
||
| 95 | } |
||
| 96 | Timer(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg) { |
||
| 97 | init(TimerName, TimerDescription, tg); |
||
| 98 | } |
||
| 99 | Timer(const Timer &RHS) { |
||
| 100 | assert(!RHS.TG && "Can only copy uninitialized timers"); |
||
| 101 | } |
||
| 102 | const Timer &operator=(const Timer &T) { |
||
| 103 | assert(!TG && !T.TG && "Can only assign uninit timers"); |
||
| 104 | return *this; |
||
| 105 | } |
||
| 106 | ~Timer(); |
||
| 107 | |||
| 108 | /// Create an uninitialized timer, client must use 'init'. |
||
| 109 | explicit Timer() = default; |
||
| 110 | void init(StringRef TimerName, StringRef TimerDescription); |
||
| 111 | void init(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg); |
||
| 112 | |||
| 113 | const std::string &getName() const { return Name; } |
||
| 114 | const std::string &getDescription() const { return Description; } |
||
| 115 | bool isInitialized() const { return TG != nullptr; } |
||
| 116 | |||
| 117 | /// Check if the timer is currently running. |
||
| 118 | bool isRunning() const { return Running; } |
||
| 119 | |||
| 120 | /// Check if startTimer() has ever been called on this timer. |
||
| 121 | bool hasTriggered() const { return Triggered; } |
||
| 122 | |||
| 123 | /// Start the timer running. Time between calls to startTimer/stopTimer is |
||
| 124 | /// counted by the Timer class. Note that these calls must be correctly |
||
| 125 | /// paired. |
||
| 126 | void startTimer(); |
||
| 127 | |||
| 128 | /// Stop the timer. |
||
| 129 | void stopTimer(); |
||
| 130 | |||
| 131 | /// Clear the timer state. |
||
| 132 | void clear(); |
||
| 133 | |||
| 134 | /// Return the duration for which this timer has been running. |
||
| 135 | TimeRecord getTotalTime() const { return Time; } |
||
| 136 | |||
| 137 | private: |
||
| 138 | friend class TimerGroup; |
||
| 139 | }; |
||
| 140 | |||
| 141 | /// The TimeRegion class is used as a helper class to call the startTimer() and |
||
| 142 | /// stopTimer() methods of the Timer class. When the object is constructed, it |
||
| 143 | /// starts the timer specified as its argument. When it is destroyed, it stops |
||
| 144 | /// the relevant timer. This makes it easy to time a region of code. |
||
| 145 | class TimeRegion { |
||
| 146 | Timer *T; |
||
| 147 | TimeRegion(const TimeRegion &) = delete; |
||
| 148 | |||
| 149 | public: |
||
| 150 | explicit TimeRegion(Timer &t) : T(&t) { |
||
| 151 | T->startTimer(); |
||
| 152 | } |
||
| 153 | explicit TimeRegion(Timer *t) : T(t) { |
||
| 154 | if (T) T->startTimer(); |
||
| 155 | } |
||
| 156 | ~TimeRegion() { |
||
| 157 | if (T) T->stopTimer(); |
||
| 158 | } |
||
| 159 | }; |
||
| 160 | |||
| 161 | /// This class is basically a combination of TimeRegion and Timer. It allows |
||
| 162 | /// you to declare a new timer, AND specify the region to time, all in one |
||
| 163 | /// statement. All timers with the same name are merged. This is primarily |
||
| 164 | /// used for debugging and for hunting performance problems. |
||
| 165 | struct NamedRegionTimer : public TimeRegion { |
||
| 166 | explicit NamedRegionTimer(StringRef Name, StringRef Description, |
||
| 167 | StringRef GroupName, |
||
| 168 | StringRef GroupDescription, bool Enabled = true); |
||
| 169 | }; |
||
| 170 | |||
| 171 | /// The TimerGroup class is used to group together related timers into a single |
||
| 172 | /// report that is printed when the TimerGroup is destroyed. It is illegal to |
||
| 173 | /// destroy a TimerGroup object before all of the Timers in it are gone. A |
||
| 174 | /// TimerGroup can be specified for a newly created timer in its constructor. |
||
| 175 | class TimerGroup { |
||
| 176 | struct PrintRecord { |
||
| 177 | TimeRecord Time; |
||
| 178 | std::string Name; |
||
| 179 | std::string Description; |
||
| 180 | |||
| 181 | PrintRecord(const PrintRecord &Other) = default; |
||
| 182 | PrintRecord &operator=(const PrintRecord &Other) = default; |
||
| 183 | PrintRecord(const TimeRecord &Time, const std::string &Name, |
||
| 184 | const std::string &Description) |
||
| 185 | : Time(Time), Name(Name), Description(Description) {} |
||
| 186 | |||
| 187 | bool operator <(const PrintRecord &Other) const { |
||
| 188 | return Time < Other.Time; |
||
| 189 | } |
||
| 190 | }; |
||
| 191 | std::string Name; |
||
| 192 | std::string Description; |
||
| 193 | Timer *FirstTimer = nullptr; ///< First timer in the group. |
||
| 194 | std::vector<PrintRecord> TimersToPrint; |
||
| 195 | |||
| 196 | TimerGroup **Prev; ///< Pointer to Next field of previous timergroup in list. |
||
| 197 | TimerGroup *Next; ///< Pointer to next timergroup in list. |
||
| 198 | TimerGroup(const TimerGroup &TG) = delete; |
||
| 199 | void operator=(const TimerGroup &TG) = delete; |
||
| 200 | |||
| 201 | public: |
||
| 202 | explicit TimerGroup(StringRef Name, StringRef Description); |
||
| 203 | |||
| 204 | explicit TimerGroup(StringRef Name, StringRef Description, |
||
| 205 | const StringMap<TimeRecord> &Records); |
||
| 206 | |||
| 207 | ~TimerGroup(); |
||
| 208 | |||
| 209 | void setName(StringRef NewName, StringRef NewDescription) { |
||
| 210 | Name.assign(NewName.begin(), NewName.end()); |
||
| 211 | Description.assign(NewDescription.begin(), NewDescription.end()); |
||
| 212 | } |
||
| 213 | |||
| 214 | /// Print any started timers in this group, optionally resetting timers after |
||
| 215 | /// printing them. |
||
| 216 | void print(raw_ostream &OS, bool ResetAfterPrint = false); |
||
| 217 | |||
| 218 | /// Clear all timers in this group. |
||
| 219 | void clear(); |
||
| 220 | |||
| 221 | /// This static method prints all timers. |
||
| 222 | static void printAll(raw_ostream &OS); |
||
| 223 | |||
| 224 | /// Clear out all timers. This is mostly used to disable automatic |
||
| 225 | /// printing on shutdown, when timers have already been printed explicitly |
||
| 226 | /// using \c printAll or \c printJSONValues. |
||
| 227 | static void clearAll(); |
||
| 228 | |||
| 229 | const char *printJSONValues(raw_ostream &OS, const char *delim); |
||
| 230 | |||
| 231 | /// Prints all timers as JSON key/value pairs. |
||
| 232 | static const char *printAllJSONValues(raw_ostream &OS, const char *delim); |
||
| 233 | |||
| 234 | /// Ensure global objects required for statistics printing are initialized. |
||
| 235 | /// This function is used by the Statistic code to ensure correct order of |
||
| 236 | /// global constructors and destructors. |
||
| 237 | static void constructForStatistics(); |
||
| 238 | |||
| 239 | /// This makes the default group unmanaged, and lets the user manage the |
||
| 240 | /// group's lifetime. |
||
| 241 | static std::unique_ptr<TimerGroup> aquireDefaultGroup(); |
||
| 242 | |||
| 243 | private: |
||
| 244 | friend class Timer; |
||
| 245 | friend void PrintStatisticsJSON(raw_ostream &OS); |
||
| 246 | void addTimer(Timer &T); |
||
| 247 | void removeTimer(Timer &T); |
||
| 248 | void prepareToPrintList(bool reset_time = false); |
||
| 249 | void PrintQueuedTimers(raw_ostream &OS); |
||
| 250 | void printJSONValue(raw_ostream &OS, const PrintRecord &R, |
||
| 251 | const char *suffix, double Value); |
||
| 252 | }; |
||
| 253 | |||
| 254 | } // end namespace llvm |
||
| 255 | |||
| 256 | #endif |