LCOV - code coverage report
Current view: top level - util/wksp - fd_wksp_admin.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 134 416 32.2 %
Date: 2026-10-22 18:10:43 Functions: 8 19 42.1 %

          Line data    Source code
       1             : #include "fd_wksp_private.h"
       2             : 
       3             : int
       4         178 : fd_wksp_private_lock( fd_wksp_t * wksp ) {
       5             : # if FD_WKSP_LOCK_RECLAIM
       6             :   int   warning = 0;
       7             : #endif
       8         178 :   ulong me      = fd_log_group_id();
       9             : 
      10         178 :   ulong * _owner = &wksp->owner;
      11         178 :   for(;;) {
      12             : 
      13             :     /* Note that we emulate CAS on platforms without FD_HAS_ATOMIC
      14             :        to minimize the amount of code differences we have to test.  On
      15             :        platforms without FD_HAS_ATOMIC, a workspace should not be used
      16             :        concurrently though. */
      17             : 
      18         178 :     FD_COMPILER_MFENCE();
      19         178 : #   if FD_HAS_ATOMIC
      20         178 :     ulong pid = FD_ATOMIC_CAS( _owner, ULONG_MAX, me );
      21             : #   else
      22             :     ulong pid = FD_VOLATILE_CONST( *_owner );
      23             :     if( pid==ULONG_MAX ) FD_VOLATILE( *_owner ) = me;
      24             : #   endif
      25         178 :     FD_COMPILER_MFENCE();
      26             : 
      27         178 :     if( FD_LIKELY( pid==ULONG_MAX ) ) return FD_WKSP_SUCCESS;
      28             : 
      29             : # if FD_WKSP_LOCK_RECLAIM
      30             :     int status = fd_log_group_id_query( pid );
      31             :     if( FD_UNLIKELY( status==FD_LOG_GROUP_ID_QUERY_DEAD ) ) { /* A process died while holding the lock, try to recover the lock */
      32             : 
      33             :       FD_COMPILER_MFENCE();
      34             : #     if FD_HAS_ATOMIC
      35             :       ulong cur = FD_ATOMIC_CAS( _owner, pid, me );
      36             : #     else
      37             :       ulong cur = FD_VOLATILE_CONST( *_owner );
      38             :       if( cur==pid ) FD_VOLATILE( *_owner ) = me;
      39             : #     endif
      40             :       FD_COMPILER_MFENCE();
      41             : 
      42             :       if( FD_LIKELY( cur==pid ) ) { /* We recovered the lock from the dead pid, try to fix up incomplete ops */
      43             : 
      44             :         FD_LOG_WARNING(( "Process %lu died in an operation on wksp %s; verifying", pid, wksp->name ));
      45             :         if( FD_LIKELY( !fd_wksp_verify( wksp ) ) ) { /* logs details of issues detected */
      46             :           FD_LOG_NOTICE(( "wksp verified" ));
      47             :           return FD_WKSP_SUCCESS;
      48             :         }
      49             : 
      50             :         FD_LOG_WARNING(( "Issues detected; rebuilding" ));
      51             :         if( FD_UNLIKELY( fd_wksp_rebuild( wksp, wksp->seed ) ) ) { /* Rebuild failed (logs details of issues detected) */
      52             :           /* Return control of the lock to the previous owner */
      53             :           FD_COMPILER_MFENCE();
      54             :           FD_VOLATILE( *_owner ) = pid;
      55             :           FD_COMPILER_MFENCE();
      56             :           FD_LOG_WARNING(( "corrupt wksp detected" ));
      57             :           return FD_WKSP_ERR_CORRUPT;
      58             :         }
      59             : 
      60             :         FD_LOG_NOTICE(( "wksp rebuilt" ));
      61             :         return FD_WKSP_SUCCESS;
      62             : 
      63             :       }
      64             : 
      65             :       /* Somebody beat us to recovering the lock ... try again */
      66             : 
      67             :     } else if( FD_UNLIKELY( status!=FD_LOG_GROUP_ID_QUERY_LIVE ) ) { /* Unclear pid status ... issue a warning and try again */
      68             : 
      69             :       if( FD_UNLIKELY( !warning ) ) {
      70             :         FD_LOG_WARNING(( "wksp %s is owned by unknown pid %li; attempting to recover", wksp->name, pid ));
      71             :         warning = 1;
      72             :       }
      73             : 
      74             :     }
      75             : 
      76             :     /* At this point, either another thread in this process has the
      77             :        lock, another active thread in another process has the lock,
      78             :        another unknown status thread in other process has the lock or
      79             :        another thread beat us to reclaim the lock from a dead process.
      80             :        In any case, we don't have the lock.  Wait a while to limit O/S
      81             :        contention and try again. */
      82             : 
      83             :     FD_YIELD();
      84             : # else
      85             : 
      86             :     /* If we are running without FD_WKSP_LOCK_RECLAIM then it is assumed
      87             :        that the contention is caused by a tile pinned to another core,
      88             :        and that this core is itself pinned so spin locking is best. */
      89           0 :     FD_SPIN_PAUSE();
      90             : 
      91           0 : #endif
      92           0 :   }
      93             : 
      94             :   /* never get here */
      95         178 : }
      96             : 
      97             : /* Public APIs ********************************************************/
      98             : 
      99             : ulong
     100             : fd_wksp_part_max_est( ulong footprint,
     101           1 :                       ulong sz_typical ) {
     102           1 :   footprint       = fd_ulong_align_dn( footprint, FD_WKSP_ALIGN );
     103           1 :   ulong data_end  = footprint - 1UL;
     104           1 :   ulong pinfo_off = fd_wksp_private_pinfo_off();
     105           1 :   ulong consumed  = sizeof(fd_wksp_private_pinfo_t) + sz_typical;
     106           1 :   ulong part_max  = (data_end - pinfo_off) / (consumed + (ulong)!consumed); /* avoid div-by-zero */
     107           1 :   if( FD_UNLIKELY( (!footprint) | (!sz_typical) | (sz_typical>consumed) | (pinfo_off>data_end) ) ) return 0UL;
     108           1 :   return fd_ulong_min( part_max, FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     109           1 : }
     110             : 
     111             : ulong
     112             : fd_wksp_data_max_est( ulong footprint,
     113           0 :                       ulong part_max ) {
     114           0 :   footprint = fd_ulong_align_dn( footprint, FD_WKSP_ALIGN );
     115           0 :   ulong data_end  = footprint - 1UL;
     116           0 :   ulong data_off  = fd_wksp_private_data_off( part_max );
     117           0 :   if( FD_UNLIKELY( (!part_max) | (part_max>FD_WKSP_PRIVATE_PINFO_IDX_NULL) |
     118           0 :                    (part_max > ((ULONG_MAX - fd_wksp_private_pinfo_off())/sizeof(fd_wksp_private_pinfo_t))) | /* covered above */
     119           0 :                    (!footprint) | (data_off>=data_end) ) ) return 0UL;
     120           0 :   return data_end - data_off;
     121           0 : }
     122             : 
     123             : ulong
     124           0 : fd_wksp_align( void ) {
     125           0 :   return FD_WKSP_ALIGN;
     126           0 : }
     127             : 
     128             : ulong
     129             : fd_wksp_footprint( ulong part_max,
     130           4 :                    ulong data_max ) {
     131           4 :   ulong data_off = fd_wksp_private_data_off( part_max );
     132           4 :   if( FD_UNLIKELY( (!part_max) | (part_max>FD_WKSP_PRIVATE_PINFO_IDX_NULL) | (!data_max) |
     133           4 :                    (part_max > ((ULONG_MAX - fd_wksp_private_pinfo_off())/sizeof(fd_wksp_private_pinfo_t))) | /* Covered above */
     134           4 :                    (data_max > (ULONG_MAX - FD_WKSP_ALIGN + 1UL - data_off - 1UL)                         ) ) ) return 0UL;
     135           4 :   return fd_ulong_align_up( data_off + data_max + 1UL, FD_WKSP_ALIGN );
     136           4 : }
     137             : 
     138             : void *
     139             : fd_wksp_new( void *       shmem,
     140             :              char const * name,
     141             :              uint         seed,
     142             :              ulong        part_max,
     143           1 :              ulong        data_max ) {
     144           1 :   fd_wksp_t * wksp = (fd_wksp_t *)shmem;
     145             : 
     146           1 :   if( FD_UNLIKELY( !wksp ) ) {
     147           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     148           0 :     return NULL;
     149           0 :   }
     150             : 
     151           1 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)wksp, FD_WKSP_ALIGN ) ) ) {
     152           0 :     FD_LOG_WARNING(( "bad align" ));
     153           0 :     return NULL;
     154           0 :   }
     155             : 
     156           1 :   ulong name_len = fd_shmem_name_len( name );
     157           1 :   if( FD_UNLIKELY( !name_len ) ) {
     158           0 :     FD_LOG_WARNING(( "bad name" ));
     159           0 :     return NULL;
     160           0 :   }
     161             : 
     162           1 :   ulong footprint = fd_wksp_footprint( part_max, data_max );
     163           1 :   if( FD_UNLIKELY( !footprint ) ) {
     164           0 :     FD_LOG_WARNING(( "bad part_max and/or data_max" ));
     165           0 :     return NULL;
     166           0 :   }
     167             : 
     168           1 :   fd_memset( wksp, 0, fd_wksp_footprint( part_max, 1UL ) );
     169             : 
     170           1 :   wksp->part_max       = part_max;
     171           1 :   wksp->data_max       = data_max;
     172           1 :   wksp->gaddr_lo       = fd_wksp_private_data_off( part_max );
     173           1 :   wksp->gaddr_hi       = wksp->gaddr_lo + data_max;
     174           1 :   fd_memcpy( wksp->name, name, name_len+1UL );
     175           1 :   wksp->seed           = seed;
     176           1 :   wksp->idle_top_cidx  = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     177           1 :   wksp->part_head_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     178           1 :   wksp->part_tail_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     179           1 :   wksp->part_used_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     180           1 :   wksp->part_free_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     181           1 :   wksp->cycle_tag      = 4UL;  /* Verify uses tags 0-3 */
     182           1 :   wksp->owner          = 0UL;  /* Mark as locked and in construction */
     183             : 
     184             :   /* Note that wksp->owner was set to zero above, "locking" the wksp by
     185             :      group_id 0.  And the memset above set all the partition tags to
     186             :      zero such that there are no allocated partitions.  So once we set
     187             :      magic below, we can finish the initialization by rebuilding and
     188             :      unlocking.  Since fd_log_group_id is non-zero, the zero owner
     189             :      indicates to any remote observer of the shared memory region that
     190             :      the wksp is being built for the first time. */
     191             : 
     192           1 :   FD_COMPILER_MFENCE();
     193           1 :   FD_VOLATILE( wksp->magic ) = FD_WKSP_MAGIC;
     194           1 :   FD_COMPILER_MFENCE();
     195             : 
     196           1 :   int err = fd_wksp_rebuild( wksp, seed );
     197           1 :   if( FD_UNLIKELY( err ) ) { /* Should be impossible at this point */
     198             : 
     199           0 :     FD_COMPILER_MFENCE();
     200           0 :     FD_VOLATILE( wksp->magic ) = 0UL;
     201           0 :     FD_COMPILER_MFENCE();
     202             : 
     203           0 :     FD_LOG_WARNING(( "fd_wksp_rebuild failed (%i-%s)", err, fd_wksp_strerror( err ) ));
     204           0 :     return NULL;
     205           0 :   }
     206             : 
     207             :   #if FD_HAS_DEEPASAN
     208             :   fd_wksp_private_asan_poison_data( wksp );
     209             :   #endif
     210             : 
     211           1 :   fd_wksp_private_unlock( wksp );
     212             : 
     213           1 :   return wksp;
     214           1 : }
     215             : 
     216             : fd_wksp_t *
     217           1 : fd_wksp_join( void * shwksp ) {
     218           1 :   fd_wksp_t * wksp = (fd_wksp_t *)shwksp;
     219             : 
     220           1 :   if( FD_UNLIKELY( !wksp ) ) {
     221           0 :     FD_LOG_WARNING(( "NULL shwksp" ));
     222           0 :     return NULL;
     223           0 :   }
     224             : 
     225           1 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)wksp, FD_WKSP_ALIGN ) ) ) {
     226           0 :     FD_LOG_WARNING(( "bad align" ));
     227           0 :     return NULL;
     228           0 :   }
     229             : 
     230           1 :   if( FD_UNLIKELY( wksp->magic!=FD_WKSP_MAGIC ) ) {
     231           0 :     FD_LOG_WARNING(( "bad magic" ));
     232           0 :     return NULL;
     233           0 :   }
     234             : 
     235           1 :   return wksp;
     236           1 : }
     237             : 
     238             : void *
     239           1 : fd_wksp_leave( fd_wksp_t * wksp ) {
     240           1 :   if( FD_UNLIKELY( !wksp ) ) {
     241           0 :     FD_LOG_WARNING(( "NULL wksp" ));
     242           0 :     return NULL;
     243           0 :   }
     244             : 
     245           1 :   return (void *)wksp;
     246           1 : }
     247             : 
     248             : void *
     249           1 : fd_wksp_delete( void * shwksp ) {
     250           1 :   fd_wksp_t * wksp = (fd_wksp_t *)shwksp;
     251             : 
     252           1 :   if( FD_UNLIKELY( !wksp ) ) {
     253           0 :     FD_LOG_WARNING(( "NULL shwksp" ));
     254           0 :     return NULL;
     255           0 :   }
     256             : 
     257           1 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)wksp, FD_WKSP_ALIGN ) ) ) {
     258           0 :     FD_LOG_WARNING(( "bad align" ));
     259           0 :     return NULL;
     260           0 :   }
     261             : 
     262           1 :   if( FD_UNLIKELY( wksp->magic!=FD_WKSP_MAGIC ) ) {
     263           0 :     FD_LOG_WARNING(( "bad magic" ));
     264           0 :     return NULL;
     265           0 :   }
     266             : 
     267             :   /* TODO: consider testing owner */
     268             : 
     269           1 :   FD_COMPILER_MFENCE();
     270           1 :   FD_VOLATILE( wksp->magic ) = 0UL;
     271           1 :   FD_COMPILER_MFENCE();
     272             : 
     273             : # if FD_HAS_DEEPASAN
     274             :   /* Unpoison entire wksp region. */
     275             :   ulong footprint = fd_wksp_footprint( wksp->part_max, wksp->data_max );
     276             :   void * wksp_data = (void*)((ulong)wksp + fd_wksp_private_pinfo_off());
     277             :   fd_asan_unpoison( wksp_data, footprint - fd_wksp_private_pinfo_off());
     278             : # endif
     279             : 
     280           1 :   return wksp;
     281           1 : }
     282             : 
     283           0 : char const * fd_wksp_name    ( fd_wksp_t const * wksp ) { return wksp->name;     }
     284           0 : uint         fd_wksp_seed    ( fd_wksp_t const * wksp ) { return wksp->seed;     }
     285           0 : ulong        fd_wksp_part_max( fd_wksp_t const * wksp ) { return wksp->part_max; }
     286           0 : ulong        fd_wksp_data_max( fd_wksp_t const * wksp ) { return wksp->data_max; }
     287           0 : ulong        fd_wksp_gaddr_lo( fd_wksp_t const * wksp ) { return wksp->gaddr_lo; }
     288           0 : ulong        fd_wksp_gaddr_hi( fd_wksp_t const * wksp ) { return wksp->gaddr_hi; }
     289             : 
     290             : ulong
     291           0 : fd_wksp_owner( fd_wksp_t const * wksp ) {
     292           0 :   FD_COMPILER_MFENCE();
     293           0 :   ulong owner = FD_VOLATILE_CONST( wksp->owner );
     294           0 :   FD_COMPILER_MFENCE();
     295           0 :   return owner;
     296           0 : }
     297             : 
     298             : char const *
     299           0 : fd_wksp_strerror( int err ) {
     300           0 :   switch( err ) {
     301           0 :   case FD_WKSP_SUCCESS:     return "success";
     302           0 :   case FD_WKSP_ERR_INVAL:   return "inval";
     303           0 :   case FD_WKSP_ERR_FAIL:    return "fail";
     304           0 :   case FD_WKSP_ERR_CORRUPT: return "corrupt";
     305           0 :   default: break;
     306           0 :   }
     307           0 :   return "unknown";
     308           0 : }
     309             : 
     310             : int
     311           0 : fd_wksp_verify( fd_wksp_t * wksp ) {
     312             : 
     313           0 : # define TEST(c) do {                                                                             \
     314           0 :     if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return FD_WKSP_ERR_CORRUPT; } \
     315           0 :   } while(0)
     316             : 
     317             :   /* Validate metadata */
     318             : 
     319           0 :   TEST( wksp );
     320           0 :   TEST( wksp->magic==FD_WKSP_MAGIC );
     321             : 
     322           0 :   ulong part_max = wksp->part_max;
     323           0 :   ulong data_max = wksp->data_max;
     324           0 :   TEST( fd_wksp_footprint( part_max, data_max ) );
     325             : 
     326           0 :   ulong gaddr_lo = wksp->gaddr_lo; TEST( gaddr_lo==fd_wksp_private_data_off( part_max ) );
     327           0 :   ulong gaddr_hi = wksp->gaddr_hi; TEST( gaddr_hi==gaddr_lo+data_max                    );
     328             : 
     329           0 :   TEST( fd_shmem_name_len( wksp->name ) );
     330             : 
     331             :   /* seed is arbitrary */
     332             : 
     333           0 :   TEST( wksp->cycle_tag >= 4UL );
     334             : 
     335             :   /* TODO: consider verifying owner */
     336             : 
     337           0 :   fd_wksp_private_pinfo_t * pinfo = fd_wksp_private_pinfo( wksp );
     338             : 
     339             :   /* Clear out cycle tags */
     340             : 
     341           0 :   for( ulong i=0UL; i<part_max; i++ ) pinfo[ i ].cycle_tag = 0UL;
     342             : 
     343             :   /* Verify the idle stack */
     344             : 
     345           0 :   ulong idle_cnt = 0UL;
     346             : 
     347           0 :   do {
     348           0 :     ulong i = fd_wksp_private_pinfo_idx( wksp->idle_top_cidx );
     349           0 :     while( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     350             : 
     351             :       /* Visit i.  Note that i has not been validated yet. */
     352             : 
     353           0 :       TEST( i<part_max            ); /* Validate i */
     354           0 :       TEST( !pinfo[ i ].cycle_tag ); /* Make sure not visited before */
     355           0 :       pinfo[ i ].cycle_tag = 1UL;    /* Mark as visited in idle stack */
     356           0 :       idle_cnt++;                    /* Update the idle cnt */
     357             : 
     358             :       /* Advance to the next idle */
     359             : 
     360           0 :       i = fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx );
     361           0 :     }
     362           0 :   } while(0);
     363             : 
     364             :   /* Idle stack looks intact, verify partitioning */
     365             : 
     366           0 :   ulong free_cnt = 0UL;
     367           0 :   ulong used_cnt = 0UL;
     368             : 
     369           0 :   do {
     370           0 :     ulong j = FD_WKSP_PRIVATE_PINFO_IDX_NULL;
     371           0 :     ulong i = fd_wksp_private_pinfo_idx( wksp->part_head_cidx );
     372           0 :     ulong g = gaddr_lo;
     373             : 
     374           0 :     int last_free = 0;
     375             : 
     376           0 :     while( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     377             : 
     378             :       /* At this point, we last visited j.  Visit i.  Note that j has
     379             :          been validated but i has not. */
     380             : 
     381           0 :       TEST( i<part_max                                           ); /* Validate i */
     382           0 :       TEST( pinfo[ i ].gaddr_lo==g                               ); /* Make sure partition is tightly adjacent to previous */
     383           0 :       TEST( pinfo[ i ].gaddr_hi> g                               ); /* Make sure partition size is non-zero */
     384           0 :       TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].prev_cidx )==j ); /* Make sure correct prev partition */
     385           0 :       TEST( !pinfo[ i ].cycle_tag                                ); /* Make sure not visited before */
     386           0 :       pinfo[ i ].cycle_tag = 2UL;                                   /* Mark as visited in partitioning */
     387             : 
     388           0 :       g = pinfo[ i ].gaddr_hi;                                      /* Extract where the next partition should start */
     389           0 :       int is_free = !pinfo[ i ].tag;                                /* Determine if this partition is free or used */
     390           0 :       TEST( !(last_free & is_free) );                               /* Make sure no adjacent free partitions */
     391           0 :       free_cnt += (ulong) is_free;                                  /* Update the free cnt */
     392           0 :       used_cnt += (ulong)!is_free;                                  /* Update the used cnt */
     393             : 
     394             :       /* Advance to the next partition */
     395             : 
     396           0 :       last_free = is_free;
     397             : 
     398           0 :       j = i;
     399           0 :       i = fd_wksp_private_pinfo_idx( pinfo[ i ].next_cidx );
     400           0 :     }
     401             : 
     402           0 :     TEST( fd_wksp_private_pinfo_idx( wksp->part_tail_cidx )==j ); /* Make sure correct partition tail */
     403           0 :     TEST( g==gaddr_hi );                                          /* Make sure complete partitioning */
     404           0 :     TEST( (idle_cnt + free_cnt + used_cnt)==part_max );           /* Make sure no lost idle partitions */
     405           0 :   } while(0);
     406             : 
     407             :   /* Idle stack and partitioning look intact, validate used treap */
     408             : 
     409           0 :   do {
     410           0 :     ulong visit_cnt = 0UL;
     411             : 
     412           0 :     ulong i = fd_wksp_private_pinfo_idx( wksp->part_used_cidx );
     413           0 :     ulong s = FD_WKSP_PRIVATE_PINFO_IDX_NULL;
     414           0 :     ulong g = gaddr_lo;
     415             : 
     416           0 :     if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     417           0 :       TEST( i<part_max );                                             /* Validate i */
     418           0 :       TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==s ); /* Validate parent */
     419           0 :     }
     420             : 
     421           0 :     for(;;) {
     422             : 
     423             :       /* At this point i is and everything on stack is validated */
     424             : 
     425           0 :       if( fd_wksp_private_pinfo_idx_is_null( i ) ) {
     426           0 :         if( fd_wksp_private_pinfo_idx_is_null( s ) ) break; /* Done */
     427             : 
     428             :         /* Pop stack */
     429             : 
     430           0 :         i = s;
     431           0 :         s = fd_wksp_private_pinfo_idx( pinfo[ i ].stack_cidx );
     432             : 
     433             :         /* Visit i */
     434             : 
     435           0 :         ulong p = fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx ); /* Extract the parent */
     436             : 
     437           0 :         TEST( pinfo[ i ].gaddr_lo>=g    );            /* Make sure this starts after last visited */
     438           0 :         TEST( pinfo[ i ].tag            );            /* Make sure tagged as a used partition */
     439           0 :         TEST( pinfo[ i ].cycle_tag==2UL );            /* Make sure in partitioning and not visited yet this traversal */
     440           0 :         if( !fd_wksp_private_pinfo_idx_is_null( p ) ) /* Make sure heap property satisfied */
     441           0 :           TEST( pinfo[ p ].heap_prio >= pinfo[ i ].heap_prio );
     442             : 
     443           0 :         TEST( !pinfo[ i ].in_same );                  /* Make sure unique */
     444           0 :         TEST( fd_wksp_private_pinfo_idx_is_null( fd_wksp_private_pinfo_idx( pinfo[ i ].same_cidx ) ) ); /* " */
     445             : 
     446           0 :         pinfo[ i ].cycle_tag = 3UL;                   /* Mark as visited this traversal */
     447           0 :         visit_cnt++;                                  /* Update the visit cnt */
     448           0 :         g = pinfo[ i ].gaddr_hi;                      /* Get minimum start for next partition */
     449             : 
     450             :         /* Traverse the right subtree */
     451             : 
     452           0 :         p = i;
     453           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].right_cidx );
     454           0 :         if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     455           0 :           TEST( i<part_max );                                             /* Validate i */
     456           0 :           TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==p ); /* Validate parent */
     457           0 :         }
     458             : 
     459           0 :       } else {
     460             : 
     461             :         /* At this point i and everything on the stack is validated.
     462             :            Push i to the stack and recurse on the left subtree. */
     463             : 
     464           0 :         pinfo[ i ].stack_cidx = fd_wksp_private_pinfo_cidx( s );
     465           0 :         s = i;
     466           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].left_cidx );
     467           0 :         if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     468           0 :           TEST( i<part_max );                                             /* Validate i */
     469           0 :           TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==s ); /* Validate parent */
     470           0 :         }
     471             : 
     472           0 :       }
     473           0 :     }
     474             : 
     475           0 :     TEST( visit_cnt==used_cnt ); /* Make sure all used partitions in used treap */
     476           0 :   } while(0);
     477             : 
     478             :   /* Idle stack, partitioning and used treap look intact, validate the
     479             :      free treap. */
     480             : 
     481           0 :   do {
     482           0 :     ulong visit_cnt = 0UL;
     483             : 
     484           0 :     ulong i  = fd_wksp_private_pinfo_idx( wksp->part_free_cidx );
     485           0 :     ulong s  = FD_WKSP_PRIVATE_PINFO_IDX_NULL;
     486           0 :     ulong sz = 0UL;
     487             : 
     488           0 :     if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     489           0 :       TEST( i<part_max );                                             /* Validate i */
     490           0 :       TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==s ); /* Validate parent */
     491           0 :     }
     492             : 
     493           0 :     for(;;) {
     494             : 
     495             :       /* At this point i and everything on the stack is validated */
     496             : 
     497           0 :       if( fd_wksp_private_pinfo_idx_is_null( i ) ) {
     498           0 :         if( fd_wksp_private_pinfo_idx_is_null( s ) ) break; /* Done */
     499             : 
     500             :         /* Pop stack */
     501             : 
     502           0 :         i = s;
     503           0 :         s = fd_wksp_private_pinfo_idx( pinfo[ i ].stack_cidx );
     504             : 
     505             :         /* Visit i */
     506             : 
     507           0 :         ulong p   = fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx ); /* Extract the parent */
     508           0 :         ulong isz = fd_wksp_private_pinfo_sz( pinfo + i );               /* Extract the size */
     509             : 
     510           0 :         TEST( isz>sz              ); /* Make sure this partition i larger than previous */
     511           0 :         TEST( !pinfo[ i ].tag     ); /* Make sure tagged as a free partition */
     512           0 :         TEST( !pinfo[ i ].in_same ); /* Make sure marked as not in same */
     513             : 
     514           0 :         if( !fd_wksp_private_pinfo_idx_is_null( p ) ) { /* Make sure heap property satisfied */
     515           0 :           TEST( pinfo[ p ].heap_prio >= pinfo[ i ].heap_prio );
     516           0 :         }
     517             : 
     518           0 :         sz = isz; /* Update largest size partition seen so far */
     519             : 
     520             :         /* Traverse all same sized partitions */
     521             : 
     522           0 :         ulong j = i;
     523           0 :         for(;;) {
     524             : 
     525             :           /* At this point, j is validated */
     526             : 
     527           0 :           TEST( pinfo[ j ].cycle_tag==2UL ); /* Make sure in partitioning and not visited yet this traversal */
     528           0 :           pinfo[ j ].cycle_tag = 3UL;        /* Mark as visited this traversal */
     529           0 :           visit_cnt++;
     530             : 
     531           0 :           ulong k = fd_wksp_private_pinfo_idx( pinfo[ j ].same_cidx );    /* Get the next same sized */
     532           0 :           if( fd_wksp_private_pinfo_idx_is_null( k ) ) break;             /* If no more, we are done with this node */
     533           0 :           TEST( k<part_max );                                             /* Make sure valid index */
     534           0 :           TEST( fd_wksp_private_pinfo_sz( pinfo + k )==sz );              /* Make sure same size */
     535           0 :           TEST( pinfo[ k ].in_same );                                     /* Make sure marked as in same */
     536           0 :           TEST( fd_wksp_private_pinfo_idx_is_null( fd_wksp_private_pinfo_idx( pinfo[ k ].left_cidx  ) ) );
     537           0 :           TEST( fd_wksp_private_pinfo_idx_is_null( fd_wksp_private_pinfo_idx( pinfo[ k ].right_cidx ) ) );
     538           0 :           TEST( fd_wksp_private_pinfo_idx( pinfo[ k ].parent_cidx )==j ); /* Make sure correct parent */
     539           0 :           j = k;
     540           0 :         }
     541             : 
     542             :         /* Recurse on the right subtree */
     543             : 
     544           0 :         p = i;
     545           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].right_cidx );
     546           0 :         if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     547           0 :           TEST( i<part_max );                                             /* Validate i */
     548           0 :           TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==p ); /* Validate parent */
     549           0 :         }
     550             : 
     551           0 :       } else {
     552             : 
     553           0 :         TEST( i<part_max ); /* Validate i */
     554             : 
     555             :         /* At this point i and everything on the stack is validated.
     556             :            Push i to the stack and recurse on the left subtree. */
     557             : 
     558           0 :         pinfo[ i ].stack_cidx = fd_wksp_private_pinfo_cidx( s );
     559           0 :         s = i;
     560           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].left_cidx );
     561           0 :         if( !fd_wksp_private_pinfo_idx_is_null( i ) ) {
     562           0 :           TEST( i<part_max );                                             /* Validate i */
     563           0 :           TEST( fd_wksp_private_pinfo_idx( pinfo[ i ].parent_cidx )==s ); /* Validate parent */
     564           0 :         }
     565           0 :       }
     566           0 :     }
     567             : 
     568           0 :     TEST( visit_cnt==free_cnt ); /* Make sure all free partitions in free treap */
     569             : 
     570           0 :   } while(0);
     571             : 
     572           0 : # undef TEST
     573             : 
     574           0 :   return FD_WKSP_SUCCESS;
     575           0 : }
     576             : 
     577             : int
     578             : fd_wksp_rebuild( fd_wksp_t * wksp,
     579           1 :                  uint        seed ) {
     580             : 
     581             :   /* Load the wksp metadata, don't rebuild if any of it looks even
     582             :      slightly off. */
     583             : 
     584           1 :   if( FD_UNLIKELY( !wksp ) ) {
     585           0 :     FD_LOG_WARNING(( "NULL wksp" ));
     586           0 :     return FD_WKSP_ERR_CORRUPT;
     587           0 :   }
     588             : 
     589           1 :   ulong magic     = wksp->magic;
     590           1 :   ulong part_max  = wksp->part_max;
     591           1 :   ulong data_max  = wksp->data_max;
     592           1 :   ulong gaddr_lo  = wksp->gaddr_lo;
     593           1 :   ulong gaddr_hi  = wksp->gaddr_hi;
     594           1 :   ulong cycle_tag = wksp->cycle_tag;
     595             : 
     596             :   /* TODO: consider verifying owner */
     597             : 
     598           1 :   ulong footprint    = fd_wksp_footprint( part_max, data_max );
     599           1 :   ulong gaddr_lo_exp = fd_wksp_private_data_off( part_max );
     600           1 :   ulong gaddr_hi_exp = gaddr_lo_exp + data_max;
     601           1 :   if( FD_UNLIKELY( (magic!=FD_WKSP_MAGIC) | (!footprint) | (!fd_shmem_name_len( wksp->name )) |
     602           1 :                    (gaddr_lo!=gaddr_lo_exp) | (gaddr_hi!=gaddr_hi_exp) | (cycle_tag<4UL) ) ) {
     603           0 :     FD_LOG_WARNING(( "bad metadata\n\t"
     604           0 :                      "magic     %016lx (exp %016lx)\n\t"
     605           0 :                      "part_max  %lu data_max %lu (footprint %lu)\n\t"
     606           0 :                      "gaddr_lo  %lu (exp %lu)\n\t"
     607           0 :                      "gaddr_hi  %lu (exp %lu)\n\t"
     608           0 :                      "cycle_tag %lu (exp>=4)",
     609           0 :                      magic, FD_WKSP_MAGIC, part_max, data_max, footprint,
     610           0 :                      gaddr_lo, gaddr_lo_exp, gaddr_hi, gaddr_hi_exp, cycle_tag ));
     611           0 :     return FD_WKSP_ERR_CORRUPT;
     612           0 :   }
     613             : 
     614             :   /* Scan the wksp pinfo and insert any used partitions into the used
     615             :      treap and put the rest on the idle stack.  If there is any sign of
     616             :      corruption (empty, bad range or overlap between used partitions),
     617             :      we abort the rebuild (this is almost certainly data corruption of
     618             :      some form and we don't have enough info to resolve a conflict
     619             :      without potentially making the situation worse).  We do the scan in
     620             :      reverse order to rebuild the idle stack in forward order.
     621             : 
     622             :      Note that we don't ever change the gaddr_lo,gaddr_hi of any tagged
     623             :      partitions such that operation is guaranteed to never change the
     624             :      single source of truth.  As such, this operation can be interrupted
     625             :      and restarted arbitrarily safely.*/
     626             : 
     627           1 :   fd_wksp_private_pinfo_t * pinfo = fd_wksp_private_pinfo( wksp );
     628             : 
     629           1 :   do {
     630           1 :     wksp->seed           = seed;
     631           1 :     wksp->idle_top_cidx  = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL ); /* Flush idle stack */
     632           1 :     wksp->part_used_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL ); /* Flush used treap */
     633           1 :     wksp->part_free_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL ); /* Flush free treap */
     634             : 
     635           1 :     ulong i = part_max;
     636     4321157 :     while( i ) {
     637     4321156 :       i--;
     638             : 
     639             :       /* Ideally, heap priorities should just be a shuffling of the
     640             :          integers [0,part_max).  fd_uint_hash will generate such a
     641             :          shuffling for part_max = 2^32.  Using the lower 30 bits
     642             :          (reserving bit 31 for bulk operations) will yield something
     643             :          very close.  We use seed to mix it up some more. */
     644             : 
     645     4321156 :       pinfo[ i ].in_same    = 0U;
     646     4321156 :       pinfo[ i ].heap_prio  = fd_uint_hash( seed ^ (uint)i ) & ((1U<<30)-1U);
     647     4321156 :       pinfo[ i ].stack_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     648     4321156 :       pinfo[ i ].cycle_tag  = 0U;
     649             : 
     650     4321156 :       ulong tag = pinfo[ i ].tag;
     651     4321156 :       if( !tag ) { /* Not used ... make it available for reuse below */
     652     4321156 :         fd_wksp_private_idle_stack_push( i, wksp, pinfo );
     653     4321156 :         continue;
     654     4321156 :       }
     655             : 
     656           0 :       pinfo[ i ].prev_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     657           0 :       pinfo[ i ].next_cidx = fd_wksp_private_pinfo_cidx( FD_WKSP_PRIVATE_PINFO_IDX_NULL );
     658             : 
     659           0 :       if( FD_UNLIKELY( fd_wksp_private_used_treap_insert( i, wksp, pinfo ) ) ) return FD_WKSP_ERR_CORRUPT; /* Logs details */
     660           0 :     }
     661           1 :   } while(0);
     662             : 
     663             :   /* At this point, a partition is either in the idle stack or used
     664             :      treap.  Further, we have:
     665             : 
     666             :                  | used                       | idle
     667             :        ----------+----------------------------+--------
     668             :        gaddr_*   | non-empty range            | 0
     669             :                  | no overlap with other used | 0
     670             :        tag       | non-zero                   | 0
     671             :        in_same   | 0                          | 0
     672             :        heap_prio | randomized                 | randomized
     673             :        prev      | NULL                       | NULL
     674             :        next      | NULL                       | NULL
     675             :        left      | used treap managed         | NULL
     676             :        right     | used treap managed         | NULL
     677             :        same      | used treap managed (NULL)  | NULL
     678             :        parent    | used treap managed         | idle stack managed
     679             :        stack     | wksp managed               | wksp managed
     680             :        cycle_tag | wksp managed               | wksp managed
     681             : 
     682             :      In-order traverse the used treap to rebuild the partitioning and
     683             :      the free treap. */
     684             : 
     685           1 :   do {
     686           1 :     uint * j_next_cidx_ptr = &wksp->part_head_cidx;  /* Location of most recently added partition next link */
     687             : 
     688           1 :     ulong  j  = FD_WKSP_PRIVATE_PINFO_IDX_NULL; /* Most recently added partition */
     689           1 :     ulong  g0 = gaddr_lo;                       /* Most recently added partition end */
     690             : 
     691           1 :     ulong i = fd_wksp_private_pinfo_idx( wksp->part_used_cidx );
     692           1 :     ulong s = FD_WKSP_PRIVATE_PINFO_IDX_NULL;
     693           1 :     for(;;) {
     694           1 :       if( fd_wksp_private_pinfo_idx_is_null( i ) ) {
     695           1 :         if( fd_wksp_private_pinfo_idx_is_null( s ) ) break; /* Done */
     696             : 
     697             :         /* Pop traversal stack */
     698             : 
     699           0 :         i = s;
     700           0 :         s = fd_wksp_private_pinfo_idx( pinfo[ i ].stack_cidx );
     701             : 
     702             :         /* Visit i */
     703             : 
     704           0 :         ulong g1 = pinfo[ i ].gaddr_lo;
     705           0 :         if( g1 > g0 ) { /* There's a gap between i and the most recently added partition */
     706             : 
     707             :           /* Acquire an idle partition to hold the gap */
     708             : 
     709           0 :           if( FD_UNLIKELY( fd_wksp_private_idle_stack_is_empty( wksp ) ) ) {
     710           0 :             FD_LOG_WARNING(( "part_max (%lu) too small to fill gap before partition %lu (tag %lu gaddr_lo %lu gaddr_hi %lu)",
     711           0 :                              part_max, i, pinfo[i].tag, pinfo[i].gaddr_lo, pinfo[i].gaddr_hi ));
     712           0 :             return FD_WKSP_ERR_CORRUPT;
     713           0 :           }
     714           0 :           ulong k = fd_wksp_private_idle_stack_pop( wksp, pinfo );
     715             : 
     716             :           /* Populate the acquired partition with the gap details,
     717             :              append it to the wksp partitioning and insert it into the
     718             :              free treap.  Note that stack_push/pop reset gaddr_lo,
     719             :              gaddr_hi, tag, in_same, {prev, next, left, right, same,
     720             :              parent}_cidx.  It preserved heap_prio from its original
     721             :              assignment and didn't touch stack_cidx or cycle_tag. */
     722             : 
     723           0 :           pinfo[ k ].gaddr_lo  = g0;
     724           0 :           pinfo[ k ].gaddr_hi  = g1;
     725           0 :           pinfo[ k ].prev_cidx = fd_wksp_private_pinfo_cidx( j );
     726           0 :           *j_next_cidx_ptr = fd_wksp_private_pinfo_cidx( k );
     727           0 :           j_next_cidx_ptr  = &pinfo[ k ].next_cidx;
     728           0 :           j  = k;
     729           0 :           g0 = g1;
     730             : 
     731           0 :           fd_wksp_private_free_treap_insert( j, wksp, pinfo );
     732           0 :         }
     733             : 
     734             :         /* Add i to the partitioning. */
     735             : 
     736           0 :         pinfo[ i ].prev_cidx = fd_wksp_private_pinfo_cidx( j );
     737           0 :         *j_next_cidx_ptr = fd_wksp_private_pinfo_cidx( i );
     738           0 :         j_next_cidx_ptr  = &pinfo[ i ].next_cidx;
     739           0 :         j  = i;
     740           0 :         g0 = pinfo[ i ].gaddr_hi;
     741             : 
     742             :         /* Traverse the right subtree */
     743             : 
     744           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].right_cidx );
     745             : 
     746           0 :       } else {
     747             : 
     748             :         /* Push i to the stack and recurse on the left subtree. */
     749             : 
     750           0 :         pinfo[ i ].stack_cidx = fd_wksp_private_pinfo_cidx( s );
     751           0 :         s = i;
     752           0 :         i = fd_wksp_private_pinfo_idx( pinfo[ i ].left_cidx );
     753             : 
     754           0 :       }
     755           1 :     }
     756             : 
     757           1 :     if( g0 < gaddr_hi ) { /* Have final gap to fill */
     758             : 
     759             :       /* This works the same as the above */
     760             : 
     761           1 :       if( FD_UNLIKELY( fd_wksp_private_idle_stack_is_empty( wksp ) ) ) {
     762           0 :         FD_LOG_WARNING(( "part_max (%lu) too small to complete partitioning", part_max ));
     763           0 :         return FD_WKSP_ERR_CORRUPT;
     764           0 :       }
     765           1 :       ulong k = fd_wksp_private_idle_stack_pop( wksp, pinfo );
     766             : 
     767           1 :       pinfo[ k ].gaddr_lo  = g0;
     768           1 :       pinfo[ k ].gaddr_hi  = gaddr_hi;
     769           1 :       pinfo[ k ].prev_cidx = fd_wksp_private_pinfo_cidx( j );
     770           1 :       *j_next_cidx_ptr = fd_wksp_private_pinfo_cidx( k );
     771           1 :       j_next_cidx_ptr  = &pinfo[ k ].next_cidx;
     772           1 :       j  = k;
     773             :     //g0 = gaddr_hi;
     774             : 
     775           1 :       fd_wksp_private_free_treap_insert( j, wksp, pinfo );
     776           1 :     }
     777             : 
     778           1 :     wksp->part_tail_cidx = fd_wksp_private_pinfo_cidx( j );
     779             : 
     780           1 :   } while(0);
     781             : 
     782           1 :   return FD_WKSP_SUCCESS;
     783           1 : }

Generated by: LCOV version 1.14