Line data Source code
1 : #include "fd_topob.h"
2 :
3 : #include "../../util/pod/fd_pod_format.h"
4 : #include "fd_cpu_topo.h"
5 :
6 : fd_topo_t *
7 : fd_topob_new( void * mem,
8 0 : char const * app_name ) {
9 0 : fd_topo_t * topo = (fd_topo_t *)mem;
10 :
11 0 : if( FD_UNLIKELY( !topo ) ) {
12 0 : FD_LOG_WARNING( ( "NULL topo" ) );
13 0 : return NULL;
14 0 : }
15 :
16 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)topo, alignof(fd_topo_t) ) ) ) {
17 0 : FD_LOG_WARNING( ( "misaligned topo" ) );
18 0 : return NULL;
19 0 : }
20 :
21 0 : fd_memset( topo, 0, sizeof(fd_topo_t) );
22 :
23 0 : FD_TEST( fd_pod_new( topo->props, sizeof(topo->props) ) );
24 :
25 0 : if( FD_UNLIKELY( strlen( app_name )>=sizeof(topo->app_name) ) ) FD_LOG_ERR(( "app_name too long: %s", app_name ));
26 0 : strncpy( topo->app_name, app_name, sizeof(topo->app_name) );
27 :
28 0 : topo->max_page_size = FD_SHMEM_GIGANTIC_PAGE_SZ;
29 0 : topo->gigantic_page_threshold = 4 * FD_SHMEM_HUGE_PAGE_SZ;
30 :
31 0 : topo->agave_affinity_cnt = 0;
32 0 : topo->blocklist_cores_cnt = 0;
33 :
34 0 : return topo;
35 0 : }
36 :
37 : fd_topo_wksp_t *
38 : fd_topob_wksp( fd_topo_t * topo,
39 0 : char const * name ) {
40 0 : if( FD_UNLIKELY( !topo || !name || !strlen( name ) ) ) FD_LOG_ERR(( "NULL args" ));
41 0 : if( FD_UNLIKELY( strlen( name )>=sizeof(topo->workspaces[ topo->wksp_cnt ].name ) ) ) FD_LOG_ERR(( "wksp name too long: %s", name ));
42 0 : if( FD_UNLIKELY( topo->wksp_cnt>=FD_TOPO_MAX_WKSPS ) ) FD_LOG_ERR(( "too many workspaces" ));
43 :
44 0 : fd_topo_wksp_t * wksp = &topo->workspaces[ topo->wksp_cnt ];
45 0 : strncpy( wksp->name, name, sizeof(wksp->name) );
46 0 : wksp->id = topo->wksp_cnt;
47 0 : wksp->core_dump_level = FD_TOPO_CORE_DUMP_LEVEL_REGULAR;
48 0 : topo->wksp_cnt++;
49 0 : return wksp;
50 0 : }
51 :
52 : fd_topo_obj_t *
53 : fd_topob_obj( fd_topo_t * topo,
54 : char const * obj_name,
55 0 : char const * wksp_name ) {
56 0 : if( FD_UNLIKELY( !topo || !obj_name || !wksp_name ) ) FD_LOG_ERR(( "NULL args" ));
57 0 : if( FD_UNLIKELY( strlen( obj_name )>=sizeof(topo->objs[ topo->obj_cnt ].name ) ) ) FD_LOG_ERR(( "obj name too long: %s", obj_name ));
58 0 : if( FD_UNLIKELY( topo->obj_cnt>=FD_TOPO_MAX_OBJS ) ) FD_LOG_ERR(( "too many objects" ));
59 :
60 0 : ulong wksp_id = fd_topo_find_wksp( topo, wksp_name );
61 0 : if( FD_UNLIKELY( wksp_id==ULONG_MAX ) ) FD_LOG_ERR(( "workspace not found: %s", wksp_name ));
62 :
63 0 : fd_topo_obj_t * obj = &topo->objs[ topo->obj_cnt ];
64 0 : memset( obj, 0, sizeof(fd_topo_obj_t) );
65 0 : strncpy( obj->name, obj_name, sizeof(obj->name) );
66 0 : obj->id = topo->obj_cnt;
67 0 : obj->wksp_id = wksp_id;
68 0 : obj->label_idx = ULONG_MAX;
69 0 : topo->obj_cnt++;
70 :
71 0 : return obj;
72 0 : }
73 :
74 : fd_topo_obj_t *
75 : fd_topob_obj_named( fd_topo_t * topo,
76 : char const * obj_type,
77 : char const * wksp_name,
78 0 : char const * label ) {
79 0 : if( FD_UNLIKELY( !label ) ) FD_LOG_ERR(( "NULL args" ));
80 0 : if( FD_UNLIKELY( strlen( label )>=sizeof(topo->objs[ topo->obj_cnt ].label ) ) ) FD_LOG_ERR(( "obj label too long: %s", label ));
81 0 : fd_topo_obj_t * obj = fd_topob_obj( topo, obj_type, wksp_name );
82 0 : if( FD_UNLIKELY( !obj ) ) return NULL;
83 :
84 0 : fd_cstr_ncpy( obj->label, label, sizeof(obj->label) );
85 0 : obj->label_idx = fd_topo_obj_cnt( topo, obj_type, label );
86 :
87 0 : return obj;
88 0 : }
89 :
90 : fd_topo_link_t *
91 : fd_topob_link( fd_topo_t * topo,
92 : char const * link_name,
93 : char const * wksp_name,
94 : ulong depth,
95 : ulong mtu,
96 0 : ulong burst ) {
97 0 : if( FD_UNLIKELY( !topo || !link_name || !wksp_name ) ) FD_LOG_ERR(( "NULL args" ));
98 0 : if( FD_UNLIKELY( strlen( link_name )>=sizeof(topo->links[ topo->link_cnt ].name ) ) ) FD_LOG_ERR(( "link name too long: %s", link_name ));
99 0 : if( FD_UNLIKELY( topo->link_cnt>=FD_TOPO_MAX_LINKS ) ) FD_LOG_ERR(( "too many links" ));
100 :
101 0 : ulong kind_id = 0UL;
102 0 : for( ulong i=0UL; i<topo->link_cnt; i++ ) {
103 0 : if( !strcmp( topo->links[ i ].name, link_name ) ) kind_id++;
104 0 : }
105 :
106 0 : fd_topo_link_t * link = &topo->links[ topo->link_cnt ];
107 0 : strncpy( link->name, link_name, sizeof(link->name) );
108 0 : link->id = topo->link_cnt;
109 0 : link->kind_id = kind_id;
110 0 : link->depth = depth;
111 0 : link->mtu = mtu;
112 0 : link->burst = burst;
113 :
114 0 : fd_topo_obj_t * obj = fd_topob_obj( topo, "mcache", wksp_name );
115 0 : link->mcache_obj_id = obj->id;
116 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, depth, "obj.%lu.depth", obj->id ) );
117 :
118 0 : if( mtu ) {
119 0 : obj = fd_topob_obj( topo, "dcache", wksp_name );
120 0 : link->dcache_obj_id = obj->id;
121 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, depth, "obj.%lu.depth", obj->id ) );
122 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, burst, "obj.%lu.burst", obj->id ) );
123 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, mtu, "obj.%lu.mtu", obj->id ) );
124 0 : }
125 0 : topo->link_cnt++;
126 :
127 0 : return link;
128 0 : }
129 :
130 : void
131 : fd_topob_tile_uses( fd_topo_t * topo,
132 : fd_topo_tile_t * tile,
133 : fd_topo_obj_t const * obj,
134 0 : int mode ) {
135 0 : (void)topo;
136 :
137 0 : if( FD_UNLIKELY( tile->uses_obj_cnt>=FD_TOPO_MAX_TILE_OBJS ) ) FD_LOG_ERR(( "tile `%s` uses too many objects", tile->name ));
138 :
139 0 : tile->uses_obj_id[ tile->uses_obj_cnt ] = obj->id;
140 0 : tile->uses_obj_mode[ tile->uses_obj_cnt ] = mode;
141 0 : tile->uses_obj_cnt++;
142 0 : }
143 :
144 : fd_topo_tile_t *
145 : fd_topob_tile( fd_topo_t * topo,
146 : char const * tile_name,
147 : char const * tile_wksp,
148 : char const * metrics_wksp,
149 : ulong cpu_idx,
150 : int is_agave,
151 : int uses_id_keyswitch,
152 0 : int uses_av_keyswitch ) {
153 :
154 0 : if( FD_UNLIKELY( !topo || !tile_name || !tile_wksp || !metrics_wksp ) ) FD_LOG_ERR(( "NULL args" ));
155 0 : if( FD_UNLIKELY( strlen( tile_name )>=sizeof(topo->tiles[ topo->tile_cnt ].name ) ) ) FD_LOG_ERR(( "tile name too long: %s", tile_name ));
156 0 : if( FD_UNLIKELY( topo->tile_cnt>=FD_TOPO_MAX_TILES ) ) FD_LOG_ERR(( "too many tiles %lu", topo->tile_cnt ));
157 :
158 0 : ulong kind_id = 0UL;
159 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
160 0 : if( !strcmp( topo->tiles[ i ].name, tile_name ) ) kind_id++;
161 0 : }
162 :
163 0 : fd_topo_tile_t * tile = &topo->tiles[ topo->tile_cnt ];
164 0 : strncpy( tile->name, tile_name, sizeof(tile->name) );
165 0 : tile->id = topo->tile_cnt;
166 0 : tile->kind_id = kind_id;
167 0 : tile->is_agave = is_agave;
168 0 : tile->cpu_idx = cpu_idx;
169 0 : tile->in_cnt = 0UL;
170 0 : tile->out_cnt = 0UL;
171 0 : tile->uses_obj_cnt = 0UL;
172 :
173 0 : fd_topo_obj_t * tile_obj = fd_topob_obj( topo, "tile", tile_wksp );
174 0 : tile->tile_obj_id = tile_obj->id;
175 0 : fd_topob_tile_uses( topo, tile, tile_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
176 :
177 0 : fd_topo_obj_t * obj = fd_topob_obj( topo, "metrics", metrics_wksp );
178 0 : tile->metrics_obj_id = obj->id;
179 0 : fd_topob_tile_uses( topo, tile, obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
180 :
181 0 : if( FD_LIKELY( uses_id_keyswitch ) ) {
182 0 : obj = fd_topob_obj( topo, "keyswitch", tile_wksp );
183 0 : tile->id_keyswitch_obj_id = obj->id;
184 0 : fd_topob_tile_uses( topo, tile, obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
185 0 : } else {
186 0 : tile->id_keyswitch_obj_id = ULONG_MAX;
187 0 : }
188 :
189 0 : if( FD_UNLIKELY( uses_av_keyswitch ) ) {
190 0 : obj = fd_topob_obj( topo, "keyswitch", tile_wksp );
191 0 : tile->av_keyswitch_obj_id = obj->id;
192 0 : fd_topob_tile_uses( topo, tile, obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
193 0 : } else {
194 0 : tile->av_keyswitch_obj_id = ULONG_MAX;
195 0 : }
196 :
197 0 : topo->tile_cnt++;
198 0 : return tile;
199 0 : }
200 :
201 : void
202 : fd_topob_tile_in( fd_topo_t * topo,
203 : char const * tile_name,
204 : ulong tile_kind_id,
205 : char const * fseq_wksp,
206 : char const * link_name,
207 : ulong link_kind_id,
208 : int reliable,
209 0 : int polled ) {
210 0 : if( FD_UNLIKELY( !topo || !tile_name || !fseq_wksp || !link_name ) ) FD_LOG_ERR(( "NULL args" ));
211 :
212 0 : ulong tile_id = fd_topo_find_tile( topo, tile_name, tile_kind_id );
213 0 : if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "tile not found: %s:%lu", tile_name, tile_kind_id ));
214 0 : fd_topo_tile_t * tile = &topo->tiles[ tile_id ];
215 :
216 0 : ulong link_id = fd_topo_find_link( topo, link_name, link_kind_id );
217 0 : if( FD_UNLIKELY( link_id==ULONG_MAX ) ) FD_LOG_ERR(( "link not found: %s:%lu", link_name, link_kind_id ));
218 0 : fd_topo_link_t * link = &topo->links[ link_id ];
219 :
220 0 : if( FD_UNLIKELY( tile->in_cnt>=FD_TOPO_MAX_TILE_IN_LINKS ) ) FD_LOG_ERR(( "too many in links: %s:%lu", tile_name, tile_kind_id ) );
221 0 : tile->in_link_id[ tile->in_cnt ] = link->id;
222 0 : tile->in_link_reliable[ tile->in_cnt ] = reliable;
223 0 : tile->in_link_poll[ tile->in_cnt ] = polled;
224 0 : fd_topo_obj_t * obj = fd_topob_obj( topo, "fseq", fseq_wksp );
225 0 : fd_topob_tile_uses( topo, tile, obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
226 0 : tile->in_link_fseq_obj_id[ tile->in_cnt ] = obj->id;
227 0 : tile->in_cnt++;
228 :
229 0 : fd_topob_tile_uses( topo, tile, &topo->objs[ link->mcache_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
230 0 : if( FD_LIKELY( link->mtu ) ) {
231 0 : fd_topob_tile_uses( topo, tile, &topo->objs[ link->dcache_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
232 0 : }
233 0 : }
234 :
235 : void
236 : fd_topob_tile_out( fd_topo_t * topo,
237 : char const * tile_name,
238 : ulong tile_kind_id,
239 : char const * link_name,
240 0 : ulong link_kind_id ) {
241 0 : ulong tile_id = fd_topo_find_tile( topo, tile_name, tile_kind_id );
242 0 : if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "tile not found: %s:%lu", tile_name, tile_kind_id ));
243 0 : fd_topo_tile_t * tile = &topo->tiles[ tile_id ];
244 :
245 0 : ulong link_id = fd_topo_find_link( topo, link_name, link_kind_id );
246 0 : if( FD_UNLIKELY( link_id==ULONG_MAX ) ) FD_LOG_ERR(( "link not found: %s:%lu", link_name, link_kind_id ));
247 0 : fd_topo_link_t * link = &topo->links[ link_id ];
248 :
249 0 : if( FD_UNLIKELY( tile->out_cnt>=FD_TOPO_MAX_TILE_OUT_LINKS ) ) FD_LOG_ERR(( "too many out links: %s", tile_name ));
250 0 : tile->out_link_id[ tile->out_cnt ] = link->id;
251 0 : tile->out_cnt++;
252 :
253 0 : fd_topob_tile_uses( topo, tile, &topo->objs[ link->mcache_obj_id ], FD_SHMEM_JOIN_MODE_READ_WRITE );
254 0 : if( FD_LIKELY( link->mtu ) ) {
255 0 : fd_topob_tile_uses( topo, tile, &topo->objs[ link->dcache_obj_id ], FD_SHMEM_JOIN_MODE_READ_WRITE );
256 0 : }
257 0 : }
258 :
259 : static void
260 0 : validate( fd_topo_t const * topo ) {
261 : /* Objects have valid wksp_ids */
262 0 : for( ulong i=0UL; i<topo->obj_cnt; i++ ) {
263 0 : if( FD_UNLIKELY( topo->objs[ i ].wksp_id>=topo->wksp_cnt ) )
264 0 : FD_LOG_ERR(( "invalid workspace id %lu", topo->objs[ i ].wksp_id ));
265 0 : }
266 :
267 : /* Tile ins are valid */
268 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
269 0 : for( ulong j=0UL; j<topo->tiles[ i ].in_cnt; j++ ) {
270 0 : if( FD_UNLIKELY( topo->tiles[ i ].in_link_id[ j ]>=topo->link_cnt ) )
271 0 : FD_LOG_ERR(( "tile %lu (%s) has invalid in link %lu", i, topo->tiles[ i ].name, topo->tiles[ i ].in_link_id[ j ] ));
272 0 : }
273 0 : }
274 :
275 : /* Tile does not have duplicated ins */
276 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
277 0 : for( ulong j=0UL; j<topo->tiles[ i ].in_cnt; j++ ) {
278 0 : for( ulong k=0UL; k<topo->tiles[ i ].in_cnt; k++ ) {
279 0 : if( FD_UNLIKELY( j==k ) ) continue;
280 0 : if( FD_UNLIKELY( topo->tiles[ i ].in_link_id[ j ] == topo->tiles[ i ].in_link_id[ k ] ) )
281 0 : FD_LOG_ERR(( "tile %lu (%s) has duplicated in link %lu (%s)", i, topo->tiles[ i ].name,
282 0 : topo->tiles[ i ].in_link_id[ j ], topo->links[ topo->tiles[ i ].in_link_id[ j ] ].name ));
283 0 : }
284 0 : }
285 0 : }
286 :
287 : /* Tile does not have duplicated outs */
288 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
289 0 : for( ulong j=0UL; j<topo->tiles[ i ].out_cnt; j++ ) {
290 0 : for( ulong k=0UL; k<topo->tiles[ i ].out_cnt; k++ ) {
291 0 : if( FD_UNLIKELY( j==k ) ) continue;
292 0 : if( FD_UNLIKELY( topo->tiles[ i ].out_link_id[ j ] == topo->tiles[ i ].out_link_id[ k ] ) )
293 0 : FD_LOG_ERR(( "tile %lu (%s) has duplicated out link %lu (%s)", i, topo->tiles[ i ].name,
294 0 : topo->tiles[ i ].out_link_id[ j ], topo->links[ topo->tiles[ i ].out_link_id[ j ] ].name ));
295 0 : }
296 0 : }
297 0 : }
298 :
299 : /* Tile outs are different than ins */
300 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
301 0 : for( ulong j=0UL; j<topo->tiles[ i ].out_cnt; j++ ) {
302 0 : for( ulong k=0UL; k<topo->tiles[ i ].in_cnt; k++ ) {
303 0 : char const * link_name = topo->links[ topo->tiles[ i ].out_link_id[ j ] ].name;
304 : /* PoH tile "publishes" this on behalf of Agave, so it's not
305 : a real circular link. */
306 0 : if( FD_UNLIKELY( !strcmp( link_name, "stake_out" ) ||
307 0 : !strcmp( link_name, "crds_shred" ) ) ) continue;
308 :
309 0 : if( FD_UNLIKELY( topo->tiles[ i ].out_link_id[ j ] == topo->tiles[ i ].in_link_id[ k ] ) )
310 0 : FD_LOG_ERR(( "tile %lu has out link %lu same as in", i, topo->tiles[ i ].out_link_id[ j ] ));
311 0 : }
312 0 : }
313 0 : }
314 :
315 : /* Non polling tile ins are also not reliable */
316 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
317 0 : for( ulong j=0UL; j<topo->tiles[ i ].in_cnt; j++ ) {
318 0 : if( FD_UNLIKELY( !topo->tiles[ i ].in_link_poll[ j ] && topo->tiles[ i ].in_link_reliable[ j ] ) )
319 0 : FD_LOG_ERR(( "tile %lu has in link %lu which is not polled but reliable", i, topo->tiles[ i ].in_link_id[ j ] ));
320 0 : }
321 0 : }
322 :
323 : /* Tile outs are valid */
324 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
325 0 : for( ulong j=0UL; j<topo->tiles[ i ].out_cnt; j++ ) {
326 0 : if( FD_UNLIKELY( topo->tiles[ i ].out_link_id[ j ] >= topo->link_cnt ) )
327 0 : FD_LOG_ERR(( "tile %lu has invalid out link %lu", i, topo->tiles[ i ].out_link_id[ j ] ));
328 0 : }
329 0 : }
330 :
331 : /* Workspace names are unique */
332 0 : for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
333 0 : for( ulong j=0UL; j<topo->wksp_cnt; j++ ) {
334 0 : if( FD_UNLIKELY( i==j ) ) continue;
335 0 : if( FD_UNLIKELY( !strcmp( topo->workspaces[ i ].name, topo->workspaces[ j ].name ) ) )
336 0 : FD_LOG_ERR(( "duplicate workspace name %s", topo->workspaces[ i ].name ));
337 0 : }
338 0 : }
339 :
340 : /* Each workspace is identified correctly */
341 0 : for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
342 0 : if( FD_UNLIKELY( topo->workspaces[ i ].id != i ) )
343 0 : FD_LOG_ERR(( "workspace %lu has id %lu", i, topo->workspaces[ i ].id ));
344 0 : }
345 :
346 : /* Each link has exactly one producer */
347 0 : for( ulong i=0UL; i<topo->link_cnt; i++ ) {
348 0 : ulong producer_cnt = 0;
349 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
350 0 : for( ulong k=0UL; k<topo->tiles[ j ].out_cnt; k++ ) {
351 0 : if( topo->tiles[ j ].out_link_id[ k ]==i ) producer_cnt++;
352 0 : }
353 0 : }
354 0 : if( FD_UNLIKELY( producer_cnt>1UL || ( producer_cnt==0UL && !topo->links[ i ].permit_no_producers ) ) )
355 0 : FD_LOG_ERR(( "link %lu (%s:%lu) has %lu producers", i, topo->links[ i ].name, topo->links[ i ].kind_id, producer_cnt ));
356 0 : }
357 :
358 : /* Each link has at least one consumer */
359 0 : for( ulong i=0UL; i<topo->link_cnt; i++ ) {
360 0 : ulong cnt = fd_topo_link_consumer_cnt( topo, &topo->links[ i ] );
361 0 : if( FD_UNLIKELY( cnt < 1UL && !topo->links[ i ].permit_no_consumers ) ) {
362 0 : FD_LOG_ERR(( "link %lu (%s:%lu) has 0 consumers", i, topo->links[ i ].name, topo->links[ i ].kind_id ));
363 0 : }
364 0 : }
365 0 : }
366 :
367 : void
368 : fd_topob_auto_layout_cpus( fd_topo_t * topo,
369 : fd_topo_cpus_t * cpus,
370 0 : int reserve_agave_cores ) {
371 : /* Incredibly simple automatic layout system for now ... just assign
372 : tiles to CPU cores in NUMA sequential order, except for a few tiles
373 : which should be floating. */
374 :
375 0 : char const * FLOATING[] = {
376 0 : "netlnk",
377 0 : "metric",
378 0 : "diag",
379 0 : "bencho",
380 0 : "genesi", /* FIREDANCER ONLY */
381 0 : "ipecho", /* FIREDANCER ONLY */
382 0 : "snapwr", /* FIREDANCER ONLY */
383 0 : };
384 :
385 0 : char const * ORDERED[] = {
386 0 : "backt",
387 0 : "benchg",
388 0 : "benchs",
389 0 : "net",
390 0 : "sock",
391 0 : "quic",
392 0 : "bundle",
393 0 : "verify",
394 0 : "dedup",
395 0 : "resolh", /* FRANK only */
396 0 : "resolv", /* FIREDANCER only */
397 0 : "pack",
398 0 : "bank", /* FRANK only */
399 0 : "execle", /* FIREDANCER only */
400 0 : "poh", /* FRANK only */
401 0 : "pohh", /* FIREDANCER only */
402 0 : "shred",
403 0 : "event", /* FIREADNCER only */
404 0 : "store", /* FRANK only */
405 0 : "sign",
406 0 : "plugin", /* FRANK only */
407 0 : "gui",
408 0 : "rpc", /* FIREDANCER only */
409 0 : "gossvf", /* FIREDANCER only */
410 0 : "gossip", /* FIREDANCER only */
411 0 : "repair", /* FIREDANCER only */
412 0 : "replay", /* FIREDANCER only */
413 0 : "execrp", /* FIREDANCER only */
414 0 : "txsend", /* FIREDANCER only */
415 0 : "tower", /* FIREDANCER only */
416 0 : "pktgen",
417 0 : "snapct", /* FIREDANCER only */
418 0 : "snapld", /* FIREDANCER only */
419 0 : "snapdc", /* FIREDANCER only */
420 0 : "snapin", /* FIREDANCER only */
421 0 : "snapwm", /* FIREDANCER only */
422 0 : "snapwh", /* FIREDANCER only */
423 0 : "snapla", /* FIREDANCER only */
424 0 : "snapls", /* FIREDANCER only */
425 0 : "snaplh", /* FIREDANCER only */
426 0 : "snaplv", /* FIREDANCER only */
427 0 : "arch_f", /* FIREDANCER only */
428 0 : "arch_w", /* FIREDANCER only */
429 0 : "accdb", /* FIREDANCER only */
430 0 : };
431 :
432 0 : char const * CRITICAL_TILES[] = {
433 0 : "pack",
434 0 : "poh",
435 0 : "pohh",
436 0 : "gui",
437 0 : "snapld", /* TODO: Snapshot loading speed depends on having full core */
438 0 : "snapdc", /* TODO: Snapshot loading speed depends on having full core */
439 0 : "snapin", /* TODO: Snapshot loading speed depends on having full core */
440 0 : "snapwm", /* TODO: Snapshot loading speed depends on having full core */
441 0 : "snapwh", /* TODO: Snapshot loading speed depends on having full core */
442 0 : };
443 :
444 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
445 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
446 0 : tile->cpu_idx = ULONG_MAX;
447 0 : }
448 :
449 0 : ulong cpu_ordering[ FD_TILE_MAX ] = { 0UL };
450 0 : int pairs_assigned[ FD_TILE_MAX ] = { 0 };
451 :
452 0 : ulong next_cpu_idx = 0UL;
453 0 : for( ulong i=0UL; i<cpus->numa_node_cnt; i++ ) {
454 0 : for( ulong j=0UL; j<cpus->cpu_cnt; j++ ) {
455 0 : fd_topo_cpu_t * cpu = &cpus->cpu[ j ];
456 :
457 0 : if( FD_UNLIKELY( pairs_assigned[ j ] || cpu->numa_node!=i ) ) continue;
458 :
459 0 : FD_TEST( next_cpu_idx<FD_TILE_MAX );
460 0 : cpu_ordering[ next_cpu_idx++ ] = j;
461 :
462 0 : if( FD_UNLIKELY( cpu->sibling!=ULONG_MAX ) ) {
463 : /* If the CPU has a HT pair, place it immediately after so they
464 : are sequentially assigned. */
465 0 : FD_TEST( next_cpu_idx<FD_TILE_MAX );
466 0 : cpu_ordering[ next_cpu_idx++ ] = cpu->sibling;
467 0 : pairs_assigned[ cpu->sibling ] = 1;
468 0 : }
469 0 : }
470 0 : }
471 :
472 0 : FD_TEST( next_cpu_idx==cpus->cpu_cnt );
473 :
474 0 : int cpu_assigned[ FD_TILE_MAX ] = {0};
475 : /* excluded cpus are simply considered already assigned */
476 0 : for( ulong i=0UL; i<topo->blocklist_cores_cnt; i++ ) {
477 0 : FD_TEST( topo->blocklist_cores_cpu_idx[ i ]<FD_TILE_MAX );
478 0 : cpu_assigned[ topo->blocklist_cores_cpu_idx[ i ] ] = 1;
479 0 : }
480 :
481 : /* Compute total number of available physical cores */
482 0 : ulong available_physical = 0UL;
483 0 : for( ulong i=0UL; i<cpus->cpu_cnt; i++ ) {
484 0 : if( !cpu_assigned[ i ] && !pairs_assigned[ i ] ) available_physical++;
485 0 : }
486 :
487 : /* Compute total number of tiles that need assignment */
488 0 : ulong tiles_to_assign = 0UL;
489 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
490 0 : for( ulong i=0UL; i<sizeof(ORDERED)/sizeof(ORDERED[0]); i++ ) {
491 0 : if( !strcmp( topo->tiles[ j ].name, ORDERED[ i ] ) ) {
492 0 : tiles_to_assign++;
493 0 : break;
494 0 : }
495 0 : }
496 0 : }
497 :
498 : /* If we have enough physical cores (excluding HT siblings) for all
499 : tiles that need assignment, exclude HT siblings so that no tile
500 : gets scheduled on a hyperthread pair.
501 : For Frankendancer, we reserve 2x cores so we have enough for Agave */
502 0 : int skip_ht_pairs = reserve_agave_cores
503 0 : ? (available_physical>=2*tiles_to_assign) /* Frankendancer */
504 0 : : (available_physical>=tiles_to_assign); /* Firedancer */
505 :
506 0 : ulong cpu_idx = 0UL;
507 :
508 0 : for( ulong i=0UL; i<sizeof(ORDERED)/sizeof(ORDERED[0]); i++ ) {
509 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
510 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
511 0 : if( !strcmp( tile->name, ORDERED[ i ] ) ) {
512 0 : while( cpu_idx<FD_TILE_MAX && cpu_assigned[ cpu_ordering[ cpu_idx ] ] ) cpu_idx++;
513 0 : if( FD_UNLIKELY( cpu_idx>=cpus->cpu_cnt ) ) {
514 0 : FD_LOG_ERR(( "auto layout cannot set affinity for tile `%s:%lu` because all the CPUs are already assigned", tile->name, tile->kind_id ));
515 0 : } else {
516 : /* Certain tiles are latency and throughput critical and
517 : should not get a HT pair assigned. */
518 0 : fd_topo_cpu_t const * cpu = &cpus->cpu[ cpu_ordering[ cpu_idx ] ];
519 :
520 0 : int is_ht_critical = 0;
521 0 : if( FD_UNLIKELY( cpu->sibling!=ULONG_MAX ) ) {
522 0 : for( ulong k=0UL; k<sizeof(CRITICAL_TILES)/sizeof(CRITICAL_TILES[0]); k++ ) {
523 0 : if( !strcmp( tile->name, CRITICAL_TILES[ k ] ) ) {
524 0 : is_ht_critical = 1;
525 0 : break;
526 0 : }
527 0 : }
528 0 : }
529 :
530 0 : if( FD_UNLIKELY( is_ht_critical || skip_ht_pairs ) ) {
531 0 : ulong try_assign = cpu_idx;
532 0 : while( cpu_assigned[ cpu_ordering[ try_assign ] ] || (cpus->cpu[ cpu_ordering[ try_assign ] ].sibling!=ULONG_MAX && cpu_assigned[ cpus->cpu[ cpu_ordering[ try_assign ] ].sibling ]) ) {
533 0 : try_assign++;
534 0 : if( FD_UNLIKELY( try_assign>=cpus->cpu_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", tile->name, tile->kind_id ));
535 0 : }
536 :
537 0 : ulong sibling = cpus->cpu[ cpu_ordering[ try_assign ] ].sibling;
538 0 : cpu_assigned[ cpu_ordering[ try_assign ] ] = 1;
539 0 : if( sibling!=ULONG_MAX ) {
540 0 : cpu_assigned[ sibling ] = 1;
541 0 : }
542 0 : tile->cpu_idx = cpu_ordering[ try_assign ];
543 0 : } else {
544 0 : cpu_assigned[ cpu_ordering[ cpu_idx ] ] = 1;
545 0 : tile->cpu_idx = cpu_ordering[ cpu_idx ];
546 0 : }
547 0 : }
548 0 : }
549 0 : }
550 0 : }
551 :
552 : /* Make sure all the tiles we haven't set are supposed to be floating. */
553 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
554 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
555 0 : if( tile->cpu_idx!=ULONG_MAX ) continue;
556 :
557 0 : int found = 0;
558 0 : for( ulong j=0UL; j<sizeof(FLOATING)/sizeof(FLOATING[0]); j++ ) {
559 0 : if( !strcmp( tile->name, FLOATING[ j ] ) ) {
560 0 : found = 1;
561 0 : break;
562 0 : }
563 0 : }
564 :
565 0 : if( FD_UNLIKELY( !found ) ) FD_LOG_WARNING(( "auto layout cannot affine tile `%s:%lu` because it is unknown. Leaving it floating", tile->name, tile->kind_id ));
566 0 : }
567 :
568 0 : topo->agave_affinity_cnt = 0UL;
569 0 : if( FD_UNLIKELY( reserve_agave_cores ) ) {
570 0 : for( ulong i=cpu_idx; i<cpus->cpu_cnt; i++ ) {
571 0 : if( FD_UNLIKELY( !cpus->cpu[ cpu_ordering[ i ] ].online ) ) continue;
572 0 : if( FD_UNLIKELY( cpu_assigned[ cpu_ordering[ i ] ] ) ) continue;
573 :
574 0 : if( FD_LIKELY( topo->agave_affinity_cnt<sizeof(topo->agave_affinity_cpu_idx)/sizeof(topo->agave_affinity_cpu_idx[0]) ) ) {
575 0 : topo->agave_affinity_cpu_idx[ topo->agave_affinity_cnt++ ] = cpu_ordering[ i ];
576 0 : }
577 0 : }
578 0 : }
579 0 : }
580 :
581 : void
582 : fd_topob_auto_layout( fd_topo_t * topo,
583 0 : int reserve_agave_cores ) {
584 0 : fd_topo_cpus_t cpus[1];
585 0 : fd_topo_cpus_init( cpus );
586 0 : fd_topob_auto_layout_cpus( topo, cpus, reserve_agave_cores );
587 0 : }
588 :
589 : ulong
590 : fd_numa_node_idx( ulong cpu_idx );
591 :
592 : static void
593 0 : initialize_numa_assignments( fd_topo_t * topo ) {
594 : /* Assign workspaces to NUMA nodes. The heuristic here is pretty
595 : simple for now: workspaces go on the NUMA node of the first
596 : tile which maps the largest object in the workspace. */
597 :
598 0 : for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
599 0 : ulong max_footprint = 0UL;
600 0 : ulong max_obj = ULONG_MAX;
601 :
602 0 : for( ulong j=0UL; j<topo->obj_cnt; j++ ) {
603 0 : fd_topo_obj_t * obj = &topo->objs[ j ];
604 0 : if( obj->wksp_id!=i ) continue;
605 0 : if( FD_UNLIKELY( !obj->footprint ) ) FD_LOG_ERR(( "obj %lu (%s) has invalid parameters", j, obj->name ));
606 :
607 0 : if( FD_UNLIKELY( !max_footprint || obj->footprint>max_footprint ) ) {
608 0 : max_footprint = obj->footprint;
609 0 : max_obj = j;
610 0 : }
611 0 : }
612 :
613 0 : if( FD_UNLIKELY( max_obj==ULONG_MAX ) ) FD_LOG_ERR(( "no object found for workspace %s", topo->workspaces[ i ].name ));
614 :
615 0 : int found_strict = 0;
616 0 : int found_lazy = 0;
617 0 : int found_assigned = 0;
618 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
619 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
620 0 : if( FD_UNLIKELY( tile->tile_obj_id==max_obj && tile->cpu_idx<FD_TILE_MAX ) ) {
621 0 : topo->workspaces[ i ].numa_idx = fd_numa_node_idx( tile->cpu_idx );
622 0 : FD_TEST( topo->workspaces[ i ].numa_idx!=ULONG_MAX );
623 0 : found_strict = 1;
624 0 : found_lazy = 1;
625 0 : found_assigned = 1;
626 0 : break;
627 0 : } else if( FD_UNLIKELY( tile->tile_obj_id==max_obj && tile->cpu_idx>=FD_TILE_MAX ) ) {
628 0 : topo->workspaces[ i ].numa_idx = 0;
629 0 : found_lazy = 1;
630 0 : break;
631 0 : }
632 0 : }
633 :
634 0 : if( FD_LIKELY( !found_strict ) ) {
635 0 : for( ulong j=0UL; j<topo->tile_cnt; j++ ) {
636 0 : fd_topo_tile_t * tile = &topo->tiles[ j ];
637 0 : for( ulong k=0UL; k<tile->uses_obj_cnt; k++ ) {
638 0 : if( FD_LIKELY( tile->uses_obj_id[ k ]==max_obj && tile->cpu_idx<FD_TILE_MAX ) ) {
639 0 : topo->workspaces[ i ].numa_idx = fd_numa_node_idx( tile->cpu_idx );
640 0 : FD_TEST( topo->workspaces[ i ].numa_idx!=ULONG_MAX );
641 0 : found_lazy = 1;
642 0 : found_assigned = 1;
643 0 : break;
644 0 : } else if( FD_UNLIKELY( tile->uses_obj_id[ k ]==max_obj ) && tile->cpu_idx>=FD_TILE_MAX ) {
645 0 : topo->workspaces[ i ].numa_idx = 0;
646 0 : found_lazy = 1;
647 : /* Don't break, keep looking -- a tile with a CPU assignment
648 : might also use object in which case we want to use that
649 : NUMA node. */
650 0 : }
651 0 : }
652 :
653 0 : if( FD_UNLIKELY( found_assigned ) ) break;
654 0 : }
655 0 : }
656 :
657 0 : if( FD_UNLIKELY( !found_lazy ) ) FD_LOG_ERR(( "no tile uses object %s for workspace %s", topo->objs[ max_obj ].name, topo->workspaces[ i ].name ));
658 0 : }
659 0 : }
660 :
661 : void
662 : fd_topob_finish( fd_topo_t * topo,
663 0 : fd_topo_obj_callbacks_t ** callbacks ) {
664 0 : for( ulong z=0UL; z<topo->tile_cnt; z++ ) {
665 0 : fd_topo_tile_t * tile = &topo->tiles[ z ];
666 :
667 0 : ulong in_cnt = 0UL;
668 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
669 0 : if( FD_UNLIKELY( !tile->in_link_poll[ i ] ) ) continue;
670 0 : in_cnt++;
671 0 : }
672 :
673 0 : FD_TEST( !fd_pod_replacef_ulong( topo->props, in_cnt, "obj.%lu.in_cnt", tile->metrics_obj_id ) );
674 0 : }
675 :
676 0 : for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
677 0 : fd_topo_wksp_t * wksp = &topo->workspaces[ i ];
678 :
679 0 : ulong loose_sz = 0UL;
680 0 : for( ulong j=0UL; j<topo->obj_cnt; j++ ) {
681 0 : fd_topo_obj_t * obj = &topo->objs[ j ];
682 0 : if( FD_UNLIKELY( obj->wksp_id!=wksp->id ) ) continue;
683 :
684 0 : fd_topo_obj_callbacks_t * cb = NULL;
685 0 : for( ulong i=0UL; callbacks[ i ]; i++ ) {
686 0 : if( FD_UNLIKELY( !strcmp( callbacks[ i ]->name, obj->name ) ) ) {
687 0 : cb = callbacks[ i ];
688 0 : break;
689 0 : }
690 0 : }
691 0 : if( FD_UNLIKELY( !cb ) ) FD_LOG_ERR(( "no callbacks for object %s", obj->name ));
692 :
693 0 : if( FD_UNLIKELY( cb->loose ) ) loose_sz += cb->loose( topo, obj );
694 0 : }
695 :
696 0 : ulong part_max = wksp->part_max;
697 0 : if( !part_max ) part_max = (loose_sz / (64UL << 10)); /* alloc + residual padding */
698 0 : part_max += 3; /* for initial alignment */
699 0 : ulong offset = fd_ulong_align_up( fd_wksp_private_data_off( part_max ), fd_topo_workspace_align() );
700 :
701 0 : for( ulong j=0UL; j<topo->obj_cnt; j++ ) {
702 0 : fd_topo_obj_t * obj = &topo->objs[ j ];
703 0 : if( FD_UNLIKELY( obj->wksp_id!=wksp->id ) ) continue;
704 :
705 0 : fd_topo_obj_callbacks_t * cb = NULL;
706 0 : for( ulong i=0UL; callbacks[ i ]; i++ ) {
707 0 : if( FD_UNLIKELY( !strcmp( callbacks[ i ]->name, obj->name ) ) ) {
708 0 : cb = callbacks[ i ];
709 0 : break;
710 0 : }
711 0 : }
712 0 : if( FD_UNLIKELY( !cb ) ) FD_LOG_ERR(( "no callbacks for object %s", obj->name ));
713 :
714 0 : ulong align_ = cb->align( topo, obj );
715 0 : if( FD_UNLIKELY( !fd_ulong_is_pow2( align_ ) ) ) FD_LOG_ERR(( "Return value of fdctl_obj_align(%s,%lu) is not a power of 2", obj->name, obj->id ));
716 0 : offset = fd_ulong_align_up( offset, align_ );
717 0 : obj->offset = offset;
718 0 : obj->footprint = cb->footprint( topo, obj );
719 0 : if( FD_UNLIKELY( 0!=strcmp( obj->name, "tile" ) && (!obj->footprint || obj->footprint>LONG_MAX) ) ) {
720 0 : FD_LOG_ERR(( "fdctl_obj_footprint(%s,%lu) failed", obj->name, obj->id ));
721 0 : }
722 0 : offset += obj->footprint;
723 0 : }
724 :
725 0 : ulong footprint = fd_ulong_align_up( offset, fd_topo_workspace_align() );
726 :
727 0 : part_max = fd_ulong_max( part_max, wksp->min_part_max );
728 0 : loose_sz = fd_ulong_max( loose_sz, wksp->min_loose_sz );
729 :
730 : /* Compute footprint for a workspace that can store our footprint,
731 : with an extra align of padding incase gaddr_lo is not aligned. */
732 0 : ulong total_wksp_footprint = fd_wksp_footprint( part_max, footprint + fd_topo_workspace_align() + loose_sz );
733 :
734 0 : ulong page_sz = topo->max_page_size;
735 0 : if( total_wksp_footprint < topo->gigantic_page_threshold ) page_sz = FD_SHMEM_HUGE_PAGE_SZ;
736 0 : if( FD_UNLIKELY( page_sz!=FD_SHMEM_HUGE_PAGE_SZ && page_sz!=FD_SHMEM_GIGANTIC_PAGE_SZ ) ) FD_LOG_ERR(( "invalid page_sz" ));
737 :
738 0 : ulong wksp_aligned_footprint = fd_ulong_align_up( total_wksp_footprint, page_sz );
739 :
740 : /* Give any leftover space in the underlying shared memory to the
741 : data region of the workspace, since we might as well use it. */
742 0 : wksp->part_max = part_max;
743 0 : wksp->known_footprint = footprint;
744 0 : wksp->total_footprint = wksp_aligned_footprint - fd_ulong_align_up( fd_wksp_private_data_off( part_max ), fd_topo_workspace_align() );
745 0 : wksp->page_sz = page_sz;
746 0 : wksp->page_cnt = wksp_aligned_footprint / page_sz;
747 0 : }
748 :
749 0 : initialize_numa_assignments( topo );
750 :
751 0 : validate( topo );
752 0 : }
|