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