]> git.cworth.org Git - apitrace/commitdiff
Make os::getTime() inline and make time frequency OS-dependent variable.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Thu, 19 Jan 2012 15:40:59 +0000 (15:40 +0000)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Thu, 19 Jan 2012 15:40:59 +0000 (15:40 +0000)
Linux's  clock_gettime() implementation from Ryan C. Gordon.

CMakeLists.txt
common/os_posix.cpp
common/os_time.hpp [new file with mode: 0644]
common/os_win32.cpp
glretrace_main.cpp

index 3fa8e454d2276d142005813308a02c9e942f9158..24e2afc06bcdeb5e52a6e8ccb7ce9f3e1a2851de 100755 (executable)
@@ -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 ()
 
index e6af7383f35b6a78b4efad1fcf1339bb8a3f5531..261fe88c929409d796b8cbd82ed6ea574dfc9990 100644 (file)
@@ -30,7 +30,6 @@
 #include <stdlib.h>
 
 #include <unistd.h>
-#include <sys/time.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -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 (file)
index 0000000..a4f5209
--- /dev/null
@@ -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 <windows.h>
+#elif defined(__linux__)
+#include <time.h>
+#else
+#include <sys/time.h>
+#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_ */
index 7b35ccf8cacf322338fa1107fffc3af40cc6affa..199ae5a7e4a656be9b41c24d8c53090acabe0360 100644 (file)
@@ -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)
index 978a34eef38b2f9b9811abf59b819a7354ad372f..c2ca664239500d76d7d0dc2390533d80139a99f7 100644 (file)
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #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 <<