2006-04-12 01:59:08 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : timer.cpp
|
|
|
|
* Project : 0 A.D.
|
2008-01-07 21:03:19 +01:00
|
|
|
* Description : platform-independent high resolution timer
|
2006-04-12 01:59:08 +02:00
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
// license: GPL; see lib/license.txt
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "timer.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2006-06-22 20:26:08 +02:00
|
|
|
#include <numeric>
|
|
|
|
#include <math.h>
|
2007-09-03 01:38:58 +02:00
|
|
|
#include <float.h>
|
2006-06-22 20:26:08 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "lib/posix/posix_time.h"
|
2008-08-09 21:22:13 +02:00
|
|
|
#include "lib/sysdep/os_cpu.h"
|
2007-05-26 18:57:39 +02:00
|
|
|
#if OS_WIN
|
2008-06-30 19:34:18 +02:00
|
|
|
#include "lib/sysdep/os/win/whrt/whrt.h"
|
2007-05-26 18:57:39 +02:00
|
|
|
#endif
|
2007-12-20 21:09:19 +01:00
|
|
|
#if OS_UNIX
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2008-01-19 12:33:11 +01:00
|
|
|
#include "lib/config2.h" // CONFIG2_TIMER_ALLOW_RDTSC
|
2008-05-12 20:15:08 +02:00
|
|
|
#if (ARCH_IA32 || ARCH_AMD64) && CONFIG2_TIMER_ALLOW_RDTSC
|
2008-06-30 19:34:18 +02:00
|
|
|
# include "lib/sysdep/arch/x86_x64/x86_x64.h" // x86_x64_rdtsc
|
2007-04-30 16:35:19 +02:00
|
|
|
#endif
|
|
|
|
|
2007-12-20 21:09:19 +01:00
|
|
|
#if OS_UNIX || OS_WIN
|
|
|
|
# define HAVE_GETTIMEOFDAY 1
|
|
|
|
#else
|
|
|
|
# define HAVE_GETTIMEOFDAY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) || OS_WIN
|
|
|
|
# define HAVE_CLOCK_GETTIME 1
|
|
|
|
#else
|
|
|
|
# define HAVE_CLOCK_GETTIME 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// rationale for wrapping gettimeofday and clock_gettime, instead of just
|
|
|
|
// emulating them where not available: allows returning higher-resolution
|
|
|
|
// timer values than their us / ns interface, via double [seconds].
|
|
|
|
// they're also not guaranteed to be monotonic.
|
|
|
|
|
|
|
|
#if HAVE_GETTIMEOFDAY
|
2007-05-26 18:57:39 +02:00
|
|
|
static struct timespec start;
|
2007-12-20 21:09:19 +01:00
|
|
|
#elif HAVE_CLOCK_GETTIME
|
2007-05-26 18:57:39 +02:00
|
|
|
static struct timeval start;
|
|
|
|
#endif
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// timer API
|
|
|
|
|
|
|
|
void timer_LatchStartTime()
|
2007-05-26 18:57:39 +02:00
|
|
|
{
|
|
|
|
#if HAVE_CLOCK_GETTIME
|
|
|
|
(void)clock_gettime(CLOCK_REALTIME, &start);
|
|
|
|
#elif HAVE_GETTIMEOFDAY
|
|
|
|
gettimeofday(&start, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
|
|
|
|
double timer_Time()
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
|
|
|
double t;
|
|
|
|
|
2007-06-13 01:29:27 +02:00
|
|
|
#if OS_WIN
|
|
|
|
t = whrt_Time();
|
|
|
|
#elif HAVE_CLOCK_GETTIME
|
2007-05-26 18:57:39 +02:00
|
|
|
struct timespec cur;
|
|
|
|
(void)clock_gettime(CLOCK_REALTIME, &cur);
|
|
|
|
t = (cur.tv_sec - start.tv_sec) + (cur.tv_nsec - start.tv_nsec)*1e-9;
|
2005-08-09 18:23:19 +02:00
|
|
|
#elif HAVE_GETTIMEOFDAY
|
2004-03-03 00:56:51 +01:00
|
|
|
struct timeval cur;
|
|
|
|
gettimeofday(&cur, 0);
|
2004-06-02 17:31:55 +02:00
|
|
|
t = (cur.tv_sec - start.tv_sec) + (cur.tv_usec - start.tv_usec)*1e-6;
|
2004-03-03 00:56:51 +01:00
|
|
|
#else
|
2008-01-07 21:03:19 +01:00
|
|
|
# error "timer_Time: add timer implementation for this platform!"
|
2004-03-03 00:56:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// make sure time is monotonic (never goes backwards)
|
2005-01-23 18:48:32 +01:00
|
|
|
static double t_last = 0.0;
|
2004-06-22 18:05:44 +02:00
|
|
|
if(t < t_last)
|
2007-05-16 17:56:47 +02:00
|
|
|
t = t_last+DBL_EPSILON;
|
2004-06-22 18:05:44 +02:00
|
|
|
t_last = t;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
double timer_Resolution()
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-24 02:41:29 +02:00
|
|
|
// may take a while to determine, so cache it
|
2005-01-23 18:48:32 +01:00
|
|
|
static double cached_res = 0.0;
|
2004-06-24 02:41:29 +02:00
|
|
|
if(cached_res != 0.0)
|
|
|
|
return cached_res;
|
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
double res = 0.0;
|
2004-06-24 02:41:29 +02:00
|
|
|
|
2005-08-09 18:23:19 +02:00
|
|
|
#if HAVE_CLOCK_GETTIME
|
2004-06-24 02:41:29 +02:00
|
|
|
struct timespec ts;
|
2005-01-23 18:48:32 +01:00
|
|
|
if(clock_getres(CLOCK_REALTIME, &ts) == 0)
|
|
|
|
res = ts.tv_nsec * 1e-9;
|
2007-05-26 18:57:39 +02:00
|
|
|
#elif OS_WIN
|
|
|
|
res = whrt_Resolution();
|
2004-03-03 00:56:51 +01:00
|
|
|
#else
|
2008-01-07 21:03:19 +01:00
|
|
|
const double t0 = timer_Time();
|
2004-06-24 02:41:29 +02:00
|
|
|
double t1, t2;
|
2008-01-07 21:03:19 +01:00
|
|
|
do t1 = timer_Time(); while(t1 == t0);
|
|
|
|
do t2 = timer_Time(); while(t2 == t1);
|
2004-06-24 02:41:29 +02:00
|
|
|
res = t2-t1;
|
2004-03-03 00:56:51 +01:00
|
|
|
#endif
|
2004-06-24 02:41:29 +02:00
|
|
|
|
|
|
|
cached_res = res;
|
|
|
|
return res;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-10 19:33:34 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
ScopeTimer::ScopeTimer(const char* description)
|
|
|
|
: m_t0(timer_Time()), m_description(description)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ScopeTimer::~ScopeTimer()
|
|
|
|
{
|
|
|
|
double t1 = timer_Time();
|
|
|
|
double dt = t1-m_t0;
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1e6;
|
|
|
|
const char* unit = "us";
|
|
|
|
if(dt > 1.0)
|
|
|
|
scale = 1, unit = "s";
|
|
|
|
else if(dt > 1e-3)
|
|
|
|
scale = 1e3, unit = "ms";
|
|
|
|
|
|
|
|
debug_printf("TIMER| %s: %g %s\n", m_description, dt*scale, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// TimerUnit
|
|
|
|
|
|
|
|
// since TIMER_ACCRUE et al. are called so often, we try to keep
|
|
|
|
// overhead to an absolute minimum. storing raw tick counts (e.g. CPU cycles
|
|
|
|
// returned by ia32_rdtsc) instead of absolute time has two benefits:
|
|
|
|
// - no need to convert from raw->time on every call
|
|
|
|
// (instead, it's only done once when displaying the totals)
|
|
|
|
// - possibly less overhead to querying the time itself
|
|
|
|
// (timer_Time may be using slower time sources with ~3us overhead)
|
|
|
|
//
|
|
|
|
// however, the cycle count is not necessarily a measure of wall-clock time
|
|
|
|
// (see http://www.gamedev.net/reference/programming/features/timing).
|
|
|
|
// therefore, on systems with SpeedStep active, measurements of I/O or other
|
|
|
|
// non-CPU bound activity may be skewed. this is ok because the timer is
|
|
|
|
// only used for profiling; just be aware of the issue.
|
2008-01-19 12:33:11 +01:00
|
|
|
// if this is a problem, disable CONFIG2_TIMER_ALLOW_RDTSC.
|
2008-01-07 21:03:19 +01:00
|
|
|
//
|
|
|
|
// note that overflow isn't an issue either way (63 bit cycle counts
|
|
|
|
// at 10 GHz cover intervals of 29 years).
|
|
|
|
|
2008-01-19 12:33:11 +01:00
|
|
|
#if ARCH_IA32 && CONFIG2_TIMER_ALLOW_RDTSC
|
2008-01-07 21:03:19 +01:00
|
|
|
|
|
|
|
void TimerUnit::SetToZero()
|
|
|
|
{
|
|
|
|
m_ticks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::SetFromTimer()
|
|
|
|
{
|
2008-05-12 20:15:08 +02:00
|
|
|
m_ticks = x86_x64_rdtsc();
|
2008-01-07 21:03:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::AddDifference(TimerUnit t0, TimerUnit t1)
|
|
|
|
{
|
|
|
|
m_ticks += t1.m_ticks - t0.m_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::Subtract(TimerUnit t)
|
|
|
|
{
|
|
|
|
m_ticks -= t.m_ticks;
|
|
|
|
}
|
|
|
|
|
2008-01-13 12:25:19 +01:00
|
|
|
std::string TimerUnit::ToString() const
|
2008-01-07 21:03:19 +01:00
|
|
|
{
|
2008-01-13 12:25:19 +01:00
|
|
|
debug_assert(m_ticks >= 0.0);
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1.0;
|
|
|
|
const char* unit = " c";
|
2008-01-26 05:43:02 +01:00
|
|
|
if(m_ticks > 10000000000LL) // 10 Gc
|
2008-01-13 12:25:19 +01:00
|
|
|
scale = 1e-9, unit = " Gc";
|
|
|
|
else if(m_ticks > 10000000) // 10 Mc
|
|
|
|
scale = 1e-6, unit = " Mc";
|
|
|
|
else if(m_ticks > 10000) // 10 kc
|
|
|
|
scale = 1e-3, unit = " kc";
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << m_ticks*scale;
|
|
|
|
ss << unit;
|
|
|
|
return ss.str();
|
2008-01-07 21:03:19 +01:00
|
|
|
}
|
|
|
|
|
2008-08-09 21:22:13 +02:00
|
|
|
double TimerUnit::ToSeconds() const
|
|
|
|
{
|
|
|
|
return m_ticks / os_cpu_ClockFrequency();
|
|
|
|
}
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
void TimerUnit::SetToZero()
|
|
|
|
{
|
|
|
|
m_seconds = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::SetFromTimer()
|
|
|
|
{
|
|
|
|
m_seconds = timer_Time();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::AddDifference(TimerUnit t0, TimerUnit t1)
|
|
|
|
{
|
|
|
|
m_seconds += t1.m_seconds - t0.m_seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerUnit::Subtract(TimerUnit t)
|
|
|
|
{
|
|
|
|
m_seconds -= t.m_seconds;
|
|
|
|
}
|
|
|
|
|
2008-01-13 12:25:19 +01:00
|
|
|
std::string TimerUnit::ToString() const
|
2008-01-07 21:03:19 +01:00
|
|
|
{
|
2008-01-13 12:25:19 +01:00
|
|
|
debug_assert(m_seconds >= 0.0);
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1e6;
|
|
|
|
const char* unit = " us";
|
|
|
|
if(m_seconds > 1.0)
|
|
|
|
scale = 1, unit = " s";
|
|
|
|
else if(m_seconds > 1e-3)
|
|
|
|
scale = 1e3, unit = " ms";
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << m_seconds*scale;
|
|
|
|
ss << unit;
|
|
|
|
return ss.str();
|
2008-01-07 21:03:19 +01:00
|
|
|
}
|
|
|
|
|
2008-08-09 21:22:13 +02:00
|
|
|
double TimerUnit::ToSeconds() const
|
|
|
|
{
|
|
|
|
return m_seconds;
|
|
|
|
}
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// client API
|
2005-10-10 19:33:34 +02:00
|
|
|
|
2005-10-18 01:35:16 +02:00
|
|
|
// intrusive linked-list of all clients. a fixed-size limit would be
|
|
|
|
// acceptable (since timers are added manually), but the list is easy
|
|
|
|
// to implement and only has the drawback of exposing TimerClient to users.
|
|
|
|
//
|
|
|
|
// do not use std::list et al. for this! we must be callable at any time,
|
|
|
|
// especially before NLSO ctors run or before heap init.
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static size_t num_clients;
|
2005-10-17 01:16:08 +02:00
|
|
|
static TimerClient* clients;
|
2005-10-10 19:33:34 +02:00
|
|
|
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
TimerClient* timer_AddClient(TimerClient* tc, const char* description)
|
2005-10-10 19:33:34 +02:00
|
|
|
{
|
2008-01-07 21:03:19 +01:00
|
|
|
tc->sum.SetToZero();
|
|
|
|
|
2005-10-17 01:16:08 +02:00
|
|
|
tc->description = description;
|
|
|
|
|
|
|
|
// insert at front of list
|
|
|
|
tc->next = clients;
|
|
|
|
clients = tc;
|
|
|
|
num_clients++;
|
|
|
|
|
2005-10-10 19:33:34 +02:00
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
void timer_BillClient(TimerClient* tc, TimerUnit t0, TimerUnit t1)
|
2005-10-10 19:33:34 +02:00
|
|
|
{
|
2008-01-07 21:03:19 +01:00
|
|
|
tc->sum.AddDifference(t0, t1);
|
2005-10-19 22:26:53 +02:00
|
|
|
tc->num_calls++;
|
2005-10-10 19:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
void timer_DisplayClientTotals()
|
2005-10-10 19:33:34 +02:00
|
|
|
{
|
|
|
|
debug_printf("TIMER TOTALS (%d clients)\n", num_clients);
|
|
|
|
debug_printf("-----------------------------------------------------\n");
|
2005-10-17 01:16:08 +02:00
|
|
|
|
|
|
|
while(clients)
|
2005-10-10 19:33:34 +02:00
|
|
|
{
|
2005-10-17 01:16:08 +02:00
|
|
|
// (make sure list and count are consistent)
|
|
|
|
debug_assert(num_clients != 0);
|
|
|
|
TimerClient* tc = clients;
|
|
|
|
clients = tc->next;
|
|
|
|
num_clients--;
|
|
|
|
|
2008-01-13 12:25:19 +01:00
|
|
|
const std::string duration = tc->sum.ToString();
|
|
|
|
debug_printf(" %s: %s (%dx)\n", tc->description, duration.c_str(), tc->num_calls);
|
2005-10-10 19:33:34 +02:00
|
|
|
}
|
2005-10-17 01:16:08 +02:00
|
|
|
|
2005-10-10 19:33:34 +02:00
|
|
|
debug_printf("-----------------------------------------------------\n");
|
|
|
|
}
|
2007-04-30 16:35:19 +02:00
|
|
|
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
ScopeTimerAccrue::ScopeTimerAccrue(TimerClient* tc)
|
|
|
|
: m_tc(tc)
|
2007-04-30 16:35:19 +02:00
|
|
|
{
|
2008-01-07 21:03:19 +01:00
|
|
|
m_t0.SetFromTimer();
|
2007-04-30 16:35:19 +02:00
|
|
|
}
|
|
|
|
|
2007-05-26 19:56:38 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
ScopeTimerAccrue::~ScopeTimerAccrue()
|
2007-05-26 19:56:38 +02:00
|
|
|
{
|
2008-01-07 21:03:19 +01:00
|
|
|
TimerUnit t1;
|
|
|
|
t1.SetFromTimer();
|
|
|
|
timer_BillClient(m_tc, m_t0, t1);
|
2007-05-26 19:56:38 +02:00
|
|
|
}
|