2008-05-14 11:57:45 +04:00
|
|
|
#include "Timer.h"
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "Util.h"
|
2012-02-27 03:34:51 +04:00
|
|
|
#include <cstdio>
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-02-27 03:34:51 +04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
uint64_t GetMicroSeconds(const struct timeval& tv) {
|
|
|
|
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2012-02-27 03:34:51 +04:00
|
|
|
uint64_t GetTimeOfDayMicroSeconds() {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-03-03 18:49:17 +04:00
|
|
|
void Timer::GetCPUTimeMicroSeconds(Timer::CPUTime* cpu_time) const {
|
2012-02-27 03:34:51 +04:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
struct rusage usage;
|
|
|
|
if (getrusage(RUSAGE_SELF, &usage)) {
|
|
|
|
TRACE_ERR("Error occurred: getrusage().\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-03 18:49:17 +04:00
|
|
|
cpu_time->user_time = GetMicroSeconds(usage.ru_utime);
|
|
|
|
cpu_time->sys_time = GetMicroSeconds(usage.ru_stime);
|
2012-02-27 03:34:51 +04:00
|
|
|
#else // Windows
|
|
|
|
// Not implemented yet.
|
|
|
|
// TODO: implement the Windows version using native APIs.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::get_elapsed_cpu_time() const {
|
|
|
|
return static_cast<double>(get_elapsed_cpu_time_microseconds()) * 1e-6;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Timer::get_elapsed_cpu_time_microseconds() const {
|
2012-03-03 18:49:17 +04:00
|
|
|
CPUTime e;
|
|
|
|
GetCPUTimeMicroSeconds(&e);
|
2012-02-27 03:34:51 +04:00
|
|
|
return (e.user_time - m_start_time.user_time) +
|
|
|
|
(e.sys_time - m_start_time.sys_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::get_elapsed_wall_time() const {
|
|
|
|
return static_cast<double>(get_elapsed_wall_time_microseconds()) * 1e-6;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Timer::get_elapsed_wall_time_microseconds() const {
|
|
|
|
return GetTimeOfDayMicroSeconds() - m_wall;
|
2009-01-07 16:30:06 +03:00
|
|
|
}
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
void Timer::start(const char* msg)
|
|
|
|
{
|
|
|
|
// Print an optional message, something like "Starting timer t";
|
2011-02-24 15:42:19 +03:00
|
|
|
if (msg) TRACE_ERR( msg << std::endl);
|
2012-02-26 08:52:47 +04:00
|
|
|
if (m_is_running) return;
|
|
|
|
m_is_running = true;
|
2012-02-27 03:34:51 +04:00
|
|
|
m_wall = GetTimeOfDayMicroSeconds();
|
2012-03-03 18:49:17 +04:00
|
|
|
GetCPUTimeMicroSeconds(&m_start_time);
|
2012-02-27 03:34:51 +04:00
|
|
|
}
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2012-02-27 03:34:51 +04:00
|
|
|
void Timer::restart(const char* msg)
|
|
|
|
{
|
|
|
|
if (msg) {
|
|
|
|
TRACE_ERR(msg << std::endl);
|
|
|
|
}
|
|
|
|
m_wall = GetTimeOfDayMicroSeconds();
|
2012-03-03 18:49:17 +04:00
|
|
|
GetCPUTimeMicroSeconds(&m_start_time);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::check(const char* msg)
|
|
|
|
{
|
|
|
|
// Print an optional message, something like "Checking timer t";
|
2009-01-07 16:30:06 +03:00
|
|
|
if (msg) TRACE_ERR( msg << " : ");
|
2012-02-27 03:34:51 +04:00
|
|
|
|
|
|
|
if (m_is_running) {
|
|
|
|
TRACE_ERR("[Wall " << get_elapsed_wall_time()
|
|
|
|
<< " CPU " << get_elapsed_cpu_time() << "] seconds.\n");
|
|
|
|
} else {
|
|
|
|
TRACE_ERR("WARNING: the timer is not running.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Timer::ToString() const {
|
|
|
|
std::string res;
|
|
|
|
char tmp[64];
|
|
|
|
const double wall = get_elapsed_wall_time();
|
2012-03-03 18:49:17 +04:00
|
|
|
CPUTime e;
|
|
|
|
GetCPUTimeMicroSeconds(&e);
|
2012-02-27 03:34:51 +04:00
|
|
|
const double utime = (e.user_time - m_start_time.user_time) * 1e-6;
|
|
|
|
const double stime = (e.sys_time - m_start_time.sys_time) * 1e-6;
|
|
|
|
std::snprintf(tmp, sizeof(tmp), "wall %f user %f sec. sys %f sec. total %f sec.",
|
|
|
|
wall, utime, stime, utime + stime);
|
|
|
|
res.append(tmp);
|
|
|
|
return res;
|
2009-01-07 16:30:06 +03:00
|
|
|
}
|