Timer.h revision fc86c3cfd6e81113722f17bebc54bc0b63389b58
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/System/DataTypes.h"
19#include <string>
20#include <vector>
21#include <cassert>
22
23namespace llvm {
24
25class TimerGroup;
26class raw_ostream;
27
28/// Timer - This class is used to track the amount of time spent between
29/// invocations of its 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  double Elapsed;        // Wall clock time elapsed in seconds
38  double UserTime;       // User time elapsed
39  double SystemTime;     // System time elapsed
40  ssize_t MemUsed;       // Memory allocated (in bytes)
41  std::string Name;      // The name of this time variable.
42  bool Started;          // Has this time variable ever been started?
43  TimerGroup *TG;        // The TimerGroup this Timer is in.
44public:
45  explicit Timer(const std::string &N);
46  Timer(const std::string &N, TimerGroup &tg);
47  Timer(const Timer &T);
48  ~Timer();
49
50private:
51  double getProcessTime() const { return UserTime+SystemTime; }
52  double getWallTime() const { return Elapsed; }
53  ssize_t getMemUsed() const { return MemUsed; }
54public:
55  std::string getName() const { return Name; }
56
57  const Timer &operator=(const Timer &T);
58
59  // operator< - Allow sorting.
60  bool operator<(const Timer &T) const {
61    // Sort by Wall Time elapsed, as it is the only thing really accurate
62    return Elapsed < T.Elapsed;
63  }
64  bool operator>(const Timer &T) const { return T.operator<(*this); }
65
66  /// startTimer - Start the timer running.  Time between calls to
67  /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
68  /// must be correctly paired.
69  ///
70  void startTimer();
71
72  /// stopTimer - Stop the timer.
73  ///
74  void stopTimer();
75
76  /// print - Print the current timer to standard error, and reset the "Started"
77  /// flag.
78  void print(const Timer &Total, raw_ostream &OS);
79
80private:
81  friend class TimerGroup;
82
83  // Copy ctor, initialize with no TG member.
84  Timer(bool, const Timer &T);
85
86  /// sum - Add the time accumulated in the specified timer into this timer.
87  ///
88  void sum(const Timer &T);
89};
90
91
92/// The TimeRegion class is used as a helper class to call the startTimer() and
93/// stopTimer() methods of the Timer class.  When the object is constructed, it
94/// starts the timer specified as it's argument.  When it is destroyed, it stops
95/// the relevant timer.  This makes it easy to time a region of code.
96///
97class TimeRegion {
98  Timer *T;
99  TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
100public:
101  explicit TimeRegion(Timer &t) : T(&t) {
102    T->startTimer();
103  }
104  explicit TimeRegion(Timer *t) : T(t) {
105    if (T)
106      T->startTimer();
107  }
108  ~TimeRegion() {
109    if (T)
110      T->stopTimer();
111  }
112};
113
114
115/// NamedRegionTimer - This class is basically a combination of TimeRegion and
116/// Timer.  It allows you to declare a new timer, AND specify the region to
117/// time, all in one statement.  All timers with the same name are merged.  This
118/// is primarily used for debugging and for hunting performance problems.
119///
120struct NamedRegionTimer : public TimeRegion {
121  explicit NamedRegionTimer(const std::string &Name);
122  explicit NamedRegionTimer(const std::string &Name,
123                            const std::string &GroupName);
124};
125
126
127/// The TimerGroup class is used to group together related timers into a single
128/// report that is printed when the TimerGroup is destroyed.  It is illegal to
129/// destroy a TimerGroup object before all of the Timers in it are gone.  A
130/// TimerGroup can be specified for a newly created timer in its constructor.
131///
132class TimerGroup {
133  std::string Name;
134  unsigned NumTimers;
135  std::vector<Timer> TimersToPrint;
136public:
137  explicit TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
138  ~TimerGroup() {
139    assert(NumTimers == 0 &&
140           "TimerGroup destroyed before all contained timers!");
141  }
142
143private:
144  friend class Timer;
145  void addTimer();
146  void removeTimer();
147  void addTimerToPrint(const Timer &T);
148};
149
150} // End llvm namespace
151
152#endif
153