2007-05-26 09:57:39 -07:00
|
|
|
/**
|
|
|
|
|
* =========================================================================
|
|
|
|
|
* File : qpc.cpp
|
|
|
|
|
* Project : 0 A.D.
|
|
|
|
|
* Description : Timer implementation using QueryPerformanceCounter
|
|
|
|
|
* =========================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// license: GPL; see lib/license.txt
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
#include "qpc.h"
|
|
|
|
|
|
2008-05-13 12:43:02 -07:00
|
|
|
#include "lib/sysdep/os_cpu.h"
|
2008-06-30 10:34:18 -07:00
|
|
|
#include "lib/sysdep/os/win/win.h"
|
|
|
|
|
#include "lib/sysdep/os/win/wutil.h" // wutil_argv
|
2007-05-26 09:57:39 -07:00
|
|
|
#include "pit.h" // PIT_FREQ
|
|
|
|
|
#include "pmt.h" // PMT_FREQ
|
|
|
|
|
|
|
|
|
|
|
2007-05-28 02:25:38 -07:00
|
|
|
LibError CounterQPC::Activate()
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
|
|
|
|
// note: QPC is observed to be universally supported, but the API
|
|
|
|
|
// provides for failure, so play it safe.
|
|
|
|
|
|
|
|
|
|
LARGE_INTEGER qpcFreq, qpcValue;
|
|
|
|
|
const BOOL ok1 = QueryPerformanceFrequency(&qpcFreq);
|
|
|
|
|
const BOOL ok2 = QueryPerformanceCounter(&qpcValue);
|
2007-05-28 02:25:38 -07:00
|
|
|
WARN_RETURN_IF_FALSE(ok1 && ok2);
|
|
|
|
|
if(!qpcFreq.QuadPart || !qpcValue.QuadPart)
|
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
2007-05-26 09:57:39 -07:00
|
|
|
|
|
|
|
|
m_frequency = (i64)qpcFreq.QuadPart;
|
2007-05-28 02:25:38 -07:00
|
|
|
return INFO::OK;
|
2007-05-26 09:57:39 -07:00
|
|
|
}
|
|
|
|
|
|
2007-05-28 02:25:38 -07:00
|
|
|
void CounterQPC::Shutdown()
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-28 02:25:38 -07:00
|
|
|
bool CounterQPC::IsSafe() const
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
|
|
|
|
// note: we have separate modules that directly access some of the
|
2007-05-29 09:28:34 -07:00
|
|
|
// counters potentially used by QPC. disabling the redundant counters
|
|
|
|
|
// would be ugly (increased coupling). instead, we'll make sure our
|
2007-05-30 17:11:38 -07:00
|
|
|
// implementations could (if necessary) coexist with QPC, but it
|
|
|
|
|
// shouldn't come to that since only one counter is needed/used.
|
2007-05-29 09:28:34 -07:00
|
|
|
|
2007-09-08 01:09:32 -07:00
|
|
|
// the PIT is entirely safe (even if annoyingly slow to read)
|
|
|
|
|
if(m_frequency == PIT_FREQ)
|
|
|
|
|
return true;
|
|
|
|
|
|
2007-05-29 09:28:34 -07:00
|
|
|
// the PMT is generally safe (see discussion in CounterPmt::IsSafe),
|
|
|
|
|
// but older QPC implementations had problems with 24-bit rollover.
|
|
|
|
|
// "System clock problem can inflate benchmark scores"
|
|
|
|
|
// (http://www.lionbridge.com/bi/cont2000/200012/perfcnt.asp ; no longer
|
|
|
|
|
// online, nor findable in Google Cache / archive.org) tells of
|
|
|
|
|
// incorrect values every 4.6 seconds (i.e. 24 bits @ 3.57 MHz) unless
|
|
|
|
|
// the timer is polled in the meantime. fortunately, this is guaranteed
|
|
|
|
|
// by our periodic updates (which come at least that often).
|
2007-09-08 01:09:32 -07:00
|
|
|
if(m_frequency == PMT_FREQ)
|
2007-05-26 09:57:39 -07:00
|
|
|
return true;
|
|
|
|
|
|
2007-09-08 01:09:32 -07:00
|
|
|
// the TSC has been known to be buggy (even mentioned in MSDN). it is
|
|
|
|
|
// used on MP HAL systems and can be detected by comparing QPF with the
|
|
|
|
|
// CPU clock. we consider it unsafe unless the user promises (via
|
|
|
|
|
// command line) that it's patched and thus reliable on their system.
|
2008-05-13 12:43:02 -07:00
|
|
|
bool usesTsc = IsSimilarMagnitude(m_frequency, os_cpu_ClockFrequency());
|
2007-05-26 09:57:39 -07:00
|
|
|
// unconfirmed reports indicate QPC sometimes uses 1/3 of the
|
|
|
|
|
// CPU clock frequency, so check that as well.
|
2008-05-13 12:43:02 -07:00
|
|
|
usesTsc |= IsSimilarMagnitude(m_frequency, os_cpu_ClockFrequency()/3);
|
2007-09-08 01:09:32 -07:00
|
|
|
if(usesTsc)
|
|
|
|
|
{
|
|
|
|
|
const bool isTscSafe = wutil_HasCommandLineArgument("-wQpcTscSafe");
|
|
|
|
|
return isTscSafe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the HPET is reliable and used on Vista. it can't easily be recognized
|
|
|
|
|
// since its frequency is variable (the spec says > 10 MHz; the master
|
|
|
|
|
// 14.318 MHz oscillator is often used). considering frequencies in
|
|
|
|
|
// [10, 100 MHz) to be a HPET would be dangerous because it may actually
|
|
|
|
|
// be faster or RDTSC slower. we have to exclude all other cases and
|
|
|
|
|
// assume it's a HPET - and thus safe - if we get here.
|
2007-05-26 09:57:39 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-28 02:25:38 -07:00
|
|
|
u64 CounterQPC::Counter() const
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
|
|
|
|
// fairly time-critical here, don't check the return value
|
|
|
|
|
// (IsSupported made sure it succeeded initially)
|
|
|
|
|
LARGE_INTEGER qpc_value;
|
|
|
|
|
(void)QueryPerformanceCounter(&qpc_value);
|
|
|
|
|
return qpc_value.QuadPart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WHRT uses this to ensure the counter (running at nominal frequency)
|
|
|
|
|
* doesn't overflow more than once during CALIBRATION_INTERVAL_MS.
|
|
|
|
|
**/
|
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 11:48:32 -07:00
|
|
|
size_t CounterQPC::CounterBits() const
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
2007-05-28 02:25:38 -07:00
|
|
|
// there are reports of incorrect rollover handling in the PMT
|
|
|
|
|
// implementation of QPC (see CounterPMT::IsSafe). however, other
|
|
|
|
|
// counters would be used on those systems, so it's irrelevant.
|
|
|
|
|
// we'll report the full 64 bits.
|
2007-05-26 09:57:39 -07:00
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* initial measurement of the tick rate. not necessarily correct
|
2008-05-13 12:43:02 -07:00
|
|
|
* (e.g. when using TSC: os_cpu_ClockFrequency isn't exact).
|
2007-05-26 09:57:39 -07:00
|
|
|
**/
|
2007-05-28 02:25:38 -07:00
|
|
|
double CounterQPC::NominalFrequency() const
|
2007-05-26 09:57:39 -07:00
|
|
|
{
|
|
|
|
|
return (double)m_frequency;
|
|
|
|
|
}
|