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