#ifndef LLVM_SUPPORT_TIMER_H
#define LLVM_SUPPORT_TIMER_H
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include <cassert>
#include <memory>
#include <string>
#include <vector>
namespace llvm {
class TimerGroup;
class raw_ostream;
class TimeRecord {
double WallTime; double UserTime; double SystemTime; ssize_t MemUsed; uint64_t InstructionsExecuted; public:
TimeRecord()
: WallTime(0), UserTime(0), SystemTime(0), MemUsed(0),
InstructionsExecuted(0) {}
static TimeRecord getCurrentTime(bool Start = true);
double getProcessTime() const { return UserTime + SystemTime; }
double getUserTime() const { return UserTime; }
double getSystemTime() const { return SystemTime; }
double getWallTime() const { return WallTime; }
ssize_t getMemUsed() const { return MemUsed; }
uint64_t getInstructionsExecuted() const { return InstructionsExecuted; }
bool operator<(const TimeRecord &T) const {
return WallTime < T.WallTime;
}
void operator+=(const TimeRecord &RHS) {
WallTime += RHS.WallTime;
UserTime += RHS.UserTime;
SystemTime += RHS.SystemTime;
MemUsed += RHS.MemUsed;
InstructionsExecuted += RHS.InstructionsExecuted;
}
void operator-=(const TimeRecord &RHS) {
WallTime -= RHS.WallTime;
UserTime -= RHS.UserTime;
SystemTime -= RHS.SystemTime;
MemUsed -= RHS.MemUsed;
InstructionsExecuted -= RHS.InstructionsExecuted;
}
void print(const TimeRecord &Total, raw_ostream &OS) const;
};
class Timer {
TimeRecord Time; TimeRecord StartTime; std::string Name; std::string Description; bool Running = false; bool Triggered = false; TimerGroup *TG = nullptr;
Timer **Prev = nullptr; Timer *Next = nullptr; public:
explicit Timer(StringRef TimerName, StringRef TimerDescription) {
init(TimerName, TimerDescription);
}
Timer(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg) {
init(TimerName, TimerDescription, tg);
}
Timer(const Timer &RHS) {
assert(!RHS.TG && "Can only copy uninitialized timers");
}
const Timer &operator=(const Timer &T) {
assert(!TG && !T.TG && "Can only assign uninit timers");
return *this;
}
~Timer();
explicit Timer() = default;
void init(StringRef TimerName, StringRef TimerDescription);
void init(StringRef TimerName, StringRef TimerDescription, TimerGroup &tg);
const std::string &getName() const { return Name; }
const std::string &getDescription() const { return Description; }
bool isInitialized() const { return TG != nullptr; }
bool isRunning() const { return Running; }
bool hasTriggered() const { return Triggered; }
void startTimer();
void stopTimer();
void clear();
TimeRecord getTotalTime() const { return Time; }
private:
friend class TimerGroup;
};
class TimeRegion {
Timer *T;
TimeRegion(const TimeRegion &) = delete;
public:
explicit TimeRegion(Timer &t) : T(&t) {
T->startTimer();
}
explicit TimeRegion(Timer *t) : T(t) {
if (T) T->startTimer();
}
~TimeRegion() {
if (T) T->stopTimer();
}
};
struct NamedRegionTimer : public TimeRegion {
explicit NamedRegionTimer(StringRef Name, StringRef Description,
StringRef GroupName,
StringRef GroupDescription, bool Enabled = true);
};
class TimerGroup {
struct PrintRecord {
TimeRecord Time;
std::string Name;
std::string Description;
PrintRecord(const PrintRecord &Other) = default;
PrintRecord &operator=(const PrintRecord &Other) = default;
PrintRecord(const TimeRecord &Time, const std::string &Name,
const std::string &Description)
: Time(Time), Name(Name), Description(Description) {}
bool operator <(const PrintRecord &Other) const {
return Time < Other.Time;
}
};
std::string Name;
std::string Description;
Timer *FirstTimer = nullptr; std::vector<PrintRecord> TimersToPrint;
TimerGroup **Prev; TimerGroup *Next; TimerGroup(const TimerGroup &TG) = delete;
void operator=(const TimerGroup &TG) = delete;
public:
explicit TimerGroup(StringRef Name, StringRef Description);
explicit TimerGroup(StringRef Name, StringRef Description,
const StringMap<TimeRecord> &Records);
~TimerGroup();
void setName(StringRef NewName, StringRef NewDescription) {
Name.assign(NewName.begin(), NewName.end());
Description.assign(NewDescription.begin(), NewDescription.end());
}
void print(raw_ostream &OS, bool ResetAfterPrint = false);
void clear();
static void printAll(raw_ostream &OS);
static void clearAll();
const char *printJSONValues(raw_ostream &OS, const char *delim);
static const char *printAllJSONValues(raw_ostream &OS, const char *delim);
static void ConstructTimerLists();
static std::unique_ptr<TimerGroup> aquireDefaultGroup();
private:
friend class Timer;
friend void PrintStatisticsJSON(raw_ostream &OS);
void addTimer(Timer &T);
void removeTimer(Timer &T);
void prepareToPrintList(bool reset_time = false);
void PrintQueuedTimers(raw_ostream &OS);
void printJSONValue(raw_ostream &OS, const PrintRecord &R,
const char *suffix, double Value);
};
}
#endif