Skip to content

Commit 19c60f5

Browse files
committed
change sysctl of cpuinfo
1 parent cb64777 commit 19c60f5

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/freebsd/api.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct cpuinfo_freebsd_topology {
77
uint32_t packages;
88
uint32_t cores;
99
uint32_t threads;
10+
uint32_t threads_per_core;
1011
};
1112

1213

src/freebsd/topology.c

+16-14
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@
88
#include <cpuinfo/log.h>
99
#include <freebsd/api.h>
1010

11-
12-
13-
struct cpuinfo_freebsd_topology cpuinfo_freebsd_detect_topology(void) {
14-
int threads = 1;
15-
size_t sizeof_threads = sizeof(threads);
16-
if (sysctlbyname("hw.ncpu", &threads, &sizeof_threads, NULL, 0) != 0) {
17-
cpuinfo_log_error("sysctlbyname(\"hw.ncpu\") failed: %s", strerror(errno));
18-
} else if (threads <= 0) {
19-
cpuinfo_log_error("sysctlbyname(\"hw.ncpu\") returned invalid value %d", threads);
11+
static int cpuinfo_from_freebsd_sysctl(const char *name) {
12+
int value = 0;
13+
size_t sizeof_int = sizeof(value);
14+
if (sysctlbyname(name, &value, &sizeof_int, NULL, 0) != 0) {
15+
cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", name, strerror(errno));
16+
} else if (value <= 0) {
17+
cpuinfo_log_error("sysctlbyname(\"%s\") returned invalid value %d", name, value);
2018
}
19+
return value;
20+
}
2121

22-
int cores = threads / 2;
23-
int packages = 1;
24-
25-
cpuinfo_log_debug("freebsd topology: packages = %d, cores = %d, threads = %d", packages, (int) cores, (int) threads);
22+
struct cpuinfo_freebsd_topology cpuinfo_freebsd_detect_topology(void) {
23+
int packages = cpuinfo_from_freebsd_sysctl("kern.smp.cpus");
24+
int cores = cpuinfo_from_freebsd_sysctl("kern.smp.cores");
25+
int threads_per_core = cpuinfo_from_freebsd_sysctl("kern.smp.threads_per_core");
26+
cpuinfo_log_debug("freebsd topology: packages = %d, cores = %d, threads_per_core = %d", packages, cores, threads_per_core);
2627
struct cpuinfo_freebsd_topology topology = {
2728
.packages = (uint32_t) packages,
2829
.cores = (uint32_t) cores,
29-
.threads = (uint32_t) threads
30+
.threads_per_core = (uint32_t) threads_per_core,
31+
.threads = (uint32_t) (threads_per_core * cores)
3032
};
3133

3234
return topology;

src/x86/freebsd/init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void cpuinfo_x86_freebsd_init(void) {
6161
char brand_string[48];
6262
cpuinfo_x86_normalize_brand_string(x86_processor.brand_string, brand_string);
6363

64-
const uint32_t threads_per_core = freebsd_topology.threads / freebsd_topology.cores;
64+
const uint32_t threads_per_core = freebsd_topology.threads_per_core;
6565
const uint32_t threads_per_package = freebsd_topology.threads / freebsd_topology.packages;
6666
const uint32_t cores_per_package = freebsd_topology.cores / freebsd_topology.packages;
6767
for (uint32_t i = 0; i < freebsd_topology.packages; i++) {

0 commit comments

Comments
 (0)