LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_pack.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 21 375 5.6 %
Date: 2024-11-13 11:58:15 Functions: 2 12 16.7 %

          Line data    Source code
       1             : #include "../../../../disco/tiles.h"
       2             : 
       3             : #include "generated/pack_seccomp.h"
       4             : 
       5             : #include "../../../../disco/topo/fd_pod_format.h"
       6             : #include "../../../../disco/shred/fd_shredder.h"
       7             : #include "../../../../disco/metrics/fd_metrics.h"
       8             : #include "../../../../ballet/pack/fd_pack.h"
       9             : 
      10             : #include <linux/unistd.h>
      11             : 
      12             : /* fd_pack is responsible for taking verified transactions, and
      13             :    arranging them into "microblocks" (groups) of transactions to
      14             :    be executed serially.  It can try to do clever things so that
      15             :    multiple microblocks can execute in parallel, if they don't
      16             :    write to the same accounts. */
      17             : 
      18           0 : #define IN_KIND_RESOLV (0UL)
      19           0 : #define IN_KIND_POH    (1UL)
      20           0 : #define IN_KIND_BANK   (2UL)
      21           0 : #define IN_KIND_BUNDLE (3UL)
      22             : 
      23             : #define MAX_SLOTS_PER_EPOCH          432000UL
      24             : 
      25             : /* Pace microblocks, but only slightly.  This helps keep performance
      26             :    more stable.  This limit is 2,000 microblocks/second/bank.  At 31
      27             :    transactions/microblock, that's 62k txn/sec/bank. */
      28           0 : #define MICROBLOCK_DURATION_NS  (0L)
      29             : 
      30             : /* There are 151 accepted blockhashes, but those don't include skips.
      31             :    This check is neither precise nor accurate, but just good enough.
      32             :    The bank tile does the final check.  We give a little margin for a
      33             :    few percent skip rate. */
      34           0 : #define TRANSACTION_LIFETIME_SLOTS 160UL
      35             : 
      36             : /* About 6 kB on the stack */
      37             : #define FD_PACK_PACK_MAX_OUT FD_PACK_MAX_BANK_TILES
      38             : 
      39             : /* Time is normally a long, but pack expects a ulong.  Add -LONG_MIN to
      40             :    the time values so that LONG_MIN maps to 0, LONG_MAX maps to
      41             :    ULONG_MAX, and everything in between maps linearly with a slope of 1.
      42             :    Just subtracting LONG_MIN results in signed integer overflow, which
      43             :    is U.B. */
      44             : #define TIME_OFFSET 0x8000000000000000UL
      45             : FD_STATIC_ASSERT( (ulong)LONG_MIN+TIME_OFFSET==0UL,       time_offset );
      46             : FD_STATIC_ASSERT( (ulong)LONG_MAX+TIME_OFFSET==ULONG_MAX, time_offset );
      47             : 
      48             : 
      49             : /* Optionally allow a larger limit for benchmarking */
      50           0 : #define LARGER_MAX_COST_PER_BLOCK (18UL*48000000UL)
      51             : 
      52             : /* 1.5 M cost units, enough for 1 max size transaction */
      53             : const ulong CUS_PER_MICROBLOCK = 1500000UL;
      54             : 
      55             : #define SMALL_MICROBLOCKS 1
      56             : 
      57             : #if SMALL_MICROBLOCKS
      58             : const float VOTE_FRACTION = 1.0f; /* schedule all available votes first */
      59           3 : #define EFFECTIVE_TXN_PER_MICROBLOCK 1UL
      60             : #else
      61             : const float VOTE_FRACTION = 0.75f; /* TODO: Is this the right value? */
      62             : #define EFFECTIVE_TXN_PER_MICROBLOCK MAX_TXN_PER_MICROBLOCK
      63             : #endif
      64             : 
      65             : /* There's overhead associated with each microblock the bank tile tries
      66             :    to execute it, so the optimal strategy is not to produce a microblock
      67             :    with a single transaction as soon as we receive it.  Basically, if we
      68             :    have less than 31 transactions, we want to wait a little to see if we
      69             :    receive additional transactions before we schedule a microblock.  We
      70             :    can model the optimum amount of time to wait, but the equation is
      71             :    complicated enough that we want to compute it before compile time.
      72             :    wait_duration[i] for i in [0, 31] gives the time in nanoseconds pack
      73             :    should wait after receiving its most recent transaction before
      74             :    scheduling if it has i transactions available.  Unsurprisingly,
      75             :    wait_duration[31] is 0.  wait_duration[0] is ULONG_MAX, so we'll
      76             :    always wait if we have 0 transactions. */
      77             : FD_IMPORT( wait_duration, "src/ballet/pack/pack_delay.bin", ulong, 6, "" );
      78             : 
      79             : 
      80             : 
      81             : #if FD_PACK_USE_EXTRA_STORAGE
      82             : /* When we are done being leader for a slot and we are leader in the
      83             :    very next slot, it can still take some time to transition.  This is
      84             :    because the bank has to be finalized, a hash calculated, and various
      85             :    other things done in the replay stage to create the new child bank.
      86             : 
      87             :    During that time, pack cannot send transactions to banks so it needs
      88             :    to be able to buffer.  Typically, these so called "leader
      89             :    transitions" are short (<15 millis), so a low value here would
      90             :    suffice.  However, in some cases when there is memory pressure on the
      91             :    NUMA node or when the operating system context switches relevant
      92             :    threads out, it can take significantly longer.
      93             : 
      94             :    To prevent drops in these cases and because we assume banks are fast
      95             :    enough to drain this buffer once we do become leader, we set this
      96             :    buffer size to be quite large. */
      97             : 
      98             : #define DEQUE_NAME extra_txn_deq
      99             : #define DEQUE_T    fd_txn_e_t
     100             : #define DEQUE_MAX  (128UL*1024UL)
     101             : #include "../../../../util/tmpl/fd_deque.c"
     102             : 
     103             : #endif
     104             : 
     105             : 
     106             : typedef struct {
     107             :   fd_wksp_t * mem;
     108             :   ulong       chunk0;
     109             :   ulong       wmark;
     110             : } fd_pack_in_ctx_t;
     111             : 
     112             : typedef struct {
     113             :   fd_pack_t *  pack;
     114             :   fd_txn_e_t * cur_spot;
     115             : 
     116             :   /* The value passed to fd_pack_new, etc. */
     117             :   ulong    max_pending_transactions;
     118             : 
     119             :   /* The leader slot we are currently packing for, or ULONG_MAX if we
     120             :      are not the leader. */
     121             :   ulong  leader_slot;
     122             :   void const * leader_bank;
     123             : 
     124             :   /* The number of microblocks we have packed for the current leader
     125             :      slot.  Will always be <= slot_max_microblocks.  We must track
     126             :      this so that when we are done we can tell the PoH tile how many
     127             :      microblocks to expect in the slot. */
     128             :   ulong slot_microblock_cnt;
     129             : 
     130             :   /* The maximum number of microblocks that can be packed in this slot.
     131             :      Provided by the PoH tile when we become leader.*/
     132             :   ulong slot_max_microblocks;
     133             : 
     134             :   /* Cap (in bytes) of the amount of transaction data we produce in each
     135             :      block to avoid hitting the shred limits.  See where this is set for
     136             :      more explanation. */
     137             :   ulong slot_max_data;
     138             :   int   larger_shred_limits_per_block;
     139             : 
     140             :   /* If drain_banks is non-zero, then the pack tile must wait until all
     141             :      banks are idle before scheduling any more microblocks.  This is
     142             :      primarily helpful in irregular leader transitions, e.g. while being
     143             :      leader for slot N, we switch forks to a slot M (!=N+1) in which we
     144             :      are also leader.  We don't want to execute microblocks for
     145             :      different slots concurrently. */
     146             :   int drain_banks;
     147             : 
     148             :   /* Updated during housekeeping and used only for checking if the
     149             :      leader slot has ended.  Might be off by one housekeeping duration,
     150             :      but that should be small relative to a slot duration. */
     151             :   long  approx_wallclock_ns;
     152             : 
     153             :   fd_rng_t * rng;
     154             : 
     155             :   /* The end wallclock time of the leader slot we are currently packing
     156             :      for, if we are currently packing for a slot.
     157             : 
     158             :      _slot_end_ns is used as a temporary between during_frag and
     159             :      after_frag in case the tile gets overrun. */
     160             :   long _slot_end_ns;
     161             :   long slot_end_ns;
     162             : 
     163             :   /* last_successful_insert stores the tickcount of the last
     164             :      successful transaction insert. */
     165             :   long last_successful_insert;
     166             : 
     167             :   /* highest_observed_slot stores the highest slot number we've seen
     168             :      from any transaction coming from the resolv tile.  When this
     169             :      increases, we expire old transactions. */
     170             :   ulong highest_observed_slot;
     171             : 
     172             :   /* microblock_duration_ns, and wait_duration
     173             :      respectively scaled to be in ticks instead of nanoseconds */
     174             :   ulong microblock_duration_ticks;
     175             :   ulong wait_duration_ticks[ MAX_TXN_PER_MICROBLOCK+1UL ];
     176             : 
     177             : #if FD_PACK_USE_EXTRA_STORAGE
     178             :   /* In addition to the available transactions that pack knows about, we
     179             :      also store a larger ring buffer for handling cases when pack is
     180             :      full.  This is an fd_deque. */
     181             :   fd_txn_e_t * extra_txn_deq;
     182             :   int          insert_to_extra; /* whether the last insert was into pack or the extra deq */
     183             : #endif
     184             : 
     185             :   fd_pack_in_ctx_t in[ 32 ];
     186             :   int              in_kind[ 32 ];
     187             : 
     188             :   ulong    bank_cnt;
     189             :   ulong    bank_idle_bitset; /* bit i is 1 if we've observed *bank_current[i]==bank_expect[i] */
     190             :   int      poll_cursor; /* in [0, bank_cnt), the next bank to poll */
     191             :   int      use_consumed_cus;
     192             :   long     skip_cnt;
     193             :   ulong *  bank_current[ FD_PACK_PACK_MAX_OUT ];
     194             :   ulong    bank_expect[ FD_PACK_PACK_MAX_OUT  ];
     195             :   /* bank_ready_at[x] means don't check bank x until tickcount is at
     196             :      least bank_ready_at[x]. */
     197             :   long     bank_ready_at[ FD_PACK_PACK_MAX_OUT  ];
     198             : 
     199             :   fd_wksp_t * out_mem;
     200             :   ulong       out_chunk0;
     201             :   ulong       out_wmark;
     202             :   ulong       out_chunk;
     203             : 
     204             :   ulong      insert_result[ FD_PACK_INSERT_RETVAL_CNT ];
     205             :   fd_histf_t schedule_duration[ 1 ];
     206             :   fd_histf_t insert_duration  [ 1 ];
     207             : 
     208             :   struct {
     209             :     uint metric_state;
     210             :     long metric_state_begin;
     211             :     long metric_timing[ 16 ];
     212             :   };
     213             : 
     214             :   /* Used between during_frag and after_frag */
     215             :   ulong pending_rebate_cnt;
     216             :   fd_txn_p_t pending_rebate[ MAX_TXN_PER_MICROBLOCK ]; /* indexed [0, pending_rebate_cnt) */
     217             : } fd_pack_ctx_t;
     218             : 
     219             : 
     220           0 : #define FD_PACK_METRIC_STATE_TRANSACTIONS 0
     221           0 : #define FD_PACK_METRIC_STATE_BANKS        1
     222           0 : #define FD_PACK_METRIC_STATE_LEADER       2
     223           0 : #define FD_PACK_METRIC_STATE_MICROBLOCKS  3
     224             : 
     225             : /* Updates one component of the metric state.  If the state has changed,
     226             :    records the change. */
     227             : static inline void
     228             : update_metric_state( fd_pack_ctx_t * ctx,
     229             :                      long            effective_as_of,
     230             :                      int             type,
     231           0 :                      int             status ) {
     232           0 :   uint current_state = fd_uint_insert_bit( ctx->metric_state, type, status );
     233           0 :   if( FD_UNLIKELY( current_state!=ctx->metric_state ) ) {
     234           0 :     ctx->metric_timing[ ctx->metric_state ] += effective_as_of - ctx->metric_state_begin;
     235           0 :     ctx->metric_state_begin = effective_as_of;
     236           0 :     ctx->metric_state = current_state;
     237           0 :   }
     238           0 : }
     239             : 
     240             : 
     241             : FD_FN_CONST static inline ulong
     242           3 : scratch_align( void ) {
     243           3 :   return 4096UL;
     244           3 : }
     245             : 
     246             : FD_FN_PURE static inline ulong
     247           3 : scratch_footprint( fd_topo_tile_t const * tile ) {
     248           3 :   fd_pack_limits_t limits[1] = {{
     249           3 :     .max_cost_per_block        = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK,
     250           3 :     .max_vote_cost_per_block   = FD_PACK_MAX_VOTE_COST_PER_BLOCK,
     251           3 :     .max_write_cost_per_acct   = FD_PACK_MAX_WRITE_COST_PER_ACCT,
     252           3 :     .max_data_bytes_per_block  = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
     253           3 :     .max_txn_per_microblock    = EFFECTIVE_TXN_PER_MICROBLOCK,
     254           3 :     .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
     255           3 :   }};
     256             : 
     257           3 :   ulong l = FD_LAYOUT_INIT;
     258           3 :   l = FD_LAYOUT_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t )                                   );
     259           3 :   l = FD_LAYOUT_APPEND( l, fd_rng_align(),           fd_rng_footprint()                                        );
     260           3 :   l = FD_LAYOUT_APPEND( l, fd_pack_align(),          fd_pack_footprint( tile->pack.max_pending_transactions,
     261           3 :                                                                         tile->pack.bank_tile_count,
     262           3 :                                                                         limits                               ) );
     263             : #if FD_PACK_USE_EXTRA_STORAGE
     264             :   l = FD_LAYOUT_APPEND( l, extra_txn_deq_align(),    extra_txn_deq_footprint()                                 );
     265             : #endif
     266           3 :   return FD_LAYOUT_FINI( l, scratch_align() );
     267           3 : }
     268             : 
     269             : static inline void
     270           0 : metrics_write( fd_pack_ctx_t * ctx ) {
     271           0 :   FD_MCNT_ENUM_COPY( PACK, TRANSACTION_INSERTED,          ctx->insert_result  );
     272           0 :   FD_MCNT_ENUM_COPY( PACK, METRIC_TIMING,        ((ulong*)ctx->metric_timing) );
     273           0 :   FD_MHIST_COPY( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS, ctx->schedule_duration );
     274           0 :   FD_MHIST_COPY( PACK, INSERT_TRANSACTION_DURATION_SECONDS,  ctx->insert_duration   );
     275             : 
     276           0 :   fd_pack_metrics_write( ctx->pack );
     277           0 : }
     278             : 
     279             : static inline void
     280           0 : during_housekeeping( fd_pack_ctx_t * ctx ) {
     281           0 :   ctx->approx_wallclock_ns = fd_log_wallclock();
     282           0 : }
     283             : 
     284             : static inline void
     285             : before_credit( fd_pack_ctx_t *     ctx,
     286             :                fd_stem_context_t * stem,
     287           0 :                int *               charge_busy ) {
     288           0 :   (void)stem;
     289             : 
     290           0 :   if( FD_UNLIKELY( ctx->cur_spot ) ) {
     291           0 :     *charge_busy = 1;
     292             : 
     293             :     /* If we were overrun while processing a frag from an in, then cur_spot
     294             :        is left dangling and not cleaned up, so clean it up here (by returning
     295             :        the slot to the pool of free slots). */
     296             : #if FD_PACK_USE_EXTRA_STORAGE
     297             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     298             :     else                                     extra_txn_deq_remove_tail( ctx->extra_txn_deq       );
     299             : #else
     300           0 :     fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
     301           0 : #endif
     302           0 :     ctx->cur_spot = NULL;
     303           0 :   }
     304           0 : }
     305             : 
     306             : #if FD_PACK_USE_EXTRA_STORAGE
     307             : /* insert_from_extra: helper method to pop the transaction at the head
     308             :    off the extra txn deque and insert it into pack.  Requires that
     309             :    ctx->extra_txn_deq is non-empty, but it's okay to call it if pack is
     310             :    full.  Returns the result of fd_pack_insert_txn_fini. */
     311             : static inline int
     312             : insert_from_extra( fd_pack_ctx_t * ctx ) {
     313             :   fd_txn_e_t       * spot       = fd_pack_insert_txn_init( ctx->pack );
     314             :   fd_txn_e_t const * insert     = extra_txn_deq_peek_head( ctx->extra_txn_deq );
     315             :   fd_txn_t   const * insert_txn = TXN(insert->txnp);
     316             :   fd_memcpy( spot->txnp->payload, insert->txnp->payload, insert->txnp->payload_sz                                                     );
     317             :   fd_memcpy( TXN(spot->txnp),     insert_txn,            fd_txn_footprint( insert_txn->instr_cnt, insert_txn->addr_table_lookup_cnt ) );
     318             :   fd_memcpy( spot->alt_accts,     insert->alt_accts,     insert_txn->addr_table_adtl_cnt*sizeof(fd_acct_addr_t)                       );
     319             :   spot->txnp->payload_sz = insert->txnp->payload_sz;
     320             :   extra_txn_deq_remove_head( ctx->extra_txn_deq );
     321             : 
     322             :   ulong blockhash_slot = insert->txnp->blockhash_slot;
     323             : 
     324             :   long insert_duration = -fd_tickcount();
     325             :   int result = fd_pack_insert_txn_fini( ctx->pack, spot, blockhash_slot );
     326             :   insert_duration      += fd_tickcount();
     327             :   ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
     328             :   fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     329             :   FD_MCNT_INC( PACK, TRANSACTION_INSERTED_FROM_EXTRA, 1UL );
     330             :   return result;
     331             : }
     332             : #endif
     333             : 
     334             : static inline void
     335             : after_credit( fd_pack_ctx_t *     ctx,
     336             :               fd_stem_context_t * stem,
     337             :               int *               opt_poll_in,
     338           0 :               int *               charge_busy ) {
     339           0 :   (void)opt_poll_in;
     340             : 
     341           0 :   if( FD_UNLIKELY( (ctx->skip_cnt--)>0L ) ) return; /* It would take ages for this to hit LONG_MIN */
     342             : 
     343           0 :   long now = fd_tickcount();
     344             : 
     345           0 :   ulong bank_cnt = ctx->bank_cnt;
     346             : 
     347             :   /* If we're using CU rebates, then we have one in for each bank in
     348             :      addition to the two normal ones.  That means that after_credit will
     349             :      be called about (bank_cnt/2) times more frequently per transaction
     350             :      we receive. */
     351           0 :   fd_long_store_if( ctx->use_consumed_cus, &(ctx->skip_cnt), (long)(bank_cnt/2UL) );
     352             : 
     353             :   /* If any banks are busy, check one of the busy ones see if it is
     354             :      still busy. */
     355           0 :   if( FD_LIKELY( ctx->bank_idle_bitset!=fd_ulong_mask_lsb( (int)bank_cnt ) ) ) {
     356           0 :     int   poll_cursor = ctx->poll_cursor;
     357           0 :     ulong busy_bitset = (~ctx->bank_idle_bitset) & fd_ulong_mask_lsb( (int)bank_cnt );
     358             : 
     359             :     /* Suppose bank_cnt is 4 and idle_bitset looks something like this
     360             :        (pretending it's a uchar):
     361             :                 0000 1001
     362             :                        ^ busy cursor is 1
     363             :        Then busy_bitset is
     364             :                 0000 0110
     365             :        Rotate it right by 2 bits
     366             :                 1000 0001
     367             :        Find lsb returns 0, so busy cursor remains 2, and we poll bank 2.
     368             : 
     369             :        If instead idle_bitset were
     370             :                 0000 1110
     371             :                        ^
     372             :        The rotated version would be
     373             :                 0100 0000
     374             :        Find lsb will return 6, so busy cursor would be set to 0, and
     375             :        we'd poll bank 0, which is the right one. */
     376           0 :     poll_cursor++;
     377           0 :     poll_cursor = (poll_cursor + fd_ulong_find_lsb( fd_ulong_rotate_right( busy_bitset, (poll_cursor&63) ) )) & 63;
     378             : 
     379           0 :     if( FD_UNLIKELY(
     380             :         /* if microblock duration is 0, bypass the bank_ready_at check
     381             :            to avoid a potential cache miss.  Can't use an ifdef here
     382             :            because FD_UNLIKELY is a macro, but the compiler should
     383             :            eliminate the check easily. */
     384           0 :         ( (MICROBLOCK_DURATION_NS==0L) || (ctx->bank_ready_at[poll_cursor]<now) ) &&
     385           0 :         (fd_fseq_query( ctx->bank_current[poll_cursor] )==ctx->bank_expect[poll_cursor]) ) ) {
     386           0 :       *charge_busy = 1;
     387           0 :       ctx->bank_idle_bitset |= 1UL<<poll_cursor;
     388           0 :     }
     389             : 
     390           0 :     ctx->poll_cursor = poll_cursor;
     391           0 :   }
     392             : 
     393             : 
     394             :   /* If we time out on our slot, then stop being leader.  This can only
     395             :      happen in the first after_credit after a housekeeping. */
     396           0 :   if( FD_UNLIKELY( ctx->approx_wallclock_ns>=ctx->slot_end_ns && ctx->leader_slot!=ULONG_MAX ) ) {
     397           0 :     *charge_busy = 1;
     398             : 
     399           0 :     if( FD_UNLIKELY( ctx->slot_microblock_cnt<ctx->slot_max_microblocks )) {
     400             :       /* As an optimization, The PoH tile will automatically end a slot
     401             :          if it receives the maximum allowed microblocks, since it knows
     402             :          there is nothing left to receive.  In that case, we don't need
     403             :          to send a DONE_PACKING notification, since they are already on
     404             :          the next slot.  If we did send one it would just get dropped. */
     405           0 :       fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
     406           0 :       done_packing->microblocks_in_slot = ctx->slot_microblock_cnt;
     407             : 
     408           0 :       fd_stem_publish( stem, 0UL, fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_DONE_PACKING, ULONG_MAX ), ctx->out_chunk, sizeof(fd_done_packing_t), 0UL, 0UL, 0UL );
     409           0 :       ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, sizeof(fd_done_packing_t), ctx->out_chunk0, ctx->out_wmark );
     410           0 :     }
     411             : 
     412           0 :     ctx->drain_banks         = 1;
     413           0 :     ctx->leader_slot         = ULONG_MAX;
     414           0 :     ctx->slot_microblock_cnt = 0UL;
     415           0 :     fd_pack_end_block( ctx->pack );
     416           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     417           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,        0 );
     418           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     419           0 :     return;
     420           0 :   }
     421             : 
     422             :   /* Am I leader? If not, see about inserting at most one transaction
     423             :      from extra storage.  It's important not to insert too many
     424             :      transactions here, or we won't end up servicing dedup_pack enough.
     425             :      If extra storage is empty or pack is full, do nothing. */
     426           0 :   if( FD_UNLIKELY( ctx->leader_slot==ULONG_MAX ) ) {
     427             : #if FD_PACK_USE_EXTRA_STORAGE
     428             :     if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) &&
     429             :          fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
     430             :       *charge_busy = 1;
     431             : 
     432             :       int result = insert_from_extra( ctx );
     433             :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
     434             :     }
     435             : #endif
     436           0 :     return;
     437           0 :   }
     438             : 
     439             :   /* Am I in drain mode?  If so, check if I can exit it */
     440           0 :   if( FD_UNLIKELY( ctx->drain_banks ) ) {
     441           0 :     if( FD_LIKELY( ctx->bank_idle_bitset==fd_ulong_mask_lsb( (int)bank_cnt ) ) ) ctx->drain_banks = 0;
     442           0 :     else                                                                         return;
     443           0 :   }
     444             : 
     445             :   /* Have I sent the max allowed microblocks? Nothing to do. */
     446           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt>=ctx->slot_max_microblocks ) ) return;
     447             : 
     448             :   /* Do I have enough transactions and/or have I waited enough time? */
     449           0 :   if( FD_UNLIKELY( (ulong)(now-ctx->last_successful_insert) <
     450           0 :         ctx->wait_duration_ticks[ fd_ulong_min( fd_pack_avail_txn_cnt( ctx->pack ), MAX_TXN_PER_MICROBLOCK ) ] ) ) {
     451           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, 0 );
     452           0 :     return;
     453           0 :   }
     454             : 
     455           0 :   int any_ready     = 0;
     456           0 :   int any_scheduled = 0;
     457             : 
     458           0 :   *charge_busy = 1;
     459             : 
     460             :   /* Try to schedule the next microblock.  Do we have any idle bank
     461             :      tiles? */
     462           0 :   if( FD_LIKELY( ctx->bank_idle_bitset ) ) { /* Optimize for schedule */
     463           0 :     any_ready = 1;
     464             : 
     465           0 :     int i               = fd_ulong_find_lsb( ctx->bank_idle_bitset );
     466             : 
     467             :     /* TODO: You can maybe make the case that this should happen as soon
     468             :        as we detect the bank has become idle, but doing it now probably
     469             :        helps with account locality. */
     470           0 :     fd_pack_microblock_complete( ctx->pack, (ulong)i );
     471             : 
     472           0 :     void * microblock_dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
     473           0 :     long schedule_duration = -fd_tickcount();
     474           0 :     ulong schedule_cnt = fd_pack_schedule_next_microblock( ctx->pack, CUS_PER_MICROBLOCK, VOTE_FRACTION, (ulong)i, microblock_dst );
     475           0 :     schedule_duration      += fd_tickcount();
     476           0 :     fd_histf_sample( ctx->schedule_duration, (ulong)schedule_duration );
     477             : 
     478           0 :     if( FD_LIKELY( schedule_cnt ) ) {
     479           0 :       any_scheduled = 1;
     480           0 :       ulong tspub  = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
     481           0 :       ulong chunk  = ctx->out_chunk;
     482           0 :       ulong msg_sz = schedule_cnt*sizeof(fd_txn_p_t);
     483           0 :       fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t*)((uchar*)microblock_dst+msg_sz);
     484           0 :       trailer->bank = ctx->leader_bank;
     485           0 :       trailer->microblock_idx = ctx->slot_microblock_cnt;
     486             : 
     487           0 :       ulong sig = fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_MICROBLOCK, (ulong)i );
     488           0 :       fd_stem_publish( stem, 0UL, sig, chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), 0UL, 0UL, tspub );
     489           0 :       ctx->bank_expect[ i ] = stem->seqs[0]-1UL;
     490           0 :       ctx->bank_ready_at[i] = now + (long)ctx->microblock_duration_ticks;
     491           0 :       ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), ctx->out_chunk0, ctx->out_wmark );
     492           0 :       ctx->slot_microblock_cnt++;
     493             : 
     494           0 :       ctx->bank_idle_bitset = fd_ulong_pop_lsb( ctx->bank_idle_bitset );
     495           0 :       ctx->skip_cnt         = (long)schedule_cnt * fd_long_if( ctx->use_consumed_cus, (long)bank_cnt/2L, 1L );
     496           0 :     }
     497           0 :   }
     498             : 
     499           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,       any_ready     );
     500           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, any_scheduled );
     501           0 :   now = fd_tickcount();
     502           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
     503             : 
     504             : #if FD_PACK_USE_EXTRA_STORAGE
     505             :   if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) ) ) {
     506             :     /* Don't start pulling from the extra storage until the available
     507             :        transaction count drops below half. */
     508             :     ulong avail_space   = (ulong)fd_long_max( 0L, (long)(ctx->max_pending_transactions>>1)-(long)fd_pack_avail_txn_cnt( ctx->pack ) );
     509             :     ulong qty_to_insert = fd_ulong_min( 10UL, fd_ulong_min( extra_txn_deq_cnt( ctx->extra_txn_deq ), avail_space ) );
     510             :     int any_successes = 0;
     511             :     for( ulong i=0UL; i<qty_to_insert; i++ ) any_successes |= (0<=insert_from_extra( ctx ));
     512             :     if( FD_LIKELY( any_successes ) ) ctx->last_successful_insert = now;
     513             :   }
     514             : #endif
     515             : 
     516             :   /* Did we send the maximum allowed microblocks? Then end the slot. */
     517           0 :   if( FD_UNLIKELY( ctx->slot_microblock_cnt==ctx->slot_max_microblocks )) {
     518           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER,       0 );
     519           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS,        0 );
     520           0 :     update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS,  0 );
     521             :     /* The pack object also does this accounting and increases this
     522             :        metric, but we end the slot early so won't see it unless we also
     523             :        increment it here. */
     524           0 :     FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT, 1UL );
     525           0 :     ctx->drain_banks         = 1;
     526           0 :     ctx->leader_slot         = ULONG_MAX;
     527           0 :     ctx->slot_microblock_cnt = 0UL;
     528           0 :     fd_pack_end_block( ctx->pack );
     529           0 :   }
     530           0 : }
     531             : 
     532             : 
     533             : /* At this point, we have started receiving frag seq with details in
     534             :     mline at time now.  Speculatively process it here. */
     535             : 
     536             : static inline void
     537             : during_frag( fd_pack_ctx_t * ctx,
     538             :              ulong           in_idx,
     539             :              ulong           seq,
     540             :              ulong           sig,
     541             :              ulong           chunk,
     542           0 :              ulong           sz ) {
     543           0 :   (void)seq;
     544             : 
     545           0 :   uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     546             : 
     547           0 :   switch( ctx->in_kind[ in_idx ] ) {
     548           0 :   case IN_KIND_POH: {
     549             :       /* Not interested in stamped microblocks, only leader updates. */
     550           0 :     if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
     551             : 
     552             :     /* There was a leader transition.  Handle it. */
     553           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
     554           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     555             : 
     556           0 :     if( FD_UNLIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
     557           0 :       FD_LOG_WARNING(( "switching to slot %lu while packing for slot %lu. Draining bank tiles.", fd_disco_poh_sig_slot( sig ), ctx->leader_slot ));
     558           0 :       ctx->drain_banks         = 1;
     559           0 :       ctx->leader_slot         = ULONG_MAX;
     560           0 :       ctx->slot_microblock_cnt = 0UL;
     561           0 :       fd_pack_end_block( ctx->pack );
     562           0 :     }
     563           0 :     ctx->leader_slot = fd_disco_poh_sig_slot( sig );
     564             : 
     565           0 :     ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->leader_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
     566           0 :     FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
     567             : 
     568           0 :     fd_became_leader_t * became_leader = (fd_became_leader_t *)dcache_entry;
     569           0 :     ctx->leader_bank          = became_leader->bank;
     570           0 :     ctx->slot_max_microblocks = became_leader->max_microblocks_in_slot;
     571             :     /* Reserve some space in the block for ticks */
     572           0 :     ctx->slot_max_data        = (ctx->larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK)
     573           0 :                                       - 48UL*(became_leader->ticks_per_slot+became_leader->total_skipped_ticks);
     574             : 
     575           0 :     FD_LOG_INFO(( "pack_became_leader(slot=%lu,ends_at=%ld)", ctx->leader_slot, became_leader->slot_end_ns ));
     576             : 
     577             :     /* The dcache might get overrun, so set slot_end_ns to 0, so if it does
     578             :        the slot will get skipped.  Then update it in the `after_frag` case
     579             :        below to the correct value. */
     580           0 :     ctx->slot_end_ns = 0L;
     581           0 :     ctx->_slot_end_ns = became_leader->slot_end_ns;
     582             : 
     583           0 :     update_metric_state( ctx, fd_tickcount(), FD_PACK_METRIC_STATE_LEADER, 1 );
     584           0 :     return;
     585           0 :   }
     586           0 :   case IN_KIND_BUNDLE: {
     587           0 :     FD_LOG_WARNING(( "Pack tile received a bundle... dropping..." ));
     588           0 :     return;
     589           0 :   }
     590           0 :   case IN_KIND_BANK: {
     591           0 :     FD_TEST( ctx->use_consumed_cus );
     592             :       /* For a previous slot */
     593           0 :     if( FD_UNLIKELY( fd_disco_bank_sig_slot( sig )!=ctx->leader_slot ) ) return;
     594             : 
     595           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<sizeof(fd_microblock_trailer_t)
     596           0 :           || sz>sizeof(fd_microblock_trailer_t)+sizeof(fd_txn_p_t)*MAX_TXN_PER_MICROBLOCK ) )
     597           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     598             : 
     599           0 :     ctx->pending_rebate_cnt = (sz-sizeof(fd_microblock_trailer_t))/sizeof(fd_txn_p_t);
     600           0 :     fd_memcpy( ctx->pending_rebate, dcache_entry, sz-sizeof(fd_microblock_trailer_t) );
     601           0 :     return;
     602           0 :   }
     603           0 :   case IN_KIND_RESOLV: {
     604           0 :     if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_TPU_RESOLVED_DCACHE_MTU ) )
     605           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     606             : 
     607           0 :     if( FD_UNLIKELY( (ctx->leader_slot==ULONG_MAX) & (sig>ctx->highest_observed_slot) ) ) {
     608             :       /* Using the resolv tile's knowledge of the current slot is a bit
     609             :          of a hack, since we don't get any info if there are no
     610             :          transactions and we're not leader.  We're actually in exactly
     611             :          the case where that's okay though.  The point of calling
     612             :          expire_before long before we become leader is so that we don't
     613             :          drop new but low-fee-paying transactions when pack is clogged
     614             :          with expired but high-fee-paying transactions.  That can only
     615             :          happen if we are getting transactions. */
     616           0 :       ctx->highest_observed_slot = sig;
     617           0 :       ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->highest_observed_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
     618           0 :       FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
     619           0 :     }
     620             : 
     621             : #if FD_PACK_USE_EXTRA_STORAGE
     622             :     if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX || fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
     623             :       ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
     624             :       ctx->insert_to_extra = 0;
     625             :     } else {
     626             :       if( FD_UNLIKELY( extra_txn_deq_full( ctx->extra_txn_deq ) ) ) {
     627             :         extra_txn_deq_remove_head( ctx->extra_txn_deq );
     628             :         FD_MCNT_INC( PACK, TRANSACTION_DROPPED_FROM_EXTRA, 1UL );
     629             :       }
     630             :       ctx->cur_spot = extra_txn_deq_peek_tail( extra_txn_deq_insert_tail( ctx->extra_txn_deq ) );
     631             :       /* We want to store the current time in cur_spot so that we can
     632             :          track its expiration better.  We just stash it in the CU
     633             :          fields, since those aren't important right now. */
     634             :       ctx->cur_spot->txnp->blockhash_slot = sig;
     635             :       ctx->insert_to_extra                = 1;
     636             :       FD_MCNT_INC( PACK, TRANSACTION_INSERTED_TO_EXTRA, 1UL );
     637             :     }
     638             : #else
     639           0 :     ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
     640           0 : #endif
     641             : 
     642           0 :     ulong payload_sz;
     643             :     /* We get transactions from the resolv tile.
     644             :       The transactions should have been parsed and verified. */
     645           0 :     FD_MCNT_INC( PACK, NORMAL_TRANSACTION_RECEIVED, 1UL );
     646             :     /* Assume that the dcache entry is:
     647             :           Payload ....... (payload_sz bytes)
     648             :           0 or 1 byte of padding (since alignof(fd_txn) is 2)
     649             :           fd_txn ....... (size computed by fd_txn_footprint)
     650             :           Optionally:
     651             :               0 to 30 bytes of padding
     652             :               expanded alt (32*txn->addr_table_adtl_cnt) bytes
     653             :           payload_sz  (2B)
     654             :       mline->sz includes all three or four fields and the padding */
     655           0 :     payload_sz = *(ushort*)(dcache_entry + sz - sizeof(ushort));
     656           0 :     uchar    const * payload   = dcache_entry;
     657           0 :     fd_txn_t const * txn       = (fd_txn_t const *)( dcache_entry + fd_ulong_align_up( payload_sz, 2UL ) );
     658           0 :     ulong txn_footprint        = fd_txn_footprint( txn->instr_cnt, txn->addr_table_lookup_cnt );
     659           0 :     fd_acct_addr_t const * alt = (fd_acct_addr_t const *)( (uchar const *)dcache_entry + fd_ulong_align_up( fd_ulong_align_up( payload_sz, 2UL ) + txn_footprint, 32UL ) );
     660           0 :     fd_memcpy( ctx->cur_spot->txnp->payload, payload, payload_sz                     );
     661           0 :     fd_memcpy( TXN(ctx->cur_spot->txnp),     txn,     txn_footprint                  );
     662           0 :     fd_memcpy( ctx->cur_spot->alt_accts,     alt,     32UL*txn->addr_table_adtl_cnt  );
     663           0 :     ctx->cur_spot->txnp->payload_sz = payload_sz;
     664             : 
     665             :   #if DETAILED_LOGGING
     666             :     FD_LOG_NOTICE(( "Pack got a packet. Payload size: %lu, txn footprint: %lu", payload_sz,
     667             :           fd_txn_footprint( txn->instr_cnt, txn->addr_table_lookup_cnt )
     668             :         ));
     669             :   #endif
     670           0 :     break;
     671           0 :   }
     672           0 :   }
     673           0 : }
     674             : 
     675             : /* After the transaction has been fully received, and we know we were
     676             :    not overrun while reading it, insert it into pack. */
     677             : 
     678             : static inline void
     679             : after_frag( fd_pack_ctx_t *     ctx,
     680             :             ulong               in_idx,
     681             :             ulong               seq,
     682             :             ulong               sig,
     683             :             ulong               chunk,
     684             :             ulong               sz,
     685             :             ulong               tsorig,
     686           0 :             fd_stem_context_t * stem ) {
     687           0 :   (void)seq;
     688           0 :   (void)chunk;
     689           0 :   (void)sz;
     690           0 :   (void)tsorig;
     691           0 :   (void)stem;
     692             : 
     693           0 :   long now = fd_tickcount();
     694             : 
     695           0 :   switch( ctx->in_kind[ in_idx ] ) {
     696           0 :   case IN_KIND_POH: {
     697           0 :     if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
     698             : 
     699           0 :     ctx->slot_end_ns = ctx->_slot_end_ns;
     700           0 :     fd_pack_set_block_limits( ctx->pack, ctx->slot_max_microblocks, ctx->slot_max_data );
     701           0 :     break;
     702           0 :   }
     703           0 :   case IN_KIND_BUNDLE: {
     704           0 :     FD_LOG_WARNING(( "Pack tile received a bundle... dropping..." ));
     705           0 :     break;
     706           0 :   }
     707           0 :   case IN_KIND_BANK: {
     708             :     /* For a previous slot */
     709           0 :     if( FD_UNLIKELY( fd_disco_bank_sig_slot( sig )!=ctx->leader_slot ) ) return;
     710             : 
     711           0 :     fd_pack_rebate_cus( ctx->pack, ctx->pending_rebate, ctx->pending_rebate_cnt );
     712           0 :     ctx->pending_rebate_cnt = 0UL;
     713           0 :     break;
     714           0 :   }
     715           0 :   case IN_KIND_RESOLV: {
     716             :     /* Normal transaction case */
     717             : #if FD_PACK_USE_EXTRA_STORAGE
     718             :     if( FD_LIKELY( !ctx->insert_to_extra ) ) {
     719             : #else
     720           0 :     if( 1 ) {
     721           0 : #endif
     722           0 :       ulong blockhash_slot = sig;
     723           0 :       long insert_duration = -fd_tickcount();
     724           0 :       int result = fd_pack_insert_txn_fini( ctx->pack, ctx->cur_spot, blockhash_slot );
     725           0 :       insert_duration      += fd_tickcount();
     726           0 :       ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
     727           0 :       fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
     728           0 :       if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
     729           0 :     }
     730             : 
     731           0 :     ctx->cur_spot = NULL;
     732           0 :     break;
     733           0 :   }
     734           0 :   }
     735             : 
     736           0 :   update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
     737           0 : }
     738             : 
     739             : static void
     740             : unprivileged_init( fd_topo_t *      topo,
     741           0 :                    fd_topo_tile_t * tile ) {
     742           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     743             : 
     744           0 :   fd_pack_limits_t limits[1] = {{
     745           0 :     .max_cost_per_block        = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK,
     746           0 :     .max_vote_cost_per_block   = FD_PACK_MAX_VOTE_COST_PER_BLOCK,
     747           0 :     .max_write_cost_per_acct   = FD_PACK_MAX_WRITE_COST_PER_ACCT,
     748           0 :     .max_data_bytes_per_block  = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
     749           0 :     .max_txn_per_microblock    = EFFECTIVE_TXN_PER_MICROBLOCK,
     750           0 :     .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
     751           0 :   }};
     752             : 
     753           0 :   ulong pack_footprint = fd_pack_footprint( tile->pack.max_pending_transactions, tile->pack.bank_tile_count, limits );
     754             : 
     755           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     756           0 :   fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
     757           0 :   fd_rng_t *      rng = fd_rng_join( fd_rng_new( FD_SCRATCH_ALLOC_APPEND( l, fd_rng_align(), fd_rng_footprint() ), 0U, 0UL ) );
     758           0 :   if( FD_UNLIKELY( !rng ) ) FD_LOG_ERR(( "fd_rng_new failed" ));
     759             : 
     760           0 :   ctx->pack = fd_pack_join( fd_pack_new( FD_SCRATCH_ALLOC_APPEND( l, fd_pack_align(), pack_footprint ),
     761           0 :                                          tile->pack.max_pending_transactions, tile->pack.bank_tile_count,
     762           0 :                                          limits, rng ) );
     763           0 :   if( FD_UNLIKELY( !ctx->pack ) ) FD_LOG_ERR(( "fd_pack_new failed" ));
     764             : 
     765           0 :   if( FD_UNLIKELY( tile->in_cnt>32UL ) ) FD_LOG_ERR(( "Too many input links (%lu>32) to pack tile", tile->in_cnt ));
     766             : 
     767           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     768           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     769             : 
     770           0 :     if( FD_LIKELY(      !strcmp( link->name, "resolv_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
     771           0 :     else if( FD_LIKELY( !strcmp( link->name, "dedup_pack"  ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
     772           0 :     else if( FD_LIKELY( !strcmp( link->name, "poh_pack"    ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
     773           0 :     else if( FD_LIKELY( !strcmp( link->name, "bank_poh"    ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK;
     774           0 :     else if( FD_LIKELY( !strcmp( link->name, "bundle_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
     775           0 :     else FD_LOG_ERR(( "pack tile has unexpected input link %lu %s", i, link->name ));
     776           0 :   }
     777             : 
     778           0 :   ulong out_cnt = fd_topo_link_consumer_cnt( topo, &topo->links[ tile->out_link_id[ 0 ] ] );
     779             : 
     780           0 :   if( FD_UNLIKELY( !out_cnt                                ) ) FD_LOG_ERR(( "pack tile connects to no banking tiles" ));
     781           0 :   if( FD_UNLIKELY( out_cnt>FD_PACK_PACK_MAX_OUT            ) ) FD_LOG_ERR(( "pack tile connects to too many banking tiles" ));
     782           0 :   if( FD_UNLIKELY( out_cnt!=tile->pack.bank_tile_count+1UL ) ) FD_LOG_ERR(( "pack tile connects to %lu banking tiles, but tile->pack.bank_tile_count is %lu", out_cnt-1UL, tile->pack.bank_tile_count ));
     783             : 
     784             : #if FD_PACK_USE_EXTRA_STORAGE
     785             :   ctx->extra_txn_deq = extra_txn_deq_join( extra_txn_deq_new( FD_SCRATCH_ALLOC_APPEND( l, extra_txn_deq_align(),
     786             :                                                                                           extra_txn_deq_footprint() ) ) );
     787             : #endif
     788             : 
     789           0 :   ctx->cur_spot                      = NULL;
     790           0 :   ctx->max_pending_transactions      = tile->pack.max_pending_transactions;
     791           0 :   ctx->leader_slot                   = ULONG_MAX;
     792           0 :   ctx->leader_bank                   = NULL;
     793           0 :   ctx->slot_microblock_cnt           = 0UL;
     794           0 :   ctx->slot_max_microblocks          = 0UL;
     795           0 :   ctx->slot_max_data                 = 0UL;
     796           0 :   ctx->larger_shred_limits_per_block = tile->pack.larger_shred_limits_per_block;
     797           0 :   ctx->rng                           = rng;
     798           0 :   ctx->last_successful_insert        = 0L;
     799           0 :   ctx->highest_observed_slot         = 0UL;
     800           0 :   ctx->microblock_duration_ticks     = (ulong)(fd_tempo_tick_per_ns( NULL )*(double)MICROBLOCK_DURATION_NS  + 0.5);
     801             : #if FD_PACK_USE_EXTRA_STORAGE
     802             :   ctx->insert_to_extra               = 0;
     803             : #endif
     804           0 :   ctx->use_consumed_cus              = tile->pack.use_consumed_cus;
     805             : 
     806           0 :   ctx->wait_duration_ticks[ 0 ] = ULONG_MAX;
     807           0 :   for( ulong i=1UL; i<MAX_TXN_PER_MICROBLOCK+1UL; i++ ) {
     808           0 :     ctx->wait_duration_ticks[ i ]=(ulong)(fd_tempo_tick_per_ns( NULL )*(double)wait_duration[ i ] + 0.5);
     809           0 :   }
     810             : 
     811             : 
     812           0 :   ctx->bank_cnt         = tile->pack.bank_tile_count;
     813           0 :   ctx->poll_cursor      = 0;
     814           0 :   ctx->skip_cnt         = 0L;
     815           0 :   ctx->bank_idle_bitset = fd_ulong_mask_lsb( (int)tile->pack.bank_tile_count );
     816           0 :   for( ulong i=0UL; i<tile->pack.bank_tile_count; i++ ) {
     817           0 :     ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "bank_busy.%lu", i );
     818           0 :     FD_TEST( busy_obj_id!=ULONG_MAX );
     819           0 :     ctx->bank_current[ i ] = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
     820           0 :     ctx->bank_expect[ i ] = ULONG_MAX;
     821           0 :     if( FD_UNLIKELY( !ctx->bank_current[ i ] ) ) FD_LOG_ERR(( "banking tile %lu has no busy flag", i ));
     822           0 :     ctx->bank_ready_at[ i ] = 0L;
     823           0 :     FD_TEST( ULONG_MAX==fd_fseq_query( ctx->bank_current[ i ] ) );
     824           0 :   }
     825             : 
     826           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     827           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
     828           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     829             : 
     830           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     831           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     832           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     833           0 :   }
     834             : 
     835           0 :   ctx->out_mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
     836           0 :   ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
     837           0 :   ctx->out_wmark  = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
     838           0 :   ctx->out_chunk  = ctx->out_chunk0;
     839             : 
     840             :   /* Initialize metrics storage */
     841           0 :   memset( ctx->insert_result, '\0', FD_PACK_INSERT_RETVAL_CNT * sizeof(ulong) );
     842           0 :   fd_histf_join( fd_histf_new( ctx->schedule_duration, FD_MHIST_SECONDS_MIN( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ),
     843           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ) ) );
     844           0 :   fd_histf_join( fd_histf_new( ctx->insert_duration,   FD_MHIST_SECONDS_MIN( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ),
     845           0 :                                                        FD_MHIST_SECONDS_MAX( PACK, INSERT_TRANSACTION_DURATION_SECONDS  ) ) );
     846           0 :   ctx->metric_state = 0;
     847           0 :   ctx->metric_state_begin = fd_tickcount();
     848           0 :   memset( ctx->metric_timing, '\0', 16*sizeof(long) );
     849             : 
     850           0 :   FD_LOG_INFO(( "packing microblocks of at most %lu transactions to %lu bank tiles", EFFECTIVE_TXN_PER_MICROBLOCK, tile->pack.bank_tile_count ));
     851             : 
     852           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     853           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     854           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     855             : 
     856           0 : }
     857             : 
     858             : static ulong
     859             : populate_allowed_seccomp( fd_topo_t const *      topo,
     860             :                           fd_topo_tile_t const * tile,
     861             :                           ulong                  out_cnt,
     862           0 :                           struct sock_filter *   out ) {
     863           0 :   (void)topo;
     864           0 :   (void)tile;
     865             : 
     866           0 :   populate_sock_filter_policy_pack( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     867           0 :   return sock_filter_policy_pack_instr_cnt;
     868           0 : }
     869             : 
     870             : static ulong
     871             : populate_allowed_fds( fd_topo_t const *      topo,
     872             :                       fd_topo_tile_t const * tile,
     873             :                       ulong                  out_fds_cnt,
     874           0 :                       int *                  out_fds ) {
     875           0 :   (void)topo;
     876           0 :   (void)tile;
     877             : 
     878           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     879             : 
     880           0 :   ulong out_cnt = 0UL;
     881           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     882           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     883           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     884           0 :   return out_cnt;
     885           0 : }
     886             : 
     887           0 : #define STEM_BURST (1UL)
     888             : 
     889             : /* We want lazy (measured in ns) to be small enough that the producer
     890             :     and the consumer never have to wait for credits.  For most tango
     891             :     links, we use a default worst case speed coming from 100 Gbps
     892             :     Ethernet.  That's not very suitable for microblocks that go from
     893             :     pack to bank.  Instead we manually estimate the very aggressive
     894             :     1000ns per microblock, and then reduce it further (in line with the
     895             :     default lazy value computation) to ensure the random value chosen
     896             :     based on this won't lead to credit return stalls. */
     897           0 : #define STEM_LAZY  (128L*3000L)
     898             : 
     899           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_pack_ctx_t
     900           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_pack_ctx_t)
     901             : 
     902           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     903           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     904           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     905           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     906           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     907           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     908             : 
     909             : #include "../../../../disco/stem/fd_stem.c"
     910             : 
     911             : fd_topo_run_tile_t fd_tile_pack = {
     912             :   .name                     = "pack",
     913             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     914             :   .populate_allowed_fds     = populate_allowed_fds,
     915             :   .scratch_align            = scratch_align,
     916             :   .scratch_footprint        = scratch_footprint,
     917             :   .unprivileged_init        = unprivileged_init,
     918             :   .run                      = stem_run,
     919             : };

Generated by: LCOV version 1.14