2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* platform-independent high resolution timer
|
2006-04-12 01:59:08 +02:00
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_TIMER
|
|
|
|
#define INCLUDED_TIMER
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2008-12-16 22:13:04 +01:00
|
|
|
#include "lib/config2.h" // CONFIG2_TIMER_ALLOW_RDTSC
|
|
|
|
#if ARCH_X86_X64 && CONFIG2_TIMER_ALLOW_RDTSC
|
|
|
|
# include "lib/sysdep/arch/x86_x64/x86_x64.h" // x86_x64_rdtsc
|
|
|
|
# include "lib/sysdep/os_cpu.h" // os_cpu_ClockFrequency
|
|
|
|
#endif
|
|
|
|
|
2009-01-02 22:19:41 +01:00
|
|
|
#include <sstream> // std::stringstream
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* timer_Time will subsequently return values relative to the current time.
|
|
|
|
**/
|
|
|
|
LIB_API void timer_LatchStartTime();
|
2007-05-26 19:56:38 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* @return high resolution (> 1 us) timestamp [s].
|
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API double timer_Time();
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* @return resolution [s] of the timer.
|
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API double timer_Resolution();
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-10-10 19:33:34 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-01-07 21:03:19 +01:00
|
|
|
// scope timing
|
2007-05-02 14:07:08 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/// used by TIMER
|
2008-12-17 17:32:46 +01:00
|
|
|
class ScopeTimer
|
2007-05-02 14:07:08 +02:00
|
|
|
{
|
2008-12-17 17:32:46 +01:00
|
|
|
NONCOPYABLE(ScopeTimer);
|
2007-05-02 14:07:08 +02:00
|
|
|
public:
|
2009-11-03 22:46:35 +01:00
|
|
|
ScopeTimer(const wchar_t* description)
|
2008-12-16 22:13:04 +01:00
|
|
|
: m_t0(timer_Time()), m_description(description)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopeTimer()
|
|
|
|
{
|
|
|
|
double t1 = timer_Time();
|
|
|
|
double dt = t1-m_t0;
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1e6;
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* unit = L"us";
|
2008-12-16 22:13:04 +01:00
|
|
|
if(dt > 1.0)
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1, unit = L"s";
|
2008-12-16 22:13:04 +01:00
|
|
|
else if(dt > 1e-3)
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1e3, unit = L"ms";
|
2008-12-16 22:13:04 +01:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"TIMER| %ls: %g %ls\n", m_description, dt*scale, unit);
|
2008-12-16 22:13:04 +01:00
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
private:
|
|
|
|
double m_t0;
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* m_description;
|
2007-05-02 14:07:08 +02:00
|
|
|
};
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* Measures the time taken to execute code up until end of the current scope;
|
|
|
|
* displays it via debug_printf. Can safely be nested.
|
|
|
|
* Useful for measuring time spent in a function or basic block.
|
|
|
|
* <description> must remain valid over the lifetime of this object;
|
|
|
|
* a string literal is safest.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* void func()
|
|
|
|
* {
|
2009-11-03 22:46:35 +01:00
|
|
|
* TIMER(L"description");
|
2008-01-07 21:03:19 +01:00
|
|
|
* // code to be measured
|
|
|
|
* }
|
|
|
|
**/
|
|
|
|
#define TIMER(description) ScopeTimer UID__(description)
|
2007-05-02 14:07:08 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* Measures the time taken to execute code between BEGIN and END markers;
|
|
|
|
* displays it via debug_printf. Can safely be nested.
|
|
|
|
* Useful for measuring several pieces of code within the same function/block.
|
|
|
|
* <description> must remain valid over the lifetime of this object;
|
|
|
|
* a string literal is safest.
|
|
|
|
*
|
|
|
|
* Caveats:
|
|
|
|
* - this wraps the code to be measured in a basic block, so any
|
|
|
|
* variables defined there are invisible to surrounding code.
|
|
|
|
* - the description passed to END isn't inspected; you are responsible for
|
|
|
|
* ensuring correct nesting!
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* void func2()
|
|
|
|
* {
|
|
|
|
* // uninteresting code
|
2009-11-03 22:46:35 +01:00
|
|
|
* TIMER_BEGIN(L"description2");
|
2008-01-07 21:03:19 +01:00
|
|
|
* // code to be measured
|
2009-11-03 22:46:35 +01:00
|
|
|
* TIMER_END(L"description2");
|
2008-01-07 21:03:19 +01:00
|
|
|
* // uninteresting code
|
|
|
|
* }
|
|
|
|
**/
|
|
|
|
#define TIMER_BEGIN(description) { ScopeTimer UID__(description)
|
|
|
|
#define TIMER_END(description) }
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// cumulative timer API
|
|
|
|
|
|
|
|
// this supplements in-game profiling by providing low-overhead,
|
2008-01-07 21:03:19 +01:00
|
|
|
// high resolution time accounting of specific areas.
|
|
|
|
|
2008-12-16 22:13:04 +01:00
|
|
|
// 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.
|
|
|
|
// if this is a problem, disable CONFIG2_TIMER_ALLOW_RDTSC.
|
|
|
|
//
|
|
|
|
// note that overflow isn't an issue either way (63 bit cycle counts
|
|
|
|
// at 10 GHz cover intervals of 29 years).
|
|
|
|
|
|
|
|
#if ARCH_X86_X64 && CONFIG2_TIMER_ALLOW_RDTSC
|
|
|
|
|
|
|
|
class TimerUnit
|
2008-01-07 21:03:19 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-12-16 22:13:04 +01:00
|
|
|
void SetToZero()
|
|
|
|
{
|
|
|
|
m_ticks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFromTimer()
|
|
|
|
{
|
|
|
|
m_ticks = x86_x64_rdtsc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddDifference(TimerUnit t0, TimerUnit t1)
|
|
|
|
{
|
|
|
|
m_ticks += t1.m_ticks - t0.m_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Subtract(TimerUnit t)
|
|
|
|
{
|
|
|
|
m_ticks -= t.m_ticks;
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
std::wstring ToString() const
|
2008-12-16 22:13:04 +01:00
|
|
|
{
|
|
|
|
debug_assert(m_ticks >= 0.0);
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1.0;
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* unit = L" c";
|
2008-12-16 22:13:04 +01:00
|
|
|
if(m_ticks > 10000000000LL) // 10 Gc
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1e-9, unit = L" Gc";
|
2008-12-16 22:13:04 +01:00
|
|
|
else if(m_ticks > 10000000) // 10 Mc
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1e-6, unit = L" Mc";
|
2008-12-16 22:13:04 +01:00
|
|
|
else if(m_ticks > 10000) // 10 kc
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1e-3, unit = L" kc";
|
2008-12-16 22:13:04 +01:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
std::wstringstream ss;
|
2008-12-16 22:13:04 +01:00
|
|
|
ss << m_ticks*scale;
|
|
|
|
ss << unit;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
double ToSeconds() const
|
|
|
|
{
|
|
|
|
return m_ticks / os_cpu_ClockFrequency();
|
|
|
|
}
|
2008-01-07 21:03:19 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
u64 m_ticks;
|
2008-12-16 22:13:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class TimerUnit
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void SetToZero()
|
|
|
|
{
|
|
|
|
m_seconds = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFromTimer()
|
|
|
|
{
|
|
|
|
m_seconds = timer_Time();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddDifference(TimerUnit t0, TimerUnit t1)
|
|
|
|
{
|
|
|
|
m_seconds += t1.m_seconds - t0.m_seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Subtract(TimerUnit t)
|
|
|
|
{
|
|
|
|
m_seconds -= t.m_seconds;
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
std::wstring ToString() const
|
2008-12-16 22:13:04 +01:00
|
|
|
{
|
|
|
|
debug_assert(m_seconds >= 0.0);
|
|
|
|
|
|
|
|
// determine scale factor for pretty display
|
|
|
|
double scale = 1e6;
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* unit = L" us";
|
2008-12-16 22:13:04 +01:00
|
|
|
if(m_seconds > 1.0)
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1, unit = L" s";
|
2008-12-16 22:13:04 +01:00
|
|
|
else if(m_seconds > 1e-3)
|
2009-11-03 22:46:35 +01:00
|
|
|
scale = 1e3, unit = L" ms";
|
2008-12-16 22:13:04 +01:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
std::wstringstream ss;
|
2008-12-16 22:13:04 +01:00
|
|
|
ss << m_seconds*scale;
|
|
|
|
ss << unit;
|
2008-12-17 17:32:46 +01:00
|
|
|
return ss.str();
|
|
|
|
}
|
2008-12-16 22:13:04 +01:00
|
|
|
|
|
|
|
double ToSeconds() const
|
|
|
|
{
|
|
|
|
return m_seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2008-01-07 21:03:19 +01:00
|
|
|
double m_seconds;
|
|
|
|
};
|
2005-12-14 00:17:50 +01:00
|
|
|
|
2008-12-16 22:13:04 +01:00
|
|
|
#endif
|
|
|
|
|
2005-10-18 01:35:16 +02:00
|
|
|
// opaque - do not access its fields!
|
2005-10-17 01:16:08 +02:00
|
|
|
// note: must be defined here because clients instantiate them;
|
2007-05-28 11:20:55 +02:00
|
|
|
// fields cannot be made private due to POD requirement.
|
2005-10-17 01:16:08 +02:00
|
|
|
struct TimerClient
|
|
|
|
{
|
2005-12-14 00:17:50 +01:00
|
|
|
TimerUnit sum; // total bill
|
2005-10-17 01:16:08 +02:00
|
|
|
|
|
|
|
// only store a pointer for efficiency.
|
2009-11-03 22:46:35 +01:00
|
|
|
const wchar_t* description;
|
2005-10-17 01:16:08 +02:00
|
|
|
|
|
|
|
TimerClient* next;
|
2005-10-19 22:26:53 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
// how often timer_BillClient was called (helps measure relative
|
2005-10-19 22:26:53 +02:00
|
|
|
// performance of something that is done indeterminately often).
|
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
|
|
|
size_t num_calls;
|
2005-10-17 01:16:08 +02:00
|
|
|
};
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* make the given TimerClient (usually instantiated as static data)
|
|
|
|
* ready for use. returns its address for TIMER_ADD_CLIENT's convenience.
|
|
|
|
* this client's total (added to by timer_BillClient) will be
|
|
|
|
* displayed by timer_DisplayClientTotals.
|
|
|
|
* notes:
|
|
|
|
* - may be called at any time;
|
|
|
|
* - always succeeds (there's no fixed limit);
|
|
|
|
* - free() is not needed nor possible.
|
|
|
|
* - description must remain valid until exit; a string literal is safest.
|
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API TimerClient* timer_AddClient(TimerClient* tc, const wchar_t* description);
|
2005-10-10 19:33:34 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* "allocate" a new TimerClient that will keep track of the total time
|
|
|
|
* billed to it, along with a description string. These are displayed when
|
|
|
|
* timer_DisplayClientTotals is called.
|
|
|
|
* Invoke this at file or function scope; a (static) TimerClient pointer of
|
|
|
|
* name <id> will be defined, which should be passed to TIMER_ACCRUE.
|
|
|
|
**/
|
|
|
|
#define TIMER_ADD_CLIENT(id)\
|
|
|
|
static TimerClient UID__;\
|
2010-02-02 14:57:52 +01:00
|
|
|
static TimerClient* id = timer_AddClient(&UID__, WIDEN(#id))
|
2005-10-10 19:33:34 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* bill the difference between t0 and t1 to the client's total.
|
|
|
|
**/
|
|
|
|
LIB_API 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
|
|
|
/**
|
|
|
|
* display all clients' totals; does not reset them.
|
|
|
|
* typically called at exit.
|
|
|
|
**/
|
|
|
|
LIB_API void timer_DisplayClientTotals();
|
2005-10-18 01:35:16 +02:00
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/// used by TIMER_ACCRUE
|
2008-12-16 22:13:04 +01:00
|
|
|
class ScopeTimerAccrue
|
2005-10-10 19:33:34 +02:00
|
|
|
{
|
2008-12-17 17:32:46 +01:00
|
|
|
NONCOPYABLE(ScopeTimerAccrue);
|
2005-10-10 19:33:34 +02:00
|
|
|
public:
|
2008-12-16 22:13:04 +01:00
|
|
|
ScopeTimerAccrue(TimerClient* tc)
|
|
|
|
: m_tc(tc)
|
|
|
|
{
|
|
|
|
m_t0.SetFromTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopeTimerAccrue()
|
|
|
|
{
|
|
|
|
TimerUnit t1;
|
|
|
|
t1.SetFromTimer();
|
|
|
|
timer_BillClient(m_tc, m_t0, t1);
|
|
|
|
}
|
2005-10-10 19:33:34 +02:00
|
|
|
|
|
|
|
private:
|
2008-01-07 21:03:19 +01:00
|
|
|
TimerUnit m_t0;
|
|
|
|
TimerClient* m_tc;
|
2005-10-10 19:33:34 +02:00
|
|
|
};
|
|
|
|
|
2008-01-07 21:03:19 +01:00
|
|
|
/**
|
|
|
|
* Measure the time taken to execute code up until end of the current scope;
|
|
|
|
* bill it to the given TimerClient object. Can safely be nested.
|
|
|
|
* Useful for measuring total time spent in a function or basic block over the
|
|
|
|
* entire program.
|
|
|
|
* <description> must remain valid over the lifetime of this object;
|
|
|
|
* a string literal is safest.
|
|
|
|
*
|
|
|
|
* Example usage:
|
2010-02-02 14:57:52 +01:00
|
|
|
* TIMER_ADD_CLIENT(identifier);
|
2008-01-07 21:03:19 +01:00
|
|
|
*
|
|
|
|
* void func()
|
|
|
|
* {
|
|
|
|
* TIMER_ACCRUE(name_of_pointer_to_client);
|
|
|
|
* // code to be measured
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* [at exit]
|
|
|
|
* timer_DisplayClientTotals();
|
|
|
|
**/
|
|
|
|
#define TIMER_ACCRUE(client) ScopeTimerAccrue UID__(client)
|
2005-10-10 19:33:34 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#endif // #ifndef INCLUDED_TIMER
|