LCOV - code coverage report
Current view: top level - funk - fd_funk.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 244 278 87.8 %
Date: 2025-11-06 04:46:49 Functions: 8 8 100.0 %

          Line data    Source code
       1             : #include "fd_funk.h"
       2             : #include "fd_funk_base.h"
       3             : #include <stdio.h>
       4             : 
       5             : ulong
       6         585 : fd_funk_align( void ) {
       7         585 :   return FD_FUNK_ALIGN;
       8         585 : }
       9             : 
      10             : ulong
      11             : fd_funk_footprint( ulong txn_max,
      12         231 :                    ulong rec_max ) {
      13         231 :   if( FD_UNLIKELY( rec_max>UINT_MAX ) ) return 0UL;
      14             : 
      15         231 :   ulong l = FD_LAYOUT_INIT;
      16             : 
      17         231 :   l = FD_LAYOUT_APPEND( l, alignof(fd_funk_shmem_t), sizeof(fd_funk_shmem_t) );
      18             : 
      19         231 :   ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
      20         231 :   l = FD_LAYOUT_APPEND( l, fd_funk_txn_map_align(), fd_funk_txn_map_footprint( txn_chain_cnt ) );
      21         231 :   l = FD_LAYOUT_APPEND( l, fd_funk_txn_pool_align(), fd_funk_txn_pool_footprint() );
      22         231 :   l = FD_LAYOUT_APPEND( l, alignof(fd_funk_txn_t), sizeof(fd_funk_txn_t) * txn_max );
      23             : 
      24         231 :   ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
      25         231 :   l = FD_LAYOUT_APPEND( l, fd_funk_rec_map_align(), fd_funk_rec_map_footprint( rec_chain_cnt ) );
      26         231 :   l = FD_LAYOUT_APPEND( l, fd_funk_rec_pool_align(), fd_funk_rec_pool_footprint() );
      27         231 :   l = FD_LAYOUT_APPEND( l, alignof(fd_funk_rec_t), sizeof(fd_funk_rec_t) * rec_max );
      28             : 
      29         231 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
      30             : 
      31         231 :   return l;
      32         231 : }
      33             : 
      34             : /* TODO: Consider letter user just passing a join of alloc to use,
      35             :    inferring the backing wksp and cgroup_hint from that and then
      36             :    allocating exclusively from that? */
      37             : 
      38             : void *
      39             : fd_funk_new( void * shmem,
      40             :              ulong  wksp_tag,
      41             :              ulong  seed,
      42             :              ulong  txn_max,
      43         129 :              ulong  rec_max ) {
      44         129 :   fd_funk_shmem_t * funk = shmem;
      45         129 :   fd_wksp_t *       wksp = fd_wksp_containing( funk );
      46             : 
      47         129 :   if( FD_UNLIKELY( !funk ) ) {
      48           3 :     FD_LOG_WARNING(( "NULL funk" ));
      49           3 :     return NULL;
      50           3 :   }
      51             : 
      52         126 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)funk, fd_funk_align() ) ) ) {
      53           3 :     FD_LOG_WARNING(( "misaligned funk" ));
      54           3 :     return NULL;
      55           3 :   }
      56             : 
      57         123 :   if( FD_UNLIKELY( !wksp_tag ) ) {
      58           3 :     FD_LOG_WARNING(( "bad wksp_tag" ));
      59           3 :     return NULL;
      60           3 :   }
      61             : 
      62         120 :   if( FD_UNLIKELY( !wksp ) ) {
      63           3 :     FD_LOG_WARNING(( "shmem must be part of a workspace" ));
      64           3 :     return NULL;
      65           3 :   }
      66             : 
      67         117 :   if( FD_UNLIKELY( txn_max>FD_FUNK_TXN_IDX_NULL ) ) { /* See note in fd_funk.h about this limit */
      68           3 :     FD_LOG_WARNING(( "txn_max too large for index compression" ));
      69           3 :     return NULL;
      70           3 :   }
      71             : 
      72         114 :   if( FD_UNLIKELY( rec_max>UINT_MAX ) ) {
      73           0 :     FD_LOG_WARNING(( "invalid rec_max" ));
      74           0 :     return NULL;
      75           0 :   }
      76             : 
      77         114 :   FD_SCRATCH_ALLOC_INIT( l, funk+1 );
      78             : 
      79         114 :   ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
      80         114 :   void * txn_map = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_txn_map_align(), fd_funk_txn_map_footprint( txn_chain_cnt ) );
      81         114 :   void * txn_pool = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_txn_pool_align(), fd_funk_txn_pool_footprint() );
      82         114 :   fd_funk_txn_t * txn_ele = (fd_funk_txn_t *)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_funk_txn_t), sizeof(fd_funk_txn_t) * txn_max );
      83             : 
      84           0 :   ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
      85         114 :   void * rec_map = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_rec_map_align(), fd_funk_rec_map_footprint( rec_chain_cnt ) );
      86         114 :   void * rec_pool = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_rec_pool_align(), fd_funk_rec_pool_footprint() );
      87         114 :   fd_funk_rec_t * rec_ele = (fd_funk_rec_t *)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_funk_rec_t), sizeof(fd_funk_rec_t) * rec_max );
      88             : 
      89         114 :   void * alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
      90             : 
      91         114 :   FD_TEST( _l == (ulong)funk + fd_funk_footprint( txn_max, rec_max ) );
      92             : 
      93         114 :   fd_memset( funk, 0, sizeof(fd_funk_shmem_t) );
      94             : 
      95         114 :   funk->funk_gaddr = fd_wksp_gaddr_fast( wksp, funk );
      96         114 :   funk->wksp_tag   = wksp_tag;
      97         114 :   funk->seed       = seed;
      98         114 :   funk->cycle_tag  = 3UL; /* various verify functions use tags 0-2 */
      99             : 
     100         114 :   funk->txn_map_gaddr = fd_wksp_gaddr_fast( wksp, fd_funk_txn_map_new( txn_map, txn_chain_cnt, seed ) );
     101         114 :   void * txn_pool2 = fd_funk_txn_pool_new( txn_pool );
     102         114 :   funk->txn_pool_gaddr = fd_wksp_gaddr_fast( wksp, txn_pool2 );
     103         114 :   fd_funk_txn_pool_t txn_join[1];
     104         114 :   fd_funk_txn_pool_join( txn_join, txn_pool2, txn_ele, txn_max );
     105         114 :   fd_funk_txn_pool_reset( txn_join, 0UL );
     106         114 :   funk->txn_ele_gaddr = fd_wksp_gaddr_fast( wksp, txn_ele );
     107         114 :   funk->txn_max = txn_max;
     108         114 :   funk->child_head_cidx = fd_funk_txn_cidx( FD_FUNK_TXN_IDX_NULL );
     109         114 :   funk->child_tail_cidx = fd_funk_txn_cidx( FD_FUNK_TXN_IDX_NULL );
     110             : 
     111     1574532 :   for( ulong i=0UL; i<txn_max; i++ ) {
     112     1574418 :     fd_rwlock_new( txn_join->ele[ i ].lock );
     113     1574418 :     txn_join->ele[ i ].state = FD_FUNK_TXN_STATE_FREE;
     114     1574418 :   }
     115             : 
     116         114 :   fd_funk_txn_xid_set_root( funk->root         );
     117         114 :   fd_funk_txn_xid_set_root( funk->last_publish );
     118             : 
     119         114 :   funk->rec_map_gaddr = fd_wksp_gaddr_fast( wksp, fd_funk_rec_map_new( rec_map, rec_chain_cnt, seed ) );
     120         114 :   void * rec_pool2 = fd_funk_rec_pool_new( rec_pool );
     121         114 :   funk->rec_pool_gaddr = fd_wksp_gaddr_fast( wksp, rec_pool2 );
     122         114 :   fd_funk_rec_pool_t rec_join[1];
     123         114 :   fd_funk_rec_pool_join( rec_join, rec_pool2, rec_ele, rec_max );
     124         114 :   fd_funk_rec_pool_reset( rec_join, 0UL );
     125         114 :   funk->rec_ele_gaddr = fd_wksp_gaddr_fast( wksp, rec_ele );
     126         114 :   funk->rec_max = (uint)rec_max;
     127             : 
     128         114 :   funk->alloc_gaddr = fd_wksp_gaddr_fast( wksp, fd_alloc_join( fd_alloc_new( alloc, wksp_tag ), 0UL ) );
     129             : 
     130         114 :   FD_COMPILER_MFENCE();
     131         114 :   FD_VOLATILE( funk->magic ) = FD_FUNK_MAGIC;
     132         114 :   FD_COMPILER_MFENCE();
     133             : 
     134         114 :   return (void *)funk;
     135         114 : }
     136             : 
     137             : fd_funk_t *
     138             : fd_funk_join( fd_funk_t * ljoin,
     139         231 :               void *      shfunk ) {
     140         231 :   if( FD_UNLIKELY( !shfunk ) ) {
     141           3 :     FD_LOG_WARNING(( "NULL shfunk" ));
     142           3 :     return NULL;
     143           3 :   }
     144             : 
     145         228 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
     146           3 :     FD_LOG_WARNING(( "misaligned shfunk" ));
     147           3 :     return NULL;
     148           3 :   }
     149             : 
     150         225 :   fd_wksp_t * wksp = fd_wksp_containing( shfunk );
     151         225 :   if( FD_UNLIKELY( !wksp ) ) {
     152           3 :     FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
     153           3 :     return NULL;
     154           3 :   }
     155             : 
     156         222 :   fd_funk_shmem_t * shmem = shfunk;
     157         222 :   if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
     158           3 :     if (shmem->magic == FD_FUNK_MAGIC+1) {
     159           0 :       FD_LOG_WARNING(( "attempted to join a funk that crashed in a critical section" ));
     160           3 :     } else {
     161           3 :       FD_LOG_WARNING(( "bad magic" ));
     162           3 :     }
     163           3 :     return NULL;
     164           3 :   }
     165             : 
     166         219 :   if( FD_UNLIKELY( !ljoin ) ) {
     167           0 :     FD_LOG_WARNING(( "NULL ljoin" ));
     168           0 :     return NULL;
     169           0 :   }
     170             : 
     171             : #ifdef FD_FUNK_WKSP_PROTECT
     172             :   fd_wksp_mprotect( wksp, 1 );
     173             : #endif
     174             : 
     175         219 :   fd_funk_t * funk = ljoin;
     176         219 :   memset( funk, 0, sizeof(fd_funk_t) );
     177             : 
     178         219 :   funk->shmem = shfunk;
     179         219 :   funk->wksp  = wksp;
     180             : 
     181         219 :   if( FD_UNLIKELY( !fd_funk_txn_pool_join( funk->txn_pool, fd_wksp_laddr( wksp, shmem->txn_pool_gaddr ), fd_wksp_laddr( wksp, shmem->txn_ele_gaddr ), shmem->txn_max ) ) ) {
     182           0 :     FD_LOG_WARNING(( "failed to join txn_pool" ));
     183           0 :     return NULL;
     184           0 :   }
     185         219 :   if( FD_UNLIKELY( !fd_funk_txn_map_join( funk->txn_map, fd_wksp_laddr( wksp, shmem->txn_map_gaddr ), fd_wksp_laddr_fast( wksp, shmem->txn_ele_gaddr ), shmem->txn_max ) ) ) {
     186           0 :     FD_LOG_WARNING(( "failed to join txn_map" ));
     187           0 :     return NULL;
     188           0 :   }
     189         219 :   if( FD_UNLIKELY( !fd_funk_rec_map_join( funk->rec_map, fd_wksp_laddr( wksp, shmem->rec_map_gaddr ), fd_wksp_laddr( wksp, shmem->rec_ele_gaddr ), shmem->rec_max ) ) ) {
     190           0 :     FD_LOG_WARNING(( "failed to join rec_map" ));
     191           0 :     return NULL;
     192           0 :   }
     193         219 :   if( FD_UNLIKELY( !fd_funk_rec_pool_join( funk->rec_pool, fd_wksp_laddr( wksp, shmem->rec_pool_gaddr ), fd_wksp_laddr_fast( wksp, shmem->rec_ele_gaddr ), shmem->rec_max ) ) ) {
     194           0 :     FD_LOG_WARNING(( "failed to join rec_pool" ));
     195           0 :     return NULL;
     196           0 :   }
     197         219 :   funk->alloc = fd_wksp_laddr( wksp, shmem->alloc_gaddr );
     198         219 :   if( FD_UNLIKELY( !fd_alloc_join( funk->alloc, fd_tile_idx() ) ) ) {
     199           0 :     FD_LOG_WARNING(( "failed to join funk alloc" ));
     200           0 :     return NULL;
     201           0 :   }
     202             : 
     203         219 :   return funk;
     204         219 : }
     205             : 
     206             : void *
     207             : fd_funk_leave( fd_funk_t * funk,
     208         168 :                void **     opt_shfunk ) {
     209             : 
     210         168 :   if( FD_UNLIKELY( !funk ) ) {
     211           3 :     FD_LOG_WARNING(( "NULL funk" ));
     212           3 :     if( opt_shfunk ) *opt_shfunk = NULL;
     213           3 :     return NULL;
     214           3 :   }
     215         165 :   void * shfunk = funk->shmem;
     216             : 
     217         165 :   memset( funk, 0, sizeof(fd_funk_t) );
     218             : 
     219         165 :   if( opt_shfunk ) *opt_shfunk = shfunk;
     220         165 :   return (void *)funk;
     221         168 : }
     222             : 
     223             : void *
     224         114 : fd_funk_delete( void * shfunk ) {
     225             : 
     226         114 :   if( FD_UNLIKELY( !shfunk ) ) {
     227           3 :     FD_LOG_WARNING(( "NULL shfunk" ));
     228           3 :     return NULL;
     229           3 :   }
     230             : 
     231         111 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
     232           3 :     FD_LOG_WARNING(( "misaligned shfunk" ));
     233           3 :     return NULL;
     234           3 :   }
     235             : 
     236         108 :   fd_wksp_t * wksp = fd_wksp_containing( shfunk );
     237         108 :   if( FD_UNLIKELY( !wksp ) ) {
     238           3 :     FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
     239           3 :     return NULL;
     240           3 :   }
     241             : 
     242         105 :   fd_funk_shmem_t * shmem = shfunk;
     243         105 :   if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
     244           6 :     FD_LOG_WARNING(( "bad magic" ));
     245           6 :     return NULL;
     246           6 :   }
     247             : 
     248             :   /* Free all fd_alloc allocations made, individually
     249             :      (FIXME consider walking the element pool instead of the map?) */
     250             : 
     251          99 :   fd_alloc_t * alloc = fd_alloc_join( fd_wksp_laddr_fast( wksp, shmem->alloc_gaddr ), fd_tile_idx() );
     252             : 
     253          99 :   void * shmap = fd_wksp_laddr_fast( wksp, shmem->rec_map_gaddr );
     254          99 :   void * shele = fd_wksp_laddr_fast( wksp, shmem->rec_ele_gaddr );
     255          99 :   fd_funk_rec_map_t rec_map[1];
     256          99 :   if( FD_UNLIKELY( !fd_funk_rec_map_join( rec_map, shmap, shele, 0UL ) ) ) {
     257           0 :     FD_LOG_ERR(( "failed to join rec_map (corrupt funk?)" ));
     258           0 :     return NULL;
     259           0 :   }
     260          99 :   ulong chain_cnt = fd_funk_rec_map_chain_cnt( rec_map );
     261      788649 :   for( ulong chain_idx=0UL; chain_idx<chain_cnt; chain_idx++ ) {
     262      788550 :     for(
     263      788550 :         fd_funk_rec_map_iter_t iter = fd_funk_rec_map_iter( rec_map, chain_idx );
     264      788568 :         !fd_funk_rec_map_iter_done( iter );
     265      788550 :         iter = fd_funk_rec_map_iter_next( iter )
     266      788550 :     ) {
     267          18 :       fd_funk_val_flush( fd_funk_rec_map_iter_ele( iter ), alloc, wksp );
     268          18 :     }
     269      788550 :   }
     270             : 
     271          99 :   fd_funk_rec_map_leave( rec_map );
     272             : 
     273             :   /* Free the fd_alloc instance */
     274             : 
     275          99 :   fd_wksp_free_laddr( fd_alloc_delete( fd_alloc_leave( alloc ) ) );
     276             : 
     277          99 :   FD_COMPILER_MFENCE();
     278          99 :   FD_VOLATILE( shmem->magic ) = 0UL;
     279          99 :   FD_COMPILER_MFENCE();
     280             : 
     281          99 :   return shmem;
     282          99 : }
     283             : 
     284             : void
     285           9 : fd_funk_delete_fast( void * shfunk ) {
     286             : 
     287           9 :   if( FD_UNLIKELY( !shfunk ) ) {
     288           0 :     FD_LOG_WARNING(( "NULL shfunk" ));
     289           0 :   }
     290             : 
     291           9 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
     292           0 :     FD_LOG_WARNING(( "misaligned shfunk" ));
     293           0 :   }
     294             : 
     295           9 :   fd_funk_shmem_t * shmem = shfunk;
     296           9 :   if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
     297           0 :     FD_LOG_WARNING(( "bad magic" ));
     298           0 :   }
     299             : 
     300           9 :   fd_wksp_t * wksp = fd_wksp_containing( shmem );
     301           9 :   if( FD_UNLIKELY( !wksp ) ) {
     302           0 :     FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
     303           0 :   }
     304             : 
     305           9 :   ulong const tags[1] = { shmem->wksp_tag };
     306           9 :   fd_wksp_tag_free( wksp, tags, 1UL );
     307             : 
     308           9 : }
     309             : 
     310             : int
     311         252 : fd_funk_verify( fd_funk_t * join ) {
     312         252 :   fd_funk_shmem_t * funk = join->shmem;
     313             : 
     314        6552 : # define TEST(c) do {                                                                           \
     315        6552 :     if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return FD_FUNK_ERR_INVAL; } \
     316        6552 :   } while(0)
     317             : 
     318         252 :   TEST( funk );
     319             : 
     320             :   /* Test metadata */
     321             : 
     322         252 :   TEST( funk->magic==FD_FUNK_MAGIC );
     323             : 
     324         252 :   ulong funk_gaddr = funk->funk_gaddr;
     325         252 :   TEST( funk_gaddr );
     326         252 :   fd_wksp_t * wksp = fd_funk_wksp( join );
     327         252 :   TEST( wksp );
     328         252 :   TEST( fd_wksp_laddr_fast( wksp, funk_gaddr )==(void *)funk );
     329         252 :   TEST( fd_wksp_gaddr_fast( wksp, funk       )==funk_gaddr   );
     330             : 
     331         252 :   ulong wksp_tag = fd_funk_wksp_tag( join );
     332         252 :   TEST( !!wksp_tag );
     333             : 
     334         252 :   ulong seed = funk->seed; /* seed can be anything */
     335             : 
     336         252 :   TEST( funk->cycle_tag>2UL );
     337             : 
     338             :   /* Test transaction map */
     339             : 
     340         252 :   ulong txn_max = fd_funk_txn_pool_ele_max( join->txn_pool );
     341         252 :   TEST( txn_max<=FD_FUNK_TXN_IDX_NULL );
     342             : 
     343         252 :   ulong txn_map_gaddr = funk->txn_map_gaddr;
     344         252 :   TEST( txn_map_gaddr );
     345         252 :   fd_funk_txn_map_t * txn_map = fd_funk_txn_map( join );
     346         252 :   ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
     347         252 :   TEST( txn_chain_cnt==fd_funk_txn_map_chain_cnt( txn_map ) );
     348         252 :   TEST( seed==fd_funk_txn_map_seed( txn_map ) );
     349             : 
     350         252 :   ulong child_head_idx = fd_funk_txn_idx( funk->child_head_cidx );
     351         252 :   ulong child_tail_idx = fd_funk_txn_idx( funk->child_tail_cidx );
     352             : 
     353         252 :   int null_child_head = fd_funk_txn_idx_is_null( child_head_idx );
     354         252 :   int null_child_tail = fd_funk_txn_idx_is_null( child_tail_idx );
     355             : 
     356         252 :   if( !txn_max ) TEST( null_child_head & null_child_tail );
     357         246 :   else {
     358         246 :     if( null_child_head ) TEST( null_child_tail );
     359         168 :     else                  TEST( child_head_idx<txn_max );
     360             : 
     361         246 :     if( null_child_tail ) TEST( null_child_head );
     362         168 :     else                  TEST( child_tail_idx<txn_max );
     363         246 :   }
     364             : 
     365         252 :   if( !txn_max ) TEST( fd_funk_txn_idx_is_null( child_tail_idx ) );
     366             : 
     367         252 :   fd_funk_txn_xid_t const * root = fd_funk_root( join );
     368         252 :   TEST( root ); /* Practically guaranteed */
     369         252 :   TEST( fd_funk_txn_xid_eq_root( root ) );
     370             : 
     371         252 :   fd_funk_txn_xid_t * last_publish = funk->last_publish;
     372         252 :   TEST( last_publish ); /* Practically guaranteed */
     373             :   /* (*last_publish) only be root at creation and anything but root post
     374             :      creation.  But we don't know which situation applies here so this
     375             :      could be anything. */
     376             : 
     377         252 :   TEST( !fd_funk_txn_verify( join ) );
     378             : 
     379             :   /* Test record map */
     380             : 
     381         252 :   ulong rec_max = fd_funk_rec_pool_ele_max( join->rec_pool );
     382         252 :   TEST( rec_max<=FD_FUNK_TXN_IDX_NULL );
     383             : 
     384         252 :   ulong rec_map_gaddr = funk->rec_map_gaddr;
     385         252 :   TEST( rec_map_gaddr );
     386         252 :   fd_funk_rec_map_t * rec_map = fd_funk_rec_map( join );
     387         252 :   ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
     388         252 :   TEST( rec_chain_cnt==fd_funk_rec_map_chain_cnt( rec_map ) );
     389         252 :   TEST( seed==fd_funk_rec_map_seed( rec_map ) );
     390             : 
     391         252 :   TEST( !fd_funk_rec_verify( join ) );
     392             : 
     393             :   /* Test values */
     394             : 
     395         252 :   ulong alloc_gaddr = funk->alloc_gaddr;
     396         252 :   TEST( alloc_gaddr );
     397         252 :   fd_alloc_t * alloc = fd_funk_alloc( join );
     398         252 :   TEST( alloc );
     399             : 
     400         252 :   TEST( !fd_funk_val_verify( join ) );
     401             : 
     402         252 : # undef TEST
     403             : 
     404         252 :   return FD_FUNK_SUCCESS;
     405         252 : }

Generated by: LCOV version 1.14