From ddbf8c0065ce1b7502e3e127a846add476ab7a4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 19 Jan 2012 15:40:59 +0000 Subject: [PATCH] Make os::getTime() inline and make time frequency OS-dependent variable. Linux's clock_gettime() implementation from Ryan C. Gordon. --- CMakeLists.txt | 9 +++++ common/os_posix.cpp | 9 ----- common/os_time.hpp | 85 +++++++++++++++++++++++++++++++++++++++++++++ common/os_win32.cpp | 11 +----- glretrace_main.cpp | 3 +- 5 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 common/os_time.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fa8e45..24e2afc 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -573,6 +573,11 @@ if (WIN32 OR APPLE OR X11_FOUND) pthread dl ) + + if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + target_link_libraries (glretrace rt) + endif () + endif () install (TARGETS glretrace RUNTIME DESTINATION bin) @@ -601,6 +606,10 @@ if (EGL_FOUND AND X11_FOUND AND NOT WIN32 AND NOT APPLE) dl ) + if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + target_link_libraries (eglretrace rt) + endif () + install (TARGETS eglretrace RUNTIME DESTINATION bin) endif () diff --git a/common/os_posix.cpp b/common/os_posix.cpp index e6af738..261fe88 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -159,14 +158,6 @@ log(const char *format, ...) logging = false; } -long long -getTime(void) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_usec + tv.tv_sec*1000000LL; -} - void abort(void) { diff --git a/common/os_time.hpp b/common/os_time.hpp new file mode 100644 index 0000000..a4f5209 --- /dev/null +++ b/common/os_time.hpp @@ -0,0 +1,85 @@ +/************************************************************************** + * + * Copyright 2011 Jose Fonseca + * All Rights Reserved. + * + * 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. + * + **************************************************************************/ + +/* + * Simple OS time measurement abstraction. + */ + +#ifndef _OS_TIME_HPP_ +#define _OS_TIME_HPP_ + + +#if defined(_WIN32) +#include +#elif defined(__linux__) +#include +#else +#include +#endif + + +namespace os { + + // OS dependent time frequency +#if defined(_WIN32) + // runtime variable on Windows + extern long long timeFrequency; +#elif defined(__linux__) + // nanoseconds + static const long long timeFrequency = 1000000000; +#else + // microseconds on + static const long long timeFrequency = 1000000; +#endif + + // Time from an unknown base in a unit determined by timeFrequency + inline long long + getTime(void) { +#if defined(_WIN32) + if (!timeFrequency) { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + timeFrequency = frequency.QuadPart; + } + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + return counter.QuadPart; +#elif defined(__linux__) + struct timespec tp; + if (clock_gettime(CLOCK_REALTIME, &tp) == -1) { + return 0; + } + return tp.tv_sec * 1000000000LL + tp.tv_nsec; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000000LL + tv.tv_usec; +#endif + } + + +} /* namespace os */ + +#endif /* _OS_TIME_HPP_ */ diff --git a/common/os_win32.cpp b/common/os_win32.cpp index 7b35ccf..199ae5a 100644 --- a/common/os_win32.cpp +++ b/common/os_win32.cpp @@ -213,16 +213,7 @@ log(const char *format, ...) #endif } -long long -getTime(void) -{ - static LARGE_INTEGER frequency; - LARGE_INTEGER counter; - if (!frequency.QuadPart) - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&counter); - return counter.QuadPart*1000000LL/frequency.QuadPart; -} +long long timeFrequency = 0LL; void abort(void) diff --git a/glretrace_main.cpp b/glretrace_main.cpp index 978a34e..c2ca664 100644 --- a/glretrace_main.cpp +++ b/glretrace_main.cpp @@ -27,6 +27,7 @@ #include #include "os_string.hpp" +#include "os_time.hpp" #include "image.hpp" #include "retrace.hpp" #include "glproc.hpp" @@ -230,7 +231,7 @@ static void display(void) { glFlush(); long long endTime = os::getTime(); - float timeInterval = (endTime - startTime) * 1.0E-6; + float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency); if (retrace::verbosity >= -1) { std::cout << -- 2.43.0