|
8 | 8 | #include <cpuinfo/log.h>
|
9 | 9 | #include <freebsd/api.h>
|
10 | 10 |
|
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); |
20 | 18 | }
|
| 19 | + return value; |
| 20 | +} |
21 | 21 |
|
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); |
26 | 27 | struct cpuinfo_freebsd_topology topology = {
|
27 | 28 | .packages = (uint32_t) packages,
|
28 | 29 | .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) |
30 | 32 | };
|
31 | 33 |
|
32 | 34 | return topology;
|
|
0 commit comments