1 //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines three classes: Timer, TimeRegion, and TimerGroup,
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_TIMER_H
16 #define LLVM_SUPPORT_TIMER_H
18 #include "llvm/System/DataTypes.h"
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.
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.
45 explicit Timer(const std::string &N);
46 Timer(const std::string &N, TimerGroup &tg);
47 Timer(const Timer &T);
51 double getProcessTime() const { return UserTime+SystemTime; }
52 double getWallTime() const { return Elapsed; }
53 ssize_t getMemUsed() const { return MemUsed; }
55 std::string getName() const { return Name; }
57 const Timer &operator=(const Timer &T);
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;
64 bool operator>(const Timer &T) const { return T.operator<(*this); }
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.
72 /// stopTimer - Stop the timer.
76 /// print - Print the current timer to standard error, and reset the "Started"
78 void print(const Timer &Total, raw_ostream &OS);
81 friend class TimerGroup;
83 // Copy ctor, initialize with no TG member.
84 Timer(bool, const Timer &T);
86 /// sum - Add the time accumulated in the specified timer into this timer.
88 void sum(const Timer &T);
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.
99 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
101 explicit TimeRegion(Timer &t) : T(&t) {
104 explicit TimeRegion(Timer *t) : T(t) {
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.
120 struct NamedRegionTimer : public TimeRegion {
121 explicit NamedRegionTimer(const std::string &Name);
122 explicit NamedRegionTimer(const std::string &Name,
123 const std::string &GroupName);
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.
135 std::vector<Timer> TimersToPrint;
137 explicit TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
139 assert(NumTimers == 0 &&
140 "TimerGroup destroyed before all contained timers!");
147 void addTimerToPrint(const Timer &T);
150 } // End llvm namespace