Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FreeBSD][Apple] Implement fallback frequency calculation #251

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[v1.05][FreeBSD][Apple] Implement fallback frequency calculation
  • Loading branch information
er2off committed Aug 7, 2024
commit 607f982a34bc425bb2c4a47169b68bdaa7ce2af6
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 30,10 @@ ifneq ($(OS),Windows_NT)
HEADERS = $(SRC_DIR)freq/freq.h
CFLAGS = -pthread
endif
ifeq ($(os), FreeBSD)
SOURCE = $(SRC_COMMON)sysctl.c
HEADERS = $(SRC_COMMON)sysctl.h
endif
CFLAGS = -DARCH_X86 -std=c99 -fstack-protector-all
else ifeq ($(arch), $(filter $(arch), ppc64le ppc64 ppcle ppc))
SRC_DIR=src/ppc/
Expand Down
8 changes: 8 additions & 0 deletions src/common/sysctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 40,14 @@
#define CPUSUBFAMILY_ARM_HC_HD 5
#endif

// For alternative way to get CPU frequency on macOS and *BSD
#ifdef __APPLE__
#define CPUFREQUENCY_SYSCTL "hw.cpufrequency_max"
#else
// For FreeBSD, not sure about other *BSD
#define CPUFREQUENCY_SYSCTL "dev.cpu.0.freq"
#endif

uint32_t get_sys_info_by_name(char* name);

#endif
15 changes: 14 additions & 1 deletion src/x86/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,9 @@
#include "../common/udev.h"
#include <unistd.h>
#endif
#if defined (__FreeBSD__) || defined (__APPLE__)
#include "../common/sysctl.h"
#endif

#ifdef __linux__
#include "../common/freq.h"
Expand Down Expand Up @@ -938,10 941,20 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) {
freq->measured = false;

if(cpu->maxLevels < 0x00000016) {
#if defined (_WIN32) || defined (__APPLE__)
#if defined (_WIN32)
printWarn("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000016, cpu->maxLevels);
freq->base = UNKNOWN_DATA;
freq->max = UNKNOWN_DATA;
#elif defined (__FreeBSD__) || defined (__APPLE__)
printWarn("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X). Using sysctl", 0x00000016, cpu->maxLevels);
uint32_t freq_hz = get_sys_info_by_name(CPUFREQUENCY_SYSCTL);
if (freq_hz == 0) {
printWarn("Read max CPU frequency from sysctl and got 0 MHz");
freq->max = UNKNOWN_DATA;
}

freq->base = UNKNOWN_DATA;
freq->max = freq_hz;
#else
printWarn("Can't read frequency information from cpuid (needed level is 0x%.8X, max is 0x%.8X). Using udev", 0x00000016, cpu->maxLevels);
freq->base = UNKNOWN_DATA;
Expand Down