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

Generated by: LCOV version 1.14