Line data Source code
1 : #include "fd_sched.h"
2 : #include "../../disco/fd_disco_base.h" /* for FD_MAX_TXN_PER_SLOT */
3 : #include "../../flamenco/runtime/fd_runtime.h" /* for fd_runtime_load_txn_address_lookup_tables */
4 :
5 : #include "../../flamenco/runtime/sysvar/fd_sysvar_slot_hashes.h" /* for ALUTs */
6 :
7 :
8 0 : #define FD_SCHED_MAX_DEPTH (FD_RDISP_MAX_DEPTH>>2)
9 0 : #define FD_SCHED_MAX_STAGING_LANES_LOG (2)
10 0 : #define FD_SCHED_MAX_STAGING_LANES (1UL<<FD_SCHED_MAX_STAGING_LANES_LOG)
11 :
12 : /* 64 ticks per slot, and a single gigantic microblock containing min
13 : size transactions. */
14 : FD_STATIC_ASSERT( FD_MAX_TXN_PER_SLOT_SHRED==((FD_SHRED_DATA_PAYLOAD_MAX_PER_SLOT-65UL*sizeof(fd_microblock_hdr_t))/FD_TXN_MIN_SERIALIZED_SZ), max_txn_per_slot_shred );
15 :
16 : /* We size the buffer to be able to hold residual data from the previous
17 : FEC set that only becomes parseable after the next FEC set is
18 : ingested, as well as the incoming FEC set. The largest minimally
19 : parseable unit of data is a transaction. So that much data may
20 : straddle FEC set boundaries. Other minimally parseable units of data
21 : include the microblock header and the microblock count within a
22 : batch. */
23 0 : #define FD_SCHED_MAX_PAYLOAD_PER_FEC (FD_STORE_DATA_MAX)
24 : #define FD_SCHED_MAX_FEC_BUF_SZ (FD_SCHED_MAX_PAYLOAD_PER_FEC+FD_TXN_MTU)
25 : FD_STATIC_ASSERT( FD_TXN_MTU>=sizeof(fd_microblock_hdr_t), resize buffer for residual data );
26 : FD_STATIC_ASSERT( FD_TXN_MTU>=sizeof(ulong), resize buffer for residual data );
27 :
28 0 : #define FD_SCHED_MAGIC (0xace8a79c181f89b6UL) /* echo -n "fd_sched_v0" | sha512sum | head -c 16 */
29 :
30 0 : #define FD_SCHED_PARSER_OK (0)
31 0 : #define FD_SCHED_PARSER_AGAIN_LATER (1)
32 0 : #define FD_SCHED_PARSER_BAD_BLOCK (2)
33 :
34 :
35 : /* Structs. */
36 :
37 : #define SET_NAME txn_bitset
38 : #define SET_MAX FD_SCHED_MAX_DEPTH
39 : #include "../../util/tmpl/fd_set.c"
40 :
41 : struct fd_sched_block {
42 : ulong slot;
43 : ulong parent_slot;
44 : ulong parent_idx; /* Index of the parent in the pool. */
45 : ulong child_idx; /* Index of the left-child in the pool. */
46 : ulong sibling_idx; /* Index of the right-sibling in the pool. */
47 :
48 : /* Counters. */
49 : uint txn_parsed_cnt;
50 : /* txn_queued_cnt = txn_parsed_cnt-txn_in_flight_cnt-txn_done_cnt */
51 : uint txn_exec_in_flight_cnt;
52 : uint txn_exec_done_cnt;
53 : uint txn_sigverify_in_flight_cnt;
54 : uint txn_sigverify_done_cnt;
55 : uint txn_done_cnt; /* A transaction is considered done when all types of tasks associated with it are done. */
56 : ulong txn_idx[ FD_MAX_TXN_PER_SLOT ]; /* Indexed by parse order. */
57 : uint shred_cnt;
58 :
59 : /* Parser state. */
60 : uchar txn[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
61 : fd_hash_t poh; /* Latest PoH hash we've seen from the ingested FEC sets. */
62 : ulong mblks_rem; /* Number of microblocks remaining in the current batch. */
63 : ulong txns_rem; /* Number of transactions remaining in the current microblock. */
64 : fd_acct_addr_t aluts[ 256 ]; /* Resolve ALUT accounts into this buffer for more parallelism. */
65 : uint fec_buf_sz; /* Size of the fec_buf in bytes. */
66 : uint fec_buf_soff; /* Starting offset into fec_buf for unparsed transactions. */
67 : uint fec_eob:1; /* FEC end-of-batch: set if the last FEC set in the batch is being
68 : ingested. */
69 : uint fec_sob:1; /* FEC start-of-batch: set if the parser expects to be receiving a new
70 : batch. */
71 :
72 : /* Block state. */
73 : uint fec_eos:1; /* FEC end-of-stream: set if the last FEC set in the block has been
74 : ingested. */
75 : uint rooted:1; /* Set if the block is rooted. */
76 : uint dying:1; /* Set if the block has been abandoned and no transactions should be
77 : scheduled from it. */
78 : uint in_sched:1; /* Set if the block is being tracked by the scheduler. */
79 : uint in_rdisp:1; /* Set if the block is being tracked by the dispatcher, either as staged
80 : or unstaged. */
81 : uint block_start_signaled:1; /* Set if the start-of-block sentinel has been dispatched. */
82 : uint block_end_signaled:1; /* Set if the end-of-block sentinel has been dispatched. */
83 : uint block_start_done:1; /* Set if the start-of-block processing has been completed. */
84 : uint block_end_done:1; /* Set if the end-of-block processing has been completed. */
85 : uint staged:1; /* Set if the block is in a dispatcher staging lane; a staged block is
86 : tracked by the dispatcher. */
87 : ulong staging_lane; /* Ignored if staged==0. */
88 : ulong luf_depth; /* Depth of longest unstaged fork starting from this node; only
89 : stageable unstaged descendants are counted. */
90 : uchar fec_buf[ FD_SCHED_MAX_FEC_BUF_SZ ]; /* The previous FEC set could have some residual data that only becomes
91 : parseable after the next FEC set is ingested. */
92 : };
93 : typedef struct fd_sched_block fd_sched_block_t;
94 :
95 : FD_STATIC_ASSERT( sizeof(fd_hash_t)==sizeof(((fd_microblock_hdr_t *)0)->hash), unexpected poh hash size );
96 :
97 :
98 : struct fd_sched_metrics {
99 : uint block_added_cnt;
100 : uint block_added_staged_cnt;
101 : uint block_added_unstaged_cnt;
102 : uint block_added_dead_ood_cnt;
103 : uint block_removed_cnt;
104 : uint block_abandoned_cnt;
105 : uint block_bad_cnt;
106 : uint block_promoted_cnt;
107 : uint block_demoted_cnt;
108 : uint deactivate_no_child_cnt;
109 : uint deactivate_no_txn_cnt;
110 : uint deactivate_pruned_cnt;
111 : uint deactivate_abandoned_cnt;
112 : uint lane_switch_cnt;
113 : uint lane_promoted_cnt;
114 : uint lane_demoted_cnt;
115 : uint alut_success_cnt;
116 : uint alut_serializing_cnt;
117 : uint txn_abandoned_parsed_cnt;
118 : uint txn_abandoned_exec_done_cnt;
119 : uint txn_abandoned_done_cnt;
120 : uint txn_max_in_flight_cnt;
121 : ulong txn_weighted_in_flight_cnt;
122 : ulong txn_weighted_in_flight_tickcount;
123 : ulong txn_none_in_flight_tickcount;
124 : ulong txn_parsed_cnt;
125 : ulong txn_exec_done_cnt;
126 : ulong txn_sigverify_done_cnt;
127 : ulong txn_done_cnt;
128 : ulong bytes_ingested_cnt;
129 : ulong bytes_ingested_unparsed_cnt;
130 : ulong bytes_dropped_cnt;
131 : ulong fec_cnt;
132 : };
133 : typedef struct fd_sched_metrics fd_sched_metrics_t;
134 :
135 : struct fd_sched {
136 : fd_sched_metrics_t metrics[ 1 ];
137 : ulong canary; /* == FD_SCHED_MAGIC */
138 : ulong block_cnt_max; /* Immutable. */
139 : ulong exec_cnt; /* Immutable. */
140 : long txn_in_flight_last_tick;
141 : ulong root_idx;
142 : fd_rdisp_t * rdisp;
143 : ulong txn_exec_ready_bitset[ 1 ];
144 : ulong sigverify_ready_bitset[ 1 ];
145 : ulong active_bank_idx; /* Index of the actively replayed block, or ULONG_MAX if no block is
146 : actively replayed; has to have a transaction to dispatch; staged
147 : blocks that have no transactions to dispatch are not eligible for
148 : being active. */
149 : ulong staged_bitset; /* Bit i set if staging lane i is occupied. */
150 : ulong staged_head_bank_idx[ FD_SCHED_MAX_STAGING_LANES ]; /* Head of the linear chain in each staging lane, ignored if bit i is
151 : not set in the bitset. */
152 : ulong txn_pool_free_cnt;
153 : fd_txn_p_t txn_pool[ FD_SCHED_MAX_DEPTH ];
154 : ulong txn_to_bank_idx[ FD_SCHED_MAX_DEPTH ]; /* Index of the bank that the txn belongs to. */
155 : txn_bitset_t exec_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
156 : txn_bitset_t sigverify_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
157 : fd_sched_block_t * block_pool; /* Just a flat array. */
158 : };
159 : typedef struct fd_sched fd_sched_t;
160 :
161 :
162 : /* Internal helpers. */
163 :
164 : static void
165 : add_block( fd_sched_t * sched,
166 : ulong bank_idx,
167 : ulong parent_bank_idx );
168 :
169 : FD_WARN_UNUSED static int
170 : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
171 :
172 : FD_WARN_UNUSED static int
173 : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
174 :
175 : static void
176 : try_activate_block( fd_sched_t * sched );
177 :
178 : static void
179 : check_or_set_active_block( fd_sched_t * sched );
180 :
181 : static void
182 : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block );
183 :
184 : static void
185 : maybe_switch_block( fd_sched_t * sched, ulong bank_idx );
186 :
187 : FD_FN_UNUSED static ulong
188 : find_and_stage_longest_unstaged_fork( fd_sched_t * sched, int lane_idx );
189 :
190 : static ulong
191 : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx );
192 :
193 : static ulong
194 : stage_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx );
195 :
196 : static inline fd_sched_block_t *
197 0 : block_pool_ele( fd_sched_t * sched, ulong idx ) {
198 0 : FD_TEST( idx<sched->block_cnt_max || idx==ULONG_MAX );
199 0 : return idx==ULONG_MAX ? NULL : sched->block_pool+idx;
200 0 : }
201 :
202 : FD_FN_UNUSED static inline int
203 0 : block_is_void( fd_sched_block_t * block ) {
204 0 : /* We've seen everything in the block and no transaction got parsed
205 0 : out. */
206 0 : return block->fec_eos && block->txn_parsed_cnt==0;
207 0 : }
208 :
209 : static inline int
210 0 : block_should_signal_end( fd_sched_block_t * block ) {
211 0 : return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->block_start_done && !block->block_end_signaled;
212 0 : }
213 :
214 : static inline int
215 0 : block_will_signal_end( fd_sched_block_t * block ) {
216 0 : return block->fec_eos && !block->block_end_signaled;
217 0 : }
218 :
219 : /* Is there something known to be dispatchable in the block? This is an
220 : important liveness property. A block that doesn't contain any known
221 : dispatchable tasks will be deactivated or demoted. */
222 : static inline int
223 0 : block_is_dispatchable( fd_sched_block_t * block ) {
224 0 : ulong exec_queued_cnt = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
225 0 : ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
226 0 : return exec_queued_cnt>0UL ||
227 0 : sigverify_queued_cnt>0UL ||
228 0 : !block->block_start_signaled ||
229 0 : block_will_signal_end( block );
230 0 : }
231 :
232 : static inline int
233 0 : block_is_in_flight( fd_sched_block_t * block ) {
234 0 : return block->txn_exec_in_flight_cnt || block->txn_sigverify_in_flight_cnt || (block->block_end_signaled && !block->block_end_done);
235 0 : }
236 :
237 : static inline int
238 0 : block_is_done( fd_sched_block_t * block ) {
239 0 : return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->block_start_done && block->block_end_done;
240 0 : }
241 :
242 : static inline int
243 0 : block_is_stageable( fd_sched_block_t * block ) {
244 0 : int rv = !block_is_done( block ) && !block->dying;
245 0 : if( FD_UNLIKELY( rv && !block->in_rdisp ) ) {
246 : /* Invariant: stageable blocks may be currently staged or unstaged,
247 : but must be in the dispatcher either way. When a block
248 : transitions to DONE, it will be immediately removed from the
249 : dispatcher. When a block transitions to DYING, it will be
250 : eventually abandoned from the dispatcher. */
251 0 : FD_LOG_CRIT(( "invariant violation: stageable block->in_rdisp==0, txn_parsed_cnt %u, txn_done_cnt %u, fec_eos %u,, slot %lu, parent slot %lu",
252 0 : block->txn_parsed_cnt, block->txn_done_cnt, (uint)block->fec_eos, block->slot, block->parent_slot ));
253 0 : }
254 0 : return rv;
255 0 : }
256 :
257 : static inline int
258 0 : block_is_promotable( fd_sched_block_t * block ) {
259 0 : return block_is_stageable( block ) && block_is_dispatchable( block ) && !block->staged;
260 0 : }
261 :
262 : static inline int
263 0 : block_is_activatable( fd_sched_block_t * block ) {
264 0 : return block_is_stageable( block ) && block_is_dispatchable( block ) && block->staged;
265 0 : }
266 :
267 : static inline int
268 0 : block_should_deactivate( fd_sched_block_t * block ) {
269 : /* We allow a grace period, during which a block has nothing to
270 : dispatch, but has something in-flight. The block is allowed to
271 : stay activated and ingest FEC sets during this time. The block
272 : will be deactivated if there's still nothing to dispatch by the
273 : time all in-flight tasks are completed. */
274 0 : return !block_is_activatable( block ) && !block_is_in_flight( block );
275 0 : }
276 :
277 : FD_FN_UNUSED static void
278 0 : print_block( fd_sched_block_t * block ) {
279 0 : FD_LOG_INFO(( "block slot %lu, parent_slot %lu, staged %d (lane %lu), dying %d, in_rdisp %d, fec_eos %d, rooted %d, block_start_signaled %d, block_end_signaled %d, block_start_done %d, block_end_done %d, txn_parsed_cnt %u, txn_exec_in_flight_cnt %u, txn_exec_done_cnt %u, txn_sigverify_in_flight_cnt %u, txn_sigverify_done_cnt %u, txn_done_cnt %u, shred_cnt %u, mblks_rem %lu, txns_rem %lu, fec_buf_sz %u, fec_buf_soff %u, fec_eob %d, fec_sob %d",
280 0 : block->slot, block->parent_slot, block->staged, block->staging_lane, block->dying, block->in_rdisp, block->fec_eos, block->rooted, block->block_start_signaled, block->block_end_signaled, block->block_start_done, block->block_end_done, block->txn_parsed_cnt, block->txn_exec_in_flight_cnt, block->txn_exec_done_cnt, block->txn_sigverify_in_flight_cnt, block->txn_sigverify_done_cnt, block->txn_done_cnt, block->shred_cnt, block->mblks_rem, block->txns_rem, block->fec_buf_sz, block->fec_buf_soff, block->fec_eob, block->fec_sob ));
281 0 : }
282 :
283 : FD_FN_UNUSED static void
284 0 : print_block_and_parent( fd_sched_t * sched, fd_sched_block_t * block ) {
285 0 : print_block( block );
286 0 : fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
287 0 : if( FD_LIKELY( parent ) ) print_block( parent );
288 0 : }
289 :
290 : FD_FN_UNUSED static void
291 0 : print_metrics( fd_sched_t * sched ) {
292 0 : FD_LOG_NOTICE(( "metrics: block_added_cnt %u, block_added_staged_cnt %u, block_added_unstaged_cnt %u, block_added_dead_ood_cnt %u, block_removed_cnt %u, block_abandoned_cnt %u, block_promoted_cnt %u, block_demoted_cnt %u, deactivate_no_child_cnt %u, deactivate_no_txn_cnt %u, deactivate_pruned_cnt %u, deactivate_abandoned_cnt %u, lane_switch_cnt %u, lane_promoted_cnt %u, lane_demoted_cnt %u, alut_success_cnt %u, alut_serializing_cnt %u, txn_abandoned_parsed_cnt %u, txn_abandoned_done_cnt %u, txn_max_in_flight_cnt %u, txn_weighted_in_flight_cnt %lu, txn_weighted_in_flight_tickcount %lu, txn_none_in_flight_tickcount %lu, txn_parsed_cnt %lu, txn_exec_done_cnt %lu, txn_sigverify_done_cnt %lu, txn_done_cnt %lu, bytes_ingested_cnt %lu, bytes_ingested_unparsed_cnt %lu, bytes_dropped_cnt %lu, fec_cnt %lu",
293 0 : sched->metrics->block_added_cnt, sched->metrics->block_added_staged_cnt, sched->metrics->block_added_unstaged_cnt, sched->metrics->block_added_dead_ood_cnt, sched->metrics->block_removed_cnt, sched->metrics->block_abandoned_cnt, sched->metrics->block_promoted_cnt, sched->metrics->block_demoted_cnt, sched->metrics->deactivate_no_child_cnt, sched->metrics->deactivate_no_txn_cnt, sched->metrics->deactivate_pruned_cnt, sched->metrics->deactivate_abandoned_cnt, sched->metrics->lane_switch_cnt, sched->metrics->lane_promoted_cnt, sched->metrics->lane_demoted_cnt, sched->metrics->alut_success_cnt, sched->metrics->alut_serializing_cnt, sched->metrics->txn_abandoned_parsed_cnt, sched->metrics->txn_abandoned_done_cnt, sched->metrics->txn_max_in_flight_cnt, sched->metrics->txn_weighted_in_flight_cnt, sched->metrics->txn_weighted_in_flight_tickcount, sched->metrics->txn_none_in_flight_tickcount, sched->metrics->txn_parsed_cnt, sched->metrics->txn_exec_done_cnt, sched->metrics->txn_sigverify_done_cnt, sched->metrics->txn_done_cnt, sched->metrics->bytes_ingested_cnt, sched->metrics->bytes_ingested_unparsed_cnt, sched->metrics->bytes_dropped_cnt, sched->metrics->fec_cnt ));
294 0 : }
295 :
296 : FD_FN_UNUSED static void
297 0 : print_sched( fd_sched_t * sched ) {
298 0 : FD_LOG_NOTICE(( "sched canary 0x%lx, block_cnt_max %lu, exec_cnt %lu, root_idx %lu, txn_exec_ready_bitset[ 0 ] 0x%lx, sigverify_ready_bitset[ 0 ] 0x%lx, active_idx %lu, staged_bitset %lu, staged_head_idx[0] %lu, staged_head_idx[1] %lu, staged_head_idx[2] %lu, staged_head_idx[3] %lu, txn_pool_free_cnt %lu",
299 0 : sched->canary, sched->block_cnt_max, sched->exec_cnt, sched->root_idx, sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ], sched->active_bank_idx, sched->staged_bitset, sched->staged_head_bank_idx[ 0 ], sched->staged_head_bank_idx[ 1 ], sched->staged_head_bank_idx[ 2 ], sched->staged_head_bank_idx[ 3 ], sched->txn_pool_free_cnt ));
300 0 : for( int l=0; l<(int)FD_SCHED_MAX_STAGING_LANES; l++ ) {
301 0 : if( fd_ulong_extract_bit( sched->staged_bitset, l ) ) {
302 0 : fd_sched_block_t * block = block_pool_ele( sched, sched->staged_head_bank_idx[ l ] );
303 0 : print_block( block );
304 0 : }
305 0 : }
306 0 : }
307 :
308 : FD_FN_UNUSED static void
309 0 : print_all( fd_sched_t * sched, fd_sched_block_t * block ) {
310 0 : print_metrics( sched );
311 0 : print_sched( sched );
312 0 : print_block_and_parent( sched, block );
313 0 : }
314 :
315 :
316 : /* Public functions. */
317 :
318 0 : ulong fd_sched_align( void ) {
319 0 : return fd_ulong_max( alignof(fd_sched_t),
320 0 : fd_ulong_max( fd_rdisp_align(),
321 0 : fd_ulong_max( alignof(fd_sched_block_t), 64UL ))); /* Minimally cache line aligned. */
322 0 : }
323 :
324 : ulong
325 0 : fd_sched_footprint( ulong block_cnt_max ) {
326 0 : ulong l = FD_LAYOUT_INIT;
327 0 : l = FD_LAYOUT_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
328 0 : l = FD_LAYOUT_APPEND( l, fd_rdisp_align(), fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) ); /* dispatcher */
329 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) ); /* block pool */
330 0 : return FD_LAYOUT_FINI( l, fd_sched_align() );
331 0 : }
332 :
333 : void *
334 0 : fd_sched_new( void * mem, ulong block_cnt_max, ulong exec_cnt ) {
335 0 : FD_TEST( exec_cnt && exec_cnt<=64UL );
336 :
337 0 : FD_SCRATCH_ALLOC_INIT( l, mem );
338 0 : fd_sched_t * sched = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
339 0 : void * _rdisp = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(), fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) );
340 0 : void * _bpool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) );
341 0 : FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
342 :
343 0 : ulong seed = ((ulong)fd_tickcount()) ^ FD_SCHED_MAGIC;
344 0 : fd_rdisp_new( _rdisp, FD_SCHED_MAX_DEPTH, block_cnt_max, seed );
345 :
346 0 : fd_sched_block_t * bpool = (fd_sched_block_t *)_bpool;
347 0 : for( ulong i=0; i<block_cnt_max; i++ ) {
348 0 : bpool[ i ].in_sched = 0;
349 0 : }
350 :
351 0 : fd_memset( sched->metrics, 0, sizeof(fd_sched_metrics_t) );
352 0 : sched->txn_in_flight_last_tick = LONG_MAX;
353 :
354 0 : sched->canary = FD_SCHED_MAGIC;
355 0 : sched->block_cnt_max = block_cnt_max;
356 0 : sched->exec_cnt = exec_cnt;
357 0 : sched->root_idx = ULONG_MAX;
358 0 : sched->active_bank_idx = ULONG_MAX;
359 0 : sched->staged_bitset = 0UL;
360 :
361 0 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
362 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
363 :
364 0 : sched->txn_pool_free_cnt = FD_SCHED_MAX_DEPTH-1UL; /* -1 because index 0 is unusable as a sentinel reserved by the dispatcher */
365 :
366 0 : txn_bitset_new( sched->exec_done_set );
367 0 : txn_bitset_new( sched->sigverify_done_set );
368 :
369 0 : return sched;
370 0 : }
371 :
372 : fd_sched_t *
373 0 : fd_sched_join( void * mem, ulong block_cnt_max ) {
374 0 : fd_sched_t * sched = (fd_sched_t *)mem;
375 :
376 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
377 0 : FD_TEST( sched->block_cnt_max==block_cnt_max );
378 :
379 0 : FD_SCRATCH_ALLOC_INIT( l, mem );
380 0 : /* */ FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
381 0 : void * _rdisp = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(), fd_rdisp_footprint( FD_SCHED_MAX_DEPTH, block_cnt_max ) );
382 0 : void * _bpool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) );
383 0 : FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
384 :
385 0 : sched->rdisp = fd_rdisp_join( _rdisp );
386 0 : sched->block_pool = _bpool;
387 :
388 0 : txn_bitset_join( sched->exec_done_set );
389 0 : txn_bitset_join( sched->sigverify_done_set );
390 :
391 0 : return sched;
392 0 : }
393 :
394 : int
395 0 : fd_sched_fec_can_ingest( fd_sched_t * sched, fd_sched_fec_t * fec ) {
396 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
397 0 : FD_TEST( fec->bank_idx<sched->block_cnt_max );
398 0 : FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
399 :
400 0 : if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
401 0 : FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
402 0 : fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
403 0 : }
404 :
405 0 : ulong fec_buf_sz = 0UL;
406 0 : fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
407 0 : if( FD_LIKELY( !fec->is_first_in_block ) ) {
408 0 : fec_buf_sz += block->fec_buf_sz-block->fec_buf_soff;
409 0 : } else {
410 : /* No residual data as this is a fresh new block. */
411 0 : }
412 : /* Addition is safe and won't overflow because we checked the FEC set
413 : size above. */
414 0 : fec_buf_sz += fec->fec->data_sz;
415 : /* Assuming every transaction is min size, do we have enough free
416 : entries in the txn pool? For a more precise txn count, we would
417 : have to do some parsing. */
418 0 : return sched->txn_pool_free_cnt>=fec_buf_sz/FD_TXN_MIN_SERIALIZED_SZ;
419 0 : }
420 :
421 : int
422 : fd_sched_can_ingest( fd_sched_t * sched,
423 0 : ulong fec_cnt ) {
424 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
425 : /* Worst case, we need one byte from the incoming data to extract a
426 : transaction out of the residual data, and the rest of the incoming
427 : data contributes toward min sized transactions. */
428 0 : ulong txn_cnt = (FD_SCHED_MAX_PAYLOAD_PER_FEC-1UL)/FD_TXN_MIN_SERIALIZED_SZ+1UL; /* 478 */
429 0 : return sched->txn_pool_free_cnt>=(txn_cnt*fec_cnt);
430 0 : }
431 :
432 : FD_WARN_UNUSED int
433 0 : fd_sched_fec_ingest( fd_sched_t * sched, fd_sched_fec_t * fec ) {
434 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
435 0 : FD_TEST( fec->bank_idx<sched->block_cnt_max );
436 0 : FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
437 :
438 0 : if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
439 0 : FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
440 0 : fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
441 0 : }
442 :
443 0 : fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
444 0 : if( FD_UNLIKELY( fec->is_first_in_block ) ) {
445 : /* This is a new block. */
446 0 : add_block( sched, fec->bank_idx, fec->parent_bank_idx );
447 0 : block->slot = fec->slot;
448 0 : block->parent_slot = fec->parent_slot;
449 :
450 0 : if( FD_UNLIKELY( block->dying ) ) {
451 : /* The child of a dead block is also dead. We added it to our
452 : fork tree just so we could track an entire lineage of dead
453 : children and propagate the dead property to the entire lineage,
454 : in case there were frags for more than one dead children
455 : in-flight at the time the parent was abandoned. That being
456 : said, we shouldn't need to add the dead child to the
457 : dispatcher. */
458 0 : sched->metrics->block_added_dead_ood_cnt++;
459 :
460 : /* Ignore the FEC set for a dead block. */
461 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
462 0 : return 1;
463 0 : }
464 :
465 : /* Try to find a staging lane for this block. */
466 0 : int alloc_lane = 0;
467 0 : fd_sched_block_t * parent_block = block_pool_ele( sched, fec->parent_bank_idx );
468 0 : if( FD_LIKELY( parent_block->staged ) ) {
469 : /* Parent is staged. So see if we can continue down the same
470 : staging lane. */
471 0 : ulong staging_lane = parent_block->staging_lane;
472 0 : ulong child_idx = parent_block->child_idx;
473 0 : while( child_idx!=ULONG_MAX ) {
474 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
475 0 : if( child->staged && child->staging_lane==staging_lane ) {
476 : /* Found a child on the same lane. So we're done. */
477 0 : staging_lane = FD_RDISP_UNSTAGED;
478 0 : break;
479 0 : }
480 0 : child_idx = child->sibling_idx;
481 0 : }
482 : /* No child is staged on the same lane as the parent. So stage
483 : this block. This is the common case. */
484 0 : if( FD_LIKELY( staging_lane!=FD_RDISP_UNSTAGED ) ) {
485 0 : block->in_rdisp = 1;
486 0 : block->staged = 1;
487 0 : block->staging_lane = staging_lane;
488 0 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, staging_lane );
489 0 : sched->metrics->block_added_cnt++;
490 0 : sched->metrics->block_added_staged_cnt++;
491 0 : } else {
492 0 : alloc_lane = 1;
493 0 : }
494 0 : } else {
495 0 : if( block_is_stageable( parent_block ) ) {
496 : /* Parent is unstaged but stageable. So let's be unstaged too.
497 : This is a policy decision to be lazy and not promote parent
498 : at the moment. */
499 0 : block->in_rdisp = 1;
500 0 : block->staged = 0;
501 0 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, FD_RDISP_UNSTAGED );
502 0 : sched->metrics->block_added_cnt++;
503 0 : sched->metrics->block_added_unstaged_cnt++;
504 0 : } else {
505 0 : alloc_lane = 1;
506 0 : }
507 0 : }
508 0 : if( FD_UNLIKELY( alloc_lane ) ) {
509 : /* We weren't able to inherit the parent's staging lane. So try
510 : to find a new staging lane. */
511 0 : if( FD_LIKELY( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) { /* Optimize for lane available. */
512 0 : int lane_idx = fd_ulong_find_lsb( ~sched->staged_bitset );
513 0 : if( FD_UNLIKELY( lane_idx>=(int)FD_SCHED_MAX_STAGING_LANES ) ) {
514 0 : FD_LOG_CRIT(( "invariant violation: lane_idx %d, sched->staged_bitset %lx",
515 0 : lane_idx, sched->staged_bitset ));
516 0 : }
517 0 : sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
518 0 : sched->staged_head_bank_idx[ lane_idx ] = fec->bank_idx;
519 0 : block->in_rdisp = 1;
520 0 : block->staged = 1;
521 0 : block->staging_lane = (ulong)lane_idx;
522 0 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, block->staging_lane );
523 0 : sched->metrics->block_added_cnt++;
524 0 : sched->metrics->block_added_staged_cnt++;
525 0 : } else {
526 : /* No lanes available. */
527 0 : block->in_rdisp = 1;
528 0 : block->staged = 0;
529 0 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, FD_RDISP_UNSTAGED );
530 0 : sched->metrics->block_added_cnt++;
531 0 : sched->metrics->block_added_unstaged_cnt++;
532 0 : }
533 0 : }
534 0 : }
535 :
536 0 : if( FD_UNLIKELY( block->dying ) ) {
537 : /* Ignore the FEC set for a dead block. */
538 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
539 0 : return 1;
540 0 : }
541 :
542 0 : if( FD_UNLIKELY( !block->in_rdisp ) ) {
543 : /* Invariant: block must be in the dispatcher at this point. */
544 0 : FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, slot %lu, parent slot %lu",
545 0 : block->slot, block->parent_slot ));
546 0 : }
547 :
548 0 : if( FD_UNLIKELY( block->fec_eos ) ) {
549 : /* This means something is wrong upstream. */
550 0 : FD_LOG_CRIT(( "invariant violation: block->fec_eos set but getting more FEC sets fec->mr %s, slot %lu, parent slot %lu",
551 0 : FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
552 0 : }
553 0 : if( FD_UNLIKELY( block->fec_eob && fec->is_last_in_batch ) ) {
554 : /* This means the previous batch didn't parse properly. So this is
555 : a bad block. We should refuse to replay down the fork. */
556 0 : FD_LOG_INFO(( "bad block: block->fec_eob set but getting another FEC set that is last in batch fec->mr %s, slot %lu, parent slot %lu",
557 0 : FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
558 0 : print_block( block );
559 0 : subtree_abandon( sched, block );
560 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
561 0 : check_or_set_active_block( sched );
562 0 : sched->metrics->block_bad_cnt++;
563 0 : return 0;
564 0 : }
565 0 : if( FD_UNLIKELY( block->child_idx!=ULONG_MAX ) ) {
566 : /* This means something is wrong upstream. FEC sets are not being
567 : delivered in replay order. */
568 0 : FD_LOG_CRIT(( "invariant violation: block->child_idx %lu, fec->mr %s, slot %lu, parent slot %lu",
569 0 : block->child_idx, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
570 0 : }
571 :
572 0 : FD_TEST( block->fec_buf_sz>=block->fec_buf_soff );
573 0 : if( FD_LIKELY( block->fec_buf_sz>block->fec_buf_soff ) ) {
574 : /* If there is residual data from the previous FEC set within the
575 : same batch, we move it to the beginning of the buffer and append
576 : the new FEC set. */
577 0 : memmove( block->fec_buf, block->fec_buf+block->fec_buf_soff, block->fec_buf_sz-block->fec_buf_soff );
578 0 : }
579 0 : block->fec_buf_sz -= block->fec_buf_soff;
580 0 : block->fec_buf_soff = 0;
581 : /* Addition is safe and won't overflow because we checked the FEC
582 : set size above. */
583 0 : if( FD_UNLIKELY( block->fec_buf_sz-block->fec_buf_soff+fec->fec->data_sz>FD_SCHED_MAX_FEC_BUF_SZ ) ) {
584 : /* In a conformant block, there shouldn't be more than a
585 : transaction's worth of residual data left over from the previous
586 : FEC set within the same batch. So if this condition doesn't
587 : hold, it's a bad block. Instead of crashing, we should refuse to
588 : replay down the fork. */
589 0 : FD_LOG_INFO(( "bad block: fec_buf_sz %u, fec_buf_soff %u, fec->data_sz %lu, fec->mr %s, slot %lu, parent slot %lu",
590 0 : block->fec_buf_sz, block->fec_buf_soff, fec->fec->data_sz, FD_BASE58_ENC_32_ALLOCA( fec->fec->key.mr.hash ), fec->slot, fec->parent_slot ));
591 0 : print_block( block );
592 0 : subtree_abandon( sched, block );
593 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
594 0 : sched->metrics->block_bad_cnt++;
595 0 : check_or_set_active_block( sched );
596 0 : return 0;
597 0 : }
598 :
599 0 : block->shred_cnt += fec->shred_cnt;
600 0 : sched->metrics->fec_cnt++;
601 :
602 : /* Append the new FEC set to the end of the buffer. */
603 0 : fd_memcpy( block->fec_buf+block->fec_buf_sz, fec->fec->data, fec->fec->data_sz );
604 0 : block->fec_buf_sz += (uint)fec->fec->data_sz;
605 0 : sched->metrics->bytes_ingested_cnt += fec->fec->data_sz;
606 :
607 0 : block->fec_eob = fec->is_last_in_batch;
608 0 : block->fec_eos = fec->is_last_in_block;
609 :
610 0 : int err = fd_sched_parse( sched, block, fec->alut_ctx );
611 0 : if( FD_UNLIKELY( err==FD_SCHED_PARSER_BAD_BLOCK ) ) {
612 0 : FD_LOG_INFO(( "bad block" ));
613 0 : print_block( block );
614 0 : subtree_abandon( sched, block );
615 0 : sched->metrics->bytes_dropped_cnt += block->fec_buf_sz-block->fec_buf_soff;
616 0 : sched->metrics->block_bad_cnt++;
617 0 : check_or_set_active_block( sched );
618 0 : return 0;
619 0 : }
620 :
621 : /* Check if we need to set the active block. */
622 0 : check_or_set_active_block( sched );
623 :
624 0 : return 1;
625 0 : }
626 :
627 : ulong
628 0 : fd_sched_task_next_ready( fd_sched_t * sched, fd_sched_task_t * out ) {
629 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
630 :
631 0 : ulong exec_ready_bitset0 = sched->txn_exec_ready_bitset[ 0 ];
632 0 : ulong exec_fully_ready_bitset = sched->sigverify_ready_bitset[ 0 ] & exec_ready_bitset0;
633 0 : if( FD_UNLIKELY( !exec_fully_ready_bitset ) ) {
634 : /* Early exit if no exec tiles available. */
635 0 : return 0UL;
636 0 : }
637 :
638 0 : if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
639 : /* No need to try activating a block. If we're in this state,
640 : there's truly nothing to execute. We will activate something
641 : when we ingest a FEC set with transactions. */
642 0 : return 0UL;
643 0 : }
644 :
645 0 : out->task_type = FD_SCHED_TT_NULL;
646 :
647 : /* We could in theory reevaluate staging lane allocation here and do
648 : promotion/demotion as needed. It's a policy decision to minimize
649 : fork churn for now and just execute down the same active fork. */
650 :
651 0 : ulong bank_idx = sched->active_bank_idx;
652 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
653 0 : if( FD_UNLIKELY( block_should_deactivate( block ) ) ) {
654 0 : print_all( sched, block );
655 0 : FD_LOG_CRIT(( "invariant violation: active_bank_idx %lu is not activatable nor has anything in-flight", sched->active_bank_idx ));
656 0 : }
657 :
658 0 : if( FD_UNLIKELY( !block->block_start_signaled ) ) {
659 0 : out->task_type = FD_SCHED_TT_BLOCK_START;
660 0 : out->block_start->bank_idx = bank_idx;
661 0 : out->block_start->parent_bank_idx = block->parent_idx;
662 0 : out->block_start->slot = block->slot;
663 0 : block->block_start_signaled = 1;
664 0 : return 1UL;
665 0 : }
666 :
667 0 : ulong exec_tile_idx0 = fd_ulong_if( !!exec_fully_ready_bitset, (ulong)fd_ulong_find_lsb( exec_fully_ready_bitset ), ULONG_MAX );
668 0 : ulong exec_queued_cnt = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
669 0 : if( FD_LIKELY( exec_queued_cnt>0UL && fd_ulong_popcnt( exec_fully_ready_bitset ) ) ) { /* Optimize for no fork switching. */
670 : /* Transaction execution has the highest priority. Current mainnet
671 : block times are very much dominated by critical path transaction
672 : execution. To achieve the fastest block replay speed, we can't
673 : afford to make any mistake in critical path dispatching. Any
674 : deviation from perfect critical path dispatching is basically
675 : irrecoverable. As such, we try to keep all the exec tiles busy
676 : with transaction execution, but we allow at most one transaction
677 : to be in-flight per exec tile. This is to ensure that whenever a
678 : critical path transaction completes, we have at least one exec
679 : tile, e.g. the one that just completed said transaction, readily
680 : available to continue executing down the critical path. */
681 0 : out->txn_exec->txn_idx = fd_rdisp_get_next_ready( sched->rdisp, bank_idx );
682 0 : if( FD_UNLIKELY( out->txn_exec->txn_idx==0UL ) ) {
683 : /* There are transactions queued but none ready for execution.
684 : This implies that there must be in-flight transactions on whose
685 : completion the queued transactions depend. So we return and
686 : wait for those in-flight transactions to retire. This is a
687 : policy decision to execute as much as we can down the current
688 : fork. */
689 0 : if( FD_UNLIKELY( !block->txn_exec_in_flight_cnt ) ) {
690 0 : print_all( sched, block );
691 0 : FD_LOG_CRIT(( "invariant violation: no ready transaction found but block->txn_exec_in_flight_cnt==0" ));
692 0 : }
693 :
694 : /* Dispatch more sigverify tasks only if at least one exec tile is
695 : executing transactions or completely idle. Allow at most one
696 : sigverify task in-flight per tile, and only dispatch to
697 : completely idle tiles. */
698 0 : ulong sigverify_ready_bitset = exec_fully_ready_bitset;
699 0 : ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
700 0 : if( FD_LIKELY( sigverify_queued_cnt>0UL && fd_ulong_popcnt( sigverify_ready_bitset )>fd_int_if( block->txn_exec_in_flight_cnt>0U, 0, 1 ) ) ) {
701 : /* Dispatch transactions for sigverify in parse order. */
702 0 : int exec_tile_idx_sigverify = fd_ulong_find_lsb( sigverify_ready_bitset );
703 0 : out->task_type = FD_SCHED_TT_TXN_SIGVERIFY;
704 0 : out->txn_sigverify->bank_idx = bank_idx;
705 0 : out->txn_sigverify->txn_idx = block->txn_idx[ block->txn_sigverify_done_cnt+block->txn_sigverify_in_flight_cnt ];
706 0 : out->txn_sigverify->exec_idx = (ulong)exec_tile_idx_sigverify;
707 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx_sigverify );
708 0 : block->txn_sigverify_in_flight_cnt++;
709 0 : if( FD_UNLIKELY( (~sched->txn_exec_ready_bitset[ 0 ])&(~sched->sigverify_ready_bitset[ 0 ])&fd_ulong_mask_lsb( (int)sched->exec_cnt ) ) ) FD_LOG_CRIT(( "invariant violation: txn_exec_ready_bitset 0x%lx sigverify_ready_bitset 0x%lx", sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ] ));
710 0 : return 1UL;
711 0 : }
712 0 : return 0UL;
713 0 : }
714 0 : out->task_type = FD_SCHED_TT_TXN_EXEC;
715 0 : out->txn_exec->bank_idx = bank_idx;
716 0 : out->txn_exec->slot = block->slot;
717 0 : out->txn_exec->exec_idx = exec_tile_idx0;
718 0 : FD_TEST( out->txn_exec->exec_idx!=ULONG_MAX );
719 :
720 0 : long now = fd_tickcount();
721 0 : ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
722 0 : ulong txn_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( exec_ready_bitset0 );
723 0 : sched->metrics->txn_none_in_flight_tickcount += fd_ulong_if( txn_exec_busy_cnt==0UL && sched->txn_in_flight_last_tick!=LONG_MAX, delta, 0UL );
724 0 : sched->metrics->txn_weighted_in_flight_tickcount += fd_ulong_if( txn_exec_busy_cnt!=0UL, delta, 0UL );
725 0 : sched->metrics->txn_weighted_in_flight_cnt += delta*txn_exec_busy_cnt;
726 0 : sched->txn_in_flight_last_tick = now;
727 :
728 0 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_clear_bit( exec_ready_bitset0, (int)exec_tile_idx0);
729 :
730 0 : block->txn_exec_in_flight_cnt++;
731 0 : sched->metrics->txn_max_in_flight_cnt = fd_uint_max( sched->metrics->txn_max_in_flight_cnt, block->txn_exec_in_flight_cnt );
732 :
733 0 : ulong total_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ]&sched->sigverify_ready_bitset[ 0 ] );
734 0 : if( FD_UNLIKELY( (~sched->txn_exec_ready_bitset[ 0 ])&(~sched->sigverify_ready_bitset[ 0 ])&fd_ulong_mask_lsb( (int)sched->exec_cnt ) ) ) FD_LOG_CRIT(( "invariant violation: txn_exec_ready_bitset 0x%lx sigverify_ready_bitset 0x%lx", sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ] ));
735 0 : if( FD_UNLIKELY( block->txn_exec_in_flight_cnt+block->txn_sigverify_in_flight_cnt!=total_exec_busy_cnt ) ) {
736 : /* Ideally we'd simply assert that the two sides of the equation
737 : are equal. But abandoned blocks throw a wrench into this. We
738 : allow abandoned blocks to have in-flight transactions that are
739 : naturally drained while we try to dispatch from another block.
740 : In such cases, the total number of in-flight transactions
741 : should include the abandoned blocks too. The contract is that
742 : blocks with in-flight transactions cannot be abandoned or
743 : demoted from rdisp. So a dying block has to be the head of one
744 : of the staging lanes. */
745 0 : ulong total_in_flight = 0UL;
746 0 : for( int l=0; l<(int)FD_SCHED_MAX_STAGING_LANES; l++ ) {
747 0 : if( fd_ulong_extract_bit( sched->staged_bitset, l ) ) {
748 0 : fd_sched_block_t * staged_block = block_pool_ele( sched, sched->staged_head_bank_idx[ l ] );
749 0 : if( FD_UNLIKELY( block_is_in_flight( staged_block )&&!(staged_block==block||staged_block->dying) ) ) {
750 0 : print_all( sched, staged_block );
751 0 : FD_LOG_CRIT(( "invariant violation: in-flight block is neither active nor dying" ));
752 0 : }
753 0 : total_in_flight += staged_block->txn_exec_in_flight_cnt;
754 0 : total_in_flight += staged_block->txn_sigverify_in_flight_cnt;
755 0 : }
756 0 : }
757 0 : if( FD_UNLIKELY( total_in_flight!=total_exec_busy_cnt ) ) {
758 0 : print_all( sched, block );
759 0 : FD_LOG_CRIT(( "invariant violation: total_in_flight %lu != total_exec_busy_cnt %lu", total_in_flight, total_exec_busy_cnt ));
760 0 : }
761 0 : }
762 0 : return 1UL;
763 0 : }
764 :
765 : /* At this point txn_queued_cnt==0 */
766 :
767 : /* Try to dispatch a sigverify task, but leave one exec tile idle for
768 : critical path execution, unless there's not going to be any more
769 : real transactions for the critical path. In the degenerate case of
770 : only one exec tile, keep it busy. */
771 0 : ulong sigverify_ready_bitset = exec_fully_ready_bitset;
772 0 : ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
773 0 : if( FD_LIKELY( sigverify_queued_cnt>0UL && fd_ulong_popcnt( sigverify_ready_bitset )>fd_int_if( block->fec_eos||block->txn_exec_in_flight_cnt>0U||sched->exec_cnt==1UL, 0, 1 ) ) ) {
774 : /* Dispatch transactions for sigverify in parse order. */
775 0 : int exec_tile_idx_sigverify = fd_ulong_find_lsb( sigverify_ready_bitset );
776 0 : out->task_type = FD_SCHED_TT_TXN_SIGVERIFY;
777 0 : out->txn_sigverify->txn_idx = block->txn_idx[ block->txn_sigverify_done_cnt+block->txn_sigverify_in_flight_cnt ];
778 0 : out->txn_sigverify->bank_idx = bank_idx;
779 0 : out->txn_sigverify->exec_idx = (ulong)exec_tile_idx_sigverify;
780 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx_sigverify );
781 0 : block->txn_sigverify_in_flight_cnt++;
782 0 : if( FD_UNLIKELY( (~sched->txn_exec_ready_bitset[ 0 ])&(~sched->sigverify_ready_bitset[ 0 ])&fd_ulong_mask_lsb( (int)sched->exec_cnt ) ) ) FD_LOG_CRIT(( "invariant violation: txn_exec_ready_bitset 0x%lx sigverify_ready_bitset 0x%lx", sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ] ));
783 0 : return 1UL;
784 0 : }
785 :
786 0 : if( FD_UNLIKELY( block_should_signal_end( block ) ) ) {
787 0 : FD_TEST( block->block_start_signaled );
788 0 : out->task_type = FD_SCHED_TT_BLOCK_END;
789 0 : out->block_end->bank_idx = bank_idx;
790 0 : block->block_end_signaled = 1;
791 0 : return 1UL;
792 0 : }
793 :
794 : /* Nothing queued for the active block. If we haven't received all
795 : the FEC sets for it, then return and wait for more FEC sets, while
796 : there are in-flight transactions. This is a policy decision to
797 : minimize fork churn and allow for executing down the current fork
798 : as much as we can. If we have received all the FEC sets for it,
799 : then we'd still like to return and wait for the in-flight
800 : transactions to retire, before switching to a different block.
801 :
802 : Either way, there should be in-flight transactions. We deactivate
803 : the active block the moment we exhausted transactions from it. */
804 0 : if( FD_UNLIKELY( !block_is_in_flight( block ) ) ) {
805 0 : print_all( sched, block );
806 0 : FD_LOG_CRIT(( "invariant violation: expected in-flight transactions but none" ));
807 0 : }
808 :
809 0 : return 0UL;
810 0 : }
811 :
812 : void
813 0 : fd_sched_task_done( fd_sched_t * sched, ulong task_type, ulong txn_idx, ulong exec_idx ) {
814 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
815 :
816 0 : ulong bank_idx = ULONG_MAX;
817 0 : switch( task_type ) {
818 0 : case FD_SCHED_TT_BLOCK_START:
819 0 : case FD_SCHED_TT_BLOCK_END: {
820 0 : (void)txn_idx;
821 0 : bank_idx = sched->active_bank_idx;
822 0 : break;
823 0 : }
824 0 : case FD_SCHED_TT_TXN_EXEC:
825 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
826 0 : FD_TEST( txn_idx<FD_SCHED_MAX_DEPTH );
827 0 : bank_idx = sched->txn_to_bank_idx[ txn_idx ];
828 0 : break;
829 0 : }
830 0 : default: FD_LOG_CRIT(( "unsupported task_type %lu", task_type ));
831 0 : }
832 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
833 :
834 0 : if( FD_UNLIKELY( !block->staged ) ) {
835 : /* Invariant: only staged blocks can have in-flight transactions. */
836 0 : FD_LOG_CRIT(( "invariant violation: block->staged==0, slot %lu, parent slot %lu",
837 0 : block->slot, block->parent_slot ));
838 0 : }
839 0 : if( FD_UNLIKELY( !block->in_rdisp ) ) {
840 : /* Invariant: staged blocks must be in the dispatcher. */
841 0 : FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, slot %lu, parent slot %lu",
842 0 : block->slot, block->parent_slot ));
843 0 : }
844 :
845 0 : int exec_tile_idx = (int)exec_idx;
846 :
847 0 : switch( task_type ) {
848 0 : case FD_SCHED_TT_BLOCK_START: {
849 0 : FD_TEST( !block->block_start_done );
850 0 : block->block_start_done = 1;
851 0 : break;
852 0 : }
853 0 : case FD_SCHED_TT_BLOCK_END: {
854 : /* It may seem redundant to be invoking task_done() on these
855 : somewhat fake tasks. But these are necessary to drive state
856 : transition for empty blocks or slow blocks. */
857 0 : FD_TEST( !block->block_end_done );
858 0 : block->block_end_done = 1;
859 0 : break;
860 0 : }
861 0 : case FD_SCHED_TT_TXN_EXEC: {
862 0 : long now = fd_tickcount();
863 0 : ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
864 0 : ulong txn_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ] );
865 0 : sched->metrics->txn_weighted_in_flight_tickcount += delta;
866 0 : sched->metrics->txn_weighted_in_flight_cnt += delta*txn_exec_busy_cnt;
867 0 : sched->txn_in_flight_last_tick = now;
868 :
869 0 : block->txn_exec_done_cnt++;
870 0 : block->txn_exec_in_flight_cnt--;
871 0 : sched->metrics->txn_exec_done_cnt++;
872 0 : txn_bitset_insert( sched->exec_done_set, txn_idx );
873 0 : if( txn_bitset_test( sched->exec_done_set, txn_idx ) && txn_bitset_test( sched->sigverify_done_set, txn_idx ) ) {
874 : /* Release txn_idx if both exec and sigverify are done. This is
875 : guaranteed to only happen once per transaction because
876 : whichever one completed first would not release. */
877 0 : fd_rdisp_complete_txn( sched->rdisp, txn_idx, 1 );
878 0 : sched->txn_pool_free_cnt++;
879 0 : block->txn_done_cnt++;
880 0 : sched->metrics->txn_done_cnt++;
881 0 : } else {
882 0 : fd_rdisp_complete_txn( sched->rdisp, txn_idx, 0 );
883 0 : }
884 :
885 0 : FD_TEST( !fd_ulong_extract_bit( sched->txn_exec_ready_bitset[ 0 ], exec_tile_idx ) );
886 0 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->txn_exec_ready_bitset[ 0 ], exec_tile_idx );
887 0 : FD_TEST( block->txn_exec_in_flight_cnt==(uint)(((int)sched->exec_cnt)-fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ] )) );
888 0 : break;
889 0 : }
890 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
891 0 : block->txn_sigverify_done_cnt++;
892 0 : block->txn_sigverify_in_flight_cnt--;
893 0 : sched->metrics->txn_sigverify_done_cnt++;
894 0 : txn_bitset_insert( sched->sigverify_done_set, txn_idx );
895 0 : if( txn_bitset_test( sched->exec_done_set, txn_idx ) && txn_bitset_test( sched->sigverify_done_set, txn_idx ) ) {
896 : /* Release txn_idx if both exec and sigverify are done. This is
897 : guaranteed to only happen once per transaction because
898 : whichever one completed first would not release. */
899 0 : fd_rdisp_complete_txn( sched->rdisp, txn_idx, 1 );
900 0 : sched->txn_pool_free_cnt++;
901 0 : block->txn_done_cnt++;
902 0 : sched->metrics->txn_done_cnt++;
903 0 : }
904 :
905 0 : FD_TEST( !fd_ulong_extract_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx ) );
906 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx );
907 0 : break;
908 0 : }
909 0 : }
910 :
911 0 : if( FD_UNLIKELY( block->dying && !block_is_in_flight( block ) ) ) {
912 0 : if( FD_UNLIKELY( sched->active_bank_idx==bank_idx ) ) {
913 0 : FD_LOG_CRIT(( "invariant violation: active block shouldn't be dying, bank_idx %lu, slot %lu, parent slot %lu",
914 0 : bank_idx, block->slot, block->parent_slot ));
915 0 : }
916 0 : subtree_abandon( sched, block );
917 0 : return;
918 0 : }
919 :
920 0 : if( FD_UNLIKELY( !block->dying && sched->active_bank_idx!=bank_idx ) ) {
921 : /* Block is not dead. So we should be actively replaying it. */
922 0 : fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
923 0 : FD_LOG_CRIT(( "invariant violation: sched->active_bank_idx %lu, slot %lu, parent slot %lu, bank_idx %lu, slot %lu, parent slot %lu",
924 0 : sched->active_bank_idx, active_block->slot, active_block->parent_slot,
925 0 : bank_idx, block->slot, block->parent_slot ));
926 0 : }
927 :
928 0 : maybe_switch_block( sched, bank_idx );
929 0 : }
930 :
931 : void
932 0 : fd_sched_block_abandon( fd_sched_t * sched, ulong bank_idx ) {
933 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
934 0 : FD_TEST( bank_idx<sched->block_cnt_max );
935 :
936 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
937 0 : if( FD_UNLIKELY( bank_idx!=sched->active_bank_idx ) ) {
938 : /* Invariant: abandoning should only be performed on actively
939 : replayed blocks. We impose this requirement on the caller
940 : because the dispatcher expects blocks to be abandoned in the same
941 : order that they were added, and having this requirement makes it
942 : easier to please the dispatcher. */
943 0 : print_all( sched, block );
944 0 : FD_LOG_CRIT(( "invariant violation: active_bank_idx %lu, bank_idx %lu, slot %lu, parent slot %lu",
945 0 : sched->active_bank_idx, bank_idx, block->slot, block->parent_slot ));
946 0 : }
947 :
948 0 : subtree_abandon( sched, block );
949 :
950 : /* Reset the active block. */
951 0 : sched->active_bank_idx = ULONG_MAX;
952 0 : sched->metrics->deactivate_abandoned_cnt++;
953 0 : FD_LOG_INFO(( "block %lu abandoned", block->slot ));
954 0 : print_block_and_parent( sched, block );
955 0 : try_activate_block( sched );
956 0 : }
957 :
958 : void
959 0 : fd_sched_block_add_done( fd_sched_t * sched, ulong bank_idx, ulong parent_bank_idx ) {
960 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
961 0 : FD_TEST( bank_idx<sched->block_cnt_max );
962 :
963 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
964 0 : add_block( sched, bank_idx, parent_bank_idx );
965 0 : block->txn_parsed_cnt = UINT_MAX;
966 0 : block->txn_exec_done_cnt = UINT_MAX;
967 0 : block->txn_sigverify_done_cnt = UINT_MAX;
968 0 : block->txn_done_cnt = UINT_MAX;
969 0 : block->fec_eos = 1;
970 0 : block->block_start_signaled = 1;
971 0 : block->block_end_signaled = 1;
972 0 : block->block_start_done = 1;
973 0 : block->block_end_done = 1;
974 0 : if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
975 : /* Assumes that a NULL parent implies the snapshot slot. */
976 0 : sched->root_idx = bank_idx;
977 0 : block->rooted = 1;
978 0 : }
979 0 : }
980 :
981 : void
982 0 : fd_sched_advance_root( fd_sched_t * sched, ulong root_idx ) {
983 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
984 0 : FD_TEST( root_idx<sched->block_cnt_max );
985 0 : FD_TEST( sched->root_idx<sched->block_cnt_max );
986 :
987 0 : fd_sched_block_t * new_root = block_pool_ele( sched, root_idx );
988 0 : fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
989 0 : if( FD_UNLIKELY( !old_root->rooted ) ) {
990 0 : FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
991 0 : old_root->slot, old_root->parent_slot ));
992 0 : }
993 :
994 : /* Early exit if the new root is the same as the old root. */
995 0 : if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
996 0 : FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
997 0 : new_root->slot, new_root->parent_slot ));
998 0 : return;
999 0 : }
1000 :
1001 0 : fd_sched_block_t * head = old_root;
1002 0 : head->parent_idx = ULONG_MAX;
1003 0 : fd_sched_block_t * tail = head;
1004 :
1005 0 : while( head ) {
1006 0 : FD_TEST( head->in_sched );
1007 0 : head->in_sched = 0;
1008 0 : ulong child_idx = head->child_idx;
1009 0 : while( child_idx!=ULONG_MAX ) {
1010 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1011 : /* Add children to be visited. We abuse the parent_idx field to
1012 : link up the next block to visit. */
1013 0 : if( child!=new_root ) {
1014 0 : tail->parent_idx = child_idx;
1015 0 : tail = child;
1016 0 : tail->parent_idx = ULONG_MAX;
1017 0 : }
1018 0 : child_idx = child->sibling_idx;
1019 0 : }
1020 :
1021 : /* Prune the current block. We will never publish halfway into a
1022 : staging lane, because anything on the rooted fork should have
1023 : finished replaying gracefully and be out of the dispatcher. In
1024 : fact, anything that we are publishing away should be out of the
1025 : dispatcher at this point. And there should be no more in-flight
1026 : transactions. */
1027 0 : if( FD_UNLIKELY( block_is_in_flight( head ) ) ) {
1028 0 : FD_LOG_CRIT(( "invariant violation: block has transactions in flight (%u exec %u sigverify), slot %lu, parent slot %lu",
1029 0 : head->txn_exec_in_flight_cnt, head->txn_sigverify_in_flight_cnt, head->slot, head->parent_slot ));
1030 0 : }
1031 0 : if( FD_UNLIKELY( head->in_rdisp ) ) {
1032 : /* We should have removed it from the dispatcher when we were
1033 : notified of the new root, or when in-flight transactions were
1034 : drained. */
1035 0 : FD_LOG_CRIT(( "invariant violation: block is in the dispatcher, slot %lu, parent slot %lu",
1036 0 : head->slot, head->parent_slot ));
1037 0 : }
1038 0 : fd_sched_block_t * next = block_pool_ele( sched, head->parent_idx );
1039 0 : head = next;
1040 0 : }
1041 :
1042 0 : new_root->parent_idx = ULONG_MAX;
1043 0 : sched->root_idx = root_idx;
1044 0 : }
1045 :
1046 : void
1047 0 : fd_sched_root_notify( fd_sched_t * sched, ulong root_idx ) {
1048 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
1049 0 : FD_TEST( root_idx<sched->block_cnt_max );
1050 0 : FD_TEST( sched->root_idx<sched->block_cnt_max );
1051 :
1052 0 : fd_sched_block_t * block = block_pool_ele( sched, root_idx );
1053 0 : fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
1054 0 : if( FD_UNLIKELY( !old_root->rooted ) ) {
1055 0 : FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
1056 0 : old_root->slot, old_root->parent_slot ));
1057 0 : }
1058 :
1059 : /* Early exit if the new root is the same as the old root. */
1060 0 : if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
1061 0 : FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
1062 0 : block->slot, block->parent_slot ));
1063 0 : return;
1064 0 : }
1065 :
1066 : /* Mark every node from the new root up through its parents to the
1067 : old root as being rooted. */
1068 0 : fd_sched_block_t * curr = block;
1069 0 : fd_sched_block_t * prev = NULL;
1070 0 : while( curr ) {
1071 0 : if( FD_UNLIKELY( !block_is_done( curr ) ) ) {
1072 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is not done, slot %lu, parent slot %lu",
1073 0 : curr->slot, curr->parent_slot ));
1074 0 : }
1075 0 : if( FD_UNLIKELY( curr->dying ) ) {
1076 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is dying, slot %lu, parent slot %lu",
1077 0 : curr->slot, curr->parent_slot ));
1078 0 : }
1079 0 : if( FD_UNLIKELY( curr->staged ) ) {
1080 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is staged, slot %lu, parent slot %lu",
1081 0 : curr->slot, curr->parent_slot ));
1082 0 : }
1083 0 : if( FD_UNLIKELY( curr->in_rdisp ) ) {
1084 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is in the dispatcher, slot %lu, parent slot %lu",
1085 0 : curr->slot, curr->parent_slot ));
1086 0 : }
1087 0 : curr->rooted = 1;
1088 0 : prev = curr;
1089 0 : curr = block_pool_ele( sched, curr->parent_idx );
1090 0 : }
1091 :
1092 : /* If we didn't reach the old root, the new root is not a descendant. */
1093 0 : if( FD_UNLIKELY( prev!=old_root ) ) {
1094 0 : FD_LOG_CRIT(( "invariant violation: new root is not a descendant of old root, new root slot %lu, parent slot %lu, old root slot %lu, parent slot %lu",
1095 0 : block->slot, block->parent_slot, old_root->slot, old_root->parent_slot ));
1096 0 : }
1097 :
1098 0 : ulong old_active_bank_idx = sched->active_bank_idx;
1099 :
1100 : /* Now traverse from old root towards new root, and abandon all
1101 : minority forks. */
1102 0 : curr = old_root;
1103 0 : while( curr && curr->rooted && curr!=block ) { /* curr!=block to avoid abandoning good forks. */
1104 0 : fd_sched_block_t * rooted_child_block = NULL;
1105 0 : ulong child_idx = curr->child_idx;
1106 0 : while( child_idx!=ULONG_MAX ) {
1107 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1108 0 : if( child->rooted ) {
1109 0 : rooted_child_block = child;
1110 0 : } else {
1111 : /* This is a minority fork. */
1112 0 : subtree_abandon( sched, child );
1113 0 : }
1114 0 : child_idx = child->sibling_idx;
1115 0 : }
1116 0 : curr = rooted_child_block;
1117 0 : }
1118 :
1119 : /* If the active block got abandoned, we need to reset it. */
1120 0 : if( sched->active_bank_idx==ULONG_MAX ) {
1121 0 : sched->metrics->deactivate_pruned_cnt += fd_uint_if( old_active_bank_idx!=ULONG_MAX, 1U, 0U );
1122 0 : try_activate_block( sched );
1123 0 : }
1124 0 : }
1125 :
1126 : fd_txn_p_t *
1127 0 : fd_sched_get_txn( fd_sched_t * sched, ulong txn_idx ) {
1128 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
1129 0 : if( FD_UNLIKELY( txn_idx>=FD_SCHED_MAX_DEPTH ) ) {
1130 0 : return NULL;
1131 0 : }
1132 0 : return sched->txn_pool+txn_idx;
1133 0 : }
1134 :
1135 : fd_hash_t *
1136 0 : fd_sched_get_poh( fd_sched_t * sched, ulong bank_idx ) {
1137 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
1138 0 : FD_TEST( bank_idx<sched->block_cnt_max );
1139 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1140 0 : return &block->poh;
1141 0 : }
1142 :
1143 : uint
1144 0 : fd_sched_get_shred_cnt( fd_sched_t * sched, ulong bank_idx ) {
1145 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
1146 0 : FD_TEST( bank_idx<sched->block_cnt_max );
1147 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1148 0 : return block->shred_cnt;
1149 0 : }
1150 :
1151 0 : void * fd_sched_leave ( fd_sched_t * sched ) { return sched; }
1152 0 : void * fd_sched_delete( void * mem ) { return mem; }
1153 :
1154 :
1155 : /* Internal helpers. */
1156 :
1157 : static void
1158 : add_block( fd_sched_t * sched,
1159 : ulong bank_idx,
1160 0 : ulong parent_bank_idx ) {
1161 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1162 0 : FD_TEST( !block->in_sched );
1163 :
1164 0 : block->txn_parsed_cnt = 0U;
1165 0 : block->txn_exec_in_flight_cnt = 0U;
1166 0 : block->txn_exec_done_cnt = 0U;
1167 0 : block->txn_sigverify_in_flight_cnt = 0U;
1168 0 : block->txn_sigverify_done_cnt = 0U;
1169 0 : block->txn_done_cnt = 0U;
1170 0 : block->shred_cnt = 0U;
1171 :
1172 0 : block->mblks_rem = 0UL;
1173 0 : block->txns_rem = 0UL;
1174 0 : block->fec_buf_sz = 0U;
1175 0 : block->fec_buf_soff = 0U;
1176 0 : block->fec_eob = 0;
1177 0 : block->fec_sob = 1;
1178 :
1179 0 : block->fec_eos = 0;
1180 0 : block->rooted = 0;
1181 0 : block->dying = 0;
1182 0 : block->in_sched = 1;
1183 0 : block->in_rdisp = 0;
1184 0 : block->block_start_signaled = 0;
1185 0 : block->block_end_signaled = 0;
1186 0 : block->block_start_done = 0;
1187 0 : block->block_end_done = 0;
1188 0 : block->staged = 0;
1189 :
1190 0 : block->luf_depth = 0UL;
1191 :
1192 : /* New leaf node, no child, no sibling. */
1193 0 : block->child_idx = ULONG_MAX;
1194 0 : block->sibling_idx = ULONG_MAX;
1195 0 : block->parent_idx = ULONG_MAX;
1196 :
1197 0 : if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
1198 0 : return;
1199 0 : }
1200 :
1201 : /* node->parent link */
1202 0 : fd_sched_block_t * parent_block = block_pool_ele( sched, parent_bank_idx );
1203 0 : block->parent_idx = parent_bank_idx;
1204 :
1205 : /* parent->node and sibling->node links */
1206 0 : ulong child_idx = bank_idx;
1207 0 : if( FD_LIKELY( parent_block->child_idx==ULONG_MAX ) ) { /* Optimize for no forking. */
1208 0 : parent_block->child_idx = child_idx;
1209 0 : } else {
1210 0 : fd_sched_block_t * curr_block = block_pool_ele( sched, parent_block->child_idx );
1211 0 : while( curr_block->sibling_idx!=ULONG_MAX ) {
1212 0 : curr_block = block_pool_ele( sched, curr_block->sibling_idx );
1213 0 : }
1214 0 : curr_block->sibling_idx = child_idx;
1215 0 : }
1216 :
1217 0 : if( FD_UNLIKELY( parent_block->dying ) ) {
1218 0 : block->dying = 1;
1219 0 : }
1220 0 : }
1221 :
1222 0 : #define CHECK( cond ) do { \
1223 0 : if( FD_UNLIKELY( !(cond) ) ) { \
1224 0 : return FD_SCHED_PARSER_AGAIN_LATER; \
1225 0 : } \
1226 0 : } while( 0 )
1227 :
1228 : /* CHECK that it is safe to read at least n more bytes. */
1229 0 : #define CHECK_LEFT( n ) CHECK( (n)<=(block->fec_buf_sz-block->fec_buf_soff) )
1230 :
1231 : /* Consume as much as possible from the buffer. By the end of this
1232 : function, we will either have residual data that is unparseable only
1233 : because it is a batch that straddles FEC set boundaries, or we will
1234 : have reached the end of a batch. In the former case, any remaining
1235 : bytes should be concatenated with the next FEC set for further
1236 : parsing. In the latter case, any remaining bytes should be thrown
1237 : away. */
1238 : FD_WARN_UNUSED static int
1239 0 : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
1240 0 : while( 1 ) {
1241 0 : while( block->txns_rem>0UL ) {
1242 0 : int err;
1243 0 : if( FD_UNLIKELY( (err=fd_sched_parse_txn( sched, block, alut_ctx ))!=FD_SCHED_PARSER_OK ) ) {
1244 0 : return err;
1245 0 : }
1246 0 : }
1247 0 : if( block->txns_rem==0UL && block->mblks_rem>0UL ) {
1248 0 : CHECK_LEFT( sizeof(fd_microblock_hdr_t) );
1249 0 : fd_microblock_hdr_t * hdr = (fd_microblock_hdr_t *)fd_type_pun( block->fec_buf+block->fec_buf_soff );
1250 0 : block->fec_buf_soff += (uint)sizeof(fd_microblock_hdr_t);
1251 :
1252 0 : memcpy( block->poh.hash, hdr->hash, sizeof(block->poh.hash) );
1253 0 : block->txns_rem = hdr->txn_cnt;
1254 0 : block->mblks_rem--;
1255 0 : continue;
1256 0 : }
1257 0 : if( block->txns_rem==0UL && block->mblks_rem==0UL && block->fec_sob ) {
1258 0 : CHECK_LEFT( sizeof(ulong) );
1259 0 : FD_TEST( block->fec_buf_soff==0U );
1260 0 : block->mblks_rem = FD_LOAD( ulong, block->fec_buf );
1261 0 : block->fec_buf_soff += (uint)sizeof(ulong);
1262 : /* FIXME what happens if someone sends us mblks_rem==0UL here? */
1263 :
1264 0 : block->fec_sob = 0;
1265 0 : continue;
1266 0 : }
1267 0 : if( block->txns_rem==0UL && block->mblks_rem==0UL ) {
1268 0 : break;
1269 0 : }
1270 0 : }
1271 0 : if( block->fec_eob ) {
1272 : /* Ignore trailing bytes at the end of a batch. */
1273 0 : sched->metrics->bytes_ingested_unparsed_cnt += block->fec_buf_sz-block->fec_buf_soff;
1274 0 : block->fec_buf_soff = 0U;
1275 0 : block->fec_buf_sz = 0U;
1276 0 : block->fec_sob = 1;
1277 0 : block->fec_eob = 0;
1278 0 : }
1279 0 : return FD_SCHED_PARSER_OK;
1280 0 : }
1281 :
1282 : FD_WARN_UNUSED static int
1283 0 : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
1284 0 : fd_txn_t * txn = fd_type_pun( block->txn );
1285 :
1286 0 : ulong pay_sz = 0UL;
1287 0 : ulong txn_sz = fd_txn_parse_core( block->fec_buf+block->fec_buf_soff,
1288 0 : fd_ulong_min( FD_TXN_MTU, block->fec_buf_sz-block->fec_buf_soff ),
1289 0 : txn,
1290 0 : NULL,
1291 0 : &pay_sz );
1292 :
1293 0 : if( FD_UNLIKELY( !pay_sz || !txn_sz ) ) {
1294 : /* Can't parse out a full transaction. */
1295 0 : return FD_SCHED_PARSER_AGAIN_LATER;
1296 0 : }
1297 :
1298 0 : if( FD_UNLIKELY( block->txn_parsed_cnt>=FD_MAX_TXN_PER_SLOT ) ) {
1299 : /* The block contains more transactions than a valid block would.
1300 : Mark the block dead instead of keep processing it. */
1301 0 : return FD_SCHED_PARSER_BAD_BLOCK;
1302 0 : }
1303 :
1304 : /* Try to expand ALUTs. */
1305 0 : int has_aluts = txn->transaction_version==FD_TXN_V0 && txn->addr_table_adtl_cnt>0;
1306 0 : int serializing = 0;
1307 0 : if( has_aluts ) {
1308 0 : uchar __attribute__((aligned(FD_SLOT_HASHES_GLOBAL_ALIGN))) slot_hashes_mem[ FD_SYSVAR_SLOT_HASHES_FOOTPRINT ];
1309 0 : fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_slot_hashes_read( alut_ctx->accdb->funk, alut_ctx->xid, slot_hashes_mem );
1310 0 : if( FD_LIKELY( slot_hashes_global ) ) {
1311 0 : fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar *)slot_hashes_global + slot_hashes_global->hashes_offset );
1312 0 : serializing = !!fd_runtime_load_txn_address_lookup_tables( txn, block->fec_buf+block->fec_buf_soff, alut_ctx->accdb->funk, alut_ctx->xid, alut_ctx->els, slot_hash, block->aluts );
1313 0 : sched->metrics->alut_success_cnt += (uint)!serializing;
1314 0 : } else {
1315 0 : serializing = 1;
1316 0 : }
1317 0 : }
1318 :
1319 0 : ulong bank_idx = (ulong)(block-sched->block_pool);
1320 0 : ulong txn_idx = fd_rdisp_add_txn( sched->rdisp, bank_idx, txn, block->fec_buf+block->fec_buf_soff, serializing ? NULL : block->aluts, serializing );
1321 0 : FD_TEST( txn_idx!=0UL );
1322 0 : sched->metrics->txn_parsed_cnt++;
1323 0 : sched->metrics->alut_serializing_cnt += (uint)serializing;
1324 0 : sched->txn_pool_free_cnt--;
1325 0 : fd_txn_p_t * txn_p = sched->txn_pool + txn_idx;
1326 0 : txn_p->payload_sz = pay_sz;
1327 0 : fd_memcpy( txn_p->payload, block->fec_buf+block->fec_buf_soff, pay_sz );
1328 0 : fd_memcpy( TXN(txn_p), txn, txn_sz );
1329 0 : sched->txn_to_bank_idx[ txn_idx ] = bank_idx;
1330 0 : txn_bitset_remove( sched->exec_done_set, txn_idx );
1331 0 : txn_bitset_remove( sched->sigverify_done_set, txn_idx );
1332 0 : block->txn_idx[ block->txn_parsed_cnt ] = txn_idx;
1333 0 : block->fec_buf_soff += (uint)pay_sz;
1334 0 : block->txn_parsed_cnt++;
1335 : #if FD_SCHED_SKIP_SIGVERIFY
1336 : txn_bitset_insert( sched->sigverify_done_set, txn_idx );
1337 : block->txn_sigverify_done_cnt++;
1338 : #endif
1339 0 : block->txns_rem--;
1340 0 : return FD_SCHED_PARSER_OK;
1341 0 : }
1342 :
1343 : #undef CHECK
1344 : #undef CHECK_LEFT
1345 :
1346 : static void
1347 0 : try_activate_block( fd_sched_t * sched ) {
1348 :
1349 : /* See if there are any allocated staging lanes that we can activate
1350 : for scheduling ... */
1351 0 : ulong staged_bitset = sched->staged_bitset;
1352 0 : while( staged_bitset ) {
1353 0 : int lane_idx = fd_ulong_find_lsb( staged_bitset );
1354 0 : staged_bitset = fd_ulong_pop_lsb( staged_bitset );
1355 :
1356 0 : ulong head_idx = sched->staged_head_bank_idx[ lane_idx ];
1357 0 : fd_sched_block_t * head_block = block_pool_ele( sched, head_idx );
1358 0 : fd_sched_block_t * parent_block = block_pool_ele( sched, head_block->parent_idx );
1359 0 : if( FD_UNLIKELY( parent_block->dying ) ) {
1360 : /* Invariant: no child of a dying block should be staged. */
1361 0 : FD_LOG_CRIT(( "invariant violation: staged_head_bank_idx %lu, slot %lu, parent slot %lu on lane %d has parent_block->dying set, slot %lu, parent slot %lu",
1362 0 : head_idx, head_block->slot, head_block->parent_slot, lane_idx, parent_block->slot, parent_block->parent_slot ));
1363 0 : }
1364 : //FIXME: restore this invariant check when we have immediate demotion of dying blocks
1365 : // if( FD_UNLIKELY( head_block->dying ) ) {
1366 : // /* Invariant: no dying block should be staged. */
1367 : // FD_LOG_CRIT(( "invariant violation: staged_head_bank_idx %lu, slot %lu, prime %lu on lane %u has head_block->dying set",
1368 : // head_idx, (ulong)head_block->block_id.slot, (ulong)head_block->block_id.prime, lane_idx ));
1369 : // }
1370 0 : if( block_is_done( parent_block ) && block_is_activatable( head_block ) ) {
1371 : /* ... Yes, on this staging lane the parent block is done. So we
1372 : can switch to the staged child. */
1373 0 : sched->active_bank_idx = head_idx;
1374 0 : sched->metrics->lane_switch_cnt++;
1375 0 : return;
1376 0 : }
1377 0 : }
1378 :
1379 : /* ... No, promote unstaged blocks. */
1380 0 : ulong root_idx = sched->root_idx;
1381 0 : if( FD_UNLIKELY( root_idx==ULONG_MAX ) ) {
1382 0 : FD_LOG_CRIT(( "invariant violation: root_idx==ULONG_MAX indicating fd_sched is unintialized" ));
1383 0 : }
1384 : /* Find and stage the longest stageable unstaged fork. This is a
1385 : policy decision. */
1386 0 : ulong depth = compute_longest_unstaged_fork( sched, root_idx );
1387 0 : if( FD_LIKELY( depth>0UL ) ) {
1388 0 : if( FD_UNLIKELY( sched->staged_bitset==fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) {
1389 : /* No more staging lanes available. All of them are occupied by
1390 : slow squatters. Demote one of them. */
1391 : //FIXME implement this, note that only empty blocks can be
1392 : //demoted, and so blocks with in-flight transactions, including
1393 : //dying in-flight blocks, shouldn't be demoted
1394 0 : FD_LOG_CRIT(( "unimplemented" ));
1395 0 : sched->metrics->lane_demoted_cnt++;
1396 : // sched->metrics->block_demoted_cnt++; for every demoted block
1397 0 : }
1398 0 : FD_TEST( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) );
1399 0 : int lane_idx = fd_ulong_find_lsb( ~sched->staged_bitset );
1400 0 : if( FD_UNLIKELY( lane_idx>=(int)FD_SCHED_MAX_STAGING_LANES ) ) {
1401 0 : FD_LOG_CRIT(( "invariant violation: lane_idx %d, sched->staged_bitset %lx",
1402 0 : lane_idx, sched->staged_bitset ));
1403 0 : }
1404 0 : ulong head_bank_idx = stage_longest_unstaged_fork( sched, root_idx, lane_idx );
1405 0 : if( FD_UNLIKELY( head_bank_idx==ULONG_MAX ) ) {
1406 : /* We found a promotable fork depth>0. This should not happen. */
1407 0 : FD_LOG_CRIT(( "invariant violation: head_bank_idx==ULONG_MAX" ));
1408 0 : }
1409 : /* We don't bother with promotion unless the block is immediately
1410 : dispatchable. So it's okay to set the active block here. */
1411 0 : sched->active_bank_idx = head_bank_idx;
1412 0 : return;
1413 0 : }
1414 : /* No unstaged blocks to promote. So we're done. Yay. */
1415 0 : }
1416 :
1417 : static void
1418 0 : check_or_set_active_block( fd_sched_t * sched ) {
1419 0 : if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
1420 0 : try_activate_block( sched );
1421 0 : } else {
1422 0 : fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
1423 0 : if( FD_UNLIKELY( block_should_deactivate( active_block ) ) ) {
1424 0 : print_all( sched, active_block );
1425 0 : FD_LOG_CRIT(( "invariant violation: should have been deactivated" ));
1426 0 : }
1427 0 : }
1428 0 : }
1429 :
1430 : /* It's safe to call this function more than once on the same block. */
1431 : static void
1432 0 : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block ) {
1433 0 : if( FD_UNLIKELY( block->rooted ) ) {
1434 0 : FD_LOG_CRIT(( "invariant violation: rooted block should not be abandoned, slot %lu, parent slot %lu",
1435 0 : block->slot, block->parent_slot ));
1436 0 : }
1437 : /* All minority fork nodes pass through this function eventually. So
1438 : this is a good point to check per-node invariants for minority
1439 : forks. */
1440 0 : if( FD_UNLIKELY( block->staged && !block->in_rdisp ) ) {
1441 0 : FD_LOG_CRIT(( "invariant violation: staged block is not in the dispatcher, slot %lu, parent slot %lu",
1442 0 : block->slot, block->parent_slot ));
1443 0 : }
1444 :
1445 : /* Setting the flag is non-optional and can happen more than once. */
1446 0 : block->dying = 1;
1447 :
1448 : /* Removal from dispatcher should only happen once. */
1449 0 : if( block->in_rdisp ) {
1450 0 : fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
1451 0 : if( FD_UNLIKELY( !parent ) ) {
1452 : /* Only the root has no parent. Abandon should never be called on
1453 : the root. So any block we are trying to abandon should have a
1454 : parent. */
1455 0 : FD_LOG_CRIT(( "invariant violation: parent not found slot %lu, parent slot %lu",
1456 0 : block->slot, block->parent_slot ));
1457 0 : }
1458 :
1459 : /* The dispatcher expects blocks to be abandoned in the same order
1460 : that they were added on each lane. There are no requirements on
1461 : the order of abandoning if two blocks are not on the same lane,
1462 : or if a block is unstaged. This means that in general we
1463 : shouldn't abandon a child block if the parent hasn't been
1464 : abandoned yet, if and only if they are on the same lane. So wait
1465 : until we can abandon the parent, and then descend down the fork
1466 : tree to ensure orderly abandoning. */
1467 0 : int in_order = !parent->in_rdisp || /* parent is not in the dispatcher */
1468 0 : !parent->staged || /* parent is in the dispatcher but not staged */
1469 0 : !block->staged || /* parent is in the dispatcher and staged but this block is unstaged */
1470 0 : block->staging_lane!=parent->staging_lane; /* this block is on a different staging lane than its parent */
1471 :
1472 : /* We inform the dispatcher of an abandon only when there are no
1473 : more in-flight transactions. Otherwise, if the dispatcher
1474 : recycles the same txn_id that was just abandoned, and we receive
1475 : completion of an in-flight transaction whose txn_id was just
1476 : recycled, we would basically be aliasing the same txn_id and end
1477 : up indexing into txn_to_bank_idx[] that is already overwritten
1478 : with new blocks. */
1479 0 : int abandon = in_order && block->txn_exec_in_flight_cnt==0 && block->txn_sigverify_in_flight_cnt==0;
1480 :
1481 0 : if( abandon ) {
1482 0 : block->in_rdisp = 0;
1483 0 : fd_rdisp_abandon_block( sched->rdisp, (ulong)(block-sched->block_pool) );
1484 0 : sched->txn_pool_free_cnt += block->txn_parsed_cnt-block->txn_done_cnt; /* in_flight_cnt==0 */
1485 0 : sched->metrics->block_abandoned_cnt++;
1486 0 : sched->metrics->txn_abandoned_parsed_cnt += block->txn_parsed_cnt;
1487 0 : sched->metrics->txn_abandoned_exec_done_cnt += block->txn_exec_done_cnt;
1488 0 : sched->metrics->txn_abandoned_done_cnt += block->txn_done_cnt;
1489 :
1490 : /* Now release the staging lane. */
1491 : //FIXME when demote supports non-empty blocks, we should demote
1492 : //the block from the lane unconditionally and immediately,
1493 : //regardles of whether it's safe to abandon or not. So a block
1494 : //would go immediately from staged to unstaged and eventually to
1495 : //abandoned.
1496 0 : if( FD_LIKELY( block->staged ) ) {
1497 0 : block->staged = 0;
1498 0 : sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
1499 0 : sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
1500 0 : }
1501 0 : }
1502 :
1503 0 : if( FD_UNLIKELY( in_order && block->staged && sched->active_bank_idx==sched->staged_head_bank_idx[ block->staging_lane ] ) ) {
1504 0 : FD_TEST( block_pool_ele( sched, sched->active_bank_idx )==block );
1505 : /* Dying blocks should not be active. */
1506 0 : sched->active_bank_idx = ULONG_MAX;
1507 0 : }
1508 0 : }
1509 :
1510 : /* Abandon the entire fork chaining off of this block. */
1511 0 : ulong child_idx = block->child_idx;
1512 0 : while( child_idx!=ULONG_MAX ) {
1513 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1514 0 : subtree_abandon( sched, child );
1515 0 : child_idx = child->sibling_idx;
1516 0 : }
1517 0 : }
1518 :
1519 : static void
1520 0 : maybe_switch_block( fd_sched_t * sched, ulong bank_idx ) {
1521 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1522 0 : if( FD_UNLIKELY( block_is_done( block ) ) ) {
1523 0 : block->in_rdisp = 0;
1524 0 : block->staged = 0;
1525 0 : fd_rdisp_remove_block( sched->rdisp, bank_idx );
1526 0 : sched->metrics->block_removed_cnt++;
1527 :
1528 : /* See if there is a child block down the same staging lane. This
1529 : is a policy decision to minimize fork churn. We could in theory
1530 : reevaluate staging lane allocation here and do promotion/demotion
1531 : as needed. */
1532 0 : ulong child_idx = block->child_idx;
1533 0 : while( child_idx!=ULONG_MAX ) {
1534 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1535 0 : if( FD_LIKELY( child->staged && child->staging_lane==block->staging_lane ) ) {
1536 : /* There is a child block down the same staging lane ... */
1537 0 : if( FD_LIKELY( !child->dying ) ) {
1538 : /* ... and the child isn't dead */
1539 0 : if( FD_UNLIKELY( !block_is_activatable( child ) ) ) {
1540 : /* ... but the child is not activatable, likely because
1541 : there are no transactions available yet. */
1542 0 : sched->active_bank_idx = ULONG_MAX;
1543 0 : sched->metrics->deactivate_no_txn_cnt++;
1544 0 : try_activate_block( sched );
1545 0 : return;
1546 0 : }
1547 : /* ... and it's immediately dispatchable, so switch the active
1548 : block to it, and have the child inherit the head status of
1549 : the lane. This is the common case. */
1550 0 : sched->active_bank_idx = child_idx;
1551 0 : sched->staged_head_bank_idx[ block->staging_lane ] = child_idx;
1552 0 : if( FD_UNLIKELY( !fd_ulong_extract_bit( sched->staged_bitset, (int)block->staging_lane ) ) ) {
1553 0 : FD_LOG_CRIT(( "invariant violation: staged_bitset 0x%lx bit %lu is not set, slot %lu, parent slot %lu, child slot %lu, parent slot %lu",
1554 0 : sched->staged_bitset, block->staging_lane, block->slot, block->parent_slot, child->slot, child->parent_slot ));
1555 0 : }
1556 0 : return;
1557 0 : } else {
1558 : /* ... but the child block is considered dead, likely because
1559 : the parser considers it invalid. */
1560 0 : subtree_abandon( sched, child );
1561 0 : break;
1562 0 : }
1563 0 : }
1564 0 : child_idx = child->sibling_idx;
1565 0 : }
1566 : /* There isn't a child block down the same staging lane. This is
1567 : the last block in the staging lane. Release the staging lane. */
1568 0 : sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
1569 0 : sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
1570 :
1571 : /* Reset the active block. */
1572 0 : sched->active_bank_idx = ULONG_MAX;
1573 0 : sched->metrics->deactivate_no_child_cnt++;
1574 0 : try_activate_block( sched );
1575 0 : } else if( block_should_deactivate( block ) ) {
1576 : /* We exhausted the active block, but it's not fully done yet. We
1577 : are just not getting FEC sets for it fast enough. This could
1578 : happen when the network path is congested, or when the leader
1579 : simply went down. Reset the active block. */
1580 0 : sched->active_bank_idx = ULONG_MAX;
1581 0 : sched->metrics->deactivate_no_txn_cnt++;
1582 0 : try_activate_block( sched );
1583 0 : }
1584 0 : }
1585 :
1586 : FD_FN_UNUSED static ulong
1587 0 : find_and_stage_longest_unstaged_fork( fd_sched_t * sched, int lane_idx ) {
1588 0 : ulong root_idx = sched->root_idx;
1589 0 :
1590 0 : if( FD_UNLIKELY( root_idx==ULONG_MAX ) ) {
1591 0 : FD_LOG_CRIT(( "invariant violation: root_idx==ULONG_MAX indicating fd_sched is unintialized" ));
1592 0 : }
1593 0 :
1594 0 : /* First pass: compute the longest unstaged fork depth for each node
1595 0 : in the fork tree. */
1596 0 : ulong depth = compute_longest_unstaged_fork( sched, root_idx );
1597 0 :
1598 0 : /* Second pass: stage blocks on the longest unstaged fork. */
1599 0 : ulong head_bank_idx = stage_longest_unstaged_fork( sched, root_idx, lane_idx );
1600 0 :
1601 0 : if( FD_UNLIKELY( (depth>0UL && head_bank_idx==ULONG_MAX) || (depth==0UL && head_bank_idx!=ULONG_MAX) ) ) {
1602 0 : FD_LOG_CRIT(( "invariant violation: depth %lu, head_bank_idx %lu",
1603 0 : depth, head_bank_idx ));
1604 0 : }
1605 0 :
1606 0 : return head_bank_idx;
1607 0 : }
1608 :
1609 : /* Returns length of the longest stageable unstaged fork, if there is
1610 : one, and 0 otherwise. */
1611 : static ulong
1612 0 : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx ) {
1613 0 : if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) {
1614 0 : FD_LOG_CRIT(( "invariant violation: bank_idx==ULONG_MAX" ));
1615 0 : }
1616 :
1617 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1618 :
1619 0 : ulong max_child_depth = 0UL;
1620 0 : ulong child_idx = block->child_idx;
1621 0 : while( child_idx!=ULONG_MAX ) {
1622 0 : ulong child_depth = compute_longest_unstaged_fork( sched, child_idx );
1623 0 : if( child_depth > max_child_depth ) {
1624 0 : max_child_depth = child_depth;
1625 0 : }
1626 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1627 0 : child_idx = child->sibling_idx;
1628 0 : }
1629 :
1630 0 : block->luf_depth = max_child_depth + fd_ulong_if( block_is_promotable( block ), 1UL, 0UL );
1631 0 : return block->luf_depth;
1632 0 : }
1633 :
1634 : static ulong
1635 0 : stage_longest_unstaged_fork_helper( fd_sched_t * sched, ulong bank_idx, int lane_idx ) {
1636 0 : if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) {
1637 0 : FD_LOG_CRIT(( "invariant violation: bank_idx==ULONG_MAX" ));
1638 0 : }
1639 :
1640 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1641 :
1642 0 : int stage_it = fd_int_if( block_is_promotable( block ), 1, 0 );
1643 0 : ulong rv = fd_ulong_if( stage_it, bank_idx, ULONG_MAX );
1644 0 : if( FD_LIKELY( stage_it ) ) {
1645 0 : block->staged = 1;
1646 0 : block->staging_lane = (ulong)lane_idx;
1647 0 : fd_rdisp_promote_block( sched->rdisp, bank_idx, block->staging_lane );
1648 0 : sched->metrics->block_promoted_cnt++;
1649 0 : }
1650 :
1651 : /* Base case: leaf node. */
1652 0 : if( block->child_idx==ULONG_MAX ) return rv;
1653 :
1654 0 : ulong max_depth = 0UL;
1655 0 : ulong best_child_idx = ULONG_MAX;
1656 0 : ulong child_idx = block->child_idx;
1657 0 : while( child_idx!=ULONG_MAX ) {
1658 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
1659 0 : if( child->luf_depth>max_depth ) {
1660 0 : max_depth = child->luf_depth;
1661 0 : best_child_idx = child_idx;
1662 0 : }
1663 0 : child_idx = child->sibling_idx;
1664 0 : }
1665 :
1666 : /* Recursively stage descendants. */
1667 0 : if( best_child_idx!=ULONG_MAX ) {
1668 0 : ulong head_bank_idx = stage_longest_unstaged_fork_helper( sched, best_child_idx, lane_idx );
1669 0 : rv = fd_ulong_if( rv!=ULONG_MAX, rv, head_bank_idx );
1670 0 : }
1671 :
1672 0 : return rv;
1673 0 : }
1674 :
1675 : /* Returns idx of head block of staged lane on success, idx_null
1676 : otherwise. */
1677 : static ulong
1678 0 : stage_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx ) {
1679 0 : ulong head_bank_idx = stage_longest_unstaged_fork_helper( sched, bank_idx, lane_idx );
1680 0 : if( FD_LIKELY( head_bank_idx!=ULONG_MAX ) ) {
1681 0 : sched->metrics->lane_promoted_cnt++;
1682 0 : sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
1683 0 : sched->staged_head_bank_idx[ lane_idx ] = head_bank_idx;
1684 0 : }
1685 0 : return head_bank_idx;
1686 0 : }
|