LCOV - code coverage report
Current view: top level - flamenco/progcache - fd_progcache_admin.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 92 385 23.9 %
Date: 2026-11-26 02:16:43 Functions: 4 13 30.8 %

          Line data    Source code
       1             : #include "fd_progcache.h"
       2             : #include "fd_progcache_admin.h"
       3             : #include "fd_progcache_base.h"
       4             : #include "fd_progcache_clock.h"
       5             : #include "fd_progcache_rec.h"
       6             : #include "fd_progcache_reclaim.h"
       7             : #include "fd_progcache_xid.h"
       8             : #include "../../util/racesan/fd_racesan_target.h"
       9             : 
      10             : /* FIXME get rid of this thread-local */
      11             : FD_TL fd_progcache_admin_metrics_t fd_progcache_admin_metrics_g;
      12             : 
      13             : /* Transaction-level operations.  txn_pool and txn_map are serialized by
      14             :    txn.rwlock: written here under the exclusive lock, read shared by the user
      15             :    and eviction paths. */
      16             : 
      17             : fd_progcache_fork_id_t
      18             : fd_progcache_attach_child( fd_progcache_join_t *  cache,
      19      120271 :                            fd_progcache_fork_id_t parent_fork_id ) {
      20      120271 :   if( FD_UNLIKELY( !cache ) ) FD_LOG_CRIT(( "invalid arguments" ));
      21             : 
      22      120271 :   fd_rwlock_write( &cache->shmem->txn.rwlock );
      23      120271 :   if( FD_UNLIKELY( fd_prog_txnp_free( cache->txn.pool )==0UL ) ) {
      24           0 :     FD_LOG_ERR(( "fd_progcache_attach_child failed: transaction object pool out of memory" ));
      25           0 :   }
      26             : 
      27      120271 :   ulong  txn_max = fd_prog_txnp_max( cache->txn.pool );
      28      120271 :   ulong  parent_idx;
      29      120271 :   uint * _child_head_idx;
      30      120271 :   uint * _child_tail_idx;
      31             : 
      32      120271 :   fd_progcache_fork_id_t root = __atomic_load_n( &cache->shmem->txn.root, memory_order_relaxed );
      33      120271 :   if( FD_UNLIKELY( parent_fork_id == root ) ) {
      34             : 
      35       64067 :     parent_idx = FD_PROGCACHE_TXN_IDX_NULL;
      36             : 
      37       64067 :     _child_head_idx = &cache->shmem->txn.child_head_idx;
      38       64067 :     _child_tail_idx = &cache->shmem->txn.child_tail_idx;
      39             : 
      40       64067 :   } else {
      41             : 
      42       56204 :     parent_idx = fd_prog_txnm_idx_query( cache->txn.map, &parent_fork_id, ULONG_MAX, cache->txn.pool );
      43       56204 :     if( FD_UNLIKELY( parent_idx==ULONG_MAX ) ) {
      44           0 :       FD_LOG_CRIT(( "fd_progcache_attach_child failed: user provided invalid parent fork_id %lu", parent_fork_id ));
      45           0 :     }
      46       56204 :     if( FD_UNLIKELY( parent_idx >= txn_max ) )
      47           0 :       FD_LOG_CRIT(( "progcache: corruption detected (attach_child parent_idx=%lu txn_max=%lu)", parent_idx, txn_max ));
      48             : 
      49       56204 :     _child_head_idx = &cache->txn.pool[ parent_idx ].child_head_idx;
      50       56204 :     _child_tail_idx = &cache->txn.pool[ parent_idx ].child_tail_idx;
      51             : 
      52       56204 :   }
      53             : 
      54      120271 :   uint txn_idx = (uint)fd_prog_txnp_idx_acquire( cache->txn.pool );
      55      120271 :   if( FD_UNLIKELY( txn_idx==UINT_MAX ) ) FD_LOG_ERR(( "fd_progcache_attach_child failed: transaction object pool out of memory" ));
      56      120271 :   fd_progcache_txn_t * txn = &cache->txn.pool[ txn_idx ];
      57      120271 :   txn->xid = __atomic_add_fetch( &cache->shmem->txn.seq, 1UL, memory_order_relaxed );
      58             : 
      59      120271 :   uint sibling_prev_idx = *_child_tail_idx;
      60             : 
      61      120271 :   int first_born = sibling_prev_idx==UINT_MAX;
      62      120271 :   if( FD_UNLIKELY( !first_born && (ulong)sibling_prev_idx >= txn_max ) )
      63           0 :     FD_LOG_CRIT(( "progcache: corruption detected (attach_child sibling_prev_idx=%u txn_max=%lu)", sibling_prev_idx, txn_max ));
      64             : 
      65      120271 :   txn->parent_idx       = (uint)parent_idx;
      66      120271 :   txn->child_head_idx   = UINT_MAX;
      67      120271 :   txn->child_tail_idx   = UINT_MAX;
      68      120271 :   txn->sibling_prev_idx = (uint)sibling_prev_idx;
      69      120271 :   txn->sibling_next_idx = UINT_MAX;
      70             : 
      71      120271 :   txn->rec_head_idx = UINT_MAX;
      72      120271 :   txn->rec_tail_idx = UINT_MAX;
      73             : 
      74             :   /* TODO: consider branchless impl */
      75      120499 :   if( FD_LIKELY( first_born ) ) *_child_head_idx            = (uint)txn_idx; /* opt for non-compete */
      76 >1844*10^16 :   else cache->txn.pool[ sibling_prev_idx ].sibling_next_idx = (uint)txn_idx;
      77             : 
      78      120271 :   *_child_tail_idx = (uint)txn_idx;
      79             : 
      80      120271 :   fd_prog_txnm_idx_insert( cache->txn.map, txn_idx, cache->txn.pool );
      81             : 
      82      120271 :   fd_rwlock_unwrite( &cache->shmem->txn.rwlock );
      83      120271 :   return txn->xid;
      84      120271 : }
      85             : 
      86             : static void
      87             : fd_progcache_cancel_one( fd_progcache_join_t * cache,
      88           0 :                          fd_progcache_txn_t *  txn ) {
      89           0 :   ulong rec_max = cache->rec.max;
      90           0 :   ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
      91             : 
      92           0 :   fd_rwlock_write( &txn->lock );
      93             : 
      94           0 :   if( FD_UNLIKELY( txn->child_head_idx!=UINT_MAX ||
      95           0 :                    txn->child_tail_idx!=UINT_MAX ) ) {
      96           0 :     FD_LOG_CRIT(( "fd_progcache_cancel failed: txn at %p with fork_id %lu has children (data corruption?)",
      97           0 :                   (void *)txn, txn->xid ));
      98           0 :   }
      99             : 
     100             :   /* Remove records */
     101             : 
     102           0 :   for( uint idx = txn->rec_head_idx; idx!=UINT_MAX; ) {
     103           0 :     if( FD_UNLIKELY( (ulong)idx >= rec_max ) )
     104           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_one rec_idx=%u rec_max=%lu)", idx, rec_max ));
     105           0 :     fd_progcache_rec_t * rec = &cache->rec.ele[ idx ];
     106           0 :     uint next_idx = rec->next_idx;
     107           0 :     if( FD_UNLIKELY( next_idx!=UINT_MAX && (ulong)next_idx >= rec_max ) )
     108           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_one next_idx=%u rec_max=%lu)", next_idx, rec_max ));
     109           0 :     atomic_store_explicit( &rec->txn_idx, UINT_MAX, memory_order_release );
     110           0 :     fd_racesan_hook( "prog_cancel_one:post_orphan" );
     111           0 :     fd_prog_delete_rec( cache, rec );
     112           0 :     idx = next_idx;
     113           0 :   }
     114             : 
     115           0 :   txn->rec_head_idx = UINT_MAX;
     116           0 :   txn->rec_tail_idx = UINT_MAX;
     117             : 
     118             :   /* Remove transaction from fork graph */
     119             : 
     120           0 :   uint self_idx = (uint)( txn - cache->txn.pool );
     121           0 :   uint prev_idx = txn->sibling_prev_idx;
     122           0 :   uint next_idx = txn->sibling_next_idx;
     123           0 :   if( next_idx!=UINT_MAX ) {
     124           0 :     if( FD_UNLIKELY( (ulong)next_idx >= txn_max ) )
     125           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_one sibling_next_idx=%u txn_max=%lu)", next_idx, txn_max ));
     126           0 :     cache->txn.pool[ next_idx ].sibling_prev_idx = prev_idx;
     127           0 :   }
     128           0 :   if( prev_idx!=UINT_MAX ) {
     129           0 :     if( FD_UNLIKELY( (ulong)prev_idx >= txn_max ) )
     130           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_one sibling_prev_idx=%u txn_max=%lu)", prev_idx, txn_max ));
     131           0 :     cache->txn.pool[ prev_idx ].sibling_next_idx = next_idx;
     132           0 :   }
     133           0 :   if( txn->parent_idx!=UINT_MAX ) {
     134           0 :     if( FD_UNLIKELY( (ulong)txn->parent_idx >= txn_max ) )
     135           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_one parent_idx=%u txn_max=%lu)", txn->parent_idx, txn_max ));
     136           0 :     fd_progcache_txn_t * parent = &cache->txn.pool[ txn->parent_idx ];
     137           0 :     if( parent->child_head_idx==self_idx ) parent->child_head_idx = next_idx;
     138           0 :     if( parent->child_tail_idx==self_idx ) parent->child_tail_idx = prev_idx;
     139           0 :   } else {
     140           0 :     if( cache->shmem->txn.child_head_idx==self_idx ) cache->shmem->txn.child_head_idx = next_idx;
     141           0 :     if( cache->shmem->txn.child_tail_idx==self_idx ) cache->shmem->txn.child_tail_idx = prev_idx;
     142           0 :   }
     143             : 
     144             :   /* Remove transaction from index */
     145             : 
     146           0 :   if( FD_UNLIKELY( !fd_prog_txnm_ele_remove( cache->txn.map, &txn->xid, NULL, cache->txn.pool ) ) ) {
     147           0 :     FD_LOG_CRIT(( "fd_progcache_cancel failed: fd_prog_txnm_ele_remove(%lu) failed", txn->xid ));
     148           0 :   }
     149             : 
     150             :   /* Free transaction object */
     151             : 
     152           0 :   fd_rwlock_unwrite( &txn->lock );
     153           0 :   fd_prog_txnp_ele_release( cache->txn.pool, txn );
     154           0 : }
     155             : 
     156             : /* Cancels txn and all children */
     157             : 
     158             : static void
     159             : fd_progcache_cancel_tree( fd_progcache_join_t * cache,
     160           0 :                           fd_progcache_txn_t *  txn ) {
     161           0 :   ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
     162           0 :   for(;;) {
     163           0 :     uint child_idx = txn->child_head_idx;
     164           0 :     if( child_idx==UINT_MAX ) break;
     165           0 :     if( FD_UNLIKELY( (ulong)child_idx >= txn_max ) )
     166           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_tree child_idx=%u txn_max=%lu)", child_idx, txn_max ));
     167           0 :     fd_progcache_txn_t * child = &cache->txn.pool[ child_idx ];
     168           0 :     fd_progcache_cancel_tree( cache, child );
     169           0 :   }
     170           0 :   fd_progcache_cancel_one( cache, txn );
     171           0 : }
     172             : 
     173             : /* Cancels all left/right siblings */
     174             : 
     175             : static void
     176             : fd_progcache_cancel_prev_list( fd_progcache_join_t * cache,
     177           0 :                                fd_progcache_txn_t *  txn ) {
     178           0 :   ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
     179           0 :   uint cur_idx = txn->sibling_prev_idx;
     180           0 :   while( cur_idx!=UINT_MAX ) {
     181           0 :     if( FD_UNLIKELY( (ulong)cur_idx >= txn_max ) )
     182           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_prev_list txn_idx=%u txn_max=%lu)", cur_idx, txn_max ));
     183           0 :     fd_progcache_txn_t * sibling = &cache->txn.pool[ cur_idx ];
     184           0 :     uint next = sibling->sibling_prev_idx;
     185           0 :     fd_progcache_cancel_tree( cache, sibling );
     186           0 :     cur_idx = next;
     187           0 :   }
     188           0 : }
     189             : 
     190             : static void
     191             : fd_progcache_cancel_next_list( fd_progcache_join_t * cache,
     192           0 :                                fd_progcache_txn_t *  txn ) {
     193           0 :   ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
     194           0 :   uint cur_idx = txn->sibling_next_idx;
     195           0 :   while( cur_idx!=UINT_MAX ) {
     196           0 :     if( FD_UNLIKELY( (ulong)cur_idx >= txn_max ) )
     197           0 :       FD_LOG_CRIT(( "progcache: corruption detected (cancel_next_list txn_idx=%u txn_max=%lu)", cur_idx, txn_max ));
     198           0 :     fd_progcache_txn_t * sibling = &cache->txn.pool[ cur_idx ];
     199           0 :     uint next = sibling->sibling_next_idx;
     200           0 :     fd_progcache_cancel_tree( cache, sibling );
     201           0 :     cur_idx = next;
     202           0 :   }
     203           0 : }
     204             : 
     205             : /* fd_progcache_txn_publish_one merges an in-prep transaction whose
     206             :    parent is the last published, into the parent. */
     207             : 
     208             : static void
     209             : fd_progcache_txn_publish_one( fd_progcache_join_t * cache,
     210           0 :                               fd_progcache_txn_t *  txn ) {
     211             : 
     212             :   /* Phase 1: Mark transaction as "last published" */
     213             : 
     214           0 :   fd_progcache_fork_id_t const fork_id = txn->xid;
     215           0 :   if( FD_UNLIKELY( txn->parent_idx!=UINT_MAX ) ) {
     216           0 :     FD_LOG_CRIT(( "fd_progcache_publish failed: txn with fork_id %lu is not a child of the last published txn", fork_id ));
     217           0 :   }
     218           0 :   fd_racesan_hook( "prog_publish_one:pre_xid_store" );
     219           0 :   __atomic_store_n( &cache->shmem->txn.root, fork_id, memory_order_release );
     220             : 
     221             :   /* Phase 2: Lock the record list for detachment.  The caller's
     222             :      exclusive txn.rwlock has already drained inserters. */
     223             : 
     224           0 :   fd_rwlock_write( &txn->lock );
     225             : 
     226             :   /* Phase 3: Detach records */
     227             : 
     228           0 :   ulong rec_max = cache->rec.max;
     229           0 :   for( uint idx = txn->rec_head_idx; idx!=UINT_MAX; ) {
     230           0 :     if( FD_UNLIKELY( (ulong)idx >= rec_max ) )
     231           0 :       FD_LOG_CRIT(( "progcache: corruption detected (publish_one rec_idx=%u rec_max=%lu)", idx, rec_max ));
     232           0 :     uint next_idx = cache->rec.ele[ idx ].next_idx;
     233           0 :     if( FD_UNLIKELY( next_idx!=UINT_MAX && (ulong)next_idx >= rec_max ) )
     234           0 :       FD_LOG_CRIT(( "progcache: corruption detected (publish_one next_idx=%u rec_max=%lu)", next_idx, rec_max ));
     235           0 :     atomic_store_explicit( &cache->rec.ele[ idx ].txn_idx, UINT_MAX, memory_order_release );
     236           0 :     fd_racesan_hook( "prog_publish_one:post_detach" );
     237           0 :     fd_progcache_admin_metrics_g.root_cnt++;
     238           0 :     idx = next_idx;
     239           0 :   }
     240             : 
     241           0 :   txn->rec_head_idx = UINT_MAX;
     242           0 :   txn->rec_tail_idx = UINT_MAX;
     243             : 
     244             :   /* Phase 4: Remove transaction from fork graph */
     245             : 
     246           0 :   { /* Adjust the parent pointers of the children to point to "last published" */
     247           0 :     ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
     248           0 :     ulong child_idx = txn->child_head_idx;
     249           0 :     while( child_idx!=UINT_MAX ) {
     250           0 :       if( FD_UNLIKELY( child_idx >= txn_max ) )
     251           0 :         FD_LOG_CRIT(( "progcache: corruption detected (publish_one child_idx=%lu txn_max=%lu)", child_idx, txn_max ));
     252           0 :       cache->txn.pool[ child_idx ].parent_idx = UINT_MAX;
     253           0 :       child_idx = cache->txn.pool[ child_idx ].sibling_next_idx;
     254           0 :     }
     255           0 :   }
     256             : 
     257             :   /* Phase 5: Remove transaction from index */
     258             : 
     259           0 :   if( FD_UNLIKELY( fd_prog_txnm_idx_remove( cache->txn.map, &txn->xid, ULONG_MAX, cache->txn.pool )==ULONG_MAX ) ) {
     260           0 :     FD_LOG_CRIT(( "fd_progcache_publish failed: fd_prog_txnm_idx_remove(%lu) failed", txn->xid ));
     261           0 :   }
     262             : 
     263             :   /* Phase 6: Free transaction object */
     264             : 
     265           0 :   fd_rwlock_unwrite( &txn->lock );
     266           0 :   txn->parent_idx       = UINT_MAX;
     267           0 :   txn->sibling_prev_idx = UINT_MAX;
     268           0 :   txn->sibling_next_idx = UINT_MAX;
     269           0 :   txn->child_head_idx   = UINT_MAX;
     270           0 :   txn->child_tail_idx   = UINT_MAX;
     271           0 :   fd_prog_txnp_ele_release( cache->txn.pool, txn );
     272           0 : }
     273             : 
     274             : void
     275             : fd_progcache_advance_root( fd_progcache_join_t *  cache,
     276           0 :                            fd_progcache_fork_id_t fork_id ) {
     277           0 :   if( FD_UNLIKELY( !cache ) ) FD_LOG_CRIT(( "invalid arguments" ));
     278             : 
     279             :   /* Detach records from txns without acquiring record locks */
     280             : 
     281           0 :   fd_rwlock_write( &cache->shmem->txn.rwlock );
     282             : 
     283           0 :   ulong txn_max = fd_prog_txnp_max( cache->txn.pool );
     284           0 :   uint txn_idx = (uint)fd_prog_txnm_idx_query( cache->txn.map, &fork_id, UINT_MAX, cache->txn.pool );
     285           0 :   if( FD_UNLIKELY( txn_idx==UINT_MAX ) ) {
     286           0 :     FD_LOG_CRIT(( "fd_progcache_advance_root failed: invalid fork_id %lu", fork_id ));
     287           0 :   }
     288           0 :   if( FD_UNLIKELY( (ulong)txn_idx >= txn_max ) )
     289           0 :     FD_LOG_CRIT(( "progcache: corruption detected (advance_root txn_idx=%u txn_max=%lu)", txn_idx, txn_max ));
     290           0 :   fd_progcache_txn_t * txn = &cache->txn.pool[ txn_idx ];
     291           0 :   if( FD_UNLIKELY( txn->parent_idx!=UINT_MAX ) ) {
     292           0 :     FD_LOG_CRIT(( "fd_progcache_advance_root: parent of txn %lu is not root", fork_id ));
     293           0 :   }
     294             : 
     295           0 :   fd_progcache_cancel_prev_list( cache, txn );
     296           0 :   fd_progcache_cancel_next_list( cache, txn );
     297             : 
     298           0 :   txn->sibling_prev_idx = UINT_MAX;
     299           0 :   txn->sibling_next_idx = UINT_MAX;
     300           0 :   cache->shmem->txn.child_head_idx = txn->child_head_idx;
     301           0 :   cache->shmem->txn.child_tail_idx = txn->child_tail_idx;
     302             : 
     303           0 :   fd_progcache_txn_publish_one( cache, txn );
     304             : 
     305           0 :   fd_rwlock_unwrite( &cache->shmem->txn.rwlock );
     306           0 : }
     307             : 
     308             : void
     309             : fd_progcache_cancel_fork( fd_progcache_join_t *  cache,
     310           0 :                           fd_progcache_fork_id_t fork_id ) {
     311           0 :   if( FD_UNLIKELY( !cache ) ) {
     312           0 :     FD_LOG_CRIT(( "invalid arguments" ));
     313           0 :   }
     314             : 
     315           0 :   fd_rwlock_write( &cache->shmem->txn.rwlock );
     316             : 
     317           0 :   fd_progcache_txn_t * txn = fd_prog_txnm_ele_query( cache->txn.map, &fork_id, NULL, cache->txn.pool );
     318           0 :   if( FD_UNLIKELY( !txn ) ) {
     319           0 :     FD_LOG_CRIT(( "fd_progcache_cancel failed: invalid fork_id %lu", fork_id ));
     320           0 :   }
     321           0 :   fd_progcache_cancel_tree( cache, txn );
     322             : 
     323           0 :   fd_rwlock_unwrite( &cache->shmem->txn.rwlock );
     324           0 : }
     325             : 
     326             : /* reset_rec_map frees all records in a progcache instance. */
     327             : 
     328             : static void
     329       64043 : reset_rec_map( fd_progcache_join_t * cache ) {
     330       64043 :   ulong chain_cnt = fd_prog_recm_chain_cnt( cache->rec.map );
     331     8180929 :   for( ulong chain_idx=0UL; chain_idx<chain_cnt; chain_idx++ ) {
     332     8116886 :     for(
     333     8116886 :         fd_prog_recm_iter_t iter = fd_prog_recm_iter( cache->rec.map, chain_idx );
     334     8121336 :         !fd_prog_recm_iter_done( iter );
     335     8116886 :     ) {
     336        4450 :       fd_progcache_rec_t * rec = fd_prog_recm_iter_ele( iter );
     337        4450 :       ulong next = fd_prog_recm_private_idx( rec->map_next );
     338             : 
     339        4450 :       fd_prog_recm_query_t rec_query[1];
     340        4450 :       int err = fd_prog_recm_remove( cache->rec.map, &rec->pair, NULL, rec_query, FD_MAP_FLAG_BLOCKING );
     341        4450 :       if( FD_UNLIKELY( err!=FD_MAP_SUCCESS ) ) FD_LOG_CRIT(( "fd_prog_recm_remove failed (%i-%s)", err, fd_map_strerror( err ) ));
     342        4450 :       if( FD_UNLIKELY( !fd_rwlock_trywrite( &rec->lock ) ) )
     343           0 :         FD_LOG_CRIT(( "fd_progcache_reset requires quiescence: record still read-locked" ));
     344        4450 :       fd_progcache_rec_release( cache, rec );
     345             : 
     346        4450 :       iter.ele_idx = next;
     347        4450 :     }
     348     8116886 :   }
     349       64043 : }
     350             : 
     351             : /* clear_txn_list does a depth-first traversal of the txn tree.
     352             :    Removes all txns. */
     353             : 
     354             : static void
     355             : clear_txn_list( fd_progcache_join_t * join,
     356      184492 :                 uint                  txn_head_idx ) {
     357      184492 :   ulong txn_max = fd_prog_txnp_max( join->txn.pool );
     358      304910 :   for( uint idx = txn_head_idx; idx!=UINT_MAX; ) {
     359      120418 :     if( FD_UNLIKELY( (ulong)idx >= txn_max ) )
     360           0 :       FD_LOG_CRIT(( "progcache: corruption detected (clear_txn_list txn_idx=%u txn_max=%lu)", idx, txn_max ));
     361      120418 :     fd_progcache_txn_t * txn = &join->txn.pool[ idx ];
     362      120418 :     uint next_idx  = txn->sibling_next_idx;
     363      120418 :     uint child_idx = txn->child_head_idx;
     364      120418 :     txn->rec_head_idx     = UINT_MAX;
     365      120418 :     txn->rec_tail_idx     = UINT_MAX;
     366      120418 :     txn->child_head_idx   = UINT_MAX;
     367      120418 :     txn->child_tail_idx   = UINT_MAX;
     368      120418 :     txn->parent_idx       = UINT_MAX;
     369      120418 :     txn->sibling_prev_idx = UINT_MAX;
     370      120418 :     txn->sibling_next_idx = UINT_MAX;
     371      120418 :     clear_txn_list( join, child_idx );
     372      120418 :     if( FD_UNLIKELY( !fd_prog_txnm_ele_remove( join->txn.map, &txn->xid, NULL, join->txn.pool ) ) ) FD_LOG_CRIT(( "fd_prog_txnm_ele_remove failed" ));
     373      120418 :     fd_prog_txnp_ele_release( join->txn.pool, txn );
     374      120418 :     idx = next_idx;
     375      120418 :   }
     376      184492 : }
     377             : 
     378             : void
     379       63994 : fd_progcache_reset( fd_progcache_join_t * cache ) {
     380             :   /* Zombies are not in the map, so reset_rec_map cannot see them.  Collect
     381             :      them first; one that survives the sweep is held by an active reader. */
     382       63994 :   fd_prog_reclaim_work( cache );
     383    11135401 :   for( ulong i=0UL; i<cache->rec.max; i++ ) {
     384    11071407 :     uchar st = __atomic_load_n( &cache->rec.ele[ i ].state, __ATOMIC_RELAXED );
     385    11071407 :     if( FD_UNLIKELY( ( st & ( FD_PROGCACHE_REC_LIVE|FD_PROGCACHE_REC_MAPPED ) )==FD_PROGCACHE_REC_LIVE ) )
     386           0 :       FD_LOG_CRIT(( "fd_progcache_reset requires quiescence: record %lu awaits collection (active readers?)", i ));
     387    11071407 :   }
     388       63994 :   if( FD_UNLIKELY( cache->shmem->spill.lock.value || cache->shmem->spill.rec_used || cache->shmem->spill.spad_used ) )
     389           0 :     FD_LOG_CRIT(( "fd_progcache_reset requires quiescence: spill in use" ));
     390       63994 :   clear_txn_list( cache, cache->shmem->txn.child_head_idx );
     391       63994 :   cache->shmem->txn.child_head_idx = UINT_MAX;
     392       63994 :   cache->shmem->txn.child_tail_idx = UINT_MAX;
     393       63994 :   reset_rec_map( cache );
     394       63994 :   cache->shmem->txn.root = fd_progcache_fork_id_initial();
     395       63994 : }
     396             : 
     397             : static int
     398             : fd_progcache_verify_siblings( fd_progcache_txn_t * pool,
     399             :                               ulong                txn_max,
     400             :                               uint                 head_idx,
     401             :                               uint                 tail_idx,
     402             :                               uint                 expected_parent_idx,
     403             :                               uint *               stack,
     404           0 :                               ulong *              stack_top ) {
     405             : 
     406           0 : # define TEST(c) do {                                                    \
     407           0 :     if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return -1; } \
     408           0 :   } while(0)
     409             : 
     410           0 :   TEST( (head_idx==UINT_MAX)==(tail_idx==UINT_MAX) );
     411             : 
     412           0 :   uint last_idx = UINT_MAX;
     413           0 :   for( uint idx = head_idx; idx!=UINT_MAX; ) {
     414           0 :     TEST( idx<txn_max );
     415           0 :     fd_progcache_txn_t * child = &pool[ idx ];
     416           0 :     TEST( !child->tag );
     417           0 :     TEST( child->parent_idx==expected_parent_idx );
     418           0 :     child->tag = 1;
     419           0 :     TEST( *stack_top<FD_PROGCACHE_DEPTH_MAX );
     420           0 :     stack[ (*stack_top)++ ] = idx;
     421           0 :     last_idx = idx;
     422           0 :     uint next_idx = child->sibling_next_idx;
     423           0 :     if( next_idx!=UINT_MAX ) {
     424           0 :       TEST( next_idx<txn_max );
     425           0 :       TEST( pool[ next_idx ].sibling_prev_idx==idx );
     426           0 :     }
     427           0 :     idx = next_idx;
     428           0 :   }
     429           0 :   TEST( last_idx==tail_idx );
     430             : 
     431           0 : # undef TEST
     432             : 
     433           0 :   return 0;
     434           0 : }
     435             : 
     436             : int
     437           0 : fd_progcache_verify( fd_progcache_join_t * join ) {
     438             : 
     439           0 : # define TEST(c) do {                                                    \
     440           0 :     if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return -1; } \
     441           0 :   } while(0)
     442             : 
     443           0 :   TEST( join );
     444             : 
     445           0 :   fd_progcache_shmem_t * shmem = join->shmem;
     446           0 :   TEST( shmem );
     447           0 :   TEST( shmem->magic==FD_PROGCACHE_SHMEM_MAGIC );
     448           0 :   TEST( shmem->wksp_tag );
     449             : 
     450           0 :   TEST( !fd_prog_recm_verify( join->rec.map ) );
     451             : 
     452           0 :   ulong rec_max = join->rec.max;
     453           0 :   fd_progcache_rec_t * rec0 = join->rec.ele;
     454             : 
     455           0 :   ulong txn_max = fd_prog_txnp_max( join->txn.pool );
     456           0 :   TEST( !fd_prog_txnm_verify( join->txn.map, txn_max, join->txn.pool ) );
     457             : 
     458           0 :   for( ulong i=0UL; i<txn_max; i++ ) join->txn.pool[ i ].tag = 0;
     459             : 
     460           0 :   uint  stack[ FD_PROGCACHE_DEPTH_MAX ];
     461           0 :   ulong stack_top = 0UL;
     462             : 
     463           0 :   TEST( !fd_progcache_verify_siblings( join->txn.pool, txn_max,
     464           0 :       shmem->txn.child_head_idx, shmem->txn.child_tail_idx,
     465           0 :       UINT_MAX, stack, &stack_top ) );
     466             : 
     467           0 :   while( stack_top ) {
     468           0 :     uint txn_idx = stack[ --stack_top ];
     469           0 :     fd_progcache_txn_t * txn = &join->txn.pool[ txn_idx ];
     470           0 :     TEST( !fd_progcache_verify_siblings( join->txn.pool, txn_max,
     471           0 :         txn->child_head_idx, txn->child_tail_idx,
     472           0 :         txn_idx, stack, &stack_top ) );
     473           0 :   }
     474             : 
     475           0 :   for( ulong i=0UL; i<txn_max; i++ ) {
     476           0 :     if( !join->txn.pool[ i ].tag ) continue;
     477           0 :     fd_progcache_txn_t * txn = &join->txn.pool[ i ];
     478             : 
     479           0 :     TEST( (txn->rec_head_idx==UINT_MAX)==(txn->rec_tail_idx==UINT_MAX) );
     480             : 
     481           0 :     ulong rec_cnt = 0UL;
     482           0 :     uint  prev    = UINT_MAX;
     483           0 :     for( uint idx = txn->rec_head_idx; idx!=UINT_MAX; ) {
     484           0 :       TEST( idx<rec_max );
     485           0 :       TEST( rec_cnt<rec_max ); /* cycle detection */
     486           0 :       fd_progcache_rec_t * rec = &rec0[ idx ];
     487           0 :       TEST( rec->prev_idx==prev );
     488           0 :       TEST( rec->exists );
     489           0 :       prev = idx;
     490           0 :       idx  = rec->next_idx;
     491           0 :       rec_cnt++;
     492           0 :     }
     493           0 :     TEST( prev==txn->rec_tail_idx );
     494           0 :   }
     495             : 
     496             :   /* A record is mapped, a zombie, free, or in flight -- never two. */
     497           0 :   ulong mapped_cnt = 0UL;
     498           0 :   ulong free_cnt   = 0UL;
     499             : 
     500           0 :   ulong chain_cnt = fd_prog_recm_chain_cnt( join->rec.map );
     501           0 :   for( ulong chain_idx=0UL; chain_idx<chain_cnt; chain_idx++ ) {
     502           0 :     for(
     503           0 :         fd_prog_recm_iter_t iter = fd_prog_recm_iter( join->rec.map, chain_idx );
     504           0 :         !fd_prog_recm_iter_done( iter );
     505           0 :         iter = fd_prog_recm_iter_next( iter )
     506           0 :     ) {
     507           0 :       fd_progcache_rec_t * rec = fd_prog_recm_iter_ele( iter );
     508           0 :       TEST( rec->exists );
     509             : 
     510             :       /* Verify state is LIVE for mapped records */
     511           0 :       ulong rec_idx = (ulong)( rec - rec0 );
     512           0 :       TEST( rec_idx<rec_max );
     513           0 :       uchar st = __atomic_load_n( &rec->state, __ATOMIC_RELAXED );
     514             :       /* Mapped means LIVE, or LOADING while its publisher finishes. */
     515           0 :       TEST( st & ( FD_PROGCACHE_REC_LIVE | FD_PROGCACHE_REC_LOADING ) );
     516             :       /* Detached means rooted, so the load is over. */
     517           0 :       if( atomic_load_explicit( &rec->txn_idx, memory_order_acquire )==UINT_MAX )
     518           0 :         TEST( st & FD_PROGCACHE_REC_LIVE );
     519           0 :       TEST( st & FD_PROGCACHE_REC_MAPPED );
     520           0 :       TEST( (ulong)rec->size_class==fd_progcache_rec_class( shmem, rec_idx ) );
     521           0 :       mapped_cnt++;
     522           0 :       TEST( rec->lock.value!=FD_RWLOCK_WRITE_LOCK ); /* push relies on this */
     523           0 :     }
     524           0 :   }
     525             : 
     526             :   /* A free record is write-locked, dead, and in its own class's list. */
     527           0 :   for( ulong c=0UL; c<FD_PROGCACHE_CACHE_CLASS_CNT; c++ ) {
     528           0 :     ulong base      = shmem->cache.rec_base [ c ];
     529           0 :     ulong class_max = shmem->cache.class_max[ c ];
     530             : 
     531           0 :     ulong cnt = 0UL;
     532           0 :     uint  idx = (uint)( shmem->cache.free_top[ c ].ver_top & (ulong)UINT_MAX );
     533           0 :     while( idx!=UINT_MAX ) {
     534           0 :       TEST( (ulong)idx>=base && (ulong)idx<base+class_max );
     535           0 :       fd_progcache_rec_t * rec = &rec0[ idx ];
     536           0 :       TEST( !rec->exists );
     537           0 :       TEST( rec->lock.value==FD_RWLOCK_WRITE_LOCK );
     538           0 :       TEST( !__atomic_load_n( &rec->state, __ATOMIC_RELAXED ) );
     539           0 :       TEST( cnt<class_max ); /* cycle detection */
     540           0 :       cnt++;
     541           0 :       idx = rec->free_next;
     542           0 :     }
     543           0 :     TEST( cnt<=class_max );
     544           0 :     TEST( cnt==shmem->cache.free_cnt[ c ].val );
     545           0 :     free_cnt += cnt;
     546           0 :   }
     547             : 
     548           0 :   TEST( mapped_cnt+free_cnt<=rec_max );
     549             : 
     550           0 : # undef TEST
     551             : 
     552           0 :   return 0;
     553           0 : }

Generated by: LCOV version 1.14