Line data Source code
1 : #include "fd_topob_layout.h"
2 : #include "../net/fd_net_tile.h"
3 : #include "../../util/tile/fd_tile_private.h" /* fd_tile_private_sibling_idx */
4 :
5 : #include <ctype.h>
6 :
7 : #define SET_NAME cpu_bv
8 : #define SET_MAX FD_TILE_MAX
9 : #include "../../util/tmpl/fd_set.c"
10 :
11 : #define ALWAYS FD_TOPOB_TILE_ALWAYS
12 : #define STARTUP FD_TOPOB_TILE_STARTUP
13 : #define POSTSTART FD_TOPOB_TILE_POSTSTART
14 : #define FLOATING FD_TOPOB_TILE_FLOATING
15 : #define CRITICAL FD_TOPOB_TILE_SMT_CRITICAL
16 : #define DEDICATED FD_TOPOB_TILE_DEDICATED
17 : #define THRUPUT FD_TOPOB_TILE_THROUGHPUT
18 : #define PIN_EFF FD_TOPOB_TILE_PIN_EFFICIENT
19 :
20 : /* Per-tile classes, keyed by tile name. Auto layout places each
21 : phase in turn (ALWAYS, then STARTUP, then POSTSTART), walking rows
22 : top to bottom, so within a phase earlier rows get earlier CPUs in
23 : the NUMA/sibling ordering. Reordering rows changes layouts. */
24 :
25 : static struct { char const * name; uint flags; } const TILE_CLASS[] = {
26 : { "backt", ALWAYS },
27 : { "benchg", ALWAYS|THRUPUT },
28 : { "benchs", ALWAYS|THRUPUT },
29 : { "net", ALWAYS },
30 : { "mlx5", ALWAYS },
31 : { "iavf", ALWAYS|PIN_EFF },
32 : { "sock", ALWAYS|PIN_EFF },
33 : { "quic", ALWAYS },
34 : { "bundle", ALWAYS },
35 : { "verify", ALWAYS|THRUPUT },
36 : { "dedup", ALWAYS },
37 : { "resolv", ALWAYS },
38 : { "pack", ALWAYS|CRITICAL },
39 : { "sign", ALWAYS },
40 : { "shred", ALWAYS },
41 : { "event", ALWAYS },
42 : { "gui", ALWAYS },
43 : { "rpc", ALWAYS },
44 : { "gossvf", ALWAYS },
45 : { "gossip", ALWAYS },
46 : { "repair", ALWAYS },
47 : { "rserve", ALWAYS },
48 : { "replay", ALWAYS|THRUPUT },
49 : { "votor", ALWAYS },
50 : { "pktgen", ALWAYS },
51 : { "mwaitx", ALWAYS|PIN_EFF },
52 :
53 : /* Must shut down after snapshot load */
54 : { "snapct", STARTUP },
55 : { "snapld", STARTUP },
56 : { "snapdc", STARTUP|THRUPUT },
57 : { "snapin", STARTUP|THRUPUT },
58 :
59 : /* Must sleep until snapshot load finishes */
60 : { "accdb", POSTSTART|THRUPUT },
61 : { "execle", POSTSTART|THRUPUT },
62 : { "motor", POSTSTART|CRITICAL },
63 : { "execrp", POSTSTART|DEDICATED|THRUPUT },
64 : { "solcap", POSTSTART|PIN_EFF },
65 :
66 : /* Yield to the kernel scheduler */
67 : { "netlnk", FLOATING },
68 : { "metric", FLOATING },
69 : { "diag", FLOATING },
70 : { "waker", FLOATING },
71 : { "bencho", FLOATING },
72 : { "l4swap", FLOATING },
73 : { "genesi", FLOATING },
74 : { "ipecho", FLOATING },
75 : { "admin", FLOATING },
76 : { "failov", FLOATING },
77 : { "snapsv", FLOATING|PIN_EFF },
78 : { "snapmk", FLOATING },
79 : { "snapzp", FLOATING },
80 : { "snaprd", FLOATING },
81 : };
82 :
83 : #undef ALWAYS
84 : #undef STARTUP
85 : #undef POSTSTART
86 : #undef FLOATING
87 : #undef CRITICAL
88 : #undef DEDICATED
89 : #undef THRUPUT
90 : #undef PIN_EFF
91 :
92 0 : #define TILE_CLASS_CNT (sizeof(TILE_CLASS)/sizeof(TILE_CLASS[0]))
93 :
94 : uint
95 0 : fd_topob_tile_class( char const * name ) {
96 0 : for( ulong i=0UL; i<TILE_CLASS_CNT; i++ ) {
97 0 : if( !strcmp( name, TILE_CLASS[ i ].name ) ) return TILE_CLASS[ i ].flags;
98 0 : }
99 0 : return 0U;
100 0 : }
101 :
102 : /* The pinned tiles that park and wake each other the most, in
103 : placement priority. A wake across an L3 domain costs more than one
104 : inside it, so these are packed into as few domains as possible,
105 : mwaitx first. Floating tiles are not listed: their cpu_idx only
106 : picks the NUMA node of the pool they run over, not an L3. */
107 :
108 : static char const * HOT_TILES[] = {
109 : "mwaitx",
110 : "replay",
111 : "execrp",
112 : "mlx5",
113 : "iavf",
114 : NULL
115 : };
116 :
117 :
118 : static int
119 : tile_name_in( char const * name,
120 0 : char const * const * names ) {
121 0 : for( char const * const * p = names; *p; p++ ) {
122 0 : if( !strcmp( name, *p ) ) return 1;
123 0 : }
124 0 : return 0;
125 0 : }
126 :
127 : static int
128 0 : tile_is_floating( fd_topo_tile_t const * tile ) {
129 : /* RX only loopback sock copies its associated net tile's CPU affinity,
130 : so no extra CPU is needed for pinning. */
131 0 : return !!( fd_topob_tile_class( tile->name ) & FD_TOPOB_TILE_FLOATING ) || sock_lo_net_tile_id( tile )!=ULONG_MAX;
132 0 : }
133 :
134 : int
135 0 : fd_topob_tile_priority_type( fd_topo_tile_t const * tile ) {
136 0 : uint cls = fd_topob_tile_class( tile->name );
137 0 : if( !cls || tile_is_floating( tile ) ) return FD_TOPOB_PRIORITY_FLOATING;
138 0 : if( cls & FD_TOPOB_TILE_STARTUP ) return FD_TOPOB_PRIORITY_STARTUP;
139 0 : if( cls & FD_TOPOB_TILE_SMT_CRITICAL ) return FD_TOPOB_PRIORITY_CRITICAL;
140 0 : return FD_TOPOB_PRIORITY_NORMAL;
141 0 : }
142 :
143 : FD_STATIC_ASSERT( FD_TILE_MAX<FD_TOPOB_CPU_SHARED, update_tile_to_cpu_type );
144 :
145 : ulong
146 : fd_topob_parse_affinity_cstr( char const * cstr,
147 : ushort * tile_to_cpu,
148 : int allow_repeats,
149 0 : int allow_shared ) {
150 0 : if( !cstr ) return 0UL;
151 0 : ulong cnt = 0UL;
152 :
153 0 : cpu_bv_t cpu_assigned[ cpu_bv_word_cnt ];
154 0 : cpu_bv_new( cpu_assigned );
155 :
156 0 : char const * p = cstr;
157 0 : for(;;) {
158 :
159 0 : while( fd_isspace( (int)p[0] ) ) p++;
160 :
161 0 : if( p[0]=='f' ) {
162 0 : p++;
163 :
164 0 : ulong float_cnt;
165 :
166 0 : while( fd_isspace( (int)p[0] ) ) p++;
167 0 : if ( p[0]==',' ) float_cnt = 1UL, p++;
168 0 : else if( p[0]=='\0' ) float_cnt = 1UL;
169 0 : else if( !fd_isdigit( (int)p[0] ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (malformed float count)" ));
170 0 : else {
171 0 : float_cnt = fd_cstr_to_ulong( p );
172 0 : if( FD_UNLIKELY( !float_cnt ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (bad float count)" ));
173 0 : p++; while( fd_isdigit( (int)p[0] ) ) p++;
174 0 : while( fd_isspace( (int)p[0] ) ) p++;
175 0 : if( FD_UNLIKELY( !( p[0]==',' || p[0]=='\0' ) ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (bad float count delimiter)" ));
176 0 : if( p[0]==',' ) p++;
177 0 : }
178 :
179 0 : do {
180 0 : if( FD_UNLIKELY( cnt>=FD_TILE_MAX ) ) FD_LOG_ERR(( "fd_topob: too many affinity entries" ));
181 0 : tile_to_cpu[ cnt++ ] = (ushort)65535;
182 0 : } while( --float_cnt );
183 :
184 0 : continue;
185 0 : }
186 :
187 0 : ulong shared = 0UL;
188 0 : if( p[0]=='s' ) {
189 0 : if( FD_UNLIKELY( !allow_shared ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (shared cpus not supported here)" ));
190 0 : p++; shared = FD_TOPOB_CPU_SHARED;
191 0 : }
192 :
193 0 : if( !fd_isdigit( (int)p[0] ) ) {
194 0 : if( FD_UNLIKELY( shared || p[0]!='\0' ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (range lo not a cpu)" ));
195 0 : break;
196 0 : }
197 0 : ulong cpu0 = fd_cstr_to_ulong( p );
198 0 : ulong cpu1 = cpu0;
199 0 : ulong stride = 1UL;
200 0 : int rev = 0; /* Nh lists N first */
201 0 : p++; while( fd_isdigit( (int)p[0] ) ) p++;
202 0 : while( fd_isspace( (int)p[0] ) ) p++;
203 0 : if( p[0]=='-' ) {
204 0 : p++;
205 0 : while( fd_isspace( (int)p[0] ) ) p++;
206 0 : if( FD_UNLIKELY( !fd_isdigit( (int)p[0] ) ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (range hi not a cpu)" ));
207 0 : cpu1 = fd_cstr_to_ulong( p );
208 0 : p++; while( fd_isdigit( (int)p[0] ) ) p++;
209 0 : while( fd_isspace( (int)p[0] ) ) p++;
210 0 : if( p[0]=='/' || p[0]==':' ) {
211 0 : p++;
212 0 : while( fd_isspace( (int)p[0] ) ) p++;
213 0 : if( FD_UNLIKELY( !fd_isdigit( (int)p[0] ) ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (stride not an int)" ));
214 0 : stride = fd_cstr_to_ulong( p );
215 0 : p++; while( fd_isdigit( (int)p[0] ) ) p++;
216 0 : }
217 0 : }
218 0 : else if( p[0]=='h' ) {
219 0 : p++;
220 0 : ulong sibling = fd_tile_private_sibling_idx( cpu0 );
221 0 : if( sibling!=ULONG_MAX ) {
222 0 : rev = sibling<cpu0;
223 0 : cpu1 = fd_ulong_max( cpu0, sibling );
224 0 : cpu0 = fd_ulong_min( cpu0, sibling );
225 0 : stride = cpu1-cpu0;
226 0 : }
227 0 : }
228 0 : while( fd_isspace( (int)p[0] ) ) p++;
229 0 : if( FD_UNLIKELY( !( p[0]==',' || p[0]=='\0' ) ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (bad range delimiter)" ));
230 0 : if( p[0]==',' ) p++;
231 0 : cpu1++;
232 0 : if( FD_UNLIKELY( cpu1<=cpu0 ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (invalid range)" ));
233 0 : if( FD_UNLIKELY( !stride ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (invalid stride)" ));
234 :
235 0 : for( ulong cpu=cpu0; cpu<cpu1; cpu+=stride ) {
236 0 : if( FD_UNLIKELY( cnt>=FD_TILE_MAX ) ) FD_LOG_ERR(( "fd_topob: too many affinity entries" ));
237 0 : if( FD_UNLIKELY( cpu>=FD_TILE_MAX ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (cpu index too large)" )); /* cpu_assigned holds FD_TILE_MAX */
238 0 : if( FD_UNLIKELY( !allow_repeats && !shared && cpu_bv_test( cpu_assigned, cpu ) ) ) FD_LOG_ERR(( "fd_topob: malformed affinity string (repeated cpu)" ));
239 0 : tile_to_cpu[ cnt++ ] = (ushort)(cpu | shared);
240 0 : if( !shared ) cpu_bv_insert( cpu_assigned, cpu );
241 0 : }
242 0 : if( rev ) { ushort t = tile_to_cpu[ cnt-2UL ]; tile_to_cpu[ cnt-2UL ] = tile_to_cpu[ cnt-1UL ]; tile_to_cpu[ cnt-1UL ] = t; }
243 0 : }
244 :
245 0 : return cnt;
246 0 : }
247 :
248 0 : #define FD_TOPOB_LIVE_ALWAYS (1)
249 0 : #define FD_TOPOB_LIVE_STARTUP (2)
250 0 : #define FD_TOPOB_LIVE_POSTSTART (3)
251 :
252 : static int
253 0 : fd_topob_tile_live_phase( fd_topo_tile_t const * tile ) {
254 0 : uint cls = fd_topob_tile_class( tile->name );
255 0 : if( ( cls & FD_TOPOB_TILE_STARTUP ) || tile->allow_shutdown ) return FD_TOPOB_LIVE_STARTUP;
256 0 : if( cls & FD_TOPOB_TILE_POSTSTART ) return FD_TOPOB_LIVE_POSTSTART;
257 0 : return FD_TOPOB_LIVE_ALWAYS;
258 0 : }
259 :
260 : int
261 : fd_topob_cpu_overlap_allowed( fd_topo_tile_t const * a,
262 : fd_topo_tile_t const * b,
263 0 : int floats_share ) {
264 0 : if( sock_lo_net_tile_id( a )==b->id || sock_lo_net_tile_id( b )==a->id ) return 1;
265 0 : if( floats_share && a->floats && b->floats ) return 1;
266 :
267 0 : int a_phase = fd_topob_tile_live_phase( a );
268 0 : int b_phase = fd_topob_tile_live_phase( b );
269 :
270 0 : return ( a_phase==FD_TOPOB_LIVE_STARTUP && b_phase==FD_TOPOB_LIVE_POSTSTART ) ||
271 0 : ( a_phase==FD_TOPOB_LIVE_POSTSTART && b_phase==FD_TOPOB_LIVE_STARTUP );
272 0 : }
273 :
274 : void
275 0 : fd_topob_validate_cpu_overlaps( fd_topo_t const * topo ) {
276 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
277 0 : fd_topo_tile_t const * a = &topo->tiles[ i ];
278 0 : if( a->cpu_idx>=FD_TILE_MAX ) continue;
279 :
280 0 : for( ulong j=i+1UL; j<topo->tile_cnt; j++ ) {
281 0 : fd_topo_tile_t const * b = &topo->tiles[ j ];
282 0 : if( b->cpu_idx!=a->cpu_idx ) continue;
283 0 : if( FD_LIKELY( fd_topob_cpu_overlap_allowed( a, b, 1 ) ) ) continue;
284 :
285 0 : FD_LOG_ERR(( "tile `%s:%lu` and tile `%s:%lu` are both assigned to CPU %lu and may try to run at the same time",
286 0 : a->name, a->kind_id, b->name, b->kind_id, a->cpu_idx ));
287 0 : }
288 0 : }
289 0 : }
290 :
291 : static char const *
292 : offline_note( fd_topo_cpus_t const * cpus,
293 0 : char buf[ static 128 ] ) {
294 0 : ulong offline_cnt = 0UL;
295 0 : ulong core_cnt = 0UL;
296 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
297 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ i ];
298 0 : offline_cnt += (ulong)!cpu->online;
299 0 : core_cnt += (ulong)( cpu->online && ( cpu->sibling==ULONG_MAX || cpu->sibling>i ) );
300 0 : }
301 0 : if( FD_LIKELY( !offline_cnt ) ) return "";
302 0 : return fd_cstr_printf( buf, 128UL, NULL, " (%lu CPUs are offline, leaving %lu online physical cores; reduce tile counts)", offline_cnt, core_cnt );
303 0 : }
304 :
305 : /* iso tier test: 3 wants an isolated CPU with a non-isolated
306 : sibling, 2 one whose free threads are all isolated, 1 an isolated
307 : CPU, -1 a non-isolated one, 0 any */
308 :
309 : static int
310 : iso_mismatch( fd_topo_cpus_t const * cpus,
311 : ulong cpu_idx,
312 0 : int iso ) {
313 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ cpu_idx ];
314 0 : if( iso==3 ) return !cpu->isolated || cpu->sibling==ULONG_MAX || cpus->cpu[ cpu->sibling ].isolated;
315 0 : if( iso==2 ) return !cpu->isolated || ( cpu->sibling!=ULONG_MAX && !cpus->cpu[ cpu->sibling ].isolated );
316 0 : return iso && (iso>0)!=!!cpu->isolated;
317 0 : }
318 :
319 : /* Free CPUs by core type: both threads free (NN, NI, II by isolated
320 : thread count), sibling absent (N1, I1), sibling taken (NH, IH) */
321 :
322 0 : #define CT_NN 0
323 0 : #define CT_NI 1
324 0 : #define CT_II 2
325 0 : #define CT_N1 3
326 0 : #define CT_I1 4
327 0 : #define CT_NH 5
328 0 : #define CT_IH 6
329 :
330 : /* Counts core types as if cpu_id (and its sibling if whole_core) were
331 : taken, cpu_id ULONG_MAX for none */
332 :
333 : static void
334 : core_type_cnt( fd_topo_cpus_t const * cpus,
335 : cpu_bv_t const cpu_assigned[ static cpu_bv_word_cnt ],
336 : ulong cpu_id,
337 : int whole_core,
338 0 : ulong cnt[ static 7 ] ) {
339 0 : cpu_bv_t taken[ cpu_bv_word_cnt ];
340 0 : cpu_bv_copy( taken, cpu_assigned );
341 0 : if( cpu_id!=ULONG_MAX ) {
342 0 : cpu_bv_insert( taken, cpu_id );
343 0 : if( whole_core && cpus->cpu[ cpu_id ].sibling!=ULONG_MAX ) cpu_bv_insert( taken, cpus->cpu[ cpu_id ].sibling );
344 0 : }
345 :
346 0 : for( ulong t=0UL; t<7UL; t++ ) cnt[ t ] = 0UL;
347 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
348 0 : if( cpu_bv_test( taken, i ) ) continue; /* offline CPUs are taken */
349 0 : ulong s = cpus->cpu[ i ].sibling;
350 0 : ulong iso = (ulong)!!cpus->cpu[ i ].isolated;
351 0 : if( s==ULONG_MAX ) cnt[ CT_N1+iso ]++;
352 0 : else if( cpu_bv_test( taken, s ) ) cnt[ CT_NH+iso ]++;
353 0 : else if( s>i ) cnt[ CT_NN+iso+(ulong)!!cpus->cpu[ s ].isolated ]++;
354 0 : }
355 0 : }
356 :
357 : /* Exact test that the tiles left still have CPUs. dem[ phase*3+kind ]
358 : counts them by phase (always, startup, post-start) and kind
359 : (critical: whole core, pinned: any CPU, floater: non-isolated CPU).
360 : Each of the left critical tiles tries each core type, always ones
361 : first; a post-start one lends its core's threads (qi isolated, qn
362 : not) to startup tiles, which never run beside it. */
363 :
364 : static int
365 : plan_fits( ulong cnt[ static 7 ],
366 : ulong const dem[ static 9 ],
367 : ulong left,
368 : ulong qi,
369 0 : ulong qn ) {
370 0 : static uchar const core_ty [ 5 ] = { CT_I1, CT_II, CT_N1, CT_NI, CT_NN };
371 0 : static uchar const core_iso[ 5 ] = { 0, 1, 2, 0, 1 }; /* threads of CT_NN..CT_I1 */
372 0 : static uchar const core_non[ 5 ] = { 2, 1, 0, 1, 0 };
373 0 : if( left ) {
374 0 : int post = left<=dem[ 6 ];
375 0 : for( ulong t=0UL; t<5UL; t++ ) {
376 0 : ulong ty = core_ty[ t ];
377 0 : if( !cnt[ ty ] ) continue;
378 0 : cnt[ ty ]--;
379 0 : int ok = plan_fits( cnt, dem, left-1UL, qi+(post ? core_iso[ ty ] : 0UL), qn+(post ? core_non[ ty ] : 0UL) );
380 0 : cnt[ ty ]++;
381 0 : if( ok ) return 1;
382 0 : }
383 0 : return 0;
384 0 : }
385 0 : ulong i = cnt[ CT_NI ] + 2UL*cnt[ CT_II ] + cnt[ CT_I1 ] + cnt[ CT_IH ];
386 0 : ulong n = cnt[ CT_NI ] + 2UL*cnt[ CT_NN ] + cnt[ CT_N1 ] + cnt[ CT_NH ];
387 0 : return i+n >=dem[ 1 ]+dem[ 2 ]+dem[ 7 ]+dem[ 8 ] && n >=dem[ 2 ]+dem[ 8 ] &&
388 0 : i+n+qi+qn >=dem[ 1 ]+dem[ 2 ]+dem[ 4 ]+dem[ 5 ] && n+qn>=dem[ 2 ]+dem[ 5 ]+fd_ulong_sat_sub( dem[ 1 ], i );
389 0 : }
390 :
391 : /* First free CPU from cpu_idx in tier iso (whole_core: with a free
392 : sibling) that keeps the plan if plan is set. Tier 2, and a
393 : dedicated or floater whole core idling an isolated sibling, also
394 : need the isolated slots left to cover the pinned tiles left. */
395 :
396 : static ulong
397 : auto_tile_cpu_scan( fd_topo_cpus_t const * cpus,
398 : cpu_bv_t const cpu_assigned[ static cpu_bv_word_cnt ],
399 : ushort const cpu_ordering[ static FD_TILE_MAX ],
400 : ulong cpu_idx,
401 : int iso,
402 : int whole_core,
403 : int is_dedicated,
404 : _Bool skip_ht_pairs,
405 : int plan,
406 0 : ulong const dem[ static 9 ] ) {
407 0 : for( ulong j=cpu_idx; j<cpus->cpu_cnt; j++ ) {
408 0 : ulong cpu_id = cpu_ordering[ j ];
409 0 : ulong sibling = cpus->cpu[ cpu_id ].sibling;
410 0 : if( cpu_bv_test( cpu_assigned, cpu_id ) || iso_mismatch( cpus, cpu_id, iso ) ) continue;
411 0 : if( whole_core && sibling!=ULONG_MAX && cpu_bv_test( cpu_assigned, sibling ) ) continue;
412 0 : if( !plan ) return cpu_id;
413 :
414 0 : ulong cnt[ 7 ];
415 0 : core_type_cnt( cpus, cpu_assigned, cpu_id, whole_core, cnt );
416 0 : if( !plan_fits( cnt, dem, dem[ 0 ]+dem[ 6 ], 0UL, 0UL ) ) continue;
417 0 : int guard = iso==2 || ( ( is_dedicated || iso<0 ) && whole_core && sibling!=ULONG_MAX && cpus->cpu[ sibling ].isolated );
418 0 : if( !guard ) return cpu_id;
419 0 : ulong iso_slots = skip_ht_pairs ? cnt[ CT_NI ]+cnt[ CT_II ]+cnt[ CT_I1 ] : cnt[ CT_NI ]+2UL*cnt[ CT_II ]+cnt[ CT_I1 ]+cnt[ CT_IH ];
420 0 : if( iso_slots>=dem[ 0 ]+dem[ 1 ]+fd_ulong_max( dem[ 3 ]+dem[ 4 ], dem[ 6 ]+dem[ 7 ] ) ) return cpu_id;
421 0 : }
422 0 : return ULONG_MAX;
423 0 : }
424 :
425 : /* In efficient mode pinned tiles take isolated CPUs first and floaters
426 : never do. While the tiles left fit, a CPU is only taken if they
427 : still fit after it, and the optional isolated preference and
428 : dedicated whole core relax rather than abort. Pinned non-critical
429 : tiles first skip all-isolated cores the critical tiles left need. */
430 :
431 : static void
432 : auto_tile_cpu( fd_topo_tile_t * tile,
433 : fd_topo_cpus_t * cpus,
434 : ulong * cpu_idx_p,
435 : cpu_bv_t cpu_assigned[ static cpu_bv_word_cnt ],
436 : ushort const cpu_ordering[ static FD_TILE_MAX ],
437 : _Bool skip_ht_pairs,
438 : ulong * spare_siblings,
439 : int efficient,
440 : ulong dem[ static 9 ],
441 0 : ulong phase ) {
442 0 : ulong cpu_idx = *cpu_idx_p;
443 :
444 0 : ulong cpu_cnt = cpus->cpu_cnt;
445 0 : while( cpu_idx<cpu_cnt && cpu_bv_test( cpu_assigned, cpu_ordering[ cpu_idx ] ) ) cpu_idx++;
446 :
447 : /* Certain tiles are latency and throughput critical and
448 : should not get a HT pair assigned. */
449 0 : uint cls = fd_topob_tile_class( tile->name );
450 0 : int crit = !!( cls & FD_TOPOB_TILE_SMT_CRITICAL );
451 0 : int pinned = !!( cls & FD_TOPOB_TILE_EFFICIENT_PINNED );
452 0 : ulong cnt[ 7 ];
453 0 : core_type_cnt( cpus, cpu_assigned, ULONG_MAX, 0, cnt );
454 0 : int plan = efficient && plan_fits( cnt, dem, dem[ 0 ]+dem[ 6 ], 0UL, 0UL );
455 0 : dem[ phase*3UL + ( crit ? 0UL : pinned ? 1UL : 2UL ) ]--;
456 0 : int iso_hi = efficient ? ( pinned ? 1+( crit && plan ) : -1 ) : 0;
457 0 : int iso_lo = efficient && !pinned ? -1 : 0;
458 0 : ulong crit_cores = cnt[ CT_II ]+cnt[ CT_I1 ];
459 0 : if( plan && pinned && !crit && crit_cores && crit_cores<=dem[ 0 ]+dem[ 3 ]+dem[ 6 ] &&
460 0 : cnt[ CT_NI ]+cnt[ CT_IH ]>=dem[ 1 ]+fd_ulong_max( dem[ 4 ], dem[ 7 ] )+1UL ) iso_hi = 3;
461 :
462 0 : ulong cpu_id = ULONG_MAX;
463 0 : int whole_core = 0;
464 0 : int tier_free = 0;
465 0 : for( int iso=iso_hi; iso>=iso_lo && cpu_id==ULONG_MAX; iso-=1+( iso==3 ) ) {
466 0 : ulong first = cpu_idx;
467 0 : while( first<cpu_cnt && ( cpu_bv_test( cpu_assigned, cpu_ordering[ first ] ) || iso_mismatch( cpus, cpu_ordering[ first ], iso ) ) ) first++;
468 0 : tier_free = first<cpu_cnt;
469 0 : if( !tier_free ) continue;
470 :
471 0 : int is_dedicated = !crit && *spare_siblings && ( cls & FD_TOPOB_TILE_DEDICATED ) && cpus->cpu[ cpu_ordering[ first ] ].sibling!=ULONG_MAX;
472 0 : whole_core = crit || is_dedicated || skip_ht_pairs;
473 0 : if( whole_core ) {
474 0 : cpu_id = auto_tile_cpu_scan( cpus, cpu_assigned, cpu_ordering, cpu_idx, iso, 1, is_dedicated, skip_ht_pairs, plan, dem );
475 0 : if( cpu_id!=ULONG_MAX ) { *spare_siblings -= (ulong)is_dedicated; break; }
476 0 : if( !is_dedicated ) continue;
477 0 : *spare_siblings = 0UL; /* spare logical CPUs can be half cores */
478 0 : whole_core = 0;
479 0 : }
480 0 : cpu_id = auto_tile_cpu_scan( cpus, cpu_assigned, cpu_ordering, cpu_idx, iso, 0, 0, skip_ht_pairs, plan, dem );
481 0 : }
482 0 : if( FD_UNLIKELY( cpu_id==ULONG_MAX ) ) {
483 0 : char note[ 128 ];
484 0 : if( tier_free ) FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned or have a HT pair assigned%s", tile->name, tile->kind_id, offline_note( cpus, note ) ));
485 0 : FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned%s%s", tile->name, tile->kind_id,
486 0 : iso_lo ? " or isolated by isolcpus=" : "", offline_note( cpus, note ) ));
487 0 : }
488 :
489 0 : cpu_bv_insert( cpu_assigned, cpu_id );
490 0 : if( whole_core && cpus->cpu[ cpu_id ].sibling!=ULONG_MAX ) cpu_bv_insert( cpu_assigned, cpus->cpu[ cpu_id ].sibling );
491 0 : tile->cpu_idx = cpu_id;
492 :
493 0 : *cpu_idx_p = cpu_idx;
494 0 : }
495 :
496 : static ulong
497 : l3_free_core_cnt( fd_topo_cpus_t const * cpus,
498 : cpu_bv_t const cpu_assigned[ static cpu_bv_word_cnt ],
499 : ulong l3_idx,
500 0 : int iso ) {
501 0 : ulong cnt = 0UL;
502 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
503 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ i ];
504 0 : if( cpu->l3_idx!=l3_idx || cpu_bv_test( cpu_assigned, i ) || iso_mismatch( cpus, i, iso ) ) continue;
505 0 : if( cpu->sibling!=ULONG_MAX && ( cpu_bv_test( cpu_assigned, cpu->sibling ) ||
506 0 : ( cpu->sibling<i && !iso_mismatch( cpus, cpu->sibling, iso ) ) ) ) continue; /* core counted once */
507 0 : cnt++;
508 0 : }
509 0 : return cnt;
510 0 : }
511 :
512 : /* Place a hot tile on the first free physical core of L3 domain
513 : *hot_l3, sibling left empty. When it is full, the domain with the
514 : most free cores becomes the hot one. The l3_layout gate leaves a
515 : whole core for every tile, so this never needs the plan. */
516 :
517 : static void
518 : auto_tile_cpu_l3( fd_topo_tile_t * tile,
519 : fd_topo_cpus_t * cpus,
520 : ulong * hot_l3,
521 : cpu_bv_t cpu_assigned[ static cpu_bv_word_cnt ],
522 : ushort const cpu_ordering[ static FD_TILE_MAX ],
523 0 : ulong dem[ static 9 ] ) {
524 0 : uint cls = fd_topob_tile_class( tile->name );
525 0 : int pinned = !!( cls & FD_TOPOB_TILE_EFFICIENT_PINNED );
526 0 : dem[ ( ( cls & FD_TOPOB_TILE_POSTSTART ) ? 6UL : 0UL ) + ( pinned ? 1UL : 2UL ) ]--; /* hot tiles are not critical */
527 0 : int iso = pinned ? 1 : -1;
528 0 : retry:
529 0 : if( *hot_l3==ULONG_MAX || !l3_free_core_cnt( cpus, cpu_assigned, *hot_l3, iso ) ) {
530 0 : ulong best_cnt = 0UL;
531 0 : for( ulong l3=0UL; l3<cpus->l3_cnt; l3++ ) {
532 0 : ulong cnt = l3_free_core_cnt( cpus, cpu_assigned, l3, iso );
533 0 : if( cnt>best_cnt ) { best_cnt = cnt; *hot_l3 = l3; }
534 0 : }
535 0 : if( !best_cnt && iso>0 ) { iso = 0; goto retry; }
536 0 : char note[ 128 ];
537 0 : if( FD_UNLIKELY( !best_cnt ) ) FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned or have a HT pair assigned%s", tile->name, tile->kind_id, offline_note( cpus, note ) ));
538 0 : }
539 :
540 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
541 0 : ulong cpu_idx = cpu_ordering[ i ];
542 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ cpu_idx ];
543 0 : if( cpu->l3_idx!=*hot_l3 || cpu_bv_test( cpu_assigned, cpu_idx ) || iso_mismatch( cpus, cpu_idx, iso ) ) continue;
544 0 : if( cpu->sibling!=ULONG_MAX && cpu_bv_test( cpu_assigned, cpu->sibling ) ) continue;
545 0 : cpu_bv_insert( cpu_assigned, cpu_idx );
546 0 : if( cpu->sibling!=ULONG_MAX ) cpu_bv_insert( cpu_assigned, cpu->sibling );
547 0 : tile->cpu_idx = cpu_idx;
548 0 : return;
549 0 : }
550 0 : FD_LOG_ERR(( "auto layout found no free core in L3 %lu for tile `%s:%lu`", *hot_l3, tile->name, tile->kind_id ));
551 0 : }
552 :
553 : #undef CT_NN
554 : #undef CT_NI
555 : #undef CT_II
556 : #undef CT_N1
557 : #undef CT_I1
558 : #undef CT_NH
559 : #undef CT_IH
560 :
561 : static ulong
562 : name_idx( char const * name,
563 0 : char const * const * names ) {
564 0 : for( ulong i=0UL; names[ i ]; i++ ) if( !strcmp( name, names[ i ] ) ) return i;
565 0 : return ULONG_MAX;
566 0 : }
567 :
568 : /* Whole core rank of a steady tile, lower first, tier in bits 24+ */
569 :
570 0 : #define SMT_TIER_COLD (7UL)
571 :
572 : static ulong
573 : smt_rank( fd_topo_tile_t const * tile,
574 : ulong execrp_floor,
575 0 : int replay_first ) {
576 0 : static char const * const T4[] = { "votor", "shred", "net", "mlx5", "sock", "accdb", NULL };
577 0 : static char const * const T5[] = { "repair", NULL };
578 0 : static char const * const T6[] = { "execle", "dedup", "sign", "resolv", "quic", "verify", "bundle", "shred", "net", "mlx5", "sock", NULL };
579 0 : static char const * const T7[] = { "gui", "rpc", "event", "rserve", "gossip", "gossvf", NULL };
580 :
581 0 : char const * name = tile->name;
582 0 : ulong k = tile->kind_id;
583 0 : if( fd_topob_tile_class( name ) & FD_TOPOB_TILE_SMT_CRITICAL ) return k;
584 0 : if( !strcmp( name, "replay" ) ) return replay_first ? 1UL<<24 : 2UL<<24;
585 0 : if( !strcmp( name, "execrp" ) ) return ( k>=execrp_floor ? 3UL<<24 : replay_first ? 2UL<<24 : 1UL<<24 ) | k;
586 0 : if( !strcmp( name, "sign" ) && k ) return (SMT_TIER_COLD<<24) | (4095UL<<12) | k;
587 0 : ulong i;
588 0 : if( !k && (i=name_idx( name, T4 ))!=ULONG_MAX ) return (4UL<<24) | (i<<12);
589 0 : if( (i=name_idx( name, T5 ))!=ULONG_MAX ) return (5UL<<24) | (i<<12) | k;
590 0 : if( (i=name_idx( name, T6 ))!=ULONG_MAX ) return (6UL<<24) | (i<<12) | k;
591 0 : if( (i=name_idx( name, T7 ))!=ULONG_MAX ) return (SMT_TIER_COLD<<24) | (i<<12) | k;
592 0 : return (6UL<<24) | (4094UL<<12) | k;
593 0 : }
594 :
595 0 : #define SMT_WHOLE (0)
596 0 : #define SMT_HALF (1)
597 0 : #define SMT_PAIR (2)
598 :
599 : /* Shares only as many cores as needed, lowest ranked tiles first.
600 : Returns 0 if the tiles do not fit or a critical tile would pair. */
601 :
602 : static int
603 : smt_plan( fd_topo_t const * topo,
604 : fd_topo_cpus_t const * cpus,
605 : cpu_bv_t const assigned[ static cpu_bv_word_cnt ],
606 : ulong const * tile_idx,
607 : ulong cnt,
608 : ulong execrp_floor,
609 : int efficient,
610 : uchar * mode,
611 0 : ulong * partner ) {
612 0 : static char const * const AVX[] = { "verify", "sign", "shred", "benchg", NULL };
613 :
614 0 : ulong p2 = 0UL, p1 = 0UL, half = 0UL;
615 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
616 0 : if( cpu_bv_test( assigned, i ) ) continue;
617 0 : ulong s = cpus->cpu[ i ].sibling;
618 0 : if( s==ULONG_MAX ) p1++;
619 0 : else if( !cpu_bv_test( assigned, s ) ) p2 += (ulong)( s>i );
620 0 : else half++;
621 0 : }
622 0 : ulong h = fd_ulong_min( half, fd_ulong_sat_sub( cnt, p2+p1 ) );
623 0 : ulong d = fd_ulong_sat_sub( cnt, p2+p1+half );
624 0 : if( FD_UNLIKELY( d>p2 ) ) return 0;
625 0 : ulong whole = cnt - h - 2UL*d;
626 :
627 0 : ulong top = 0UL;
628 0 : for( ulong i=0UL; i<cnt; i++ ) {
629 0 : fd_topo_tile_t const * tile = &topo->tiles[ tile_idx[ i ] ];
630 0 : top += (ulong)( ( fd_topob_tile_class( tile->name ) & FD_TOPOB_TILE_SMT_CRITICAL ) || !strcmp( tile->name, "replay" ) ||
631 0 : ( !strcmp( tile->name, "execrp" ) && tile->kind_id<execrp_floor ) );
632 0 : }
633 0 : int replay_first = whole>=top;
634 :
635 0 : ulong order[ FD_TOPO_MAX_TILES ];
636 0 : ulong key [ FD_TOPO_MAX_TILES ];
637 0 : FD_TEST( cnt<=FD_TOPO_MAX_TILES );
638 0 : for( ulong i=0UL; i<cnt; i++ ) {
639 0 : ulong k = smt_rank( &topo->tiles[ tile_idx[ i ] ], execrp_floor, replay_first );
640 0 : ulong j = i;
641 0 : for( ; j && key[ j-1UL ]>k; j-- ) { key[ j ] = key[ j-1UL ]; order[ j ] = order[ j-1UL ]; }
642 0 : key[ j ] = k; order[ j ] = i;
643 0 : }
644 0 : for( ulong j=whole; j<cnt; j++ ) {
645 0 : if( FD_UNLIKELY( !(key[ j ]>>24) ) ) return 0;
646 0 : }
647 :
648 0 : for( ulong j=0UL; j<cnt; j++ ) {
649 0 : mode [ order[ j ] ] = (uchar)( j<whole ? SMT_WHOLE : j<whole+h ? SMT_HALF : SMT_PAIR );
650 0 : partner[ order[ j ] ] = ULONG_MAX;
651 0 : }
652 :
653 0 : for( ulong pass=0UL; pass<6UL; pass++ ) {
654 0 : for( ulong a=whole+h; a<cnt; a++ ) {
655 0 : if( partner[ order[ a ] ]!=ULONG_MAX ) continue;
656 0 : fd_topo_tile_t const * ta = &topo->tiles[ tile_idx[ order[ a ] ] ];
657 0 : for( ulong b=cnt-1UL; b>a; b-- ) {
658 0 : if( partner[ order[ b ] ]!=ULONG_MAX ) continue;
659 0 : fd_topo_tile_t const * tb = &topo->tiles[ tile_idx[ order[ b ] ] ];
660 0 : int hot_a = (key[ a ]>>24)<=4UL && !( fd_topob_tile_class( ta->name ) & FD_TOPOB_TILE_DEDICATED );
661 0 : int cold_a = (key[ a ]>>24)==SMT_TIER_COLD;
662 0 : int cold_b = (key[ b ]>>24)==SMT_TIER_COLD;
663 0 : int same = !strcmp( ta->name, tb->name );
664 0 : int avx = !same && name_idx( ta->name, AVX )!=ULONG_MAX && name_idx( tb->name, AVX )!=ULONG_MAX;
665 0 : int pin_a = !!( fd_topob_tile_class( ta->name ) & FD_TOPOB_TILE_EFFICIENT_PINNED );
666 0 : int pin_b = !!( fd_topob_tile_class( tb->name ) & FD_TOPOB_TILE_EFFICIENT_PINNED );
667 0 : int strict = !avx && ( !efficient || pin_a==pin_b );
668 0 : int ok;
669 0 : switch( pass ) { /* a hot tile that must share gets a cold sibling */
670 0 : case 0UL: ok = strict && hot_a && cold_b; break;
671 0 : case 1UL: ok = strict && cold_a && cold_b; break;
672 0 : case 2UL: ok = strict && same; break;
673 0 : case 3UL: ok = strict && cold_b; break;
674 0 : case 4UL: ok = strict; break;
675 0 : default: ok = 1; break;
676 0 : }
677 0 : if( !ok ) continue;
678 0 : if( FD_UNLIKELY( pass==5UL && avx ) ) FD_LOG_WARNING(( "auto layout is short of physical cores and puts AVX heavy tile `%s:%lu` on the hyperthread sibling of tile `%s:%lu`",
679 0 : tb->name, tb->kind_id, ta->name, ta->kind_id ));
680 0 : partner[ order[ a ] ] = order[ b ];
681 0 : partner[ order[ b ] ] = order[ a ];
682 0 : break;
683 0 : }
684 0 : }
685 0 : }
686 0 : return 1;
687 0 : }
688 :
689 : /* Places plan entries [lo,hi) in CPU order */
690 :
691 : static void
692 : smt_place( fd_topo_t * topo,
693 : fd_topo_cpus_t const * cpus,
694 : cpu_bv_t assigned[ static cpu_bv_word_cnt ],
695 : ushort const cpu_ordering[ static FD_TILE_MAX ],
696 : ulong const * tile_idx,
697 : ulong lo,
698 : ulong hi,
699 : ulong cnt,
700 : uchar * mode,
701 : ulong * partner,
702 0 : int efficient ) {
703 0 : ulong p2_free = 0UL;
704 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
705 0 : ulong s = cpus->cpu[ i ].sibling;
706 0 : p2_free += (ulong)( s!=ULONG_MAX && s>i && !cpu_bv_test( assigned, i ) && !cpu_bv_test( assigned, s ) );
707 0 : }
708 0 : ulong pairs_left = 0UL;
709 0 : for( ulong i=0UL; i<cnt; i++ ) pairs_left += (ulong)( mode[ i ]==SMT_PAIR && partner[ i ]>i && partner[ i ]!=ULONG_MAX &&
710 0 : topo->tiles[ tile_idx[ i ] ].cpu_idx==ULONG_MAX );
711 :
712 0 : for( ulong i=lo; i<hi; i++ ) {
713 0 : fd_topo_tile_t * tile = &topo->tiles[ tile_idx[ i ] ];
714 0 : if( tile->cpu_idx!=ULONG_MAX ) continue;
715 0 : int avoid = efficient && !( fd_topob_tile_class( tile->name ) & FD_TOPOB_TILE_EFFICIENT_PINNED );
716 0 : int pair = mode[ i ]==SMT_PAIR && partner[ i ]!=ULONG_MAX;
717 0 : fd_topo_tile_t * mate = pair ? &topo->tiles[ tile_idx[ partner[ i ] ] ] : NULL;
718 0 : int mate_avoid = pair && efficient && !( fd_topob_tile_class( mate->name ) & FD_TOPOB_TILE_EFFICIENT_PINNED );
719 :
720 0 : ulong pick = ULONG_MAX;
721 0 : ulong any = ULONG_MAX;
722 0 : for( ulong j=0UL; j<cpus->cpu_cnt && pick==ULONG_MAX; j++ ) {
723 0 : ulong c = cpu_ordering[ j ];
724 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ c ];
725 0 : if( cpu_bv_test( assigned, c ) || ( avoid && cpu->isolated ) ) continue;
726 0 : if( any==ULONG_MAX ) any = c;
727 0 : ulong s = cpu->sibling;
728 0 : int sib_free = s!=ULONG_MAX && !cpu_bv_test( assigned, s );
729 0 : int ok;
730 0 : if( pair ) ok = sib_free && !( mate_avoid && cpus->cpu[ s ].isolated );
731 0 : else if( mode[ i ]==SMT_HALF ) ok = s!=ULONG_MAX && !sib_free;
732 0 : else ok = s==ULONG_MAX || ( sib_free && p2_free>pairs_left );
733 0 : if( ok ) pick = c;
734 0 : }
735 :
736 0 : if( FD_UNLIKELY( pick==ULONG_MAX ) ) {
737 0 : if( FD_UNLIKELY( any==ULONG_MAX ) ) {
738 0 : char note[ 128 ];
739 0 : FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned%s%s", tile->name, tile->kind_id,
740 0 : avoid ? " or isolated by isolcpus=" : "", offline_note( cpus, note ) ));
741 0 : }
742 0 : if( pair ) { /* the mate is placed on its own */
743 0 : mode [ partner[ i ] ] = SMT_HALF;
744 0 : partner[ partner[ i ] ] = ULONG_MAX;
745 0 : pairs_left--;
746 0 : }
747 0 : pick = any;
748 0 : tile->cpu_idx = pick;
749 0 : cpu_bv_insert( assigned, pick );
750 0 : ulong s = cpus->cpu[ pick ].sibling;
751 0 : if( s!=ULONG_MAX && !cpu_bv_test( assigned, s ) ) p2_free--;
752 0 : continue;
753 0 : }
754 :
755 0 : ulong s = cpus->cpu[ pick ].sibling;
756 0 : tile->cpu_idx = pick;
757 0 : cpu_bv_insert( assigned, pick );
758 0 : if( mode[ i ]==SMT_HALF || s==ULONG_MAX ) continue;
759 0 : cpu_bv_insert( assigned, s );
760 0 : p2_free--;
761 0 : if( pair ) { mate->cpu_idx = s; pairs_left--; }
762 0 : }
763 0 : }
764 :
765 : /* Whole cores free during snapshot load for startup tiles, snapin
766 : first, preferring execrp's cores */
767 :
768 : static void
769 : auto_startup_cpus( fd_topo_t * topo,
770 : fd_topo_cpus_t const * cpus,
771 : cpu_bv_t snap[ static cpu_bv_word_cnt ],
772 : ushort const cpu_ordering[ static FD_TILE_MAX ],
773 0 : int efficient ) {
774 0 : static char const * const ORDER[] = { "snapin", "snapdc", "snapld", "snapct", NULL };
775 :
776 0 : ulong idx[ FD_TOPO_MAX_TILES ];
777 0 : ulong cnt = 0UL;
778 0 : for( ulong r=0UL; r<4UL+TILE_CLASS_CNT; r++ ) {
779 0 : char const * name = r<4UL ? ORDER[ r ] : TILE_CLASS[ r-4UL ].name;
780 0 : if( r>=4UL && ( !( TILE_CLASS[ r-4UL ].flags & FD_TOPOB_TILE_STARTUP ) || name_idx( name, ORDER )!=ULONG_MAX ) ) continue;
781 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
782 0 : if( tile_is_floating( &topo->tiles[ j ] ) || strcmp( topo->tiles[ j ].name, name ) ) continue;
783 0 : idx[ cnt++ ] = j;
784 0 : }
785 0 : }
786 0 : if( !cnt ) return;
787 :
788 0 : cpu_bv_t banned[ cpu_bv_word_cnt ]; cpu_bv_new( banned );
789 0 : cpu_bv_t execrp[ cpu_bv_word_cnt ]; cpu_bv_new( execrp );
790 0 : for( ulong i=0UL; i<topo->blocklist_cores_cnt; i++ ) cpu_bv_insert( banned, topo->blocklist_cores_cpu_idx[ i ] );
791 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) if( !cpus->cpu[ i ].online ) cpu_bv_insert( banned, i );
792 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
793 0 : fd_topo_tile_t const * tile = &topo->tiles[ j ];
794 0 : ulong c = tile->cpu_idx;
795 0 : if( c>=cpus->cpu_cnt ) continue;
796 0 : uint cls = fd_topob_tile_class( tile->name );
797 0 : ulong s = cpus->cpu[ c ].sibling;
798 0 : if( !strcmp( tile->name, "execrp" ) ) { cpu_bv_insert( execrp, c ); if( s!=ULONG_MAX ) cpu_bv_insert( execrp, s ); }
799 0 : if( !( cls & FD_TOPOB_TILE_ALWAYS ) ) continue;
800 0 : cpu_bv_insert( banned, c );
801 0 : if( ( cls & FD_TOPOB_TILE_SMT_CRITICAL ) && s!=ULONG_MAX ) cpu_bv_insert( banned, s );
802 0 : }
803 :
804 0 : ulong free_cnt = 0UL;
805 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) free_cnt += (ulong)!cpu_bv_test( snap, i );
806 0 : ulong spare = fd_ulong_sat_sub( free_cnt, cnt ); /* siblings that can idle */
807 :
808 0 : for( ulong k=0UL; k<cnt; k++ ) {
809 0 : fd_topo_tile_t * tile = &topo->tiles[ idx[ k ] ];
810 0 : int avoid = efficient && !( fd_topob_tile_class( tile->name ) & FD_TOPOB_TILE_EFFICIENT_PINNED );
811 0 : ulong pick = ULONG_MAX;
812 0 : for( ulong pass=0UL; pass<4UL && pick==ULONG_MAX; pass++ ) {
813 0 : for( ulong j=0UL; j<cpus->cpu_cnt && pick==ULONG_MAX; j++ ) {
814 0 : ulong c = cpu_ordering[ j ];
815 0 : ulong s = cpus->cpu[ c ].sibling;
816 0 : if( avoid && cpus->cpu[ c ].isolated ) continue;
817 0 : switch( pass ) {
818 0 : case 0UL:
819 0 : case 1UL: if( cpu_bv_test( snap, c ) || ( s!=ULONG_MAX && ( cpu_bv_test( snap, s ) || !spare ) ) || ( !pass && !cpu_bv_test( execrp, c ) ) ) continue; break;
820 0 : case 2UL: if( cpu_bv_test( snap, c ) ) continue; break;
821 0 : default: if( cpu_bv_test( banned, c ) ) continue; break;
822 0 : }
823 0 : pick = c;
824 0 : if( pass<2UL && s!=ULONG_MAX ) { cpu_bv_insert( snap, s ); spare--; }
825 0 : }
826 0 : }
827 0 : if( FD_UNLIKELY( pick==ULONG_MAX ) ) {
828 0 : char note[ 128 ];
829 0 : FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned%s%s", tile->name, tile->kind_id,
830 0 : avoid ? " or isolated by isolcpus=" : "", offline_note( cpus, note ) ));
831 0 : }
832 0 : tile->cpu_idx = pick;
833 0 : cpu_bv_insert( snap, pick );
834 0 : cpu_bv_insert( banned, pick );
835 0 : }
836 0 : }
837 :
838 : void
839 : fd_topob_auto_layout_cpus( fd_topo_t * topo,
840 0 : fd_topo_cpus_t * cpus ) {
841 : /* See the strategy in fd_topob_layout.h */
842 :
843 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
844 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
845 0 : tile->cpu_idx = ULONG_MAX;
846 0 : }
847 :
848 0 : ushort cpu_ordering[ FD_TILE_MAX ] = {0};
849 0 : cpu_bv_t pairs_assigned[ cpu_bv_word_cnt ]; cpu_bv_new( pairs_assigned );
850 0 : FD_STATIC_ASSERT( FD_TILE_MAX<=USHORT_MAX, layout );
851 :
852 0 : ulong next_cpu_idx = 0UL;
853 0 : for( ulong i=0UL; i<cpus->numa_node_cnt; i++ ) {
854 0 : for( ulong j=0UL; j<cpus->cpu_cnt; j++ ) {
855 0 : fd_topo_cpu_t * cpu = &cpus->cpu[ j ];
856 :
857 0 : if( FD_UNLIKELY( cpu_bv_test( pairs_assigned, j ) || cpu->numa_node!=i ) ) continue;
858 :
859 0 : FD_TEST( next_cpu_idx<FD_TILE_MAX );
860 0 : cpu_ordering[ next_cpu_idx++ ] = (ushort)j;
861 :
862 0 : if( FD_UNLIKELY( cpu->sibling!=ULONG_MAX ) ) {
863 : /* If the CPU has a HT pair, place it immediately after so they
864 : are sequentially assigned. */
865 0 : FD_TEST( next_cpu_idx<FD_TILE_MAX );
866 0 : cpu_ordering[ next_cpu_idx++ ] = (ushort)cpu->sibling;
867 0 : cpu_bv_insert( pairs_assigned, cpu->sibling );
868 0 : }
869 0 : }
870 0 : }
871 :
872 0 : FD_TEST( next_cpu_idx==cpus->cpu_cnt );
873 :
874 : /* excluded cpus are simply considered already assigned */
875 0 : cpu_bv_t cpu_assigned[ cpu_bv_word_cnt ];
876 0 : cpu_bv_new( cpu_assigned );
877 0 : for( ulong i=0UL; i<topo->blocklist_cores_cnt; i++ ) {
878 0 : FD_TEST( topo->blocklist_cores_cpu_idx[ i ]<FD_TILE_MAX );
879 0 : cpu_bv_insert( cpu_assigned, topo->blocklist_cores_cpu_idx[ i ] );
880 0 : }
881 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) if( !cpus->cpu[ i ].online ) cpu_bv_insert( cpu_assigned, i );
882 :
883 : /* Compute total number of available physical cores */
884 0 : ulong available_physical = 0UL;
885 0 : ulong available_floater = 0UL; /* no isolated thread */
886 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
887 0 : ulong sibling = cpus->cpu[ i ].sibling;
888 0 : if( !cpu_bv_test( cpu_assigned, i ) &&
889 0 : !cpu_bv_test( pairs_assigned, i ) &&
890 0 : ( sibling==ULONG_MAX ||
891 0 : !cpu_bv_test( cpu_assigned, sibling ) ) ) {
892 0 : available_physical++;
893 0 : available_floater += (ulong)( !cpus->cpu[ i ].isolated && ( sibling==ULONG_MAX || !cpus->cpu[ sibling ].isolated ) );
894 0 : }
895 0 : }
896 :
897 : /* Tiles that need assignment, dem[ phase*3+kind ] by phase (always,
898 : startup, post-start) and kind (SMT critical, other pinned,
899 : efficient mode floater, which skips isolated CPUs) */
900 0 : ulong dem[ 9 ] = {0};
901 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
902 0 : fd_topo_tile_t const * tile = &topo->tiles[ j ];
903 0 : uint cls = fd_topob_tile_class( tile->name );
904 0 : if( !cls || tile_is_floating( tile ) ) continue;
905 0 : ulong phase = ( cls & FD_TOPOB_TILE_STARTUP ) ? 1UL : ( cls & FD_TOPOB_TILE_POSTSTART ) ? 2UL : 0UL;
906 0 : ulong kind = ( cls & FD_TOPOB_TILE_SMT_CRITICAL ) ? 0UL : ( cls & FD_TOPOB_TILE_EFFICIENT_PINNED ) ? 1UL : 2UL;
907 0 : dem[ phase*3UL+kind ]++;
908 0 : }
909 0 : FD_TEST( !dem[ 3 ] ); /* plan_fits has no SMT critical startup tile */
910 0 : ulong always_tiles_to_assign = dem[ 0 ]+dem[ 1 ]+dem[ 2 ];
911 0 : ulong startup_tiles_to_assign = dem[ 3 ]+dem[ 4 ]+dem[ 5 ];
912 0 : ulong post_start_tiles_to_assign = dem[ 6 ]+dem[ 7 ]+dem[ 8 ];
913 0 : ulong tiles_to_assign = always_tiles_to_assign +
914 0 : fd_ulong_max( startup_tiles_to_assign, post_start_tiles_to_assign );
915 0 : ulong floaters_to_assign = dem[ 2 ] + fd_ulong_max( dem[ 5 ], dem[ 8 ] );
916 :
917 : /* L3 packing gives the hot post-start tiles their own cores instead
918 : of sharing with startup tiles, so it needs that many more.
919 : Performance mode, single-L3 hosts, hosts with a partial cache
920 : topology and hosts too small for it keep the sequential layout. */
921 0 : ulong hot_post_start_tiles = 0UL;
922 0 : ulong hot_post_start_floaters = 0UL;
923 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
924 0 : fd_topo_tile_t const * tile = &topo->tiles[ j ];
925 0 : if( tile_is_floating( tile ) ) continue;
926 0 : uint cls = fd_topob_tile_class( tile->name );
927 0 : int hot = ( cls & FD_TOPOB_TILE_POSTSTART ) && tile_name_in( tile->name, HOT_TILES );
928 0 : hot_post_start_tiles += (ulong)hot;
929 0 : hot_post_start_floaters += (ulong)( hot && !( cls & FD_TOPOB_TILE_EFFICIENT_PINNED ) );
930 0 : }
931 0 : ulong l3_tiles_to_assign = always_tiles_to_assign + hot_post_start_tiles +
932 0 : fd_ulong_max( startup_tiles_to_assign, post_start_tiles_to_assign-hot_post_start_tiles );
933 0 : ulong l3_floaters_to_assign = dem[ 2 ] + hot_post_start_floaters +
934 0 : fd_ulong_max( dem[ 5 ], dem[ 8 ]-hot_post_start_floaters );
935 0 : int efficient = topo->sleep_obj_id!=ULONG_MAX;
936 0 : int l3_layout = efficient && fd_topo_cpus_l3_complete( cpus ) &&
937 0 : available_physical>=l3_tiles_to_assign && available_floater>=l3_floaters_to_assign;
938 :
939 : /* If we have enough physical cores (excluding HT siblings) for all
940 : tiles that need assignment, exclude HT siblings so that no tile
941 : gets scheduled on a hyperthread pair. */
942 0 : _Bool skip_ht_pairs = available_physical>=tiles_to_assign && ( !efficient || available_floater>=floaters_to_assign );
943 :
944 : /* Otherwise spend the spare logical CPUs on whole cores for the
945 : dedicated tiles. Each costs its sibling, like a critical tile.
946 : The L3 layout already places them on whole cores. */
947 0 : ulong spare_siblings = 0UL;
948 0 : if( !skip_ht_pairs && !l3_layout ) {
949 0 : ulong free_logical = 0UL;
950 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) free_logical += !cpu_bv_test( cpu_assigned, i );
951 0 : ulong critical_cnt = 0UL;
952 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) critical_cnt += (ulong)!!( fd_topob_tile_class( topo->tiles[ j ].name ) & FD_TOPOB_TILE_SMT_CRITICAL );
953 0 : spare_siblings = fd_ulong_sat_sub( free_logical, tiles_to_assign+critical_cnt );
954 0 : }
955 :
956 : /* Short of physical cores: share only as many as needed, by rank */
957 0 : ulong steady_idx [ FD_TOPO_MAX_TILES ];
958 0 : uchar steady_mode [ FD_TOPO_MAX_TILES ];
959 0 : ulong steady_partner[ FD_TOPO_MAX_TILES ];
960 0 : ulong steady_cnt = 0UL;
961 0 : ulong always_cnt = 0UL;
962 0 : int graded = !skip_ht_pairs && !l3_layout;
963 : /* Efficient mode floaters avoid isolcpus=, which the plan ignores */
964 0 : for( ulong i=0UL; i<cpus->cpu_cnt && graded && efficient; i++ ) graded = cpu_bv_test( cpu_assigned, i ) || !cpus->cpu[ i ].isolated;
965 0 : cpu_bv_t numa_fence[ cpu_bv_word_cnt ]; cpu_bv_new( numa_fence );
966 :
967 : /* Pack, motor, accdb and execle go first into one L3 with the cores the
968 : plan gives them, pairing execle to fit and spilling the highest to
969 : the next L3. The rest is planned around them, or without them if
970 : it then does not fit. */
971 0 : static char const * const GROUP[] = { "pack", "motor", "accdb", "execle", NULL };
972 0 : int group = !efficient && fd_topo_cpus_l3_complete( cpus );
973 0 : cpu_bv_t base [ cpu_bv_word_cnt ]; cpu_bv_copy( base, cpu_assigned );
974 0 : cpu_bv_t group_post[ cpu_bv_word_cnt ]; cpu_bv_new( group_post ); /* free during snapshot load */
975 0 : for( ulong attempt=0UL;; attempt++ ) {
976 0 : if( graded ) {
977 0 : steady_cnt = 0UL;
978 0 : for( ulong r=0UL; r<TILE_CLASS_CNT; r++ ) {
979 0 : if( !( TILE_CLASS[ r ].flags & FD_TOPOB_TILE_ALWAYS ) ) continue;
980 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
981 0 : if( tile_is_floating( &topo->tiles[ j ] ) || topo->tiles[ j ].cpu_idx!=ULONG_MAX || strcmp( topo->tiles[ j ].name, TILE_CLASS[ r ].name ) ) continue;
982 0 : steady_idx[ steady_cnt++ ] = j;
983 0 : }
984 0 : }
985 0 : always_cnt = steady_cnt;
986 0 : for( ulong r=0UL; r<TILE_CLASS_CNT; r++ ) {
987 0 : if( !( TILE_CLASS[ r ].flags & FD_TOPOB_TILE_POSTSTART ) ) continue;
988 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
989 0 : if( topo->tiles[ j ].cpu_idx!=ULONG_MAX || strcmp( topo->tiles[ j ].name, TILE_CLASS[ r ].name ) ) continue;
990 0 : steady_idx[ steady_cnt++ ] = j;
991 0 : }
992 0 : }
993 : /* Fill the first NUMA nodes that fit before crossing to the next */
994 0 : ulong need = steady_cnt;
995 0 : for( ulong i=0UL; i<steady_cnt; i++ ) need += (ulong)!!( fd_topob_tile_class( topo->tiles[ steady_idx[ i ] ].name ) & FD_TOPOB_TILE_SMT_CRITICAL );
996 0 : ulong node_cnt = 0UL;
997 0 : ulong free_cnt = 0UL;
998 0 : for( ; node_cnt<cpus->numa_node_cnt && free_cnt<need; node_cnt++ ) {
999 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) free_cnt += (ulong)( cpus->cpu[ i ].numa_node==node_cnt && !cpu_bv_test( cpu_assigned, i ) );
1000 0 : }
1001 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
1002 0 : if( cpus->cpu[ i ].numa_node>=node_cnt && !cpu_bv_test( cpu_assigned, i ) ) cpu_bv_insert( numa_fence, i );
1003 0 : }
1004 : /* Never fence part of a package, its NUMA nodes share the cores */
1005 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
1006 0 : if( !cpu_bv_test( numa_fence, i ) || cpus->cpu[ i ].package_idx==ULONG_MAX ) continue;
1007 0 : for( ulong j=0UL; j<cpus->cpu_cnt; j++ ) {
1008 0 : if( cpus->cpu[ j ].numa_node<node_cnt && cpus->cpu[ j ].package_idx==cpus->cpu[ i ].package_idx ) { cpu_bv_remove( numa_fence, i ); break; }
1009 0 : }
1010 0 : }
1011 0 : cpu_bv_union( cpu_assigned, cpu_assigned, numa_fence );
1012 0 : graded = smt_plan( topo, cpus, cpu_assigned, steady_idx, steady_cnt, spare_siblings, efficient, steady_mode, steady_partner );
1013 0 : if( !graded ) {
1014 0 : cpu_bv_subtract( cpu_assigned, cpu_assigned, numa_fence );
1015 0 : cpu_bv_new( numa_fence );
1016 0 : graded = smt_plan( topo, cpus, cpu_assigned, steady_idx, steady_cnt, spare_siblings, efficient, steady_mode, steady_partner );
1017 0 : }
1018 0 : }
1019 0 : if( attempt==1UL && !graded ) {
1020 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) topo->tiles[ j ].cpu_idx = ULONG_MAX;
1021 0 : cpu_bv_copy( cpu_assigned, base );
1022 0 : cpu_bv_new( numa_fence );
1023 0 : cpu_bv_new( group_post );
1024 0 : graded = 1;
1025 0 : continue;
1026 0 : }
1027 0 : if( attempt || !group || !( graded || skip_ht_pairs ) ) break;
1028 :
1029 0 : ulong lead = 0UL, ex = 0UL, ew = 0UL; /* cores of pack, motor and accdb, execle, execle on whole cores */
1030 0 : for( ulong i=0UL; i<( graded ? steady_cnt : topo->tile_cnt ); i++ ) {
1031 0 : ulong g = name_idx( topo->tiles[ graded ? steady_idx[ i ] : i ].name, GROUP );
1032 0 : lead += (ulong)( g<3UL );
1033 0 : ex += (ulong)( g==3UL );
1034 0 : ew += (ulong)( g==3UL && ( !graded || steady_mode[ i ]==SMT_WHOLE ) );
1035 0 : }
1036 : /* First L3 in CPU order that fits, else fits execle paired, else the largest */
1037 0 : ulong l3 = ULONG_MAX, l3_cores = 0UL, best = 0UL;
1038 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
1039 0 : ulong k = cpus->cpu[ cpu_ordering[ i ] ].l3_idx;
1040 0 : if( k==ULONG_MAX || ( i && k==cpus->cpu[ cpu_ordering[ i-1UL ] ].l3_idx ) ) continue;
1041 0 : ulong c = l3_free_core_cnt( cpus, cpu_assigned, k, 0 );
1042 0 : ulong score = c>=lead+ew+(ex-ew+1UL)/2UL ? ULONG_MAX : c>=lead+(ex+1UL)/2UL ? ULONG_MAX-1UL : c;
1043 0 : if( score>best ) { best = score; l3 = k; l3_cores = c; }
1044 0 : }
1045 0 : while( ew && lead+ew+(ex-ew+1UL)/2UL>l3_cores ) ew--; /* the lowest execle stay whole */
1046 0 : ulong e = 0UL;
1047 0 : for( ulong g=0UL; GROUP[ g ] && l3!=ULONG_MAX; g++ ) {
1048 0 : for( ulong j=0UL; j<topo->tile_cnt && l3!=ULONG_MAX; j++ ) {
1049 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
1050 0 : if( tile->cpu_idx!=ULONG_MAX || strcmp( tile->name, GROUP[ g ] ) ) continue;
1051 0 : ulong c = ULONG_MAX;
1052 0 : for( ulong d=0UL; d<cpus->l3_cnt && c==ULONG_MAX; d++ ) {
1053 0 : for( ulong i=0UL; i<cpus->cpu_cnt && c==ULONG_MAX; i++ ) {
1054 0 : ulong k = cpu_ordering[ i ];
1055 0 : ulong s = cpus->cpu[ k ].sibling;
1056 0 : if( cpus->cpu[ k ].l3_idx==l3 && !cpu_bv_test( cpu_assigned, k ) && ( s==ULONG_MAX || !cpu_bv_test( cpu_assigned, s ) ) ) c = k;
1057 0 : }
1058 0 : if( c==ULONG_MAX ) l3 = (l3+1UL)%cpus->l3_cnt;
1059 0 : }
1060 0 : if( c==ULONG_MAX ) { l3 = ULONG_MAX; break; } /* the rest is placed as usual */
1061 0 : ulong s = cpus->cpu[ c ].sibling;
1062 0 : ulong m = ULONG_MAX; /* tile on the sibling, which goes to the rest if s is cleared */
1063 0 : if( g<3UL ) {
1064 0 : for( ulong i=0UL; i<steady_cnt && graded; i++ ) { /* as planned */
1065 0 : if( steady_idx[ i ]!=j ) continue;
1066 0 : if( steady_mode[ i ]==SMT_PAIR && steady_partner[ i ]!=ULONG_MAX ) m = steady_idx[ steady_partner[ i ] ];
1067 0 : if( steady_mode[ i ]==SMT_HALF ) s = ULONG_MAX;
1068 0 : }
1069 0 : } else if( e++>=ew ) { /* paired with the next execle */
1070 0 : for( ulong k=j+1UL; k<topo->tile_cnt && m==ULONG_MAX; k++ ) if( topo->tiles[ k ].cpu_idx==ULONG_MAX && !strcmp( topo->tiles[ k ].name, "execle" ) ) m = k;
1071 0 : if( m==ULONG_MAX || s==ULONG_MAX ) s = m = ULONG_MAX;
1072 0 : e += (ulong)( m!=ULONG_MAX );
1073 0 : }
1074 0 : tile->cpu_idx = c;
1075 0 : cpu_bv_insert( cpu_assigned, c );
1076 0 : if( g ) cpu_bv_insert( group_post, c );
1077 0 : if( s==ULONG_MAX ) continue;
1078 0 : cpu_bv_insert( cpu_assigned, s );
1079 0 : if( m!=ULONG_MAX ) topo->tiles[ m ].cpu_idx = s;
1080 0 : if( g && ( m==ULONG_MAX || ( fd_topob_tile_class( topo->tiles[ m ].name ) & FD_TOPOB_TILE_POSTSTART ) ) ) cpu_bv_insert( group_post, s );
1081 0 : }
1082 0 : }
1083 0 : if( !graded ) break;
1084 0 : cpu_bv_subtract( cpu_assigned, cpu_assigned, numa_fence );
1085 0 : cpu_bv_new( numa_fence );
1086 0 : }
1087 :
1088 0 : if( FD_UNLIKELY( l3_layout ) ) {
1089 0 : ulong hot_l3 = ULONG_MAX;
1090 0 : for( char const ** p = HOT_TILES; *p; p++ ) {
1091 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
1092 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
1093 0 : if( tile_is_floating( tile ) || strcmp( tile->name, *p ) ) continue;
1094 0 : auto_tile_cpu_l3( tile, cpus, &hot_l3, cpu_assigned, cpu_ordering, dem );
1095 0 : }
1096 0 : }
1097 0 : }
1098 :
1099 : /* First, assign always-on tiles */
1100 0 : ulong cpu_idx = 0UL;
1101 0 : if( graded ) smt_place( topo, cpus, cpu_assigned, cpu_ordering, steady_idx, 0UL, always_cnt, steady_cnt, steady_mode, steady_partner, efficient );
1102 0 : for( ulong r=0UL; r<TILE_CLASS_CNT && !graded; r++ ) {
1103 0 : if( !( TILE_CLASS[ r ].flags & FD_TOPOB_TILE_ALWAYS ) ) continue;
1104 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
1105 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
1106 0 : if( tile_is_floating( tile ) || tile->cpu_idx!=ULONG_MAX || strcmp( tile->name, TILE_CLASS[ r ].name ) ) continue;
1107 0 : auto_tile_cpu( tile, cpus, &cpu_idx, cpu_assigned, cpu_ordering, skip_ht_pairs, &spare_siblings, efficient, dem, 0UL );
1108 0 : }
1109 0 : }
1110 0 : ulong cpu_idx_startup = cpu_bv_is_null( group_post ) ? cpu_idx : 0UL; /* may reuse group post-start cores */
1111 0 : cpu_bv_t cpu_assigned_startup[ cpu_bv_word_cnt ];
1112 0 : cpu_bv_subtract( cpu_assigned_startup, cpu_assigned, numa_fence );
1113 0 : cpu_bv_subtract( cpu_assigned_startup, cpu_assigned_startup, group_post );
1114 0 : ulong dem_startup[ 9 ] = { 0UL, 0UL, 0UL, dem[ 3 ], dem[ 4 ], dem[ 5 ], 0UL, 0UL, 0UL };
1115 0 : dem[ 3 ] = dem[ 4 ] = dem[ 5 ] = 0UL;
1116 :
1117 : /* Separately assign startup and post-start tiles */
1118 0 : for( ulong r=0UL; r<TILE_CLASS_CNT && !graded; r++ ) {
1119 0 : if( !( TILE_CLASS[ r ].flags & FD_TOPOB_TILE_STARTUP ) ) continue;
1120 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
1121 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
1122 0 : if( tile_is_floating( tile ) || strcmp( tile->name, TILE_CLASS[ r ].name ) ) continue;
1123 0 : auto_tile_cpu( tile, cpus, &cpu_idx_startup, cpu_assigned_startup, cpu_ordering, skip_ht_pairs, &spare_siblings, efficient, dem_startup, 1UL );
1124 0 : }
1125 0 : }
1126 0 : if( graded ) smt_place( topo, cpus, cpu_assigned, cpu_ordering, steady_idx, always_cnt, steady_cnt, steady_cnt, steady_mode, steady_partner, efficient );
1127 0 : if( graded ) {
1128 0 : cpu_bv_subtract( cpu_assigned, cpu_assigned, numa_fence );
1129 0 : auto_startup_cpus( topo, cpus, cpu_assigned_startup, cpu_ordering, efficient );
1130 0 : }
1131 0 : for( ulong r=0UL; r<TILE_CLASS_CNT && !graded; r++ ) {
1132 0 : if( !( TILE_CLASS[ r ].flags & FD_TOPOB_TILE_POSTSTART ) ) continue;
1133 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
1134 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
1135 0 : if( tile->cpu_idx!=ULONG_MAX || strcmp( tile->name, TILE_CLASS[ r ].name ) ) continue;
1136 0 : auto_tile_cpu( tile, cpus, &cpu_idx, cpu_assigned, cpu_ordering, skip_ht_pairs, &spare_siblings, efficient, dem, 2UL );
1137 0 : }
1138 0 : }
1139 :
1140 : /* Make sure all the tiles we haven't set are supposed to be floating. */
1141 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
1142 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
1143 0 : if( tile->cpu_idx!=ULONG_MAX ) continue;
1144 :
1145 0 : if( FD_UNLIKELY( !tile_is_floating( tile ) ) ) FD_LOG_WARNING(( "auto layout cannot affine tile `%s:%lu` because it is unknown. Leaving it floating", tile->name, tile->kind_id ));
1146 0 : }
1147 :
1148 0 : }
1149 :
1150 : void
1151 0 : fd_topob_auto_layout( fd_topo_t * topo ) {
1152 0 : fd_topo_cpus_t cpus[1];
1153 0 : fd_topo_cpus_init( cpus );
1154 0 : fd_topob_auto_layout_cpus( topo, cpus );
1155 0 : }
|