#define LLVM_SUPPORT_TIMER_H
#include "llvm/Support/DataTypes.h"
+#include "llvm/System/Mutex.h"
#include <string>
#include <vector>
-#include <iosfwd>
#include <cassert>
namespace llvm {
class TimerGroup;
+class raw_ostream;
/// Timer - This class is used to track the amount of time spent between
-/// invocations of it's startTimer()/stopTimer() methods. Given appropriate OS
+/// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
/// support it can also keep track of the RSS of the program at various points.
/// By default, the Timer will print the amount of time it has captured to
/// standard error when the laster timer is destroyed, otherwise it is printed
std::string Name; // The name of this time variable
bool Started; // Has this time variable ever been started?
TimerGroup *TG; // The TimerGroup this Timer is in.
+ mutable sys::SmartMutex<true> Lock; // Mutex for the contents of this Timer.
public:
explicit Timer(const std::string &N);
Timer(const std::string &N, TimerGroup &tg);
std::string getName() const { return Name; }
const Timer &operator=(const Timer &T) {
+ if (&T < this) {
+ T.Lock.acquire();
+ Lock.acquire();
+ } else {
+ Lock.acquire();
+ T.Lock.acquire();
+ }
+
Elapsed = T.Elapsed;
UserTime = T.UserTime;
SystemTime = T.SystemTime;
Name = T.Name;
Started = T.Started;
assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
+
+ if (&T < this) {
+ T.Lock.release();
+ Lock.release();
+ } else {
+ Lock.release();
+ T.Lock.release();
+ }
+
return *this;
}
/// print - Print the current timer to standard error, and reset the "Started"
/// flag.
- void print(const Timer &Total, std::ostream &OS);
+ void print(const Timer &Total, raw_ostream &OS);
private:
friend class TimerGroup;
/// the relevant timer. This makes it easy to time a region of code.
///
class TimeRegion {
- Timer &T;
+ Timer *T;
TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
public:
- explicit TimeRegion(Timer &t) : T(t) {
- T.startTimer();
+ explicit TimeRegion(Timer &t) : T(&t) {
+ T->startTimer();
+ }
+ explicit TimeRegion(Timer *t) : T(t) {
+ if (T)
+ T->startTimer();
}
~TimeRegion() {
- T.stopTimer();
+ if (T)
+ T->stopTimer();
}
};
///
struct NamedRegionTimer : public TimeRegion {
explicit NamedRegionTimer(const std::string &Name);
+ explicit NamedRegionTimer(const std::string &Name,
+ const std::string &GroupName);
};
private:
friend class Timer;
- void addTimer() { ++NumTimers; }
+ void addTimer();
void removeTimer();
- void addTimerToPrint(const Timer &T) {
- TimersToPrint.push_back(Timer(true, T));
- }
+ void addTimerToPrint(const Timer &T);
};
} // End llvm namespace