@@ -29,28 +29,32 @@ void cpuinfo_x86_freebsd_init(void) {
29
29
struct cpuinfo_cache * l4 = NULL ;
30
30
31
31
struct cpuinfo_freebsd_topology freebsd_topology = cpuinfo_freebsd_detect_topology ();
32
+ if (freebsd_topology .packages == 0 ) {
33
+ cpuinfo_log_error ("failed to detect topology" );
34
+ goto cleanup ;
35
+ }
32
36
processors = calloc (freebsd_topology .threads , sizeof (struct cpuinfo_processor ));
33
37
if (processors == NULL ) {
34
- cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " logical processors" ,
38
+ cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " logical processors" ,
35
39
freebsd_topology .threads * sizeof (struct cpuinfo_processor ), freebsd_topology .threads );
36
40
goto cleanup ;
37
41
}
38
42
cores = calloc (freebsd_topology .cores , sizeof (struct cpuinfo_core ));
39
43
if (cores == NULL ) {
40
- cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " cores" ,
44
+ cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " cores" ,
41
45
freebsd_topology .cores * sizeof (struct cpuinfo_core ), freebsd_topology .cores );
42
46
goto cleanup ;
43
47
}
44
48
/* On x86 cluster of cores is a physical package */
45
49
clusters = calloc (freebsd_topology .packages , sizeof (struct cpuinfo_cluster ));
46
50
if (clusters == NULL ) {
47
- cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " core clusters" ,
51
+ cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " core clusters" ,
48
52
freebsd_topology .packages * sizeof (struct cpuinfo_cluster ), freebsd_topology .packages );
49
53
goto cleanup ;
50
54
}
51
55
packages = calloc (freebsd_topology .packages , sizeof (struct cpuinfo_package ));
52
56
if (packages == NULL ) {
53
- cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " physical packages" ,
57
+ cpuinfo_log_error ("failed to allocate %zu bytes for descriptions of %" PRIu32 " physical packages" ,
54
58
freebsd_topology .packages * sizeof (struct cpuinfo_package ), freebsd_topology .packages );
55
59
goto cleanup ;
56
60
}
@@ -61,7 +65,7 @@ void cpuinfo_x86_freebsd_init(void) {
61
65
char brand_string [48 ];
62
66
cpuinfo_x86_normalize_brand_string (x86_processor .brand_string , brand_string );
63
67
64
- const uint32_t threads_per_core = freebsd_topology .threads / freebsd_topology . cores ;
68
+ const uint32_t threads_per_core = freebsd_topology .threads_per_core ;
65
69
const uint32_t threads_per_package = freebsd_topology .threads / freebsd_topology .packages ;
66
70
const uint32_t cores_per_package = freebsd_topology .cores / freebsd_topology .packages ;
67
71
for (uint32_t i = 0 ; i < freebsd_topology .packages ; i ++ ) {
@@ -122,60 +126,52 @@ void cpuinfo_x86_freebsd_init(void) {
122
126
123
127
uint32_t threads_per_l1 = 0 , l1_count = 0 ;
124
128
if (x86_processor .cache .l1i .size != 0 || x86_processor .cache .l1d .size != 0 ) {
125
- if (threads_per_l1 == 0 ) {
126
- /* Assume that threads on the same core share L1 */
127
- threads_per_l1 = freebsd_topology .threads / freebsd_topology .cores ;
128
- cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L1 cache; assume %" PRIu32 ,
129
- threads_per_l1 );
130
- }
129
+ /* Assume that threads on the same core share L1 */
130
+ threads_per_l1 = freebsd_topology .threads / freebsd_topology .cores ;
131
+ cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L1 cache; assume %" PRIu32 ,
132
+ threads_per_l1 );
131
133
l1_count = freebsd_topology .threads / threads_per_l1 ;
132
134
cpuinfo_log_debug ("detected %" PRIu32 " L1 caches" , l1_count );
133
135
}
134
136
135
137
uint32_t threads_per_l2 = 0 , l2_count = 0 ;
136
138
if (x86_processor .cache .l2 .size != 0 ) {
137
- if (threads_per_l2 == 0 ) {
138
- if (x86_processor .cache .l3 .size != 0 ) {
139
- /* This is not a last-level cache; assume that threads on the same core share L2 */
140
- threads_per_l2 = freebsd_topology .threads / freebsd_topology .cores ;
141
- } else {
142
- /* This is a last-level cache; assume that threads on the same package share L2 */
143
- threads_per_l2 = freebsd_topology .threads / freebsd_topology .packages ;
144
- }
145
- cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L2 cache; assume %" PRIu32 ,
146
- threads_per_l2 );
147
- }
139
+ if (x86_processor .cache .l3 .size != 0 ) {
140
+ /* This is not a last-level cache; assume that threads on the same core share L2 */
141
+ threads_per_l2 = freebsd_topology .threads / freebsd_topology .cores ;
142
+ } else {
143
+ /* This is a last-level cache; assume that threads on the same package share L2 */
144
+ threads_per_l2 = freebsd_topology .threads / freebsd_topology .packages ;
145
+ }
146
+ cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L2 cache; assume %" PRIu32 ,
147
+ threads_per_l2 );
148
148
l2_count = freebsd_topology .threads / threads_per_l2 ;
149
149
cpuinfo_log_debug ("detected %" PRIu32 " L2 caches" , l2_count );
150
150
}
151
151
152
152
uint32_t threads_per_l3 = 0 , l3_count = 0 ;
153
153
if (x86_processor .cache .l3 .size != 0 ) {
154
- if (threads_per_l3 == 0 ) {
155
- /*
156
- * Assume that threads on the same package share L3.
157
- * However, is it not necessarily the last-level cache (there may be L4 cache as well)
158
- */
159
- threads_per_l3 = freebsd_topology .threads / freebsd_topology .packages ;
160
- cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L3 cache; assume %" PRIu32 ,
161
- threads_per_l3 );
162
- }
154
+ /*
155
+ * Assume that threads on the same package share L3.
156
+ * However, is it not necessarily the last-level cache (there may be L4 cache as well)
157
+ */
158
+ threads_per_l3 = freebsd_topology .threads / freebsd_topology .packages ;
159
+ cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L3 cache; assume %" PRIu32 ,
160
+ threads_per_l3 );
163
161
l3_count = freebsd_topology .threads / threads_per_l3 ;
164
162
cpuinfo_log_debug ("detected %" PRIu32 " L3 caches" , l3_count );
165
163
}
166
164
167
165
uint32_t threads_per_l4 = 0 , l4_count = 0 ;
168
166
if (x86_processor .cache .l4 .size != 0 ) {
169
- if (threads_per_l4 == 0 ) {
170
- /*
171
- * Assume that all threads share this L4.
172
- * As of now, L4 cache exists only on notebook x86 CPUs, which are single-package,
173
- * but multi-socket systems could have shared L4 (like on IBM POWER8).
174
- */
175
- threads_per_l4 = freebsd_topology .threads ;
176
- cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L4 cache; assume %" PRIu32 ,
177
- threads_per_l4 );
178
- }
167
+ /*
168
+ * Assume that all threads share this L4.
169
+ * As of now, L4 cache exists only on notebook x86 CPUs, which are single-package,
170
+ * but multi-socket systems could have shared L4 (like on IBM POWER8).
171
+ */
172
+ threads_per_l4 = freebsd_topology .threads ;
173
+ cpuinfo_log_warning ("freebsd kernel did not report number of threads sharing L4 cache; assume %" PRIu32 ,
174
+ threads_per_l4 );
179
175
l4_count = freebsd_topology .threads / threads_per_l4 ;
180
176
cpuinfo_log_debug ("detected %" PRIu32 " L4 caches" , l4_count );
181
177
}
0 commit comments