Bugfix: progress response time was long sometimes

Reason: the timer involved measured CPU time. So if the
process wasn't CPU intensive (i.e. waiting for network
response), cancelling the operation wasn't possible.
This commit is contained in:
Matthias Koefferlein 2018-04-30 23:25:23 +02:00
parent 2454104346
commit 90c5ff4b12
2 changed files with 48 additions and 19 deletions

View File

@ -32,7 +32,6 @@
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <time.h>
#ifndef _MSC_VER // not available on MS VC++ #ifndef _MSC_VER // not available on MS VC++
# include <unistd.h> # include <unistd.h>
@ -67,7 +66,7 @@ void
Timer::start () Timer::start ()
{ {
#ifdef _WIN32 #ifdef _WIN32
long clks_ms = (long) ((double) clock () * (1000.0 / CLOCKS_PER_SEC) + 0.5); timer_t clks_ms = (timer_t) ((double) clock () * (1000.0 / CLOCKS_PER_SEC) + 0.5);
m_user_ms += clks_ms; m_user_ms += clks_ms;
// no system time available .. // no system time available ..
#else #else
@ -75,8 +74,8 @@ Timer::start ()
times (&clks); times (&clks);
const double clk2msec = 1000.0 / sysconf (_SC_CLK_TCK); const double clk2msec = 1000.0 / sysconf (_SC_CLK_TCK);
m_user_ms += (long) ((clks.tms_utime + clks.tms_cutime) * clk2msec + 0.5); m_user_ms += (timer_t) ((clks.tms_utime + clks.tms_cutime) * clk2msec + 0.5);
m_sys_ms += (long) ((clks.tms_stime + clks.tms_cstime) * clk2msec + 0.5); m_sys_ms += (timer_t) ((clks.tms_stime + clks.tms_cstime) * clk2msec + 0.5);
#endif #endif
m_wall_ms += msecs_to (QDateTime::fromTime_t (0), QDateTime::currentDateTime ()); m_wall_ms += msecs_to (QDateTime::fromTime_t (0), QDateTime::currentDateTime ());
} }
@ -97,6 +96,27 @@ Timer::stop ()
m_wall_ms = 0; m_wall_ms = 0;
} }
void
Timer::take ()
{
timer_t user_ms = m_user_ms;
timer_t sys_ms = m_sys_ms;
timer_t wall_ms = m_wall_ms;
m_user_ms = -m_user_ms;
m_sys_ms = -m_sys_ms;
m_wall_ms = -m_wall_ms;
start ();
m_user_ms_res = m_user_ms;
m_sys_ms_res = m_sys_ms;
m_wall_ms_res = m_wall_ms;
m_user_ms = user_ms;
m_sys_ms = sys_ms;
m_wall_ms = wall_ms;
}
void void
SelfTimer::report () const SelfTimer::report () const
{ {
@ -168,20 +188,20 @@ SelfTimer::report () const
Clock::Clock (double s) Clock::Clock (double s)
{ {
m_clock = clock_value (s * double (CLOCKS_PER_SEC)); m_clock_ms = s * 1000.0;
} }
double double
Clock::seconds () const Clock::seconds () const
{ {
return double (m_clock) / double (CLOCKS_PER_SEC); return double (m_clock_ms) * 0.001;
} }
Clock Clock
Clock::current () Clock::current ()
{ {
Clock c; Clock c;
c.m_clock = clock (); c.m_clock_ms += msecs_to (QDateTime::fromTime_t (0), QDateTime::currentDateTime ());
return c; return c;
} }

View File

@ -26,7 +26,7 @@
#include "tlCommon.h" #include "tlCommon.h"
#include <string> #include <string>
#include <stdint.h> #include <stdint.h>
class QDateTime; class QDateTime;
@ -50,6 +50,8 @@ TL_PUBLIC int64_t msecs_to (const QDateTime &from, const QDateTime &to);
class TL_PUBLIC Timer class TL_PUBLIC Timer
{ {
public: public:
typedef int64_t timer_t;
Timer (); Timer ();
/** /**
@ -59,10 +61,16 @@ public:
/** /**
* @brief Stops the timer * @brief Stops the timer
* Only after stop or take, the time can be read with sec_user etc.
*/ */
void stop (); void stop ();
/** /**
* @brief Takes the time, but does not stop
*/
void take ();
/**
* @brief Reports the time spent between start() and stop() in user space * @brief Reports the time spent between start() and stop() in user space
*/ */
double sec_user () const double sec_user () const
@ -87,8 +95,8 @@ public:
} }
private: private:
long m_user_ms, m_sys_ms, m_wall_ms; timer_t m_user_ms, m_sys_ms, m_wall_ms;
long m_user_ms_res, m_sys_ms_res, m_wall_ms_res; timer_t m_user_ms_res, m_sys_ms_res, m_wall_ms_res;
}; };
/** /**
@ -152,12 +160,14 @@ private:
class TL_PUBLIC Clock class TL_PUBLIC Clock
{ {
public: public:
typedef int64_t timer_t;
typedef unsigned long clock_value; typedef unsigned long clock_value;
/** /**
* @brief Default constructor: construct a clock object pointing to an arbitrary value * @brief Default constructor: construct a clock object pointing to an arbitrary value
*/ */
Clock () : m_clock (0) Clock () : m_clock_ms (0)
{ {
// .. nothing yet .. // .. nothing yet ..
} }
@ -171,7 +181,7 @@ public:
* @brief Copy constructor * @brief Copy constructor
*/ */
Clock (const Clock &d) Clock (const Clock &d)
: m_clock (d.m_clock) : m_clock_ms (d.m_clock_ms)
{ {
// .. nothing yet .. // .. nothing yet ..
} }
@ -181,7 +191,7 @@ public:
*/ */
Clock &operator= (Clock d) Clock &operator= (Clock d)
{ {
m_clock = d.m_clock; m_clock_ms = d.m_clock_ms;
return *this; return *this;
} }
@ -190,7 +200,7 @@ public:
*/ */
bool operator== (Clock d) const bool operator== (Clock d) const
{ {
return m_clock == d.m_clock; return m_clock_ms == d.m_clock_ms;
} }
/** /**
@ -206,8 +216,7 @@ public:
*/ */
bool operator< (Clock d) const bool operator< (Clock d) const
{ {
// to account for wrap around, we do the comparison this way: return m_clock_ms < d.m_clock_ms;
return long (m_clock) - long (d.m_clock) < 0;
} }
/** /**
@ -215,7 +224,7 @@ public:
*/ */
Clock &operator-= (Clock d) Clock &operator-= (Clock d)
{ {
m_clock -= d.m_clock; m_clock_ms -= d.m_clock_ms;
return *this; return *this;
} }
@ -240,7 +249,7 @@ public:
static Clock current (); static Clock current ();
private: private:
clock_value m_clock; timer_t m_clock_ms;
}; };
} // namespace tl } // namespace tl