LCOV - code coverage report
Current view: top level - discof/replay - fd_sched.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 967 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 45 0.0 %

          Line data    Source code
       1             : #include "fd_sched.h"
       2             : #include "../../disco/fd_disco_base.h" /* for FD_MAX_TXN_PER_SLOT */
       3             : #include "../../flamenco/runtime/fd_runtime.h" /* for fd_runtime_load_txn_address_lookup_tables */
       4             : 
       5             : #include "../../flamenco/runtime/sysvar/fd_sysvar_slot_hashes.h" /* for ALUTs */
       6             : 
       7             : 
       8           0 : #define FD_SCHED_MAX_DEPTH                 (FD_RDISP_MAX_DEPTH>>2)
       9             : #define FD_SCHED_MAX_STAGING_LANES_LOG     (2)
      10             : #define FD_SCHED_MAX_STAGING_LANES         (1UL<<FD_SCHED_MAX_STAGING_LANES_LOG)
      11             : 
      12             : /* 64 ticks per slot, and a single gigantic microblock containing min
      13             :    size transactions. */
      14             : FD_STATIC_ASSERT( FD_MAX_TXN_PER_SLOT_SHRED==((FD_SHRED_DATA_PAYLOAD_MAX_PER_SLOT-65UL*sizeof(fd_microblock_hdr_t))/FD_TXN_MIN_SERIALIZED_SZ), max_txn_per_slot_shred );
      15             : 
      16             : /* We size the buffer to be able to hold residual data from the previous
      17             :    FEC set that only becomes parseable after the next FEC set is
      18             :    ingested, as well as the incoming FEC set.  The largest minimally
      19             :    parseable unit of data is a transaction.  So that much data may
      20             :    straddle FEC set boundaries.  Other minimally parseable units of data
      21             :    include the microblock header and the microblock count within a
      22             :    batch. */
      23           0 : #define FD_SCHED_MAX_PAYLOAD_PER_FEC       (FD_STORE_DATA_MAX)
      24             : #define FD_SCHED_MAX_FEC_BUF_SZ            (FD_SCHED_MAX_PAYLOAD_PER_FEC+FD_TXN_MTU)
      25             : FD_STATIC_ASSERT( FD_TXN_MTU>=sizeof(fd_microblock_hdr_t), resize buffer for residual data );
      26             : FD_STATIC_ASSERT( FD_TXN_MTU>=sizeof(ulong),               resize buffer for residual data );
      27             : 
      28           0 : #define FD_SCHED_MAGIC (0xace8a79c181f89b6UL) /* echo -n "fd_sched_v0" | sha512sum | head -c 16 */
      29             : 
      30           0 : #define FD_SCHED_PARSER_OK          (0)
      31           0 : #define FD_SCHED_PARSER_AGAIN_LATER (1)
      32           0 : #define FD_SCHED_PARSER_BAD_BLOCK   (2)
      33             : 
      34             : 
      35             : /* Structs. */
      36             : 
      37             : #define SET_NAME txn_bitset
      38             : #define SET_MAX  FD_SCHED_MAX_DEPTH
      39             : #include "../../util/tmpl/fd_set.c"
      40             : 
      41             : struct fd_sched_block {
      42             :   ulong               slot;
      43             :   ulong               parent_slot;
      44             :   ulong               parent_idx;  /* Index of the parent in the pool. */
      45             :   ulong               child_idx;   /* Index of the left-child in the pool. */
      46             :   ulong               sibling_idx; /* Index of the right-sibling in the pool. */
      47             : 
      48             :   /* Counters. */
      49             :   uint                txn_parsed_cnt;
      50             :   /*                  txn_queued_cnt = txn_parsed_cnt-txn_in_flight_cnt-txn_done_cnt */
      51             :   uint                txn_exec_in_flight_cnt;
      52             :   uint                txn_exec_done_cnt;
      53             :   uint                txn_sigverify_in_flight_cnt;
      54             :   uint                txn_sigverify_done_cnt;
      55             :   uint                txn_done_cnt; /* A transaction is considered done when all types of tasks associated with it are done. */
      56             :   ulong               txn_idx[ FD_MAX_TXN_PER_SLOT ]; /* Indexed by parse order. */
      57             :   uint                shred_cnt;
      58             : 
      59             :   /* Parser state. */
      60             :   uchar               txn[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
      61             :   fd_hash_t           poh;          /* Latest PoH hash we've seen from the ingested FEC sets. */
      62             :   ulong               mblks_rem;    /* Number of microblocks remaining in the current batch. */
      63             :   ulong               txns_rem;     /* Number of transactions remaining in the current microblock. */
      64             :   fd_acct_addr_t      aluts[ 256 ]; /* Resolve ALUT accounts into this buffer for more parallelism. */
      65             :   uint                fec_buf_sz;   /* Size of the fec_buf in bytes. */
      66             :   uint                fec_buf_soff; /* Starting offset into fec_buf for unparsed transactions. */
      67             :   uint                fec_eob:1;    /* FEC end-of-batch: set if the last FEC set in the batch is being
      68             :                                        ingested. */
      69             :   uint                fec_sob:1;    /* FEC start-of-batch: set if the parser expects to be receiving a new
      70             :                                        batch. */
      71             : 
      72             :   /* Block state. */
      73             :   uint                fec_eos:1;                          /* FEC end-of-stream: set if the last FEC set in the block has been
      74             :                                                              ingested. */
      75             :   uint                rooted:1;                           /* Set if the block is rooted. */
      76             :   uint                dying:1;                            /* Set if the block has been abandoned and no transactions should be
      77             :                                                              scheduled from it. */
      78             :   uint                in_sched:1;                         /* Set if the block is being tracked by the scheduler. */
      79             :   uint                in_rdisp:1;                         /* Set if the block is being tracked by the dispatcher, either as staged
      80             :                                                              or unstaged. */
      81             :   uint                block_start_signaled:1;             /* Set if the start-of-block sentinel has been dispatched. */
      82             :   uint                block_end_signaled:1;               /* Set if the end-of-block sentinel has been dispatched. */
      83             :   uint                block_start_done:1;                 /* Set if the start-of-block processing has been completed. */
      84             :   uint                block_end_done:1;                   /* Set if the end-of-block processing has been completed. */
      85             :   uint                staged:1;                           /* Set if the block is in a dispatcher staging lane; a staged block is
      86             :                                                              tracked by the dispatcher. */
      87             :   ulong               staging_lane;                       /* Ignored if staged==0. */
      88             :   ulong               luf_depth;                          /* Depth of longest unstaged fork starting from this node; only
      89             :                                                              stageable unstaged descendants are counted. */
      90             :   uchar               fec_buf[ FD_SCHED_MAX_FEC_BUF_SZ ]; /* The previous FEC set could have some residual data that only becomes
      91             :                                                              parseable after the next FEC set is ingested. */
      92             : };
      93             : typedef struct fd_sched_block fd_sched_block_t;
      94             : 
      95             : FD_STATIC_ASSERT( sizeof(fd_hash_t)==sizeof(((fd_microblock_hdr_t *)0)->hash), unexpected poh hash size );
      96             : 
      97             : 
      98             : struct fd_sched_metrics {
      99             :   uint  block_added_cnt;
     100             :   uint  block_added_staged_cnt;
     101             :   uint  block_added_unstaged_cnt;
     102             :   uint  block_added_dead_ood_cnt;
     103             :   uint  block_removed_cnt;
     104             :   uint  block_abandoned_cnt;
     105             :   uint  block_bad_cnt;
     106             :   uint  block_promoted_cnt;
     107             :   uint  block_demoted_cnt;
     108             :   uint  deactivate_no_child_cnt;
     109             :   uint  deactivate_no_txn_cnt;
     110             :   uint  deactivate_pruned_cnt;
     111             :   uint  deactivate_abandoned_cnt;
     112             :   uint  lane_switch_cnt;
     113             :   uint  lane_promoted_cnt;
     114             :   uint  lane_demoted_cnt;
     115             :   uint  alut_success_cnt;
     116             :   uint  alut_serializing_cnt;
     117             :   uint  txn_abandoned_parsed_cnt;
     118             :   uint  txn_abandoned_exec_done_cnt;
     119             :   uint  txn_abandoned_done_cnt;
     120             :   uint  txn_max_in_flight_cnt;
     121             :   ulong txn_weighted_in_flight_cnt;
     122             :   ulong txn_weighted_in_flight_tickcount;
     123             :   ulong txn_none_in_flight_tickcount;
     124             :   ulong txn_parsed_cnt;
     125             :   ulong txn_exec_done_cnt;
     126             :   ulong txn_sigverify_done_cnt;
     127             :   ulong txn_done_cnt;
     128             :   ulong bytes_ingested_cnt;
     129             :   ulong bytes_ingested_unparsed_cnt;
     130             :   ulong bytes_dropped_cnt;
     131             :   ulong fec_cnt;
     132             : };
     133             : typedef struct fd_sched_metrics fd_sched_metrics_t;
     134             : 
     135             : struct fd_sched {
     136             :   fd_sched_metrics_t  metrics[ 1 ];
     137             :   ulong               canary; /* == FD_SCHED_MAGIC */
     138             :   ulong               block_cnt_max; /* Immutable. */
     139             :   long                txn_in_flight_last_tick;
     140             :   ulong               root_idx;
     141             :   fd_rdisp_t *        rdisp;
     142             :   ulong               active_bank_idx; /* Index of the actively replayed block, or ULONG_MAX if no block is
     143             :                                           actively replayed; has to have a transaction to dispatch; staged
     144             :                                           blocks that have no transactions to dispatch are not eligible for
     145             :                                           being active. */
     146             :   ulong               staged_bitset;    /* Bit i set if staging lane i is occupied. */
     147             :   ulong               staged_head_bank_idx[ FD_SCHED_MAX_STAGING_LANES ]; /* Head of the linear chain in each staging lane, ignored if bit i is
     148             :                                                                              not set in the bitset. */
     149             :   ulong               txn_pool_free_cnt;
     150             :   fd_txn_p_t          txn_pool[ FD_SCHED_MAX_DEPTH ];
     151             :   ulong               txn_to_bank_idx[ FD_SCHED_MAX_DEPTH ]; /* Index of the bank that the txn belongs to. */
     152             :   txn_bitset_t        exec_done_set[ txn_bitset_word_cnt ];      /* Indexed by txn_idx. */
     153             :   txn_bitset_t        sigverify_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
     154             :   fd_sched_block_t *  block_pool; /* Just a flat array. */
     155             : };
     156             : typedef struct fd_sched fd_sched_t;
     157             : 
     158             : 
     159             : /* Internal helpers. */
     160             : 
     161             : static void
     162             : add_block( fd_sched_t * sched,
     163             :            ulong        bank_idx,
     164             :            ulong        parent_bank_idx );
     165             : 
     166             : FD_WARN_UNUSED static int
     167             : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
     168             : 
     169             : FD_WARN_UNUSED static int
     170             : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
     171             : 
     172             : static void
     173             : try_activate_block( fd_sched_t * sched );
     174             : 
     175             : static void
     176             : check_or_set_active_block( fd_sched_t * sched );
     177             : 
     178             : static void
     179             : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block );
     180             : 
     181             : static void
     182             : maybe_switch_block( fd_sched_t * sched, ulong bank_idx );
     183             : 
     184             : FD_FN_UNUSED static ulong
     185             : find_and_stage_longest_unstaged_fork( fd_sched_t * sched, int lane_idx );
     186             : 
     187             : static ulong
     188             : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx );
     189             : 
     190             : static ulong
     191             : stage_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx );
     192             : 
     193             : static inline fd_sched_block_t *
     194           0 : block_pool_ele( fd_sched_t * sched, ulong idx ) {
     195           0 :   FD_TEST( idx<sched->block_cnt_max || idx==ULONG_MAX );
     196           0 :   return idx==ULONG_MAX ? NULL : sched->block_pool+idx;
     197           0 : }
     198             : 
     199             : FD_FN_UNUSED static inline int
     200           0 : block_is_void( fd_sched_block_t * block ) {
     201           0 :   /* We've seen everything in the block and no transaction got parsed
     202           0 :      out. */
     203           0 :   return block->fec_eos && block->txn_parsed_cnt==0;
     204           0 : }
     205             : 
     206             : static inline int
     207           0 : block_should_signal_end( fd_sched_block_t * block ) {
     208           0 :   return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->block_start_done && !block->block_end_signaled;
     209           0 : }
     210             : 
     211             : static inline int
     212           0 : block_will_signal_end( fd_sched_block_t * block ) {
     213           0 :   return block->fec_eos && !block->block_end_signaled;
     214           0 : }
     215             : 
     216             : /* Is there something known to be dispatchable in the block?  This is an
     217             :    important liveness property.  A block that doesn't contain any known
     218             :    dispatchable tasks will be deactivated or demoted. */
     219             : static inline int
     220           0 : block_is_dispatchable( fd_sched_block_t * block ) {
     221           0 :   ulong exec_queued_cnt      = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
     222           0 :   ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
     223           0 :   return exec_queued_cnt>0UL ||
     224           0 :          sigverify_queued_cnt>0UL ||
     225           0 :          !block->block_start_signaled ||
     226           0 :          block_will_signal_end( block );
     227           0 : }
     228             : 
     229             : static inline int
     230           0 : block_is_in_flight( fd_sched_block_t * block ) {
     231           0 :   return block->txn_exec_in_flight_cnt || block->txn_sigverify_in_flight_cnt || (block->block_end_signaled && !block->block_end_done);
     232           0 : }
     233             : 
     234             : static inline int
     235           0 : block_is_done( fd_sched_block_t * block ) {
     236           0 :   return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->block_start_done && block->block_end_done;
     237           0 : }
     238             : 
     239             : static inline int
     240           0 : block_is_stageable( fd_sched_block_t * block ) {
     241           0 :   int rv = !block_is_done( block ) && !block->dying;
     242           0 :   if( FD_UNLIKELY( rv && !block->in_rdisp ) ) {
     243             :     /* Invariant: stageable blocks may be currently staged or unstaged,
     244             :        but must be in the dispatcher either way.  When a block
     245             :        transitions to DONE, it will be immediately removed from the
     246             :        dispatcher.  When a block transitions to DYING, it will be
     247             :        eventually abandoned from the dispatcher. */
     248           0 :     FD_LOG_CRIT(( "invariant violation: stageable block->in_rdisp==0, txn_parsed_cnt %u, txn_done_cnt %u, fec_eos %u,, slot %lu, parent slot %lu",
     249           0 :                   block->txn_parsed_cnt, block->txn_done_cnt, (uint)block->fec_eos, block->slot, block->parent_slot ));
     250           0 :   }
     251           0 :   return rv;
     252           0 : }
     253             : 
     254             : static inline int
     255           0 : block_is_promotable( fd_sched_block_t * block ) {
     256           0 :   return block_is_stageable( block ) && block_is_dispatchable( block ) && !block->staged;
     257           0 : }
     258             : 
     259             : static inline int
     260           0 : block_is_activatable( fd_sched_block_t * block ) {
     261           0 :   return block_is_stageable( block ) && block_is_dispatchable( block ) && block->staged;
     262           0 : }
     263             : 
     264             : static inline int
     265           0 : block_should_deactivate( fd_sched_block_t * block ) {
     266             :   /* We allow a grace period, during which a block has nothing to
     267             :      dispatch, but has something in-flight.  The block is allowed to
     268             :      stay activated and ingest FEC sets during this time.  The block
     269             :      will be deactivated if there's still nothing to dispatch by the
     270             :      time all in-flight tasks are completed. */
     271           0 :   return !block_is_activatable( block ) && !block_is_in_flight( block );
     272           0 : }
     273             : 
     274             : FD_FN_UNUSED static void
     275           0 : print_block( fd_sched_block_t * block ) {
     276           0 :   FD_LOG_NOTICE(( "block slot %lu, parent_slot %lu, staged %d (lane %lu), dying %d, in_rdisp %d, fec_eos %d, rooted %d, block_start_signaled %d, block_end_signaled %d, block_start_done %d, block_end_done %d, txn_parsed_cnt %u, txn_exec_in_flight_cnt %u, txn_exec_done_cnt %u, txn_sigverify_in_flight_cnt %u, txn_sigverify_done_cnt %u, txn_done_cnt %u, shred_cnt %u",
     277           0 :                   block->slot, block->parent_slot, block->staged, block->staging_lane, block->dying, block->in_rdisp, block->fec_eos, block->rooted, block->block_start_signaled, block->block_end_signaled, block->block_start_done, block->block_end_done, block->txn_parsed_cnt, block->txn_exec_in_flight_cnt, block->txn_exec_done_cnt, block->txn_sigverify_in_flight_cnt, block->txn_sigverify_done_cnt, block->txn_done_cnt, block->shred_cnt ));
     278           0 : }
     279             : 
     280             : FD_FN_UNUSED static void
     281           0 : print_block_and_parent( fd_sched_t * sched, fd_sched_block_t * block ) {
     282           0 :   print_block( block );
     283           0 :   fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
     284           0 :   if( FD_LIKELY( parent ) ) print_block( parent );
     285           0 : }
     286             : 
     287             : FD_FN_UNUSED static void
     288           0 : print_metrics( fd_sched_t * sched ) {
     289           0 :   FD_LOG_NOTICE(( "metrics: block_added_cnt %u, block_added_staged_cnt %u, block_added_unstaged_cnt %u, block_added_dead_ood_cnt %u, block_removed_cnt %u, block_abandoned_cnt %u, block_promoted_cnt %u, block_demoted_cnt %u, deactivate_no_child_cnt %u, deactivate_no_txn_cnt %u, deactivate_pruned_cnt %u, deactivate_abandoned_cnt %u, lane_switch_cnt %u, lane_promoted_cnt %u, lane_demoted_cnt %u, alut_success_cnt %u, alut_serializing_cnt %u, txn_abandoned_parsed_cnt %u, txn_abandoned_done_cnt %u, txn_max_in_flight_cnt %u, txn_weighted_in_flight_cnt %lu, txn_weighted_in_flight_tickcount %lu, txn_none_in_flight_tickcount %lu, txn_parsed_cnt %lu, txn_exec_done_cnt %lu, txn_sigverify_done_cnt %lu, txn_done_cnt %lu, bytes_ingested_cnt %lu, bytes_ingested_unparsed_cnt %lu, bytes_dropped_cnt %lu, fec_cnt %lu",
     290           0 :                   sched->metrics->block_added_cnt, sched->metrics->block_added_staged_cnt, sched->metrics->block_added_unstaged_cnt, sched->metrics->block_added_dead_ood_cnt, sched->metrics->block_removed_cnt, sched->metrics->block_abandoned_cnt, sched->metrics->block_promoted_cnt, sched->metrics->block_demoted_cnt, sched->metrics->deactivate_no_child_cnt, sched->metrics->deactivate_no_txn_cnt, sched->metrics->deactivate_pruned_cnt, sched->metrics->deactivate_abandoned_cnt, sched->metrics->lane_switch_cnt, sched->metrics->lane_promoted_cnt, sched->metrics->lane_demoted_cnt, sched->metrics->alut_success_cnt, sched->metrics->alut_serializing_cnt, sched->metrics->txn_abandoned_parsed_cnt, sched->metrics->txn_abandoned_done_cnt, sched->metrics->txn_max_in_flight_cnt, sched->metrics->txn_weighted_in_flight_cnt, sched->metrics->txn_weighted_in_flight_tickcount, sched->metrics->txn_none_in_flight_tickcount, sched->metrics->txn_parsed_cnt, sched->metrics->txn_exec_done_cnt, sched->metrics->txn_sigverify_done_cnt, sched->metrics->txn_done_cnt, sched->metrics->bytes_ingested_cnt, sched->metrics->bytes_ingested_unparsed_cnt, sched->metrics->bytes_dropped_cnt, sched->metrics->fec_cnt ));
     291           0 : }
     292             : 
     293             : FD_FN_UNUSED static void
     294           0 : print_sched( fd_sched_t * sched ) {
     295           0 :   FD_LOG_NOTICE(( "sched canary 0x%lx, block_cnt_max %lu, root_idx %lu, active_idx %lu, staged_bitset %lu, staged_head_idx[0] %lu, staged_head_idx[1] %lu, staged_head_idx[2] %lu, staged_head_idx[3] %lu, txn_pool_free_cnt %lu",
     296           0 :                   sched->canary, sched->block_cnt_max, sched->root_idx, sched->active_bank_idx, sched->staged_bitset, sched->staged_head_bank_idx[ 0 ], sched->staged_head_bank_idx[ 1 ], sched->staged_head_bank_idx[ 2 ], sched->staged_head_bank_idx[ 3 ], sched->txn_pool_free_cnt ));
     297           0 : }
     298             : 
     299             : FD_FN_UNUSED static void
     300           0 : print_all( fd_sched_t * sched, fd_sched_block_t * block ) {
     301           0 :   print_metrics( sched );
     302           0 :   print_sched( sched );
     303           0 :   print_block_and_parent( sched, block );
     304           0 : }
     305             : 
     306             : 
     307             : /* Public functions. */
     308             : 
     309           0 : ulong fd_sched_align( void ) {
     310           0 :   return fd_ulong_max( alignof(fd_sched_t),
     311           0 :          fd_ulong_max( fd_rdisp_align(),
     312           0 :          fd_ulong_max( alignof(fd_sched_block_t), 64UL ))); /* Minimally cache line aligned. */
     313           0 : }
     314             : 
     315             : ulong
     316           0 : fd_sched_footprint( ulong block_cnt_max ) {
     317           0 :   ulong l = FD_LAYOUT_INIT;
     318           0 :   l = FD_LAYOUT_APPEND( l, fd_sched_align(),          sizeof(fd_sched_t)                                      );
     319           0 :   l = FD_LAYOUT_APPEND( l, fd_rdisp_align(),          fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) ); /* dispatcher */
     320           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t)                  ); /* block pool */
     321           0 :   return FD_LAYOUT_FINI( l, fd_sched_align() );
     322           0 : }
     323             : 
     324             : void *
     325           0 : fd_sched_new( void * mem, ulong block_cnt_max ) {
     326           0 :   FD_SCRATCH_ALLOC_INIT( l, mem );
     327           0 :   fd_sched_t * sched = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(),          sizeof(fd_sched_t)                                      );
     328           0 :   void * _rdisp      = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(),          fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) );
     329           0 :   void * _bpool      = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t)                  );
     330           0 :   FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
     331             : 
     332           0 :   ulong seed = ((ulong)fd_tickcount()) ^ FD_SCHED_MAGIC;
     333           0 :   fd_rdisp_new( _rdisp, FD_SCHED_MAX_DEPTH, block_cnt_max, seed );
     334             : 
     335           0 :   fd_sched_block_t * bpool = (fd_sched_block_t *)_bpool;
     336           0 :   for( ulong i=0; i<block_cnt_max; i++ ) {
     337           0 :     bpool[ i ].in_sched = 0;
     338           0 :   }
     339             : 
     340           0 :   fd_memset( sched->metrics, 0, sizeof(fd_sched_metrics_t) );
     341           0 :   sched->txn_in_flight_last_tick = LONG_MAX;
     342             : 
     343           0 :   sched->canary           = FD_SCHED_MAGIC;
     344           0 :   sched->block_cnt_max    = block_cnt_max;
     345           0 :   sched->root_idx         = ULONG_MAX;
     346           0 :   sched->active_bank_idx  = ULONG_MAX;
     347           0 :   sched->staged_bitset    = 0UL;
     348             : 
     349           0 :   sched->txn_pool_free_cnt = FD_SCHED_MAX_DEPTH-1UL; /* -1 because index 0 is unusable as a sentinel reserved by the dispatcher */
     350             : 
     351           0 :   txn_bitset_new( sched->exec_done_set );
     352           0 :   txn_bitset_new( sched->sigverify_done_set );
     353             : 
     354           0 :   return sched;
     355           0 : }
     356             : 
     357             : fd_sched_t *
     358           0 : fd_sched_join( void * mem, ulong block_cnt_max ) {
     359           0 :   fd_sched_t * sched = (fd_sched_t *)mem;
     360             : 
     361           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     362           0 :   FD_TEST( sched->block_cnt_max==block_cnt_max );
     363             : 
     364           0 :   FD_SCRATCH_ALLOC_INIT( l, mem );
     365           0 :   /*           */ FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(),          sizeof(fd_sched_t)                                      );
     366           0 :   void * _rdisp = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(),          fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) );
     367           0 :   void * _bpool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t)                  );
     368           0 :   FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
     369             : 
     370           0 :   sched->rdisp      = fd_rdisp_join( _rdisp );
     371           0 :   sched->block_pool = _bpool;
     372             : 
     373           0 :   txn_bitset_join( sched->exec_done_set );
     374           0 :   txn_bitset_join( sched->sigverify_done_set );
     375             : 
     376           0 :   return sched;
     377           0 : }
     378             : 
     379             : int
     380           0 : fd_sched_fec_can_ingest( fd_sched_t * sched, fd_sched_fec_t * fec ) {
     381           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     382           0 :   FD_TEST( fec->bank_idx<sched->block_cnt_max );
     383           0 :   FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
     384             : 
     385           0 :   if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
     386           0 :     FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
     387           0 :                   fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     388           0 :   }
     389             : 
     390           0 :   ulong fec_buf_sz = 0UL;
     391           0 :   fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
     392           0 :   if( FD_LIKELY( !fec->is_first_in_block ) ) {
     393           0 :     fec_buf_sz += block->fec_buf_sz-block->fec_buf_soff;
     394           0 :   } else {
     395             :     /* No residual data as this is a fresh new block. */
     396           0 :   }
     397             :   /* Addition is safe and won't overflow because we checked the FEC set
     398             :      size above. */
     399           0 :   fec_buf_sz += fec->fec->data_sz;
     400             :   /* Assuming every transaction is min size, do we have enough free
     401             :      entries in the txn pool?  For a more precise txn count, we would
     402             :      have to do some parsing. */
     403           0 :   return sched->txn_pool_free_cnt>=fec_buf_sz/FD_TXN_MIN_SERIALIZED_SZ;
     404           0 : }
     405             : 
     406             : int
     407           0 : fd_sched_can_ingest( fd_sched_t * sched ) {
     408           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     409             :   /* Worst case, we need one byte from the incoming data to extract a
     410             :      transaction out of the residual data, and the rest of the incoming
     411             :      data contributes toward min sized transactions. */
     412           0 :   ulong txn_cnt = (FD_SCHED_MAX_PAYLOAD_PER_FEC-1UL)/FD_TXN_MIN_SERIALIZED_SZ+1UL; /* 478 */
     413           0 :   return sched->txn_pool_free_cnt>=txn_cnt;
     414           0 : }
     415             : 
     416             : FD_WARN_UNUSED int
     417           0 : fd_sched_fec_ingest( fd_sched_t * sched, fd_sched_fec_t * fec ) {
     418           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     419           0 :   FD_TEST( fec->bank_idx<sched->block_cnt_max );
     420           0 :   FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
     421             : 
     422           0 :   if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
     423           0 :     FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
     424           0 :                   fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     425           0 :   }
     426             : 
     427           0 :   fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
     428           0 :   if( FD_UNLIKELY( fec->is_first_in_block ) ) {
     429             :     /* This is a new block. */
     430           0 :     add_block( sched, fec->bank_idx, fec->parent_bank_idx );
     431           0 :     block->slot        = fec->slot;
     432           0 :     block->parent_slot = fec->parent_slot;
     433             : 
     434           0 :     if( FD_UNLIKELY( block->dying ) ) {
     435             :       /* The child of a dead block is also dead.  We added it to our
     436             :          fork tree just so we could track an entire lineage of dead
     437             :          children and propagate the dead property to the entire lineage,
     438             :          in case there were frags for more than one dead children
     439             :          in-flight at the time the parent was abandoned.  That being
     440             :          said, we shouldn't need to add the dead child to the
     441             :          dispatcher. */
     442           0 :       sched->metrics->block_added_dead_ood_cnt++;
     443             : 
     444             :       /* Ignore the FEC set for a dead block. */
     445           0 :       sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
     446           0 :       return 1;
     447           0 :     }
     448             : 
     449             :     /* Try to find a staging lane for this block. */
     450           0 :     int alloc_lane = 0;
     451           0 :     fd_sched_block_t * parent_block = block_pool_ele( sched, fec->parent_bank_idx );
     452           0 :     if( FD_LIKELY( parent_block->staged ) ) {
     453             :       /* Parent is staged.  So see if we can continue down the same
     454             :          staging lane. */
     455           0 :       ulong staging_lane = parent_block->staging_lane;
     456           0 :       ulong child_idx    = parent_block->child_idx;
     457           0 :       while( child_idx!=ULONG_MAX ) {
     458           0 :         fd_sched_block_t * child = block_pool_ele( sched, child_idx );
     459           0 :         if( child->staged && child->staging_lane==staging_lane ) {
     460             :           /* Found a child on the same lane.  So we're done. */
     461           0 :           staging_lane = FD_RDISP_UNSTAGED;
     462           0 :           break;
     463           0 :         }
     464           0 :         child_idx = child->sibling_idx;
     465           0 :       }
     466             :       /* No child is staged on the same lane as the parent.  So stage
     467             :          this block.  This is the common case. */
     468           0 :       if( FD_LIKELY( staging_lane!=FD_RDISP_UNSTAGED ) ) {
     469           0 :         block->in_rdisp     = 1;
     470           0 :         block->staged       = 1;
     471           0 :         block->staging_lane = staging_lane;
     472           0 :         fd_rdisp_add_block( sched->rdisp, fec->bank_idx, staging_lane );
     473           0 :         sched->metrics->block_added_cnt++;
     474           0 :         sched->metrics->block_added_staged_cnt++;
     475           0 :       } else {
     476           0 :         alloc_lane = 1;
     477           0 :       }
     478           0 :     } else {
     479           0 :       if( block_is_stageable( parent_block ) ) {
     480             :         /* Parent is unstaged but stageable.  So let's be unstaged too.
     481             :            This is a policy decision to be lazy and not promote parent
     482             :            at the moment. */
     483           0 :         block->in_rdisp = 1;
     484           0 :         block->staged   = 0;
     485           0 :         fd_rdisp_add_block( sched->rdisp, fec->bank_idx, FD_RDISP_UNSTAGED );
     486           0 :         sched->metrics->block_added_cnt++;
     487           0 :         sched->metrics->block_added_unstaged_cnt++;
     488           0 :       } else {
     489           0 :         alloc_lane = 1;
     490           0 :       }
     491           0 :     }
     492           0 :     if( FD_UNLIKELY( alloc_lane ) ) {
     493             :       /* We weren't able to inherit the parent's staging lane.  So try
     494             :          to find a new staging lane. */
     495           0 :       if( FD_LIKELY( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) { /* Optimize for lane available. */
     496           0 :         int lane_idx = fd_ulong_find_lsb( ~sched->staged_bitset );
     497           0 :         if( FD_UNLIKELY( lane_idx>=(int)FD_SCHED_MAX_STAGING_LANES ) ) {
     498           0 :           FD_LOG_CRIT(( "invariant violation: lane_idx %d, sched->staged_bitset %lx",
     499           0 :                         lane_idx, sched->staged_bitset ));
     500           0 :         }
     501           0 :         sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
     502           0 :         sched->staged_head_bank_idx[ lane_idx ] = fec->bank_idx;
     503           0 :         block->in_rdisp     = 1;
     504           0 :         block->staged       = 1;
     505           0 :         block->staging_lane = (ulong)lane_idx;
     506           0 :         fd_rdisp_add_block( sched->rdisp, fec->bank_idx, block->staging_lane );
     507           0 :         sched->metrics->block_added_cnt++;
     508           0 :         sched->metrics->block_added_staged_cnt++;
     509           0 :       } else {
     510             :         /* No lanes available. */
     511           0 :         block->in_rdisp = 1;
     512           0 :         block->staged   = 0;
     513           0 :         fd_rdisp_add_block( sched->rdisp, fec->bank_idx, FD_RDISP_UNSTAGED );
     514           0 :         sched->metrics->block_added_cnt++;
     515           0 :         sched->metrics->block_added_unstaged_cnt++;
     516           0 :       }
     517           0 :     }
     518           0 :   }
     519             : 
     520           0 :   if( FD_UNLIKELY( block->dying ) ) {
     521             :     /* Ignore the FEC set for a dead block. */
     522           0 :     sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
     523           0 :     return 1;
     524           0 :   }
     525             : 
     526           0 :   if( FD_UNLIKELY( !block->in_rdisp ) ) {
     527             :     /* Invariant: block must be in the dispatcher at this point. */
     528           0 :     FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, slot %lu, parent slot %lu",
     529           0 :                   block->slot, block->parent_slot ));
     530           0 :   }
     531             : 
     532           0 :   if( FD_UNLIKELY( block->fec_eos ) ) {
     533             :     /* This means something is wrong upstream. */
     534           0 :     FD_LOG_CRIT(( "invariant violation: block->fec_eos set but getting more FEC sets fec->mr %s, slot %lu, parent slot %lu",
     535           0 :                   FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     536           0 :   }
     537           0 :   if( FD_UNLIKELY( block->fec_eob && fec->is_last_in_batch ) ) {
     538             :     /* This means the previous batch didn't parse properly.  So this is
     539             :        a bad block.  We should refuse to replay down the fork. */
     540           0 :     FD_LOG_INFO(( "bad block: block->fec_eob set but getting another FEC set that is last in batch fec->mr %s, slot %lu, parent slot %lu",
     541           0 :                   FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     542           0 :     subtree_abandon( sched, block );
     543           0 :     sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
     544           0 :     check_or_set_active_block( sched );
     545           0 :     sched->metrics->block_bad_cnt++;
     546           0 :     return 0;
     547           0 :   }
     548           0 :   if( FD_UNLIKELY( block->child_idx!=ULONG_MAX ) ) {
     549             :     /* This means something is wrong upstream.  FEC sets are not being
     550             :        delivered in replay order. */
     551           0 :     FD_LOG_CRIT(( "invariant violation: block->child_idx %lu, fec->mr %s, slot %lu, parent slot %lu",
     552           0 :                   block->child_idx, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     553           0 :   }
     554             : 
     555           0 :   FD_TEST( block->fec_buf_sz>=block->fec_buf_soff );
     556           0 :   if( FD_LIKELY( block->fec_buf_sz>block->fec_buf_soff ) ) {
     557             :     /* If there is residual data from the previous FEC set within the
     558             :        same batch, we move it to the beginning of the buffer and append
     559             :        the new FEC set. */
     560           0 :     memmove( block->fec_buf, block->fec_buf+block->fec_buf_soff, block->fec_buf_sz-block->fec_buf_soff );
     561           0 :   }
     562           0 :   block->fec_buf_sz  -= block->fec_buf_soff;
     563           0 :   block->fec_buf_soff = 0;
     564             :   /* Addition is safe and won't overflow because we checked the FEC
     565             :      set size above. */
     566           0 :   if( FD_UNLIKELY( block->fec_buf_sz-block->fec_buf_soff+fec->fec->data_sz>FD_SCHED_MAX_FEC_BUF_SZ ) ) {
     567             :     /* In a conformant block, there shouldn't be more than a
     568             :        transaction's worth of residual data left over from the previous
     569             :        FEC set within the same batch.  So if this condition doesn't
     570             :        hold, it's a bad block.  Instead of crashing, we should refuse to
     571             :        replay down the fork. */
     572           0 :     FD_LOG_INFO(( "bad block: fec_buf_sz %u, fec_buf_soff %u, fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
     573           0 :                   block->fec_buf_sz, block->fec_buf_soff, fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
     574           0 :     subtree_abandon( sched, block );
     575           0 :     sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
     576           0 :     sched->metrics->block_bad_cnt++;
     577           0 :     check_or_set_active_block( sched );
     578           0 :     return 0;
     579           0 :   }
     580             : 
     581           0 :   block->shred_cnt += fec->shred_cnt;
     582           0 :   sched->metrics->fec_cnt++;
     583             : 
     584             :   /* Append the new FEC set to the end of the buffer. */
     585           0 :   fd_memcpy( block->fec_buf+block->fec_buf_sz, fec->fec->data, fec->fec->data_sz );
     586           0 :   block->fec_buf_sz += (uint)fec->fec->data_sz;
     587           0 :   sched->metrics->bytes_ingested_cnt += fec->fec->data_sz;
     588             : 
     589           0 :   block->fec_eob = fec->is_last_in_batch;
     590           0 :   block->fec_eos = fec->is_last_in_block;
     591             : 
     592           0 :   int err = fd_sched_parse( sched, block, fec->alut_ctx );
     593           0 :   if( FD_UNLIKELY( err==FD_SCHED_PARSER_BAD_BLOCK ) ) {
     594           0 :     FD_LOG_INFO(( "bad block: slot %lu, parent slot %lu, txn_parsed_cnt %u, txn_done_cnt %u",
     595           0 :                   fec->slot, fec->parent_slot, block->txn_parsed_cnt, block->txn_done_cnt ));
     596           0 :     subtree_abandon( sched, block );
     597           0 :     sched->metrics->bytes_dropped_cnt += block->fec_buf_sz-block->fec_buf_soff;
     598           0 :     sched->metrics->block_bad_cnt++;
     599           0 :     check_or_set_active_block( sched );
     600           0 :     return 0;
     601           0 :   }
     602             : 
     603             :   /* Check if we need to set the active block. */
     604           0 :   check_or_set_active_block( sched );
     605             : 
     606           0 :   return 1;
     607           0 : }
     608             : 
     609             : ulong
     610           0 : fd_sched_task_next_ready( fd_sched_t * sched, fd_sched_task_t * out, ulong exec_tile_cnt ) {
     611           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     612             : 
     613           0 :   if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
     614             :     /* No need to try activating a block.  If we're in this state,
     615             :        there's truly nothing to execute.  We will activate something
     616             :        when we ingest a FEC set with transactions. */
     617           0 :     return 0UL;
     618           0 :   }
     619             : 
     620           0 :   out->task_type = FD_SCHED_TT_NULL;
     621             : 
     622             :   /* We could in theory reevaluate staging lane allocation here and do
     623             :      promotion/demotion as needed.  It's a policy decision to minimize
     624             :      fork churn for now and just execute down the same active fork. */
     625             : 
     626           0 :   ulong bank_idx = sched->active_bank_idx;
     627           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
     628           0 :   if( FD_UNLIKELY( block_should_deactivate( block ) ) ) {
     629           0 :     print_all( sched, block );
     630           0 :     FD_LOG_CRIT(( "invariant violation: active_bank_idx %lu is not activatable nor has anything in-flight", sched->active_bank_idx ));
     631           0 :   }
     632             : 
     633           0 :   if( FD_UNLIKELY( !block->block_start_signaled ) ) {
     634           0 :     out->task_type = FD_SCHED_TT_BLOCK_START;
     635           0 :     out->block_start->bank_idx        = bank_idx;
     636           0 :     out->block_start->parent_bank_idx = block->parent_idx;
     637           0 :     out->block_start->slot            = block->slot;
     638           0 :     block->block_start_signaled = 1;
     639           0 :     return 1UL;
     640           0 :   }
     641             : 
     642           0 :   ulong exec_queued_cnt = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
     643           0 :   if( FD_LIKELY( exec_queued_cnt>0UL ) ) { /* Optimize for no fork switching. */
     644           0 :     out->txn_exec->txn_idx = fd_rdisp_get_next_ready( sched->rdisp, bank_idx );
     645           0 :     if( FD_UNLIKELY( out->txn_exec->txn_idx==0UL ) ) {
     646             :       /* There are transactions queued but none ready for execution.
     647             :          This implies that there must be in-flight transactions on whose
     648             :          completion the queued transactions depend. So we return and
     649             :          wait for those in-flight transactions to retire.  This is a
     650             :          policy decision to execute as much as we can down the current
     651             :          fork. */
     652           0 :       if( FD_UNLIKELY( !block->txn_exec_in_flight_cnt ) ) {
     653           0 :         print_all( sched, block );
     654           0 :         FD_LOG_CRIT(( "invariant violation: no ready transaction found but block->txn_exec_in_flight_cnt==0" ));
     655           0 :       }
     656           0 :       ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
     657           0 :       if( FD_LIKELY( sigverify_queued_cnt>0UL && exec_tile_cnt>1UL ) ) { /* Leave one exec tile idle for critical path execution. */
     658             :         /* Dispatch transactions for sigverify in parse order. */
     659           0 :         out->task_type = FD_SCHED_TT_TXN_SIGVERIFY;
     660           0 :         out->txn_sigverify->bank_idx = bank_idx;
     661           0 :         out->txn_sigverify->txn_idx  = block->txn_idx[ block->txn_sigverify_done_cnt+block->txn_sigverify_in_flight_cnt ];
     662           0 :         block->txn_sigverify_in_flight_cnt++;
     663           0 :         return 1UL;
     664           0 :       }
     665           0 :       return 0UL;
     666           0 :     }
     667           0 :     out->task_type = FD_SCHED_TT_TXN_EXEC;
     668           0 :     out->txn_exec->bank_idx = bank_idx;
     669           0 :     out->txn_exec->slot     = block->slot;
     670             : 
     671           0 :     long now = fd_tickcount();
     672           0 :     ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
     673           0 :     sched->metrics->txn_none_in_flight_tickcount     += fd_ulong_if( block->txn_exec_in_flight_cnt==0U && sched->txn_in_flight_last_tick!=LONG_MAX, delta, 0UL );
     674           0 :     sched->metrics->txn_weighted_in_flight_tickcount += fd_ulong_if( block->txn_exec_in_flight_cnt!=0U, delta, 0UL );
     675           0 :     sched->metrics->txn_weighted_in_flight_cnt       += delta*block->txn_exec_in_flight_cnt;
     676           0 :     sched->txn_in_flight_last_tick = now;
     677             : 
     678           0 :     block->txn_exec_in_flight_cnt++;
     679           0 :     sched->metrics->txn_max_in_flight_cnt = fd_uint_max( sched->metrics->txn_max_in_flight_cnt, block->txn_exec_in_flight_cnt );
     680           0 :     return 1UL;
     681           0 :   }
     682             : 
     683             :   /* At this point txn_queued_cnt==0 */
     684             : 
     685             :   /* Try to dispatch a sigverify task, but leave one exec tile idle for
     686             :      critical path execution, unless there's not going to be any more
     687             :      real transactions for the critical path. */
     688           0 :   ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
     689           0 :   if( FD_LIKELY( sigverify_queued_cnt>0UL && exec_tile_cnt>fd_ulong_if( block->fec_eos, 0UL, 1UL ) ) ) {
     690             :     /* Dispatch transactions for sigverify in parse order. */
     691           0 :     out->task_type = FD_SCHED_TT_TXN_SIGVERIFY;
     692           0 :     out->txn_sigverify->txn_idx  = block->txn_idx[ block->txn_sigverify_done_cnt+block->txn_sigverify_in_flight_cnt ];
     693           0 :     out->txn_sigverify->bank_idx = bank_idx;
     694           0 :     block->txn_sigverify_in_flight_cnt++;
     695           0 :     return 1UL;
     696           0 :   }
     697             : 
     698           0 :   if( FD_UNLIKELY( block_should_signal_end( block ) ) ) {
     699           0 :     FD_TEST( block->block_start_signaled );
     700           0 :     out->task_type = FD_SCHED_TT_BLOCK_END;
     701           0 :     out->block_end->bank_idx = bank_idx;
     702           0 :     block->block_end_signaled = 1;
     703           0 :     return 1UL;
     704           0 :   }
     705             : 
     706             :   /* Nothing queued for the active block.  If we haven't received all
     707             :      the FEC sets for it, then return and wait for more FEC sets, while
     708             :      there are in-flight transactions.  This is a policy decision to
     709             :      minimize fork churn and allow for executing down the current fork
     710             :      as much as we can.  If we have received all the FEC sets for it,
     711             :      then we'd still like to return and wait for the in-flight
     712             :      transactions to retire, before switching to a different block.
     713             : 
     714             :      Either way, there should be in-flight transactions.  We deactivate
     715             :      the active block the moment we exhausted transactions from it. */
     716           0 :   if( FD_UNLIKELY( !block_is_in_flight( block ) ) ) {
     717           0 :     print_all( sched, block );
     718           0 :     FD_LOG_CRIT(( "invariant violation: expected in-flight transactions but none" ));
     719           0 :   }
     720             : 
     721           0 :   return 0UL;
     722           0 : }
     723             : 
     724             : void
     725           0 : fd_sched_task_done( fd_sched_t * sched, ulong task_type, ulong txn_idx ) {
     726           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     727             : 
     728           0 :   ulong bank_idx = ULONG_MAX;
     729           0 :   switch( task_type ) {
     730           0 :     case FD_SCHED_TT_BLOCK_START:
     731           0 :     case FD_SCHED_TT_BLOCK_END: {
     732           0 :       (void)txn_idx;
     733           0 :       bank_idx = sched->active_bank_idx;
     734           0 :       break;
     735           0 :     }
     736           0 :     case FD_SCHED_TT_TXN_EXEC:
     737           0 :     case FD_SCHED_TT_TXN_SIGVERIFY: {
     738           0 :       FD_TEST( txn_idx<FD_SCHED_MAX_DEPTH );
     739           0 :       bank_idx = sched->txn_to_bank_idx[ txn_idx ];
     740           0 :       break;
     741           0 :     }
     742           0 :     default: FD_LOG_CRIT(( "unsupported task_type %lu", task_type ));
     743           0 :   }
     744           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
     745             : 
     746           0 :   if( FD_UNLIKELY( !block->staged ) ) {
     747             :     /* Invariant: only staged blocks can have in-flight transactions. */
     748           0 :     FD_LOG_CRIT(( "invariant violation: block->staged==0, slot %lu, parent slot %lu",
     749           0 :                   block->slot, block->parent_slot ));
     750           0 :   }
     751           0 :   if( FD_UNLIKELY( !block->in_rdisp ) ) {
     752             :     /* Invariant: staged blocks must be in the dispatcher. */
     753           0 :     FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, slot %lu, parent slot %lu",
     754           0 :                   block->slot, block->parent_slot ));
     755           0 :   }
     756             : 
     757           0 :   switch( task_type ) {
     758           0 :     case FD_SCHED_TT_BLOCK_START: {
     759           0 :       FD_TEST( !block->block_start_done );
     760           0 :       block->block_start_done = 1;
     761           0 :       break;
     762           0 :     }
     763           0 :     case FD_SCHED_TT_BLOCK_END: {
     764             :       /* It may seem redundant to be invoking task_done() on these
     765             :          somewhat fake tasks.  But these are necessary to drive state
     766             :          transition for empty blocks or slow blocks. */
     767           0 :       FD_TEST( !block->block_end_done );
     768           0 :       block->block_end_done = 1;
     769           0 :       break;
     770           0 :     }
     771           0 :     case FD_SCHED_TT_TXN_EXEC: {
     772           0 :       long now = fd_tickcount();
     773           0 :       ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
     774           0 :       sched->metrics->txn_weighted_in_flight_tickcount += delta;
     775           0 :       sched->metrics->txn_weighted_in_flight_cnt       += delta*block->txn_exec_in_flight_cnt;
     776           0 :       sched->txn_in_flight_last_tick = now;
     777             : 
     778           0 :       block->txn_exec_done_cnt++;
     779           0 :       block->txn_exec_in_flight_cnt--;
     780           0 :       sched->metrics->txn_exec_done_cnt++;
     781           0 :       txn_bitset_insert( sched->exec_done_set, txn_idx );
     782           0 :       if( txn_bitset_test( sched->exec_done_set, txn_idx ) && txn_bitset_test( sched->sigverify_done_set, txn_idx ) ) {
     783             :         /* Release txn_idx if both exec and sigverify are done.  This is
     784             :            guaranteed to only happen once per transaction because
     785             :            whichever one completed first would not release. */
     786           0 :         fd_rdisp_complete_txn( sched->rdisp, txn_idx, 1 );
     787           0 :         sched->txn_pool_free_cnt++;
     788           0 :         block->txn_done_cnt++;
     789           0 :         sched->metrics->txn_done_cnt++;
     790           0 :       } else {
     791           0 :         fd_rdisp_complete_txn( sched->rdisp, txn_idx, 0 );
     792           0 :       }
     793           0 :       break;
     794           0 :     }
     795           0 :     case FD_SCHED_TT_TXN_SIGVERIFY: {
     796           0 :       block->txn_sigverify_done_cnt++;
     797           0 :       block->txn_sigverify_in_flight_cnt--;
     798           0 :       sched->metrics->txn_sigverify_done_cnt++;
     799           0 :       txn_bitset_insert( sched->sigverify_done_set, txn_idx );
     800           0 :       if( txn_bitset_test( sched->exec_done_set, txn_idx ) && txn_bitset_test( sched->sigverify_done_set, txn_idx ) ) {
     801             :         /* Release txn_idx if both exec and sigverify are done.  This is
     802             :            guaranteed to only happen once per transaction because
     803             :            whichever one completed first would not release. */
     804           0 :         fd_rdisp_complete_txn( sched->rdisp, txn_idx, 1 );
     805           0 :         sched->txn_pool_free_cnt++;
     806           0 :         block->txn_done_cnt++;
     807           0 :         sched->metrics->txn_done_cnt++;
     808           0 :       }
     809           0 :       break;
     810           0 :     }
     811           0 :   }
     812             : 
     813           0 :   if( FD_UNLIKELY( block->dying && !block_is_in_flight( block ) ) ) {
     814           0 :     if( FD_UNLIKELY( sched->active_bank_idx==bank_idx ) ) {
     815           0 :       FD_LOG_CRIT(( "invariant violation: active block shouldn't be dying, bank_idx %lu, slot %lu, parent slot %lu",
     816           0 :                     bank_idx, block->slot, block->parent_slot ));
     817           0 :     }
     818           0 :     subtree_abandon( sched, block );
     819           0 :     return;
     820           0 :   }
     821             : 
     822           0 :   if( FD_UNLIKELY( !block->dying && sched->active_bank_idx!=bank_idx ) ) {
     823             :     /* Block is not dead.  So we should be actively replaying it. */
     824           0 :     fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
     825           0 :     FD_LOG_CRIT(( "invariant violation: sched->active_bank_idx %lu, slot %lu, parent slot %lu, bank_idx %lu, slot %lu, parent slot %lu",
     826           0 :                   sched->active_bank_idx, active_block->slot, active_block->parent_slot,
     827           0 :                   bank_idx, block->slot, block->parent_slot ));
     828           0 :   }
     829             : 
     830           0 :   maybe_switch_block( sched, bank_idx );
     831           0 : }
     832             : 
     833             : void
     834           0 : fd_sched_block_abandon( fd_sched_t * sched, ulong bank_idx ) {
     835           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     836           0 :   FD_TEST( bank_idx<sched->block_cnt_max );
     837             : 
     838           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
     839           0 :   if( FD_UNLIKELY( bank_idx!=sched->active_bank_idx ) ) {
     840             :     /* Invariant: abandoning should only be performed on actively
     841             :        replayed blocks.  We impose this requirement on the caller
     842             :        because the dispatcher expects blocks to be abandoned in the same
     843             :        order that they were added, and having this requirement makes it
     844             :        easier to please the dispatcher. */
     845           0 :     print_all( sched, block );
     846           0 :     FD_LOG_CRIT(( "invariant violation: active_bank_idx %lu, bank_idx %lu, slot %lu, parent slot %lu",
     847           0 :                   sched->active_bank_idx, bank_idx, block->slot, block->parent_slot ));
     848           0 :   }
     849             : 
     850           0 :   subtree_abandon( sched, block );
     851             : 
     852             :   /* Reset the active block. */
     853           0 :   sched->active_bank_idx = ULONG_MAX;
     854           0 :   sched->metrics->deactivate_abandoned_cnt++;
     855           0 :   try_activate_block( sched );
     856           0 : }
     857             : 
     858             : void
     859           0 : fd_sched_block_add_done( fd_sched_t * sched, ulong bank_idx, ulong parent_bank_idx ) {
     860           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     861           0 :   FD_TEST( bank_idx<sched->block_cnt_max );
     862             : 
     863           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
     864           0 :   add_block( sched, bank_idx, parent_bank_idx );
     865           0 :   block->txn_parsed_cnt         = UINT_MAX;
     866           0 :   block->txn_exec_done_cnt      = UINT_MAX;
     867           0 :   block->txn_sigverify_done_cnt = UINT_MAX;
     868           0 :   block->txn_done_cnt           = UINT_MAX;
     869           0 :   block->fec_eos                = 1;
     870           0 :   block->block_start_signaled   = 1;
     871           0 :   block->block_end_signaled     = 1;
     872           0 :   block->block_start_done       = 1;
     873           0 :   block->block_end_done         = 1;
     874           0 :   if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
     875             :     /* Assumes that a NULL parent implies the snapshot slot. */
     876           0 :     sched->root_idx = bank_idx;
     877           0 :     block->rooted   = 1;
     878           0 :   }
     879           0 : }
     880             : 
     881             : void
     882           0 : fd_sched_advance_root( fd_sched_t * sched, ulong root_idx ) {
     883           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     884           0 :   FD_TEST( root_idx<sched->block_cnt_max );
     885           0 :   FD_TEST( sched->root_idx<sched->block_cnt_max );
     886             : 
     887           0 :   fd_sched_block_t * new_root = block_pool_ele( sched, root_idx );
     888           0 :   fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
     889           0 :   if( FD_UNLIKELY( !old_root->rooted ) ) {
     890           0 :     FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
     891           0 :                   old_root->slot, old_root->parent_slot ));
     892           0 :   }
     893             : 
     894             :   /* Early exit if the new root is the same as the old root. */
     895           0 :   if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
     896           0 :     FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
     897           0 :                   new_root->slot, new_root->parent_slot ));
     898           0 :     return;
     899           0 :   }
     900             : 
     901           0 :   fd_sched_block_t * head = old_root;
     902           0 :   head->parent_idx        = ULONG_MAX;
     903           0 :   fd_sched_block_t * tail = head;
     904             : 
     905           0 :   while( head ) {
     906           0 :     FD_TEST( head->in_sched );
     907           0 :     head->in_sched = 0;
     908           0 :     ulong child_idx = head->child_idx;
     909           0 :     while( child_idx!=ULONG_MAX ) {
     910           0 :       fd_sched_block_t * child = block_pool_ele( sched, child_idx );
     911             :       /* Add children to be visited.  We abuse the parent_idx field to
     912             :          link up the next block to visit. */
     913           0 :       if( child!=new_root ) {
     914           0 :         tail->parent_idx = child_idx;
     915           0 :         tail             = child;
     916           0 :         tail->parent_idx = ULONG_MAX;
     917           0 :       }
     918           0 :       child_idx = child->sibling_idx;
     919           0 :     }
     920             : 
     921             :     /* Prune the current block.  We will never publish halfway into a
     922             :        staging lane, because anything on the rooted fork should have
     923             :        finished replaying gracefully and be out of the dispatcher.  In
     924             :        fact, anything that we are publishing away should be out of the
     925             :        dispatcher at this point.  And there should be no more in-flight
     926             :        transactions. */
     927           0 :     if( FD_UNLIKELY( block_is_in_flight( head ) ) ) {
     928           0 :       FD_LOG_CRIT(( "invariant violation: block has transactions in flight (%u exec %u sigverify), slot %lu, parent slot %lu",
     929           0 :                     head->txn_exec_in_flight_cnt, head->txn_sigverify_in_flight_cnt, head->slot, head->parent_slot ));
     930           0 :     }
     931           0 :     if( FD_UNLIKELY( head->in_rdisp ) ) {
     932             :       /* We should have removed it from the dispatcher when we were
     933             :          notified of the new root, or when in-flight transactions were
     934             :          drained. */
     935           0 :       FD_LOG_CRIT(( "invariant violation: block is in the dispatcher, slot %lu, parent slot %lu",
     936           0 :                     head->slot, head->parent_slot ));
     937           0 :     }
     938           0 :     fd_sched_block_t * next = block_pool_ele( sched, head->parent_idx );
     939           0 :     head = next;
     940           0 :   }
     941             : 
     942           0 :   new_root->parent_idx = ULONG_MAX;
     943           0 :   sched->root_idx = root_idx;
     944           0 : }
     945             : 
     946             : void
     947           0 : fd_sched_root_notify( fd_sched_t * sched, ulong root_idx ) {
     948           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
     949           0 :   FD_TEST( root_idx<sched->block_cnt_max );
     950           0 :   FD_TEST( sched->root_idx<sched->block_cnt_max );
     951             : 
     952           0 :   fd_sched_block_t * block    = block_pool_ele( sched, root_idx );
     953           0 :   fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
     954           0 :   if( FD_UNLIKELY( !old_root->rooted ) ) {
     955           0 :     FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
     956           0 :                   old_root->slot, old_root->parent_slot ));
     957           0 :   }
     958             : 
     959             :   /* Early exit if the new root is the same as the old root. */
     960           0 :   if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
     961           0 :     FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
     962           0 :                   block->slot, block->parent_slot ));
     963           0 :     return;
     964           0 :   }
     965             : 
     966             :   /* Mark every node from the new root up through its parents to the
     967             :      old root as being rooted. */
     968           0 :   fd_sched_block_t * curr = block;
     969           0 :   fd_sched_block_t * prev = NULL;
     970           0 :   while( curr ) {
     971           0 :     if( FD_UNLIKELY( !block_is_done( curr ) ) ) {
     972           0 :       FD_LOG_CRIT(( "invariant violation: rooting a block that is not done, slot %lu, parent slot %lu",
     973           0 :                     curr->slot, curr->parent_slot ));
     974           0 :     }
     975           0 :     if( FD_UNLIKELY( curr->dying ) ) {
     976           0 :       FD_LOG_CRIT(( "invariant violation: rooting a block that is dying, slot %lu, parent slot %lu",
     977           0 :                     curr->slot, curr->parent_slot ));
     978           0 :     }
     979           0 :     if( FD_UNLIKELY( curr->staged ) ) {
     980           0 :       FD_LOG_CRIT(( "invariant violation: rooting a block that is staged, slot %lu, parent slot %lu",
     981           0 :                     curr->slot, curr->parent_slot ));
     982           0 :     }
     983           0 :     if( FD_UNLIKELY( curr->in_rdisp ) ) {
     984           0 :       FD_LOG_CRIT(( "invariant violation: rooting a block that is in the dispatcher, slot %lu, parent slot %lu",
     985           0 :                     curr->slot, curr->parent_slot ));
     986           0 :     }
     987           0 :     curr->rooted = 1;
     988           0 :     prev = curr;
     989           0 :     curr = block_pool_ele( sched, curr->parent_idx );
     990           0 :   }
     991             : 
     992             :   /* If we didn't reach the old root, the new root is not a descendant. */
     993           0 :   if( FD_UNLIKELY( prev!=old_root ) ) {
     994           0 :     FD_LOG_CRIT(( "invariant violation: new root is not a descendant of old root, new root slot %lu, parent slot %lu, old root slot %lu, parent slot %lu",
     995           0 :                   block->slot, block->parent_slot, old_root->slot, old_root->parent_slot ));
     996           0 :   }
     997             : 
     998           0 :   ulong old_active_bank_idx = sched->active_bank_idx;
     999             : 
    1000             :   /* Now traverse from old root towards new root, and abandon all
    1001             :      minority forks. */
    1002           0 :   curr = old_root;
    1003           0 :   while( curr && curr->rooted && curr!=block ) { /* curr!=block to avoid abandoning good forks. */
    1004           0 :     fd_sched_block_t * rooted_child_block = NULL;
    1005           0 :     ulong              child_idx          = curr->child_idx;
    1006           0 :     while( child_idx!=ULONG_MAX ) {
    1007           0 :       fd_sched_block_t * child = block_pool_ele( sched, child_idx );
    1008           0 :       if( child->rooted ) {
    1009           0 :         rooted_child_block = child;
    1010           0 :       } else {
    1011             :         /* This is a minority fork. */
    1012           0 :         subtree_abandon( sched, child );
    1013           0 :       }
    1014           0 :       child_idx = child->sibling_idx;
    1015           0 :     }
    1016           0 :     curr = rooted_child_block;
    1017           0 :   }
    1018             : 
    1019             :   /* If the active block got abandoned, we need to reset it. */
    1020           0 :   if( sched->active_bank_idx==ULONG_MAX ) {
    1021           0 :     sched->metrics->deactivate_pruned_cnt += fd_uint_if( old_active_bank_idx!=ULONG_MAX, 1U, 0U );
    1022           0 :     try_activate_block( sched );
    1023           0 :   }
    1024           0 : }
    1025             : 
    1026             : fd_txn_p_t *
    1027           0 : fd_sched_get_txn( fd_sched_t * sched, ulong txn_idx ) {
    1028           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
    1029           0 :   if( FD_UNLIKELY( txn_idx>=FD_SCHED_MAX_DEPTH ) ) {
    1030           0 :     return NULL;
    1031           0 :   }
    1032           0 :   return sched->txn_pool+txn_idx;
    1033           0 : }
    1034             : 
    1035             : fd_hash_t *
    1036           0 : fd_sched_get_poh( fd_sched_t * sched, ulong bank_idx ) {
    1037           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
    1038           0 :   FD_TEST( bank_idx<sched->block_cnt_max );
    1039           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1040           0 :   return &block->poh;
    1041           0 : }
    1042             : 
    1043             : uint
    1044           0 : fd_sched_get_shred_cnt( fd_sched_t * sched, ulong bank_idx ) {
    1045           0 :   FD_TEST( sched->canary==FD_SCHED_MAGIC );
    1046           0 :   FD_TEST( bank_idx<sched->block_cnt_max );
    1047           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1048           0 :   return block->shred_cnt;
    1049           0 : }
    1050             : 
    1051           0 : void * fd_sched_leave ( fd_sched_t * sched ) { return sched; }
    1052           0 : void * fd_sched_delete( void * mem         ) { return   mem; }
    1053             : 
    1054             : 
    1055             : /* Internal helpers. */
    1056             : 
    1057             : static void
    1058             : add_block( fd_sched_t * sched,
    1059             :            ulong        bank_idx,
    1060           0 :            ulong        parent_bank_idx ) {
    1061           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1062           0 :   FD_TEST( !block->in_sched );
    1063             : 
    1064           0 :   block->txn_parsed_cnt              = 0U;
    1065           0 :   block->txn_exec_in_flight_cnt      = 0U;
    1066           0 :   block->txn_exec_done_cnt           = 0U;
    1067           0 :   block->txn_sigverify_in_flight_cnt = 0U;
    1068           0 :   block->txn_sigverify_done_cnt      = 0U;
    1069           0 :   block->txn_done_cnt                = 0U;
    1070           0 :   block->shred_cnt                   = 0U;
    1071             : 
    1072           0 :   block->mblks_rem    = 0UL;
    1073           0 :   block->txns_rem     = 0UL;
    1074           0 :   block->fec_buf_sz   = 0U;
    1075           0 :   block->fec_buf_soff = 0U;
    1076           0 :   block->fec_eob      = 0;
    1077           0 :   block->fec_sob      = 1;
    1078             : 
    1079           0 :   block->fec_eos              = 0;
    1080           0 :   block->rooted               = 0;
    1081           0 :   block->dying                = 0;
    1082           0 :   block->in_sched             = 1;
    1083           0 :   block->in_rdisp             = 0;
    1084           0 :   block->block_start_signaled = 0;
    1085           0 :   block->block_end_signaled   = 0;
    1086           0 :   block->block_start_done     = 0;
    1087           0 :   block->block_end_done       = 0;
    1088           0 :   block->staged               = 0;
    1089             : 
    1090           0 :   block->luf_depth = 0UL;
    1091             : 
    1092             :   /* New leaf node, no child, no sibling. */
    1093           0 :   block->child_idx   = ULONG_MAX;
    1094           0 :   block->sibling_idx = ULONG_MAX;
    1095           0 :   block->parent_idx  = ULONG_MAX;
    1096             : 
    1097           0 :   if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
    1098           0 :     return;
    1099           0 :   }
    1100             : 
    1101             :   /* node->parent link */
    1102           0 :   fd_sched_block_t * parent_block = block_pool_ele( sched, parent_bank_idx );
    1103           0 :   block->parent_idx = parent_bank_idx;
    1104             : 
    1105             :   /* parent->node and sibling->node links */
    1106           0 :   ulong child_idx = bank_idx;
    1107           0 :   if( FD_LIKELY( parent_block->child_idx==ULONG_MAX ) ) { /* Optimize for no forking. */
    1108           0 :     parent_block->child_idx = child_idx;
    1109           0 :   } else {
    1110           0 :     fd_sched_block_t * curr_block = block_pool_ele( sched, parent_block->child_idx );
    1111           0 :     while( curr_block->sibling_idx!=ULONG_MAX ) {
    1112           0 :       curr_block = block_pool_ele( sched, curr_block->sibling_idx );
    1113           0 :     }
    1114           0 :     curr_block->sibling_idx = child_idx;
    1115           0 :   }
    1116             : 
    1117           0 :   if( FD_UNLIKELY( parent_block->dying ) ) {
    1118           0 :     block->dying = 1;
    1119           0 :   }
    1120           0 : }
    1121             : 
    1122           0 : #define CHECK( cond )  do {             \
    1123           0 :   if( FD_UNLIKELY( !(cond) ) ) {        \
    1124           0 :     return FD_SCHED_PARSER_AGAIN_LATER; \
    1125           0 :   }                                     \
    1126           0 : } while( 0 )
    1127             : 
    1128             : /* CHECK that it is safe to read at least n more bytes. */
    1129           0 : #define CHECK_LEFT( n ) CHECK( (n)<=(block->fec_buf_sz-block->fec_buf_soff) )
    1130             : 
    1131             : /* Consume as much as possible from the buffer.  By the end of this
    1132             :    function, we will either have residual data that is unparseable only
    1133             :    because it is a batch that straddles FEC set boundaries, or we will
    1134             :    have reached the end of a batch.  In the former case, any remaining
    1135             :    bytes should be concatenated with the next FEC set for further
    1136             :    parsing.  In the latter case, any remaining bytes should be thrown
    1137             :    away. */
    1138             : FD_WARN_UNUSED static int
    1139           0 : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
    1140           0 :   while( 1 ) {
    1141           0 :     while( block->txns_rem>0UL ) {
    1142           0 :       int err;
    1143           0 :       if( FD_UNLIKELY( (err=fd_sched_parse_txn( sched, block, alut_ctx ))!=FD_SCHED_PARSER_OK ) ) {
    1144           0 :         return err;
    1145           0 :       }
    1146           0 :     }
    1147           0 :     if( block->txns_rem==0UL && block->mblks_rem>0UL ) {
    1148           0 :       CHECK_LEFT( sizeof(fd_microblock_hdr_t) );
    1149           0 :       fd_microblock_hdr_t * hdr = (fd_microblock_hdr_t *)fd_type_pun( block->fec_buf+block->fec_buf_soff );
    1150           0 :       block->fec_buf_soff      += (uint)sizeof(fd_microblock_hdr_t);
    1151             : 
    1152           0 :       memcpy( block->poh.hash, hdr->hash, sizeof(block->poh.hash) );
    1153           0 :       block->txns_rem = hdr->txn_cnt;
    1154           0 :       block->mblks_rem--;
    1155           0 :       continue;
    1156           0 :     }
    1157           0 :     if( block->txns_rem==0UL && block->mblks_rem==0UL && block->fec_sob ) {
    1158           0 :       CHECK_LEFT( sizeof(ulong) );
    1159           0 :       FD_TEST( block->fec_buf_soff==0U );
    1160           0 :       block->mblks_rem     = FD_LOAD( ulong, block->fec_buf );
    1161           0 :       block->fec_buf_soff += (uint)sizeof(ulong);
    1162             :       /* FIXME what happens if someone sends us mblks_rem==0UL here? */
    1163             : 
    1164           0 :       block->fec_sob = 0;
    1165           0 :       continue;
    1166           0 :     }
    1167           0 :     if( block->txns_rem==0UL && block->mblks_rem==0UL ) {
    1168           0 :       break;
    1169           0 :     }
    1170           0 :   }
    1171           0 :   if( block->fec_eob ) {
    1172             :     /* Ignore trailing bytes at the end of a batch. */
    1173           0 :     sched->metrics->bytes_ingested_unparsed_cnt += block->fec_buf_sz-block->fec_buf_soff;
    1174           0 :     block->fec_buf_soff = 0U;
    1175           0 :     block->fec_buf_sz   = 0U;
    1176           0 :     block->fec_sob      = 1;
    1177           0 :     block->fec_eob      = 0;
    1178           0 :   }
    1179           0 :   return FD_SCHED_PARSER_OK;
    1180           0 : }
    1181             : 
    1182             : FD_WARN_UNUSED static int
    1183           0 : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
    1184           0 :   fd_txn_t * txn = fd_type_pun( block->txn );
    1185             : 
    1186           0 :   ulong pay_sz = 0UL;
    1187           0 :   ulong txn_sz = fd_txn_parse_core( block->fec_buf+block->fec_buf_soff,
    1188           0 :                                     fd_ulong_min( FD_TXN_MTU, block->fec_buf_sz-block->fec_buf_soff ),
    1189           0 :                                     txn,
    1190           0 :                                     NULL,
    1191           0 :                                     &pay_sz );
    1192             : 
    1193           0 :   if( FD_UNLIKELY( !pay_sz || !txn_sz ) ) {
    1194             :     /* Can't parse out a full transaction. */
    1195           0 :     return FD_SCHED_PARSER_AGAIN_LATER;
    1196           0 :   }
    1197             : 
    1198           0 :   if( FD_UNLIKELY( block->txn_parsed_cnt>=FD_MAX_TXN_PER_SLOT ) ) {
    1199             :     /* The block contains more transactions than a valid block would.
    1200             :        Mark the block dead instead of keep processing it. */
    1201           0 :     return FD_SCHED_PARSER_BAD_BLOCK;
    1202           0 :   }
    1203             : 
    1204             :   /* Try to expand ALUTs. */
    1205           0 :   int has_aluts   = txn->transaction_version==FD_TXN_V0 && txn->addr_table_adtl_cnt>0;
    1206           0 :   int serializing = 0;
    1207           0 :   if( has_aluts ) {
    1208             :     /* FIXME: statically size out slot hashes decode footprint. */
    1209           0 :     FD_SPAD_FRAME_BEGIN( alut_ctx->runtime_spad ) {
    1210           0 :     fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_slot_hashes_read( alut_ctx->funk, alut_ctx->xid, alut_ctx->runtime_spad );
    1211           0 :     if( FD_LIKELY( slot_hashes_global ) ) {
    1212           0 :       fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar *)slot_hashes_global + slot_hashes_global->hashes_offset );
    1213           0 :       serializing = !!fd_runtime_load_txn_address_lookup_tables( txn, block->fec_buf+block->fec_buf_soff, alut_ctx->funk, alut_ctx->xid, alut_ctx->els, slot_hash, block->aluts );
    1214           0 :       sched->metrics->alut_success_cnt += (uint)!serializing;
    1215           0 :     } else {
    1216           0 :       serializing = 1;
    1217           0 :     }
    1218           0 :     } FD_SPAD_FRAME_END;
    1219           0 :   }
    1220             : 
    1221           0 :   ulong bank_idx = (ulong)(block-sched->block_pool);
    1222           0 :   ulong txn_idx   = fd_rdisp_add_txn( sched->rdisp, bank_idx, txn, block->fec_buf+block->fec_buf_soff, serializing ? NULL : block->aluts, serializing );
    1223           0 :   FD_TEST( txn_idx!=0UL );
    1224           0 :   sched->metrics->txn_parsed_cnt++;
    1225           0 :   sched->metrics->alut_serializing_cnt += (uint)serializing;
    1226           0 :   sched->txn_pool_free_cnt--;
    1227           0 :   fd_txn_p_t * txn_p = sched->txn_pool + txn_idx;
    1228           0 :   txn_p->payload_sz  = pay_sz;
    1229           0 :   fd_memcpy( txn_p->payload, block->fec_buf+block->fec_buf_soff, pay_sz );
    1230           0 :   fd_memcpy( TXN(txn_p),     txn,                                txn_sz );
    1231           0 :   sched->txn_to_bank_idx[ txn_idx ] = bank_idx;
    1232           0 :   txn_bitset_remove( sched->exec_done_set, txn_idx );
    1233           0 :   txn_bitset_remove( sched->sigverify_done_set, txn_idx );
    1234           0 :   block->txn_idx[ block->txn_parsed_cnt ] = txn_idx;
    1235           0 :   block->fec_buf_soff += (uint)pay_sz;
    1236           0 :   block->txn_parsed_cnt++;
    1237             : #if FD_SCHED_SKIP_SIGVERIFY
    1238             :   txn_bitset_insert( sched->sigverify_done_set, txn_idx );
    1239             :   block->txn_sigverify_done_cnt++;
    1240             : #endif
    1241           0 :   block->txns_rem--;
    1242           0 :   return FD_SCHED_PARSER_OK;
    1243           0 : }
    1244             : 
    1245             : #undef CHECK
    1246             : #undef CHECK_LEFT
    1247             : 
    1248             : static void
    1249           0 : try_activate_block( fd_sched_t * sched ) {
    1250             : 
    1251             :   /* See if there are any allocated staging lanes that we can activate
    1252             :      for scheduling ... */
    1253           0 :   ulong staged_bitset = sched->staged_bitset;
    1254           0 :   while( staged_bitset ) {
    1255           0 :     int lane_idx  = fd_ulong_find_lsb( staged_bitset );
    1256           0 :     staged_bitset = fd_ulong_pop_lsb( staged_bitset );
    1257             : 
    1258           0 :     ulong              head_idx     = sched->staged_head_bank_idx[ lane_idx ];
    1259           0 :     fd_sched_block_t * head_block   = block_pool_ele( sched, head_idx );
    1260           0 :     fd_sched_block_t * parent_block = block_pool_ele( sched, head_block->parent_idx );
    1261           0 :     if( FD_UNLIKELY( parent_block->dying ) ) {
    1262             :       /* Invariant: no child of a dying block should be staged. */
    1263           0 :       FD_LOG_CRIT(( "invariant violation: staged_head_bank_idx %lu, slot %lu, parent slot %lu on lane %d has parent_block->dying set, slot %lu, parent slot %lu",
    1264           0 :                     head_idx, head_block->slot, head_block->parent_slot, lane_idx, parent_block->slot, parent_block->parent_slot ));
    1265           0 :     }
    1266             :     //FIXME: restore this invariant check when we have immediate demotion of dying blocks
    1267             :     // if( FD_UNLIKELY( head_block->dying ) ) {
    1268             :     //   /* Invariant: no dying block should be staged. */
    1269             :     //   FD_LOG_CRIT(( "invariant violation: staged_head_bank_idx %lu, slot %lu, prime %lu on lane %u has head_block->dying set",
    1270             :     //                 head_idx, (ulong)head_block->block_id.slot, (ulong)head_block->block_id.prime, lane_idx ));
    1271             :     // }
    1272           0 :     if( block_is_done( parent_block ) && block_is_activatable( head_block ) ) {
    1273             :       /* ... Yes, on this staging lane the parent block is done.  So we
    1274             :          can switch to the staged child. */
    1275           0 :       sched->active_bank_idx = head_idx;
    1276           0 :       sched->metrics->lane_switch_cnt++;
    1277           0 :       return;
    1278           0 :     }
    1279           0 :   }
    1280             : 
    1281             :   /* ... No, promote unstaged blocks. */
    1282           0 :   ulong root_idx = sched->root_idx;
    1283           0 :   if( FD_UNLIKELY( root_idx==ULONG_MAX ) ) {
    1284           0 :     FD_LOG_CRIT(( "invariant violation: root_idx==ULONG_MAX indicating fd_sched is unintialized" ));
    1285           0 :   }
    1286             :   /* Find and stage the longest stageable unstaged fork.  This is a
    1287             :      policy decision. */
    1288           0 :   ulong depth = compute_longest_unstaged_fork( sched, root_idx );
    1289           0 :   if( FD_LIKELY( depth>0UL ) ) {
    1290           0 :     if( FD_UNLIKELY( sched->staged_bitset==fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) {
    1291             :       /* No more staging lanes available.  All of them are occupied by
    1292             :          slow squatters.  Demote one of them. */
    1293             :       //FIXME implement this
    1294           0 :       FD_LOG_CRIT(( "unimplemented" ));
    1295           0 :       sched->metrics->lane_demoted_cnt++;
    1296             :       // sched->metrics->block_demoted_cnt++; for every demoted block
    1297           0 :     }
    1298           0 :     FD_TEST( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) );
    1299           0 :     int lane_idx = fd_ulong_find_lsb( ~sched->staged_bitset );
    1300           0 :     if( FD_UNLIKELY( lane_idx>=(int)FD_SCHED_MAX_STAGING_LANES ) ) {
    1301           0 :       FD_LOG_CRIT(( "invariant violation: lane_idx %d, sched->staged_bitset %lx",
    1302           0 :                     lane_idx, sched->staged_bitset ));
    1303           0 :     }
    1304           0 :     ulong head_bank_idx = stage_longest_unstaged_fork( sched, root_idx, lane_idx );
    1305           0 :     if( FD_UNLIKELY( head_bank_idx==ULONG_MAX ) ) {
    1306             :       /* We found a promotable fork depth>0.  This should not happen. */
    1307           0 :       FD_LOG_CRIT(( "invariant violation: head_bank_idx==ULONG_MAX" ));
    1308           0 :     }
    1309             :     /* We don't bother with promotion unless the block is immediately
    1310             :        dispatchable.  So it's okay to set the active block here. */
    1311           0 :     sched->active_bank_idx = head_bank_idx;
    1312           0 :     return;
    1313           0 :   }
    1314             :   /* No unstaged blocks to promote.  So we're done.  Yay. */
    1315           0 : }
    1316             : 
    1317             : static void
    1318           0 : check_or_set_active_block( fd_sched_t * sched ) {
    1319           0 :   if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
    1320           0 :     try_activate_block( sched );
    1321           0 :   } else {
    1322           0 :     fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
    1323           0 :     if( FD_UNLIKELY( block_should_deactivate( active_block ) ) ) {
    1324           0 :       print_all( sched, active_block );
    1325           0 :       FD_LOG_CRIT(( "invariant violation: should have been deactivated" ));
    1326           0 :     }
    1327           0 :   }
    1328           0 : }
    1329             : 
    1330             : /* It's safe to call this function more than once on the same block. */
    1331             : static void
    1332           0 : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block ) {
    1333           0 :   if( FD_UNLIKELY( block->rooted ) ) {
    1334           0 :     FD_LOG_CRIT(( "invariant violation: rooted block should not be abandoned, slot %lu, parent slot %lu",
    1335           0 :                   block->slot, block->parent_slot ));
    1336           0 :   }
    1337             :   /* All minority fork nodes pass through this function eventually.  So
    1338             :      this is a good point to check per-node invariants for minority
    1339             :      forks. */
    1340           0 :   if( FD_UNLIKELY( block->staged && !block->in_rdisp ) ) {
    1341           0 :     FD_LOG_CRIT(( "invariant violation: staged block is not in the dispatcher, slot %lu, parent slot %lu",
    1342           0 :                   block->slot, block->parent_slot ));
    1343           0 :   }
    1344             : 
    1345             :   /* Setting the flag is non-optional and can happen more than once. */
    1346           0 :   block->dying = 1;
    1347             : 
    1348             :   /* Removal from dispatcher should only happen once. */
    1349           0 :   if( block->in_rdisp ) {
    1350           0 :     fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
    1351           0 :     if( FD_UNLIKELY( !parent ) ) {
    1352             :       /* Only the root has no parent.  Abandon should never be called on
    1353             :          the root.  So any block we are trying to abandon should have a
    1354             :          parent. */
    1355           0 :       FD_LOG_CRIT(( "invariant violation: parent not found slot %lu, parent slot %lu",
    1356           0 :                     block->slot, block->parent_slot ));
    1357           0 :     }
    1358             : 
    1359             :     /* The dispatcher expects blocks to be abandoned in the same order
    1360             :        that they were added on each lane.  There are no requirements on
    1361             :        the order of abandoning if two blocks are not on the same lane,
    1362             :        or if a block is unstaged.  This means that in general we
    1363             :        shouldn't abandon a child block if the parent hasn't been
    1364             :        abandoned yet, if and only if they are on the same lane.  So wait
    1365             :        until we can abandon the parent, and then descend down the fork
    1366             :        tree to ensure orderly abandoning. */
    1367           0 :     int in_order = !parent->in_rdisp || /* parent is not in the dispatcher */
    1368           0 :                    !parent->staged   || /* parent is in the dispatcher but not staged */
    1369           0 :                    !block->staged    || /* parent is in the dispatcher and staged but this block is unstaged */
    1370           0 :                    block->staging_lane!=parent->staging_lane; /* this block is on a different staging lane than its parent */
    1371             : 
    1372             :     /* We inform the dispatcher of an abandon only when there are no
    1373             :        more in-flight transactions.  Otherwise, if the dispatcher
    1374             :        recycles the same txn_id that was just abandoned, and we receive
    1375             :        completion of an in-flight transaction whose txn_id was just
    1376             :        recycled, we would basically be aliasing the same txn_id and end
    1377             :        up indexing into txn_to_bank_idx[] that is already overwritten
    1378             :        with new blocks. */
    1379           0 :     int abandon = in_order && block->txn_exec_in_flight_cnt==0 && block->txn_sigverify_in_flight_cnt==0;
    1380             : 
    1381           0 :     if( abandon ) {
    1382           0 :       block->in_rdisp = 0;
    1383           0 :       fd_rdisp_abandon_block( sched->rdisp, (ulong)(block-sched->block_pool) );
    1384           0 :       sched->txn_pool_free_cnt += block->txn_parsed_cnt-block->txn_done_cnt; /* in_flight_cnt==0 */
    1385           0 :       sched->metrics->block_abandoned_cnt++;
    1386           0 :       sched->metrics->txn_abandoned_parsed_cnt    += block->txn_parsed_cnt;
    1387           0 :       sched->metrics->txn_abandoned_exec_done_cnt += block->txn_exec_done_cnt;
    1388           0 :       sched->metrics->txn_abandoned_done_cnt      += block->txn_done_cnt;
    1389             : 
    1390             :       /* Now release the staging lane. */
    1391             :       //FIXME when demote supports non-empty blocks, we should demote
    1392             :       //the block from the lane unconditionally and immediately,
    1393             :       //regardles of whether it's safe to abandon or not.  So a block
    1394             :       //would go immediately from staged to unstaged and eventually to
    1395             :       //abandoned.
    1396           0 :       if( FD_LIKELY( block->staged ) ) {
    1397           0 :         block->staged = 0;
    1398           0 :         sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
    1399           0 :         sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
    1400           0 :       }
    1401           0 :     }
    1402             : 
    1403           0 :     if( FD_UNLIKELY( in_order && block->staged && sched->active_bank_idx==sched->staged_head_bank_idx[ block->staging_lane ] ) ) {
    1404           0 :       FD_TEST( block_pool_ele( sched, sched->active_bank_idx )==block );
    1405             :       /* Dying blocks should not be active. */
    1406           0 :       sched->active_bank_idx = ULONG_MAX;
    1407           0 :     }
    1408           0 :   }
    1409             : 
    1410             :   /* Abandon the entire fork chaining off of this block. */
    1411           0 :   ulong child_idx = block->child_idx;
    1412           0 :   while( child_idx!=ULONG_MAX ) {
    1413           0 :     fd_sched_block_t * child = block_pool_ele( sched, child_idx );
    1414           0 :     subtree_abandon( sched, child );
    1415           0 :     child_idx = child->sibling_idx;
    1416           0 :   }
    1417           0 : }
    1418             : 
    1419             : static void
    1420           0 : maybe_switch_block( fd_sched_t * sched, ulong bank_idx ) {
    1421           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1422           0 :   if( FD_UNLIKELY( block_is_done( block ) ) ) {
    1423           0 :     block->in_rdisp = 0;
    1424           0 :     block->staged   = 0;
    1425           0 :     fd_rdisp_remove_block( sched->rdisp, bank_idx );
    1426           0 :     sched->metrics->block_removed_cnt++;
    1427             : 
    1428             :     /* See if there is a child block down the same staging lane.  This
    1429             :        is a policy decision to minimize fork churn.  We could in theory
    1430             :        reevaluate staging lane allocation here and do promotion/demotion
    1431             :        as needed. */
    1432           0 :     ulong child_idx = block->child_idx;
    1433           0 :     while( child_idx!=ULONG_MAX ) {
    1434           0 :       fd_sched_block_t * child = block_pool_ele( sched, child_idx );
    1435           0 :       if( FD_LIKELY( child->staged && child->staging_lane==block->staging_lane ) ) {
    1436             :         /* There is a child block down the same staging lane ... */
    1437           0 :         if( FD_LIKELY( !child->dying ) ) {
    1438             :           /* ... and the child isn't dead */
    1439           0 :           if( FD_UNLIKELY( !block_is_activatable( child ) ) ) {
    1440             :             /* ... but the child is not activatable, likely because
    1441             :                there are no transactions available yet. */
    1442           0 :             sched->active_bank_idx = ULONG_MAX;
    1443           0 :             sched->metrics->deactivate_no_txn_cnt++;
    1444           0 :             try_activate_block( sched );
    1445           0 :             return;
    1446           0 :           }
    1447             :           /* ... and it's immediately dispatchable, so switch the active
    1448             :              block to it, and have the child inherit the head status of
    1449             :              the lane.  This is the common case. */
    1450           0 :           sched->active_bank_idx = child_idx;
    1451           0 :           sched->staged_head_bank_idx[ block->staging_lane ] = child_idx;
    1452           0 :           if( FD_UNLIKELY( !fd_ulong_extract_bit( sched->staged_bitset, (int)block->staging_lane ) ) ) {
    1453           0 :             FD_LOG_CRIT(( "invariant violation: staged_bitset 0x%lx bit %lu is not set, slot %lu, parent slot %lu, child slot %lu, parent slot %lu",
    1454           0 :                           sched->staged_bitset, block->staging_lane, block->slot, block->parent_slot, child->slot, child->parent_slot ));
    1455           0 :           }
    1456           0 :           return;
    1457           0 :         } else {
    1458             :           /* ... but the child block is considered dead, likely because
    1459             :              the parser considers it invalid. */
    1460           0 :           subtree_abandon( sched, child );
    1461           0 :           break;
    1462           0 :         }
    1463           0 :       }
    1464           0 :       child_idx = child->sibling_idx;
    1465           0 :     }
    1466             :     /* There isn't a child block down the same staging lane.  This is
    1467             :        the last block in the staging lane.  Release the staging lane. */
    1468           0 :     sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
    1469           0 :     sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
    1470             : 
    1471             :     /* Reset the active block. */
    1472           0 :     sched->active_bank_idx = ULONG_MAX;
    1473           0 :     sched->metrics->deactivate_no_child_cnt++;
    1474           0 :     try_activate_block( sched );
    1475           0 :   } else if( block_should_deactivate( block ) ) {
    1476             :     /* We exhausted the active block, but it's not fully done yet.  We
    1477             :        are just not getting FEC sets for it fast enough.  This could
    1478             :        happen when the network path is congested, or when the leader
    1479             :        simply went down.  Reset the active block. */
    1480           0 :     sched->active_bank_idx = ULONG_MAX;
    1481           0 :     sched->metrics->deactivate_no_txn_cnt++;
    1482           0 :     try_activate_block( sched );
    1483           0 :   }
    1484           0 : }
    1485             : 
    1486             : FD_FN_UNUSED static ulong
    1487           0 : find_and_stage_longest_unstaged_fork( fd_sched_t * sched, int lane_idx ) {
    1488           0 :   ulong root_idx = sched->root_idx;
    1489           0 : 
    1490           0 :   if( FD_UNLIKELY( root_idx==ULONG_MAX ) ) {
    1491           0 :     FD_LOG_CRIT(( "invariant violation: root_idx==ULONG_MAX indicating fd_sched is unintialized" ));
    1492           0 :   }
    1493           0 : 
    1494           0 :   /* First pass: compute the longest unstaged fork depth for each node
    1495           0 :      in the fork tree. */
    1496           0 :   ulong depth = compute_longest_unstaged_fork( sched, root_idx );
    1497           0 : 
    1498           0 :   /* Second pass: stage blocks on the longest unstaged fork. */
    1499           0 :   ulong head_bank_idx = stage_longest_unstaged_fork( sched, root_idx, lane_idx );
    1500           0 : 
    1501           0 :   if( FD_UNLIKELY( (depth>0UL && head_bank_idx==ULONG_MAX) || (depth==0UL && head_bank_idx!=ULONG_MAX) ) ) {
    1502           0 :     FD_LOG_CRIT(( "invariant violation: depth %lu, head_bank_idx %lu",
    1503           0 :                   depth, head_bank_idx ));
    1504           0 :   }
    1505           0 : 
    1506           0 :   return head_bank_idx;
    1507           0 : }
    1508             : 
    1509             : /* Returns length of the longest stageable unstaged fork, if there is
    1510             :    one, and 0 otherwise. */
    1511             : static ulong
    1512           0 : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx ) {
    1513           0 :   if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) {
    1514           0 :     FD_LOG_CRIT(( "invariant violation: bank_idx==ULONG_MAX" ));
    1515           0 :   }
    1516             : 
    1517           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1518             : 
    1519           0 :   ulong max_child_depth = 0UL;
    1520           0 :   ulong child_idx       = block->child_idx;
    1521           0 :   while( child_idx!=ULONG_MAX ) {
    1522           0 :     ulong child_depth = compute_longest_unstaged_fork( sched, child_idx );
    1523           0 :     if( child_depth > max_child_depth ) {
    1524           0 :       max_child_depth = child_depth;
    1525           0 :     }
    1526           0 :     fd_sched_block_t * child = block_pool_ele( sched, child_idx );
    1527           0 :     child_idx = child->sibling_idx;
    1528           0 :   }
    1529             : 
    1530           0 :   block->luf_depth = max_child_depth + fd_ulong_if( block_is_promotable( block ), 1UL, 0UL );
    1531           0 :   return block->luf_depth;
    1532           0 : }
    1533             : 
    1534             : static ulong
    1535           0 : stage_longest_unstaged_fork_helper( fd_sched_t * sched, ulong bank_idx, int lane_idx ) {
    1536           0 :   if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) {
    1537           0 :     FD_LOG_CRIT(( "invariant violation: bank_idx==ULONG_MAX" ));
    1538           0 :   }
    1539             : 
    1540           0 :   fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
    1541             : 
    1542           0 :   int   stage_it = fd_int_if( block_is_promotable( block ), 1, 0 );
    1543           0 :   ulong rv       = fd_ulong_if( stage_it, bank_idx, ULONG_MAX );
    1544           0 :   if( FD_LIKELY( stage_it ) ) {
    1545           0 :     block->staged = 1;
    1546           0 :     block->staging_lane = (ulong)lane_idx;
    1547           0 :     fd_rdisp_promote_block( sched->rdisp, bank_idx, block->staging_lane );
    1548           0 :     sched->metrics->block_promoted_cnt++;
    1549           0 :   }
    1550             : 
    1551             :   /* Base case: leaf node. */
    1552           0 :   if( block->child_idx==ULONG_MAX ) return rv;
    1553             : 
    1554           0 :   ulong max_depth      = 0UL;
    1555           0 :   ulong best_child_idx = ULONG_MAX;
    1556           0 :   ulong child_idx      = block->child_idx;
    1557           0 :   while( child_idx!=ULONG_MAX ) {
    1558           0 :     fd_sched_block_t * child = block_pool_ele( sched, child_idx );
    1559           0 :     if( child->luf_depth>max_depth ) {
    1560           0 :       max_depth      = child->luf_depth;
    1561           0 :       best_child_idx = child_idx;
    1562           0 :     }
    1563           0 :     child_idx = child->sibling_idx;
    1564           0 :   }
    1565             : 
    1566             :   /* Recursively stage descendants. */
    1567           0 :   if( best_child_idx!=ULONG_MAX ) {
    1568           0 :     ulong head_bank_idx = stage_longest_unstaged_fork_helper( sched, best_child_idx, lane_idx );
    1569           0 :     rv = fd_ulong_if( rv!=ULONG_MAX, rv, head_bank_idx );
    1570           0 :   }
    1571             : 
    1572           0 :   return rv;
    1573           0 : }
    1574             : 
    1575             : /* Returns idx of head block of staged lane on success, idx_null
    1576             :    otherwise. */
    1577             : static ulong
    1578           0 : stage_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx ) {
    1579           0 :   ulong head_bank_idx = stage_longest_unstaged_fork_helper( sched, bank_idx, lane_idx );
    1580           0 :   if( FD_LIKELY( head_bank_idx!=ULONG_MAX ) ) {
    1581           0 :     sched->metrics->lane_promoted_cnt++;
    1582           0 :     sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
    1583           0 :     sched->staged_head_bank_idx[ lane_idx ] = head_bank_idx;
    1584           0 :   }
    1585           0 :   return head_bank_idx;
    1586           0 : }

Generated by: LCOV version 1.14