LCOV - code coverage report
Current view: top level - disco/topo - fd_topob.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 135 501 26.9 %
Date: 2025-12-18 05:07:35 Functions: 8 12 66.7 %

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

Generated by: LCOV version 1.14