[Mesa-dev] [PATCH 1/2] gallium/os: Add CPU time functions

Bas Nieuwenhuizen bas at basnieuwenhuizen.nl
Thu Feb 18 23:59:18 UTC 2016


Adds functions to query the elapsed CPU time of the current process
or thread. Implements Linux support only.

To be used by the gallium HUD.

Signed-off-by: Bas Nieuwenhuizen <bas at basnieuwenhuizen.nl>
---
 src/gallium/auxiliary/os/os_time.c | 48 ++++++++++++++++++++++++++++++++++++++
 src/gallium/auxiliary/os/os_time.h | 18 ++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/src/gallium/auxiliary/os/os_time.c b/src/gallium/auxiliary/os/os_time.c
index 3d2e416..e186f42 100644
--- a/src/gallium/auxiliary/os/os_time.c
+++ b/src/gallium/auxiliary/os/os_time.c
@@ -80,6 +80,54 @@ os_time_get_nano(void)
 #endif
 }
 
+bool
+os_cpu_time_supported(void)
+{
+#if defined(PIPE_OS_LINUX)
+
+   return true;
+
+#else
+
+   return false;
+
+#endif
+}
+
+int64_t
+os_process_time_get_nano(void)
+{
+#if defined(PIPE_OS_LINUX)
+
+   struct timespec tv;
+   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tv);
+   return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
+
+#else
+
+   assert(0 && "os_process_time_get_nano is not implemented on this platform");
+   return 0;
+
+#endif
+}
+
+int64_t
+os_thread_time_get_nano(void)
+{
+#if defined(PIPE_OS_LINUX)
+
+   struct timespec tv;
+   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv);
+   return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
+
+#else
+
+   assert(0 && "os_thread_time_get_nano is not implemented on this platform");
+   return 0;
+
+#endif
+}
+
 
 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
 
diff --git a/src/gallium/auxiliary/os/os_time.h b/src/gallium/auxiliary/os/os_time.h
index 9312e02..8eaca59 100644
--- a/src/gallium/auxiliary/os/os_time.h
+++ b/src/gallium/auxiliary/os/os_time.h
@@ -66,6 +66,24 @@ os_time_get(void)
    return os_time_get_nano() / 1000;
 }
 
+/*
+ * Returns whether os_process_time_get_nano and os_thread_time_get_nano are
+ * supported.
+ */
+bool
+os_cpu_time_supported(void);
+
+/*
+ * Get the CPU time spent in the current process.
+ */
+int64_t
+os_process_time_get_nano(void);
+
+/*
+ * Get the CPUtime spent in the current thread.
+ */
+int64_t
+os_thread_time_get_nano(void);
 
 /*
  * Sleep.
-- 
2.7.1



More information about the mesa-dev mailing list