From 60ad6792ba88ae6158647373530e3a2cce2b487c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 27 Oct 2022 10:08:22 +0800 Subject: [PATCH] CpuUsage: support freebsd --- CMakeLists.txt | 3 ++- src/detection/cpuUsage/cpuUsage_bsd.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/detection/cpuUsage/cpuUsage_bsd.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d59a68ecf..86a271a70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -310,6 +310,7 @@ endif() if(LINUX OR ANDROID) list(APPEND LIBFASTFETCH_SRC src/detection/cpu/cpu_linux.c + src/detection/cpuUsage/cpuUsage_linux.c src/detection/memory/memory_linux.c src/detection/processes/processes_linux.c src/detection/swap/swap_linux.c @@ -319,7 +320,6 @@ endif() if(LINUX OR ANDROID OR BSD) list(APPEND LIBFASTFETCH_SRC - src/detection/cpuUsage/cpuUsage_linux.c src/detection/disk/disk_linux.c src/detection/temps/temps_linux.c src/detection/opengl/opengl_linux.c @@ -418,6 +418,7 @@ endif() if(BSD) list(APPEND LIBFASTFETCH_SRC src/detection/cpu/cpu_bsd.c + src/detection/cpuUsage/cpuUsage_bsd.c src/detection/memory/memory_bsd.c src/detection/swap/swap_bsd.c ) diff --git a/src/detection/cpuUsage/cpuUsage_bsd.c b/src/detection/cpuUsage/cpuUsage_bsd.c new file mode 100644 index 000000000..d96064fd4 --- /dev/null +++ b/src/detection/cpuUsage/cpuUsage_bsd.c @@ -0,0 +1,19 @@ +#include "cpuUsage.h" + +#include +#include +#include + +const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) +{ + // interrupt processing, user processes, system processing, lock spinning, and idling + uint64_t cpTime[5]; + size_t neededLength = sizeof(cpTime); + if(sysctlbyname("kern.cp_time", cpTime, &neededLength, NULL, 0) != 0) + return "sysctlbyname(kern.cp_time) failed"; + + *inUseAll = cpTime[0] + cpTime[1] + cpTime[2] + cpTime[3]; + *totalAll = *inUseAll + cpTime[4]; + + return NULL; +}