gallium/hud: add CPU usage support for FreeBSD
authorGreg V <greg@unrelenting.technology>
Sun, 3 Mar 2019 15:40:58 +0000 (18:40 +0300)
committerEric Engestrom <eric@engestrom.ch>
Sun, 7 Apr 2019 06:47:57 +0000 (06:47 +0000)
Reviewed-by: Eric Engestrom <eric@engestrom.ch>
src/gallium/auxiliary/hud/hud_cpu.c

index b7a524330bf3e5aaa4a5db585c3c05addb1a5879..7059226985d9a03f4a4bcf5d97f8f2f7956fde8b 100644 (file)
 #ifdef PIPE_OS_WINDOWS
 #include <windows.h>
 #endif
+#ifdef PIPE_OS_FREEBSD
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
+#endif
 
 
 #ifdef PIPE_OS_WINDOWS
@@ -86,6 +91,46 @@ get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
    return TRUE;
 }
 
+#elif defined(PIPE_OS_FREEBSD)
+
+static boolean
+get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
+{
+   long cp_time[CPUSTATES];
+   size_t len;
+
+   if (cpu_index == ALL_CPUS) {
+      len = sizeof(cp_time);
+
+      if (sysctlbyname("kern.cp_time", cp_time, &len, NULL, 0) == -1)
+         return FALSE;
+   } else {
+      long *cp_times = NULL;
+
+      if (sysctlbyname("kern.cp_times", NULL, &len, NULL, 0) == -1)
+         return FALSE;
+
+      if (len < (cpu_index + 1) * sizeof(cp_time))
+         return FALSE;
+
+      cp_times = malloc(len);
+
+      if (sysctlbyname("kern.cp_times", cp_times, &len, NULL, 0) == -1)
+         return FALSE;
+
+      memcpy(cp_time, cp_times + (cpu_index * CPUSTATES),
+            sizeof(cp_time));
+      free(cp_times);
+   }
+
+   *busy_time = cp_time[CP_USER] + cp_time[CP_NICE] +
+      cp_time[CP_SYS] + cp_time[CP_INTR];
+
+   *total_time = *busy_time + cp_time[CP_IDLE];
+
+   return TRUE;
+}
+
 #else
 
 static boolean