From 9b3880879257d4e000375bd939702aa5d05dab16 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 1 Oct 2002 19:36:54 +0000 Subject: [PATCH] Checkin generic interval timer support llvm-svn: 3992 --- include/Support/Timer.h | 136 +++++++++++++++++++++++ lib/Support/Timer.cpp | 203 ++++++++++++++++++++++++++++++++++ support/lib/Support/Timer.cpp | 203 ++++++++++++++++++++++++++++++++++ 3 files changed, 542 insertions(+) create mode 100644 include/Support/Timer.h create mode 100644 lib/Support/Timer.cpp create mode 100644 support/lib/Support/Timer.cpp diff --git a/include/Support/Timer.h b/include/Support/Timer.h new file mode 100644 index 00000000000..93d9d166093 --- /dev/null +++ b/include/Support/Timer.h @@ -0,0 +1,136 @@ +//===-- Support/Timer.h - Interval Timing Support ---------------*- C++ -*-===// +// +// This file defines three classes: Timer, TimeRegion, and TimerGroup. +// +// The Timer class is used to track the amount of time spent between invocations +// of it's 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 when it's +// TimerGroup is destroyed. Timer's do not print their information if they are +// never started. +// +// The TimeRegion class is used as a helper class to call the startTimer() and +// stopTimer() methods of the Timer class. When the object is constructed, it +// starts the timer specified as it's argument. When it is destroyed, it stops +// the relevant timer. This makes it easy to time a region of code. +// +// The TimerGroup class is used to group together related timers into a single +// report that is printed when the TimerGroup is destroyed. It is illegal to +// destroy a TimerGroup object before all of the Timers in it are gone. A +// TimerGroup can be specified for a newly created timer in its constructor. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_TIMER_H +#define SUPPORT_TIMER_H + +#include +#include + +class TimerGroup; + +class Timer { + double Elapsed; // Wall clock time elapsed in seconds + double UserTime; // User time elapsed + double SystemTime; // System time elapsed + unsigned long MaxRSS; // Maximum resident set size (in bytes) + unsigned long RSSTemp; // Temp for calculating maxrss + 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. +public: + Timer(const std::string &N); + Timer(const std::string &N, TimerGroup &tg); + Timer(const Timer &T); + ~Timer(); + + double getProcessTime() const { return UserTime+SystemTime; } + double getWallTime() const { return Elapsed; } + unsigned long getMaxRSS() const { return MaxRSS; } + std::string getName() const { return Name; } + + const Timer &operator=(const Timer &T) { + Elapsed = T.Elapsed; + UserTime = T.UserTime; + SystemTime = T.SystemTime; + MaxRSS = T.MaxRSS; + RSSTemp = T.RSSTemp; + Name = T.Name; + Started = T.Started; + assert (TG == T.TG && "Can only assign timers in the same TimerGroup!"); + return *this; + } + + // operator< - Allow sorting... + bool operator<(const Timer &T) const { + // Primary sort key is User+System time + if (UserTime+SystemTime < T.UserTime+T.SystemTime) + return true; + if (UserTime+SystemTime > T.UserTime+T.SystemTime) + return false; + + // Secondary sort key is Wall Time + return Elapsed < T.Elapsed; + } + bool operator>(const Timer &T) const { return T.operator<(*this); } + + /// startTimer - Start the timer running. Time between calls to + /// startTimer/stopTimer is counted by the Timer class. Note that these calls + /// must be correctly paired. + /// + void startTimer(); + + /// stopTimer - Stop the timer. + /// + void stopTimer(); + + /// print - Print the current timer to standard error, and reset the "Started" + /// flag. + void print(const Timer &Total); + +private: + friend class TimerGroup; + + // Copy ctor, initialize with no TG member. + Timer(bool, const Timer &T); + + /// sum - Add the time accumulated in the specified timer into this timer. + /// + void sum(const Timer &T); +}; + + +class TimeRegion { + Timer &T; + TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT +public: + TimeRegion(Timer &t) : T(t) { + T.startTimer(); + } + ~TimeRegion() { + T.stopTimer(); + } +}; + +class TimerGroup { + std::string Name; + unsigned NumTimers; + std::vector TimersToPrint; +public: + TimerGroup(const std::string &name) : Name(name), NumTimers(0) {} + ~TimerGroup() { + assert(NumTimers == 0 && + "TimerGroup destroyed before all contained timers!"); + } + +private: + friend class Timer; + void addTimer() { ++NumTimers; } + void removeTimer(); + void addTimerToPrint(const Timer &T) { + TimersToPrint.push_back(Timer(true, T)); + } +}; + +#endif diff --git a/lib/Support/Timer.cpp b/lib/Support/Timer.cpp new file mode 100644 index 00000000000..fa4844597ed --- /dev/null +++ b/lib/Support/Timer.cpp @@ -0,0 +1,203 @@ +//===-- Timer.cpp - Interval Timing Support -------------------------------===// +// +// Interval Timing implementation. +// +//===----------------------------------------------------------------------===// + +#include "Support/Timer.h" +#include +#include +#include +#include +#include +#include + +static TimerGroup *DefaultTimerGroup = 0; +static TimerGroup *getDefaultTimerGroup() { + if (DefaultTimerGroup) return DefaultTimerGroup; + return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers"); +} + +Timer::Timer(const std::string &N) + : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), + Started(false), TG(getDefaultTimerGroup()) { + TG->addTimer(); +} + +Timer::Timer(const std::string &N, TimerGroup &tg) + : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), + Started(false), TG(&tg) { + TG->addTimer(); +} + +Timer::Timer(const Timer &T) { + TG = T.TG; + if (TG) TG->addTimer(); + operator=(T); +} + + +// Copy ctor, initialize with no TG member. +Timer::Timer(bool, const Timer &T) { + TG = T.TG; // Avoid assertion in operator= + operator=(T); // Copy contents + TG = 0; +} + + +Timer::~Timer() { + if (TG) { + if (Started) { + Started = false; + TG->addTimerToPrint(*this); + } + TG->removeTimer(); + } +} + +struct TimeRecord { + double Elapsed, UserTime, SystemTime; + unsigned long MaxRSS; +}; + +static TimeRecord getTimeRecord() { + static unsigned long PageSize = 0; + + if (PageSize == 0) { +#ifdef _SC_PAGE_SIZE + PageSize = sysconf(_SC_PAGE_SIZE); +#else +#ifdef _SC_PAGESIZE + PageSize = sysconf(_SC_PAGESIZE); +#else + PageSize = getpagesize(); +#endif +#endif + } + + struct rusage RU; + struct timeval T; + gettimeofday(&T, 0); + if (getrusage(RUSAGE_SELF, &RU)) { + perror("getrusage call failed: -time-passes info incorrect!"); + } + + TimeRecord Result; + Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; + Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; + Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; + Result.MaxRSS = RU.ru_maxrss*PageSize; + return Result; +} + + +void Timer::startTimer() { + Started = true; + TimeRecord TR = getTimeRecord(); + Elapsed -= TR.Elapsed; + UserTime -= TR.UserTime; + SystemTime -= TR.SystemTime; + MaxRSS -= TR.MaxRSS; +} + +void Timer::stopTimer() { + TimeRecord TR = getTimeRecord(); + Elapsed += TR.Elapsed; + UserTime += TR.UserTime; + SystemTime += TR.SystemTime; + MaxRSS += TR.MaxRSS; +} + +void Timer::sum(const Timer &T) { + Elapsed += T.Elapsed; + UserTime += T.UserTime; + SystemTime += T.SystemTime; + MaxRSS += T.MaxRSS; +} + +//===----------------------------------------------------------------------===// +// TimerGroup Implementation +//===----------------------------------------------------------------------===// + +static void printVal(double Val, double Total) { + if (Total < 1e-7) // Avoid dividing by zero... + fprintf(stderr, " ----- "); + else + fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total); +} + +void Timer::print(const Timer &Total) { + if (Total.UserTime) + printVal(UserTime, Total.UserTime); + if (Total.SystemTime) + printVal(SystemTime, Total.SystemTime); + if (Total.getProcessTime()) + printVal(getProcessTime(), Total.getProcessTime()); + printVal(Elapsed, Total.Elapsed); + + fprintf(stderr, " "); + + if (Total.MaxRSS) + std::cerr << MaxRSS << "\t"; + std::cerr << Name << "\n"; + + Started = false; // Once printed, don't print again +} + + +void TimerGroup::removeTimer() { + if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report... + // Sort the timers in descending order by amount of time taken... + std::sort(TimersToPrint.begin(), TimersToPrint.end(), + std::greater()); + + // Figure out how many spaces to indent TimerGroup name... + unsigned Padding = (80-Name.length())/2; + if (Padding > 80) Padding = 0; // Don't allow "negative" numbers + + ++NumTimers; + { // Scope to contain Total timer... don't allow total timer to drop us to + // zero timers... + Timer Total("TOTAL"); + + for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) + Total.sum(TimersToPrint[i]); + + // Print out timing header... + std::cerr << "===" << std::string(73, '-') << "===\n" + << std::string(Padding, ' ') << Name << "\n" + << "===" << std::string(73, '-') + << "===\n Total Execution Time: " << Total.getProcessTime() + << " seconds (" << Total.getWallTime() + << " wall clock)\n\n"; + + if (Total.UserTime) + std::cerr << " ---User Time---"; + if (Total.SystemTime) + std::cerr << " --System Time--"; + if (Total.getProcessTime()) + std::cerr << " --User+System--"; + std::cerr << " ---Wall Time---"; + + if (Total.getMaxRSS()) + std::cerr << " ---Mem---"; + std::cerr << " --- Name ---\n"; + + // Loop through all of the timing data, printing it out... + for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) + TimersToPrint[i].print(Total); + + Total.print(Total); + std::cerr << std::endl; // Flush output + } + --NumTimers; + + TimersToPrint.clear(); + } + + // Delete default timer group! + if (NumTimers == 0 && this == DefaultTimerGroup) { + delete DefaultTimerGroup; + DefaultTimerGroup = 0; + } +} diff --git a/support/lib/Support/Timer.cpp b/support/lib/Support/Timer.cpp new file mode 100644 index 00000000000..fa4844597ed --- /dev/null +++ b/support/lib/Support/Timer.cpp @@ -0,0 +1,203 @@ +//===-- Timer.cpp - Interval Timing Support -------------------------------===// +// +// Interval Timing implementation. +// +//===----------------------------------------------------------------------===// + +#include "Support/Timer.h" +#include +#include +#include +#include +#include +#include + +static TimerGroup *DefaultTimerGroup = 0; +static TimerGroup *getDefaultTimerGroup() { + if (DefaultTimerGroup) return DefaultTimerGroup; + return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers"); +} + +Timer::Timer(const std::string &N) + : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), + Started(false), TG(getDefaultTimerGroup()) { + TG->addTimer(); +} + +Timer::Timer(const std::string &N, TimerGroup &tg) + : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), + Started(false), TG(&tg) { + TG->addTimer(); +} + +Timer::Timer(const Timer &T) { + TG = T.TG; + if (TG) TG->addTimer(); + operator=(T); +} + + +// Copy ctor, initialize with no TG member. +Timer::Timer(bool, const Timer &T) { + TG = T.TG; // Avoid assertion in operator= + operator=(T); // Copy contents + TG = 0; +} + + +Timer::~Timer() { + if (TG) { + if (Started) { + Started = false; + TG->addTimerToPrint(*this); + } + TG->removeTimer(); + } +} + +struct TimeRecord { + double Elapsed, UserTime, SystemTime; + unsigned long MaxRSS; +}; + +static TimeRecord getTimeRecord() { + static unsigned long PageSize = 0; + + if (PageSize == 0) { +#ifdef _SC_PAGE_SIZE + PageSize = sysconf(_SC_PAGE_SIZE); +#else +#ifdef _SC_PAGESIZE + PageSize = sysconf(_SC_PAGESIZE); +#else + PageSize = getpagesize(); +#endif +#endif + } + + struct rusage RU; + struct timeval T; + gettimeofday(&T, 0); + if (getrusage(RUSAGE_SELF, &RU)) { + perror("getrusage call failed: -time-passes info incorrect!"); + } + + TimeRecord Result; + Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; + Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; + Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; + Result.MaxRSS = RU.ru_maxrss*PageSize; + return Result; +} + + +void Timer::startTimer() { + Started = true; + TimeRecord TR = getTimeRecord(); + Elapsed -= TR.Elapsed; + UserTime -= TR.UserTime; + SystemTime -= TR.SystemTime; + MaxRSS -= TR.MaxRSS; +} + +void Timer::stopTimer() { + TimeRecord TR = getTimeRecord(); + Elapsed += TR.Elapsed; + UserTime += TR.UserTime; + SystemTime += TR.SystemTime; + MaxRSS += TR.MaxRSS; +} + +void Timer::sum(const Timer &T) { + Elapsed += T.Elapsed; + UserTime += T.UserTime; + SystemTime += T.SystemTime; + MaxRSS += T.MaxRSS; +} + +//===----------------------------------------------------------------------===// +// TimerGroup Implementation +//===----------------------------------------------------------------------===// + +static void printVal(double Val, double Total) { + if (Total < 1e-7) // Avoid dividing by zero... + fprintf(stderr, " ----- "); + else + fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total); +} + +void Timer::print(const Timer &Total) { + if (Total.UserTime) + printVal(UserTime, Total.UserTime); + if (Total.SystemTime) + printVal(SystemTime, Total.SystemTime); + if (Total.getProcessTime()) + printVal(getProcessTime(), Total.getProcessTime()); + printVal(Elapsed, Total.Elapsed); + + fprintf(stderr, " "); + + if (Total.MaxRSS) + std::cerr << MaxRSS << "\t"; + std::cerr << Name << "\n"; + + Started = false; // Once printed, don't print again +} + + +void TimerGroup::removeTimer() { + if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report... + // Sort the timers in descending order by amount of time taken... + std::sort(TimersToPrint.begin(), TimersToPrint.end(), + std::greater()); + + // Figure out how many spaces to indent TimerGroup name... + unsigned Padding = (80-Name.length())/2; + if (Padding > 80) Padding = 0; // Don't allow "negative" numbers + + ++NumTimers; + { // Scope to contain Total timer... don't allow total timer to drop us to + // zero timers... + Timer Total("TOTAL"); + + for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) + Total.sum(TimersToPrint[i]); + + // Print out timing header... + std::cerr << "===" << std::string(73, '-') << "===\n" + << std::string(Padding, ' ') << Name << "\n" + << "===" << std::string(73, '-') + << "===\n Total Execution Time: " << Total.getProcessTime() + << " seconds (" << Total.getWallTime() + << " wall clock)\n\n"; + + if (Total.UserTime) + std::cerr << " ---User Time---"; + if (Total.SystemTime) + std::cerr << " --System Time--"; + if (Total.getProcessTime()) + std::cerr << " --User+System--"; + std::cerr << " ---Wall Time---"; + + if (Total.getMaxRSS()) + std::cerr << " ---Mem---"; + std::cerr << " --- Name ---\n"; + + // Loop through all of the timing data, printing it out... + for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) + TimersToPrint[i].print(Total); + + Total.print(Total); + std::cerr << std::endl; // Flush output + } + --NumTimers; + + TimersToPrint.clear(); + } + + // Delete default timer group! + if (NumTimers == 0 && this == DefaultTimerGroup) { + delete DefaultTimerGroup; + DefaultTimerGroup = 0; + } +}