File: qpc.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (157 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Copyright (C) 2010 Wildfire Games.
 *
 * 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.
 */

/*
 * Timer implementation using QueryPerformanceCounter
 */

#include "precompiled.h"
#include "lib/sysdep/os/win/whrt/qpc.h"

#include "lib/sysdep/os/win/whrt/counter.h"

#include "lib/sysdep/os_cpu.h"
#include "lib/sysdep/os/win/win.h"
#include "lib/sysdep/os/win/wutil.h"	// wutil_argv
#include "lib/sysdep/os/win/whrt/pit.h"	// PIT_FREQ
#include "lib/sysdep/os/win/whrt/pmt.h"	// PMT_FREQ


class CounterQPC : public ICounter
{
public:
	CounterQPC()
		: m_frequency(-1)
	{
	}

	virtual const char* Name() const
	{
		return "QPC";
	}

	Status Activate()
	{
		// 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);
		if(!ok1 || !ok2)
			WARN_RETURN(ERR::FAIL);
		if(!qpcFreq.QuadPart || !qpcValue.QuadPart)
			WARN_RETURN(ERR::FAIL);

		m_frequency = (i64)qpcFreq.QuadPart;
		return INFO::OK;
	}

	void Shutdown()
	{
	}

	bool IsSafe() const
	{
		// note: we have separate modules that directly access some of the
		// counters potentially used by QPC. disabling the redundant counters
		// would be ugly (increased coupling). instead, we'll make sure our
		// implementations could (if necessary) coexist with QPC, but it
		// shouldn't come to that since only one counter is needed/used.

		// the PIT is entirely safe (even if annoyingly slow to read)
		if(m_frequency == PIT_FREQ)
			return true;

		// 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).
		if(m_frequency == PMT_FREQ)
			return true;

		// 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.
		bool usesTsc = IsSimilarMagnitude((double)m_frequency, os_cpu_ClockFrequency());
		// unconfirmed reports indicate QPC sometimes uses 1/3 of the
		// CPU clock frequency, so check that as well.
		usesTsc |= IsSimilarMagnitude((double)m_frequency, os_cpu_ClockFrequency()/3);
		if(usesTsc)
		{
			const bool isTscSafe = wutil_HasCommandLineArgument(L"-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.
		return true;
	}

	u64 Counter() const
	{
		// 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;
	}

	size_t CounterBits() const
	{
		// 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.
		return 64;
	}

	double NominalFrequency() const
	{
		return (double)m_frequency;
	}

	double Resolution() const
	{
		return 1.0 / m_frequency;
	}

private:
	// used in several places and QPF is a bit slow+cumbersome.
	// (i64 allows easier conversion to double)
	i64 m_frequency;
};

ICounter* CreateCounterQPC(void* address, size_t size)
{
	ENSURE(sizeof(CounterQPC) <= size);
	return new(address) CounterQPC();
}