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"
19 #include "llvm/System/Mutex.h"
29 /// Timer - This class is used to track the amount of time spent between
30 /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
31 /// support it can also keep track of the RSS of the program at various points.
32 /// By default, the Timer will print the amount of time it has captured to
33 /// standard error when the laster timer is destroyed, otherwise it is printed
34 /// when its TimerGroup is destroyed. Timers do not print their information
35 /// if they are never started.
38 double Elapsed; // Wall clock time elapsed in seconds
39 double UserTime; // User time elapsed
40 double SystemTime; // System time elapsed
41 ssize_t MemUsed; // Memory allocated (in bytes)
42 size_t PeakMem; // Peak memory used
43 size_t PeakMemBase; // Temporary for peak calculation...
44 std::string Name; // The name of this time variable
45 bool Started; // Has this time variable ever been started?
46 TimerGroup *TG; // The TimerGroup this Timer is in.
47 mutable sys::SmartMutex<true> Lock; // Mutex for the contents of this Timer.
49 explicit Timer(const std::string &N);
50 Timer(const std::string &N, TimerGroup &tg);
51 Timer(const Timer &T);
54 double getProcessTime() const { return UserTime+SystemTime; }
55 double getWallTime() const { return Elapsed; }
56 ssize_t getMemUsed() const { return MemUsed; }
57 size_t getPeakMem() const { return PeakMem; }
58 std::string getName() const { return Name; }
60 const Timer &operator=(const Timer &T) {
70 UserTime = T.UserTime;
71 SystemTime = T.SystemTime;
74 PeakMemBase = T.PeakMemBase;
77 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
90 // operator< - Allow sorting...
91 bool operator<(const Timer &T) const {
92 // Sort by Wall Time elapsed, as it is the only thing really accurate
93 return Elapsed < T.Elapsed;
95 bool operator>(const Timer &T) const { return T.operator<(*this); }
97 /// startTimer - Start the timer running. Time between calls to
98 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
99 /// must be correctly paired.
103 /// stopTimer - Stop the timer.
107 /// addPeakMemoryMeasurement - This method should be called whenever memory
108 /// usage needs to be checked. It adds a peak memory measurement to the
109 /// currently active timers, which will be printed when the timer group prints
111 static void addPeakMemoryMeasurement();
113 /// print - Print the current timer to standard error, and reset the "Started"
115 void print(const Timer &Total, raw_ostream &OS);
118 friend class TimerGroup;
120 // Copy ctor, initialize with no TG member.
121 Timer(bool, const Timer &T);
123 /// sum - Add the time accumulated in the specified timer into this timer.
125 void sum(const Timer &T);
129 /// The TimeRegion class is used as a helper class to call the startTimer() and
130 /// stopTimer() methods of the Timer class. When the object is constructed, it
131 /// starts the timer specified as it's argument. When it is destroyed, it stops
132 /// the relevant timer. This makes it easy to time a region of code.
136 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
138 explicit TimeRegion(Timer &t) : T(&t) {
141 explicit TimeRegion(Timer *t) : T(t) {
152 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
153 /// Timer. It allows you to declare a new timer, AND specify the region to
154 /// time, all in one statement. All timers with the same name are merged. This
155 /// is primarily used for debugging and for hunting performance problems.
157 struct NamedRegionTimer : public TimeRegion {
158 explicit NamedRegionTimer(const std::string &Name);
159 explicit NamedRegionTimer(const std::string &Name,
160 const std::string &GroupName);
164 /// The TimerGroup class is used to group together related timers into a single
165 /// report that is printed when the TimerGroup is destroyed. It is illegal to
166 /// destroy a TimerGroup object before all of the Timers in it are gone. A
167 /// TimerGroup can be specified for a newly created timer in its constructor.
172 std::vector<Timer> TimersToPrint;
174 explicit TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
176 assert(NumTimers == 0 &&
177 "TimerGroup destroyed before all contained timers!");
184 void addTimerToPrint(const Timer &T);
187 } // End llvm namespace