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

Generated by: LCOV version 1.14