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