LCOV - code coverage report
Current view: top level - discof/restore - fd_snapdc_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 202 0.0 %
Date: 2026-01-23 05:02:40 Functions: 0 10 0.0 %

          Line data    Source code
       1             : #include "utils/fd_ssctrl.h"
       2             : 
       3             : #include "../../disco/topo/fd_topo.h"
       4             : #include "../../disco/metrics/fd_metrics.h"
       5             : 
       6             : #include "generated/fd_snapdc_tile_seccomp.h"
       7             : 
       8             : #define ZSTD_STATIC_LINKING_ONLY
       9             : #include <zstd.h>
      10             : 
      11             : #define NAME "snapdc"
      12             : 
      13           0 : #define ZSTD_WINDOW_SZ (1UL<<25UL) /* 32MiB */
      14             : 
      15             : /* The snapdc tile is a state machine that decompresses the full and
      16             :    optionally incremental snapshot byte stream that it receives from the
      17             :    snapld tile.  In the event that the snapshot is already uncompressed,
      18             :    this tile simply copies the stream to the next tile in the pipeline. */
      19             : 
      20             : struct fd_snapdc_tile {
      21             :   uint full    : 1;
      22             :   uint is_zstd : 1;
      23             :   uint dirty   : 1;  /* in the middle of a frame? */
      24             :   int state;
      25             : 
      26             :   ZSTD_DCtx * zstd;
      27             : 
      28             :   struct {
      29             :     fd_wksp_t * wksp;
      30             :     ulong       chunk0;
      31             :     ulong       wmark;
      32             :     ulong       mtu;
      33             :     ulong       frag_pos;
      34             :   } in;
      35             : 
      36             :   struct {
      37             :     fd_wksp_t * wksp;
      38             :     ulong       chunk0;
      39             :     ulong       wmark;
      40             :     ulong       chunk;
      41             :     ulong       mtu;
      42             :   } out;
      43             : 
      44             :   struct {
      45             :     struct {
      46             :       ulong compressed_bytes_read;
      47             :       ulong decompressed_bytes_written;
      48             :     } full;
      49             : 
      50             :     struct {
      51             :       ulong compressed_bytes_read;
      52             :       ulong decompressed_bytes_written;
      53             :     } incremental;
      54             :   } metrics;
      55             : };
      56             : typedef struct fd_snapdc_tile fd_snapdc_tile_t;
      57             : 
      58             : FD_FN_PURE static ulong
      59           0 : scratch_align( void ) {
      60           0 :   return fd_ulong_max( alignof(fd_snapdc_tile_t), 32UL );
      61           0 : }
      62             : 
      63             : FD_FN_PURE static ulong
      64           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      65           0 :   (void)tile;
      66           0 :   ulong l = FD_LAYOUT_INIT;
      67           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t)                   );
      68           0 :   l = FD_LAYOUT_APPEND( l, 32UL,                      ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
      69           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      70           0 : }
      71             : 
      72             : static inline int
      73           0 : should_shutdown( fd_snapdc_tile_t * ctx ) {
      74           0 :   return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
      75           0 : }
      76             : 
      77             : static void
      78           0 : metrics_write( fd_snapdc_tile_t * ctx ) {
      79           0 :   FD_MGAUGE_SET( SNAPDC, FULL_COMPRESSED_BYTES_READ,              ctx->metrics.full.compressed_bytes_read );
      80           0 :   FD_MGAUGE_SET( SNAPDC, FULL_DECOMPRESSED_BYTES_WRITTEN,         ctx->metrics.full.decompressed_bytes_written );
      81             : 
      82           0 :   FD_MGAUGE_SET( SNAPDC, INCREMENTAL_COMPRESSED_BYTES_READ,       ctx->metrics.incremental.compressed_bytes_read );
      83           0 :   FD_MGAUGE_SET( SNAPDC, INCREMENTAL_DECOMPRESSED_BYTES_WRITTEN,  ctx->metrics.incremental.decompressed_bytes_written );
      84             : 
      85           0 :   FD_MGAUGE_SET( SNAPDC, STATE,                                   (ulong)(ctx->state) );
      86           0 : }
      87             : 
      88             : static inline void
      89             : handle_control_frag( fd_snapdc_tile_t *  ctx,
      90             :                      fd_stem_context_t * stem,
      91             :                      ulong               sig,
      92             :                      ulong               chunk,
      93           0 :                      ulong               sz ) {
      94           0 :   if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) return;
      95             : 
      96             :   /* All control messages cause us to want to reset the decompression stream */
      97           0 :   ulong error = ZSTD_DCtx_reset( ctx->zstd, ZSTD_reset_session_only );
      98           0 :   if( FD_UNLIKELY( ZSTD_isError( error ) ) ) FD_LOG_ERR(( "ZSTD_DCtx_reset failed (%lu-%s)", error, ZSTD_getErrorName( error ) ));
      99             : 
     100           0 :   switch( sig ) {
     101           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_FULL: {
     102           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
     103           0 :       FD_TEST( sz==sizeof(fd_ssctrl_init_t) );
     104           0 :       fd_ssctrl_init_t const * msg = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
     105           0 :       ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
     106           0 :       ctx->full = 1;
     107           0 :       ctx->is_zstd = !!msg->zstd;
     108           0 :       ctx->dirty = 0;
     109           0 :       ctx->in.frag_pos = 0UL;
     110           0 :       ctx->metrics.full.compressed_bytes_read      = 0UL;
     111           0 :       ctx->metrics.full.decompressed_bytes_written = 0UL;
     112           0 :       break;
     113           0 :     }
     114           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
     115           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
     116           0 :       FD_TEST( sz==sizeof(fd_ssctrl_init_t) );
     117           0 :       fd_ssctrl_init_t const * msg = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
     118           0 :       ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
     119           0 :       ctx->full = 0;
     120           0 :       ctx->is_zstd = !!msg->zstd;
     121           0 :       ctx->dirty = 0;
     122           0 :       ctx->in.frag_pos = 0UL;
     123           0 :       ctx->metrics.incremental.compressed_bytes_read      = 0UL;
     124           0 :       ctx->metrics.incremental.decompressed_bytes_written = 0UL;
     125           0 :       break;
     126           0 :     }
     127           0 :     case FD_SNAPSHOT_MSG_CTRL_FAIL:
     128           0 :       ctx->state = FD_SNAPSHOT_STATE_IDLE;
     129           0 :       break;
     130           0 :     case FD_SNAPSHOT_MSG_CTRL_NEXT:
     131           0 :     case FD_SNAPSHOT_MSG_CTRL_DONE:
     132           0 :       if( FD_LIKELY( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ) ) {
     133           0 :         if( FD_UNLIKELY( ctx->is_zstd && ctx->dirty ) ) {
     134           0 :           FD_LOG_WARNING(( "encountered end-of-file in the middle of a compressed frame" ));
     135           0 :           ctx->state = FD_SNAPSHOT_STATE_ERROR;
     136           0 :           fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
     137           0 :         } else {
     138           0 :           ctx->state = FD_SNAPSHOT_STATE_IDLE;
     139           0 :         }
     140           0 :       } else FD_TEST( ctx->state==FD_SNAPSHOT_STATE_ERROR );
     141           0 :       break;
     142           0 :     case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
     143           0 :       FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
     144           0 :       ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
     145           0 :       break;
     146           0 :     case FD_SNAPSHOT_MSG_CTRL_ERROR:
     147           0 :       ctx->state = FD_SNAPSHOT_STATE_ERROR;
     148           0 :       break;
     149           0 :     default:
     150           0 :       FD_LOG_ERR(( "unexpected control sig %lu", sig ));
     151           0 :       return;
     152           0 :   }
     153             : 
     154             :   /* Forward the control message down the pipeline */
     155           0 :   fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
     156           0 : }
     157             : 
     158             : static inline int
     159             : handle_data_frag( fd_snapdc_tile_t *  ctx,
     160             :                   fd_stem_context_t * stem,
     161             :                   ulong               chunk,
     162           0 :                   ulong               sz ) {
     163           0 :   if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
     164             :     /* Ignore all data frags after observing an error in the stream until
     165             :        we receive fail & init control messages to restart processing. */
     166           0 :     return 0;
     167           0 :   }
     168           0 :   else if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
     169           0 :     FD_LOG_ERR(( "invalid state for data frag %d", ctx->state ));
     170           0 :   }
     171             : 
     172           0 :   FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
     173           0 :   uchar const * data = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
     174           0 :   uchar const * in  = data+ctx->in.frag_pos;
     175           0 :   uchar * out = fd_chunk_to_laddr( ctx->out.wksp, ctx->out.chunk );
     176             : 
     177           0 :   if( FD_UNLIKELY( !ctx->is_zstd ) ) {
     178           0 :     FD_TEST( ctx->in.frag_pos<sz );
     179           0 :     ulong cpy = fd_ulong_min( sz-ctx->in.frag_pos, ctx->out.mtu );
     180           0 :     fd_memcpy( out, in, cpy );
     181           0 :     fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, cpy, 0UL, 0UL, 0UL );
     182           0 :     ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, cpy, ctx->out.chunk0, ctx->out.wmark );
     183             : 
     184           0 :     if( FD_LIKELY( ctx->full ) ) {
     185           0 :       ctx->metrics.full.compressed_bytes_read      += cpy;
     186           0 :       ctx->metrics.full.decompressed_bytes_written += cpy;
     187           0 :     } else {
     188           0 :       ctx->metrics.incremental.compressed_bytes_read      += cpy;
     189           0 :       ctx->metrics.incremental.decompressed_bytes_written += cpy;
     190           0 :     }
     191             : 
     192           0 :     ctx->in.frag_pos += cpy;
     193           0 :     FD_TEST( ctx->in.frag_pos<=sz );
     194           0 :     if( FD_UNLIKELY( ctx->in.frag_pos<sz ) ) return 1;
     195           0 :     ctx->in.frag_pos = 0UL;
     196           0 :     return 0;
     197           0 :   }
     198             : 
     199           0 :   ulong in_consumed = 0UL, out_produced = 0UL;
     200           0 :   ulong frame_res = ZSTD_decompressStream_simpleArgs(
     201           0 :       ctx->zstd,
     202           0 :       out,
     203           0 :       ctx->out.mtu,
     204           0 :       &out_produced,
     205           0 :       in,
     206           0 :       sz-ctx->in.frag_pos,
     207           0 :       &in_consumed );
     208           0 :   if( FD_UNLIKELY( ZSTD_isError( frame_res ) ) ) {
     209           0 :     FD_LOG_WARNING(( "error while decompressing snapshot (%u-%s)", ZSTD_getErrorCode( frame_res ), ZSTD_getErrorName( frame_res ) ));
     210           0 :     ctx->state = FD_SNAPSHOT_STATE_ERROR;
     211           0 :     fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
     212           0 :     return 0;
     213           0 :   }
     214             : 
     215           0 :   if( FD_LIKELY( out_produced ) ) {
     216           0 :     fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, out_produced, 0UL, 0UL, 0UL );
     217           0 :     ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
     218           0 :   }
     219             : 
     220           0 :   ctx->in.frag_pos += in_consumed;
     221           0 :   FD_TEST( ctx->in.frag_pos<=sz );
     222             : 
     223           0 :   if( FD_LIKELY( ctx->full ) ) {
     224           0 :     ctx->metrics.full.compressed_bytes_read      += in_consumed;
     225           0 :     ctx->metrics.full.decompressed_bytes_written += out_produced;
     226           0 :   } else {
     227           0 :     ctx->metrics.incremental.compressed_bytes_read      += in_consumed;
     228           0 :     ctx->metrics.incremental.decompressed_bytes_written += out_produced;
     229           0 :   }
     230             : 
     231           0 :   ctx->dirty = frame_res!=0UL;
     232             : 
     233           0 :   int maybe_more_output = out_produced==ctx->out.mtu || ctx->in.frag_pos<sz;
     234           0 :   if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
     235           0 :   return maybe_more_output;
     236           0 : }
     237             : 
     238             : static inline int
     239             : returnable_frag( fd_snapdc_tile_t *  ctx,
     240             :                  ulong               in_idx FD_PARAM_UNUSED,
     241             :                  ulong               seq    FD_PARAM_UNUSED,
     242             :                  ulong               sig,
     243             :                  ulong               chunk,
     244             :                  ulong               sz,
     245             :                  ulong               ctl    FD_PARAM_UNUSED,
     246             :                  ulong               tsorig FD_PARAM_UNUSED,
     247             :                  ulong               tspub  FD_PARAM_UNUSED,
     248           0 :                  fd_stem_context_t * stem ) {
     249           0 :   FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
     250             : 
     251           0 :   if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
     252           0 :   else                                                handle_control_frag( ctx, stem, sig, chunk, sz );
     253             : 
     254           0 :   return 0;
     255           0 : }
     256             : 
     257             : static ulong
     258             : populate_allowed_fds( fd_topo_t      const * topo FD_PARAM_UNUSED,
     259             :                       fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     260             :                       ulong                  out_fds_cnt,
     261           0 :                       int *                  out_fds ) {
     262           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     263             : 
     264           0 :   ulong out_cnt = 0;
     265           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     266           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
     267           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     268           0 :   }
     269             : 
     270           0 :   return out_cnt;
     271           0 : }
     272             : 
     273             : static ulong
     274             : populate_allowed_seccomp( fd_topo_t const *      topo FD_PARAM_UNUSED,
     275             :                           fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     276             :                           ulong                  out_cnt,
     277           0 :                           struct sock_filter *   out ) {
     278           0 :   populate_sock_filter_policy_fd_snapdc_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     279           0 :   return sock_filter_policy_fd_snapdc_tile_instr_cnt;
     280           0 : }
     281             : 
     282             : static void
     283             : unprivileged_init( fd_topo_t *      topo,
     284           0 :                    fd_topo_tile_t * tile ) {
     285           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     286             : 
     287           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     288           0 :   fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
     289           0 :   void * _zstd           = FD_SCRATCH_ALLOC_APPEND( l, 32UL,                      ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
     290             : 
     291           0 :   ctx->state = FD_SNAPSHOT_STATE_IDLE;
     292             : 
     293           0 :   ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
     294           0 :   FD_TEST( ctx->zstd );
     295           0 :   FD_TEST( ctx->zstd==_zstd );
     296             : 
     297           0 :   ctx->dirty = 0;
     298           0 :   ctx->in.frag_pos = 0UL;
     299           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
     300             : 
     301           0 :   if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1",  tile->in_cnt  ));
     302           0 :   if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
     303             : 
     304           0 :   fd_topo_link_t * snapin_link = &topo->links[ tile->out_link_id[ 0UL ] ];
     305           0 :   FD_TEST( 0==strcmp( snapin_link->name, "snapdc_in" ) );
     306           0 :   ctx->out.wksp   = topo->workspaces[ topo->objs[ snapin_link->dcache_obj_id ].wksp_id ].wksp;
     307           0 :   ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.wksp, snapin_link->dcache );
     308           0 :   ctx->out.wmark  = fd_dcache_compact_wmark ( ctx->out.wksp, snapin_link->dcache, snapin_link->mtu );
     309           0 :   ctx->out.chunk  = ctx->out.chunk0;
     310           0 :   ctx->out.mtu    = snapin_link->mtu;
     311             : 
     312           0 :   fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
     313           0 :   fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
     314           0 :   ctx->in.wksp                   = in_wksp->wksp;
     315           0 :   ctx->in.chunk0                 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
     316           0 :   ctx->in.wmark                  = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
     317           0 :   ctx->in.mtu                    = in_link->mtu;
     318             : 
     319           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     320           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     321           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
     322           0 :                  scratch_top - (ulong)scratch - scratch_footprint( tile ),
     323           0 :                  scratch_top,
     324           0 :                  (ulong)scratch + scratch_footprint( tile ) ));
     325           0 : }
     326             : 
     327             : /* handle_data_frag can publish one data frag plus an error frag */
     328           0 : #define STEM_BURST 2UL
     329             : 
     330           0 : #define STEM_LAZY  1000L
     331             : 
     332           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_snapdc_tile_t
     333           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
     334             : 
     335             : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
     336           0 : #define STEM_CALLBACK_METRICS_WRITE   metrics_write
     337           0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
     338             : 
     339             : #include "../../disco/stem/fd_stem.c"
     340             : 
     341             : fd_topo_run_tile_t fd_tile_snapdc = {
     342             :   .name                     = NAME,
     343             :   .populate_allowed_fds     = populate_allowed_fds,
     344             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     345             :   .scratch_align            = scratch_align,
     346             :   .scratch_footprint        = scratch_footprint,
     347             :   .unprivileged_init        = unprivileged_init,
     348             :   .run                      = stem_run,
     349             : };
     350             : 
     351             : #undef NAME

Generated by: LCOV version 1.14