From c5a6e72e15b67b37e5b2e35721261ec3da60c045 Mon Sep 17 00:00:00 2001 From: Greg V Date: Sun, 3 Mar 2019 18:40:58 +0300 Subject: [PATCH] gallium/hud: add CPU usage support for FreeBSD Reviewed-by: Eric Engestrom --- src/gallium/auxiliary/hud/hud_cpu.c | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/gallium/auxiliary/hud/hud_cpu.c b/src/gallium/auxiliary/hud/hud_cpu.c index b7a5243..7059226 100644 --- a/src/gallium/auxiliary/hud/hud_cpu.c +++ b/src/gallium/auxiliary/hud/hud_cpu.c @@ -38,6 +38,11 @@ #ifdef PIPE_OS_WINDOWS #include #endif +#ifdef PIPE_OS_FREEBSD +#include +#include +#include +#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 -- 2.7.4