gallium/hud: add CPU usage support for DragonFly/NetBSD/OpenBSD

Each BSD has slightly different sysctl for retrieving per-CPU times.
FreeBSD returns long while NetBSD returns uint64_t. On OpenBSD return
type differs between summation and per-CPU times. DragonFly is
compatible with FreeBSD.

Signed-off-by: Jan Beich <jbeich@FreeBSD.org>
This commit is contained in:
Jan Beich 2019-08-31 18:32:16 +00:00 committed by Marek Olšák
parent ef621a73f7
commit 8e92ce9ba5
1 changed files with 41 additions and 2 deletions

View File

@ -38,11 +38,15 @@
#ifdef PIPE_OS_WINDOWS
#include <windows.h>
#endif
#ifdef PIPE_OS_FREEBSD
#if defined(PIPE_OS_BSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#if defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
#include <sys/sched.h>
#else
#include <sys/resource.h>
#endif
#endif
#ifdef PIPE_OS_WINDOWS
@ -91,20 +95,54 @@ get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
return TRUE;
}
#elif defined(PIPE_OS_FREEBSD)
#elif defined(PIPE_OS_BSD)
static boolean
get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
{
#if defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
uint64_t cp_time[CPUSTATES];
#else
long cp_time[CPUSTATES];
#endif
size_t len;
if (cpu_index == ALL_CPUS) {
len = sizeof(cp_time);
#if defined(PIPE_OS_NETBSD)
int mib[] = { CTL_KERN, KERN_CP_TIME };
if (sysctl(mib, ARRAY_SIZE(mib), cp_time, &len, NULL, 0) == -1)
return FALSE;
#elif defined(PIPE_OS_OPENBSD)
int mib[] = { CTL_KERN, KERN_CPTIME };
long sum_cp_time[CPUSTATES];
len = sizeof(sum_cp_time);
if (sysctl(mib, ARRAY_SIZE(mib), sum_cp_time, &len, NULL, 0) == -1)
return FALSE;
for (int state = 0; state < CPUSTATES; state++)
cp_time[state] = sum_cp_time[state];
#else
if (sysctlbyname("kern.cp_time", cp_time, &len, NULL, 0) == -1)
return FALSE;
#endif
} else {
#if defined(PIPE_OS_NETBSD)
int mib[] = { CTL_KERN, KERN_CP_TIME, cpu_index };
len = sizeof(cp_time);
if (sysctl(mib, ARRAY_SIZE(mib), cp_time, &len, NULL, 0) == -1)
return FALSE;
#elif defined(PIPE_OS_OPENBSD)
int mib[] = { CTL_KERN, KERN_CPTIME2, cpu_index };
len = sizeof(cp_time);
if (sysctl(mib, ARRAY_SIZE(mib), cp_time, &len, NULL, 0) == -1)
return FALSE;
#else
long *cp_times = NULL;
if (sysctlbyname("kern.cp_times", NULL, &len, NULL, 0) == -1)
@ -121,6 +159,7 @@ get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
memcpy(cp_time, cp_times + (cpu_index * CPUSTATES),
sizeof(cp_time));
free(cp_times);
#endif
}
*busy_time = cp_time[CP_USER] + cp_time[CP_NICE] +