Timer.h revision e269a1ac1cd795135e91e42527a9814f4807c75a
1//===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines three classes: Timer, TimeRegion, and TimerGroup,
11// documented below.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_TIMER_H
16#define LLVM_SUPPORT_TIMER_H
17
18#include <string>
19#include <vector>
20#include <iosfwd>
21#include <cassert>
22
23namespace llvm {
24
25class TimerGroup;
26
27/// Timer - This class is used to track the amount of time spent between
28/// invocations of it's startTimer()/stopTimer() methods.  Given appropriate OS
29/// support it can also keep track of the RSS of the program at various points.
30/// By default, the Timer will print the amount of time it has captured to
31/// standard error when the laster timer is destroyed, otherwise it is printed
32/// when it's TimerGroup is destroyed.  Timer's do not print their information
33/// if they are never started.
34///
35class Timer {
36  double Elapsed;        // Wall clock time elapsed in seconds
37  double UserTime;       // User time elapsed
38  double SystemTime;     // System time elapsed
39  size_t MemUsed;        // Memory allocated (in bytes)
40  size_t PeakMem;        // Peak memory used
41  size_t PeakMemBase;    // Temporary for peak calculation...
42  std::string Name;      // The name of this time variable
43  bool Started;          // Has this time variable ever been started?
44  TimerGroup *TG;        // The TimerGroup this Timer is in.
45public:
46  Timer(const std::string &N);
47  Timer(const std::string &N, TimerGroup &tg);
48  Timer(const Timer &T);
49  ~Timer();
50
51  double getProcessTime() const { return UserTime+SystemTime; }
52  double getWallTime() const { return Elapsed; }
53  size_t getMemUsed() const { return MemUsed; }
54  size_t getPeakMem() const { return PeakMem; }
55  std::string getName() const { return Name; }
56
57  const Timer &operator=(const Timer &T) {
58    Elapsed = T.Elapsed;
59    UserTime = T.UserTime;
60    SystemTime = T.SystemTime;
61    MemUsed = T.MemUsed;
62    PeakMem = T.PeakMem;
63    PeakMemBase = T.PeakMemBase;
64    Name = T.Name;
65    Started = T.Started;
66    assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
67    return *this;
68  }
69
70  // operator< - Allow sorting...
71  bool operator<(const Timer &T) const {
72    // Sort by Wall Time elapsed, as it is the only thing really accurate
73    return Elapsed < T.Elapsed;
74  }
75  bool operator>(const Timer &T) const { return T.operator<(*this); }
76
77  /// startTimer - Start the timer running.  Time between calls to
78  /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
79  /// must be correctly paired.
80  ///
81  void startTimer();
82
83  /// stopTimer - Stop the timer.
84  ///
85  void stopTimer();
86
87  /// addPeakMemoryMeasurement - This method should be called whenever memory
88  /// usage needs to be checked.  It adds a peak memory measurement to the
89  /// currently active timers, which will be printed when the timer group prints
90  ///
91  static void addPeakMemoryMeasurement();
92
93  /// print - Print the current timer to standard error, and reset the "Started"
94  /// flag.
95  void print(const Timer &Total, std::ostream &OS);
96
97private:
98  friend class TimerGroup;
99
100  // Copy ctor, initialize with no TG member.
101  Timer(bool, const Timer &T);
102
103  /// sum - Add the time accumulated in the specified timer into this timer.
104  ///
105  void sum(const Timer &T);
106};
107
108
109/// The TimeRegion class is used as a helper class to call the startTimer() and
110/// stopTimer() methods of the Timer class.  When the object is constructed, it
111/// starts the timer specified as it's argument.  When it is destroyed, it stops
112/// the relevant timer.  This makes it easy to time a region of code.
113///
114class TimeRegion {
115  Timer &T;
116  TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
117public:
118  TimeRegion(Timer &t) : T(t) {
119    T.startTimer();
120  }
121  ~TimeRegion() {
122    T.stopTimer();
123  }
124};
125
126
127/// NamedRegionTimer - This class is basically a combination of TimeRegion and
128/// Timer.  It allows you to declare a new timer, AND specify the region to
129/// time, all in one statement.  All timers with the same name are merged.  This
130/// is primarily used for debugging and for hunting performance problems.
131///
132struct NamedRegionTimer : public TimeRegion {
133  NamedRegionTimer(const std::string &Name);
134};
135
136
137/// The TimerGroup class is used to group together related timers into a single
138/// report that is printed when the TimerGroup is destroyed.  It is illegal to
139/// destroy a TimerGroup object before all of the Timers in it are gone.  A
140/// TimerGroup can be specified for a newly created timer in its constructor.
141///
142class TimerGroup {
143  std::string Name;
144  unsigned NumTimers;
145  std::vector<Timer> TimersToPrint;
146public:
147  TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
148  ~TimerGroup() {
149    assert(NumTimers == 0 &&
150           "TimerGroup destroyed before all contained timers!");
151  }
152
153private:
154  friend class Timer;
155  void addTimer() { ++NumTimers; }
156  void removeTimer();
157  void addTimerToPrint(const Timer &T) {
158    TimersToPrint.push_back(Timer(true, T));
159  }
160};
161
162} // End llvm namespace
163
164#endif
165