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"
26 #include "pbd/error.h"
30 #define DEBUG_TIMING(msg) DEBUG_TRACE (PBD::DEBUG::Timing, msg);
38 static UINT timer_res_ms = 0;
49 get_min_resolution (uint32_t& min_resolution_ms)
53 if (timeGetDevCaps (&caps, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
54 DEBUG_TIMING ("Could not get timer device capabilities.\n");
58 min_resolution_ms = caps.wPeriodMin;
65 uint32_t min_resolution = 0;
67 if (!get_min_resolution (min_resolution)) {
71 if (!set_resolution (min_resolution)) {
78 set_resolution (uint32_t timer_resolution_ms)
80 if (timer_resolution() != 0) {
82 "Timer resolution must be reset before setting new resolution.\n");
85 if (timeBeginPeriod(timer_resolution_ms) != TIMERR_NOERROR) {
87 string_compose("Could not set timer resolution to %1(ms)\n",
88 timer_resolution_ms));
92 timer_resolution() = timer_resolution_ms;
94 DEBUG_TIMING (string_compose ("Multimedia timer resolution set to %1(ms)\n",
95 timer_resolution_ms));
102 // You must match calls to timeBegin/EndPeriod with the same resolution
103 if (timeEndPeriod(timer_resolution()) != TIMERR_NOERROR) {
104 DEBUG_TIMING("Could not reset the Timer resolution.\n");
107 DEBUG_TIMING("Reset the Timer resolution.\n");
108 timer_resolution() = 0;
112 } // namespace MMTIMERS
116 static double timer_rate_us = 0.0;
122 int64_t last_timer_val = PBD::QPC::get_microseconds ();
123 if (last_timer_val < 0) return false;
125 for (int i = 0; i < 100000; ++i) {
126 int64_t timer_val = PBD::QPC::get_microseconds ();
127 if (timer_val < 0) return false;
128 // try and test for non-syncronized TSC(AMD K8/etc)
129 if (timer_val < last_timer_val) return false;
130 last_timer_val = timer_val;
142 if (!timer_rate_us) {
145 return test_qpc_validity ();
152 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart < 1) {
153 info << X_("Failed to determine frequency of QPC\n") << endmsg;
156 timer_rate_us = 1000000.0 / freq.QuadPart;
158 info << string_compose(X_("QPC timer microseconds per tick: %1\n"),
159 timer_rate_us) << endmsg;
160 return !timer_rate_us;
166 LARGE_INTEGER current_val;
169 // MS docs say this will always succeed for systems >= XP but it may
170 // not return a monotonic value with non-invariant TSC's etc
171 if (QueryPerformanceCounter(¤t_val) != 0) {
172 return (int64_t)(current_val.QuadPart * timer_rate_us);
175 DEBUG_TIMING ("Could not get QPC timer\n");
185 return QPC::get_microseconds ();
187 // For XP systems that don't support a high-res performance counter
188 return g_get_monotonic_time ();