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