LCOV - code coverage report
Current view: top level - disco/topo - fd_topob.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 139 518 26.8 %
Date: 2026-02-08 06:05:17 Functions: 8 13 61.5 %

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

Generated by: LCOV version 1.14