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 "llvm/ADT/StringRef.h" 20#include <cassert> 21#include <string> 22#include <vector> 23#include <utility> 24 25namespace llvm { 26 27class Timer; 28class TimerGroup; 29class raw_ostream; 30 31class TimeRecord { 32 double WallTime; // Wall clock time elapsed in seconds 33 double UserTime; // User time elapsed 34 double SystemTime; // System time elapsed 35 ssize_t MemUsed; // Memory allocated (in bytes) 36public: 37 TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {} 38 39 /// getCurrentTime - Get the current time and memory usage. If Start is true 40 /// we get the memory usage before the time, otherwise we get time before 41 /// memory usage. This matters if the time to get the memory usage is 42 /// significant and shouldn't be counted as part of a duration. 43 static TimeRecord getCurrentTime(bool Start = true); 44 45 double getProcessTime() const { return UserTime+SystemTime; } 46 double getUserTime() const { return UserTime; } 47 double getSystemTime() const { return SystemTime; } 48 double getWallTime() const { return WallTime; } 49 ssize_t getMemUsed() const { return MemUsed; } 50 51 52 // operator< - Allow sorting. 53 bool operator<(const TimeRecord &T) const { 54 // Sort by Wall Time elapsed, as it is the only thing really accurate 55 return WallTime < T.WallTime; 56 } 57 58 void operator+=(const TimeRecord &RHS) { 59 WallTime += RHS.WallTime; 60 UserTime += RHS.UserTime; 61 SystemTime += RHS.SystemTime; 62 MemUsed += RHS.MemUsed; 63 } 64 void operator-=(const TimeRecord &RHS) { 65 WallTime -= RHS.WallTime; 66 UserTime -= RHS.UserTime; 67 SystemTime -= RHS.SystemTime; 68 MemUsed -= RHS.MemUsed; 69 } 70 71 /// print - Print the current timer to standard error, and reset the "Started" 72 /// flag. 73 void print(const TimeRecord &Total, raw_ostream &OS) const; 74}; 75 76/// Timer - This class is used to track the amount of time spent between 77/// invocations of its startTimer()/stopTimer() methods. Given appropriate OS 78/// support it can also keep track of the RSS of the program at various points. 79/// By default, the Timer will print the amount of time it has captured to 80/// standard error when the laster timer is destroyed, otherwise it is printed 81/// when its TimerGroup is destroyed. Timers do not print their information 82/// if they are never started. 83/// 84class Timer { 85 TimeRecord Time; 86 std::string Name; // The name of this time variable. 87 bool Started; // Has this time variable ever been started? 88 TimerGroup *TG; // The TimerGroup this Timer is in. 89 90 Timer **Prev, *Next; // Doubly linked list of timers in the group. 91public: 92 explicit Timer(StringRef N) : TG(0) { init(N); } 93 Timer(StringRef N, TimerGroup &tg) : TG(0) { init(N, tg); } 94 Timer(const Timer &RHS) : TG(0) { 95 assert(RHS.TG == 0 && "Can only copy uninitialized timers"); 96 } 97 const Timer &operator=(const Timer &T) { 98 assert(TG == 0 && T.TG == 0 && "Can only assign uninit timers"); 99 return *this; 100 } 101 ~Timer(); 102 103 // Create an uninitialized timer, client must use 'init'. 104 explicit Timer() : TG(0) {} 105 void init(StringRef N); 106 void init(StringRef N, TimerGroup &tg); 107 108 const std::string &getName() const { return Name; } 109 bool isInitialized() const { return TG != 0; } 110 111 /// startTimer - Start the timer running. Time between calls to 112 /// startTimer/stopTimer is counted by the Timer class. Note that these calls 113 /// must be correctly paired. 114 /// 115 void startTimer(); 116 117 /// stopTimer - Stop the timer. 118 /// 119 void stopTimer(); 120 121private: 122 friend class TimerGroup; 123}; 124 125 126/// The TimeRegion class is used as a helper class to call the startTimer() and 127/// stopTimer() methods of the Timer class. When the object is constructed, it 128/// starts the timer specified as it's argument. When it is destroyed, it stops 129/// the relevant timer. This makes it easy to time a region of code. 130/// 131class TimeRegion { 132 Timer *T; 133 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT 134public: 135 explicit TimeRegion(Timer &t) : T(&t) { 136 T->startTimer(); 137 } 138 explicit TimeRegion(Timer *t) : T(t) { 139 if (T) T->startTimer(); 140 } 141 ~TimeRegion() { 142 if (T) T->stopTimer(); 143 } 144}; 145 146 147/// NamedRegionTimer - This class is basically a combination of TimeRegion and 148/// Timer. It allows you to declare a new timer, AND specify the region to 149/// time, all in one statement. All timers with the same name are merged. This 150/// is primarily used for debugging and for hunting performance problems. 151/// 152struct NamedRegionTimer : public TimeRegion { 153 explicit NamedRegionTimer(StringRef Name, 154 bool Enabled = true); 155 explicit NamedRegionTimer(StringRef Name, StringRef GroupName, 156 bool Enabled = true); 157}; 158 159 160/// The TimerGroup class is used to group together related timers into a single 161/// report that is printed when the TimerGroup is destroyed. It is illegal to 162/// destroy a TimerGroup object before all of the Timers in it are gone. A 163/// TimerGroup can be specified for a newly created timer in its constructor. 164/// 165class TimerGroup { 166 std::string Name; 167 Timer *FirstTimer; // First timer in the group. 168 std::vector<std::pair<TimeRecord, std::string> > TimersToPrint; 169 170 TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's. 171 TimerGroup(const TimerGroup &TG); // DO NOT IMPLEMENT 172 void operator=(const TimerGroup &TG); // DO NOT IMPLEMENT 173public: 174 explicit TimerGroup(StringRef name); 175 ~TimerGroup(); 176 177 void setName(StringRef name) { Name.assign(name.begin(), name.end()); } 178 179 /// print - Print any started timers in this group and zero them. 180 void print(raw_ostream &OS); 181 182 /// printAll - This static method prints all timers and clears them all out. 183 static void printAll(raw_ostream &OS); 184 185private: 186 friend class Timer; 187 void addTimer(Timer &T); 188 void removeTimer(Timer &T); 189 void PrintQueuedTimers(raw_ostream &OS); 190}; 191 192} // End llvm namespace 193 194#endif 195