Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.3 KiB

  1. //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_SUPPORT_TIMER_H
  10. #define LLVM_SUPPORT_TIMER_H
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/Compiler.h"
  13. #include "llvm/Support/DataTypes.h"
  14. #include <cassert>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. namespace llvm {
  19. class Timer;
  20. class TimerGroup;
  21. class raw_ostream;
  22. class TimeRecord {
  23. double WallTime; // Wall clock time elapsed in seconds
  24. double UserTime; // User time elapsed
  25. double SystemTime; // System time elapsed
  26. ssize_t MemUsed; // Memory allocated (in bytes)
  27. public:
  28. TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
  29. /// getCurrentTime - Get the current time and memory usage. If Start is true
  30. /// we get the memory usage before the time, otherwise we get time before
  31. /// memory usage. This matters if the time to get the memory usage is
  32. /// significant and shouldn't be counted as part of a duration.
  33. static TimeRecord getCurrentTime(bool Start = true);
  34. double getProcessTime() const { return UserTime+SystemTime; }
  35. double getUserTime() const { return UserTime; }
  36. double getSystemTime() const { return SystemTime; }
  37. double getWallTime() const { return WallTime; }
  38. ssize_t getMemUsed() const { return MemUsed; }
  39. // operator< - Allow sorting.
  40. bool operator<(const TimeRecord &T) const {
  41. // Sort by Wall Time elapsed, as it is the only thing really accurate
  42. return WallTime < T.WallTime;
  43. }
  44. void operator+=(const TimeRecord &RHS) {
  45. WallTime += RHS.WallTime;
  46. UserTime += RHS.UserTime;
  47. SystemTime += RHS.SystemTime;
  48. MemUsed += RHS.MemUsed;
  49. }
  50. void operator-=(const TimeRecord &RHS) {
  51. WallTime -= RHS.WallTime;
  52. UserTime -= RHS.UserTime;
  53. SystemTime -= RHS.SystemTime;
  54. MemUsed -= RHS.MemUsed;
  55. }
  56. /// print - Print the current timer to standard error, and reset the "Started"
  57. /// flag.
  58. void print(const TimeRecord &Total, raw_ostream &OS) const;
  59. };
  60. /// Timer - This class is used to track the amount of time spent between
  61. /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
  62. /// support it can also keep track of the RSS of the program at various points.
  63. /// By default, the Timer will print the amount of time it has captured to
  64. /// standard error when the last timer is destroyed, otherwise it is printed
  65. /// when its TimerGroup is destroyed. Timers do not print their information
  66. /// if they are never started.
  67. ///
  68. class Timer {
  69. TimeRecord Time;
  70. std::string Name; // The name of this time variable.
  71. bool Started; // Has this time variable ever been started?
  72. TimerGroup *TG; // The TimerGroup this Timer is in.
  73. Timer **Prev, *Next; // Doubly linked list of timers in the group.
  74. public:
  75. explicit Timer(StringRef N) : TG(0) { init(N); }
  76. Timer(StringRef N, TimerGroup &tg) : TG(0) { init(N, tg); }
  77. Timer(const Timer &RHS) : TG(0) {
  78. assert(RHS.TG == 0 && "Can only copy uninitialized timers");
  79. }
  80. const Timer &operator=(const Timer &T) {
  81. assert(TG == 0 && T.TG == 0 && "Can only assign uninit timers");
  82. return *this;
  83. }
  84. ~Timer();
  85. // Create an uninitialized timer, client must use 'init'.
  86. explicit Timer() : TG(0) {}
  87. void init(StringRef N);
  88. void init(StringRef N, TimerGroup &tg);
  89. const std::string &getName() const { return Name; }
  90. bool isInitialized() const { return TG != 0; }
  91. /// startTimer - Start the timer running. Time between calls to
  92. /// startTimer/stopTimer is counted by the Timer class. Note that these calls
  93. /// must be correctly paired.
  94. ///
  95. void startTimer();
  96. /// stopTimer - Stop the timer.
  97. ///
  98. void stopTimer();
  99. private:
  100. friend class TimerGroup;
  101. };
  102. /// The TimeRegion class is used as a helper class to call the startTimer() and
  103. /// stopTimer() methods of the Timer class. When the object is constructed, it
  104. /// starts the timer specified as its argument. When it is destroyed, it stops
  105. /// the relevant timer. This makes it easy to time a region of code.
  106. ///
  107. class TimeRegion {
  108. Timer *T;
  109. TimeRegion(const TimeRegion &) LLVM_DELETED_FUNCTION;
  110. public:
  111. explicit TimeRegion(Timer &t) : T(&t) {
  112. T->startTimer();
  113. }
  114. explicit TimeRegion(Timer *t) : T(t) {
  115. if (T) T->startTimer();
  116. }
  117. ~TimeRegion() {
  118. if (T) T->stopTimer();
  119. }
  120. };
  121. /// NamedRegionTimer - This class is basically a combination of TimeRegion and
  122. /// Timer. It allows you to declare a new timer, AND specify the region to
  123. /// time, all in one statement. All timers with the same name are merged. This
  124. /// is primarily used for debugging and for hunting performance problems.
  125. ///
  126. struct NamedRegionTimer : public TimeRegion {
  127. explicit NamedRegionTimer(StringRef Name,
  128. bool Enabled = true);
  129. explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
  130. bool Enabled = true);
  131. };
  132. /// The TimerGroup class is used to group together related timers into a single
  133. /// report that is printed when the TimerGroup is destroyed. It is illegal to
  134. /// destroy a TimerGroup object before all of the Timers in it are gone. A
  135. /// TimerGroup can be specified for a newly created timer in its constructor.
  136. ///
  137. class TimerGroup {
  138. std::string Name;
  139. Timer *FirstTimer; // First timer in the group.
  140. std::vector<std::pair<TimeRecord, std::string> > TimersToPrint;
  141. TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's.
  142. TimerGroup(const TimerGroup &TG) LLVM_DELETED_FUNCTION;
  143. void operator=(const TimerGroup &TG) LLVM_DELETED_FUNCTION;
  144. public:
  145. explicit TimerGroup(StringRef name);
  146. ~TimerGroup();
  147. void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
  148. /// print - Print any started timers in this group and zero them.
  149. void print(raw_ostream &OS);
  150. /// printAll - This static method prints all timers and clears them all out.
  151. static void printAll(raw_ostream &OS);
  152. private:
  153. friend class Timer;
  154. void addTimer(Timer &T);
  155. void removeTimer(Timer &T);
  156. void PrintQueuedTimers(raw_ostream &OS);
  157. };
  158. } // End llvm namespace
  159. #endif