1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_timer.cpp
28 #include "vogl_core.h"
29 #include "vogl_timer.h"
32 #include "vogl_timer.h"
34 #ifdef VOGL_USE_WIN32_API
35 #include "vogl_winhdr.h"
40 uint64_t timer::g_init_ticks;
41 uint64_t timer::g_freq;
42 double timer::g_inv_freq;
44 #if defined(VOGL_USE_WIN32_API)
45 inline void query_counter(timer_ticks *pTicks)
47 QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(pTicks));
49 inline void query_counter_frequency(timer_ticks *pTicks)
51 QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER *>(pTicks));
53 #elif defined(__GNUC__)
54 #include <sys/timex.h>
55 inline void query_counter(timer_ticks *pTicks)
57 struct timeval cur_time;
58 gettimeofday(&cur_time, NULL);
59 *pTicks = static_cast<unsigned long long>(cur_time.tv_sec) * 1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
61 inline void query_counter_frequency(timer_ticks *pTicks)
79 timer::timer(timer_ticks start_ticks)
84 m_start_time = start_ticks;
90 void timer::start(timer_ticks start_ticks)
92 m_start_time = start_ticks;
100 query_counter(&m_start_time);
108 VOGL_ASSERT(m_started);
110 query_counter(&m_stop_time);
115 double timer::get_elapsed_secs() const
117 VOGL_ASSERT(m_started);
121 timer_ticks stop_time = m_stop_time;
123 query_counter(&stop_time);
125 timer_ticks delta = stop_time - m_start_time;
126 return delta * g_inv_freq;
129 timer_ticks timer::get_elapsed_us() const
131 VOGL_ASSERT(m_started);
135 timer_ticks stop_time = m_stop_time;
137 query_counter(&stop_time);
139 timer_ticks delta = stop_time - m_start_time;
140 return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq;
143 timer_ticks timer::get_elapsed_ticks() const
145 VOGL_ASSERT(m_started);
149 timer_ticks stop_time = m_stop_time;
151 query_counter(&stop_time);
153 return stop_time - m_start_time;
160 query_counter_frequency(&g_freq);
161 g_inv_freq = 1.0f / g_freq;
163 query_counter(&g_init_ticks);
167 timer_ticks timer::get_init_ticks()
175 timer_ticks timer::get_ticks()
181 query_counter(&ticks);
182 return ticks - g_init_ticks;
185 double timer::ticks_to_secs(timer_ticks ticks)
190 return ticks * g_inv_freq;