1 //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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/Support/DataTypes.h"
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 it's TimerGroup is destroyed. Timer's 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 size_t PeakMem; // Peak memory used
42 size_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.
47 Timer(const std::string &N);
48 Timer(const std::string &N, TimerGroup &tg);
49 Timer(const Timer &T);
52 double getProcessTime() const { return UserTime+SystemTime; }
53 double getWallTime() const { return Elapsed; }
54 ssize_t getMemUsed() const { return MemUsed; }
55 size_t getPeakMem() const { return PeakMem; }
56 std::string getName() const { return Name; }
58 const Timer &operator=(const Timer &T) {
60 UserTime = T.UserTime;
61 SystemTime = T.SystemTime;
64 PeakMemBase = T.PeakMemBase;
67 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
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;
76 bool operator>(const Timer &T) const { return T.operator<(*this); }
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.
84 /// stopTimer - Stop the timer.
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
92 static void addPeakMemoryMeasurement();
94 /// print - Print the current timer to standard error, and reset the "Started"
96 void print(const Timer &Total, std::ostream &OS);
99 friend class TimerGroup;
101 // Copy ctor, initialize with no TG member.
102 Timer(bool, const Timer &T);
104 /// sum - Add the time accumulated in the specified timer into this timer.
106 void sum(const Timer &T);
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.
117 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
119 TimeRegion(Timer &t) : T(t) {
128 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
129 /// Timer. It allows you to declare a new timer, AND specify the region to
130 /// time, all in one statement. All timers with the same name are merged. This
131 /// is primarily used for debugging and for hunting performance problems.
133 struct NamedRegionTimer : public TimeRegion {
134 NamedRegionTimer(const std::string &Name);
138 /// The TimerGroup class is used to group together related timers into a single
139 /// report that is printed when the TimerGroup is destroyed. It is illegal to
140 /// destroy a TimerGroup object before all of the Timers in it are gone. A
141 /// TimerGroup can be specified for a newly created timer in its constructor.
146 std::vector<Timer> TimersToPrint;
148 TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
150 assert(NumTimers == 0 &&
151 "TimerGroup destroyed before all contained timers!");
156 void addTimer() { ++NumTimers; }
158 void addTimerToPrint(const Timer &T) {
159 TimersToPrint.push_back(Timer(true, T));
163 } // End llvm namespace