Line data Source code
1 : #include "utils/fd_ssctrl.h"
2 : #include "utils/fd_zstd_frame.h"
3 :
4 : #include "../../disco/topo/fd_topo.h"
5 : #include "../../disco/metrics/fd_metrics.h"
6 : #include "../../tango/fseq/fd_fseq.h"
7 :
8 : #include <linux/futex.h>
9 : #include "generated/fd_snapdc_tile_seccomp.h"
10 :
11 : #define ZSTD_STATIC_LINKING_ONLY
12 : #include <zstd.h>
13 :
14 : #define NAME "snapdc"
15 :
16 0 : #define ZSTD_WINDOW_SZ (1UL<<25UL) /* 32MiB */
17 :
18 : /* The snapdc tile is a state machine that decompresses the full and
19 : optionally incremental snapshot byte stream that it receives from the
20 : snapld tile. In the event that the snapshot is already uncompressed,
21 : this tile simply copies the stream to the next tile in the pipeline.
22 :
23 : Snapdc tiles claim which frames to decompress next using a shared
24 : counter. */
25 :
26 : struct fd_snapdc_tile {
27 : uint full : 1;
28 : uint is_zstd : 1;
29 : uint dirty : 1; /* in the middle of a frame? */
30 : int state;
31 :
32 : ulong tile_idx;
33 : ulong frame_idx; /* frame at the input cursor */
34 : ulong claimed_frame; /* frame this tile decompresses next, ULONG_MAX until the first data frag of an attempt */
35 : ulong * next_frame_ticket; /* shared counter of the next unclaimed frame */
36 :
37 : ZSTD_DCtx * zstd;
38 : fd_zstd_frame_t zstd_frame[1];
39 :
40 : struct {
41 : fd_wksp_t * mem;
42 : ulong chunk0;
43 : ulong wmark;
44 : ulong mtu;
45 : ulong frag_pos;
46 : } in;
47 :
48 : struct {
49 : fd_wksp_t * mem;
50 : ulong chunk0;
51 : ulong wmark;
52 : ulong chunk;
53 : ulong mtu;
54 : } out;
55 :
56 : struct {
57 : struct {
58 : ulong compressed_bytes_read;
59 : ulong decompressed_bytes_written;
60 : } full;
61 :
62 : struct {
63 : ulong compressed_bytes_read;
64 : ulong decompressed_bytes_written;
65 : } incremental;
66 : } metrics;
67 : };
68 : typedef struct fd_snapdc_tile fd_snapdc_tile_t;
69 :
70 : FD_FN_PURE static ulong
71 0 : scratch_align( void ) {
72 0 : return fd_ulong_max( alignof(fd_snapdc_tile_t), 32UL );
73 0 : }
74 :
75 : FD_FN_PURE static ulong
76 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
77 0 : (void)tile;
78 0 : ulong l = FD_LAYOUT_INIT;
79 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
80 0 : l = FD_LAYOUT_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
81 0 : return FD_LAYOUT_FINI( l, scratch_align() );
82 0 : }
83 :
84 : static inline int
85 0 : should_shutdown( fd_snapdc_tile_t * ctx ) {
86 0 : return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
87 0 : }
88 :
89 : static void
90 0 : metrics_write( fd_snapdc_tile_t * ctx ) {
91 0 : FD_MGAUGE_SET( SNAPDC, COMPRESSED_PROGRESS_BYTES_FULL, ctx->metrics.full.compressed_bytes_read );
92 0 : FD_MGAUGE_SET( SNAPDC, COMPRESSED_PROGRESS_BYTES_INCREMENTAL, ctx->metrics.incremental.compressed_bytes_read );
93 0 : FD_MGAUGE_SET( SNAPDC, DECOMPRESSED_PROGRESS_BYTES_FULL, ctx->metrics.full.decompressed_bytes_written );
94 0 : FD_MGAUGE_SET( SNAPDC, DECOMPRESSED_PROGRESS_BYTES_INCREMENTAL, ctx->metrics.incremental.decompressed_bytes_written );
95 0 : FD_MGAUGE_SET( SNAPDC, STREAM_STATE, (ulong)(ctx->state) );
96 0 : }
97 :
98 : static inline void
99 0 : reset_stream( fd_snapdc_tile_t * ctx ) {
100 0 : ctx->dirty = 0;
101 0 : ctx->frame_idx = 0UL;
102 0 : ctx->claimed_frame = ULONG_MAX;
103 0 : ctx->in.frag_pos = 0UL;
104 0 : FD_TEST( fd_zstd_frame_new( ctx->zstd_frame ) );
105 0 : }
106 :
107 : static void
108 : transition_malformed( fd_snapdc_tile_t * ctx,
109 0 : fd_stem_context_t * stem ) {
110 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
111 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
112 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
113 0 : }
114 :
115 : static inline void
116 : handle_control_frag( fd_snapdc_tile_t * ctx,
117 : fd_stem_context_t * stem,
118 : ulong sig,
119 : ulong chunk,
120 0 : ulong sz ) {
121 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_LOAD_COMPLETE ) ) return;
122 :
123 : /* All control messages except META reset the decompression stream */
124 0 : if( FD_UNLIKELY( sig!=FD_SNAPSHOT_MSG_META ) ) {
125 0 : ulong error = ZSTD_DCtx_reset( ctx->zstd, ZSTD_reset_session_only );
126 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) FD_LOG_ERR(( "ZSTD_DCtx_reset failed (%lu-%s)", error, ZSTD_getErrorName( error ) ));
127 0 : }
128 :
129 0 : if( ctx->state==FD_SNAPSHOT_STATE_ERROR && sig!=FD_SNAPSHOT_MSG_CTRL_FAIL ) {
130 : /* Control messages move along the snapshot load pipeline. Since
131 : error conditions can be triggered by any tile in the pipeline,
132 : it is possible to be in error state and still receive otherwise
133 : valid messages. Only a fail message can revert this. */
134 0 : return;
135 0 : };
136 :
137 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) {
138 : /* Forward META to snapin so it can update the advertised
139 : slot/hash for redirect-based downloads. */
140 0 : FD_TEST( sz<=ctx->out.mtu );
141 0 : void * dst = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
142 0 : fd_memcpy( dst, fd_chunk_to_laddr_const( ctx->in.mem, chunk ), sz );
143 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, sz, 0UL, 0UL, 0UL );
144 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, ctx->out.mtu, ctx->out.chunk0, ctx->out.wmark );
145 0 : return;
146 0 : }
147 :
148 0 : int forward_msg = 1;
149 :
150 0 : switch( sig ) {
151 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
152 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
153 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
154 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
155 0 : FD_TEST( sz==sizeof(fd_ssctrl_init_t) );
156 0 : fd_ssctrl_init_t const * msg = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
157 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
158 0 : ctx->is_zstd = !!msg->zstd;
159 0 : reset_stream( ctx );
160 0 : fd_fseq_update( ctx->next_frame_ticket, 0UL );
161 0 : if( ctx->full ) {
162 0 : ctx->metrics.full.compressed_bytes_read = 0UL;
163 0 : ctx->metrics.full.decompressed_bytes_written = 0UL;
164 0 : } else {
165 0 : ctx->metrics.incremental.compressed_bytes_read = 0UL;
166 0 : ctx->metrics.incremental.decompressed_bytes_written = 0UL;
167 0 : }
168 0 : fd_ssctrl_init_t * msg_out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
169 0 : fd_memcpy( msg_out, msg, sz );
170 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, sz, 0UL, 0UL, 0UL );
171 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, ctx->out.mtu, ctx->out.chunk0, ctx->out.wmark );
172 0 : forward_msg = 0; // we forward the control message in the `fd_ssctrl_init_t` message
173 0 : break;
174 0 : }
175 :
176 0 : case FD_SNAPSHOT_MSG_CTRL_FINI: {
177 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
178 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
179 0 : if( FD_UNLIKELY( ctx->is_zstd && ctx->dirty ) ) {
180 0 : FD_LOG_WARNING(( "encountered end-of-file in the middle of a compressed frame for %s snapshot",
181 0 : ctx->full ? "full" : "incremental" ));
182 0 : transition_malformed( ctx, stem );
183 0 : forward_msg = 0;
184 0 : break;
185 0 : }
186 0 : break;
187 0 : }
188 :
189 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
190 0 : case FD_SNAPSHOT_MSG_CTRL_DONE: {
191 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
192 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
193 0 : break;
194 0 : }
195 :
196 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR: {
197 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
198 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
199 0 : break;
200 0 : }
201 :
202 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL: {
203 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
204 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
205 0 : break;
206 0 : }
207 :
208 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN: {
209 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
210 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
211 0 : break;
212 0 : }
213 :
214 0 : default: {
215 0 : FD_LOG_ERR(( "unexpected control frag %s (%lu) in state %s (%lu)",
216 0 : fd_ssctrl_msg_ctrl_str( sig ), sig,
217 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
218 0 : break;
219 0 : }
220 0 : }
221 :
222 : /* Forward the control message down the pipeline */
223 0 : if( FD_LIKELY( forward_msg ) ) {
224 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
225 0 : }
226 0 : }
227 :
228 : static inline void
229 0 : finish_frame( fd_snapdc_tile_t * ctx ) {
230 0 : ctx->dirty = 0;
231 0 : ctx->frame_idx++;
232 0 : FD_TEST( fd_zstd_frame_new( ctx->zstd_frame ) );
233 0 : }
234 :
235 : /* Reads until the end of the current frame (or the end of the frag, if
236 : that comes earlier). Returns 1 if bytes from the next frame remain
237 : and stem must reprocess this frag. */
238 : static inline int
239 : skip_unowned_frame( fd_snapdc_tile_t * ctx,
240 : fd_stem_context_t * stem,
241 : uchar const * data,
242 0 : ulong sz ) {
243 0 : FD_TEST( ctx->frame_idx<ctx->claimed_frame );
244 0 : FD_TEST( ctx->dirty || ctx->in.frag_pos<sz );
245 0 : ctx->dirty = 1;
246 :
247 0 : ulong skipped = 0UL;
248 0 : int scan_result = fd_zstd_frame_advance( ctx->zstd_frame, data+ctx->in.frag_pos, sz-ctx->in.frag_pos, &skipped );
249 0 : switch( scan_result ) {
250 0 : case FD_ZSTD_FRAME_ERR: {
251 0 : transition_malformed( ctx, stem );
252 0 : return 0;
253 0 : }
254 0 : case FD_ZSTD_FRAME_MORE: {
255 : /* Current frame spans until the end of the frag, so we can move
256 : onto the next frag. */
257 0 : FD_TEST( skipped+ctx->in.frag_pos==sz );
258 0 : ctx->in.frag_pos = 0UL;
259 0 : return 0;
260 0 : }
261 0 : case FD_ZSTD_FRAME_END: {
262 : /* A frame ends within this frag. If the frame end coincides with
263 : the frag end, wait for the next frag, otherwise reprocess
264 : the next frame in this frag. */
265 0 : FD_TEST( skipped && skipped<=sz-ctx->in.frag_pos );
266 0 : ctx->in.frag_pos += skipped;
267 0 : finish_frame( ctx );
268 :
269 0 : if( FD_UNLIKELY( ctx->in.frag_pos==sz ) ) {
270 0 : ctx->in.frag_pos = 0UL;
271 0 : return 0;
272 0 : }
273 0 : return 1;
274 0 : }
275 0 : default: FD_LOG_ERR(( "unexpected zstd frame scan result %d", scan_result ));
276 0 : }
277 0 : }
278 :
279 : /* Decompresses up to a single frame's data. Returns 1 if the current
280 : frag needs to be reprocessed. */
281 : static inline int
282 : process_owned_frame( fd_snapdc_tile_t * ctx,
283 : fd_stem_context_t * stem,
284 : uchar const * data,
285 0 : ulong sz ) {
286 0 : FD_TEST( ctx->frame_idx==ctx->claimed_frame );
287 0 : FD_TEST( ctx->dirty || ctx->in.frag_pos<sz );
288 0 : ctx->dirty = 1;
289 :
290 0 : uchar * out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
291 0 : ulong in_sz = sz-ctx->in.frag_pos;
292 0 : ulong in_consumed = 0UL;
293 0 : ulong out_produced = 0UL;
294 0 : ulong frame_res = ZSTD_decompressStream_simpleArgs(
295 0 : ctx->zstd,
296 0 : out,
297 0 : ctx->out.mtu,
298 0 : &out_produced,
299 0 : data+ctx->in.frag_pos,
300 0 : in_sz,
301 0 : &in_consumed );
302 0 : if( FD_UNLIKELY( ZSTD_isError( frame_res ) ) ) {
303 0 : FD_LOG_WARNING(( "error while decompressing %s snapshot (%u-%s)",
304 0 : ctx->full ? "full" : "incremental",
305 0 : ZSTD_getErrorCode( frame_res ), ZSTD_getErrorName( frame_res ) ));
306 0 : transition_malformed( ctx, stem );
307 0 : return 0;
308 0 : }
309 :
310 0 : ctx->in.frag_pos += in_consumed;
311 0 : FD_TEST( ctx->in.frag_pos<=sz );
312 :
313 0 : if( FD_LIKELY( ctx->full ) ) {
314 0 : ctx->metrics.full.compressed_bytes_read += in_consumed;
315 0 : ctx->metrics.full.decompressed_bytes_written += out_produced;
316 0 : } else {
317 0 : ctx->metrics.incremental.compressed_bytes_read += in_consumed;
318 0 : ctx->metrics.incremental.decompressed_bytes_written += out_produced;
319 0 : }
320 :
321 0 : if( FD_UNLIKELY( frame_res && !in_consumed && !out_produced ) ) {
322 0 : if( FD_UNLIKELY( ctx->in.frag_pos<sz ) ) {
323 : /* No progress with remaining input would retry forever */
324 0 : transition_malformed( ctx, stem );
325 0 : } else {
326 : /* No progress with exhausted input means zstd needs the next frag */
327 0 : ctx->in.frag_pos = 0UL;
328 0 : }
329 0 : return 0;
330 0 : }
331 :
332 0 : if( FD_LIKELY( out_produced || !frame_res ) ) {
333 0 : ulong out_ctl = fd_frag_meta_ctl( 0UL, 0, !frame_res, 0 );
334 0 : fd_stem_publish( stem, 0UL, fd_snapdc_data_sig( ctx->frame_idx ), ctx->out.chunk, out_produced, out_ctl, 0UL, 0UL );
335 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
336 0 : }
337 :
338 0 : if( FD_UNLIKELY( !frame_res ) ) {
339 0 : finish_frame( ctx );
340 0 : ctx->claimed_frame = ULONG_MAX;
341 0 : }
342 :
343 : /* frame_res==0 means the frame ended exactly at the output boundary;
344 : re-polling then reports "new frame expected" and would mark the
345 : stream dirty at a clean EOF. */
346 0 : int maybe_more_output = (out_produced==ctx->out.mtu && frame_res!=0UL) || ctx->in.frag_pos<sz;
347 0 : if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
348 0 : return maybe_more_output;
349 0 : }
350 :
351 : static inline int
352 : handle_data_frag( fd_snapdc_tile_t * ctx,
353 : fd_stem_context_t * stem,
354 : ulong chunk,
355 0 : ulong sz ) {
356 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
357 : /* Ignore all data frags after observing an error in the stream until
358 : we receive fail & init control messages to restart processing. */
359 0 : return 0;
360 0 : }
361 0 : if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
362 0 : FD_LOG_ERR(( "received unexpected data frag in state %s (%lu)",
363 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
364 0 : }
365 :
366 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
367 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
368 :
369 0 : if( FD_UNLIKELY( !ctx->is_zstd ) ) {
370 0 : if( FD_UNLIKELY( ctx->tile_idx!=0UL ) ) return 0;
371 0 : FD_TEST( ctx->in.frag_pos<sz );
372 0 : uchar const * in = data+ctx->in.frag_pos;
373 0 : uchar * out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
374 0 : ulong cpy = fd_ulong_min( sz-ctx->in.frag_pos, ctx->out.mtu );
375 0 : fd_memcpy( out, in, cpy );
376 0 : fd_stem_publish( stem, 0UL, fd_snapdc_data_sig( 0UL ), ctx->out.chunk, cpy, 0UL, 0UL, 0UL );
377 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, cpy, ctx->out.chunk0, ctx->out.wmark );
378 :
379 0 : if( FD_LIKELY( ctx->full ) ) {
380 0 : ctx->metrics.full.compressed_bytes_read += cpy;
381 0 : ctx->metrics.full.decompressed_bytes_written += cpy;
382 0 : } else {
383 0 : ctx->metrics.incremental.compressed_bytes_read += cpy;
384 0 : ctx->metrics.incremental.decompressed_bytes_written += cpy;
385 0 : }
386 :
387 0 : ctx->in.frag_pos += cpy;
388 0 : FD_TEST( ctx->in.frag_pos<=sz );
389 0 : if( FD_UNLIKELY( ctx->in.frag_pos<sz ) ) return 1;
390 0 : ctx->in.frag_pos = 0UL;
391 0 : return 0;
392 0 : }
393 :
394 0 : if( FD_UNLIKELY( ctx->claimed_frame==ULONG_MAX ) ) {
395 0 : ctx->claimed_frame = FD_ATOMIC_FETCH_AND_ADD( ctx->next_frame_ticket, 1UL );
396 0 : }
397 :
398 0 : if( ctx->frame_idx!=ctx->claimed_frame ) {
399 0 : return skip_unowned_frame( ctx, stem, data, sz );
400 0 : }
401 :
402 0 : return process_owned_frame( ctx, stem, data, sz );
403 0 : }
404 :
405 : static inline int
406 : returnable_frag( fd_snapdc_tile_t * ctx,
407 : ulong in_idx FD_PARAM_UNUSED,
408 : ulong seq FD_PARAM_UNUSED,
409 : ulong sig,
410 : ulong chunk,
411 : ulong sz,
412 : ulong ctl FD_PARAM_UNUSED,
413 : ulong tsorig FD_PARAM_UNUSED,
414 : ulong tspub FD_PARAM_UNUSED,
415 0 : fd_stem_context_t * stem ) {
416 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
417 :
418 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
419 0 : else handle_control_frag( ctx, stem, sig, chunk, sz );
420 :
421 0 : return 0;
422 0 : }
423 :
424 : static ulong
425 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
426 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
427 : ulong out_fds_cnt,
428 0 : int * out_fds ) {
429 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
430 :
431 0 : ulong out_cnt = 0;
432 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
433 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
434 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
435 0 : }
436 :
437 0 : return out_cnt;
438 0 : }
439 :
440 : static ulong
441 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
442 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
443 : ulong out_cnt,
444 0 : struct sock_filter * out ) {
445 0 : populate_sock_filter_policy_fd_snapdc_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
446 0 : return sock_filter_policy_fd_snapdc_tile_instr_cnt;
447 0 : }
448 :
449 : static void
450 : unprivileged_init( fd_topo_t const * topo,
451 0 : fd_topo_tile_t const * tile ) {
452 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
453 :
454 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
455 0 : fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
456 0 : void * _zstd = FD_SCRATCH_ALLOC_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
457 :
458 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
459 0 : ctx->tile_idx = tile->kind_id;
460 :
461 0 : fd_topo_obj_t const * ticket_obj = fd_topo_find_obj( topo, "fseq", "frame_ticket", ULONG_MAX );
462 0 : FD_TEST( ticket_obj );
463 0 : ctx->next_frame_ticket = fd_fseq_join( fd_topo_obj_laddr( topo, ticket_obj->id ) );
464 0 : FD_TEST( ctx->next_frame_ticket );
465 :
466 0 : ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
467 0 : FD_TEST( ctx->zstd );
468 0 : FD_TEST( ctx->zstd==_zstd );
469 :
470 0 : reset_stream( ctx );
471 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
472 :
473 0 : if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
474 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
475 :
476 0 : fd_topo_link_t const * snapin_link = &topo->links[ tile->out_link_id[ 0UL ] ];
477 0 : FD_TEST( 0==strcmp( snapin_link->name, "snapdc_in" ) );
478 0 : ctx->out.mem = topo->workspaces[ topo->objs[ snapin_link->dcache_obj_id ].wksp_id ].wksp;
479 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.mem, snapin_link->dcache );
480 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.mem, snapin_link->dcache, snapin_link->mtu );
481 0 : ctx->out.chunk = ctx->out.chunk0;
482 0 : ctx->out.mtu = snapin_link->mtu;
483 :
484 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
485 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
486 0 : ctx->in.mem = in_wksp->wksp;
487 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.mem, in_link->dcache );
488 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.mem, in_link->dcache, in_link->mtu );
489 0 : ctx->in.mtu = in_link->mtu;
490 :
491 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
492 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
493 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
494 0 : scratch_top - (ulong)scratch - scratch_footprint( tile ),
495 0 : scratch_top,
496 0 : (ulong)scratch + scratch_footprint( tile ) ));
497 0 : }
498 :
499 0 : #define STEM_BURST 1UL
500 :
501 0 : #define STEM_LAZY (128L*3000L)
502 :
503 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapdc_tile_t
504 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
505 :
506 0 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
507 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
508 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
509 :
510 : #include "../../disco/stem/fd_stem.c"
511 :
512 : fd_topo_run_tile_t fd_tile_snapdc = {
513 : .name = NAME,
514 : .populate_allowed_fds = populate_allowed_fds,
515 : .populate_allowed_seccomp = populate_allowed_seccomp,
516 : .scratch_align = scratch_align,
517 : .scratch_footprint = scratch_footprint,
518 : .unprivileged_init = unprivileged_init,
519 : .run = stem_run,
520 : };
521 :
522 : #undef NAME
|