2 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "pbd/windows_timer_utils.h"
24 #include "pbd/compose.h"
25 #include "pbd/debug.h"
27 #define DEBUG_TIMING(msg) DEBUG_TRACE (PBD::DEBUG::Timing, msg);
34 static UINT timer_res_ms = 0;
45 get_min_resolution (uint32_t& min_resolution_ms)
49 if (timeGetDevCaps (&caps, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
50 DEBUG_TIMING ("Could not get timer device capabilities.\n");
54 min_resolution_ms = caps.wPeriodMin;
61 uint32_t min_resolution = 0;
63 if (!get_min_resolution (min_resolution)) {
67 if (!set_resolution (min_resolution)) {
74 set_resolution (uint32_t timer_resolution_ms)
76 if (timer_resolution() != 0) {
78 "Timer resolution must be reset before setting new resolution.\n");
81 if (timeBeginPeriod(timer_resolution_ms) != TIMERR_NOERROR) {
83 string_compose("Could not set timer resolution to %1(ms)\n",
84 timer_resolution_ms));
88 timer_resolution() = timer_resolution_ms;
90 DEBUG_TIMING (string_compose ("Multimedia timer resolution set to %1(ms)\n",
91 timer_resolution_ms));
98 // You must match calls to timeBegin/EndPeriod with the same resolution
99 if (timeEndPeriod(timer_resolution()) != TIMERR_NOERROR) {
100 DEBUG_TIMING("Could not reset the Timer resolution.\n");
103 timer_resolution() = 0;
107 } // namespace MMTIMERS
112 qpc_frequency_success ()
114 static bool success = false;
122 if (QueryPerformanceFrequency(&freq) == 0) {
123 DEBUG_TIMING ("Failed to determine frequency of QPC\n");
124 qpc_frequency_success() = false;
126 qpc_frequency_success() = true;
133 qpc_frequency_cached ()
135 static LARGE_INTEGER frequency = qpc_frequency ();
142 int64_t last_timer_val = PBD::QPC::get_microseconds ();
143 if (last_timer_val < 0) return false;
145 for (int i = 0; i < 100000; ++i) {
146 int64_t timer_val = PBD::QPC::get_microseconds ();
147 if (timer_val < 0) return false;
148 // try and test for non-syncronized TSC(AMD K8/etc)
149 if (timer_val < last_timer_val) return false;
150 last_timer_val = timer_val;
162 // setup caching the timer frequency
163 qpc_frequency_cached ();
164 if (!qpc_frequency_success ()) {
167 return test_qpc_validity ();
173 LARGE_INTEGER current_val;
175 if (qpc_frequency_success()) {
176 // MS docs say this will always succeed for systems >= XP but it may
177 // not return a monotonic value with non-invariant TSC's etc
178 if (QueryPerformanceCounter(¤t_val) != 0) {
179 return (int64_t)(((double)current_val.QuadPart) /
180 ((double)qpc_frequency_cached().QuadPart) * 1000000.0);
183 DEBUG_TIMING ("Could not get QPC timer\n");
192 qpc_frequency_cached();
193 if (qpc_frequency_success()) {
194 return QPC::get_microseconds ();
196 // For XP systems that don't support a high-res performance counter
197 return g_get_monotonic_time ();