LCOV - code coverage report
Current view: top level - discof/backup - fd_backup_cache.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 205 0.0 %
Date: 2026-11-26 02:16:43 Functions: 0 6 0.0 %

          Line data    Source code
       1             : #include "fd_backup_cache.h"
       2             : #include "fd_backup.h"
       3             : 
       4             : fd_backup_cache_t *
       5             : fd_backup_cache_init( fd_backup_cache_t *        backup,
       6             :                       uchar const * const        cache    [ FD_ACCDB_CACHE_CLASS_CNT ],
       7             :                       ulong const                cache_max[ FD_ACCDB_CACHE_CLASS_CNT ],
       8           0 :                       fd_backup_accidx_t const * idx ) {
       9           0 :   FD_TEST( idx->epoch_slot );
      10           0 :   FD_TEST( idx->epoch      );
      11           0 :   *backup = (fd_backup_cache_t) {
      12           0 :     .idx         = *idx,
      13           0 :     .cache_class = 0UL,
      14           0 :     .cache_idx   = 0UL
      15           0 :   };
      16           0 :   backup->idx.root_generation = 0U;
      17           0 :   FD_VOLATILE( *idx->epoch_slot ) = ULONG_MAX; /* idle until first section */
      18           0 :   for( ulong i=0UL; i<FD_ACCDB_CACHE_CLASS_CNT; i++ ) {
      19           0 :     backup->cache    [ i ] = cache    [ i ];
      20           0 :     backup->cache_max[ i ] = cache_max[ i ];
      21           0 :   }
      22           0 :   return backup;
      23           0 : }
      24             : 
      25             : fd_backup_cache_t *
      26             : fd_backup_cache_join( fd_backup_cache_t * backup,
      27             :                       fd_accdb_shmem_t *  accdb,
      28           0 :                       ulong *             epoch_fseq ) {
      29           0 :   ulong max_live_slots = accdb->max_live_slots;
      30           0 :   ulong max_accounts   = accdb->max_accounts;
      31             : 
      32           0 :   ulong chain_cnt = fd_ulong_pow2_up( (max_accounts>>1) + (max_accounts&1UL) );
      33             : 
      34           0 :   FD_SCRATCH_ALLOC_INIT( l, accdb );
      35           0 :   /*                       */FD_SCRATCH_ALLOC_APPEND( l, FD_ACCDB_SHMEM_ALIGN,           sizeof(fd_accdb_shmem_t)                                );
      36           0 :   /*                       */FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_accdb_fork_shmem_t), max_live_slots*sizeof(fd_accdb_fork_shmem_t)            );
      37           0 :   /*                       */FD_SCRATCH_ALLOC_APPEND( l, descends_set_align(),           max_live_slots*descends_set_footprint( max_live_slots ) );
      38           0 :   void * _acc_map          = FD_SCRATCH_ALLOC_APPEND( l, alignof(uint),                  chain_cnt*sizeof(uint)                                  );
      39           0 :   void * _acc_pool_ele     = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_accdb_accmeta_t),    max_accounts*sizeof(fd_accdb_accmeta_t)             );
      40             : 
      41           0 :   uchar const * cache[ FD_ACCDB_CACHE_CLASS_CNT ];
      42           0 :   for( ulong c=0UL; c<FD_ACCDB_CACHE_CLASS_CNT; c++ ) {
      43           0 :     cache[ c ] = (uchar const *)accdb + accdb->cache_region_off[ c ];
      44           0 :   }
      45             : 
      46           0 :   fd_backup_accidx_t idx = {
      47           0 :     .acc_map      = _acc_map,
      48           0 :     .acc_pool     = _acc_pool_ele,
      49           0 :     .max_accounts = max_accounts,
      50           0 :     .seed         = accdb->seed,
      51           0 :     .chain_mask   = (uint)( chain_cnt-1UL ),
      52           0 :     .epoch_slot   = epoch_fseq,
      53           0 :     .epoch        = &accdb->epoch
      54           0 :   };
      55             : 
      56           0 :   return fd_backup_cache_init( backup, cache, accdb->cache_class_max, &idx );
      57           0 : }
      58             : 
      59             : static inline fd_accdb_cache_line_t *
      60             : cache_line( fd_backup_cache_t * backup,
      61             :             ulong               cls,
      62           0 :             ulong               idx ) {
      63           0 :   return (fd_accdb_cache_line_t *)( backup->cache[ cls ] + idx * fd_accdb_cache_slot_sz[ cls ] );
      64           0 : }
      65             : 
      66             : 
      67             : /* filter_batch is called with arbitrary acc_idx found in cache.
      68             :    Filter out acc_idx that:
      69             :    - are not rooted (generation too new)
      70             :    - have been freed since then (not visible in map) -- these are
      71             :      guaranteed to have not been rooted, since rooted acc_idx are stable
      72             :      while compaction and advance_root is disabled */
      73             : 
      74             : #define SET_NAME found_set
      75             : #define SET_MAX  FD_BACKUP_CACHE_PARA
      76             : #include "../../util/tmpl/fd_set.c"
      77             : 
      78             : static void
      79             : filter_batch( fd_backup_cache_t * backup,
      80           0 :               fd_backup_cache_msg_t * frag ) {
      81           0 :   fd_backup_accidx_t const * idx      = &backup->idx;
      82           0 :   fd_accdb_accmeta_t const * acc_pool = idx->acc_pool;
      83             : 
      84             :   /* filter out non-rooted accounts and tombstones */
      85           0 :   static fd_accdb_accmeta_t const dead_meta = { .key = { .generation = UINT_MAX } };
      86           0 :   for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
      87           0 :     uint acc_idx = frag->acc_idx[ i ];
      88           0 :     fd_accdb_accmeta_t const * m = acc_idx!=UINT_MAX ? &acc_pool[ acc_idx ] : &dead_meta;
      89           0 :     int keep = fd_backup_accidx_rooted( idx,
      90           0 :                                         FD_VOLATILE_CONST( m->key.generation ),
      91           0 :                                         FD_VOLATILE_CONST( m->lamports       ) );
      92           0 :     fd_uint_store_if( !keep, &frag->acc_idx[ i ], UINT_MAX );
      93           0 :   }
      94             : 
      95             :   /* filter out invisible accounts */
      96           0 :   found_set_t found[ found_set_word_cnt ];
      97           0 :   found_set_new( found );
      98             : 
      99           0 :   uint head[ FD_BACKUP_CACHE_PARA ];
     100           0 :   for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     101           0 :     head[ i ] = frag->acc_idx[ i ]!=UINT_MAX ? idx->acc_map[ backup->chain_idx[ i ] ] : UINT_MAX;
     102           0 :   }
     103             : 
     104             :   /* sentinel to assist with branchless code */
     105           0 :   static fd_accdb_accmeta_t const dead = {
     106           0 :     .map = { .next = UINT_MAX }
     107           0 :   };
     108             : 
     109             :   /* parallel walk map chains */
     110           0 :   for(;;) {
     111             : 
     112             :     /* check for matches */
     113           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     114           0 :       found_set_insert_if( found, frag->acc_idx[ i ]==head[ i ], i );
     115           0 :     }
     116             : 
     117             :     /* convert acc_idx to pointers */
     118           0 :     fd_accdb_accmeta_t const * gather[ FD_BACKUP_CACHE_PARA ];
     119           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     120           0 :       uint acc_idx = head[ i ];
     121           0 :       FD_DCHECK_CRIT( fd_backup_accidx_valid( idx, acc_idx ) || acc_idx==UINT_MAX, "acc_idx out of bounds" );
     122           0 :       gather[ i ] = acc_idx!=UINT_MAX ? &acc_pool[ acc_idx ] : &dead;
     123           0 :     }
     124             : 
     125             :     /* wide gather */
     126           0 :     fd_accdb_accmeta_t * meta = backup->meta;
     127           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     128           0 :       meta[ i ] = *gather[ i ];
     129           0 :     }
     130             : 
     131             :     /* next: load next (the gather above), check the live node's
     132             :        unlinked bit, then use next.  An unlinked node's next may be a
     133             :        deferred stack link, so that lane restarts from its chain head. */
     134           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     135           0 :       uint next = meta[ i ].map.next;
     136           0 :       head[ i ] = fd_accdb_acc_unlinked( gather[ i ] ) ? FD_VOLATILE_CONST( idx->acc_map[ backup->chain_idx[ i ] ] ) : next;
     137           0 :     }
     138             : 
     139             :     /* done? */
     140           0 :     int done = 1;
     141           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     142           0 :       if( head[ i ]!=UINT_MAX ) done = 0;
     143           0 :     }
     144           0 :     if( done ) break;
     145             : 
     146           0 :   }
     147             : 
     148             :   /* filter out dead elements */
     149           0 :   for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     150           0 :     if( FD_UNLIKELY( !found_set_test( found, i ) ) ) {
     151           0 :       frag->acc_idx[ i ] = UINT_MAX;
     152           0 :       memset( frag->pubkey[ i ].uc, 0, sizeof(fd_pubkey_t) );
     153           0 :     }
     154           0 :   }
     155             : 
     156           0 : }
     157             : 
     158             : fd_backup_cache_msg_t *
     159             : fd_backup_cache_scan( fd_backup_cache_t * backup,
     160           0 :                       fd_backup_cache_msg_t * frag ) {
     161           0 :   fd_backup_accidx_t * accidx = &backup->idx;
     162             : 
     163           0 :   ulong cls = backup->cache_class;
     164           0 :   if( FD_UNLIKELY( cls >= FD_ACCDB_CACHE_CLASS_CNT ) ) {
     165           0 :     return NULL;
     166           0 :   }
     167             : 
     168           0 :   FD_COMPILER_MFENCE();
     169           0 :   FD_VOLATILE( *accidx->epoch_slot ) = FD_VOLATILE_CONST( *accidx->epoch );
     170           0 :   FD_HW_MFENCE();
     171             : 
     172             :   /* Scan through cache lines (sequentially)
     173             :      This discovers any cached account (rooted or not), therefore may
     174             :      produce account indices that become invalid.  These are filtered
     175             :      out below. */
     176             : 
     177           0 :   uint line_cidx[ FD_BACKUP_CACHE_PARA ];
     178           0 :   long rem = (long)backup->cache_max[ cls ] - (long)backup->cache_idx;
     179           0 :   ulong idx = backup->cache_idx;
     180           0 :   if( FD_LIKELY( rem >= (long)FD_BACKUP_CACHE_PARA ) ) {
     181             :     /* fast path */
     182           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++, idx++ ) {
     183           0 :       frag->acc_idx[ i ] = FD_VOLATILE_CONST( cache_line( backup, cls, idx )->acc_idx );
     184           0 :       line_cidx[ i ]     = FD_ACCDB_ACC_CIDX_PACK( (uint)cls, (uint)idx );
     185           0 :     }
     186           0 :   } else {
     187             :     /* slow path */
     188           0 :     for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     189           0 :       frag->acc_idx[ i ] = UINT_MAX;
     190           0 :       memset( frag->pubkey[ i ].uc, 0, sizeof(fd_pubkey_t) );
     191           0 :       backup->chain_idx[ i ] = UINT_MAX; /* deliberately not ULONG_MAX */
     192           0 :     }
     193           0 :     for( ulong i=0UL; rem--; i++, idx++ ) {
     194           0 :       frag->acc_idx[ i ] = FD_VOLATILE_CONST( cache_line( backup, cls, idx )->acc_idx );
     195           0 :       line_cidx[ i ]     = FD_ACCDB_ACC_CIDX_PACK( (uint)cls, (uint)idx );
     196           0 :     }
     197           0 :     if( FD_UNLIKELY( idx >= backup->cache_max[ cls ] ) ) {
     198           0 :       backup->cache_class++;
     199           0 :       idx = 0UL;
     200           0 :     }
     201           0 :   }
     202           0 :   backup->cache_idx = idx;
     203             : 
     204             :   /* Keep a line only if its account is bound to it.  A line keeps a
     205             :      stale acc_idx once free or orphaned (it names a version only while
     206             :      that version's data word names the line), so this drops free lines
     207             :      and duplicates.  Pubkeys come from the accmeta. */
     208             : 
     209           0 :   for( ulong i=0UL; i<FD_BACKUP_CACHE_PARA; i++ ) {
     210           0 :     uint a = frag->acc_idx[ i ];
     211           0 :     if( fd_backup_accidx_valid( accidx, a ) ) {
     212           0 :       fd_accdb_accmeta_t const * m = &accidx->acc_pool[ a ];
     213           0 :       ulong d = FD_VOLATILE_CONST( m->data );
     214           0 :       if( FD_ACCDB_SIZE_CACHE_VALID( FD_ACCDB_DATA_ES( d ) ) && FD_ACCDB_DATA_CIDX( d )==line_cidx[ i ] ) {
     215           0 :         fd_memcpy( frag->pubkey[ i ].uc, m->key.pubkey, sizeof(fd_pubkey_t) );
     216           0 :         backup->chain_idx[ i ] = fd_backup_accidx_chain( accidx, frag->pubkey[ i ].uc );
     217           0 :         continue;
     218           0 :       }
     219           0 :     }
     220           0 :     frag->acc_idx[ i ] = UINT_MAX;
     221           0 :     memset( frag->pubkey[ i ].uc, 0, sizeof(fd_pubkey_t) );
     222           0 :     backup->chain_idx[ i ] = UINT_MAX;
     223           0 :   }
     224             : 
     225             :   /* Filter out freed/invisible and non-rooted accounts */
     226             : 
     227           0 :   filter_batch( backup, frag );
     228             : 
     229           0 :   FD_COMPILER_MFENCE();
     230           0 :   FD_VOLATILE( *accidx->epoch_slot ) = ULONG_MAX;
     231           0 :   return frag;
     232           0 : }
     233             : 
     234             : int
     235             : fd_backup_cache_read( fd_backup_cache_t * ctx,
     236             :                       fd_pubkey_t const * pubkey,
     237             :                       uint                acc_idx,
     238             :                       uchar *             out,
     239             :                       ulong *             out_sz,
     240           0 :                       ulong               out_max ) {
     241           0 :   FD_TEST( pubkey );
     242             : 
     243           0 :   fd_backup_accidx_t const * accidx = &ctx->idx;
     244             : 
     245           0 :   FD_DCHECK_CRIT( FD_VOLATILE_CONST( *accidx->epoch_slot )!=ULONG_MAX, "caller must publish epoch" );
     246             : 
     247           0 :   if( FD_UNLIKELY( !fd_backup_accidx_valid( accidx, acc_idx ) ) ) {
     248           0 :     return FD_BACKUP_CACHE_ERR_MISS;
     249           0 :   }
     250             : 
     251           0 :   if( FD_UNLIKELY( *out_sz + sizeof(snap_acc_hdr_t) > out_max ) ) {
     252           0 :     return FD_BACKUP_CACHE_ERR_SPACE;
     253           0 :   }
     254             : 
     255             :   /* This is a partial copy of fd_accdb_read_one_nocache */
     256           0 :   fd_accdb_accmeta_t const * accmeta = &accidx->acc_pool[ acc_idx ];
     257           0 :   if( FD_UNLIKELY( memcmp( accmeta->key.pubkey, pubkey->uc, sizeof(fd_pubkey_t) ) ) ) {
     258           0 :     return FD_BACKUP_CACHE_ERR_MISS;
     259           0 :   }
     260             : 
     261             :   /// STEP 1.
     262             :   ///   Walk the hash chain at acc_map[hash(pubkey)] using the same
     263             :   ///   visibility test as fd_accdb_acquire_inner.  See that function
     264             :   ///   for the detailed safety argument under concurrent prepend.
     265           0 :   ulong chain_idx = fd_backup_accidx_chain( accidx, pubkey->uc );
     266           0 :   uint acc_idx2 = FD_VOLATILE_CONST( accidx->acc_map[ chain_idx ] );
     267           0 :   _Bool found = 0;
     268           0 :   while( acc_idx2!=UINT_MAX ) {
     269           0 :     FD_DCHECK_CRIT( fd_backup_accidx_valid( accidx, acc_idx2 ), "acc_idx out of bounds" );
     270           0 :     fd_accdb_accmeta_t const * candidate = &accidx->acc_pool[ acc_idx2 ];
     271           0 :     found |= acc_idx==acc_idx2;
     272           0 :     uint next = FD_VOLATILE_CONST( candidate->map.next );
     273           0 :     if( FD_UNLIKELY( fd_accdb_acc_unlinked( candidate ) ) ) {
     274             :       /* next may be on the deferred stack: rewalk */
     275           0 :       found    = 0;
     276           0 :       acc_idx2 = FD_VOLATILE_CONST( accidx->acc_map[ chain_idx ] );
     277           0 :       continue;
     278           0 :     }
     279           0 :     acc_idx2 = next;
     280           0 :   }
     281           0 :   if( !found ) return FD_BACKUP_CACHE_ERR_MISS;
     282             : 
     283             :   /// STEP 2.
     284             :   ///   Snapshot acc fields.  The acc element's metadata is effectively
     285             :   ///   immutable from the perspective of cross-fork readers (see the
     286             :   ///   comment block in fd_accdb.h about cross-fork reads). */
     287           0 :   ulong snap_data     = FD_VOLATILE_CONST( accmeta->data );  /* one load: VALID and the line agree */
     288           0 :   uint  snap_es       = FD_ACCDB_DATA_ES( snap_data );
     289           0 :   ulong snap_lamports = accmeta->lamports;
     290           0 :   ulong data_len      = (ulong)FD_ACCDB_SIZE_DATA( snap_es );
     291           0 :   int   executable    = FD_ACCDB_SIZE_EXEC( snap_es );
     292           0 :   ulong rec_sz        = sizeof(snap_acc_hdr_t) + fd_ulong_align_up( data_len, 8UL );
     293           0 :   ulong data_pad      = fd_ulong_align_up( data_len, 8UL ) - data_len;
     294           0 :   if( FD_UNLIKELY( *out_sz + rec_sz > out_max ) ) {
     295           0 :     return FD_BACKUP_CACHE_ERR_SPACE;
     296           0 :   }
     297             : 
     298             :   /// STEP 3.
     299             :   ///    Copy from the cache without holding the line; the copy is good
     300             :   ///    if the line's seq was even and unchanged and the account stayed
     301             :   ///    bound to it (fd_accdb_line_read_unheld).  Else miss.
     302           0 :   if( !FD_ACCDB_SIZE_CACHE_VALID( snap_es ) ) return FD_BACKUP_CACHE_ERR_MISS;
     303           0 :   uint cidx = FD_ACCDB_DATA_CIDX( snap_data );
     304           0 :   fd_accdb_cache_line_t const * line = cache_line( ctx, FD_ACCDB_ACC_CIDX_CLASS( cidx ), FD_ACCDB_ACC_CIDX_IDX( cidx ) );
     305             : 
     306           0 :   snap_acc_hdr_t * hdr = (snap_acc_hdr_t *)( out + *out_sz );
     307           0 :   memset( hdr, 0, sizeof(snap_acc_hdr_t) );
     308           0 :   fd_memcpy( hdr->pubkey.uc, pubkey->uc, sizeof(fd_pubkey_t) );
     309           0 :   uchar * d = (uchar *)hdr + sizeof(snap_acc_hdr_t);
     310           0 :   FD_COMPILER_MFENCE();  /* the word before the line */
     311           0 :   if( FD_UNLIKELY( !fd_accdb_line_read_unheld( line, &accmeta->data, snap_data, hdr->owner.uc, d, data_len ) ) ) return FD_BACKUP_CACHE_ERR_MISS;
     312           0 :   if( data_pad ) memset( d + data_len, 0, data_pad );
     313             : 
     314           0 :   hdr->lamports   = snap_lamports;
     315           0 :   hdr->executable = !!executable;
     316           0 :   hdr->data_len   = data_len;
     317           0 :   *out_sz += rec_sz;
     318           0 :   return FD_BACKUP_CACHE_SUCCESS;
     319           0 : }

Generated by: LCOV version 1.14