2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
|
|
|
|
#include "tstopwatch.h"
|
|
|
|
|
2016-06-14 00:50:05 +12:00
|
|
|
#include <sstream>
|
2016-06-07 19:06:00 +12:00
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <stdlib.h>
|
2016-06-15 18:43:10 +12:00
|
|
|
#else //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#if defined(__APPLE_CC__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifndef STW_TICKS_PER_SECOND
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifndef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
extern "C" long sysconf(int);
|
|
|
|
#define STW_TICKS_PER_SECOND sysconf(_SC_CLK_TCK)
|
|
|
|
#else
|
|
|
|
#define STW_TICKS_PER_SECOND CLK_TCK
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXSWNAMELENGHT 40
|
|
|
|
#define MAXSWTIMELENGHT 12
|
|
|
|
|
|
|
|
TStopWatch TStopWatch::StopWatch[10];
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
enum TimerType { TTUUnknown, TTUHiRes, TTUTickCount };
|
2016-03-19 06:57:51 +13:00
|
|
|
static void determineTimer();
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
static TimerType timerToUse = TTUUnknown;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
static LARGE_INTEGER perfFreq; // ticks per second
|
|
|
|
static int perfFreqAdjust = 0; // in case Freq is too big
|
|
|
|
static int overheadTicks = 0; // overhead in calling timer
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#else
|
|
|
|
static TimerType timerToUse = TTUTickCount;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-04-19 19:32:17 +12:00
|
|
|
TStopWatch::TStopWatch(std::string name)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_name(name), m_active(false), m_isRunning(false) {
|
|
|
|
if (timerToUse == TTUUnknown) determineTimer();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_start = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
m_startUser.dwHighDateTime = m_startUser.dwLowDateTime = 0;
|
|
|
|
m_startSystem.dwHighDateTime = m_startSystem.dwLowDateTime = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
m_startUser = 0;
|
|
|
|
m_startSystem = 0;
|
|
|
|
#endif //_WIN32
|
|
|
|
m_tm = 0;
|
|
|
|
m_tmUser = 0;
|
|
|
|
m_tmSystem = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TStopWatch::~TStopWatch() { m_active = false; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::setStartToCurrentTime() {
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
FILETIME creationTime, exitTime;
|
|
|
|
BOOL ret =
|
|
|
|
GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
|
|
&creationTime, &exitTime, &m_startSystem, &m_startUser);
|
|
|
|
|
|
|
|
if (timerToUse == TTUTickCount) {
|
|
|
|
m_start = GetTickCount();
|
|
|
|
} else {
|
|
|
|
QueryPerformanceCounter(&m_hrStart);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
struct tms clk;
|
|
|
|
m_start = times(&clk);
|
|
|
|
m_startUser = clk.tms_utime;
|
|
|
|
m_startSystem = clk.tms_stime;
|
|
|
|
#endif //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::reset() {
|
|
|
|
m_tm = 0;
|
|
|
|
m_tmUser = 0;
|
|
|
|
m_tmSystem = 0;
|
|
|
|
setStartToCurrentTime();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::start(bool resetFlag) {
|
|
|
|
if (resetFlag) reset();
|
|
|
|
if (m_isRunning) return;
|
|
|
|
m_active = true;
|
|
|
|
m_isRunning = true;
|
|
|
|
setStartToCurrentTime();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
inline __int64 FileTimeToInt64(LPFILETIME pFileTime) {
|
|
|
|
__int64 val;
|
|
|
|
val = pFileTime->dwHighDateTime;
|
|
|
|
val <<= 32;
|
|
|
|
val |= pFileTime->dwLowDateTime;
|
|
|
|
return val;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
2016-06-15 18:43:10 +12:00
|
|
|
// Aggiunge il tempo trascorso fra start(startUser, startSystem) e l'istante
|
|
|
|
// corrente
|
2016-03-19 06:57:51 +13:00
|
|
|
// a tm(tmUser, tmSystem)
|
|
|
|
//
|
2016-06-15 18:43:10 +12:00
|
|
|
static void checkTime(START start, START_USER startUser,
|
|
|
|
START_SYSTEM startSystem, TM_TOTAL &tm, TM_USER &tmUser,
|
|
|
|
TM_SYSTEM &tmSystem) {
|
|
|
|
assert(timerToUse == TTUTickCount);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
DWORD tm_stop;
|
|
|
|
FILETIME creationTime, exitTime, stopSystem, stopUser;
|
|
|
|
BOOL ret =
|
|
|
|
GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
|
|
&creationTime, &exitTime, &stopSystem, &stopUser);
|
|
|
|
tm_stop = GetTickCount();
|
|
|
|
assert(tm_stop >= start);
|
|
|
|
tm += tm_stop - start; // total elapsed time
|
|
|
|
|
|
|
|
tmUser += FileTimeToInt64(&stopUser) -
|
|
|
|
FileTimeToInt64(&startUser); // user elapsed time
|
|
|
|
tmSystem += FileTimeToInt64(&stopSystem) -
|
|
|
|
FileTimeToInt64(&startSystem); // system elapsed time
|
|
|
|
|
|
|
|
#else // _WIN32
|
|
|
|
|
|
|
|
struct tms clk;
|
|
|
|
clock_t tm_stop;
|
|
|
|
tm_stop = times(&clk);
|
|
|
|
assert(tm_stop >= start);
|
|
|
|
tm += tm_stop - start;
|
|
|
|
tmUser += clk.tms_utime - startUser;
|
|
|
|
tmSystem += clk.tms_stime - startSystem;
|
|
|
|
|
|
|
|
#endif // _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//
|
|
|
|
// come checkTime, ma usa i timer ad alta risoluzione
|
|
|
|
//
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void hrCheckTime(LARGE_INTEGER start, START_USER startUser,
|
|
|
|
START_SYSTEM startSystem, TM_TOTAL &tm, TM_USER &tmUser,
|
|
|
|
TM_SYSTEM &tmSystem) {
|
|
|
|
assert(timerToUse != TTUTickCount);
|
|
|
|
|
|
|
|
LARGE_INTEGER hrTm_stop;
|
|
|
|
FILETIME creationTime, exitTime, stopSystem, stopUser;
|
|
|
|
BOOL ret =
|
|
|
|
GetProcessTimes(GetCurrentProcess(), // specifies the process of interest
|
|
|
|
&creationTime, &exitTime, &stopSystem, &stopUser);
|
|
|
|
|
|
|
|
QueryPerformanceCounter(&hrTm_stop);
|
|
|
|
assert(hrTm_stop.HighPart > start.HighPart ||
|
|
|
|
hrTm_stop.HighPart == start.HighPart &&
|
|
|
|
hrTm_stop.LowPart >= start.LowPart);
|
|
|
|
|
|
|
|
LARGE_INTEGER Freq = perfFreq;
|
|
|
|
int Oht = overheadTicks;
|
|
|
|
|
|
|
|
LARGE_INTEGER dtime;
|
|
|
|
// faccio "a mano" la differenza dtime = m_tStop - m_tStart
|
|
|
|
dtime.HighPart = hrTm_stop.HighPart - start.HighPart;
|
|
|
|
if (hrTm_stop.LowPart >= start.LowPart)
|
|
|
|
dtime.LowPart = hrTm_stop.LowPart - start.LowPart;
|
|
|
|
else {
|
|
|
|
assert(dtime.HighPart > 0);
|
|
|
|
dtime.HighPart--;
|
|
|
|
dtime.LowPart = hrTm_stop.LowPart + ~start.LowPart + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int shift = 0;
|
|
|
|
if (Freq.HighPart > 0) {
|
|
|
|
int h = Freq.HighPart;
|
|
|
|
while (h > 0) {
|
|
|
|
h >>= 1;
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((dtime.HighPart >> shift) > 0) {
|
|
|
|
int h = dtime.HighPart >> shift;
|
|
|
|
while (h > 0) {
|
|
|
|
h >>= 1;
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shift > 0) {
|
|
|
|
dtime.QuadPart = Int64ShrlMod32(dtime.QuadPart, shift);
|
|
|
|
Freq.QuadPart = Int64ShrlMod32(Freq.QuadPart, shift);
|
|
|
|
}
|
|
|
|
assert(Freq.HighPart == 0);
|
|
|
|
assert(dtime.HighPart == 0);
|
|
|
|
|
|
|
|
double totalTime = 1000.0 * dtime.LowPart / Freq.LowPart;
|
|
|
|
tm += troundp(totalTime);
|
|
|
|
|
|
|
|
tmUser += FileTimeToInt64(&stopUser) -
|
|
|
|
FileTimeToInt64(&startUser); // user elapsed time
|
|
|
|
tmSystem += FileTimeToInt64(&stopSystem) -
|
|
|
|
FileTimeToInt64(&startSystem); // system elapsed time
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::stop() {
|
|
|
|
if (!m_isRunning) return;
|
|
|
|
m_isRunning = false;
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (timerToUse == TTUTickCount)
|
|
|
|
checkTime(m_start, m_startUser, m_startSystem, m_tm, m_tmUser, m_tmSystem);
|
|
|
|
else
|
|
|
|
hrCheckTime(m_hrStart, m_startUser, m_startSystem, m_tm, m_tmUser,
|
|
|
|
m_tmSystem);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
checkTime(m_start, m_startUser, m_startSystem, m_tm, m_tmUser, m_tmSystem);
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::getElapsedTime(TM_TOTAL &tm, TM_USER &user,
|
|
|
|
TM_SYSTEM &system) {
|
|
|
|
if (m_isRunning) {
|
|
|
|
TM_TOTAL cur_tm = 0;
|
|
|
|
TM_USER cur_tmUser = 0;
|
|
|
|
TM_SYSTEM cur_tmSystem = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (timerToUse == TTUTickCount)
|
|
|
|
checkTime(m_start, m_startUser, m_startSystem, cur_tm, cur_tmUser,
|
|
|
|
cur_tmSystem);
|
|
|
|
else
|
|
|
|
hrCheckTime(m_hrStart, m_startUser, m_startSystem, cur_tm, cur_tmUser,
|
|
|
|
cur_tmSystem);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
checkTime(m_start, m_startUser, m_startSystem, cur_tm, cur_tmUser,
|
|
|
|
cur_tmSystem);
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
tm = m_tm + cur_tm;
|
|
|
|
user = m_tmUser + cur_tmUser;
|
|
|
|
system = m_tmSystem + cur_tmSystem;
|
|
|
|
} else {
|
|
|
|
tm = m_tm;
|
|
|
|
user = m_tmUser;
|
|
|
|
system = m_tmSystem;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TUINT32 TStopWatch::getTotalTime() {
|
|
|
|
TM_TOTAL tm;
|
|
|
|
TM_USER user;
|
|
|
|
TM_SYSTEM system;
|
|
|
|
getElapsedTime(tm, user, system);
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return tm;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
return (TINT32)(tm * 1000) / STW_TICKS_PER_SECOND;
|
|
|
|
#endif //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TUINT32 TStopWatch::getUserTime() {
|
|
|
|
TM_TOTAL tm;
|
|
|
|
TM_USER user;
|
|
|
|
TM_SYSTEM system;
|
|
|
|
getElapsedTime(tm, user, system);
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return (TINT32)(user / 10000);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
return (TINT32)(user * 1000) / STW_TICKS_PER_SECOND;
|
|
|
|
#endif //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TUINT32 TStopWatch::getSystemTime() {
|
|
|
|
TM_TOTAL tm;
|
|
|
|
TM_USER user;
|
|
|
|
TM_SYSTEM system;
|
|
|
|
getElapsedTime(tm, user, system);
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return (TINT32)(system / 10000);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
return (TINT32)(system * 1000) / STW_TICKS_PER_SECOND;
|
|
|
|
#endif //_WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TStopWatch::operator string() {
|
|
|
|
ostringstream out;
|
|
|
|
out << m_name.c_str() << ": " << (int)getTotalTime() << " u"
|
|
|
|
<< (int)getUserTime() << " s" << (TINT32)getSystemTime();
|
|
|
|
return out.str();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::print() { print(cout); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::print(ostream &out) {
|
|
|
|
string s(*this);
|
|
|
|
out << s.c_str() << endl;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::printGlobals(ostream &out) {
|
|
|
|
const int n = sizeof(StopWatch) / sizeof(StopWatch[0]);
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
if (StopWatch[i].m_active) StopWatch[i].print(out);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TStopWatch::printGlobals() { printGlobals(cout); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void dummyFunction() {
|
|
|
|
// It's used just to calculate the overhead
|
|
|
|
return;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void determineTimer() {
|
|
|
|
void (*pFunc)() = dummyFunction;
|
|
|
|
// cout << "DETERMINE TIMER" << endl;
|
|
|
|
// Assume the worst
|
|
|
|
timerToUse = TTUTickCount;
|
|
|
|
if (QueryPerformanceFrequency(&perfFreq)) {
|
|
|
|
// We can use hires timer, determine overhead
|
|
|
|
timerToUse = TTUHiRes;
|
|
|
|
overheadTicks = 200;
|
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
LARGE_INTEGER b, e;
|
|
|
|
int Ticks;
|
|
|
|
QueryPerformanceCounter(&b);
|
|
|
|
(*pFunc)();
|
|
|
|
QueryPerformanceCounter(&e);
|
|
|
|
Ticks = e.LowPart - b.LowPart;
|
|
|
|
if (Ticks >= 0 && Ticks < overheadTicks) overheadTicks = Ticks;
|
|
|
|
}
|
|
|
|
// See if Freq fits in 32 bits; if not lose some precision
|
|
|
|
perfFreqAdjust = 0;
|
|
|
|
int High32 = perfFreq.HighPart;
|
|
|
|
while (High32) {
|
|
|
|
High32 >>= 1;
|
|
|
|
perfFreqAdjust++;
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
void determineTimer() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|