|
| 1 | +#include <errno.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <sys/sysctl.h> |
| 6 | +#include <sys/types.h> |
| 7 | + |
| 8 | +#include <cpuinfo/log.h> |
| 9 | +#include <freebsd/api.h> |
| 10 | + |
| 11 | +static int sysctl_int(const char* name) { |
| 12 | + int value = 0; |
| 13 | + size_t value_size = sizeof(value); |
| 14 | + if (sysctlbyname(name, &value, &value_size, 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 %zu", name, value, value_size); |
| 18 | + value = 0; |
| 19 | + } |
| 20 | + return value; |
| 21 | +} |
| 22 | + |
| 23 | +static char* sysctl_str(const char* name) { |
| 24 | + size_t value_size = 0; |
| 25 | + if (sysctlbyname(name, NULL, &value_size, NULL, 0) != 0) { |
| 26 | + cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", name, strerror(errno)); |
| 27 | + } else if (value_size <= 0) { |
| 28 | + cpuinfo_log_error("sysctlbyname(\"%s\") returned invalid value size %zu", name, value_size); |
| 29 | + } |
| 30 | + value_size += 1; |
| 31 | + char* value = calloc(value_size, 1); |
| 32 | + if (!value) { |
| 33 | + cpuinfo_log_error("calloc %zu bytes failed", value_size); |
| 34 | + return NULL; |
| 35 | + } |
| 36 | + if (sysctlbyname(name, value, &value_size, NULL, 0) != 0) { |
| 37 | + cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", name, strerror(errno)); |
| 38 | + free(value); |
| 39 | + return NULL; |
| 40 | + } |
| 41 | + return value; |
| 42 | +} |
| 43 | + |
| 44 | +struct cpuinfo_freebsd_topology cpuinfo_freebsd_detect_topology(void) { |
| 45 | + struct cpuinfo_freebsd_topology topology = { |
| 46 | + .packages = 0, |
| 47 | + .cores = 0, |
| 48 | + .threads_per_core = 0, |
| 49 | + .threads = 0, |
| 50 | + }; |
| 51 | + char* topology_spec = sysctl_str("kern.sched.topology_spec"); |
| 52 | + if (!topology_spec) { |
| 53 | + return topology; |
| 54 | + } |
| 55 | + const char* group_tag = "<group level=\"1\" cache-level=\"0\">"; |
| 56 | + char* p = strstr(topology_spec, group_tag); |
| 57 | + while (p) { |
| 58 | + const char* cpu_tag = "cpu count=\""; |
| 59 | + char* q = strstr(p, cpu_tag); |
| 60 | + if (q) { |
| 61 | + p = q + strlen(cpu_tag); |
| 62 | + topology.packages += atoi(p); |
| 63 | + } else { |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + if (topology.packages == 0) { |
| 68 | + const char* group_tag = "<group level=\"1\""; |
| 69 | + char* p = strstr(topology_spec, group_tag); |
| 70 | + while (p) { |
| 71 | + topology.packages += 1; |
| 72 | + p++; |
| 73 | + p = strstr(p, group_tag); |
| 74 | + } |
| 75 | + } |
| 76 | + if (topology.packages == 0) { |
| 77 | + cpuinfo_log_error("failed to parse topology_spec:%s", topology_spec); |
| 78 | + free(topology_spec); |
| 79 | + goto fail; |
| 80 | + } |
| 81 | + free(topology_spec); |
| 82 | + topology.cores = sysctl_int("kern.smp.cores"); |
| 83 | + if (topology.cores == 0) { |
| 84 | + goto fail; |
| 85 | + } |
| 86 | + if (topology.cores < topology.packages) { |
| 87 | + goto fail; |
| 88 | + } |
| 89 | + topology.threads_per_core = sysctl_int("kern.smp.threads_per_core"); |
| 90 | + if (topology.threads_per_core == 0) { |
| 91 | + goto fail; |
| 92 | + } |
| 93 | + cpuinfo_log_debug( |
| 94 | + "freebsd topology: packages = %d, cores = %d, " |
| 95 | + "threads_per_core = %d", |
| 96 | + topology.packages, |
| 97 | + topology.cores, |
| 98 | + topology.threads_per_core); |
| 99 | + topology.threads = topology.threads_per_core * topology.cores; |
| 100 | + return topology; |
| 101 | +fail: |
| 102 | + topology.packages = 0; |
| 103 | + return topology; |
| 104 | +} |
0 commit comments