20.10.2013 - Modified PIObject - virtual debugName() for macro piCoutObj, improved timer measurements and timers on Windows
This commit is contained in:
98
pitimer.cpp
98
pitimer.cpp
@@ -46,7 +46,21 @@
|
||||
* Example: \snippet pitimer.cpp elapsed
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*! \class PISystemTime
|
||||
* \brief System time
|
||||
*
|
||||
* \section PISystemTime_sec0 Synopsis
|
||||
* This class provide arithmetic functions for POSIX system time.
|
||||
* This time represents as seconds and nanosecons in integer formats.
|
||||
* You can take current system time with function \a currentSystemTime(),
|
||||
* compare times, sum or subtract two times, convert time to/from
|
||||
* seconds, milliseconds, microseconds or nanoseconds.
|
||||
* \section PISystemTime_sec1 Example
|
||||
* \snippet pitimer.cpp system_time
|
||||
*/
|
||||
|
||||
|
||||
bool operator ==(const PITime & t0, const PITime & t1) {
|
||||
return (t0.hours == t1.hours && t0.minutes == t1.minutes && t0.seconds == t1.seconds);
|
||||
}
|
||||
@@ -201,7 +215,7 @@ void PITimer::start(double msecs) {
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer " << msecs << " msecs\n";
|
||||
if (ti == -1) {
|
||||
piCoutObj << "[PITimer] Can`t create timer for " << msecs << " msecs: " << errorString();
|
||||
piCoutObj << "Can`t create timer for " << msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, 0, &spec, 0);
|
||||
@@ -219,7 +233,7 @@ void PITimer::deferredStart(double interval_msecs, double delay_msecs) {
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer\n";
|
||||
if (ti == -1) {
|
||||
piCoutObj << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
piCoutObj << "Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, 0, &spec, 0);
|
||||
@@ -245,7 +259,7 @@ void PITimer::deferredStart(double interval_msecs, const PIDateTime & start_date
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer\n";
|
||||
if (ti == -1) {
|
||||
piCoutObj << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
piCoutObj << "Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, TIMER_ABSTIME, &spec, 0);
|
||||
@@ -273,7 +287,7 @@ void PITimer::TimerPool::begin() {
|
||||
sa.sa_handler = empty_handler;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGALRM, &sa, 0) == -1) {
|
||||
piCoutObj << "[PITimer] sigaction error: " << errorString();
|
||||
piCoutObj << "sigaction error: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}*/
|
||||
@@ -287,12 +301,12 @@ void PITimer::TimerPool::begin() {
|
||||
spec.it_value = spec.it_interval;
|
||||
//cout << "***create pool timer\n";
|
||||
if (timer_create(CLOCK_REALTIME, &se, &timer) == -1) {
|
||||
piCoutObj << "[PITimer] Can`t create timer for pool: " << errorString();
|
||||
piCoutObj << "Can`t create timer for pool: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
if (timer_settime(timer, 0, &spec, 0) == -1) {
|
||||
piCoutObj << "[PITimer] Can`t set timer for pool: " << errorString();
|
||||
piCoutObj << "Can`t set timer for pool: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
@@ -430,8 +444,12 @@ void PITimer::run() {
|
||||
deferred_ = false;
|
||||
if (!running_) return;
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
tt_st = __PIQueryPerformanceCounter();
|
||||
#else
|
||||
(st_time - currentSystemTime()).sleep();
|
||||
st_time += inc_time;
|
||||
#endif
|
||||
//if (lockRun) lock();
|
||||
if (ret_func != 0) ret_func(data, 1);
|
||||
timeout(data, 1);
|
||||
@@ -444,6 +462,11 @@ void PITimer::run() {
|
||||
timeout(data, i.delim);
|
||||
tick(data, i.delim);
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
tt_cur = __PIQueryPerformanceCounter();
|
||||
double cdelay = interval_ - ((tt_cur - tt_st) / double(__pi_perf_freq) * 1.E+3);
|
||||
if (cdelay > 0.) Sleep(piRoundd(cdelay));
|
||||
#endif
|
||||
//if (lockRun) unlock();
|
||||
}
|
||||
|
||||
@@ -451,8 +474,10 @@ void PITimer::run() {
|
||||
|
||||
double PITimer::elapsed_n() {
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur - t_st) * 1000000.;
|
||||
pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? ((pc_cur - pc_st) / double(__pi_perf_freq) * 1.E+9) : -1.);
|
||||
//t_cur = GetCurrentTime();
|
||||
//return (t_cur - t_st) * 1000000.;
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
clock_get_time(__pi_mac_clock, &t_cur);
|
||||
@@ -466,8 +491,10 @@ double PITimer::elapsed_n() {
|
||||
|
||||
double PITimer::elapsed_u() {
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur - t_st) * 1000.;
|
||||
pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? ((pc_cur - pc_st) / double(__pi_perf_freq) * 1.E+6) : -1.);
|
||||
//t_cur = GetCurrentTime();
|
||||
//return (t_cur - t_st) * 1000.;
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
clock_get_time(__pi_mac_clock, &t_cur);
|
||||
@@ -481,8 +508,10 @@ double PITimer::elapsed_u() {
|
||||
|
||||
double PITimer::elapsed_m() {
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (double)(t_cur - t_st);
|
||||
pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? ((pc_cur - pc_st) / double(__pi_perf_freq) * 1.E+3) : -1.);
|
||||
//t_cur = GetCurrentTime();
|
||||
//return (double)(t_cur - t_st);
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
clock_get_time(__pi_mac_clock, &t_cur);
|
||||
@@ -496,8 +525,10 @@ double PITimer::elapsed_m() {
|
||||
|
||||
double PITimer::elapsed_s() {
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur - t_st) / 1000.;
|
||||
pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? ((pc_cur - pc_st) / double(__pi_perf_freq)) : -1.);
|
||||
//t_cur = GetCurrentTime();
|
||||
//return (t_cur - t_st) / 1000.;
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
clock_get_time(__pi_mac_clock, &t_cur);
|
||||
@@ -556,8 +587,10 @@ PISystemTime PITimer::reset_time() {
|
||||
|
||||
double PITimer::elapsed_system_n() {
|
||||
#ifdef WINDOWS
|
||||
long t_cur = GetCurrentTime();
|
||||
return (t_cur * 1000000.);
|
||||
llong pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? (pc_cur / double(__pi_perf_freq) * 1.E+9) : -1.);
|
||||
//long t_cur = GetCurrentTime();
|
||||
//return (t_cur * 1000000.);
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
mach_timespec_t t_cur;
|
||||
@@ -573,8 +606,10 @@ double PITimer::elapsed_system_n() {
|
||||
|
||||
double PITimer::elapsed_system_u() {
|
||||
#ifdef WINDOWS
|
||||
long t_cur = GetCurrentTime();
|
||||
return (t_cur * 1000.);
|
||||
llong pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? (pc_cur / double(__pi_perf_freq) * 1.E+6) : -1.);
|
||||
//long t_cur = GetCurrentTime();
|
||||
//return (t_cur * 1000.);
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
mach_timespec_t t_cur;
|
||||
@@ -590,8 +625,10 @@ double PITimer::elapsed_system_u() {
|
||||
|
||||
double PITimer::elapsed_system_m() {
|
||||
#ifdef WINDOWS
|
||||
long t_cur = GetCurrentTime();
|
||||
return (double)t_cur;
|
||||
llong pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? (pc_cur / double(__pi_perf_freq) * 1.E+3) : -1.);
|
||||
//long t_cur = GetCurrentTime();
|
||||
//return (double)t_cur;
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
mach_timespec_t t_cur;
|
||||
@@ -607,8 +644,10 @@ double PITimer::elapsed_system_m() {
|
||||
|
||||
double PITimer::elapsed_system_s() {
|
||||
#ifdef WINDOWS
|
||||
long t_cur = GetCurrentTime();
|
||||
return (t_cur / 1000.);
|
||||
llong pc_cur = __PIQueryPerformanceCounter();
|
||||
return ((__pi_perf_freq > 0) ? (pc_cur / double(__pi_perf_freq)) : -1.);
|
||||
//long t_cur = GetCurrentTime();
|
||||
//return (t_cur / 1000.);
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
mach_timespec_t t_cur;
|
||||
@@ -661,8 +700,17 @@ PIDateTime currentDateTime() {
|
||||
|
||||
PISystemTime currentSystemTime() {
|
||||
#ifdef WINDOWS
|
||||
long t_cur = GetCurrentTime();
|
||||
return PISystemTime(t_cur / 1000, (t_cur % 1000) * 1000000);
|
||||
FILETIME ft, sft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
sft.dwHighDateTime = ft.dwHighDateTime - __pi_ftjan1970.dwHighDateTime;
|
||||
if (ft.dwLowDateTime < __pi_ftjan1970.dwLowDateTime) {
|
||||
sft.dwLowDateTime = ft.dwLowDateTime + (0xFFFFFFFF - __pi_ftjan1970.dwLowDateTime);
|
||||
sft.dwHighDateTime++;
|
||||
} else
|
||||
sft.dwLowDateTime = ft.dwLowDateTime - __pi_ftjan1970.dwLowDateTime;
|
||||
return PISystemTime(sft.dwHighDateTime * 100 + sft.dwLowDateTime / 10000000, (sft.dwLowDateTime % 10000000) * 100);
|
||||
//long t_cur = GetCurrentTime();
|
||||
//return PISystemTime(t_cur / 1000, (t_cur % 1000) * 1000000);
|
||||
#else
|
||||
# ifdef MAC_OS
|
||||
mach_timespec_t t_cur;
|
||||
|
||||
Reference in New Issue
Block a user