LCOV - code coverage report
Current view: top level - app/firedancer-dev/commands - repair.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 982 0.0 %
Date: 2026-08-13 04:56:22 Functions: 0 25 0.0 %

          Line data    Source code
       1             : /* The repair command spawns a smaller topology for profiling the repair
       2             :    tile.  This is a standalone application, and it can be run in mainnet,
       3             :    testnet and/or a private cluster. */
       4             : 
       5             : #include "../../../disco/net/fd_net_tile.h"
       6             : #include "../../../disco/tiles.h"
       7             : #include "../../../disco/topo/fd_topob.h"
       8             : #include "../../../disco/topo/fd_cpu_topo.h"
       9             : #include "../../../util/pod/fd_pod_format.h"
      10             : 
      11             : #include "../../firedancer/topology.h"
      12             : #include "../../shared/commands/configure/configure.h"
      13             : #include "../../shared/commands/run/run.h" /* initialize_workspaces */
      14             : #include "../../shared/fd_config.h" /* config_t */
      15             : #include "../../shared_dev/commands/dev.h"
      16             : #include "../../../disco/tiles.h"
      17             : #include "../../../disco/topo/fd_topob.h"
      18             : #include "../../../util/pod/fd_pod_format.h"
      19             : #include "../../../waltz/resolv/fd_io_readline.h"
      20             : #include "../../platform/fd_sys_util.h"
      21             : #include "../../shared/commands/monitor/helper.h"
      22             : #include "../../../disco/metrics/fd_metrics.h"
      23             : #include "../../../discof/restore/utils/fd_ssmanifest_parser.h"
      24             : #include "../../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
      25             : #include "../../../flamenco/stakes/fd_stake_weight.h"
      26             : #include "../../../flamenco/leaders/fd_leaders_base.h"
      27             : #include "../../../discof/repair/fd_repair_tile.c"
      28             : 
      29             : #include "gossip.h"
      30             : #include "core_subtopo.h"
      31             : 
      32             : #include <unistd.h> /* pause */
      33             : #include <fcntl.h>
      34             : #include <stdio.h>
      35             : #include <termios.h>
      36             : #include <errno.h>
      37             : 
      38             : extern action_t fd_action_repair;
      39             : 
      40             : struct fd_location_info {
      41             :   ulong ip4_addr;         /* for map key convenience */
      42             :   char location[ 128 ];
      43             : };
      44             : typedef struct fd_location_info fd_location_info_t;
      45             : 
      46             : #define MAP_NAME    fd_location_table
      47           0 : #define MAP_T       fd_location_info_t
      48           0 : #define MAP_KEY     ip4_addr
      49           0 : #define MAP_LG_SLOT_CNT 16
      50             : #define MAP_MEMOIZE 0
      51             : #include "../../../util/tmpl/fd_map.c"
      52             : 
      53             : uchar __attribute__((aligned(alignof(fd_location_info_t)))) location_table_mem[ sizeof(fd_location_info_t) * (1 << 16 ) ];
      54             : 
      55             : static struct termios termios_backup;
      56             : 
      57             : static void
      58           0 : restore_terminal( void ) {
      59           0 :   (void)tcsetattr( STDIN_FILENO, TCSANOW, &termios_backup );
      60           0 : }
      61             : 
      62             : extern fd_topo_obj_callbacks_t * CALLBACKS[];
      63             : 
      64             : fd_topo_run_tile_t
      65             : fdctl_tile_run( fd_topo_tile_t const * tile );
      66             : 
      67           0 : #define MANIFEST_LOAD_MAX_SZ (2UL * FD_SHMEM_GIGANTIC_PAGE_SZ)
      68             : 
      69             : /* https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/snapshot_bank_utils.rs#L632 */
      70             : static int
      71           0 : repair_verify_epoch_stakes( fd_snapshot_manifest_t const * manifest ) {
      72           0 :   fd_epoch_schedule_t epoch_schedule = (fd_epoch_schedule_t){
      73           0 :     .slots_per_epoch             = manifest->epoch_schedule_params.slots_per_epoch,
      74           0 :     .leader_schedule_slot_offset = manifest->epoch_schedule_params.leader_schedule_slot_offset,
      75           0 :     .warmup                      = manifest->epoch_schedule_params.warmup,
      76           0 :     .first_normal_epoch          = manifest->epoch_schedule_params.first_normal_epoch,
      77           0 :     .first_normal_slot           = manifest->epoch_schedule_params.first_normal_slot,
      78           0 :   };
      79             : 
      80           0 :   ulong min_required_epoch = fd_slot_to_epoch( &epoch_schedule, manifest->slot, NULL );
      81           0 :   ulong max_required_epoch = fd_slot_to_leader_schedule_epoch( &epoch_schedule, manifest->slot );
      82             : 
      83           0 :   for( ulong i=min_required_epoch; i<=max_required_epoch; i++ ) {
      84           0 :     int found = 0;
      85           0 :     for( ulong j=0UL; j<FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN; j++ ) {
      86           0 :       if( manifest->epoch_stakes[j].epoch==i ) {
      87           0 :         found = 1;
      88           0 :         break;
      89           0 :       }
      90           0 :     }
      91           0 :     if( FD_UNLIKELY( !found ) ) {
      92           0 :       FD_LOG_WARNING(( "stakes not found for epoch %lu in manifest", i ));
      93           0 :       return -1;
      94           0 :     }
      95           0 :   }
      96           0 :   return 0;
      97           0 : }
      98             : 
      99             : static inline ulong
     100             : repair_generate_epoch_info_msg( ulong                                       epoch,
     101             :                                 fd_epoch_schedule_t const *                 epoch_schedule,
     102             :                                 fd_snapshot_manifest_epoch_stakes_t const * epoch_stakes,
     103           0 :                                 ulong *                                     epoch_info_msg_out ) {
     104           0 :   fd_epoch_info_msg_t *    epoch_info_msg = (fd_epoch_info_msg_t *)fd_type_pun( epoch_info_msg_out );
     105           0 :   fd_vote_stake_weight_t * stake_weights  = fd_epoch_info_msg_stake_weights( epoch_info_msg );
     106             : 
     107           0 :   epoch_info_msg->epoch             = epoch;
     108           0 :   epoch_info_msg->start_slot        = fd_epoch_slot0( epoch_schedule, epoch );
     109           0 :   epoch_info_msg->slot_cnt          = fd_epoch_slot_cnt( epoch_schedule, epoch );
     110           0 :   epoch_info_msg->ns_per_slot       = FD_SLOT_PARAMS_400MS.ns_per_slot;
     111             : 
     112           0 :   fd_memset( &epoch_info_msg->features, 0xFF, sizeof(fd_features_t) );
     113             : 
     114           0 :   ulong idx = 0UL;
     115           0 :   for( ulong i=0UL; i<epoch_stakes->vote_stakes_len; i++ ) {
     116           0 :     ulong stake = epoch_stakes->vote_stakes[ i ].stake;
     117           0 :     if( FD_UNLIKELY( !stake ) ) continue;
     118           0 :     stake_weights[ idx ].stake = stake;
     119           0 :     memcpy( stake_weights[ idx ].id_key.uc, epoch_stakes->vote_stakes[ i ].identity, sizeof(fd_pubkey_t) );
     120           0 :     memcpy( stake_weights[ idx ].vote_key.uc, epoch_stakes->vote_stakes[ i ].vote, sizeof(fd_pubkey_t) );
     121           0 :     idx++;
     122           0 :   }
     123           0 :   epoch_info_msg->staked_vote_cnt = idx;
     124           0 :   sort_vote_weights_by_stake_vote_inplace( stake_weights, idx );
     125             : 
     126           0 :   fd_stake_weight_t * id_weights = fd_epoch_info_msg_id_weights( epoch_info_msg );
     127           0 :   epoch_info_msg->staked_id_cnt = compute_id_weights_from_vote_weights( id_weights, stake_weights, epoch_info_msg->staked_vote_cnt );
     128           0 :   FD_TEST( idx<=MAX_SHRED_DESTS );
     129             : 
     130           0 :   epoch_info_msg->epoch_schedule = *epoch_schedule;
     131           0 :   return fd_epoch_info_msg_sz( epoch_info_msg->staked_vote_cnt, epoch_info_msg->staked_id_cnt );
     132           0 : }
     133             : 
     134             : /* repair_load_manifest loads the snapshot manifest from disk and
     135             :    pre-populates the snapin_manif and replay_epoch dcache links so
     136             :    that consumer tiles see the data on their first poll cycle. */
     137             : static void
     138             : repair_load_manifest( fd_topo_t *  topo,
     139           0 :                       char const * manifest_path ) {
     140           0 :   if( FD_UNLIKELY( !manifest_path || !manifest_path[0] ) ) return;
     141             : 
     142             :   /* Parse manifest */
     143             : 
     144           0 :   int fd = open( manifest_path, O_RDONLY );
     145           0 :   if( FD_UNLIKELY( fd<0 ) ) FD_LOG_ERR(( "open(%s) failed (%d-%s)", manifest_path, errno, fd_io_strerror( errno ) ));
     146             : 
     147           0 :   fd_snapshot_manifest_t * manifest = aligned_alloc( alignof(fd_snapshot_manifest_t), sizeof(fd_snapshot_manifest_t) );
     148           0 :   FD_TEST( manifest );
     149           0 :   for( ulong i=0UL; i<FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN; i++ ) manifest->epoch_stakes[i].epoch = ULONG_MAX;
     150             : 
     151           0 :   uchar * buf = aligned_alloc( 128UL, MANIFEST_LOAD_MAX_SZ );
     152           0 :   FD_TEST( buf );
     153           0 :   ulong buf_sz = 0;
     154           0 :   FD_TEST( !fd_io_read( fd, buf, 0UL, MANIFEST_LOAD_MAX_SZ-1UL, &buf_sz ) );
     155           0 :   close( fd );
     156             : 
     157           0 :   fd_ssmanifest_parser_t * parser = fd_ssmanifest_parser_join( fd_ssmanifest_parser_new(
     158           0 :       aligned_alloc( fd_ssmanifest_parser_align(), fd_ssmanifest_parser_footprint() ) ) );
     159           0 :   FD_TEST( parser );
     160           0 :   fd_ssmanifest_parser_init( parser, manifest );
     161           0 :   int parser_err = fd_ssmanifest_parser_consume( parser, buf, buf_sz );
     162           0 :   FD_TEST( parser_err!=FD_SSMANIFEST_PARSER_ADVANCE_ERROR );
     163           0 :   FD_TEST( fd_ssmanifest_parser_fini( parser )==FD_SSMANIFEST_PARSER_ADVANCE_DONE );
     164           0 :   free( parser );
     165           0 :   free( buf );
     166             : 
     167           0 :   FD_LOG_NOTICE(( "manifest bank slot %lu", manifest->slot ));
     168           0 :   FD_TEST( !repair_verify_epoch_stakes( manifest ) );
     169             : 
     170             :   /* Update root_slot fseq */
     171             : 
     172           0 :   ulong root_slot_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "root_slot" );
     173           0 :   if( FD_LIKELY( root_slot_obj_id!=ULONG_MAX ) ) {
     174           0 :     ulong * root_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, root_slot_obj_id ) );
     175           0 :     FD_TEST( root_fseq );
     176           0 :     fd_fseq_update( root_fseq, manifest->slot );
     177           0 :   }
     178             : 
     179             :   /* Publish manifest to snapin_manif dcache */
     180             : 
     181           0 :   ulong snap_link_idx = fd_topo_find_link( topo, "snapin_manif", 0UL );
     182           0 :   FD_TEST( snap_link_idx!=ULONG_MAX );
     183           0 :   fd_topo_link_t * snap_link = &topo->links[ snap_link_idx ];
     184           0 :   fd_wksp_t *      snap_mem  = topo->workspaces[ topo->objs[ snap_link->dcache_obj_id ].wksp_id ].wksp;
     185           0 :   ulong snap_chunk0 = fd_dcache_compact_chunk0( snap_mem, snap_link->dcache );
     186           0 :   ulong snap_wmark  = fd_dcache_compact_wmark ( snap_mem, snap_link->dcache, snap_link->mtu );
     187           0 :   ulong snap_chunk  = snap_chunk0;
     188             : 
     189           0 :   uchar * snap_dst = fd_chunk_to_laddr( snap_mem, snap_chunk );
     190           0 :   memcpy( snap_dst, manifest, sizeof(fd_snapshot_manifest_t) );
     191           0 :   fd_mcache_publish( snap_link->mcache, snap_link->depth, 0UL,
     192           0 :                      fd_ssmsg_sig( FD_SSMSG_MANIFEST_INCREMENTAL ),
     193           0 :                      snap_chunk, sizeof(fd_snapshot_manifest_t), 0UL, 0UL, 0UL );
     194           0 :   snap_chunk = fd_dcache_compact_next( snap_chunk, sizeof(fd_snapshot_manifest_t), snap_chunk0, snap_wmark );
     195             : 
     196           0 :   fd_mcache_publish( snap_link->mcache, snap_link->depth, 1UL,
     197           0 :                      fd_ssmsg_sig( FD_SSMSG_DONE ), 0UL, 0UL, 0UL, 0UL, 0UL );
     198             : 
     199             :   /* Publish epoch stake weights to replay_epoch dcache */
     200             : 
     201           0 :   ulong epoch_link_idx = fd_topo_find_link( topo, "replay_epoch", 0UL );
     202           0 :   FD_TEST( epoch_link_idx!=ULONG_MAX );
     203           0 :   fd_topo_link_t * epoch_link = &topo->links[ epoch_link_idx ];
     204           0 :   fd_wksp_t *      epoch_mem  = topo->workspaces[ topo->objs[ epoch_link->dcache_obj_id ].wksp_id ].wksp;
     205           0 :   ulong epoch_chunk0 = fd_dcache_compact_chunk0( epoch_mem, epoch_link->dcache );
     206           0 :   ulong epoch_wmark  = fd_dcache_compact_wmark ( epoch_mem, epoch_link->dcache, epoch_link->mtu );
     207           0 :   ulong epoch_chunk  = epoch_chunk0;
     208           0 :   ulong epoch_seq    = 0UL;
     209             : 
     210             :   /* Construct fd_epoch_schedule_t field-by-field rather than type-punning
     211             :      from the unpacked manifest struct (fd_epoch_schedule_t is packed). */
     212           0 :   fd_epoch_schedule_t schedule_local;
     213           0 :   schedule_local.slots_per_epoch             = manifest->epoch_schedule_params.slots_per_epoch;
     214           0 :   schedule_local.leader_schedule_slot_offset = manifest->epoch_schedule_params.leader_schedule_slot_offset;
     215           0 :   schedule_local.warmup                      = manifest->epoch_schedule_params.warmup;
     216           0 :   schedule_local.first_normal_epoch          = manifest->epoch_schedule_params.first_normal_epoch;
     217           0 :   schedule_local.first_normal_slot           = manifest->epoch_schedule_params.first_normal_slot;
     218           0 :   fd_epoch_schedule_t const * schedule = &schedule_local;
     219           0 :   ulong epoch = fd_slot_to_epoch( schedule, manifest->slot, NULL );
     220             : 
     221           0 :   ulong epoch_stakes_base      = epoch > 0UL ? epoch - 1UL : 0UL;
     222           0 :   ulong leader_schedule_epoch  = fd_slot_to_leader_schedule_epoch( schedule, manifest->slot );
     223           0 :   ulong cur_idx = epoch - epoch_stakes_base;
     224           0 :   FD_TEST( cur_idx < FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN );
     225             : 
     226           0 :   ulong * epoch_dst = fd_chunk_to_laddr( epoch_mem, epoch_chunk );
     227           0 :   ulong epoch_sz = repair_generate_epoch_info_msg( epoch, schedule, &manifest->epoch_stakes[cur_idx], epoch_dst );
     228           0 :   fd_mcache_publish( epoch_link->mcache, epoch_link->depth, epoch_seq,
     229           0 :                      4UL, epoch_chunk, epoch_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     230           0 :   epoch_chunk = fd_dcache_compact_next( epoch_chunk, epoch_sz, epoch_chunk0, epoch_wmark );
     231           0 :   epoch_seq++;
     232           0 :   FD_LOG_NOTICE(( "sending current epoch stake weights - epoch: %lu", epoch ));
     233             : 
     234           0 :   if( leader_schedule_epoch >= epoch + 1UL ) {
     235           0 :     ulong next_idx = epoch + 1UL - epoch_stakes_base;
     236           0 :     FD_TEST( next_idx < FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN );
     237             : 
     238           0 :     epoch_dst = fd_chunk_to_laddr( epoch_mem, epoch_chunk );
     239           0 :     epoch_sz = repair_generate_epoch_info_msg( epoch + 1UL, schedule, &manifest->epoch_stakes[next_idx], epoch_dst );
     240           0 :     fd_mcache_publish( epoch_link->mcache, epoch_link->depth, epoch_seq,
     241           0 :                        4UL, epoch_chunk, epoch_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     242           0 :     epoch_chunk = fd_dcache_compact_next( epoch_chunk, epoch_sz, epoch_chunk0, epoch_wmark );
     243           0 :     epoch_seq++;
     244           0 :     FD_LOG_NOTICE(( "sending next epoch stake weights - epoch: %lu", epoch + 1UL ));
     245           0 :   }
     246           0 :   (void)epoch_chunk;
     247             : 
     248           0 :   free( manifest );
     249           0 : }
     250             : 
     251             : /* repair_topo is a subset of "src/app/firedancer/topology.c" at commit
     252             :    0d8386f4f305bb15329813cfe4a40c3594249e96, slightly modified to work
     253             :    as a repair catchup.  TODO ideally, one should invoke the firedancer
     254             :    topology first, and exclude the parts that are not needed, instead of
     255             :    manually generating new topologies for every command.  This would
     256             :    also guarantee that the catchup is replicating (as close as possible)
     257             :    the full topology. */
     258             : static void
     259           0 : repair_topo( config_t * config ) {
     260           0 :   ulong net_tile_cnt    = config->layout.net_tile_count;
     261           0 :   ulong shred_tile_cnt  = config->layout.shred_tile_count;
     262           0 :   ulong quic_tile_cnt   = config->layout.quic_tile_count;
     263           0 :   ulong sign_tile_cnt   = config->firedancer.layout.sign_tile_count;
     264           0 :   ulong gossvf_tile_cnt = config->firedancer.layout.gossvf_tile_count;
     265             : 
     266           0 :   fd_topo_t * topo = { fd_topob_new( &config->topo, config->name ) };
     267           0 :   topo->max_page_size = fd_cstr_to_shmem_page_sz( config->hugetlbfs.max_page_size );
     268           0 :   topo->gigantic_page_threshold = config->hugetlbfs.gigantic_page_threshold_mib << 20;
     269             : 
     270           0 :   ulong tile_to_cpu[ FD_TILE_MAX ] = {0};
     271           0 :   ushort parsed_tile_to_cpu[ FD_TILE_MAX ];
     272             :   /* Unassigned tiles will be floating, unless auto topology is enabled. */
     273           0 :   for( ulong i=0UL; i<FD_TILE_MAX; i++ ) parsed_tile_to_cpu[ i ] = USHORT_MAX;
     274             : 
     275           0 :   int is_auto_affinity = !strcmp( config->layout.affinity, "auto" );
     276           0 :   int is_bench_auto_affinity = !strcmp( config->development.bench.affinity, "auto" );
     277             : 
     278           0 :   if( FD_UNLIKELY( is_auto_affinity != is_bench_auto_affinity ) ) {
     279           0 :     FD_LOG_ERR(( "The CPU affinity string in the configuration file under [layout.affinity] and [development.bench.affinity] must all be set to 'auto' or all be set to a specific CPU affinity string." ));
     280           0 :   }
     281             : 
     282           0 :   fd_topo_cpus_t cpus[1];
     283           0 :   fd_topo_cpus_init( cpus );
     284             : 
     285           0 :   ulong affinity_tile_cnt = 0UL;
     286           0 :   if( FD_LIKELY( !is_auto_affinity ) ) affinity_tile_cnt = fd_topob_parse_affinity_cstr( config->layout.affinity, parsed_tile_to_cpu, 0 );
     287             : 
     288           0 :   for( ulong i=0UL; i<affinity_tile_cnt; i++ ) {
     289           0 :     if( FD_UNLIKELY( parsed_tile_to_cpu[ i ]!=USHORT_MAX && parsed_tile_to_cpu[ i ]>=cpus->cpu_cnt ) )
     290           0 :       FD_LOG_ERR(( "The CPU affinity string in the configuration file under [layout.affinity] specifies a CPU index of %hu, but the system "
     291           0 :                   "only has %lu CPUs. You should either change the CPU allocations in the affinity string, or increase the number of CPUs "
     292           0 :                   "in the system.",
     293           0 :                   parsed_tile_to_cpu[ i ], cpus->cpu_cnt ));
     294           0 :     tile_to_cpu[ i ] = fd_ulong_if( parsed_tile_to_cpu[ i ]==USHORT_MAX, ULONG_MAX, (ulong)parsed_tile_to_cpu[ i ] );
     295           0 :   }
     296             : 
     297           0 :   fd_core_subtopo(   config, tile_to_cpu );
     298           0 :   fd_gossip_subtopo( config, tile_to_cpu );
     299             : 
     300             :   /*             topo, name */
     301           0 :   fd_topob_wksp( topo, "net_shred"    );
     302           0 :   fd_topob_wksp( topo, "net_repair"   );
     303           0 :   fd_topob_wksp( topo, "net_quic"     );
     304             : 
     305           0 :   fd_topob_wksp( topo, "shred_out"    );
     306           0 :   fd_topob_wksp( topo, "replay_epoch" );
     307             : 
     308           0 :   fd_topob_wksp( topo, "poh_shred"    );
     309             : 
     310           0 :   fd_topob_wksp( topo, "shred_sign"   );
     311           0 :   fd_topob_wksp( topo, "sign_shred"   );
     312             : 
     313           0 :   fd_topob_wksp( topo, "repair_sign"  );
     314           0 :   fd_topob_wksp( topo, "sign_repair"  );
     315           0 :   fd_topob_wksp( topo, "rnonce"       );
     316           0 :   fd_topob_wksp( topo, "repair_out"  );
     317             : 
     318           0 :   fd_topob_wksp( topo, "txsend_out"   );
     319             : 
     320           0 :   fd_topob_wksp( topo, "shred"        );
     321           0 :   fd_topob_wksp( topo, "repair"       );
     322           0 :   fd_topob_wksp( topo, "fec_sets"     );
     323           0 :   fd_topob_wksp( topo, "snapin_manif" );
     324             : 
     325           0 :   fd_topob_wksp( topo, "genesi_out"   ); /* mock genesi_out for ipecho */
     326             : 
     327           0 :   fd_topob_wksp( topo, "tower_out"    ); /* mock tower_out for confirmation msgs. Not needed for any topo except eqvoc. */
     328             : 
     329           0 :   #define FOR(cnt) for( ulong i=0UL; i<cnt; i++ )
     330             : 
     331           0 :   ulong pending_fec_shreds_depth = fd_ulong_min( fd_ulong_pow2_up( config->tiles.shred.max_pending_shred_sets * FD_REEDSOL_DATA_SHREDS_MAX ), USHORT_MAX + 1 /* dcache max */ );
     332             : 
     333             :   /*                                  topo, link_name,      wksp_name,      depth,                                    mtu,                           burst */
     334           0 :   FOR(quic_tile_cnt)   fd_topob_link( topo, "quic_net",     "net_quic",     config->net.ingress_buffer_size,          FD_NET_MTU,                    1UL );
     335           0 :   FOR(shred_tile_cnt)  fd_topob_link( topo, "shred_net",    "net_shred",    config->net.ingress_buffer_size,          FD_NET_MTU,                    1UL );
     336             : 
     337           0 :   /**/                 fd_topob_link( topo, "replay_epoch", "replay_epoch", 16UL,                                     FD_EPOCH_OUT_MTU,              1UL );
     338             : 
     339           0 :   FOR(shred_tile_cnt)  fd_topob_link( topo, "shred_sign",   "shred_sign",   128UL,                                    32UL,                          1UL );
     340           0 :   FOR(shred_tile_cnt)  fd_topob_link( topo, "sign_shred",   "sign_shred",   128UL,                                    64UL,                          1UL );
     341             : 
     342           0 :   /**/                 fd_topob_link( topo, "repair_net",   "net_repair",   config->net.ingress_buffer_size,          FD_NET_MTU,                    1UL );
     343             : 
     344           0 :   FOR(shred_tile_cnt)  fd_topob_link( topo, "shred_out",    "shred_out",    pending_fec_shreds_depth,                 sizeof(fd_shred_message_t),    2UL /* at most 2 msgs per after_frag */ );
     345           0 :   FOR(sign_tile_cnt-1) fd_topob_link( topo, "repair_sign",  "repair_sign",  256UL,                                    FD_REPAIR_MAX_PREIMAGE_SZ,     1UL );
     346           0 :   FOR(sign_tile_cnt-1) fd_topob_link( topo, "sign_repair",  "sign_repair",  128UL,                                    sizeof(fd_ed25519_sig_t),      1UL );
     347             : 
     348             :   /**/                 fd_topob_link( topo, "repair_out",   "repair_out",   128UL,                                    sizeof(fd_fec_complete_t),   1UL );
     349             : 
     350           0 :   /**/                 fd_topob_link( topo, "poh_shred",    "poh_shred",    16384UL,                                  FD_POH_SHRED_MTU,              1UL );
     351             : 
     352           0 :   /**/                 fd_topob_link( topo, "txsend_out",   "txsend_out",   128UL,                                    FD_TXN_MTU,                    1UL );
     353             : 
     354             :   /**/                 fd_topob_link( topo, "snapin_manif", "snapin_manif", 2UL,                                      sizeof(fd_snapshot_manifest_t),1UL );
     355             : 
     356           0 :   /**/                 fd_topob_link( topo, "genesi_out",   "genesi_out",   1UL,                                      FD_GENESIS_TILE_MTU,            1UL );
     357           0 :   /**/                 fd_topob_link( topo, "tower_out",    "tower_out",    1024UL,                                   sizeof(fd_tower_msg_t),         1UL );
     358             : 
     359           0 :   FOR(net_tile_cnt) fd_topos_net_rx_link( topo, "net_repair", i, config->net.ingress_buffer_size );
     360           0 :   FOR(net_tile_cnt) fd_topos_net_rx_link( topo, "net_quic",   i, config->net.ingress_buffer_size );
     361           0 :   FOR(net_tile_cnt) fd_topos_net_rx_link( topo, "net_shred",  i, config->net.ingress_buffer_size );
     362             : 
     363             :   /*                                              topo, tile_name, tile_wksp, metrics_wksp, cpu_idx,                       is_agave, uses_id_keyswitch, uses_av_keyswitch */
     364           0 :   FOR(shred_tile_cnt)              fd_topob_tile( topo, "shred",   "shred",   "metric_in",  tile_to_cpu[ topo->tile_cnt ], 0,        1,                 0 );
     365           0 :   fd_topo_tile_t * repair_tile =   fd_topob_tile( topo, "repair",  "repair",  "metric_in",  tile_to_cpu[ topo->tile_cnt ], 0,        1,                 0 );
     366             : 
     367             :   /* Setup a shared wksp object for fec sets. */
     368             : 
     369           0 :   ulong shred_depth = 65536UL; /* from fdctl/topology.c shred_store link. MAKE SURE TO KEEP IN SYNC. */
     370           0 :   ulong fec_set_cnt = 2UL*shred_depth + config->tiles.shred.max_pending_shred_sets + 6UL;
     371           0 :   ulong fec_sets_sz = fec_set_cnt*sizeof(fd_fec_set_t); /* mirrors # of dcache entries in frankendancer */
     372           0 :   fd_topo_obj_t * fec_sets_obj = setup_topo_fec_sets( topo, "fec_sets", shred_tile_cnt*fec_sets_sz );
     373           0 :   for( ulong i=0UL; i<shred_tile_cnt; i++ ) {
     374           0 :     fd_topo_tile_t * shred_tile = &topo->tiles[ fd_topo_find_tile( topo, "shred", i ) ];
     375           0 :     fd_topob_tile_uses( topo, shred_tile,  fec_sets_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
     376           0 :   }
     377           0 :   fd_topob_tile_uses( topo, repair_tile, fec_sets_obj, FD_SHMEM_JOIN_MODE_READ_ONLY );
     378           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, fec_sets_obj->id, "fec_sets" ) );
     379             : 
     380             :   /* There's another special fseq that's used to communicate the shred
     381             :     version from the Agave boot path to the shred tile. */
     382           0 :   fd_topo_obj_t * poh_shred_obj = fd_topob_obj( topo, "fseq", "poh_shred" );
     383           0 :   fd_topo_tile_t * poh_tile = &topo->tiles[ fd_topo_find_tile( topo, "gossip", 0UL ) ];
     384           0 :   fd_topob_tile_uses( topo, poh_tile, poh_shred_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
     385             : 
     386           0 :   for( ulong i=0UL; i<shred_tile_cnt; i++ ) {
     387           0 :     fd_topo_tile_t * shred_tile = &topo->tiles[ fd_topo_find_tile( topo, "shred", i ) ];
     388           0 :     fd_topob_tile_uses( topo, shred_tile, poh_shred_obj, FD_SHMEM_JOIN_MODE_READ_ONLY );
     389           0 :   }
     390           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, poh_shred_obj->id, "poh_shred" ) );
     391             : 
     392           0 :   if( FD_LIKELY( !is_auto_affinity ) ) {
     393           0 :     if( FD_UNLIKELY( affinity_tile_cnt<topo->tile_cnt ) )
     394           0 :       FD_LOG_ERR(( "The topology you are using has %lu tiles, but the CPU affinity specified in the config tile as [layout.affinity] only provides for %lu cores. "
     395           0 :                   "You should either increase the number of cores dedicated to Firedancer in the affinity string, or decrease the number of cores needed by reducing "
     396           0 :                   "the total tile count. You can reduce the tile count by decreasing individual tile counts in the [layout] section of the configuration file.",
     397           0 :                   topo->tile_cnt, affinity_tile_cnt ));
     398           0 :     if( FD_UNLIKELY( affinity_tile_cnt>topo->tile_cnt ) )
     399           0 :       FD_LOG_WARNING(( "The topology you are using has %lu tiles, but the CPU affinity specified in the config tile as [layout.affinity] provides for %lu cores. "
     400           0 :                       "Not all cores in the affinity will be used by Firedancer. You may wish to increase the number of tiles in the system by increasing "
     401           0 :                       "individual tile counts in the [layout] section of the configuration file.",
     402           0 :                       topo->tile_cnt, affinity_tile_cnt ));
     403           0 :   }
     404             : 
     405             :   /*                                      topo, tile_name, tile_kind_id, fseq_wksp,   link_name,      link_kind_id,  reliable,            polled */
     406           0 :   for( ulong j=0UL; j<shred_tile_cnt; j++ )
     407           0 :                   fd_topos_tile_in_net(  topo,                           "metric_in", "shred_net",     j,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED ); /* No reliable consumers of networking fragments, may be dropped or overrun */
     408           0 :   for( ulong j=0UL; j<quic_tile_cnt; j++ )
     409           0 :                   {fd_topos_tile_in_net(  topo,                          "metric_in", "quic_net",      j,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED );} /* No reliable consumers of networking fragments, may be dropped or overrun */
     410             : 
     411           0 :   /**/            fd_topob_tile_in(      topo, "gossip",  0UL,           "metric_in", "txsend_out",    0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED );
     412             : 
     413           0 :   /**/            fd_topos_tile_in_net(  topo,                           "metric_in", "repair_net",    0UL,          FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED ); /* No reliable consumers of networking fragments, may be dropped or overrun */
     414             : 
     415           0 :   FOR(shred_tile_cnt) for( ulong j=0UL; j<net_tile_cnt; j++ )
     416           0 :                        fd_topob_tile_in(  topo, "shred",  i,             "metric_in", "net_shred",     j,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED ); /* No reliable consumers of networking fragments, may be dropped or overrun */
     417           0 :   FOR(shred_tile_cnt)  fd_topob_tile_in(  topo, "shred",  i,             "metric_in", "poh_shred",     0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED );
     418           0 :   FOR(shred_tile_cnt)  fd_topob_tile_in(  topo, "shred",  i,             "metric_in", "replay_epoch",  0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED );
     419           0 :   FOR(shred_tile_cnt)  fd_topob_tile_in(  topo, "shred",  i,             "metric_in", "gossip_out",    0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED );
     420           0 :   FOR(shred_tile_cnt)  fd_topob_tile_out( topo, "shred",  i,                          "shred_out",     i                                                    );
     421           0 :   FOR(shred_tile_cnt)  fd_topob_tile_out( topo, "shred",  i,                          "shred_net",     i                                                    );
     422           0 :   FOR(shred_tile_cnt)  fd_topob_tile_in ( topo, "shred",  i,             "metric_in", "ipecho_out",    0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED );
     423             : 
     424             :   /**/                 fd_topob_tile_out( topo, "repair",  0UL,                       "repair_net",    0UL                                                  );
     425             : 
     426             :   /* Sign links don't need to be reliable because they are synchronous,
     427             :     so there's at most one fragment in flight at a time anyway.  The
     428             :     sign links are also not polled by the mux, instead the tiles will
     429             :     read the sign responses out of band in a dedicated spin loop. */
     430           0 :   for( ulong i=0UL; i<shred_tile_cnt; i++ ) {
     431           0 :     /**/               fd_topob_tile_in(  topo, "sign",   0UL,           "metric_in", "shred_sign",    i,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED   );
     432           0 :     /**/               fd_topob_tile_out( topo, "shred",  i,                          "shred_sign",    i                                                    );
     433           0 :     /**/               fd_topob_tile_in(  topo, "shred",  i,             "metric_in", "sign_shred",    i,            FD_TOPOB_UNRELIABLE, FD_TOPOB_UNPOLLED );
     434           0 :     /**/               fd_topob_tile_out( topo, "sign",   0UL,                        "sign_shred",    i                                                    );
     435           0 :   }
     436           0 :   FOR(gossvf_tile_cnt) fd_topob_tile_in ( topo, "gossvf",   i,           "metric_in", "replay_epoch",  0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     437             : 
     438           0 :   /**/                 fd_topob_tile_in ( topo, "gossip",   0UL,         "metric_in", "replay_epoch",  0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     439             : 
     440           0 :   FOR(net_tile_cnt)    fd_topob_tile_in(  topo, "repair",  0UL,          "metric_in", "net_repair",    i,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED   ); /* No reliable consumers of networking fragments, may be dropped or overrun */
     441           0 :   /**/                 fd_topob_tile_in(  topo, "repair",  0UL,          "metric_in", "gossip_out",    0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     442           0 :                        fd_topob_tile_in(  topo, "repair",  0UL,          "metric_in", "snapin_manif",  0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     443           0 :   FOR(shred_tile_cnt)  fd_topob_tile_in(  topo, "repair",  0UL,          "metric_in", "shred_out",     i,            FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     444           0 :   FOR(sign_tile_cnt-1) fd_topob_tile_out( topo, "repair", 0UL,                        "repair_sign",   i                                                    );
     445           0 :   FOR(sign_tile_cnt-1) fd_topob_tile_in ( topo, "sign",   i+1,           "metric_in", "repair_sign",   i,            FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     446           0 :   FOR(sign_tile_cnt-1) fd_topob_tile_out( topo, "sign",   i+1,                        "sign_repair",   i                                                    );
     447           0 :   FOR(sign_tile_cnt-1) fd_topob_tile_in ( topo, "repair", 0UL,           "metric_in", "sign_repair",   i,            FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED   );
     448             : 
     449             :   /**/                 fd_topob_tile_out( topo, "repair", 0UL,                        "repair_out",   0UL                                                   );
     450           0 :   /**/                 fd_topob_tile_in ( topo, "gossip", 0UL,           "metric_in", "sign_gossip",   0UL,          FD_TOPOB_UNRELIABLE, FD_TOPOB_UNPOLLED );
     451           0 :   /**/                 fd_topob_tile_in ( topo, "ipecho", 0UL,           "metric_in", "genesi_out",    0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     452           0 :   /**/                 fd_topob_tile_in ( topo, "repair", 0UL,           "metric_in", "tower_out",     0UL,          FD_TOPOB_RELIABLE,   FD_TOPOB_POLLED   );
     453             : 
     454             :   /* Repair and shred share a secret they use to generate the nonces.
     455             :     It's not super security sensitive, but for good hygiene, we make it
     456             :     an object. */
     457           0 :   if( 1 /* just restrict the scope for these variables in this big function */ ) {
     458           0 :     fd_topo_obj_t * rnonce_ss_obj = fd_topob_obj( topo, "rnonce_ss", "rnonce" );
     459           0 :     fd_topo_tile_t * repair_tile = &topo->tiles[ fd_topo_find_tile( topo, "repair", 0UL ) ];
     460           0 :     fd_topob_tile_uses( topo, repair_tile, rnonce_ss_obj, FD_SHMEM_JOIN_MODE_READ_ONLY );
     461           0 :     for( ulong i=0UL; i<shred_tile_cnt; i++ ) {
     462           0 :       fd_topo_tile_t * shred_tile = &topo->tiles[ fd_topo_find_tile( topo, "shred", i ) ];
     463           0 :       fd_topob_tile_uses( topo, shred_tile, rnonce_ss_obj, FD_SHMEM_JOIN_MODE_READ_ONLY );
     464           0 :     }
     465           0 :     FD_TEST( fd_pod_insertf_ulong( topo->props, rnonce_ss_obj->id, "rnonce_ss" ) );
     466           0 :   }
     467             : 
     468           0 :   FD_TEST( fd_link_permit_no_producers( topo, "quic_net"      ) == quic_tile_cnt );
     469           0 :   FD_TEST( fd_link_permit_no_producers( topo, "poh_shred"     ) == 1UL           );
     470           0 :   FD_TEST( fd_link_permit_no_producers( topo, "txsend_out"    ) == 1UL           );
     471           0 :   FD_TEST( fd_link_permit_no_producers( topo, "genesi_out"    ) == 1UL           );
     472           0 :   FD_TEST( fd_link_permit_no_producers( topo, "tower_out"     ) == 1UL           );
     473           0 :   FD_TEST( fd_link_permit_no_producers( topo, "replay_epoch"  ) == 1UL           );
     474           0 :   FD_TEST( fd_link_permit_no_producers( topo, "snapin_manif"  ) == 1UL           );
     475           0 :   FD_TEST( fd_link_permit_no_consumers( topo, "net_quic"     ) == net_tile_cnt  );
     476           0 :   FD_TEST( fd_link_permit_no_consumers( topo, "repair_out"   ) == 1UL           );
     477             : 
     478           0 :   config->tiles.txsend.txsend_src_port = 0; /* disable txsend */
     479             : 
     480           0 :   FOR(net_tile_cnt) fd_topos_net_tile_finish( topo, i );
     481             : 
     482           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     483           0 :     fd_topo_tile_t * tile = &topo->tiles[ i ];
     484           0 :     fd_topo_configure_tile( tile, config );
     485           0 :   }
     486             : 
     487           0 :   if( FD_UNLIKELY( is_auto_affinity ) ) fd_topob_auto_layout( topo, 0 );
     488             : 
     489           0 :   fd_topob_finish( topo, CALLBACKS );
     490             : 
     491           0 :   config->topo = *topo;
     492           0 : }
     493             : 
     494             : static char *
     495           0 : fmt_count( char buf[ static 64 ], ulong count ) {
     496           0 :   char tmp[ 64 ];
     497           0 :   if( FD_LIKELY( count<1000UL ) ) FD_TEST( fd_cstr_printf_check( tmp, 64UL, NULL, "%lu", count ) );
     498           0 :   else if( FD_LIKELY( count<1000000UL ) ) FD_TEST( fd_cstr_printf_check( tmp, 64UL, NULL, "%.1f K", (double)count/1000.0 ) );
     499           0 :   else if( FD_LIKELY( count<1000000000UL ) ) FD_TEST( fd_cstr_printf_check( tmp, 64UL, NULL, "%.1f M", (double)count/1000000.0 ) );
     500             : 
     501           0 :   FD_TEST( fd_cstr_printf_check( buf, 64UL, NULL, "%12s", tmp ) );
     502           0 :   return buf;
     503           0 : }
     504             : 
     505             : static void
     506             : print_histogram_buckets( volatile ulong * metrics,
     507             :                          ulong offset,
     508             :                          int converter,
     509             :                          double histmin,
     510             :                          double histmax,
     511           0 :                          char * title ) {
     512           0 :   fd_histf_t hist[1];
     513             : 
     514             :   /* Create histogram structure only to get bucket edges for display */
     515           0 :   if( FD_LIKELY( converter == FD_METRICS_CONVERTER_SECONDS ) ) {
     516             :     /* For SLOT_COMPLETE_TIME: min=0.2, max=2.0 seconds */
     517           0 :     FD_TEST( fd_histf_new( hist, fd_metrics_convert_seconds_to_ticks( histmin ), fd_metrics_convert_seconds_to_ticks( histmax ) ) );
     518           0 :   } else if( FD_LIKELY( converter == FD_METRICS_CONVERTER_NONE ) ) {
     519             :     /* For non-time histograms, we'd need the actual min/max values */
     520           0 :     FD_TEST( fd_histf_new( hist, (ulong)histmin, (ulong)histmax ) );
     521           0 :   } else {
     522           0 :     FD_LOG_ERR(( "unknown converter %i", converter ));
     523           0 :   }
     524             : 
     525           0 :   printf( " +---------------------+--------------------+--------------+\n" );
     526           0 :   printf( " | %-19s |                    | Count        |\n", title );
     527           0 :   printf( " +---------------------+--------------------+--------------+\n" );
     528             : 
     529           0 :   ulong total_count = 0;
     530           0 :   for( ulong k = 0; k < FD_HISTF_BUCKET_CNT; k++ ) {
     531           0 :     ulong bucket_count = metrics[ offset + k ];
     532           0 :     total_count += bucket_count;
     533           0 :   }
     534             : 
     535           0 :   for( ulong k = 0; k < FD_HISTF_BUCKET_CNT; k++ ) {
     536             :     /* Get individual bucket count directly from metrics array */
     537           0 :     ulong bucket_count = metrics[ offset + k ];
     538             : 
     539           0 :     char * le_str;
     540           0 :     char le_buf[ 64 ];
     541           0 :     if( FD_UNLIKELY( k == FD_HISTF_BUCKET_CNT - 1UL ) ) {
     542           0 :       le_str = "+Inf";
     543           0 :     } else {
     544           0 :       ulong edge = fd_histf_right( hist, k );
     545           0 :       if( FD_LIKELY( converter == FD_METRICS_CONVERTER_SECONDS ) ) {
     546           0 :         double edgef = fd_metrics_convert_ticks_to_seconds( edge - 1 );
     547           0 :         FD_TEST( fd_cstr_printf_check( le_buf, sizeof( le_buf ), NULL, "%.3f", edgef ) );
     548           0 :       } else {
     549           0 :         FD_TEST( fd_cstr_printf_check( le_buf, sizeof( le_buf ), NULL, "%.3f", (double)(edge - 1) / 1000000.0 ) );
     550           0 :       }
     551           0 :       le_str = le_buf;
     552           0 :     }
     553             : 
     554           0 :     char count_buf[ 64 ];
     555           0 :     fmt_count( count_buf, bucket_count );
     556             : 
     557             :     /* Match visual bar length to the %-18s display column width. */
     558           0 :     char  bar_buf[ 19 ];
     559           0 :     ulong bar_max = sizeof( bar_buf ) - 1UL;
     560           0 :     if( bucket_count > 0 && total_count > 0 ) {
     561           0 :       ulong bar_length = (bucket_count * bar_max) / total_count;
     562           0 :       if( bar_length == 0 ) bar_length = 1;
     563           0 :       if( bar_length > bar_max ) bar_length = bar_max;
     564           0 :       for( ulong i = 0; i < bar_length; i++ ) { bar_buf[ i ] = '|'; }
     565           0 :       bar_buf[ bar_length ] = '\0';
     566           0 :     } else {
     567           0 :       bar_buf[ 0 ] = '\0';
     568           0 :     }
     569             : 
     570           0 :     printf( " | %-19s | %-18s | %s |\n", le_str, bar_buf, count_buf );
     571           0 :   }
     572             : 
     573             :   /* Print sum and total count */
     574           0 :   char sum_buf[ 64 ];
     575           0 :   char avg_buf[ 64 ];
     576           0 :   if( FD_LIKELY( converter == FD_METRICS_CONVERTER_SECONDS ) ) {
     577           0 :     double sumf = fd_metrics_convert_ticks_to_seconds( metrics[ offset + FD_HISTF_BUCKET_CNT ] );
     578           0 :     FD_TEST( fd_cstr_printf_check( sum_buf, sizeof( sum_buf ), NULL, "%.6f", sumf ) );
     579           0 :     double avg = sumf / (double)total_count;
     580           0 :     FD_TEST( fd_cstr_printf_check( avg_buf, sizeof( avg_buf ), NULL, "%.6f", avg ) );
     581           0 :   } else {
     582           0 :     FD_TEST( fd_cstr_printf_check( sum_buf, sizeof( sum_buf ), NULL, "%lu", metrics[ offset + FD_HISTF_BUCKET_CNT ] ));
     583           0 :   }
     584             : 
     585           0 :   printf( " +---------------------+--------------------+---------------+\n" );
     586           0 :   printf( " | Sum: %-14s | Count: %-11lu | Avg: %-8s |\n", sum_buf, total_count, avg_buf );
     587           0 :   printf( " +---------------------+--------------------+---------------+\n" );
     588           0 : }
     589             : 
     590             : static fd_slot_metrics_t temp_slots[ FD_CATCHUP_METRICS_MAX ];
     591             : 
     592             : static void
     593           0 : print_catchup_slots( fd_wksp_t * repair_tile_wksp, ctx_t * repair_ctx, int verbose, int sort_by_slot ) {
     594           0 :   fd_repair_metrics_t * catchup = repair_ctx->slot_metrics;
     595           0 :   ulong catchup_gaddr = fd_wksp_gaddr_fast( repair_ctx->wksp, catchup );
     596           0 :   fd_repair_metrics_t * catchup_table = (fd_repair_metrics_t *)fd_wksp_laddr( repair_tile_wksp, catchup_gaddr );
     597           0 :   if( FD_LIKELY( sort_by_slot ) ) {
     598           0 :     fd_repair_metrics_print_sorted( catchup_table, verbose, temp_slots );
     599           0 :   } else {
     600           0 :     fd_repair_metrics_print( catchup_table, verbose );
     601           0 :   }
     602           0 : }
     603             : 
     604             : static fd_location_info_t * location_table;
     605             : static fd_pubkey_t peers_copy[ FD_REPAIR_PEER_MAX];
     606             : 
     607             : static ulong
     608           0 : sort_peers_by_latency( fd_policy_peer_map_t * active_table, fd_policy_peer_dlist_t * peers_dlist, fd_policy_peer_dlist_t * peers_wlist, fd_policy_peer_t * peers_arr ) {
     609           0 :   ulong i = 0;
     610           0 :   fd_policy_peer_dlist_iter_t iter = fd_policy_peer_dlist_iter_fwd_init( peers_dlist, peers_arr );
     611           0 :   while( !fd_policy_peer_dlist_iter_done( iter, peers_dlist, peers_arr ) ) {
     612           0 :     fd_policy_peer_t * peer = fd_policy_peer_dlist_iter_ele( iter, peers_dlist, peers_arr );
     613           0 :     if( FD_UNLIKELY( !peer ) ) break;
     614           0 :     peers_copy[ i++ ] = peer->key;
     615           0 :     if( FD_UNLIKELY( i >= FD_REPAIR_PEER_MAX ) ) break;
     616           0 :     iter = fd_policy_peer_dlist_iter_fwd_next( iter, peers_dlist, peers_arr );
     617           0 :   }
     618           0 :   ulong fast_cnt = i;
     619           0 :   iter = fd_policy_peer_dlist_iter_fwd_init( peers_wlist, peers_arr );
     620           0 :   while( !fd_policy_peer_dlist_iter_done( iter, peers_wlist, peers_arr ) ) {
     621           0 :     fd_policy_peer_t * peer = fd_policy_peer_dlist_iter_ele( iter, peers_wlist, peers_arr );
     622           0 :     if( FD_UNLIKELY( !peer ) ) break;
     623           0 :     peers_copy[ i++ ] = peer->key;
     624           0 :     if( FD_UNLIKELY( i >= FD_REPAIR_PEER_MAX ) ) break;
     625           0 :     iter = fd_policy_peer_dlist_iter_fwd_next( iter, peers_wlist, peers_arr );
     626           0 :   }
     627           0 :   FD_LOG_NOTICE(( "Fast peers cnt: %lu. Slow peers cnt: %lu.", fast_cnt, i - fast_cnt ));
     628             : 
     629           0 :   ulong peer_cnt = i;
     630           0 :   for( uint i = 0; i < peer_cnt - 1; i++ ) {
     631           0 :     int swapped = 0;
     632           0 :     for( uint j = 0; j < peer_cnt - 1 - i; j++ ) {
     633           0 :       fd_policy_peer_t const * active_j  = fd_policy_peer_map_ele_query( active_table, &peers_copy[ j ], NULL, peers_arr );
     634           0 :       fd_policy_peer_t const * active_j1 = fd_policy_peer_map_ele_query( active_table, &peers_copy[ j + 1 ], NULL, peers_arr );
     635             : 
     636             :       /* Skip peers with no responses */
     637           0 :       double latency_j  = 10e9;
     638           0 :       double latency_j1 = 10e9;
     639           0 :       if( FD_LIKELY( active_j  && active_j->res_cnt > 0  ) ) latency_j  = ((double)active_j->total_lat / (double)active_j->res_cnt);
     640           0 :       if( FD_LIKELY( active_j1 && active_j1->res_cnt > 0 ) ) latency_j1 = ((double)active_j1->total_lat / (double)active_j1->res_cnt);
     641             : 
     642             :       /* Swap if j has higher latency than j+1 */
     643           0 :       if( latency_j > latency_j1 ) {
     644           0 :         fd_pubkey_t temp    = peers_copy[ j ];
     645           0 :         peers_copy[ j ]     = peers_copy[ j + 1 ];
     646           0 :         peers_copy[ j + 1 ] = temp;
     647           0 :         swapped             = 1;
     648           0 :       }
     649           0 :     }
     650           0 :     if( !swapped ) break;
     651           0 :   }
     652           0 :   return peer_cnt;
     653           0 : }
     654             : 
     655             : static void
     656           0 : print_peer_location_latency( fd_wksp_t * repair_tile_wksp, ctx_t * tile_ctx ) {
     657           0 :   ulong              policy_gaddr  = fd_wksp_gaddr_fast( tile_ctx->wksp, tile_ctx->policy );
     658           0 :   fd_policy_t *      policy        = fd_wksp_laddr     ( repair_tile_wksp, policy_gaddr );
     659           0 :   ulong              peermap_gaddr = fd_wksp_gaddr_fast( tile_ctx->wksp, policy->peers.map  );
     660           0 :   ulong              peerarr_gaddr = fd_wksp_gaddr_fast( tile_ctx->wksp, policy->peers.pool );
     661           0 :   ulong              peerlst_gaddr = fd_wksp_gaddr_fast( tile_ctx->wksp, policy->peers.fast );
     662           0 :   ulong              peerwst_gaddr = fd_wksp_gaddr_fast( tile_ctx->wksp, policy->peers.slow );
     663           0 :   fd_policy_peer_map_t *   peers_map   = (fd_policy_peer_map_t *)  fd_wksp_laddr( repair_tile_wksp, peermap_gaddr );
     664           0 :   fd_policy_peer_dlist_t * peers_dlist = (fd_policy_peer_dlist_t *)fd_wksp_laddr( repair_tile_wksp, peerlst_gaddr );
     665           0 :   fd_policy_peer_dlist_t * peers_wlist = (fd_policy_peer_dlist_t *)fd_wksp_laddr( repair_tile_wksp, peerwst_gaddr );
     666           0 :   fd_policy_peer_t *       peers_arr   = (fd_policy_peer_t *)      fd_wksp_laddr( repair_tile_wksp, peerarr_gaddr );
     667             : 
     668           0 :   ulong peer_cnt = sort_peers_by_latency( peers_map, peers_dlist, peers_wlist, peers_arr );
     669           0 :   printf("\nPeer Location/Latency Information\n");
     670           0 :   printf( "     | %-46s | %-7s | %-8s | %-8s | %-7s | %-7s | %-12s | %s\n", "Pubkey", "Req Cnt", "Req B/s", "Rx B/s", "Rx Rate", "Avg Latency", "Ewma Latency", "Location Info" );
     671           0 :   for( uint i = 0; i < peer_cnt; i++ ) {
     672           0 :     fd_policy_peer_t const * active = fd_policy_peer_map_ele_query( peers_map, &peers_copy[ i ], NULL, peers_arr );
     673           0 :     if( FD_LIKELY( active && active->res_cnt > 0 ) ) {
     674           0 :       fd_location_info_t * info = fd_location_table_query( location_table, active->ip4, NULL );
     675           0 :       char * geolocation = info ? info->location : "";
     676           0 :       double peer_bps    = (double)(active->res_cnt * FD_SHRED_MIN_SZ) / ((double)(active->last_resp_ts - active->first_resp_ts) / 1e9);
     677           0 :       double req_bps     = (double)active->req_cnt * 202 / ((double)(active->last_req_ts - active->first_req_ts) / 1e9);
     678           0 :       FD_BASE58_ENCODE_32_BYTES( active->key.key, key_b58 );
     679           0 :       printf( "%-5u | %-46s | %-7lu | %-8.2f | %-8.2f | %-7.2f | %10.3fms | %10.3fms | %s\n", i, key_b58, active->req_cnt, req_bps, peer_bps, (double)active->res_cnt / (double)active->req_cnt, ((double)active->total_lat / (double)active->res_cnt) / 1e6, (double)active->ewma_lat / 1e6, geolocation );
     680           0 :     }
     681           0 :   }
     682           0 :   printf("\n");
     683           0 :   fflush( stdout );
     684           0 : }
     685             : 
     686             : static void
     687           0 : read_iptable( char * iptable_path, fd_location_info_t * location_table ) {
     688           0 :   int iptable_fd = open( iptable_path, O_RDONLY );
     689           0 :   if( FD_UNLIKELY( iptable_fd<0 ) ) return;
     690             : 
     691             :   /* read iptable line by line */
     692           0 :   if( FD_LIKELY( iptable_fd>=0 ) ) {
     693           0 :     char line[ 256 ];
     694           0 :     uchar istream_buf[256];
     695           0 :     fd_io_buffered_istream_t istream[1];
     696           0 :     fd_io_buffered_istream_init( istream, iptable_fd, istream_buf, sizeof(istream_buf) );
     697           0 :     for(;;) {
     698           0 :       int err;
     699           0 :       if( !fd_io_fgets( line, sizeof(line), istream, &err ) ) break;
     700           0 :       fd_location_info_t location_info;
     701           0 :       sscanf( line, "%lu %[^\n]", &location_info.ip4_addr, location_info.location );
     702           0 :       fd_location_info_t * info = fd_location_table_insert( location_table, location_info.ip4_addr );
     703           0 :       if( FD_UNLIKELY( info==NULL ) ) break;
     704           0 :       memcpy( info->location, location_info.location, sizeof(info->location) );
     705           0 :     }
     706           0 :   }
     707           0 : }
     708             : 
     709             : static void
     710             : print_tile_metrics( volatile ulong * shred_metrics,
     711             :                     volatile ulong * repair_metrics,
     712             :                     volatile ulong * repair_metrics_prev, /* for diffing metrics */
     713             :                     volatile ulong ** repair_net_links,
     714             :                     volatile ulong ** net_shred_links,
     715             :                     ulong   net_tile_cnt,
     716             :                     ulong * last_sent_cnt,
     717             :                     long    last_print_ts,
     718           0 :                     long    now ) {
     719           0 :   char buf2[ 64 ];
     720           0 :   ulong rcvd = shred_metrics [ MIDX( COUNTER, SHRED,  SHRED_REPAIR_RX ) ];
     721           0 :   ulong sent = repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_WINDOW ) ] +
     722           0 :                 repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_HIGHEST_WINDOW ) ] +
     723           0 :                 repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_ORPHAN ) ];
     724           0 :   printf(" Requests received: (%lu/%lu) %.1f%% \n", rcvd, sent, (double)rcvd / (double)sent * 100.0 );
     725           0 :   printf( " +---------------+--------------+\n" );
     726           0 :   printf( " | Request Type  | Count        |\n" );
     727           0 :   printf( " +---------------+--------------+\n" );
     728           0 :   printf( " | Orphan        | %s |\n", fmt_count( buf2, repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_ORPHAN         ) ] ) );
     729           0 :   printf( " | HighestWindow | %s |\n", fmt_count( buf2, repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_HIGHEST_WINDOW ) ] ) );
     730           0 :   printf( " | Index         | %s |\n", fmt_count( buf2, repair_metrics[ MIDX( COUNTER, REPAIR, REQUEST_TX_NEEDED_WINDOW         ) ] ) );
     731           0 :   printf( " +---------------+--------------+\n" );
     732           0 :   printf( " Send Pkt Rate: %s pps\n",  fmt_count( buf2, (ulong)((sent - *last_sent_cnt)*1e9L / (now - last_print_ts) ) ) );
     733           0 :   *last_sent_cnt = sent;
     734             : 
     735             :   /* Sum overrun across all net tiles connected to repair_net */
     736           0 :   ulong total_overrun = repair_net_links[0][ MIDX( COUNTER, LINK, FRAG_POLLING_OVERRUN ) ]; /* coarse double counting prevention */
     737           0 :   ulong total_consumed = 0UL;
     738           0 :   for( ulong i = 0UL; i < net_tile_cnt; i++ ) {
     739           0 :     volatile ulong * ovar_net_metrics = repair_net_links[i];
     740           0 :     total_overrun  += ovar_net_metrics[ MIDX( COUNTER, LINK, FRAG_READING_OVERRUN ) ];
     741           0 :     total_consumed += ovar_net_metrics[ MIDX( COUNTER, LINK, FRAG_CONSUMED ) ]; /* consumed is incremented after after_frag is called */
     742           0 :   }
     743           0 :   printf( " Outgoing requests overrun:  %s\n", fmt_count( buf2, total_overrun  ) );
     744           0 :   printf( " Outgoing requests consumed: %s\n", fmt_count( buf2, total_consumed ) );
     745             : 
     746           0 :   total_overrun  = net_shred_links[0][ MIDX( COUNTER, LINK, FRAG_READING_OVERRUN ) ];
     747           0 :   total_consumed = 0UL;
     748           0 :   for( ulong i = 0UL; i < net_tile_cnt; i++ ) {
     749           0 :     volatile ulong * ovar_net_metrics = net_shred_links[i];
     750           0 :     total_overrun  += ovar_net_metrics[ MIDX( COUNTER, LINK, FRAG_READING_OVERRUN ) ];
     751           0 :     total_consumed += ovar_net_metrics[ MIDX( COUNTER, LINK, FRAG_CONSUMED ) ]; /* shred frag filtering happens manually in after_frag, so no need to index every shred_tile. */
     752           0 :   }
     753             : 
     754           0 :   printf( " Incoming shreds overrun:    %s\n", fmt_count( buf2, total_overrun ) );
     755           0 :   printf( " Incoming shreds consumed:   %s\n", fmt_count( buf2, total_consumed ) );
     756             : 
     757           0 :   print_histogram_buckets( repair_metrics,
     758           0 :                             MIDX( HISTOGRAM, REPAIR, RESPONSE_LATENCY_NANOS ),
     759           0 :                             FD_METRICS_CONVERTER_NONE,
     760           0 :                             FD_METRICS_HISTOGRAM_REPAIR_RESPONSE_LATENCY_NANOS_MIN,
     761           0 :                             FD_METRICS_HISTOGRAM_REPAIR_RESPONSE_LATENCY_NANOS_MAX,
     762           0 :                             "Response Latency" );
     763             : 
     764           0 :   printf(" Repair Peers: %lu\n", repair_metrics[ MIDX( COUNTER, REPAIR, PEER_REQUESTED ) ] );
     765           0 :   printf(" Shreds rejected (no stakes): %lu\n", shred_metrics[ MIDX( COUNTER, SHRED, SHRED_PROCESSED ) ] );
     766             :   /* Print histogram buckets similar to Prometheus format */
     767           0 :   print_histogram_buckets( repair_metrics,
     768           0 :                           MIDX( HISTOGRAM, REPAIR, SLOT_COMPLETE_DURATION_SECONDS ),
     769           0 :                           FD_METRICS_CONVERTER_SECONDS,
     770           0 :                           FD_METRICS_HISTOGRAM_REPAIR_SLOT_COMPLETE_DURATION_SECONDS_MIN,
     771           0 :                           FD_METRICS_HISTOGRAM_REPAIR_SLOT_COMPLETE_DURATION_SECONDS_MAX,
     772           0 :                           "Slot Complete Time" );
     773             : 
     774           0 : #define DIFFX(METRIC) repair_metrics[ MIDX( COUNTER, TILE, METRIC ) ] - repair_metrics_prev[ MIDX( COUNTER, TILE, METRIC ) ]
     775           0 :   ulong hkeep_ticks        = DIFFX(REGIME_DURATION_NANOS_CAUGHT_UP_HOUSEKEEPING) + DIFFX(REGIME_DURATION_NANOS_PROCESSING_HOUSEKEEPING) + DIFFX(REGIME_DURATION_NANOS_BACKPRESSURE_HOUSEKEEPING);
     776           0 :   ulong busy_ticks         = DIFFX(REGIME_DURATION_NANOS_PROCESSING_PREFRAG) + DIFFX(REGIME_DURATION_NANOS_PROCESSING_POSTFRAG ) + DIFFX(REGIME_DURATION_NANOS_CAUGHT_UP_PREFRAG);
     777           0 :   ulong caught_up_ticks    = DIFFX(REGIME_DURATION_NANOS_CAUGHT_UP_POSTFRAG);
     778           0 :   ulong backpressure_ticks = DIFFX(REGIME_DURATION_NANOS_BACKPRESSURE_PREFRAG);
     779           0 :   ulong total_ticks = hkeep_ticks + busy_ticks + caught_up_ticks + backpressure_ticks;
     780             : 
     781           0 :   printf( " Repair Hkeep: %.1f %%  Busy: %.1f %%  Idle: %.1f %%  Backp: %0.1f %%\n",
     782           0 :             (double)hkeep_ticks/(double)total_ticks*100.0,
     783           0 :             (double)busy_ticks/(double)total_ticks*100.0,
     784           0 :             (double)caught_up_ticks/(double)total_ticks*100.0,
     785           0 :             (double)backpressure_ticks/(double)total_ticks*100.0 );
     786           0 : #undef DIFFX
     787           0 :   fflush( stdout );
     788             : 
     789           0 :   printf( " Block failed insert: %lu\n", repair_metrics[ MIDX( COUNTER, REPAIR, BLOCK_INSERT_FAILED ) ] );
     790           0 :   printf( " Block evicted: %lu\n", repair_metrics[ MIDX( COUNTER, REPAIR, BLOCK_EVICTED ) ] );
     791           0 :   printf( " slot evicted: %lu\n", repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_LAST_EVICTED ) ] );
     792           0 :   printf( " slot evicted by: %lu\n", repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_LAST_EVICTION_CAUSE ) ] );
     793           0 :   printf( " slot failed insert: %lu\n", repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_LAST_INSERT_FAILED ) ] );
     794           0 :   for( ulong i=0UL; i<FD_METRICS_TOTAL_SZ/sizeof(ulong); i++ ) repair_metrics_prev[ i ] = repair_metrics[ i ];
     795           0 : }
     796             : 
     797             : static void
     798             : repair_ctx_wksp( args_t *          args,
     799             :                  config_t *        config,
     800             :                  ctx_t **          repair_ctx,
     801           0 :                  fd_topo_wksp_t ** repair_wksp ) {
     802           0 :   (void)args;
     803             : 
     804           0 :   fd_topo_t * topo = &config->topo;
     805           0 :   ulong wksp_id = fd_topo_find_wksp( topo, "repair" );
     806           0 :   if( FD_UNLIKELY( wksp_id==ULONG_MAX ) ) FD_LOG_ERR(( "repair workspace not found" ));
     807             : 
     808           0 :   fd_topo_wksp_t * _repair_wksp = &topo->workspaces[ wksp_id ];
     809             : 
     810           0 :   ulong tile_id = fd_topo_find_tile( topo, "repair", 0UL );
     811           0 :   if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "repair tile not found" ));
     812             : 
     813           0 :   fd_topo_join_workspace( topo, _repair_wksp, FD_SHMEM_JOIN_MODE_READ_ONLY, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
     814             : 
     815             :   /* Access the repair tile scratch memory where repair_tile_ctx is stored */
     816           0 :   fd_topo_tile_t * tile = &topo->tiles[ tile_id ];
     817           0 :   void * scratch = fd_topo_obj_laddr( &config->topo, tile->tile_obj_id );
     818           0 :   if( FD_UNLIKELY( !scratch ) ) FD_LOG_ERR(( "Failed to access repair tile scratch memory" ));
     819             : 
     820           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     821           0 :   ctx_t * _repair_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
     822             : 
     823           0 :   *repair_ctx  = _repair_ctx;
     824           0 :   *repair_wksp = _repair_wksp;
     825           0 : }
     826             : 
     827             : static void
     828             : repair_cmd_fn_catchup( args_t *   args,
     829           0 :                        config_t * config ) {
     830             : 
     831           0 :   memset( &config->topo, 0, sizeof(config->topo) );
     832           0 :   repair_topo( config );
     833             : 
     834           0 :   fd_topo_print_log( 1, &config->topo );
     835             : 
     836           0 :   args_t configure_args = {
     837           0 :     .configure.command = CONFIGURE_CMD_INIT,
     838           0 :   };
     839           0 :   for( ulong i=0UL; STAGES[ i ]; i++ ) {
     840           0 :     configure_args.configure.stages[ i ] = STAGES[ i ];
     841           0 :   }
     842           0 :   configure_cmd_fn( &configure_args, config );
     843           0 :   if( 0==strcmp( config->net.provider, "xdp" ) ) {
     844           0 :     fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
     845           0 :   }
     846           0 :   run_firedancer_init( config, 1, 0 );
     847             : 
     848           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
     849             : 
     850           0 :   fd_topo_fill( &config->topo );
     851             : 
     852           0 :   repair_load_manifest( &config->topo, args->repair.manifest_path );
     853             : 
     854             :   /* Access repair workspace memory and metrics */
     855             : 
     856           0 :   ulong repair_tile_idx = fd_topo_find_tile( &config->topo, "repair", 0UL );
     857           0 :   ulong shred_tile_idx  = fd_topo_find_tile( &config->topo, "shred", 0UL );
     858           0 :   FD_TEST( repair_tile_idx!=ULONG_MAX );
     859           0 :   FD_TEST( shred_tile_idx !=ULONG_MAX );
     860           0 :   fd_topo_tile_t * repair_tile = &config->topo.tiles[ repair_tile_idx ];
     861           0 :   fd_topo_tile_t * shred_tile  = &config->topo.tiles[ shred_tile_idx ];
     862             : 
     863           0 :   fd_topo_wksp_t * repair_wksp;
     864           0 :   ctx_t          * repair_ctx;
     865           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
     866             : 
     867           0 :   volatile ulong * shred_metrics  = fd_metrics_tile( shred_tile->metrics );
     868           0 :   volatile ulong * repair_metrics = fd_metrics_tile( repair_tile->metrics );
     869           0 :   FD_TEST( repair_metrics );
     870           0 :   ulong * repair_metrics_prev = aligned_alloc( 8UL, sizeof(ulong) * FD_METRICS_TOTAL_SZ );
     871           0 :   FD_TEST( repair_metrics_prev );
     872           0 :   memset( repair_metrics_prev, 0, sizeof(ulong) * FD_METRICS_TOTAL_SZ );
     873             : 
     874             :   /* Collect link metrics */
     875             : 
     876             :   /* Collect all net tiles and their repair_net link metrics */
     877           0 :   ulong net_cnt  = config->layout.net_tile_count;
     878           0 :   volatile ulong ** repair_net_links = aligned_alloc( 8UL, net_cnt * sizeof(volatile ulong*) );
     879           0 :   volatile ulong ** net_shred_links  = aligned_alloc( 8UL, net_cnt * sizeof(volatile ulong*) );
     880           0 :   FD_TEST( repair_net_links );
     881           0 :   FD_TEST( net_shred_links  );
     882             : 
     883           0 :   for( ulong i = 0UL; i < net_cnt; i++ ) {
     884           0 :     ulong tile_idx = fd_topo_find_tile( &config->topo, "net", i );
     885           0 :     if( FD_UNLIKELY( tile_idx == ULONG_MAX ) ) FD_LOG_ERR(( "net tile %lu not found", i ));
     886           0 :     fd_topo_tile_t * tile = &config->topo.tiles[ tile_idx ];
     887             : 
     888           0 :     ulong repair_net_in_idx = fd_topo_find_tile_in_link( &config->topo, tile, "repair_net", 0UL );
     889           0 :     if( FD_UNLIKELY( repair_net_in_idx == ULONG_MAX ) ) FD_LOG_ERR(( "repair_net link not found for net tile %lu", i ));
     890           0 :     FD_TEST( tile->metrics );
     891           0 :     repair_net_links[i] = fd_metrics_link_in( tile->metrics, repair_net_in_idx );
     892           0 :     FD_TEST( repair_net_links[i] );
     893             : 
     894             :     /* process all net_shred links */
     895           0 :     ulong shred_tile_idx = fd_topo_find_tile( &config->topo, "shred", 0 );
     896           0 :     if( FD_UNLIKELY( shred_tile_idx == ULONG_MAX ) ) FD_LOG_ERR(( "shred tile 0 not found" ));
     897           0 :     fd_topo_tile_t * shred_tile = &config->topo.tiles[ shred_tile_idx ];
     898             : 
     899           0 :     ulong shred_out_in_idx = fd_topo_find_tile_in_link( &config->topo, shred_tile, "net_shred", i );
     900           0 :     if( FD_UNLIKELY( shred_out_in_idx == ULONG_MAX ) ) FD_LOG_ERR(( "net_shred link not found for shred tile 0" ));
     901           0 :     FD_TEST( shred_tile->metrics );
     902           0 :     net_shred_links[i] = fd_metrics_link_in( shred_tile->metrics, shred_out_in_idx );
     903           0 :     FD_TEST( net_shred_links[i] );
     904           0 :   }
     905             : 
     906           0 :   FD_LOG_NOTICE(( "Repair catchup run" ));
     907             : 
     908           0 :   ulong    shred_out_link_idx = fd_topo_find_link( &config->topo, "shred_out", 0UL );
     909           0 :   FD_TEST( shred_out_link_idx!=ULONG_MAX );
     910           0 :   fd_topo_link_t * shred_out_link   = &config->topo.links[ shred_out_link_idx  ];
     911           0 :   fd_frag_meta_t * shred_out_mcache = shred_out_link->mcache;
     912           0 :   void * shred_out_dcache = config->topo.workspaces[ config->topo.objs[ shred_out_link->dcache_obj_id ].wksp_id ].wksp;
     913             : 
     914           0 :   ulong turbine_slot0 = 0;
     915           0 :   long  last_print    = fd_log_wallclock();
     916           0 :   ulong last_sent     = 0UL;
     917             : 
     918           0 :   fd_topo_run_single_process( &config->topo, 0, config->uid, config->gid, fdctl_tile_run );
     919           0 :   for(;;) {
     920             : 
     921           0 :     if( FD_UNLIKELY( !turbine_slot0 ) ) {
     922           0 :       fd_frag_meta_t * frag = &shred_out_mcache[0]; /* hack to get first frag */
     923           0 :       if ( frag->sz > 0 ) {
     924           0 :         uchar      * shred_out_chunk = fd_chunk_to_laddr( shred_out_dcache, frag->chunk );
     925           0 :         fd_shred_base_t * shred_out_shred = (fd_shred_base_t *)fd_type_pun( shred_out_chunk );
     926           0 :         turbine_slot0 = shred_out_shred->shred.slot;
     927           0 :         FD_LOG_NOTICE(("turbine_slot0: %lu", turbine_slot0));
     928           0 :       }
     929           0 :     }
     930             : 
     931             :     /* print metrics */
     932             : 
     933           0 :     long now = fd_log_wallclock();
     934           0 :     int catchup_finished = 0;
     935           0 :     if( FD_UNLIKELY( now - last_print > 1e9L ) ) {
     936           0 :       print_tile_metrics( shred_metrics, repair_metrics, repair_metrics_prev, repair_net_links, net_shred_links, net_cnt, &last_sent, last_print, now );
     937           0 :       ulong slots_behind = turbine_slot0 > repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_HIGHEST_REPAIRED ) ] ? turbine_slot0 - repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_HIGHEST_REPAIRED ) ] : 0;
     938           0 :       printf(" Repaired slots: %lu/%lu  (slots behind: %lu)\n", repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_HIGHEST_REPAIRED ) ], turbine_slot0, slots_behind );
     939           0 :       if( turbine_slot0 && !slots_behind ) {
     940           0 :         catchup_finished = 1;
     941           0 :       }
     942           0 :       printf("\n");
     943           0 :       fflush( stdout );
     944           0 :       last_print = now;
     945           0 :     }
     946             : 
     947           0 :     if( FD_UNLIKELY( catchup_finished ) ) {
     948             :       /* repair cmd owned memory */
     949           0 :       location_table = fd_location_table_join( fd_location_table_new( location_table_mem ) );
     950           0 :       read_iptable( args->repair.iptable_path, location_table );
     951           0 :       print_peer_location_latency( repair_wksp->wksp, repair_ctx );
     952           0 :       print_catchup_slots( repair_wksp->wksp, repair_ctx, 0, 1 );
     953           0 :       FD_LOG_NOTICE(("Catchup to slot %lu completed successfully", turbine_slot0));
     954           0 :       fd_sys_util_exit_group( 0 );
     955           0 :     }
     956           0 :   }
     957           0 : }
     958             : 
     959             : /* Tests equivocation detection & repair path. */
     960             : static void
     961             : repair_cmd_fn_eqvoc( args_t *   args,
     962           0 :                      config_t * config ) {
     963           0 :   (void)args;
     964           0 :   memset( &config->topo, 0, sizeof(config->topo) );
     965           0 :   repair_topo( config );
     966             : 
     967           0 :   FD_LOG_NOTICE(( "Repair eqvoc testing init" ));
     968           0 :   fd_topo_print_log( 1, &config->topo );
     969             : 
     970           0 :   args_t configure_args = { .configure.command = CONFIGURE_CMD_INIT, };
     971           0 :   for( ulong i=0UL; STAGES[ i ]; i++ ) configure_args.configure.stages[ i ] = STAGES[ i ];
     972           0 :   configure_cmd_fn( &configure_args, config );
     973           0 :   if( 0==strcmp( config->net.provider, "xdp" ) ) fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
     974             : 
     975           0 :   run_firedancer_init( config, 1, 0 );
     976           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
     977           0 :   fd_topo_fill( &config->topo );
     978             : 
     979           0 :   repair_load_manifest( &config->topo, args->repair.manifest_path );
     980             : 
     981           0 :   ulong repair_tile_idx = fd_topo_find_tile( &config->topo, "repair", 0UL );
     982           0 :   fd_topo_tile_t * repair_tile = &config->topo.tiles[ repair_tile_idx ];
     983           0 :   volatile ulong * repair_metrics = fd_metrics_tile( repair_tile->metrics );
     984             : 
     985           0 :   void * scratch = fd_topo_obj_laddr( &config->topo, repair_tile->tile_obj_id );
     986           0 :   if( FD_UNLIKELY( !scratch ) ) FD_LOG_ERR(( "Failed to access repair tile scratch memory" ));
     987           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     988           0 :   ctx_t * repair_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
     989           0 :   (void)repair_ctx;
     990             : 
     991             :   /* read tower_out mcache dcache */
     992           0 :   ulong tower_out_link_idx = fd_topo_find_link( &config->topo, "tower_out", 0UL );
     993           0 :   FD_TEST( tower_out_link_idx!=ULONG_MAX );
     994           0 :   fd_topo_link_t * tower_out_link = &config->topo.links[ tower_out_link_idx ];
     995           0 :   fd_frag_meta_t * tower_out_mcache = tower_out_link->mcache;
     996           0 :   fd_wksp_t * tower_out_mem = config->topo.workspaces[ config->topo.objs[ tower_out_link->dcache_obj_id ].wksp_id ].wksp;
     997           0 :   ulong tower_out_chunk0 = fd_dcache_compact_chunk0( tower_out_mem, tower_out_link->dcache );
     998           0 :   ulong tower_out_wmark = fd_dcache_compact_wmark( tower_out_mem, tower_out_link->dcache, tower_out_link->mtu );
     999           0 :   ulong tower_out_chunk = tower_out_chunk0;
    1000             : 
    1001           0 :   fd_topo_run_single_process( &config->topo, 0, config->uid, config->gid, fdctl_tile_run );
    1002           0 :   int confirmed = 0;
    1003           0 :   for(;;) {
    1004             :     /* publish a confirmation on tower_out */
    1005           0 :     if( FD_UNLIKELY( !confirmed && repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_HIGHEST_REPAIRED ) ] != 0 ) ) {
    1006           0 :       fd_tower_slot_confirmed_t * msg = fd_chunk_to_laddr( tower_out_mem, tower_out_chunk );
    1007           0 :       FD_LOG_NOTICE(( "publishing confirmation for slot %lu", msg->slot ));
    1008           0 :       fd_mcache_publish( tower_out_mcache, tower_out_link->depth, 0, FD_TOWER_SIG_SLOT_CONFIRMED, tower_out_chunk, sizeof(fd_tower_slot_confirmed_t), 0, 0, 0 );
    1009           0 :       tower_out_chunk = fd_dcache_compact_next( tower_out_chunk, sizeof(fd_tower_slot_confirmed_t), tower_out_chunk0, tower_out_wmark );
    1010           0 :       confirmed = 1;
    1011           0 :     }
    1012           0 :     sleep( 1 );
    1013           0 :   }
    1014           0 : }
    1015             : 
    1016             : static void
    1017             : repair_cmd_fn_metrics( args_t *   args,
    1018           0 :                        config_t * config ) {
    1019             :   //memset( &config->topo, 0, sizeof(config->topo) );
    1020             : 
    1021           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_ONLY, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
    1022           0 :   fd_topo_fill( &config->topo );
    1023             : 
    1024           0 :   ctx_t *          repair_ctx;
    1025           0 :   fd_topo_wksp_t * repair_wksp;
    1026           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
    1027             : 
    1028           0 :   ulong    shred_tile_idx  = fd_topo_find_tile( &config->topo, "shred", 0UL );
    1029           0 :   ulong    repair_tile_idx = fd_topo_find_tile( &config->topo, "repair", 0UL );
    1030           0 :   FD_TEST( shred_tile_idx != ULONG_MAX );
    1031           0 :   FD_TEST( repair_tile_idx!= ULONG_MAX );
    1032           0 :   fd_topo_tile_t * shred_tile  = &config->topo.tiles[ shred_tile_idx ];
    1033           0 :   fd_topo_tile_t * repair_tile = &config->topo.tiles[ repair_tile_idx ];
    1034             : 
    1035           0 :   volatile ulong * shred_metrics = fd_metrics_tile( shred_tile->metrics );
    1036           0 :   FD_TEST( shred_metrics );
    1037             : 
    1038           0 :   volatile ulong * repair_metrics = fd_metrics_tile( repair_tile->metrics );
    1039           0 :   FD_TEST( repair_metrics );
    1040           0 :   ulong * repair_metrics_prev = aligned_alloc( 8UL, sizeof(ulong) * FD_METRICS_TOTAL_SZ );
    1041           0 :   FD_TEST( repair_metrics_prev );
    1042           0 :   memset( repair_metrics_prev, 0, sizeof(ulong) * FD_METRICS_TOTAL_SZ );
    1043             : 
    1044             : 
    1045           0 :   ulong net_tile_cnt = config->layout.net_tile_count;
    1046           0 :   volatile ulong ** repair_net_links = aligned_alloc( 8UL, net_tile_cnt * sizeof(volatile ulong*) );
    1047           0 :   volatile ulong ** net_shred_links  = aligned_alloc( 8UL, net_tile_cnt * sizeof(volatile ulong*) );
    1048           0 :   FD_TEST( repair_net_links );
    1049           0 :   FD_TEST( net_shred_links );
    1050             : 
    1051           0 :   for( ulong i = 0UL; i < net_tile_cnt; i++ ) {
    1052             :     /* process all repair_net links */
    1053           0 :     ulong tile_idx = fd_topo_find_tile( &config->topo, "net", i );
    1054           0 :     if( FD_UNLIKELY( tile_idx == ULONG_MAX ) ) FD_LOG_ERR(( "net tile %lu not found", i ));
    1055           0 :     fd_topo_tile_t * tile = &config->topo.tiles[ tile_idx ];
    1056             : 
    1057           0 :     ulong repair_net_in_idx = fd_topo_find_tile_in_link( &config->topo, tile, "repair_net", 0UL );
    1058           0 :     if( FD_UNLIKELY( repair_net_in_idx == ULONG_MAX ) ) FD_LOG_ERR(( "repair_net link not found for net tile %lu", i ));
    1059           0 :     repair_net_links[i] = fd_metrics_link_in( tile->metrics, repair_net_in_idx );
    1060           0 :     FD_TEST( repair_net_links[i] );
    1061             : 
    1062             :     /* process all net_shred links */
    1063           0 :     tile_idx = fd_topo_find_tile( &config->topo, "shred", 0 );
    1064           0 :     if( FD_UNLIKELY( tile_idx == ULONG_MAX ) ) FD_LOG_ERR(( "shred tile 0 not found" ));
    1065           0 :     fd_topo_tile_t * shred_tile = &config->topo.tiles[ tile_idx ];
    1066             : 
    1067           0 :     ulong shred_out_in_idx = fd_topo_find_tile_in_link( &config->topo, shred_tile, "net_shred", i );
    1068           0 :     if( FD_UNLIKELY( shred_out_in_idx == ULONG_MAX ) ) FD_LOG_ERR(( "net_shred link not found for shred tile 0" ));
    1069           0 :     net_shred_links[i] = fd_metrics_link_in( shred_tile->metrics, shred_out_in_idx );
    1070           0 :     FD_TEST( net_shred_links[i] );
    1071           0 :   }
    1072             : 
    1073           0 :   long  last_print_ts = fd_log_wallclock();
    1074           0 :   ulong last_sent     = 0UL;
    1075           0 :   for(;;) {
    1076           0 :     long now = fd_log_wallclock();
    1077           0 :     if( FD_UNLIKELY( now - last_print_ts > 1e9L ) ) {
    1078           0 :       print_tile_metrics( shred_metrics, repair_metrics, repair_metrics_prev, repair_net_links, net_shred_links, net_tile_cnt, &last_sent, last_print_ts, now );
    1079           0 :       last_print_ts = now;
    1080           0 :     }
    1081           0 :   }
    1082           0 : }
    1083             : 
    1084             : static void
    1085             : repair_cmd_fn_forest( args_t *   args,
    1086           0 :                       config_t * config ) {
    1087           0 :   ctx_t *          repair_ctx;
    1088           0 :   fd_topo_wksp_t * repair_wksp;
    1089           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
    1090             : 
    1091           0 :   ulong forest_gaddr = fd_wksp_gaddr_fast( repair_ctx->wksp, repair_ctx->forest );
    1092           0 :   fd_forest_t * forest = (fd_forest_t *)fd_wksp_laddr( repair_wksp->wksp, forest_gaddr );
    1093             : 
    1094           0 :   for( ;; ) {
    1095           0 :     fd_forest_print( forest );
    1096           0 :     sleep( 1 );
    1097           0 :   }
    1098           0 : }
    1099             : 
    1100             : static void
    1101             : repair_cmd_fn_inflight( args_t *   args,
    1102           0 :                         config_t * config ) {
    1103           0 :   ctx_t *          repair_ctx;
    1104           0 :   fd_topo_wksp_t * repair_wksp;
    1105           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
    1106             : 
    1107           0 :   ulong            inflights_gaddr = fd_wksp_gaddr_fast( repair_ctx->wksp, repair_ctx->inflights );
    1108           0 :   fd_inflights_t * inflights       = (fd_inflights_t *)fd_wksp_laddr( repair_wksp->wksp, inflights_gaddr );
    1109             : 
    1110           0 :   ulong inflight_pool_off = (ulong)inflights->pool - (ulong)repair_ctx->inflights;
    1111           0 :   fd_inflight_t * inflight_pool = (fd_inflight_t *)fd_wksp_laddr( repair_wksp->wksp, inflights_gaddr + inflight_pool_off );
    1112             : 
    1113           0 :   for( ;; ) {
    1114           0 :     fd_inflights_print( inflights->outstanding_dl, inflight_pool );
    1115           0 :     printf("popped count: %lu\n", inflights->popped_cnt);
    1116           0 :     fd_inflights_print( inflights->popped_dl, inflight_pool );
    1117           0 :     sleep( 1 );
    1118           0 :   }
    1119           0 : }
    1120             : 
    1121             : static void
    1122             : repair_cmd_fn_requests( args_t *   args,
    1123           0 :                         config_t * config ) {
    1124           0 :   ctx_t *          repair_ctx;
    1125           0 :   fd_topo_wksp_t * repair_wksp;
    1126           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
    1127             : 
    1128           0 :   fd_forest_t *          forest = fd_forest_join( fd_wksp_laddr( repair_wksp->wksp, fd_wksp_gaddr_fast( repair_ctx->wksp, repair_ctx->forest ) ) );
    1129           0 :   fd_forest_reqslist_t * dlist  = fd_forest_reqslist( forest );
    1130           0 :   fd_forest_ref_t *      pool   = fd_forest_reqspool( forest );
    1131             : 
    1132           0 :   fd_forest_reqslist_t * orphlist = fd_forest_orphlist( forest );
    1133             : 
    1134           0 :   for( ;; ) {
    1135           0 :     printf("%-15s %-12s %-12s %-12s %-20s %-12s\n",
    1136           0 :             "Slot", "Buffered Idx", "Complete Idx", "First Shred ts", "Turbine Cnt", "Repair Cnt");
    1137           0 :     printf("%-15s %-12s %-12s %-12s %-20s %-12s\n",
    1138           0 :             "---------------", "------------", "------------", "------------",
    1139           0 :             "--------------------", "------------");
    1140           0 :     for( fd_forest_reqslist_iter_t iter = fd_forest_reqslist_iter_fwd_init( dlist, pool );
    1141           0 :         !fd_forest_reqslist_iter_done( iter, dlist, pool );
    1142           0 :         iter = fd_forest_reqslist_iter_fwd_next( iter, dlist, pool ) ) {
    1143           0 :       fd_forest_ref_t * req = fd_forest_reqslist_iter_ele( iter, dlist, pool );
    1144           0 :       fd_forest_blk_t * blk = fd_forest_pool_ele( fd_forest_pool( forest ), req->idx );
    1145             : 
    1146           0 :       printf("%-15lu %-12u %-12u %-20ld %-12u %-10u\n",
    1147           0 :               blk->slot,
    1148           0 :               blk->buffered_idx,
    1149           0 :               blk->complete_idx,
    1150           0 :               blk->first_shred_ts,
    1151           0 :               blk->turbine_cnt,
    1152           0 :               blk->repair_cnt);
    1153           0 :     }
    1154           0 :     printf("\n");
    1155             : 
    1156             :     /* now lets print the orphreqs */
    1157             : 
    1158           0 :     printf("Orphan Requests:\n");
    1159           0 :     printf("%-15s %-12s %-12s %-12s %-20s %-12s %-10s\n",
    1160           0 :       "Slot", "Consumed Idx", "Buffered Idx", "Complete Idx",
    1161           0 :       "First Shred Timestamp", "Turbine Cnt", "Repair Cnt");
    1162           0 : printf("%-15s %-12s %-12s %-12s %-20s %-12s %-10s\n",
    1163           0 :       "---------------", "------------", "------------", "------------",
    1164           0 :       "--------------------", "------------", "----------");
    1165             : 
    1166           0 :     for( fd_forest_reqslist_iter_t iter = fd_forest_reqslist_iter_fwd_init( orphlist, pool );
    1167           0 :                                          !fd_forest_reqslist_iter_done( iter, orphlist, pool );
    1168           0 :                                    iter = fd_forest_reqslist_iter_fwd_next( iter, orphlist, pool ) ) {
    1169           0 :       fd_forest_ref_t * req = fd_forest_reqslist_iter_ele( iter, orphlist, pool );
    1170           0 :       fd_forest_blk_t * blk = fd_forest_pool_ele( fd_forest_pool( forest ), req->idx );
    1171           0 :       printf("%-15lu %-12u %-12u %-20ld %-12u %-10u\n",
    1172           0 :               blk->slot,
    1173           0 :               blk->buffered_idx,
    1174           0 :               blk->complete_idx,
    1175           0 :               blk->first_shred_ts,
    1176           0 :               blk->turbine_cnt,
    1177           0 :               blk->repair_cnt);
    1178           0 :     }
    1179           0 :     sleep( 1 );
    1180           0 :   }
    1181           0 : }
    1182             : 
    1183             : static void
    1184             : repair_cmd_fn_waterfall( args_t *   args,
    1185           0 :                          config_t * config ) {
    1186             : 
    1187           0 :   fd_topo_t * topo    = &config->topo;
    1188           0 :   ulong       wksp_id = fd_topo_find_wksp( topo, "repair" );
    1189           0 :   if( FD_UNLIKELY( wksp_id==ULONG_MAX ) ) FD_LOG_ERR(( "repair workspace not found" ));
    1190           0 :   fd_topo_wksp_t * repair_wksp = &topo->workspaces[ wksp_id ];
    1191           0 :   fd_topo_join_workspace( topo, repair_wksp, FD_SHMEM_JOIN_MODE_READ_ONLY, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
    1192             : 
    1193             :   /* Access the repair tile scratch memory where repair_tile_ctx is stored */
    1194           0 :   ulong tile_id = fd_topo_find_tile( topo, "repair", 0UL );
    1195           0 :   if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "repair tile not found" ));
    1196           0 :   fd_topo_tile_t * tile = &topo->tiles[ tile_id ];
    1197           0 :   void * scratch = fd_topo_obj_laddr( &config->topo, tile->tile_obj_id );
    1198           0 :   if( FD_UNLIKELY( !scratch ) ) FD_LOG_ERR(( "Failed to access repair tile scratch memory" ));
    1199             : 
    1200           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1201           0 :   ctx_t * repair_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
    1202             : 
    1203             :   /* catchup cmd owned memory */
    1204           0 :   location_table = fd_location_table_join( fd_location_table_new( location_table_mem ) );
    1205           0 :   read_iptable( args->repair.iptable_path, location_table );
    1206             : 
    1207             :   // Add terminal setup here - same as monitor.c
    1208           0 :   atexit( restore_terminal );
    1209           0 :   if( FD_UNLIKELY( 0!=tcgetattr( STDIN_FILENO, &termios_backup ) ) ) {
    1210           0 :     FD_LOG_ERR(( "tcgetattr(STDIN_FILENO) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
    1211           0 :   }
    1212             : 
    1213             :   /* Disable character echo and line buffering */
    1214           0 :   struct termios term = termios_backup;
    1215           0 :   term.c_lflag &= (tcflag_t)~(ICANON | ECHO);
    1216           0 :   if( FD_UNLIKELY( 0!=tcsetattr( STDIN_FILENO, TCSANOW, &term ) ) ) {
    1217           0 :     FD_LOG_WARNING(( "tcsetattr(STDIN_FILENO) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
    1218           0 :   }
    1219             : 
    1220           0 :   int  catchup_verbose = 0;
    1221           0 :   long last_print = 0;
    1222           0 :   for( ;; ) {
    1223           0 :     int c = fd_getchar();
    1224           0 :     if( FD_UNLIKELY( c=='i'    ) ) catchup_verbose = !catchup_verbose;
    1225           0 :     if( FD_UNLIKELY( c=='\x04' ) ) break; /* Ctrl-D */
    1226             : 
    1227           0 :     long now = fd_log_wallclock();
    1228           0 :     if( FD_UNLIKELY( now - last_print > 1e9L ) ) {
    1229           0 :       last_print = now;
    1230           0 :       print_catchup_slots( repair_wksp->wksp, repair_ctx, catchup_verbose, args->repair.sort_by_slot );
    1231           0 :       printf( "catchup slots | Use 'i' to toggle extra slot information" TEXT_NEWLINE );
    1232           0 :       fflush( stdout );
    1233             : 
    1234             :       /* Peer location latency is not that useful post catchup, and also
    1235             :          requires some concurrent dlist iteration, so only print it when
    1236             :          in catchup mode. */
    1237           0 :     }
    1238           0 :   }
    1239           0 : }
    1240             : 
    1241           0 : #define PEERS_DISPLAY_MAX 20
    1242             : 
    1243             : static void
    1244             : print_peer_dlist( fd_policy_peer_dlist_t *     dlist,
    1245             :                   fd_policy_peer_t *           pool,
    1246             :                   fd_policy_peer_dlist_iter_t  cursor,
    1247           0 :                   char const *                 label ) {
    1248           0 :   ulong cnt = 0;
    1249           0 :   for( fd_policy_peer_dlist_iter_t it = fd_policy_peer_dlist_iter_fwd_init( dlist, pool );
    1250           0 :        !fd_policy_peer_dlist_iter_done( it, dlist, pool );
    1251           0 :        it = fd_policy_peer_dlist_iter_fwd_next( it, dlist, pool ) ) cnt++;
    1252             : 
    1253           0 :   printf( "%s (%lu peers)\n", label, cnt );
    1254           0 :   if( !cnt || fd_policy_peer_dlist_iter_done( cursor, dlist, pool ) ) {
    1255           0 :     printf( "  (empty or iterator not initialized)\n\n" );
    1256           0 :     return;
    1257           0 :   }
    1258             : 
    1259           0 :   printf( "     | %-8s | %-12s | %-12s | %-8s | %-8s\n",
    1260           0 :           "Idx", "Pubkey", "Ewma Lat", "Avg Lat", "Req/Res" );
    1261           0 :   printf( "-----+----------+--------------+--------------+----------+---------\n" );
    1262             : 
    1263           0 :   fd_policy_peer_dlist_iter_t it = cursor;
    1264           0 :   for( ulong i = 0; i < PEERS_DISPLAY_MAX && i < cnt; i++ ) {
    1265           0 :     fd_policy_peer_t * peer = fd_policy_peer_dlist_iter_ele( it, dlist, pool );
    1266             : 
    1267           0 :     FD_BASE58_ENCODE_32_BYTES( peer->key.key, b58 );
    1268           0 :     char pubkey_short[13];
    1269           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( pubkey_short ), b58, 12 ) );
    1270             : 
    1271           0 :     double avg_lat_ms  = peer->res_cnt ? ((double)peer->total_lat / (double)peer->res_cnt) / 1e6 : 0.0;
    1272           0 :     double ewma_lat_ms = (double)peer->ewma_lat / 1e6;
    1273             : 
    1274           0 :     printf( " %s%c%s | %-8lu | %-12s | %9.3fms | %9.3fms | %lu/%lu\n",
    1275           0 :             i == 0 ? "\033[1;33m" : "",
    1276           0 :             i == 0 ? '>' : ' ',
    1277           0 :             i == 0 ? "\033[0m"    : "",
    1278           0 :             fd_policy_peer_pool_idx( pool, peer ),
    1279           0 :             pubkey_short,
    1280           0 :             ewma_lat_ms,
    1281           0 :             avg_lat_ms,
    1282           0 :             peer->req_cnt,
    1283           0 :             peer->res_cnt );
    1284             : 
    1285           0 :     it = fd_policy_peer_dlist_iter_fwd_next( it, dlist, pool );
    1286           0 :     if( fd_policy_peer_dlist_iter_done( it, dlist, pool ) ) {
    1287           0 :       it = fd_policy_peer_dlist_iter_fwd_init( dlist, pool );
    1288           0 :     }
    1289           0 :   }
    1290           0 :   if( cnt > PEERS_DISPLAY_MAX ) printf( "  ... (%lu more)\n", cnt - PEERS_DISPLAY_MAX );
    1291           0 :   printf( "\n" );
    1292           0 : }
    1293             : 
    1294             : static void
    1295             : repair_cmd_fn_peers( args_t *   args,
    1296           0 :                      config_t * config ) {
    1297           0 :   ctx_t *          repair_ctx;
    1298           0 :   fd_topo_wksp_t * repair_wksp;
    1299           0 :   repair_ctx_wksp( args, config, &repair_ctx, &repair_wksp );
    1300             : 
    1301           0 :   fd_policy_t * policy = fd_wksp_laddr( repair_wksp->wksp, fd_wksp_gaddr_fast( repair_ctx->wksp, repair_ctx->policy ) );
    1302             : 
    1303           0 :   fd_policy_peer_dlist_t * fast_dlist = fd_wksp_laddr( repair_wksp->wksp, fd_wksp_gaddr_fast( repair_ctx->wksp, policy->peers.fast ) );
    1304           0 :   fd_policy_peer_dlist_t * slow_dlist = fd_wksp_laddr( repair_wksp->wksp, fd_wksp_gaddr_fast( repair_ctx->wksp, policy->peers.slow ) );
    1305           0 :   fd_policy_peer_t *       pool       = fd_wksp_laddr( repair_wksp->wksp, fd_wksp_gaddr_fast( repair_ctx->wksp, policy->peers.pool ) );
    1306             : 
    1307           0 :   long last_print = 0;
    1308           0 :   for( ;; ) {
    1309           0 :     long now = fd_log_wallclock();
    1310           0 :     if( FD_UNLIKELY( now - last_print > 1e9L ) ) {
    1311           0 :       last_print = now;
    1312           0 :       printf( "\033[2J\033[H" );
    1313             : 
    1314           0 :       char fast_label[64];
    1315           0 :       char slow_label[64];
    1316           0 :       snprintf( fast_label, sizeof(fast_label), "FAST PEERS (ewma < %ldms)", (long)(FD_POLICY_LATENCY_THRESH / 1e6L) );
    1317           0 :       snprintf( slow_label, sizeof(slow_label), "SLOW PEERS (ewma >= %ldms or no responses)", (long)(FD_POLICY_LATENCY_THRESH / 1e6L) );
    1318           0 :       print_peer_dlist( fast_dlist, pool, policy->peers.select.fast_iter, fast_label );
    1319           0 :       print_peer_dlist( slow_dlist, pool, policy->peers.select.slow_iter, slow_label );
    1320             : 
    1321           0 :       printf( "select cnt: %u / %u (fast per slow)\n", policy->peers.select.cnt, FD_POLICY_FAST_PER_SLOW );
    1322           0 :       printf( "pool used: %lu / %lu\n", fd_policy_peer_pool_used( pool ), fd_policy_peer_pool_max( pool ) );
    1323             : 
    1324           0 :       fflush( stdout );
    1325           0 :     }
    1326             : 
    1327           0 :   }
    1328           0 : }
    1329             : 
    1330             : 
    1331             : 
    1332             : 
    1333             : void
    1334             : repair_cmd_args( int *    pargc,
    1335             :                  char *** pargv,
    1336           0 :                  args_t * args ) {
    1337             : 
    1338             :   /* positional arg */
    1339             : 
    1340           0 :   args->repair.pos_arg = (*pargv)[0];
    1341           0 :   if( FD_UNLIKELY( !args->repair.pos_arg ) ) {
    1342           0 :     args->repair.help = 1;
    1343           0 :     return;
    1344           0 :   }
    1345           0 :   (*pargc)--;
    1346           0 :   (*pargv)++;
    1347             : 
    1348             :   /* required args */
    1349             : 
    1350           0 :   char const * manifest_path = fd_env_strip_cmdline_cstr    ( pargc, pargv, "--manifest-path", NULL, NULL      );
    1351             : 
    1352             :   /* optional args */
    1353             : 
    1354           0 :   char const * iptable_path  = fd_env_strip_cmdline_cstr    ( pargc, pargv, "--iptable",       NULL, NULL      );
    1355           0 :   ulong        slot          = fd_env_strip_cmdline_ulong   ( pargc, pargv, "--slot",          NULL, ULONG_MAX );
    1356           0 :   int          sort_by_slot  = fd_env_strip_cmdline_contains( pargc, pargv, "--sort-by-slot"                   );
    1357             : 
    1358           0 :   if( FD_UNLIKELY( !strcmp( args->repair.pos_arg, "catchup" ) && !manifest_path ) ) {
    1359           0 :     args->repair.help = 1;
    1360           0 :     return;
    1361           0 :   }
    1362             : 
    1363           0 :   fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( args->repair.manifest_path ), manifest_path, sizeof(args->repair.manifest_path)-1UL ) );
    1364           0 :   fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( args->repair.iptable_path ),  iptable_path,  sizeof(args->repair.iptable_path )-1UL ) );
    1365           0 :   args->repair.slot         = slot;
    1366           0 :   args->repair.sort_by_slot = sort_by_slot;
    1367           0 : }
    1368             : 
    1369             : static void
    1370             : repair_cmd_fn( args_t *   args,
    1371           0 :               config_t * config ) {
    1372             : 
    1373           0 :   if( args->repair.help ) {
    1374           0 :     fd_action_help_print( &fd_action_repair );
    1375           0 :     return;
    1376           0 :   }
    1377             : 
    1378           0 :   if     ( !strcmp( args->repair.pos_arg, "catchup"   ) ) repair_cmd_fn_catchup  ( args, config );
    1379           0 :   else if( !strcmp( args->repair.pos_arg, "eqvoc"     ) ) repair_cmd_fn_eqvoc    ( args, config );
    1380           0 :   else if( !strcmp( args->repair.pos_arg, "forest"    ) ) repair_cmd_fn_forest   ( args, config );
    1381           0 :   else if( !strcmp( args->repair.pos_arg, "inflight"  ) ) repair_cmd_fn_inflight ( args, config );
    1382           0 :   else if( !strcmp( args->repair.pos_arg, "requests"  ) ) repair_cmd_fn_requests ( args, config );
    1383           0 :   else if( !strcmp( args->repair.pos_arg, "waterfall" ) ) repair_cmd_fn_waterfall( args, config );
    1384           0 :   else if( !strcmp( args->repair.pos_arg, "peers"     ) ) repair_cmd_fn_peers    ( args, config );
    1385           0 :   else if( !strcmp( args->repair.pos_arg, "metrics"   ) ) repair_cmd_fn_metrics  ( args, config );
    1386           0 :   else                                                    fd_action_help_print( &fd_action_repair );
    1387           0 : }
    1388             : 
    1389             : static void
    1390           0 : repair_args_help( fd_action_help_t * help ) {
    1391           0 :   fd_action_help_arg( help, "catchup",         NULL,     "Run a reduced topology that only repairs slots until catchup.\n"
    1392           0 :                                                          "Requires --manifest-path; accepts --iptable and --sort-by-slot" );
    1393           0 :   fd_action_help_arg( help, "eqvoc",           NULL,     "Test equivocation detection and the repair path" );
    1394           0 :   fd_action_help_arg( help, "forest",          NULL,     "Print the repair forest.  Accepts --slot to drill into a slot" );
    1395           0 :   fd_action_help_arg( help, "inflight",        NULL,     "Print the inflight repairs" );
    1396           0 :   fd_action_help_arg( help, "requests",        NULL,     "Print the queued repair requests" );
    1397           0 :   fd_action_help_arg( help, "waterfall",       NULL,     "Print a waterfall diagram of recent slot completion times and\n"
    1398           0 :                                                          "response latencies.  Accepts --iptable and --sort-by-slot" );
    1399           0 :   fd_action_help_arg( help, "peers",           NULL,     "Print the list of slow and fast repair peers" );
    1400           0 :   fd_action_help_arg( help, "metrics",         NULL,     "Print repair tile metrics in a digestible format" );
    1401           0 :   fd_action_help_arg( help, "--manifest-path", "<path>", "Path to manifest file (required by catchup)" );
    1402           0 :   fd_action_help_arg( help, "--iptable",       "<path>", "Path to iptable file (catchup, waterfall)" );
    1403           0 :   fd_action_help_arg( help, "--slot",          "<slot>", "Specific forest slot to drill into (forest)" );
    1404             :   fd_action_help_arg( help, "--sort-by-slot",  NULL,     "Sort results by slot (catchup, waterfall)" );
    1405           0 : }
    1406             : 
    1407             : action_t fd_action_repair = {
    1408             :   .name        = "repair",
    1409             :   .args        = repair_cmd_args,
    1410             :   .fn          = repair_cmd_fn,
    1411             :   .perm        = dev_cmd_perm,
    1412             :   .description = "Spawn a reduced topology for inspecting and profiling the repair tile",
    1413             :   .detail      = "Boots a smaller Firedancer topology focused on the repair tile and runs the\n"
    1414             :                  "requested subcommand to drive or inspect repair behavior.  Pick one of the\n"
    1415             :                  "subcommands below.",
    1416             :   .usage       = "repair <catchup|eqvoc|forest|inflight|requests|waterfall|peers|metrics> [OPTIONS]",
    1417             :   .args_help   = repair_args_help,
    1418             : };

Generated by: LCOV version 1.14