Line data Source code
1 : #include <stdio.h> /* for vsnprintf */
2 : #include <stdarg.h> /* for va_list */
3 :
4 : #include "fd_sched.h"
5 : #include "../../util/fd_hash32.h" /* for lthash lane maps */
6 : #include "../../flamenco/alpenglow/fd_block_marker_serde.h"
7 : #include "fd_execrp.h" /* for poh hash value */
8 : #include "../../ballet/sha256/fd_sha256.h"
9 : #include "../../disco/fd_disco_base.h" /* for FD_MAX_TXN_PER_SLOT_SHRED */
10 : #include "../../disco/fd_txn_p.h"
11 : #include "../../disco/metrics/fd_metrics.h" /* for fd_metrics_convert_ticks_to_nanoseconds and etc. */
12 : #include "../../disco/pack/fd_chkdup.h"
13 : #include "../../disco/shred/fd_shredder.h" /* FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ */
14 : #include "../../flamenco/runtime/fd_runtime.h" /* for fd_runtime_load_txn_address_lookup_tables */
15 : #include "../../flamenco/runtime/fd_system_ids.h"
16 : #include "../../flamenco/runtime/sysvar/fd_sysvar_slot_hashes.h" /* for ALUTs */
17 :
18 24 : #define FD_SCHED_MAX_PRINT_BUF_SZ (2UL<<20)
19 :
20 : /* 64 ticks per slot, and a single gigantic microblock containing min
21 : size transactions. */
22 : 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 );
23 :
24 : /* We size the buffer to be able to hold residual data from the previous
25 : FEC set that only becomes parseable after the next FEC set is
26 : ingested, as well as the incoming FEC set. The largest minimally
27 : parseable unit of data is a transaction. So that much data may
28 : straddle FEC set boundaries. Other minimally parseable units of data
29 : include the microblock header and the microblock count within a
30 : batch. */
31 0 : #define FD_SCHED_MAX_PAYLOAD_PER_FEC (63985UL)
32 : FD_STATIC_ASSERT( FD_SCHED_MAX_PAYLOAD_PER_FEC>=FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ, bump sched fec size bound ); /* FD_SHREDDER_CHAINED_FEC_SET_PAYLOAD_SZ is the bound we want, but older ledgers have larger FEC sets. */
33 : #define FD_SCHED_MAX_RESIDUAL_SZ (FD_TXN_MTU>FD_BLOCK_MARKER_SER_MAX ? FD_TXN_MTU : FD_BLOCK_MARKER_SER_MAX)
34 : #define FD_SCHED_MAX_FEC_BUF_SZ (FD_SCHED_MAX_PAYLOAD_PER_FEC+FD_SCHED_MAX_RESIDUAL_SZ)
35 : FD_STATIC_ASSERT( FD_SCHED_MAX_RESIDUAL_SZ>=sizeof(fd_microblock_hdr_t), resize buffer for residual data );
36 : FD_STATIC_ASSERT( FD_SCHED_MAX_RESIDUAL_SZ>=sizeof(ulong), resize buffer for residual data );
37 :
38 : /* Shred payload sizes are kept per block only for the shreds that the
39 : parser's shred scan has not passed yet. See shred_split(). The scan
40 : trails the parse position by at most the residual, which fits in
41 : FD_SCHED_MAX_RESIDUAL_SZ, and an ingest appends one FEC set. So this
42 : covers any FEC set made of sanely sized shreds. Only a block of
43 : absurdly small shreds has more of them outstanding at once. Such a
44 : block loses its shred attribution, which is monitoring only. */
45 267191 : #define FD_SCHED_SHRED_SZ_WINDOW (256UL)
46 : FD_STATIC_ASSERT( (FD_SCHED_SHRED_SZ_WINDOW&(FD_SCHED_SHRED_SZ_WINDOW-1UL))==0UL, shred_sz_window );
47 :
48 0 : #define FD_SCHED_MAX_TXN_PER_FEC ((FD_SCHED_MAX_PAYLOAD_PER_FEC-1UL)/FD_TXN_MIN_SERIALIZED_SZ+1UL) /* 478 */
49 0 : #define FD_SCHED_MAX_MBLK_PER_FEC ((FD_SCHED_MAX_PAYLOAD_PER_FEC-1UL)/sizeof(fd_microblock_hdr_t)+1UL) /* 1334 */
50 :
51 : FD_STATIC_ASSERT( FD_SCHED_MIN_DEPTH>=FD_SCHED_MAX_TXN_PER_FEC, limits );
52 : FD_STATIC_ASSERT( FD_SCHED_MAX_DEPTH<=FD_RDISP_MAX_DEPTH, limits );
53 : FD_STATIC_ASSERT( FD_SCHED_MAX_DEPTH<=UINT_MAX, txn_idx_width );
54 :
55 :
56 : /* Out-of-band LtHash.
57 :
58 : Recall that a bank's LtHash is the sum over every account of the hash
59 : of its current value. A block that writes an account has to subtract
60 : the hash of the value it found and add the hash of the value it
61 : leaves. In-band, the exec tile does both inside the commit phase of
62 : every writing transaction, so the hashing sits on the critical path
63 : of the block. Our goal is to take it off that path and hide it in
64 : idle exec tile cycles instead.
65 :
66 : The two hashes have different constraints. The subtraction depends
67 : only on the account's value in the parent fork, so it can run at any
68 : time after we have seen that the block writes the account, which we
69 : know at parse. The addition must hash the value the block's last
70 : writer leaves, so it has to run after that writer completes. We let
71 : the dispatcher sequence it for us. An addition is a pseudo
72 : transaction that writes the account, created through
73 : fd_rdisp_add_pseudo_txn(), and the dispatcher hands it back only once
74 : every earlier writer of the account is done. Once the block has all
75 : its FEC sets we know the last writers, and the fec_eos drain creates
76 : the pseudo transactions for every account still owed an addition, a
77 : chunk at a time. Before that we may speculate on an account whose
78 : writers so far have all completed, guessing that no more are coming,
79 : and keep the value in the speculation pool so a later write to the
80 : account can undo it. Subtractions and additions go out in batches of
81 : accounts whose hashes the exec tile sums. A speculation goes out
82 : alone. The scheduler folds the sums into the block's delta, which
83 : the caller adds to the bank's LtHash at block end. Hash tasks go
84 : only to tiles that would otherwise sit idle, and speculation is the
85 : lowest priority of all.
86 :
87 : Writes the scheduler cannot see at parse are registered later:
88 : start-of-block writes through fd_sched_block_start_writes(), and the
89 : writable accounts of lookup tables that failed to resolve, which the
90 : exec tile hands back after execution. */
91 :
92 : /* Every account a staged block owes an addition for stays in the
93 : dispatcher's writable set, and so in its account pool, potentially
94 : past the life of the transactions that reference it, up to
95 : FD_RDISP_MAX_WRITERS_PER_BLOCK per lane. The dispatcher is given
96 : that many account entries on top of what live transactions can
97 : reference. */
98 2013 : #define FD_SCHED_LTHASH_ACCT_EXTRA (FD_SCHED_MAX_STAGING_LANES*FD_RDISP_MAX_WRITERS_PER_BLOCK)
99 :
100 : /* A demoted block parks its writable set in the dispatcher's unstaged
101 : entries, which draw on the same extra capacity. Each block hashing
102 : out of band reserves one block's worth of it. See
103 : lthash_pick_mode().
104 :
105 : A block that already has all its FEC sets could reserve less, since
106 : its set can only grow by the start-of-block and lookup table
107 : registrations. We can estimate these and bound the reservation more
108 : tightly. We don't bother because it's not likely to squeeze more
109 : blocks out of it. */
110 1007 : #define FD_SCHED_LTHASH_OOB_BLOCK_MAX (FD_SCHED_LTHASH_ACCT_EXTRA/FD_RDISP_MAX_WRITERS_PER_BLOCK)
111 : FD_STATIC_ASSERT( FD_SCHED_LTHASH_OOB_BLOCK_MAX<=64UL, lthash_oob_reserved_bitmap );
112 :
113 : /* The speculation pool holds the values of speculative additions so a
114 : later intra-block rewrite can undo the addition. Speculation stops
115 : when this pool gets full. There is a threshold for free speculation
116 : entries per exec tile before a deactivated block is activated to
117 : engage in speculation. This is meant to reduce churn. */
118 16517086 : #define FD_SCHED_LTHASH_SPEC_MAX (8192UL)
119 0 : #define FD_SCHED_LTHASH_SPEC_THRESHOLD_PER_TILE (2UL)
120 :
121 : FD_STATIC_ASSERT( FD_SCHED_LTHASH_BATCH_MAX>=1UL && FD_SCHED_LTHASH_BATCH_MAX<=FD_EXECRP_LTHASH_BATCH_MAX, lthash_batch );
122 : FD_STATIC_ASSERT( FD_SCHED_LTHASH_BATCH_BUSY_MAX>=1UL && FD_SCHED_LTHASH_BATCH_BUSY_MAX<=FD_SCHED_LTHASH_BATCH_MAX, lthash_batch_busy_max );
123 :
124 : FD_STATIC_ASSERT( FD_SCHED_LTHASH_TXN_WRITERS_MAX+FD_SCHED_LTHASH_START_WRITES_MAX==FD_RDISP_MAX_WRITERS_PER_BLOCK, lthash_writers );
125 :
126 :
127 1007 : #define FD_SCHED_MAGIC (0xace8a79c181f89b6UL) /* echo -n "fd_sched_v0" | sha512sum | head -c 16 */
128 :
129 : struct fd_sched_mblk {
130 : ulong start_txn_idx; /* inclusive parse idx */
131 : ulong end_txn_idx; /* non-inclusive parse idx */
132 : ulong curr_txn_idx; /* next txn to mixin, parse idx in block */
133 : fd_hash_t end_hash[ 1 ];
134 : fd_hash_t curr_hash[ 1 ];
135 : uint curr_sig_cnt;
136 : uint next;
137 : uint mixin_txn_idx; /* rdisp pool idx of the next txn to mixin */
138 : int is_tick;
139 : };
140 : typedef struct fd_sched_mblk fd_sched_mblk_t;
141 :
142 : #define SLIST_NAME mblk_slist
143 : #define SLIST_ELE_T fd_sched_mblk_t
144 0 : #define SLIST_IDX_T uint
145 23703 : #define SLIST_NEXT next
146 : #include "../../util/tmpl/fd_slist.c"
147 :
148 : #define SET_NAME txn_bitset
149 : #define SET_MAX FD_SCHED_MAX_DEPTH
150 : #include "../../util/tmpl/fd_set.c"
151 :
152 : /* Out-of-band LtHash keeps per-account state for computing a block's
153 : LtHash delta. Each staging lane has a map from pubkey to an entry
154 : that tracks the subtraction and addition of an account the lane's
155 : block writes. A pool of LtHash values shared by all lanes keeps the
156 : values of speculative additions that a later write to the account
157 : may need to undo. A block without a reservation for parking its
158 : writable set hashes in band instead and keeps none of this state.
159 :
160 : An entry is inserted the first time the block sees a write to the
161 : account, according to either fd_rdisp_add_txn() or
162 : fd_rdisp_add_writable(). It starts SUB_QUEUED and ADD_NONE and lives
163 : until the lane is released, which also frees its speculation entry.
164 : The state machine implemented for each account is as follows:
165 :
166 : dispatched to execrp execrp done, delta -= value
167 : sub: QUEUED ---------------------> DISPATCHED ----------------------------> DONE
168 :
169 : execrp done, ptxn_idx==PIDX
170 : rdisp_add_pseudo_txn()==PIDX rdisp_get_next_ready()==PIDX delta += value, ptxn_idx=0
171 : speculative: spec_idx acquired and ptxn_idx==PIDX spec[spec_idx] = value, if any
172 : add: NONE -------------------------------> PENDING(PIDX) -----------------------------> DISPATCHED(PIDX) -------------------------------> DONE
173 : ^ | | |
174 : | rewrite rewrite rewrite
175 : | ptxn_idx=0 ptxn_idx=0 delta-=spec[spec_idx]
176 : | spec_idx released, if any spec_idx released, if any spec_idx released
177 : | | | |
178 : | v v v
179 : +------------------------------------------+---------------------------------------------+-------------------------------------------+
180 : PIDX surfaces or returns later and !=ptxn_idx: pseudo transaction completed or hash result dropped without effect
181 :
182 : A rewrite is a new first write to the account or a speculation we
183 : gave up.
184 :
185 : A speculative pseudo transaction is one the scheduler creates before
186 : the block has all its FEC sets, betting that the account's last write
187 : has happened. A speculative addition may need to be undone after it
188 : is applied, so it requires a speculation entry. A pseudo transaction
189 : created once the block is complete is no longer speculative and
190 : acquires no speculation entry, so its addition must not be rewritten
191 : once DONE. This is guaranteed by construction. Such a pseudo
192 : transaction is created only after every transaction of the block has
193 : been inserted, because the fec_eos drain happens after the last FEC
194 : set. So the only writes that can still arrive are registrations,
195 : and those land before the addition can apply. Start-of-block
196 : registrations run before anything else (including a PENDING
197 : addition) in the block gets dispatched. Late ALT registrations come
198 : from serializing transactions. A serializing transaction blocks
199 : every pseudo transaction inserted after it until it completes, and
200 : these pseudo transactions are inserted after every transaction of
201 : the block, so all of them wait on the last serializing transaction.
202 : Each serializing transaction registers at its own completion, no
203 : later than the last serializing transaction's completion. A rewrite
204 : therefore finds such an addition PENDING, which only makes it stale
205 : and needs no undo. */
206 :
207 2889 : #define FD_SCHED_LTHASH_SUB_QUEUED (0)
208 0 : #define FD_SCHED_LTHASH_SUB_DISPATCHED (1)
209 0 : #define FD_SCHED_LTHASH_SUB_DONE (2)
210 :
211 2889 : #define FD_SCHED_LTHASH_ADD_NONE (0)
212 0 : #define FD_SCHED_LTHASH_ADD_PENDING (1) /* Pseudo transaction added to rdisp, but not yet surfaced by fd_rdisp_get_next_ready(). */
213 0 : #define FD_SCHED_LTHASH_ADD_DISPATCHED (2) /* Surfaced and still the addition of record. Might be batched or immediately hashed. */
214 0 : #define FD_SCHED_LTHASH_ADD_DONE (3) /* Applied to the block's LtHash, value cached for potential undo if spec_idx is valid. */
215 :
216 : /* This represents one LtHash account. */
217 : struct fd_sched_lthash_entry {
218 : fd_acct_addr_t acct;
219 : uint map_next;
220 : uint q_next; /* Subtraction queue link while SUB_QUEUED. */
221 : uint spec_idx; /* Speculation pool entry or UINT_MAX. */
222 : uint ptxn_idx; /* Non-zero only while the entry is ADD_PENDING or ADD_DISPATCHED. High bit set if non-zero, like the index returned from fd_rdisp_get_next_ready(). */
223 : uint bank_idx;
224 : uchar sub_state;
225 : uchar add_state;
226 : uchar pad[ 2 ];
227 : };
228 : typedef struct fd_sched_lthash_entry fd_sched_lthash_entry_t;
229 : FD_STATIC_ASSERT( sizeof(fd_sched_lthash_entry_t)==56UL, lthash_entry );
230 :
231 : #define MAP_NAME lthash_map
232 : #define MAP_ELE_T fd_sched_lthash_entry_t
233 : #define MAP_KEY_T fd_acct_addr_t
234 2889 : #define MAP_KEY acct
235 2889 : #define MAP_NEXT map_next
236 15585 : #define MAP_IDX_T uint
237 8667 : #define MAP_KEY_HASH(k,s) fd_hash32( (k)->b, (s) )
238 0 : #define MAP_KEY_EQ(k0,k1) (!memcmp( (k0)->b, (k1)->b, 32UL ))
239 : #include "../../util/tmpl/fd_map_chain.c"
240 :
241 : /* A lane's map holds up to FD_RDISP_MAX_WRITERS_PER_BLOCK accounts.
242 : Entries are handed out in index order and only ever freed together
243 : when the lane is released, so the live entries are the first
244 : entry_cnt of the pool and no free list is needed. */
245 : struct fd_sched_lthash_lane {
246 : fd_sched_lthash_entry_t * entry_pool;
247 : lthash_map_t * map;
248 : uint entry_cnt; /* Live entries. */
249 : ulong bank_idx; /* Block that owns the lane, ULONG_MAX if none. */
250 : };
251 : typedef struct fd_sched_lthash_lane fd_sched_lthash_lane_t;
252 :
253 : /* Half the template's estimate for a full lane, since a lane rarely
254 : holds more than a few thousand accounts. */
255 : static inline ulong
256 16094 : lthash_chain_cnt( void ) {
257 16094 : return fd_ulong_max( lthash_map_chain_cnt_est( FD_RDISP_MAX_WRITERS_PER_BLOCK )>>1, 1UL );
258 16094 : }
259 :
260 : struct fd_sched_block {
261 : ulong slot;
262 : ulong parent_slot;
263 : ulong parent_idx; /* Index of the parent in the pool. */
264 : ulong child_idx; /* Index of the left-child in the pool. */
265 : ulong sibling_idx; /* Index of the right-sibling in the pool. */
266 :
267 : /* Counters. */
268 : uint txn_parsed_cnt;
269 : /* txn_queued_cnt = txn_parsed_cnt-txn_in_flight_cnt-txn_done_cnt */
270 : uint txn_exec_in_flight_cnt;
271 : uint txn_exec_done_cnt;
272 : uint txn_sigverify_in_flight_cnt; /* number of individual transactions in in-flight sigverify batches */
273 : uint txn_sigverify_done_cnt;
274 : uint sigverify_task_in_flight_cnt; /* number of in-flight sigverify batch tasks */
275 : uint poh_hashing_in_flight_cnt; /* number of in-flight PoH batch tasks */
276 : uint poh_mblk_in_flight_cnt; /* number of individual in-flight PoH jobs across batches */
277 : uint poh_hashing_done_cnt;
278 : uint poh_hash_cmp_done_cnt; /* poh_hashing_done_cnt==poh_hash_cmp_done_cnt+len(mixin_in_progress) */
279 : uint txn_done_cnt; /* A transaction is considered done when all types of tasks associated with it are done. */
280 : uint shred_cnt;
281 : uint shred_scan_idx; /* First shred boundary not before shred_scan_off. */
282 : uint shred_scan_off; /* Block byte offset at the start of shred_scan_idx. */
283 : uint mblk_cnt; /* Total number of microblocks, including ticks and non ticks.
284 : mblk_cnt==len(unhashed)+poh_mblk_in_flight_cnt+len(mixin_in_progress)+hash_cmp_done_cnt */
285 : uint mblk_tick_cnt; /* Total number of tick microblocks. */
286 : uint mblk_freed_cnt; /* This is ==hash_cmp_done_cnt in most cases, except for aborted
287 : blocks, where the freed cnt will catch up to mblk_cnt and surpass
288 : hash_cmp_done_cnt when the block is reaped. */
289 : uint mblk_unhashed_cnt; /* ==len(unhashed) */
290 : ulong txn_pool_max_popcnt; /* Peak transaction pool occupancy during the time this block was replaying. */
291 : ulong mblk_pool_max_popcnt; /* Peak mblk pool occupancy. */
292 : ulong block_pool_max_popcnt; /* Peak block pool occupancy. */
293 : uint txn_idx_tail; /* Most recently parsed rdisp pool index. */
294 : uint txn_sigverify_next_idx; /* Next rdisp pool index to dispatch for sigverify, or 0 if caught up. */
295 : uint parse_mblk_idx; /* mblk pool index currently receiving parsed transactions. */
296 : uint footer_in_flight_cnt;
297 :
298 : /* Out-of-band LtHash.
299 :
300 : When the block has claim on the lane map, each entry in the map
301 : corresponds to one subtraction for the block.
302 :
303 : entries_cnt==sub_queued_cnt+sub_in_flight_cnt+sub_applied_cnt
304 :
305 : Additions the block owes but has no pseudo transaction for yet are
306 :
307 : add_owed_cnt
308 :
309 : We don't track first writes per block. Each puts an addition on
310 : the owed side, so if we did, they would balance as follows.
311 :
312 : first_write_cnt==add_owed_cnt+add_created_cnt
313 :
314 : Unsurfaced additions that rdisp has been instructed to create but
315 : hasn't handed back to us yet are
316 :
317 : add_created_cnt-add_surfaced_cnt
318 :
319 : In-flight additions, including those staged in a batch and not yet
320 : dispatched, are
321 :
322 : add_surfaced_cnt-add_done_cnt
323 :
324 : Additions completed without a hash, or whose hash was dropped,
325 : because their account was written again within the same block are
326 :
327 : add_stale_cnt==add_done_cnt-add_applied_cnt
328 :
329 : The net number of additions absorbed by the block's delta is
330 :
331 : add_applied_cnt-add_undone_cnt
332 :
333 : At BLOCK_END every entry has been subtracted and added exactly once.
334 :
335 : sub_applied_cnt==add_applied_cnt-add_undone_cnt==entries_cnt */
336 : uint lthash_writable_cnt; /* Writable accounts the block's transactions have declared, counted
337 : at parse time and at late ALT registration. Blocks exceeding
338 : FD_SCHED_LTHASH_TXN_WRITERS_MAX are considered invalid. Unlike the
339 : LtHash progress counters below, this counter is a property of the
340 : block and should not be affected by promotion or demotion. */
341 : uint lthash_in_flight_cnt; /* Task count, could be either add or sub. */
342 : uint sub_q_head; /* UINT_MAX if empty. Linked through entry->q_next. */
343 : uint sub_q_tail;
344 : uint sub_queued_cnt;
345 : uint sub_in_flight_cnt;
346 : uint sub_applied_cnt;
347 : uint add_owed_cnt; /* First writes whose addition pseudo transaction is not created yet. */
348 : uint add_created_cnt; /* Addition pseudo transactions that we've instructed rdisp to create and queue up. */
349 : uint add_surfaced_cnt; /* Returned by fd_rdisp_get_next_ready(). Will be either dispatched or completed on the spot. */
350 : uint add_done_cnt; /* fd_rdisp_complete_txn() issued for pseudo transactions, with or without a dispatched hashing. */
351 : uint add_applied_cnt; /* Addition results rolled into the block's delta. */
352 : uint add_undone_cnt; /* Applied additions rolled back due to a subsequent account rewrite. */
353 :
354 : /* PoH verify. */
355 : fd_hash_t poh_hash[ 1 ]; /* running end_hash of last parsed mblk */
356 : int last_mblk_is_tick;
357 : mblk_slist_t mblks_unhashed[ 1 ]; /* A microblock, once parsed out, is in one of these queues. It
358 : progresses from unhashed to mixin. When a microblock is being
359 : hashed/in-flight, it'll be transiently out of any of the queues.
360 : Once a microblock progresses through all stages of work, it'll be
361 : immediately freed. */
362 : mblk_slist_t mblks_mixin_in_progress[ 1 ];
363 : uchar bmtree_mem[ FD_BMTREE_COMMIT_FOOTPRINT(0) ] __attribute__((aligned(FD_BMTREE_COMMIT_ALIGN)));
364 : fd_bmtree_commit_t * bmtree;
365 : ulong tick_height; /* Block is built off of a parent block with this many ticks, ULONG_MAX until known. */
366 : ulong max_tick_height; /* Block should end with precisely this many ticks. */
367 :
368 : /* Parser state. */
369 : ulong mblks_rem; /* Number of microblocks remaining in the current batch. */
370 : ulong txns_rem; /* Number of transactions remaining in the current microblock. */
371 : uint residual_sz; /* Bytes of residual data, at most FD_SCHED_MAX_RESIDUAL_SZ. */
372 : uint fec_buf_boff; /* Byte offset into raw block data of the first residual byte, which is the first byte in the parse buffer during ingest. */
373 : ushort shred_sz[ FD_SCHED_SHRED_SZ_WINDOW ]; /* Payload size of shred i at i%FD_SCHED_SHRED_SZ_WINDOW, for the shreds in [shred_scan_idx, shred_cnt). */
374 : long fec_completed_ns; /* Network arrival (wallclock ns) of the FEC set being ingested; txns
375 : parse out only once the FEC completing their bytes arrives, so this
376 : is stamped on every txn parsed during the current ingest. */
377 : uint fec_eob:1; /* FEC end-of-batch: set if the last FEC set in the batch is being
378 : ingested. */
379 : uint fec_sob:1; /* FEC start-of-batch: set if the parser expects to be parsing out a
380 : batch header. */
381 : uint shred_sz_overflow:1; /* Set once more shreds were outstanding than shred_sz holds. The
382 : shred attribution of the block's remaining transactions saturates. */
383 :
384 : /* Block state. */
385 : uint fec_eos:1; /* FEC end-of-stream: set if the last FEC set in the block has been
386 : ingested. */
387 : uint rooted:1; /* Set if the block is rooted. */
388 : uint dying:1; /* Set if the block has been abandoned and no transactions should be
389 : scheduled from it. */
390 : uint discarded:1; /* Set if the block went down with a discarded (not invalid) lineage:
391 : its own discard or an ancestor's. This field, along with
392 : dead_reason, can be in one of three possible combinations when a
393 : block is dead:
394 :
395 : dead_reason!=DEAD_ANCESTOR, discarded initialized to 0: invalid block due to dead_reason
396 : dead_reason==DEAD_ANCESTOR, discarded==0: invalid lineage
397 : dead_reason==DEAD_ANCESTOR||NONE, discarded==1: discarded lineage */
398 : uint refcnt:1; /* Starts at 1 when the block is added, set to 0 if caller has been
399 : informed to decrement refcnt for sched. */
400 : uint in_sched:1; /* Set if the block is being tracked by the scheduler. */
401 : uint in_rdisp:1; /* Set if the block is being tracked by the dispatcher, either as staged
402 : or unstaged. */
403 : uint block_start_signaled:1; /* Set if the start-of-block sentinel has been dispatched. */
404 : uint block_end_signaled:1; /* Set if the end-of-block sentinel has been dispatched. */
405 : uint block_start_done:1; /* Set if the start-of-block processing has been completed. */
406 : uint block_end_done:1; /* Set if the end-of-block processing has been completed. */
407 : uint staged:1; /* Set if the block is in a dispatcher staging lane; a staged block is
408 : tracked by the dispatcher. */
409 : uint lthash_drained:1; /* Set if fec_eos AND something equivalent to
410 : fd_rdisp_add_all_pseudo_txn() has been done since fec_eos. That is
411 : to say, every writable account has an addition pseudo transaction
412 : that will capture its final value. */
413 : uint lthash_draining:1; /* Counted in sched->lthash_draining_cnt. */
414 : uint lthash_mode_picked:1; /* Set once the block has picked its hashing mode, at its first drain or its
415 : BLOCK_START, whichever comes first. */
416 : uint lthash_inband:1; /* Set if the block hashes in band. The dispatcher tracks no writers for it and the
417 : block owes no out-of-band work. */
418 : uchar lthash_oob_reservation; /* Which out-of-band reservation the block holds, meaningful when
419 : lthash_mode_picked && !lthash_inband. */
420 : ulong staging_lane; /* Ignored if staged==0. */
421 :
422 : /* Slot state tracking */
423 : long state_start_tick; /* Start of the committed state's interval. */
424 : long state_cand_tick; /* Start of the candidate state. */
425 : uchar state; /* Committed state, NONE until the first commit. */
426 : uchar state_cand; /* Candidate state, committed once held for FD_SCHED_SLOT_STATE_DEBOUNCE_NS. */
427 : uchar state_open;
428 : uchar state_conflict; /* Last next_ready found queued txns but none ready. */
429 : ulong luf_depth; /* Depth of longest unstaged fork starting from this node; only
430 : stageable unstaged descendants are counted. */
431 : int dead_reason; /* One of FD_SCHED_DEAD_REASON_*; records the first reason the block
432 : was ruled invalid (set-once via record_dead_reason), NONE otherwise. */
433 : uchar residual[ FD_SCHED_MAX_RESIDUAL_SZ ]; /* Bytes of the previous FEC set that only become parseable once the
434 : next FEC set is ingested, at most a transaction, a block marker or
435 : a microblock or batch header. */
436 :
437 : /* Alpenglow block footer, deserialized out of the marker batch at
438 : parse time. footer_seen is set if the block carries a footer
439 : marker and it is seen by sched; footer is only meaningful in that
440 : case. */
441 : fd_block_footer_t footer;
442 :
443 : /* Alpenglow block structure, mirroring agave's BlockComponentStage
444 : as a set of "seen" flags.
445 : Together with mblk_cnt they determine what may come next:
446 :
447 : agave stage header footer alpentick mblk_cnt
448 : PreParentMarker 0 0 0 0
449 : AcceptingGenesisOrEntries 1 0 0 0 (and no genesis cert yet)
450 : AcceptingEntriesOrFooter 1 0 0 *
451 : AcceptingAlpentick 1 1 0 *
452 : Done 1 1 1 * */
453 : uint header_seen : 1;
454 : uint genesis_cert_seen : 1;
455 : uint update_parent_seen : 1;
456 : uint footer_seen : 1;
457 : uint cert_cnt : 3; /* Footer certs to verify. See fd_alpenglow_footer_certs. */
458 : uint cert_next : 3; /* Footer certs already sent out as tasks. */
459 : uint alpentick_seen : 1;
460 :
461 : /* Replay started the block at its UpdateParent FEC set, so the
462 : UpdateParent stands in for the header as the parent marker. */
463 : uint starts_at_update_parent : 1;
464 : uint flh_active : 1; /* alpenglow_fast_leader_handover is active on the block's bank, set at BLOCK_START */
465 : };
466 : typedef struct fd_sched_block fd_sched_block_t;
467 :
468 : FD_STATIC_ASSERT( sizeof(fd_sched_mblk_t)==104UL, fd_sched_mblk );
469 : FD_STATIC_ASSERT( sizeof(fd_sched_txn_info_t)==184UL, fd_sched_txn_info );
470 : FD_STATIC_ASSERT( sizeof(fd_sched_block_t)==9472UL, fd_sched_block );
471 : FD_STATIC_ASSERT( sizeof(fd_hash_t)==sizeof(((fd_microblock_hdr_t *)0)->hash), unexpected poh hash size );
472 :
473 :
474 : struct fd_sched_metrics {
475 : uint block_added_cnt;
476 : uint block_added_staged_cnt;
477 : uint block_added_unstaged_cnt;
478 : uint block_added_dead_ood_cnt;
479 : uint block_removed_cnt;
480 : uint block_abandoned_cnt;
481 : uint block_bad_cnt;
482 : uint block_promoted_cnt;
483 : uint block_demoted_cnt;
484 : uint deactivate_no_child_cnt;
485 : uint deactivate_no_txn_cnt;
486 : uint deactivate_pruned_cnt;
487 : uint deactivate_abandoned_cnt;
488 : uint lane_switch_cnt;
489 : uint lane_promoted_cnt;
490 : uint lane_demoted_cnt;
491 : uint fork_observed_cnt;
492 : uint alut_success_cnt;
493 : uint alut_serializing_cnt;
494 : uint txn_abandoned_parsed_cnt;
495 : uint txn_abandoned_exec_done_cnt;
496 : uint txn_abandoned_done_cnt;
497 : uint txn_promoted_cnt;
498 : uint txn_max_in_flight_cnt;
499 : ulong txn_weighted_in_flight_cnt;
500 : ulong txn_weighted_in_flight_tickcount;
501 : ulong txn_none_in_flight_tickcount;
502 : ulong txn_parsed_cnt;
503 : ulong txn_exec_done_cnt;
504 : ulong txn_sigverify_done_cnt;
505 : ulong txn_mixin_done_cnt;
506 : ulong txn_done_cnt;
507 : ulong mblk_parsed_cnt;
508 : ulong mblk_poh_hashed_cnt;
509 : ulong mblk_poh_done_cnt;
510 : ulong bytes_ingested_cnt;
511 : ulong bytes_ingested_unparsed_cnt;
512 : ulong bytes_dropped_cnt;
513 : ulong fec_cnt;
514 :
515 : /* Out-of-band LtHash. sum(x) sums per-block counter x over the
516 : blocks in the scheduler. The lthash_discarded_* counters are for
517 : what abandon and demotion throw away. We expect the following
518 : invariants.
519 :
520 : Writes observed, by source, and what the dispatcher made of each.
521 : A given up speculation puts its account back as written, so it is a
522 : source too:
523 : lthash_parsed_writable_cnt+lthash_start_reg_cnt+lthash_late_reg_cnt+lthash_demote_reg_cnt+lthash_spec_given_up_cnt==lthash_first_write_cnt+lthash_coalesced_cnt
524 :
525 : Sometimes rdisp reports a write as the account's first write since
526 : its last addition pseudo transaction. It makes the block owe the
527 : account one addition, which a drain or a speculation will get going
528 : later. Writes in between coalesce into that owed addition, which
529 : is the add-add coalescing rdisp provides:
530 : lthash_first_write_cnt==lthash_add_created_cnt+sum(add_owed_cnt)+lthash_discarded_add_owed_cnt+lthash_inband_add_owed_cnt
531 :
532 : Additions. A surfaced addition sits in sched->lthash_add_batch
533 : until its batch reaches a tile, so the held batch is a term:
534 : lthash_add_created_cnt==lthash_add_surfaced_cnt+sum(add_created_cnt-add_surfaced_cnt)+lthash_discarded_add_pending_cnt
535 : lthash_add_surfaced_cnt==lthash_add_surfaced_stale_cnt+lthash_spec_given_up_cnt+lthash_add_dispatched_cnt+(held additions)+lthash_discarded_add_held_cnt
536 : lthash_add_dispatched_cnt==lthash_add_dispatched_stale_cnt+lthash_add_applied_cnt+(in-flight additions)
537 : lthash_add_applied_cnt-lthash_add_undone_cnt==lthash_frozen_cnt+sum(add_applied_cnt-add_undone_cnt)+lthash_discarded_add_applied_cnt
538 :
539 : Subtractions:
540 : lthash_entry_cnt==lthash_sub_dispatched_cnt+sum(sub_queued_cnt)+lthash_discarded_sub_queued_cnt+lthash_inband_sub_queued_cnt
541 : lthash_sub_dispatched_cnt==lthash_sub_applied_cnt+(in-flight subtractions)
542 : lthash_sub_applied_cnt==lthash_frozen_cnt+sum(sub_applied_cnt)+lthash_discarded_sub_applied_cnt
543 :
544 : Speculation. lthash_spec_cnt of the additions were guesses. Once
545 : the blocks finish, lthash_spec_applied_cnt-lthash_add_undone_cnt of
546 : them survive, lthash_spec_stale_cnt were invalidated before their
547 : hash applied, and lthash_spec_given_up_cnt were withdrawn before
548 : they reached a tile with no write involved. The rest of the stale
549 : counts are drain additions invalidated by a registration that came
550 : after the drain. Draining is a parse time operation, and the
551 : registrations that invalidate it could come from either
552 : start-of-block processing or ALT expansions. The owed, pending and
553 : queued lthash_discarded_* counters come only from abandon, since a
554 : demotable block has nothing queued or unsurfaced and keeps its owed
555 : additions. The two lthash_discarded_*_applied delta counters can
556 : come from either abandon or demotion. */
557 : ulong lthash_parsed_writable_cnt; /* Declared writable accounts of parsed transactions. ALT accounts of a transaction whose tables could not be expanded are not counted here, and instead count as late registrations. */
558 : uint lthash_start_reg_cnt;
559 : uint lthash_late_reg_cnt; /* Declared writable lookup table accounts registered at transaction completion. */
560 : uint lthash_demote_reg_cnt; /* Accounts put back into rdisp when their block was demoted. A later promotion will drain these back out of rdisp. Not really new writes. */
561 : ulong lthash_first_write_cnt; /* Writes rdisp reported as the account's first since its last addition pseudo transaction. */
562 : ulong lthash_coalesced_cnt; /* Writes to an account the block already owed an addition for. */
563 : ulong lthash_entry_cnt; /* Lane head account map entries inserted, each owing one subtraction. */
564 : ulong lthash_sub_dispatched_cnt;
565 : ulong lthash_sub_task_cnt; /* Subtraction tasks dispatched to a tile, each a batch of the accounts lthash_sub_dispatched_cnt counts. */
566 : ulong lthash_sub_applied_cnt;
567 : ulong lthash_add_created_cnt; /* Addition pseudo transactions rdisp handed us. */
568 : ulong lthash_spec_cnt; /* Of those, speculatively created while the block was still receiving FEC sets. */
569 : ulong lthash_add_surfaced_cnt; /* Pseudo transactions returned by fd_rdisp_get_next_ready(). */
570 : ulong lthash_add_surfaced_stale_cnt; /* Of those, no longer the account's addition of record due to a rewrite. We will complete these pseudo transactions without hashing. */
571 : ulong lthash_add_dispatched_cnt;
572 : ulong lthash_add_task_cnt; /* Addition tasks dispatched to a tile, each a batch of the accounts lthash_add_dispatched_cnt counts. */
573 : ulong lthash_add_dispatched_stale_cnt; /* Of those, rewritten while in flight. We will drop the hashing result. */
574 : ulong lthash_add_batch_held_cnt; /* Addition batches held because a transaction surfaced behind them, released by a stale addition completed in the same poll, or because a speculative addition went alone. */
575 : ulong lthash_add_batch_wait_idle_cnt; /* Polls that left an addition batch held because of insufficient idle tile. */
576 : ulong lthash_add_applied_cnt;
577 : ulong lthash_add_undone_cnt; /* Applied additions rolled back because the account was rewritten. */
578 : ulong lthash_spec_applied_cnt; /* Of lthash_add_applied_cnt, speculative. */
579 : ulong lthash_spec_stale_cnt; /* Speculations rewritten while PENDING or DISPATCHED, invalidated before they applied. Applied speculations are not included here. */
580 : uint lthash_spec_given_up_cnt; /* Speculations that surfaced once the block was complete, or when the idleness policy would not let them onto a tile. */
581 : uint lthash_pool_full_cnt; /* Deactivations of a block that still owed additions, because the speculation pool had no free entry. Mutually exclusive with deactivate_no_txn_cnt. Such blocks have a chance to be activated if sufficient entries per exec tile are free again. */
582 : uint lthash_spec_yield_cnt; /* Deactivations of a block that could only speculate, because it's yielding to another lane head that has must-do work. */
583 : ulong lthash_frozen_cnt; /* Accounts in the delta when BLOCK_END completes. End-of-block writers are hashed in band and are not counted. */
584 : uint lthash_discarded_add_owed_cnt; /* Additions owed by abandoned blocks. Never created. */
585 : uint lthash_discarded_add_pending_cnt; /* Pseudo transactions of abandoned blocks. Never surfaced. */
586 : uint lthash_discarded_add_held_cnt; /* Additions of abandoned blocks batched behind a transaction. Never on a tile. */
587 : uint lthash_discarded_add_applied_cnt; /* Additions in the delta of an abandoned or demoted block, net of undone. */
588 : uint lthash_discarded_sub_queued_cnt; /* Subtractions of abandoned blocks. Never dispatched. */
589 : uint lthash_discarded_sub_applied_cnt; /* Subtractions in the delta of an abandoned or demoted block. */
590 : uint lthash_inband_add_owed_cnt; /* Additions owed by blocks that picked in-band hashing. Never created. */
591 : uint lthash_inband_sub_queued_cnt; /* Subtractions of blocks that picked in-band hashing. Never dispatched. */
592 : uint lthash_inband_block_cnt; /* Blocks that picked in-band hashing because every out-of-band reservation was taken. */
593 : ulong lane_wait_cnt;
594 : };
595 : typedef struct fd_sched_metrics fd_sched_metrics_t;
596 :
597 : /* A batch of LtHash entries of one block and one kind, either what a
598 : tile is actively hashing or the additions that have been staged but
599 : not yet dispatched. A pseudo transaction is unreadable once it is
600 : completed, so we capture the account addresses at batching. */
601 : struct fd_sched_lthash_batch {
602 : ulong bank_idx;
603 : ulong cnt; /* In [0, FD_SCHED_LTHASH_BATCH_MAX]. */
604 : uint lane;
605 : uchar kind;
606 : uint entry_idx[ FD_SCHED_LTHASH_BATCH_MAX ]; /* Entries in the lane map. */
607 : uint ptxn_idx [ FD_SCHED_LTHASH_BATCH_MAX ]; /* FD_RDISP_LTHASH_PSEUDO_TXN bit set. 0 for a subtraction. */
608 : fd_acct_addr_t accts [ FD_SCHED_LTHASH_BATCH_MAX ];
609 : };
610 : typedef struct fd_sched_lthash_batch fd_sched_lthash_batch_t;
611 : FD_STATIC_ASSERT( alignof(fd_sched_lthash_batch_t)==alignof(ulong), lthash_batch_align ); /* The layout places the records at alignof(ulong). */
612 :
613 : #define DEQUE_NAME ref_q
614 0 : #define DEQUE_T ulong
615 : #include "../../util/tmpl/fd_deque_dynamic.c"
616 :
617 : struct fd_sched {
618 : fd_acct_addr_t aluts[ 256 ]; /* Resolve ALUT accounts into this buffer for more parallelism. */
619 : char print_buf[ FD_SCHED_MAX_PRINT_BUF_SZ ];
620 : ulong print_buf_sz;
621 : uchar fec_buf[ FD_SCHED_MAX_FEC_BUF_SZ ]; /* Parse buffer. It holds the residual followed by the new FEC set. */
622 : uint fec_buf_sz; /* Size of the fec_buf in bytes. */
623 : uint fec_buf_soff; /* Starting offset into fec_buf for unparsed transactions. */
624 : uchar txn[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t)))); /* The transaction being parsed out. */
625 : ulong max_shreds_per_block; /* Immutable. */
626 : ulong max_txn_per_slot; /* Immutable. */
627 : ulong max_mblk_per_slot; /* Immutable, FD_SCHED_MAX_MBLK_PER_SLOT scaled with max_shreds_per_block. */
628 : ulong lthash_txn_writers_max; /* FD_SCHED_LTHASH_TXN_WRITERS_MAX unless lowered by a test. */
629 : ulong lthash_oob_reserved; /* Bit i set if out-of-band reservation i is held by a block. */
630 : ulong lthash_oob_block_max; /* FD_SCHED_LTHASH_OOB_BLOCK_MAX unless lowered by a test. */
631 : fd_chkdup_t chkdup[ 1 ];
632 : fd_sched_metrics_t metrics[ 1 ];
633 : ulong canary; /* == FD_SCHED_MAGIC */
634 : ulong depth; /* Immutable. Pool depth. */
635 : ulong block_cnt_max; /* Immutable. */
636 : ulong exec_cnt; /* Immutable. */
637 : int bypass_poh_verify; /* Test/fuzz: skip the PoH end_hash compare in maybe_mixin. */
638 : int bypass_alut_resolution; /* Test/fuzz: skip ALUT resolution (no accdb). */
639 : ulong sigverify_group; /* Signature count a sigverify task collects, FD_ED25519_VERIFIER_GROUP in production */
640 : long txn_in_flight_last_tick;
641 : long next_ready_last_tick;
642 : ulong next_ready_last_bank_idx;
643 : ulong root_idx;
644 : fd_rdisp_t * rdisp;
645 : ulong txn_exec_ready_bitset[ 1 ];
646 : ulong sigverify_ready_bitset[ 1 ];
647 : ulong poh_ready_bitset[ 1 ];
648 : ulong footer_ready_bitset[ 1 ];
649 : ulong lthash_ready_bitset[ 1 ];
650 : ulong active_bank_idx; /* Index of the actively replayed block, or ULONG_MAX if no block is
651 : actively replayed; has to have a transaction to dispatch; staged
652 : blocks that have no transactions to dispatch are not eligible for
653 : being active. */
654 : ulong last_active_bank_idx;
655 : ulong staged_bitset; /* Bit i set if staging lane i is occupied. */
656 : ulong staged_head_bank_idx[ FD_SCHED_MAX_STAGING_LANES ]; /* Head of the linear chain in each staging lane, ignored if bit i is
657 : not set in the bitset. */
658 : ulong staged_popcnt_wmk;
659 : ulong txn_pool_free_cnt;
660 : fd_txn_p_t * txn_pool; /* Just a flat array. */
661 : fd_sched_txn_info_t * txn_info_pool; /* Just a flat array. */
662 : fd_sched_mblk_t * mblk_pool; /* Just a flat array. */
663 : ulong mblk_pool_free_cnt;
664 : uint mblk_pool_free_head;
665 : ulong tile_to_bank_idx[ FD_SCHED_MAX_EXEC_TILE_CNT ]; /* Index of the bank that the exec tile is executing against. */
666 : fd_sched_poh_hash_t poh_inflight[ FD_SCHED_MAX_EXEC_TILE_CNT ]; /* PoH dispatch metadata, indexed by exec tile. */
667 : fd_sched_lthash_batch_t * lthash_inflight; /* What each tile is hashing, FD_SCHED_MAX_EXEC_TILE_CNT records indexed by exec tile. */
668 : fd_sched_lthash_batch_t * lthash_add_batch; /* Surfaced additions not yet put on a tile, cnt 0 if none. */
669 : fd_sched_lthash_lane_t lthash_lane[ FD_SCHED_MAX_STAGING_LANES ];
670 : fd_lthash_value_t * lthash_spec_pool; /* FD_SCHED_LTHASH_SPEC_MAX values of speculative additions. */
671 : uint * lthash_spec_free; /* Stack of the free lthash_spec_pool indices, lthash_spec_free[ lthash_spec_free_cnt-1 ] on top. */
672 : ulong lthash_spec_free_cnt;
673 : fd_lthash_value_t lthash_delta[ FD_SCHED_LTHASH_OOB_BLOCK_MAX ]; /* Delta of the block holding out-of-band reservation i. */
674 : uint lthash_draining_cnt;
675 : fd_sched_txn_sigverify_t sigverify_inflight[ FD_SCHED_MAX_EXEC_TILE_CNT ]; /* indexed by exec tile */
676 : txn_bitset_t exec_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
677 : txn_bitset_t sigverify_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
678 : txn_bitset_t poh_mixin_done_set[ txn_bitset_word_cnt ]; /* Indexed by txn_idx. */
679 : fd_sched_block_t * block_pool; /* Just a flat array. */
680 : ulong block_pool_popcnt;
681 : ulong * ref_q;
682 :
683 : long state_debounce_ticks;
684 : ulong state_bank_idx; /* Block whose slot state is open, ULONG_MAX if none. */
685 : ulong state_cnt;
686 : ulong state_drop_cnt;
687 : fd_sched_slot_state_t state_buf[ FD_SCHED_SLOT_STATE_MAX ];
688 : };
689 : typedef struct fd_sched fd_sched_t;
690 :
691 :
692 : /* Internal helpers. */
693 :
694 : static int
695 : verify_ticks_eager( fd_sched_block_t * block );
696 :
697 : static int
698 : verify_ticks_final( fd_sched_block_t * block );
699 :
700 : static int
701 : on_final( fd_sched_block_t * block );
702 :
703 : static void
704 : add_block( fd_sched_t * sched,
705 : ulong bank_idx,
706 : ulong parent_bank_idx );
707 :
708 : FD_WARN_UNUSED static int
709 : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
710 :
711 : FD_WARN_UNUSED static int
712 : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx );
713 :
714 : static int
715 : dispatch_sigverify( fd_sched_t * sched, fd_sched_block_t * block, ulong bank_idx, int exec_tile_idx, fd_sched_task_t * out );
716 :
717 : static uint
718 : shred_split( fd_sched_block_t * block,
719 : uint query );
720 :
721 : static void
722 : dispatch_poh( fd_sched_t * sched, fd_sched_block_t * block, ulong bank_idx, int exec_tile_idx, fd_sched_task_t * out );
723 :
724 : static int
725 : poh_retire_mblk( fd_sched_t * sched, fd_sched_block_t * block, uint mblk_idx );
726 :
727 : FD_WARN_UNUSED static int
728 : maybe_mixin( fd_sched_t * sched, fd_sched_block_t * block );
729 :
730 : static void
731 0 : free_mblk( fd_sched_t * sched, fd_sched_block_t * block, uint mblk_idx ) {
732 0 : sched->mblk_pool[ mblk_idx ].next = sched->mblk_pool_free_head;
733 0 : sched->mblk_pool_free_head = mblk_idx;
734 0 : sched->mblk_pool_free_cnt++;
735 0 : block->mblk_freed_cnt++;
736 0 : }
737 :
738 : static void
739 0 : free_mblk_slist( fd_sched_t * sched, fd_sched_block_t * block, mblk_slist_t * list ) {
740 0 : while( !mblk_slist_is_empty( list, sched->mblk_pool ) ) {
741 0 : uint idx = (uint)mblk_slist_idx_pop_head( list, sched->mblk_pool );
742 0 : free_mblk( sched, block, idx );
743 0 : }
744 0 : }
745 :
746 : static void
747 : try_activate_block( fd_sched_t * sched );
748 :
749 : static void
750 : check_or_set_active_block( fd_sched_t * sched );
751 :
752 : static void
753 : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block );
754 :
755 : static void
756 : subtree_release_refcnt( fd_sched_t * sched, fd_sched_block_t * block );
757 :
758 : static void
759 : subtree_prune( fd_sched_t * sched, ulong bank_idx, ulong except_idx );
760 :
761 : static void
762 : maybe_switch_block( fd_sched_t * sched, ulong bank_idx );
763 :
764 : static ulong
765 : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx );
766 :
767 : static ulong
768 : promote_from_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx );
769 :
770 : static int
771 : lane_is_demotable( fd_sched_t * sched, int lane_idx );
772 :
773 : static void
774 : demote_lane( fd_sched_t * sched, int lane_idx );
775 :
776 : static inline fd_sched_block_t *
777 29293 : block_pool_ele( fd_sched_t * sched, ulong idx ) {
778 29293 : FD_TEST( idx<sched->block_cnt_max || idx==ULONG_MAX );
779 29293 : return idx==ULONG_MAX ? NULL : sched->block_pool+idx;
780 29293 : }
781 :
782 : FD_FN_UNUSED static inline int
783 0 : block_is_void( fd_sched_block_t * block ) {
784 0 : /* We've seen everything in the block and no transaction got parsed
785 0 : out. */
786 0 : return block->fec_eos && block->txn_parsed_cnt==0;
787 0 : }
788 :
789 : /* Assumes that every serializing transaction has completed, so every
790 : late registration has happened. */
791 : static inline int
792 0 : block_lthash_settled( fd_sched_block_t * block ) {
793 0 : return block->lthash_drained && !block->sub_queued_cnt && !block->sub_in_flight_cnt && block->add_created_cnt==block->add_done_cnt;
794 0 : }
795 :
796 : static inline int
797 0 : block_should_signal_end( fd_sched_block_t * block ) {
798 : /* Under the current policy of eager synchronous PoH mixin, hashing
799 : done plus fec_eos imply that all mixins have been done. */
800 0 : if( FD_UNLIKELY( !( !block->fec_eos || ((block->mblk_cnt==block->poh_hashing_done_cnt&&block->mblk_cnt==block->poh_hash_cmp_done_cnt)||block->mblk_cnt!=block->poh_hashing_done_cnt) ) ) ) FD_LOG_CRIT(( "invariant violation: slot %lu fec_eos %d mblk_cnt %u poh_hashing_done_cnt %u poh_hash_cmp_done_cnt %u", block->slot, block->fec_eos, block->mblk_cnt, block->poh_hashing_done_cnt, block->poh_hash_cmp_done_cnt ));
801 0 : return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->mblk_cnt==block->poh_hashing_done_cnt && block->block_start_done && !block->block_end_signaled && block_lthash_settled( block );
802 0 : }
803 :
804 : static inline int
805 0 : block_will_signal_end( fd_sched_block_t * block ) {
806 0 : return block->fec_eos && !block->block_end_signaled;
807 0 : }
808 :
809 : /* Is there something known to be dispatchable in the block? This is an
810 : important liveness property. A block that doesn't contain any known
811 : dispatchable tasks will be deactivated or demoted. The block
812 : boundary tests come first because they settle a complete or an
813 : unstarted block with one load each. The replay tile asks this
814 : question of every staged block on every loop while a first FEC set
815 : waits for a lane, so this needs to be fast. */
816 : static inline int
817 4555 : block_is_dispatchable( fd_sched_block_t * block ) {
818 4555 : return !block->block_start_signaled ||
819 4555 : block_will_signal_end( block ) ||
820 4555 : block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt>0U || /* exec_queued_cnt */
821 4555 : block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt>0U || /* sigverify_queued_cnt */
822 4555 : block->mblk_cnt-block->poh_mblk_in_flight_cnt-block->poh_hashing_done_cnt>0U || /* poh_queued_cnt */
823 4555 : block->sub_queued_cnt>0U ||
824 4555 : block->add_created_cnt>block->add_surfaced_cnt;
825 4555 : }
826 :
827 : /* Tasks of the block on exec tiles, footer tasks included. */
828 : static inline ulong
829 0 : block_in_flight_cnt( fd_sched_block_t * block ) {
830 0 : return (ulong)block->txn_exec_in_flight_cnt + (ulong)block->sigverify_task_in_flight_cnt + (ulong)block->poh_hashing_in_flight_cnt + (ulong)block->footer_in_flight_cnt + (ulong)block->lthash_in_flight_cnt;
831 0 : }
832 :
833 : /* Unlike block_in_flight_cnt, this goes by the LtHash account counters,
834 : so an addition batch staged but not yet dispatched keeps the block in
835 : flight. */
836 : static inline int
837 4 : block_is_in_flight( fd_sched_block_t * block ) {
838 4 : return block->txn_exec_in_flight_cnt || block->txn_sigverify_in_flight_cnt || block->poh_hashing_in_flight_cnt || block->footer_in_flight_cnt || (block->block_end_signaled && !block->block_end_done)
839 4 : || block->sub_in_flight_cnt || block->add_surfaced_cnt>block->add_done_cnt;
840 4 : }
841 :
842 : static inline int
843 6561 : block_is_done( fd_sched_block_t * block ) {
844 6561 : return block->fec_eos && block->txn_parsed_cnt==block->txn_done_cnt && block->mblk_cnt==block->poh_hash_cmp_done_cnt && block->block_start_done && block->block_end_done && !block->footer_in_flight_cnt;
845 6561 : }
846 :
847 : static inline int
848 5560 : block_is_stageable( fd_sched_block_t * block ) {
849 5560 : int rv = !block_is_done( block ) && !block->dying;
850 5560 : if( FD_UNLIKELY( rv && !block->in_rdisp ) ) {
851 : /* Invariant: stageable blocks may be currently staged or unstaged,
852 : but must be in the dispatcher either way. When a block
853 : transitions to DONE, it will be immediately removed from the
854 : dispatcher. When a block transitions to DYING, it will be
855 : eventually abandoned from the dispatcher. */
856 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",
857 0 : block->txn_parsed_cnt, block->txn_done_cnt, (uint)block->fec_eos, block->slot, block->parent_slot ));
858 0 : }
859 5560 : return rv;
860 5560 : }
861 :
862 : static inline int
863 4 : block_is_promotable( fd_sched_block_t * block ) {
864 4 : return block_is_stageable( block ) && block_is_dispatchable( block ) && !block->staged;
865 4 : }
866 :
867 : static inline int
868 0 : block_is_demotable( fd_sched_block_t * block ) {
869 : /* A block can only be demoted from rdisp if it is empty, meaning no
870 : PENDING, READY, or DISPATCHED transactions. This is equivalent to
871 : having no in-flight transactions (DISPATCHED) and no queued
872 : transactions (PENDING or READY). This function actually implements
873 : a stronger requirement. We consider a block demotable only if
874 : there are no in-flight or queued tasks of any kind. */
875 0 : return block->staged && !block_is_dispatchable( block ) && !block_is_in_flight( block );
876 0 : }
877 :
878 : static inline int
879 4555 : block_is_activatable( fd_sched_block_t * block ) {
880 4555 : return block_is_stageable( block ) && block_is_dispatchable( block ) && block->staged;
881 4555 : }
882 :
883 : static inline ulong
884 6449 : block_to_idx( fd_sched_t * sched, fd_sched_block_t * block ) { return (ulong)(block-sched->block_pool); }
885 :
886 : static inline int
887 1001 : block_is_lane_head( fd_sched_t * sched, fd_sched_block_t * block ) {
888 1001 : return block->staged && sched->staged_head_bank_idx[ block->staging_lane ]==block_to_idx( sched, block );
889 1001 : }
890 :
891 : FD_FN_UNUSED static inline int
892 0 : block_is_lane_tail( fd_sched_t * sched, fd_sched_block_t * block ) {
893 0 : if( !block->staged ) return 0;
894 0 : fd_rdisp_staging_lane_info_t lane_info[ FD_SCHED_MAX_STAGING_LANES ];
895 0 : ulong occupied = fd_rdisp_staging_lane_info( sched->rdisp, lane_info );
896 0 : return fd_ulong_extract_bit( occupied, (int)block->staging_lane ) && lane_info[ block->staging_lane ].insert_ready_block==block_to_idx( sched, block );
897 0 : }
898 :
899 : /* A block with absolutely nothing to dispatch may still speculate on
900 : additions when it holds a lane, is still receiving FEC sets, owes an
901 : addition the dispatcher offers as ready (meaning every writer of the
902 : account so far has completed), and the speculation pool has at least
903 : spec_free_min free entries. Only the active block speculates. An
904 : already speculating block keeps going until the last free entry. In
905 : contrast, deactivated blocks are activated for speculation only if a
906 : higher free entry threshold is reached, so a trickle of freed entries
907 : does not bounce blocks in and out of the active state. */
908 : static inline int
909 0 : block_may_speculate( fd_sched_t * sched, fd_sched_block_t * block, ulong spec_free_min ) {
910 0 : if( block->fec_eos || !block->add_owed_cnt ) return 0;
911 0 : if( sched->lthash_spec_free_cnt<spec_free_min ) return 0;
912 : /* No need to check for tail because we have one block per lane. */
913 0 : if( !block_is_lane_head( sched, block ) ) return 0;
914 0 : return fd_rdisp_lane_ready_acct_cnt( sched->rdisp, block->staging_lane )>0UL;
915 0 : }
916 :
917 : static inline int
918 0 : other_lane_has_real_work( fd_sched_t * sched, fd_sched_block_t * block ) {
919 0 : ulong staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
920 0 : while( staged_bitset ) {
921 0 : int lane_idx = fd_ulong_find_lsb( staged_bitset );
922 0 : staged_bitset = fd_ulong_pop_lsb( staged_bitset );
923 0 : fd_sched_block_t * head = block_pool_ele( sched, sched->staged_head_bank_idx[ lane_idx ] );
924 0 : if( block_is_done( block_pool_ele( sched, head->parent_idx ) ) && block_is_activatable( head ) ) return 1;
925 0 : }
926 0 : return 0;
927 0 : }
928 :
929 : static inline int
930 3554 : block_should_deactivate( fd_sched_t * sched, fd_sched_block_t * block ) {
931 : /* We allow a grace period, during which a block has nothing to
932 : dispatch, but has something in-flight. The block is allowed to
933 : stay activated and ingest FEC sets during this time. The block
934 : will be deactivated if there's still nothing to dispatch by the
935 : time all in-flight tasks are completed, unless it can spend the
936 : idle time speculating on LtHash additions. */
937 3554 : return !block_is_activatable( block ) && !block_is_in_flight( block ) && !block_may_speculate( sched, block, 1UL );
938 3554 : }
939 :
940 : static inline int
941 2 : block_is_prunable( fd_sched_block_t * block ) {
942 2 : return !block->in_rdisp && !block_is_in_flight( block );
943 2 : }
944 :
945 : static void
946 0 : slot_state_push( fd_sched_t * sched, fd_sched_block_t * block, long end_tick ) {
947 0 : if( FD_UNLIKELY( sched->state_cnt>=FD_SCHED_SLOT_STATE_MAX ) ) { sched->state_drop_cnt++; return; }
948 0 : sched->state_buf[ sched->state_cnt++ ] = (fd_sched_slot_state_t){
949 0 : .slot = block->slot,
950 0 : .start_tick = block->state_start_tick,
951 0 : .end_tick = end_tick,
952 0 : .state = block->state
953 0 : };
954 0 : }
955 :
956 : static void
957 0 : slot_state_maybe_commit( fd_sched_t * sched, fd_sched_block_t * block, long now ) {
958 0 : if( block->state_cand==block->state || now-block->state_cand_tick<sched->state_debounce_ticks ) return;
959 : /* The first committed state starts at the window start. */
960 0 : if( FD_LIKELY( block->state!=FD_SCHED_SLOT_STATE_NONE ) ) {
961 0 : slot_state_push( sched, block, block->state_cand_tick );
962 0 : block->state_start_tick = block->state_cand_tick;
963 0 : }
964 0 : block->state = block->state_cand;
965 0 : }
966 :
967 : static void
968 4555 : slot_state_eval( fd_sched_t * sched, fd_sched_block_t * block ) {
969 4555 : if( FD_LIKELY( !block->state_open ) ) return;
970 :
971 0 : ulong queued_cnt = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
972 0 : ulong free_tiles = sched->txn_exec_ready_bitset[ 0 ] & sched->sigverify_ready_bitset[ 0 ] & sched->poh_ready_bitset[ 0 ] & sched->footer_ready_bitset[ 0 ] & sched->lthash_ready_bitset[ 0 ];
973 0 : uchar next;
974 0 : if( block->block_end_signaled ) next = FD_SCHED_SLOT_STATE_FINISH;
975 0 : else if( queued_cnt && !free_tiles ) next = FD_SCHED_SLOT_STATE_BLOCKED_TILES;
976 0 : else if( queued_cnt && block->state_conflict ) next = FD_SCHED_SLOT_STATE_BLOCKED_CONFLICTS;
977 0 : else if( queued_cnt ) next = block->state_cand; /* ready txn and free tile, dispatch is imminent */
978 0 : else if( !block->fec_eos ) next = FD_SCHED_SLOT_STATE_BLOCKED_NETWORK;
979 0 : else next = FD_SCHED_SLOT_STATE_FINISH;
980 :
981 0 : long now = fd_tickcount();
982 0 : slot_state_maybe_commit( sched, block, now );
983 0 : if( next!=block->state_cand ) {
984 0 : block->state_cand = next;
985 0 : block->state_cand_tick = now;
986 0 : }
987 0 : }
988 :
989 : static void
990 : slot_state_close( fd_sched_t * sched, fd_sched_block_t * block );
991 :
992 : static void
993 0 : slot_state_open( fd_sched_t * sched, fd_sched_block_t * block ) {
994 0 : ulong bank_idx = block_to_idx( sched, block );
995 0 : if( FD_UNLIKELY( sched->state_bank_idx!=ULONG_MAX && sched->state_bank_idx!=bank_idx ) ) {
996 0 : slot_state_close( sched, block_pool_ele( sched, sched->state_bank_idx ) );
997 0 : }
998 0 : sched->state_bank_idx = bank_idx;
999 0 : block->state_open = 1;
1000 0 : block->state_conflict = 0;
1001 0 : block->state = FD_SCHED_SLOT_STATE_NONE;
1002 0 : block->state_cand = FD_SCHED_SLOT_STATE_NONE;
1003 0 : block->state_start_tick = block->state_cand_tick = fd_tickcount();
1004 0 : }
1005 :
1006 : static void
1007 2 : slot_state_close( fd_sched_t * sched, fd_sched_block_t * block ) {
1008 2 : if( FD_LIKELY( !block->state_open ) ) return;
1009 0 : block->state_open = 0;
1010 0 : sched->state_bank_idx = ULONG_MAX;
1011 0 : long now = fd_tickcount();
1012 0 : slot_state_maybe_commit( sched, block, now );
1013 0 : if( FD_UNLIKELY( block->state==FD_SCHED_SLOT_STATE_NONE ) ) block->state = block->state_cand; /* window shorter than the debounce */
1014 0 : if( FD_UNLIKELY( block->state==FD_SCHED_SLOT_STATE_NONE ) ) return;
1015 0 : slot_state_push( sched, block, now );
1016 0 : }
1017 :
1018 : /* active_bank_set changes the active block. Slot states are tracked
1019 : for at most one block at a time. */
1020 :
1021 : static void
1022 1003 : active_bank_set( fd_sched_t * sched, ulong bank_idx ) {
1023 1003 : sched->active_bank_idx = bank_idx;
1024 1003 : if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) return;
1025 1001 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
1026 1001 : if( FD_LIKELY( block->block_start_signaled && !block->block_end_done && !block->dying && !block->state_open ) ) slot_state_open( sched, block );
1027 1001 : }
1028 :
1029 : __attribute__((format(printf,2,3)))
1030 : static void
1031 : fd_sched_printf( fd_sched_t * sched,
1032 : char const * fmt,
1033 12 : ... ) {
1034 12 : va_list ap;
1035 12 : ulong len;
1036 12 : va_start( ap, fmt );
1037 12 : int ret = vsnprintf( sched->print_buf+sched->print_buf_sz,
1038 12 : FD_SCHED_MAX_PRINT_BUF_SZ-sched->print_buf_sz,
1039 12 : fmt, ap );
1040 12 : va_end( ap );
1041 12 : len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, FD_SCHED_MAX_PRINT_BUF_SZ-sched->print_buf_sz-1UL ) );
1042 12 : sched->print_buf[ sched->print_buf_sz+len ] = '\0';
1043 12 : sched->print_buf_sz += len;
1044 12 : }
1045 :
1046 : FD_FN_UNUSED static void
1047 0 : print_histogram( fd_sched_t * sched, fd_histf_t * hist, ulong converter, char * title ) {
1048 0 : fd_sched_printf( sched, " +---------------------+----------------------+--------------+\n" );
1049 0 : fd_sched_printf( sched, " | %-19s | | Count |\n", title );
1050 0 : fd_sched_printf( sched, " +---------------------+----------------------+--------------+\n" );
1051 0 :
1052 0 : ulong total_count = 0;
1053 0 : for( ulong i=0UL; i<fd_histf_bucket_cnt( hist ); i++ ) {
1054 0 : total_count += fd_histf_cnt( hist, i );
1055 0 : }
1056 0 :
1057 0 : for( ulong i=0UL; i< fd_histf_bucket_cnt( hist ); i++ ) {
1058 0 : ulong bucket_count = fd_histf_cnt( hist, i );
1059 0 :
1060 0 : char * lt_str;
1061 0 : char lt_buf[ 64 ];
1062 0 : if( FD_UNLIKELY( i==fd_histf_bucket_cnt( hist )-1UL ) ) {
1063 0 : lt_str = "+Inf";
1064 0 : } else {
1065 0 : ulong edge = fd_histf_right( hist, i );
1066 0 : if( converter==FD_METRICS_CONVERTER_NANOSECONDS ) {
1067 0 : edge = fd_metrics_convert_ticks_to_nanoseconds( edge-1UL );
1068 0 : FD_TEST( fd_cstr_printf_check( lt_buf, sizeof( lt_buf ), NULL, "<= %lu nanos", edge ) );
1069 0 : } else if( converter==FD_METRICS_CONVERTER_NONE ) {
1070 0 : FD_TEST( fd_cstr_printf_check( lt_buf, sizeof( lt_buf ), NULL, "<= %lu", edge-1UL ) );
1071 0 : }
1072 0 : lt_str = lt_buf;
1073 0 : }
1074 0 :
1075 0 : /* Create visual bar - scale to max 20 characters. */
1076 0 : char bar_buf[ 22 ];
1077 0 : if( bucket_count>0UL && total_count>0UL ) {
1078 0 : ulong bar_length = (bucket_count*20UL)/total_count;
1079 0 : if( !bar_length ) bar_length = 1;
1080 0 : for( ulong j=0UL; j<bar_length; j++ ) { bar_buf[ j ] = '*'; }
1081 0 : bar_buf[ bar_length ] = '\0';
1082 0 : } else {
1083 0 : bar_buf[ 0 ] = '\0';
1084 0 : }
1085 0 :
1086 0 : fd_sched_printf( sched, " | %19s | %-20s | %12lu |\n", lt_str, bar_buf, bucket_count );
1087 0 : }
1088 0 : }
1089 :
1090 : FD_FN_UNUSED static void
1091 0 : print_block_metrics( fd_sched_t * sched, fd_sched_block_t * block ) {
1092 0 : fd_sched_printf( sched, "block idx %lu, block slot %lu, parent_slot %lu, txn_parsed_cnt %u, txn_exec_done_cnt %u, txn_sigverify_done_cnt %u, poh_hashing_done_cnt %u, poh_hash_cmp_done_cnt %u, txn_done_cnt %u, shred_cnt %u, mblk_cnt %u, mblk_tick_cnt %u, mblk_freed_cnt %u, mblk_unhashed_cnt %u, txn_pool_max_popcnt %lu/%lu, mblk_pool_max_popcnt %lu/%lu, block_pool_max_popcnt %lu/%lu, mblks_rem %lu, txns_rem %lu, residual_sz %u, fec_buf_boff %u, fec_eob %d, fec_sob %d, shred_sz_overflow %d, fec_eos %d, rooted %d\n",
1093 0 : block_to_idx( sched, block ), block->slot, block->parent_slot, block->txn_parsed_cnt, block->txn_exec_done_cnt, block->txn_sigverify_done_cnt, block->poh_hashing_done_cnt, block->poh_hash_cmp_done_cnt, block->txn_done_cnt, block->shred_cnt, block->mblk_cnt, block->mblk_tick_cnt, block->mblk_freed_cnt, block->mblk_unhashed_cnt, block->txn_pool_max_popcnt, sched->depth, block->mblk_pool_max_popcnt, sched->depth, block->block_pool_max_popcnt, sched->block_cnt_max, block->mblks_rem, block->txns_rem, block->residual_sz, block->fec_buf_boff, block->fec_eob, block->fec_sob, block->shred_sz_overflow, block->fec_eos, block->rooted );
1094 0 : }
1095 :
1096 : FD_FN_UNUSED static void
1097 8 : print_block_debug( fd_sched_t * sched, fd_sched_block_t * block ) {
1098 8 : fd_sched_printf( sched, "block idx %lu, block slot %lu, parent_slot %lu, 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, sigverify_task_in_flight_cnt %u, poh_hashing_in_flight_cnt %u, poh_mblk_in_flight_cnt %u, poh_hashing_done_cnt %u, poh_hash_cmp_done_cnt %u, footer_in_flight_cnt %u, txn_done_cnt %u, shred_cnt %u, mblk_cnt %u, mblk_tick_cnt %u, mblk_freed_cnt %u, mblk_unhashed_cnt %u, txn_pool_max_popcnt %lu/%lu, mblk_pool_max_popcnt %lu/%lu, block_pool_max_popcnt %lu/%lu, lthash_writable_cnt %u, lthash_in_flight_cnt %u, sub_queued_cnt %u, sub_in_flight_cnt %u, sub_applied_cnt %u, add_owed_cnt %u, add_created_cnt %u, add_surfaced_cnt %u, add_done_cnt %u, add_applied_cnt %u, add_undone_cnt %u, mblks_rem %lu, txns_rem %lu, residual_sz %u, fec_buf_boff %u, fec_eob %d, fec_sob %d, shred_sz_overflow %d, fec_eos %d, rooted %d, dying %d, in_rdisp %d, block_start_signaled %d, block_end_signaled %d, block_start_done %d, block_end_done %d, staged %d, lthash_drained %d, lthash_draining %d, lthash_mode_picked %d, lthash_inband %d, lthash_oob_reservation %u, staging_lane %lu\n",
1099 8 : block_to_idx( sched, block ), block->slot, block->parent_slot, 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->sigverify_task_in_flight_cnt, block->poh_hashing_in_flight_cnt, block->poh_mblk_in_flight_cnt, block->poh_hashing_done_cnt, block->poh_hash_cmp_done_cnt, block->footer_in_flight_cnt, block->txn_done_cnt, block->shred_cnt, block->mblk_cnt, block->mblk_tick_cnt, block->mblk_freed_cnt, block->mblk_unhashed_cnt, block->txn_pool_max_popcnt, sched->depth, block->mblk_pool_max_popcnt, sched->depth, block->block_pool_max_popcnt, sched->block_cnt_max, block->lthash_writable_cnt, block->lthash_in_flight_cnt, block->sub_queued_cnt, block->sub_in_flight_cnt, block->sub_applied_cnt, block->add_owed_cnt, block->add_created_cnt, block->add_surfaced_cnt, block->add_done_cnt, block->add_applied_cnt, block->add_undone_cnt, block->mblks_rem, block->txns_rem, block->residual_sz, block->fec_buf_boff, block->fec_eob, block->fec_sob, block->shred_sz_overflow, block->fec_eos, block->rooted, block->dying, block->in_rdisp, block->block_start_signaled, block->block_end_signaled, block->block_start_done, block->block_end_done, block->staged, block->lthash_drained, block->lthash_draining, block->lthash_mode_picked, block->lthash_inband, (uint)block->lthash_oob_reservation, block->staging_lane );
1100 8 : }
1101 :
1102 : FD_FN_UNUSED static void
1103 2 : print_block_and_parent( fd_sched_t * sched, fd_sched_block_t * block ) {
1104 2 : print_block_debug( sched, block );
1105 2 : fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
1106 2 : if( FD_LIKELY( parent ) ) print_block_debug( sched, parent );
1107 2 : }
1108 :
1109 : FD_FN_UNUSED static void
1110 2 : print_metrics( fd_sched_t * sched ) {
1111 2 : fd_sched_printf( sched, "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_bad_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, fork_observed_cnt %u, alut_success_cnt %u, alut_serializing_cnt %u, txn_abandoned_parsed_cnt %u, txn_abandoned_exec_done_cnt %u, txn_abandoned_done_cnt %u, txn_promoted_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_mixin_done_cnt %lu, txn_done_cnt %lu, mblk_parsed_cnt %lu, mblk_poh_hashed_cnt %lu, mblk_poh_done_cnt %lu, bytes_ingested_cnt %lu, bytes_ingested_unparsed_cnt %lu, bytes_dropped_cnt %lu, fec_cnt %lu, lthash_parsed_writable_cnt %lu, lthash_start_reg_cnt %u, lthash_late_reg_cnt %u, lthash_demote_reg_cnt %u, lthash_first_write_cnt %lu, lthash_coalesced_cnt %lu, lthash_entry_cnt %lu, lthash_sub_dispatched_cnt %lu, lthash_sub_task_cnt %lu, lthash_sub_applied_cnt %lu, lthash_add_created_cnt %lu, lthash_spec_cnt %lu, lthash_add_surfaced_cnt %lu, lthash_add_surfaced_stale_cnt %lu, lthash_add_dispatched_cnt %lu, lthash_add_task_cnt %lu, lthash_add_dispatched_stale_cnt %lu, lthash_add_batch_held_cnt %lu, lthash_add_batch_wait_idle_cnt %lu, lthash_add_applied_cnt %lu, lthash_add_undone_cnt %lu, lthash_spec_applied_cnt %lu, lthash_spec_stale_cnt %lu, lthash_spec_given_up_cnt %u, lthash_pool_full_cnt %u, lthash_spec_yield_cnt %u, lthash_frozen_cnt %lu, lthash_discarded_add_owed_cnt %u, lthash_discarded_add_pending_cnt %u, lthash_discarded_add_held_cnt %u, lthash_discarded_add_applied_cnt %u, lthash_discarded_sub_queued_cnt %u, lthash_discarded_sub_applied_cnt %u, lthash_inband_add_owed_cnt %u, lthash_inband_sub_queued_cnt %u, lthash_inband_block_cnt %u, lane_wait_cnt %lu\n",
1112 2 : 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_bad_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->fork_observed_cnt, sched->metrics->alut_success_cnt, sched->metrics->alut_serializing_cnt, sched->metrics->txn_abandoned_parsed_cnt, sched->metrics->txn_abandoned_exec_done_cnt, sched->metrics->txn_abandoned_done_cnt, sched->metrics->txn_promoted_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_mixin_done_cnt, sched->metrics->txn_done_cnt, sched->metrics->mblk_parsed_cnt, sched->metrics->mblk_poh_hashed_cnt, sched->metrics->mblk_poh_done_cnt, sched->metrics->bytes_ingested_cnt, sched->metrics->bytes_ingested_unparsed_cnt, sched->metrics->bytes_dropped_cnt, sched->metrics->fec_cnt, sched->metrics->lthash_parsed_writable_cnt, sched->metrics->lthash_start_reg_cnt, sched->metrics->lthash_late_reg_cnt, sched->metrics->lthash_demote_reg_cnt, sched->metrics->lthash_first_write_cnt, sched->metrics->lthash_coalesced_cnt, sched->metrics->lthash_entry_cnt, sched->metrics->lthash_sub_dispatched_cnt, sched->metrics->lthash_sub_task_cnt, sched->metrics->lthash_sub_applied_cnt, sched->metrics->lthash_add_created_cnt, sched->metrics->lthash_spec_cnt, sched->metrics->lthash_add_surfaced_cnt, sched->metrics->lthash_add_surfaced_stale_cnt, sched->metrics->lthash_add_dispatched_cnt, sched->metrics->lthash_add_task_cnt, sched->metrics->lthash_add_dispatched_stale_cnt, sched->metrics->lthash_add_batch_held_cnt, sched->metrics->lthash_add_batch_wait_idle_cnt, sched->metrics->lthash_add_applied_cnt, sched->metrics->lthash_add_undone_cnt, sched->metrics->lthash_spec_applied_cnt, sched->metrics->lthash_spec_stale_cnt, sched->metrics->lthash_spec_given_up_cnt, sched->metrics->lthash_pool_full_cnt, sched->metrics->lthash_spec_yield_cnt, sched->metrics->lthash_frozen_cnt, sched->metrics->lthash_discarded_add_owed_cnt, sched->metrics->lthash_discarded_add_pending_cnt, sched->metrics->lthash_discarded_add_held_cnt, sched->metrics->lthash_discarded_add_applied_cnt, sched->metrics->lthash_discarded_sub_queued_cnt, sched->metrics->lthash_discarded_sub_applied_cnt, sched->metrics->lthash_inband_add_owed_cnt, sched->metrics->lthash_inband_sub_queued_cnt, sched->metrics->lthash_inband_block_cnt, sched->metrics->lane_wait_cnt );
1113 2 : }
1114 :
1115 : FD_FN_UNUSED static void
1116 2 : print_sched( fd_sched_t * sched ) {
1117 2 : fd_sched_printf( sched, "sched canary 0x%lx, exec_cnt %lu, root_idx %lu, txn_exec_ready_bitset[ 0 ] 0x%lx, sigverify_ready_bitset[ 0 ] 0x%lx, poh_ready_bitset[ 0 ] 0x%lx, footer_ready_bitset[ 0 ] 0x%lx, lthash_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, staged_popcnt_wmk %lu, txn_pool_free_cnt %lu/%lu, lthash_add_batch_cnt %lu, lthash_draining_cnt %u, block_pool_popcnt %lu/%lu\n",
1118 2 : sched->canary, sched->exec_cnt, sched->root_idx, sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ], sched->poh_ready_bitset[ 0 ], sched->footer_ready_bitset[ 0 ], sched->lthash_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->staged_popcnt_wmk, sched->txn_pool_free_cnt, sched->depth, sched->lthash_add_batch->cnt, sched->lthash_draining_cnt, sched->block_pool_popcnt, sched->block_cnt_max );
1119 2 : fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
1120 2 : if( active_block ) print_block_debug( sched, active_block );
1121 10 : for( int l=0; l<(int)FD_SCHED_MAX_STAGING_LANES; l++ ) {
1122 8 : if( fd_ulong_extract_bit( sched->staged_bitset, l ) ) {
1123 2 : fd_sched_block_t * block = block_pool_ele( sched, sched->staged_head_bank_idx[ l ] );
1124 2 : print_block_debug( sched, block );
1125 2 : }
1126 8 : }
1127 2 : }
1128 :
1129 : FD_FN_UNUSED static void
1130 2 : print_all( fd_sched_t * sched, fd_sched_block_t * block ) {
1131 2 : print_metrics( sched );
1132 2 : print_sched( sched );
1133 2 : print_block_and_parent( sched, block );
1134 2 : }
1135 :
1136 : /* How many idle exec tiles a poll leaves free for the critical path
1137 : when it hands out anything but a transaction. We leave one idle
1138 : while the block may still get transactions and has none in flight,
1139 : so that a transaction that becomes ready dispatches right away.
1140 : Once the block has all its FEC sets, a queued transaction only
1141 : becomes ready when an in-flight one completes and frees its own
1142 : tile, so there is nothing to reserve for. The same holds while a
1143 : transaction is in flight. A single exec tile is kept busy
1144 : regardless. */
1145 : static inline int
1146 0 : idle_reserve( fd_sched_t const * sched, fd_sched_block_t const * block ) {
1147 0 : return !(block->fec_eos || block->txn_exec_in_flight_cnt>0U || sched->exec_cnt==1UL);
1148 0 : }
1149 :
1150 : /* The reserve for an LtHash batch. An LtHash batch may hold a tile for
1151 : longer than a sigverify, since its accounts may be large or cold, so
1152 : it always leaves one tile idle, except with a single exec tile. */
1153 : static inline int
1154 0 : lthash_idle_reserve( fd_sched_t const * sched ) {
1155 0 : return sched->exec_cnt>1UL;
1156 0 : }
1157 :
1158 : /* Each tile runs at most one task at a time, so no tile may be busy
1159 : in two bitsets. */
1160 : static inline void
1161 0 : exec_ready_bitsets_check( fd_sched_t * sched ) {
1162 0 : ulong mask = fd_ulong_mask_lsb( (int)sched->exec_cnt );
1163 0 : ulong exec_busy = (~sched->txn_exec_ready_bitset [ 0 ])&mask;
1164 0 : ulong sigverify_busy = (~sched->sigverify_ready_bitset[ 0 ])&mask;
1165 0 : ulong poh_busy = (~sched->poh_ready_bitset [ 0 ])&mask;
1166 0 : ulong footer_busy = (~sched->footer_ready_bitset [ 0 ])&mask;
1167 0 : ulong lthash_busy = (~sched->lthash_ready_bitset [ 0 ])&mask;
1168 0 : if( FD_UNLIKELY( (exec_busy&(sigverify_busy|poh_busy|footer_busy|lthash_busy)) | (sigverify_busy&(poh_busy|footer_busy|lthash_busy)) | (poh_busy&(footer_busy|lthash_busy)) | (footer_busy<hash_busy) ) ) {
1169 0 : FD_LOG_CRIT(( "invariant violation: txn_exec_ready_bitset 0x%lx sigverify_ready_bitset 0x%lx poh_ready_bitset 0x%lx footer_ready_bitset 0x%lx lthash_ready_bitset 0x%lx", sched->txn_exec_ready_bitset[ 0 ], sched->sigverify_ready_bitset[ 0 ], sched->poh_ready_bitset[ 0 ], sched->footer_ready_bitset[ 0 ], sched->lthash_ready_bitset[ 0 ] ));
1170 0 : }
1171 0 : }
1172 :
1173 : /* Every busy tile should be accounted for by an in-flight task of a
1174 : staged lane head, which could be an actively dispatching block, or a
1175 : dying block waiting for in-flight tasks to drain. */
1176 : static void
1177 0 : exec_busy_check( fd_sched_t * sched, fd_sched_block_t * block ) {
1178 0 : exec_ready_bitsets_check( sched );
1179 0 : ulong total_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ]&sched->sigverify_ready_bitset[ 0 ]&sched->poh_ready_bitset[ 0 ]&sched->footer_ready_bitset[ 0 ]&sched->lthash_ready_bitset[ 0 ] );
1180 0 : if( FD_UNLIKELY( block_in_flight_cnt( block )!=total_exec_busy_cnt ) ) {
1181 : /* Ideally we'd simply assert that the two sides of the equation are
1182 : equal. But abandoned blocks throw a wrench into this. We allow
1183 : abandoned blocks to have in-flight transactions that are
1184 : naturally drained while we try to dispatch from another block.
1185 : In such cases, the total number of in-flight transactions should
1186 : include the abandoned blocks too. The contract is that blocks
1187 : with in-flight transactions cannot be abandoned or demoted from
1188 : rdisp. So a dying block has to be the head of one of the staging
1189 : lanes. */
1190 : // FIXME This contract no longer true if we implement immediate
1191 : // demotion of abandoned blocks.
1192 0 : ulong total_in_flight = 0UL;
1193 0 : for( int l=0; l<(int)FD_SCHED_MAX_STAGING_LANES; l++ ) {
1194 0 : if( fd_ulong_extract_bit( sched->staged_bitset, l ) ) {
1195 0 : fd_sched_block_t * staged_block = block_pool_ele( sched, sched->staged_head_bank_idx[ l ] );
1196 0 : if( FD_UNLIKELY( block_is_in_flight( staged_block )&&!(staged_block==block||staged_block->dying) ) ) {
1197 0 : sched->print_buf_sz = 0UL;
1198 0 : print_all( sched, staged_block );
1199 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
1200 0 : FD_LOG_CRIT(( "invariant violation: in-flight block is neither active nor dying" ));
1201 0 : }
1202 0 : total_in_flight += block_in_flight_cnt( staged_block );
1203 0 : }
1204 0 : }
1205 0 : if( FD_UNLIKELY( total_in_flight!=total_exec_busy_cnt ) ) {
1206 0 : sched->print_buf_sz = 0UL;
1207 0 : print_all( sched, block );
1208 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
1209 0 : FD_LOG_CRIT(( "invariant violation: total_in_flight %lu != total_exec_busy_cnt %lu", total_in_flight, total_exec_busy_cnt ));
1210 0 : }
1211 0 : FD_LOG_DEBUG(( "exec_busy_cnt %lu checks out", total_exec_busy_cnt ));
1212 0 : }
1213 0 : }
1214 :
1215 : static inline void
1216 : record_dead_reason( fd_sched_block_t * block,
1217 2 : int dead_reason ) {
1218 2 : if( FD_LIKELY( block->dead_reason==FD_SCHED_DEAD_REASON_NONE ) ) {
1219 2 : block->dead_reason = dead_reason;
1220 2 : block->discarded = 0;
1221 2 : }
1222 2 : }
1223 :
1224 : /* Record that the block goes down with its lineage rather than for a
1225 : defect of its own, aka DEAD_ANCESTOR. Also record the parent's
1226 : deadness flavor so we can tell a discarded (but not invalid) lineage
1227 : from an invalid one.
1228 :
1229 : Gated on the block having no dead reason of its own, so a verdict
1230 : sched already reached is never overwritten. That gate neither bounds
1231 : how often the inherit runs nor sees a ruling the replay tile made,
1232 : which leaves dead_reason NONE. Callers are responsible for invoking
1233 : this only on a block that is newly going down. */
1234 : static inline void
1235 0 : record_lineage_death( fd_sched_block_t * block, fd_sched_block_t const * parent ) {
1236 0 : if( FD_LIKELY( block->dead_reason==FD_SCHED_DEAD_REASON_NONE ) ) {
1237 0 : record_dead_reason( block, FD_SCHED_DEAD_REASON_DEAD_ANCESTOR );
1238 0 : block->discarded = parent->discarded;
1239 0 : }
1240 0 : }
1241 :
1242 : static void
1243 2 : handle_bad_block( fd_sched_t * sched, fd_sched_block_t * block, int dead_reason ) {
1244 2 : record_dead_reason( block, dead_reason );
1245 2 : sched->print_buf_sz = 0UL;
1246 2 : print_all( sched, block );
1247 2 : FD_LOG_DEBUG(( "%s", sched->print_buf ));
1248 2 : subtree_abandon( sched, block );
1249 2 : sched->metrics->block_bad_cnt++;
1250 2 : check_or_set_active_block( sched );
1251 2 : }
1252 :
1253 : /* Returns the number of shred boundaries strictly before query, or
1254 : UINT_MAX once the block's shred sizes have overflowed their window.
1255 : Transaction offsets are visited in nondecreasing order, so each shred
1256 : length is scanned at most once per block. */
1257 : static uint
1258 : shred_split( fd_sched_block_t * block,
1259 7639 : uint query ) {
1260 7639 : if( FD_UNLIKELY( block->shred_sz_overflow ) ) return UINT_MAX;
1261 7639 : FD_TEST( block->shred_scan_idx<=block->shred_cnt );
1262 7639 : FD_TEST( block->shred_scan_off<=query );
1263 121367 : while( block->shred_scan_idx<block->shred_cnt ) {
1264 121367 : uint next_off = block->shred_scan_off + (uint)block->shred_sz[ block->shred_scan_idx&(uint)(FD_SCHED_SHRED_SZ_WINDOW-1UL) ];
1265 121367 : if( next_off>=query ) break;
1266 113728 : block->shred_scan_off = next_off;
1267 113728 : block->shred_scan_idx++;
1268 113728 : }
1269 7639 : return block->shred_scan_idx;
1270 7639 : }
1271 :
1272 :
1273 : /* Out-of-band LtHash helpers. */
1274 :
1275 : static inline int
1276 : block_owns_lane_map( fd_sched_t * sched,
1277 4433 : fd_sched_block_t * block ) {
1278 4433 : return block->staged && sched->lthash_lane[ block->staging_lane ].bank_idx==block_to_idx( sched, block );
1279 4433 : }
1280 :
1281 : static inline fd_lthash_value_t *
1282 : block_lthash_delta( fd_sched_t * sched,
1283 0 : fd_sched_block_t * block ) {
1284 0 : FD_TEST( block->lthash_mode_picked && !block->lthash_inband );
1285 0 : return sched->lthash_delta+block->lthash_oob_reservation;
1286 0 : }
1287 :
1288 : static inline fd_sched_lthash_entry_t *
1289 : lthash_lane_query( fd_sched_t * sched,
1290 : ulong lane,
1291 2889 : fd_acct_addr_t const * acct ) {
1292 2889 : fd_sched_lthash_lane_t * lane_map = sched->lthash_lane+lane;
1293 2889 : return lthash_map_ele_query( lane_map->map, acct, NULL, lane_map->entry_pool );
1294 2889 : }
1295 :
1296 : static void
1297 : lthash_spec_release( fd_sched_t * sched,
1298 0 : uint idx ) {
1299 0 : if( FD_UNLIKELY( (ulong)idx>=FD_SCHED_LTHASH_SPEC_MAX || sched->lthash_spec_free_cnt>=FD_SCHED_LTHASH_SPEC_MAX ) ) {
1300 0 : FD_LOG_CRIT(( "invariant violation: release of speculation entry %u, free cnt %lu", idx, sched->lthash_spec_free_cnt ));
1301 0 : }
1302 0 : sched->lthash_spec_free[ sched->lthash_spec_free_cnt++ ] = idx;
1303 0 : }
1304 :
1305 : /* Releases the lane's map. The entries' speculation entries go back
1306 : to the pool and the map forgets the accounts. Pointers to and
1307 : indices of its entries are invalid afterwards. */
1308 : static void
1309 : lthash_release_lane_map( fd_sched_t * sched,
1310 2 : ulong lane ) {
1311 2 : fd_sched_lthash_lane_t * lane_map = sched->lthash_lane+lane;
1312 2 : if( FD_UNLIKELY( lane_map->bank_idx==ULONG_MAX ) ) FD_LOG_CRIT(( "invariant violation: releasing lane %lu, which no bank owns", lane ));
1313 2 : for( ulong i=0UL; i<lane_map->entry_cnt; i++ ) {
1314 0 : if( lane_map->entry_pool[ i ].spec_idx!=UINT_MAX ) lthash_spec_release( sched, lane_map->entry_pool[ i ].spec_idx );
1315 0 : }
1316 2 : lthash_map_reset( lane_map->map );
1317 2 : lane_map->entry_cnt = 0U;
1318 2 : lane_map->bank_idx = ULONG_MAX;
1319 2 : }
1320 :
1321 : /* A new entry for acct in the block's lane map. The account owes its
1322 : one subtraction, so the entry goes on the block's sub queue. */
1323 : static fd_sched_lthash_entry_t *
1324 : lthash_entry_new( fd_sched_t * sched,
1325 : fd_sched_block_t * block,
1326 2889 : fd_acct_addr_t const * acct ) {
1327 : /* A block claims its lane map upon being staged. */
1328 2889 : if( FD_UNLIKELY( !block_owns_lane_map( sched, block ) ) ) FD_LOG_CRIT(( "invariant violation: block %lu:%lu owns no lane map", block->slot, block_to_idx( sched, block ) ));
1329 2889 : ulong lane = block->staging_lane;
1330 2889 : fd_sched_lthash_lane_t * lane_map = sched->lthash_lane+lane;
1331 2889 : if( FD_UNLIKELY( lane_map->entry_cnt==FD_RDISP_MAX_WRITERS_PER_BLOCK ) ) FD_LOG_CRIT(( "lane %lu has no free entry", lane ));
1332 2889 : if( FD_UNLIKELY( lthash_map_ele_query_const( lane_map->map, acct, NULL, lane_map->entry_pool ) ) ) {
1333 0 : FD_BASE58_ENCODE_32_BYTES( acct->b, acct_str );
1334 0 : FD_LOG_CRIT(( "invariant violation: account %s already in lane %lu", acct_str, lane ));
1335 0 : }
1336 :
1337 2889 : uint idx = lane_map->entry_cnt++;
1338 2889 : fd_sched_lthash_entry_t * e = lane_map->entry_pool+idx;
1339 2889 : e->acct = *acct;
1340 2889 : e->q_next = UINT_MAX;
1341 2889 : e->spec_idx = UINT_MAX;
1342 2889 : e->ptxn_idx = 0U;
1343 2889 : e->bank_idx = (uint)lane_map->bank_idx;
1344 2889 : e->sub_state = (uchar)FD_SCHED_LTHASH_SUB_QUEUED;
1345 2889 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_NONE;
1346 2889 : lthash_map_idx_insert( lane_map->map, idx, lane_map->entry_pool );
1347 2889 : sched->metrics->lthash_entry_cnt++;
1348 2889 : if( FD_LIKELY( block->sub_q_tail==UINT_MAX ) ) block->sub_q_head = idx;
1349 1919 : else lane_map->entry_pool[ block->sub_q_tail ].q_next = idx;
1350 2889 : block->sub_q_tail = idx;
1351 2889 : block->sub_queued_cnt++;
1352 2889 : return e;
1353 2889 : }
1354 :
1355 : /* Applies the rewrite rule to the entry. If its addition was applied,
1356 : the value in its speculation entry is subtracted from block_lthash,
1357 : the block's delta. Then the speculation entry is freed, the pseudo
1358 : transaction of record is cleared and the entry becomes ADD_NONE, so
1359 : the stale pseudo transaction is completed on the spot when it
1360 : surfaces, or is ignored when its in-flight hashing finishes.
1361 : sub_state is untouched. */
1362 : static void
1363 : lthash_entry_clear_add( fd_sched_t * sched,
1364 : fd_sched_lthash_entry_t * e,
1365 0 : fd_lthash_value_t * block_lthash ) {
1366 0 : uint spec_idx = e->spec_idx;
1367 0 : if( e->add_state==FD_SCHED_LTHASH_ADD_DONE ) {
1368 0 : if( FD_UNLIKELY( spec_idx==UINT_MAX ) ) FD_LOG_CRIT(( "invariant violation: rewrite of an applied addition with no entry to undo it, bank idx %u", e->bank_idx ));
1369 0 : fd_lthash_sub( block_lthash, sched->lthash_spec_pool+spec_idx );
1370 0 : }
1371 0 : if( spec_idx!=UINT_MAX ) {
1372 0 : lthash_spec_release( sched, spec_idx );
1373 0 : e->spec_idx = UINT_MAX;
1374 0 : }
1375 0 : e->ptxn_idx = 0U;
1376 0 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_NONE;
1377 0 : }
1378 :
1379 : /* A write to the entry's account arrived after its addition was
1380 : created, which undoes the addition. */
1381 : static void
1382 : lthash_entry_rewrite( fd_sched_t * sched,
1383 : fd_sched_block_t * block,
1384 0 : fd_sched_lthash_entry_t * e ) {
1385 0 : int undone = e->add_state==FD_SCHED_LTHASH_ADD_DONE;
1386 0 : sched->metrics->lthash_add_undone_cnt += (ulong)undone;
1387 0 : sched->metrics->lthash_spec_stale_cnt += (ulong)(!undone && e->spec_idx!=UINT_MAX);
1388 0 : block->add_undone_cnt += (uint)undone;
1389 0 : lthash_entry_clear_add( sched, e, block_lthash_delta( sched, block ) );
1390 0 : }
1391 :
1392 : static void
1393 : lthash_entry_new_write_observed( fd_sched_t * sched,
1394 : fd_sched_block_t * block,
1395 2889 : fd_acct_addr_t const * acct ) {
1396 2889 : fd_sched_lthash_entry_t * e = lthash_lane_query( sched, block->staging_lane, acct );
1397 2889 : if( FD_LIKELY( !e ) ) lthash_entry_new( sched, block, acct );
1398 0 : else lthash_entry_rewrite( sched, block, e );
1399 2889 : }
1400 :
1401 : /* The dispatcher created addition pseudo transaction ptxn_idx for the
1402 : block. The pseudo transaction's account gets stamped with ptxn_idx
1403 : as its addition of record. A speculative addition reserves a
1404 : speculation entry so the addition can be undone later if needed.
1405 : Caller is responsible for ensuring a speculation entry is free. */
1406 : static void
1407 : lthash_add_created( fd_sched_t * sched,
1408 : fd_sched_block_t * block,
1409 : ulong ptxn_idx,
1410 0 : int speculative ) {
1411 0 : FD_TEST( ptxn_idx & FD_RDISP_LTHASH_PSEUDO_TXN );
1412 0 : block->add_created_cnt++;
1413 0 : sched->metrics->lthash_add_created_cnt++;
1414 0 : fd_acct_addr_t acct[ 1 ];
1415 0 : fd_rdisp_pseudo_txn_to_addr( sched->rdisp, ptxn_idx, acct );
1416 0 : fd_sched_lthash_entry_t * e = lthash_lane_query( sched, block->staging_lane, acct );
1417 0 : if( FD_LIKELY( !e ) ) {
1418 : /* The account may have no entry yet, which happens when the block
1419 : was not the lane's owner while its transactions parsed, and then
1420 : one is created now. */
1421 0 : e = lthash_entry_new( sched, block, acct );
1422 0 : } else if( FD_UNLIKELY( e->add_state!=FD_SCHED_LTHASH_ADD_NONE || e->ptxn_idx || e->spec_idx!=UINT_MAX ) ) {
1423 : /* Otherwise the entry holds no addition of record. The dispatcher
1424 : only creates an addition for an account whose last reference in
1425 : the block is a write, and there are three ways an account gets
1426 : one. A parsed transaction writes it, start-of-block or late
1427 : registration writes it, or a given up speculation hands it back
1428 : as written. Each of those cleared the entry's addition on the
1429 : way. */
1430 0 : FD_LOG_CRIT(( "invariant violation: addition created for an account with an addition of record, block %lu:%lu, ptxn_idx 0x%lx, add_state %u, entry ptxn_idx 0x%x, spec_idx %u", block->slot, block_to_idx( sched, block ), ptxn_idx, (uint)e->add_state, e->ptxn_idx, e->spec_idx ));
1431 0 : }
1432 0 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_PENDING;
1433 0 : e->ptxn_idx = (uint)ptxn_idx;
1434 0 : if( FD_LIKELY( speculative ) ) {
1435 0 : FD_TEST( sched->lthash_spec_free_cnt );
1436 0 : e->spec_idx = sched->lthash_spec_free[ --sched->lthash_spec_free_cnt ];
1437 0 : }
1438 0 : }
1439 :
1440 : /* The block was just staged and claims its lane's map. A lane the
1441 : block already owns is left alone. */
1442 : static void
1443 : lthash_claim_lane_map( fd_sched_t * sched,
1444 1001 : fd_sched_block_t * block ) {
1445 1001 : FD_TEST( block_is_lane_head( sched, block ) );
1446 1001 : fd_sched_lthash_lane_t * lane_map = sched->lthash_lane+block->staging_lane;
1447 1001 : ulong bank_idx = block_to_idx( sched, block );
1448 1001 : if( lane_map->bank_idx==bank_idx ) return;
1449 1001 : if( FD_UNLIKELY( lane_map->bank_idx!=ULONG_MAX ) ) FD_LOG_CRIT(( "invariant violation: claiming lane %lu for bank %lu, owned by bank %lu", block->staging_lane, bank_idx, lane_map->bank_idx ));
1450 1001 : FD_TEST( bank_idx<UINT_MAX && !lane_map->entry_cnt );
1451 1001 : lane_map->bank_idx = bank_idx;
1452 1001 : }
1453 :
1454 : /* Which hashing mode does a block get? We would love to hash out of
1455 : band wherever we can, and the one thing that stands in the way is
1456 : demotion. A demoted block parks its writable set in the dispatcher's
1457 : unstaged entries, and that parking lot has limited capacity. We
1458 : don't wanna be stuck in a scenario where we have to demote but we
1459 : can't due to limited parking space. So a block reserves space when
1460 : it picks out-of-band hashing mode. Mode picking happens the first
1461 : time a block would do out-of-band work beyond the dispatcher tracking
1462 : its writers, which is its first drain or its BLOCK_START, whichever
1463 : comes first. A child starts only once its parent is done, so the
1464 : blocks holding reservations are the executing frontier of each fork
1465 : plus whatever demoted blocks still wait to be promoted. Wide forking
1466 : is the only way to run out.
1467 :
1468 : If a reservation cannot be made, the block commits to in-band
1469 : hashing. Such a block parks nothing when demoted, so every demotion
1470 : fits and a lane can always be given up. So replay keeps making
1471 : progress, albeit a tad bit slower. */
1472 : static void
1473 : lthash_pick_mode( fd_sched_t * sched,
1474 0 : fd_sched_block_t * block ) {
1475 0 : FD_TEST( !block->lthash_mode_picked );
1476 0 : FD_TEST( !block->lthash_in_flight_cnt && !block->sub_in_flight_cnt && !block->add_created_cnt );
1477 0 : block->lthash_mode_picked = 1;
1478 0 : ulong free_reservations = (~sched->lthash_oob_reserved)&fd_ulong_mask_lsb( (int)sched->lthash_oob_block_max );
1479 0 : if( FD_LIKELY( free_reservations ) ) {
1480 0 : int reservation = fd_ulong_find_lsb( free_reservations );
1481 0 : FD_TEST( fd_lthash_is_zero( sched->lthash_delta+reservation ) );
1482 0 : sched->lthash_oob_reserved = fd_ulong_set_bit( sched->lthash_oob_reserved, reservation );
1483 0 : block->lthash_oob_reservation = (uchar)reservation;
1484 0 : return;
1485 0 : }
1486 0 : ulong bank_idx = block_to_idx( sched, block );
1487 0 : block->lthash_inband = 1;
1488 0 : sched->metrics->lthash_inband_block_cnt++;
1489 0 : sched->metrics->lthash_inband_add_owed_cnt += block->add_owed_cnt;
1490 0 : sched->metrics->lthash_inband_sub_queued_cnt += block->sub_queued_cnt;
1491 0 : block->add_owed_cnt = 0U;
1492 0 : block->sub_queued_cnt = 0U;
1493 0 : block->sub_q_head = UINT_MAX;
1494 0 : block->sub_q_tail = UINT_MAX;
1495 0 : if( FD_LIKELY( block_owns_lane_map( sched, block ) ) ) lthash_release_lane_map( sched, block->staging_lane );
1496 0 : if( FD_UNLIKELY( fd_rdisp_clear_writable_set( sched->rdisp, bank_idx ) ) ) FD_LOG_CRIT(( "fd_rdisp_clear_writable_set failed for block %lu:%lu", block->slot, bank_idx ));
1497 0 : }
1498 :
1499 : static void
1500 : lthash_release_reservation( fd_sched_t * sched,
1501 2 : fd_sched_block_t * block ) {
1502 2 : if( block->lthash_mode_picked && !block->lthash_inband ) sched->lthash_oob_reserved = fd_ulong_clear_bit( sched->lthash_oob_reserved, (int)block->lthash_oob_reservation );
1503 2 : block->lthash_mode_picked = 0;
1504 2 : block->lthash_inband = 0;
1505 2 : }
1506 :
1507 : /* Marks a staged fec_eos block as draining. Polls that find nothing to
1508 : dispatch then drain it a chunk at a time. Creating pseudo
1509 : transactions costs replay tile time, so the drain yields to
1510 : everything the block must do and overlaps with exec tile work. */
1511 : static void
1512 : lthash_drain_later( fd_sched_t * sched,
1513 992 : fd_sched_block_t * block ) {
1514 992 : if( !block->staged || !block->fec_eos || block->lthash_drained || block->lthash_draining ) return;
1515 992 : sched->lthash_draining_cnt++;
1516 992 : block->lthash_draining = 1;
1517 992 : }
1518 :
1519 : /* Pops up to max of the accounts that rdisp is still tracking for us,
1520 : such that each gets an addition pseudo transaction. The block must
1521 : be insert ready. This is a no-op for an unstaged block, which drains
1522 : when promoted. An in-band block has nothing to drain and counts as
1523 : drained. Returns the number of pseudo transactions created. */
1524 : static ulong
1525 : lthash_maybe_drain( fd_sched_t * sched,
1526 : fd_sched_block_t * block,
1527 0 : ulong max ) {
1528 0 : if( !block->staged || !block->fec_eos || block->lthash_drained ) return 0UL;
1529 0 : if( FD_UNLIKELY( !block->lthash_mode_picked ) ) lthash_pick_mode( sched, block );
1530 0 : if( FD_UNLIKELY( block->lthash_inband ) ) {
1531 0 : sched->lthash_draining_cnt -= (uint)block->lthash_draining;
1532 0 : block->lthash_draining = 0;
1533 0 : block->lthash_drained = 1;
1534 0 : return 0UL;
1535 0 : }
1536 0 : ulong bank_idx = block_to_idx( sched, block );
1537 : /* fd_rdisp_add_pseudo_txn() returns 0 for a block that is not insert
1538 : ready, which we would mistake for a finished drain. A staged block
1539 : is alone on its lane, so it is insert ready by construction, as
1540 : long as it in fact holds its lane. So we assert being head, which
1541 : implies being tail. */
1542 0 : FD_TEST( block_is_lane_head( sched, block ) );
1543 0 : ulong pop_cnt = 0UL;
1544 0 : while( pop_cnt<max ) {
1545 0 : ulong ptxn_idx = fd_rdisp_add_pseudo_txn( sched->rdisp, bank_idx );
1546 0 : if( FD_UNLIKELY( !ptxn_idx ) ) break;
1547 0 : block->add_owed_cnt--;
1548 0 : lthash_add_created( sched, block, ptxn_idx, 0 );
1549 0 : pop_cnt++;
1550 0 : }
1551 0 : if( FD_UNLIKELY( pop_cnt==max ) ) {
1552 : /* The set may hold more, so the next call continues. */
1553 0 : sched->lthash_draining_cnt += (uint)!block->lthash_draining;
1554 0 : block->lthash_draining = 1;
1555 0 : return pop_cnt;
1556 0 : }
1557 0 : sched->lthash_draining_cnt -= (uint)block->lthash_draining;
1558 0 : block->lthash_draining = 0;
1559 0 : block->lthash_drained = 1;
1560 0 : return pop_cnt;
1561 0 : }
1562 :
1563 : /* Returns the draining block we should work on next. We prioritize
1564 : draining the active block, otherwise we work on the draining block on
1565 : the lowest lane. A block that is not active cannot run its additions
1566 : until it's activated, so draining it ahead of the active block only
1567 : delays the active block's end. Assumes lthash_draining_cnt is
1568 : non-zero. */
1569 : static fd_sched_block_t *
1570 0 : lthash_find_draining_block( fd_sched_t * sched ) {
1571 0 : fd_sched_block_t * active = block_pool_ele( sched, sched->active_bank_idx );
1572 0 : if( FD_LIKELY( active && active->lthash_draining ) ) return active;
1573 0 : for( ulong lanes=sched->staged_bitset; lanes; lanes=fd_ulong_pop_lsb( lanes ) ) {
1574 0 : fd_sched_block_t * block = block_pool_ele( sched, sched->staged_head_bank_idx[ fd_ulong_find_lsb( lanes ) ] );
1575 0 : if( block->lthash_draining ) return block;
1576 0 : }
1577 0 : FD_LOG_CRIT(( "invariant violation: %u blocks draining but no staged block is", sched->lthash_draining_cnt ));
1578 0 : }
1579 :
1580 : /* Paced at up to once per fd_sched_task_next_ready(), from the points
1581 : where it has nothing left to hand out. */
1582 : static inline ulong
1583 0 : lthash_drain_chunk( fd_sched_t * sched ) {
1584 0 : if( FD_LIKELY( !sched->lthash_draining_cnt ) ) return 0UL;
1585 0 : return lthash_maybe_drain( sched, lthash_find_draining_block( sched ), FD_SCHED_LTHASH_DRAIN_CHUNK );
1586 0 : }
1587 :
1588 : /* Returns the lowest lane whose head can be readily demoted, or -1 if
1589 : there is none.
1590 :
1591 : If we ever want to, we could instead pick the lane with the fewest
1592 : lane map entries to make demotion cheaper. */
1593 : static int
1594 0 : pick_idle_lane( fd_sched_t * sched ) {
1595 0 : for( ulong lanes=sched->staged_bitset; lanes; lanes=fd_ulong_pop_lsb( lanes ) ) {
1596 0 : int lane_idx = fd_ulong_find_lsb( lanes );
1597 0 : if( sched->staged_head_bank_idx[ lane_idx ]==sched->active_bank_idx ) continue;
1598 0 : if( lane_is_demotable( sched, lane_idx ) ) return lane_idx;
1599 0 : }
1600 0 : return -1;
1601 0 : }
1602 :
1603 : /* Should a FEC set wait for a staging lane? Recall that our aim is to
1604 : maximize replay throughput. Lane switching is cheap. So as long as
1605 : some lane has work queued up, we'd be able to keep exec tiles busy.
1606 : In that case, we shouldn't let the FEC set in if it needs a lane and
1607 : we don't have one, because it would just enter as unstaged, and later
1608 : down the road we would incur a promotion stall. Recall that the
1609 : scheduler's job is not being the biggest buffer holding all received
1610 : transactions. We're buffering transactions only to uncover the
1611 : critical path, and to bridge a bursty FEC producer with multiple exec
1612 : tiles consuming tasks at different rates. So the only reason we'd
1613 : ingest a FEC as unstaged is it could potentially unblock other FEC
1614 : sets queued behind it. Some of those held up FEC sets might complete
1615 : a staged block and thus free a lane. However, recall that the chain
1616 : rarely forks wide. Usually, it's one or two forks that may run for a
1617 : while. In that case, with the lanes we have, we'd be able to easily
1618 : switch between the few forks and overlap execution on one fork with
1619 : ingestion on another fork, without incurring any promotion or
1620 : demotion.
1621 :
1622 : With all that being said, in the common case where the lanes are just
1623 : full for holding blocks down one or two forks, there's always replay
1624 : progress to be made, and so we ask the FEC to wait if it needs a
1625 : lane. Only when no progress is to be made with any of the lanes we
1626 : have, which again happens only under wide forking, do we allow the
1627 : FEC in as potentially unstaged. The FEC would then actually trigger
1628 : a demotion upon entering, and take the lane of the demoted block.
1629 : Note that this implies demotion really only happens under wide
1630 : forking combined with incomplete frontiers. Also note that we really
1631 : do have to demote something in this case, otherwise replay would just
1632 : wedge, because the incomplete frontiers might be blocked behind this
1633 : upcoming FEC set.
1634 :
1635 : Finally, what do we consider to be a FEC set that needs a lane?
1636 : There are some cases that disqualifies a FEC from being given a lane.
1637 : Beyond that, a block's first FEC set needs a lane. */
1638 : static int
1639 : fec_waits_for_lane( fd_sched_t * sched,
1640 : ulong parent_bank_idx,
1641 0 : int is_first_in_block ) {
1642 0 : if( FD_LIKELY( !is_first_in_block ) ) return 0;
1643 0 : if( FD_LIKELY( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) return 0;
1644 : /* A ULONG_MAX parent index is only used for backfill, where at the
1645 : time of capacity check, the target ancestor is not known yet. So
1646 : we just assume we'll need a lane in that case. Any other parent
1647 : index should be valid. */
1648 0 : if( FD_LIKELY( parent_bank_idx<sched->block_cnt_max ) ) {
1649 0 : fd_sched_block_t * parent = block_pool_ele( sched, parent_bank_idx );
1650 0 : FD_TEST( parent->in_sched );
1651 : /* A child of a dying block is dead on arrival and doesn't need a
1652 : lane. */
1653 0 : if( FD_UNLIKELY( parent->dying ) ) return 0;
1654 : /* A child of an unstaged stageable block enters unstaged. */
1655 0 : if( !parent->staged && block_is_stageable( parent ) ) return 0;
1656 0 : }
1657 0 : for( ulong lanes=sched->staged_bitset; lanes; lanes=fd_ulong_pop_lsb( lanes ) ) {
1658 0 : fd_sched_block_t * head = block_pool_ele( sched, sched->staged_head_bank_idx[ fd_ulong_find_lsb( lanes ) ] );
1659 0 : if( block_is_dispatchable( head ) || block_is_in_flight( head ) ) return 1;
1660 0 : }
1661 0 : return 0;
1662 0 : }
1663 :
1664 : /* Returns the lowest free lane. Assumes at least one lane is free. */
1665 : static int
1666 1001 : pick_free_lane( fd_sched_t * sched ) {
1667 1001 : ulong free_lanes = (~sched->staged_bitset)&fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES );
1668 1001 : FD_TEST( free_lanes );
1669 1001 : return fd_ulong_find_lsb( free_lanes );
1670 1001 : }
1671 :
1672 : /* The block owes the addition of an account, a write that rdisp did not
1673 : see through a transaction at parse time and was just told of through
1674 : fd_rdisp_add_writable(), whose return value is rv. The caller has
1675 : brought the account's entry up to date. See to it that the addition
1676 : gets created whether or not the block has drained. */
1677 : static void
1678 : lthash_owe_addition( fd_sched_t * sched,
1679 : fd_sched_block_t * block,
1680 0 : int rv ) {
1681 0 : ulong bank_idx = block_to_idx( sched, block );
1682 0 : if( FD_LIKELY( rv==1 ) ) {
1683 0 : sched->metrics->lthash_first_write_cnt++;
1684 0 : block->add_owed_cnt++;
1685 0 : if( block->lthash_drained ) {
1686 : /* Re-drain. We might not get another chance to create a pseudo
1687 : transaction for the account, if this were for example a late
1688 : ALT expansion for an already fec_eos block. A block still
1689 : draining pops the account with the rest. */
1690 0 : block->lthash_drained = 0;
1691 0 : lthash_drain_later( sched, block );
1692 0 : }
1693 0 : return;
1694 0 : }
1695 0 : if( FD_LIKELY( rv==0 ) ) {
1696 : /* We got lucky. This account is already tracked by rdisp, which
1697 : essentially deduped the addition for us. We can later instruct
1698 : rdisp to create a pseudo transaction for the account, and it'll
1699 : capture the final value of the account. */
1700 0 : sched->metrics->lthash_coalesced_cnt++;
1701 0 : return;
1702 0 : }
1703 :
1704 : /* A staged block is alone on its lane and an unstaged block is insert
1705 : ready as well, so the dispatcher should never refuse a write. */
1706 0 : FD_LOG_CRIT(( "fd_rdisp_add_writable returned %d for block %lu:%lu", rv, block->slot, bank_idx ));
1707 0 : }
1708 :
1709 : /* For registering account writes that rdisp did not see through a
1710 : transaction at parse time. Typically, that would come from
1711 : start-of-block processing or an ALT expansion failure. */
1712 : static void
1713 : lthash_register( fd_sched_t * sched,
1714 : fd_sched_block_t * block,
1715 0 : fd_acct_addr_t const * addr ) {
1716 0 : FD_TEST( block_is_lane_head( sched, block ) );
1717 0 : int rv = fd_rdisp_add_writable( sched->rdisp, block_to_idx( sched, block ), addr );
1718 0 : if( FD_LIKELY( rv==1 ) ) lthash_entry_new_write_observed( sched, block, addr );
1719 0 : lthash_owe_addition( sched, block, rv );
1720 0 : }
1721 :
1722 : static void
1723 129026 : lthash_block_init( fd_sched_block_t * block ) {
1724 129026 : block->lthash_in_flight_cnt = 0U;
1725 129026 : block->add_owed_cnt = 0U;
1726 129026 : block->sub_q_head = UINT_MAX;
1727 129026 : block->sub_q_tail = UINT_MAX;
1728 129026 : block->sub_queued_cnt = 0U;
1729 129026 : block->sub_in_flight_cnt = 0U;
1730 129026 : block->sub_applied_cnt = 0U;
1731 129026 : block->add_created_cnt = 0U;
1732 129026 : block->add_surfaced_cnt = 0U;
1733 129026 : block->add_done_cnt = 0U;
1734 129026 : block->add_applied_cnt = 0U;
1735 129026 : block->add_undone_cnt = 0U;
1736 129026 : block->lthash_drained = 0;
1737 129026 : block->lthash_draining = 0;
1738 129026 : }
1739 :
1740 : /* Forgets the block's LtHash progress when it leaves the pipeline:
1741 : finished, abandoned, or demoted. A demoted block re-derives
1742 : everything if promoted. An abandoned block may still be draining.
1743 : The dispatcher forgets what is left of its set when the lane empties.
1744 : The delta is found through the block's reservation, so this needs to
1745 : come before the reservation is released. */
1746 : static void
1747 : lthash_block_reset( fd_sched_t * sched,
1748 2 : fd_sched_block_t * block ) {
1749 2 : sched->lthash_draining_cnt -= (uint)block->lthash_draining;
1750 2 : int owns = block_owns_lane_map( sched, block );
1751 2 : lthash_block_init( block );
1752 2 : if( FD_LIKELY( owns ) ) lthash_release_lane_map( sched, block->staging_lane );
1753 2 : if( block->lthash_mode_picked && !block->lthash_inband ) fd_lthash_zero( sched->lthash_delta+block->lthash_oob_reservation );
1754 2 : }
1755 :
1756 : static void
1757 0 : lthash_metrics_check( fd_sched_t * sched ) {
1758 0 : fd_sched_metrics_t const * m = sched->metrics;
1759 0 : ulong add_owed_cnt = 0UL, add_pending_cnt = 0UL, add_delta_cnt = 0UL, sub_queued_cnt = 0UL, sub_in_flight_cnt = 0UL, sub_delta_cnt = 0UL, task_in_flight_cnt = 0UL;
1760 0 : for( ulong i=0UL; i<sched->block_cnt_max; i++ ) {
1761 0 : fd_sched_block_t const * block = sched->block_pool+i;
1762 0 : if( !block->in_sched ) continue;
1763 0 : add_owed_cnt += block->add_owed_cnt;
1764 0 : add_pending_cnt += block->add_created_cnt-block->add_surfaced_cnt;
1765 0 : add_delta_cnt += block->add_applied_cnt-block->add_undone_cnt;
1766 0 : sub_queued_cnt += block->sub_queued_cnt;
1767 0 : sub_in_flight_cnt += block->sub_in_flight_cnt;
1768 0 : sub_delta_cnt += block->sub_applied_cnt;
1769 0 : task_in_flight_cnt += block->lthash_in_flight_cnt;
1770 0 : }
1771 0 : ulong add_on_tile_cnt = 0UL, sub_on_tile_cnt = 0UL, task_on_tile_cnt = 0UL;
1772 0 : for( ulong busy = (~sched->lthash_ready_bitset[ 0 ])&fd_ulong_mask_lsb( (int)sched->exec_cnt ); busy; busy = fd_ulong_pop_lsb( busy ) ) {
1773 0 : fd_sched_lthash_batch_t const * rec = sched->lthash_inflight+fd_ulong_find_lsb( busy );
1774 0 : if( rec->kind==FD_SCHED_TT_LTHASH_ADD ) add_on_tile_cnt += rec->cnt;
1775 0 : else sub_on_tile_cnt += rec->cnt;
1776 0 : task_on_tile_cnt++;
1777 0 : }
1778 0 : ulong add_held_cnt = sched->lthash_add_batch->cnt;
1779 0 : FD_TEST( task_in_flight_cnt==task_on_tile_cnt );
1780 0 : FD_TEST( sub_in_flight_cnt==sub_on_tile_cnt );
1781 0 : FD_TEST( m->lthash_parsed_writable_cnt+m->lthash_start_reg_cnt+m->lthash_late_reg_cnt+m->lthash_demote_reg_cnt+m->lthash_spec_given_up_cnt==m->lthash_first_write_cnt+m->lthash_coalesced_cnt );
1782 0 : FD_TEST( m->lthash_first_write_cnt==m->lthash_add_created_cnt+add_owed_cnt+m->lthash_discarded_add_owed_cnt+m->lthash_inband_add_owed_cnt );
1783 0 : FD_TEST( m->lthash_add_created_cnt==m->lthash_add_surfaced_cnt+add_pending_cnt+m->lthash_discarded_add_pending_cnt );
1784 0 : FD_TEST( m->lthash_add_surfaced_cnt==m->lthash_add_surfaced_stale_cnt+m->lthash_spec_given_up_cnt+m->lthash_add_dispatched_cnt+add_held_cnt+m->lthash_discarded_add_held_cnt );
1785 0 : FD_TEST( m->lthash_add_dispatched_cnt==m->lthash_add_dispatched_stale_cnt+m->lthash_add_applied_cnt+add_on_tile_cnt );
1786 0 : FD_TEST( m->lthash_add_applied_cnt-m->lthash_add_undone_cnt==m->lthash_frozen_cnt+add_delta_cnt+m->lthash_discarded_add_applied_cnt );
1787 0 : FD_TEST( m->lthash_entry_cnt==m->lthash_sub_dispatched_cnt+sub_queued_cnt+m->lthash_discarded_sub_queued_cnt+m->lthash_inband_sub_queued_cnt );
1788 0 : FD_TEST( m->lthash_sub_dispatched_cnt==m->lthash_sub_applied_cnt+sub_on_tile_cnt );
1789 0 : FD_TEST( m->lthash_sub_applied_cnt==m->lthash_frozen_cnt+sub_delta_cnt+m->lthash_discarded_sub_applied_cnt );
1790 0 : }
1791 :
1792 : /* Rolls the block's LtHash progress back before it leaves its lane.
1793 : Each account goes back into rdisp, to be re-derived with exactly one
1794 : subtraction and one addition when we later promote the block.
1795 : Rehashing a demoted block's progress is the price of keeping the lane
1796 : map per lane. */
1797 : static void
1798 : lthash_demote( fd_sched_t * sched,
1799 0 : fd_sched_block_t * block ) {
1800 0 : ulong bank_idx = block_to_idx( sched, block );
1801 0 : ulong lane = block->staging_lane;
1802 0 : FD_TEST( !block->sub_queued_cnt && !block->sub_in_flight_cnt && !block->lthash_in_flight_cnt && block->add_created_cnt==block->add_done_cnt );
1803 : /* A draining block has fec_eos set and has not signaled BLOCK_END,
1804 : so block_will_signal_end keeps it dispatchable and it is never
1805 : demotable. */
1806 0 : FD_TEST( !block->lthash_draining );
1807 :
1808 : /* Owed additions are respected by rdisp demotion. In addition, we
1809 : need to stick the writable accounts that we are tracking back into
1810 : rdisp. */
1811 0 : uint add_owed_cnt = block->add_owed_cnt;
1812 0 : if( block_owns_lane_map( sched, block ) ) {
1813 0 : fd_sched_lthash_lane_t * lane_map = sched->lthash_lane+lane;
1814 0 : for( ulong i=0UL; i<lane_map->entry_cnt; i++ ) {
1815 0 : fd_sched_lthash_entry_t * e = lane_map->entry_pool+i;
1816 : /* A demotable block has nothing queued or in flight, so every
1817 : entry should be SUB_DONE with no pending or dispatched
1818 : addition. */
1819 0 : FD_TEST( e->sub_state==FD_SCHED_LTHASH_SUB_DONE && (e->add_state==FD_SCHED_LTHASH_ADD_NONE || e->add_state==FD_SCHED_LTHASH_ADD_DONE) );
1820 0 : int rv = fd_rdisp_add_writable( sched->rdisp, bank_idx, &e->acct );
1821 : /* A staged block is alone on its lane, so it is insert ready. */
1822 0 : if( FD_UNLIKELY( rv<0 ) ) FD_LOG_CRIT(( "fd_rdisp_add_writable failed for demoted block %lu:%lu", block->slot, bank_idx ));
1823 : /* A 0 means rdisp already tracks the account and owes it an
1824 : addition. We don't count that as a coalesce, so
1825 : lthash_coalesced_cnt only counts coalescing under normal
1826 : operation. */
1827 0 : sched->metrics->lthash_demote_reg_cnt += (uint)(rv==1);
1828 0 : sched->metrics->lthash_first_write_cnt += (ulong)(rv==1);
1829 0 : add_owed_cnt += (uint)(rv==1);
1830 0 : }
1831 0 : }
1832 : /* Of the six discarded counters only the two delta counters below
1833 : apply here. The check above ensures nothing queued and nothing
1834 : unsurfaced. The owed additions are not lost, since rdisp will
1835 : carry them through demotion, and they stay in the live counter. */
1836 0 : sched->metrics->lthash_discarded_sub_applied_cnt += block->sub_applied_cnt;
1837 0 : sched->metrics->lthash_discarded_add_applied_cnt += block->add_applied_cnt-block->add_undone_cnt;
1838 0 : lthash_block_reset( sched, block );
1839 0 : block->add_owed_cnt = add_owed_cnt;
1840 0 : }
1841 :
1842 : static inline void
1843 : lthash_batch_init( fd_sched_block_t * block,
1844 : fd_sched_lthash_batch_t * batch,
1845 : ulong bank_idx,
1846 0 : ulong kind ) {
1847 0 : batch->bank_idx = bank_idx;
1848 0 : batch->cnt = 0UL;
1849 0 : batch->lane = (uint)block->staging_lane;
1850 0 : batch->kind = (uchar)kind;
1851 0 : }
1852 :
1853 : static inline void
1854 : lthash_batch_append( fd_sched_t * sched,
1855 : fd_sched_lthash_batch_t * batch,
1856 : fd_sched_lthash_entry_t * e,
1857 0 : ulong ptxn_idx ) {
1858 0 : FD_TEST( batch->cnt<FD_SCHED_LTHASH_BATCH_MAX );
1859 0 : ulong i = batch->cnt++;
1860 0 : batch->entry_idx[ i ] = (uint)(e-sched->lthash_lane[ batch->lane ].entry_pool);
1861 0 : batch->ptxn_idx [ i ] = (uint)ptxn_idx;
1862 0 : batch->accts [ i ] = e->acct;
1863 0 : }
1864 :
1865 : static void
1866 : lthash_dispatch( fd_sched_t * sched,
1867 : fd_sched_block_t * block,
1868 : fd_sched_lthash_batch_t const * batch,
1869 : int exec_tile_idx,
1870 0 : fd_sched_task_t * out ) {
1871 0 : FD_TEST( batch->cnt && batch->cnt<=FD_SCHED_LTHASH_BATCH_MAX );
1872 0 : fd_sched_lthash_batch_t * rec = sched->lthash_inflight+exec_tile_idx;
1873 0 : if( rec!=batch ) { /* FIXME optimize for end-of-block additions */
1874 0 : rec->bank_idx = batch->bank_idx;
1875 0 : rec->cnt = batch->cnt;
1876 0 : rec->lane = batch->lane;
1877 0 : rec->kind = batch->kind;
1878 0 : fd_memcpy( rec->entry_idx, batch->entry_idx, batch->cnt*sizeof(uint) );
1879 0 : fd_memcpy( rec->ptxn_idx, batch->ptxn_idx, batch->cnt*sizeof(uint) );
1880 0 : fd_memcpy( rec->accts, batch->accts, batch->cnt*sizeof(fd_acct_addr_t) );
1881 0 : }
1882 :
1883 0 : out->task_type = rec->kind;
1884 0 : out->lthash->bank_idx = rec->bank_idx;
1885 0 : out->lthash->exec_idx = (ulong)exec_tile_idx;
1886 0 : out->lthash->cnt = rec->cnt;
1887 0 : out->lthash->accts = rec->accts;
1888 :
1889 0 : sched->lthash_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->lthash_ready_bitset[ 0 ], exec_tile_idx );
1890 0 : sched->tile_to_bank_idx[ exec_tile_idx ] = rec->bank_idx;
1891 0 : block->lthash_in_flight_cnt++;
1892 0 : if( rec->kind==FD_SCHED_TT_LTHASH_ADD ) {
1893 0 : sched->metrics->lthash_add_dispatched_cnt += rec->cnt;
1894 0 : sched->metrics->lthash_add_task_cnt++;
1895 0 : } else {
1896 0 : sched->metrics->lthash_sub_dispatched_cnt += rec->cnt;
1897 0 : sched->metrics->lthash_sub_task_cnt++;
1898 0 : }
1899 0 : exec_busy_check( sched, block );
1900 0 : }
1901 :
1902 : /* A pseudo transaction surfaced from fd_rdisp_get_next_ready() for the
1903 : active block. If it is still the account's pseudo transaction of
1904 : record, mark its entry ADD_DISPATCHED, meaning it has left the
1905 : dispatcher whether or not it will be staged for batching, and return
1906 : the entry. Otherwise the account has a later rewrite, so complete
1907 : the pseudo transaction without hashing and return NULL. */
1908 : static fd_sched_lthash_entry_t *
1909 : lthash_surface( fd_sched_t * sched,
1910 : fd_sched_block_t * block,
1911 : ulong bank_idx,
1912 0 : ulong ptxn_idx ) {
1913 0 : block->add_surfaced_cnt++;
1914 0 : sched->metrics->lthash_add_surfaced_cnt++;
1915 0 : fd_acct_addr_t acct[ 1 ];
1916 0 : fd_rdisp_pseudo_txn_to_addr( sched->rdisp, ptxn_idx, acct );
1917 0 : fd_sched_lthash_entry_t * e = lthash_lane_query( sched, block->staging_lane, acct );
1918 0 : if( FD_UNLIKELY( !e ) ) {
1919 : /* Every pseudo transaction of a head block must have an entry. */
1920 0 : sched->print_buf_sz = 0UL;
1921 0 : print_all( sched, block );
1922 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
1923 0 : FD_LOG_CRIT(( "invariant violation: pseudo transaction 0x%lx of block %lu:%lu has no entry", ptxn_idx, block->slot, bank_idx ));
1924 0 : }
1925 0 : FD_TEST( e->bank_idx==(uint)bank_idx );
1926 0 : if( FD_UNLIKELY( e->ptxn_idx!=(uint)ptxn_idx ) ) {
1927 0 : sched->metrics->lthash_add_surfaced_stale_cnt++;
1928 0 : block->add_done_cnt++;
1929 0 : fd_rdisp_complete_txn( sched->rdisp, ptxn_idx, 1 );
1930 0 : return NULL;
1931 0 : }
1932 0 : FD_TEST( e->add_state==FD_SCHED_LTHASH_ADD_PENDING );
1933 0 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_DISPATCHED;
1934 0 : return e;
1935 0 : }
1936 :
1937 : static void
1938 : lthash_dispatch_one_add( fd_sched_t * sched,
1939 : fd_sched_block_t * block,
1940 : ulong bank_idx,
1941 : fd_sched_lthash_entry_t * e,
1942 : ulong ptxn_idx,
1943 : int exec_tile_idx,
1944 0 : fd_sched_task_t * out ) {
1945 0 : fd_sched_lthash_batch_t * rec = sched->lthash_inflight+exec_tile_idx;
1946 0 : lthash_batch_init( block, rec, bank_idx, FD_SCHED_TT_LTHASH_ADD );
1947 0 : lthash_batch_append( sched, rec, e, ptxn_idx );
1948 0 : lthash_dispatch( sched, block, rec, exec_tile_idx, out );
1949 0 : }
1950 :
1951 : /* A speculative addition that surfaced when the block may no longer
1952 : speculate on it, or when the idleness policy would not let it onto a
1953 : tile, is given up. The surfaced pseudo transaction is completed
1954 : without hashing. Its entry is rewritten, which frees the speculation
1955 : entry, and the account goes back to the dispatcher as written, the
1956 : way any write does, so the block owes the addition again. No
1957 : transaction actually rewrote the account, so this is neither a stale
1958 : addition nor an invalidated speculation for metric purposes. */
1959 : static void
1960 : lthash_spec_give_up( fd_sched_t * sched,
1961 : fd_sched_block_t * block,
1962 : ulong bank_idx,
1963 : fd_sched_lthash_entry_t * e,
1964 0 : ulong ptxn_idx ) {
1965 0 : sched->metrics->lthash_spec_given_up_cnt++;
1966 : /* Complete the pseudo transaction first, so that it is no longer the
1967 : account's last reference in rdisp if a re-drain below creates the
1968 : new one. */
1969 0 : block->add_done_cnt++;
1970 0 : fd_rdisp_complete_txn( sched->rdisp, ptxn_idx, 1 );
1971 0 : lthash_entry_clear_add( sched, e, block_lthash_delta( sched, block ) );
1972 0 : lthash_owe_addition( sched, block, fd_rdisp_add_writable( sched->rdisp, bank_idx, &e->acct ) );
1973 0 : }
1974 :
1975 : static void
1976 : lthash_dispatch_held_batch( fd_sched_t * sched,
1977 : fd_sched_block_t * block,
1978 : ulong bank_idx,
1979 : int exec_tile_idx,
1980 0 : fd_sched_task_t * out ) {
1981 0 : fd_sched_lthash_batch_t * batch = sched->lthash_add_batch;
1982 : /* The held batch belongs to the active block. Its surfaced,
1983 : unfinished additions keep the block in flight, so it is not
1984 : deactivated, demoted or pruned and does not signal BLOCK_END, and
1985 : no other block is activated behind the batch. The one way out is
1986 : abandon, which releases the batch where it marks the block dying.
1987 : So a held batch is either dispatched or released before its block
1988 : goes anywhere. */
1989 0 : FD_TEST( batch->cnt && batch->kind==FD_SCHED_TT_LTHASH_ADD && batch->bank_idx==bank_idx && batch->lane==(uint)block->staging_lane );
1990 :
1991 0 : for( ulong i=0UL; i<batch->cnt; i++ ) {
1992 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ batch->lane ].entry_pool+batch->entry_idx[ i ];
1993 0 : if( FD_UNLIKELY( e->ptxn_idx!=batch->ptxn_idx[ i ] || e->add_state!=FD_SCHED_LTHASH_ADD_DISPATCHED ) ) {
1994 : /* A non-speculative addition cannot become stale after it has
1995 : surfaced. See the account state machine. So no batched pseudo
1996 : transaction can actually be invalidated by a rewrite. */
1997 0 : sched->print_buf_sz = 0UL;
1998 0 : print_all( sched, block );
1999 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2000 0 : FD_LOG_CRIT(( "invariant violation: held addition %lu of %lu of block %lu:%lu was rewritten, ticket 0x%x, entry 0x%x state %u", i, batch->cnt, block->slot, bank_idx, batch->ptxn_idx[ i ], e->ptxn_idx, (uint)e->add_state ));
2001 0 : }
2002 0 : }
2003 0 : lthash_dispatch( sched, block, batch, exec_tile_idx, out );
2004 0 : batch->cnt = 0UL;
2005 0 : }
2006 :
2007 : static void
2008 : lthash_release_held_batch( fd_sched_t * sched,
2009 2 : fd_sched_block_t * block ) {
2010 2 : fd_sched_lthash_batch_t * batch = sched->lthash_add_batch;
2011 2 : if( FD_LIKELY( !batch->cnt || batch->bank_idx!=block_to_idx( sched, block ) ) ) return;
2012 0 : for( ulong i=0UL; i<batch->cnt; i++ ) {
2013 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ batch->lane ].entry_pool+batch->entry_idx[ i ];
2014 0 : if( FD_LIKELY( e->ptxn_idx==batch->ptxn_idx[ i ] ) ) {
2015 0 : e->ptxn_idx = 0U;
2016 0 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_NONE;
2017 0 : }
2018 0 : block->add_done_cnt++;
2019 0 : fd_rdisp_complete_txn( sched->rdisp, (ulong)batch->ptxn_idx[ i ], 1 );
2020 0 : }
2021 0 : sched->metrics->lthash_discarded_add_held_cnt += (uint)batch->cnt;
2022 0 : batch->cnt = 0UL;
2023 0 : }
2024 :
2025 : /* Dispatches up to cap of the block's oldest queued subtractions as one
2026 : task to the highest idle tile in ready_bitset, if more than threshold
2027 : tiles are idle. cap is in [1, FD_SCHED_LTHASH_BATCH_MAX]. Returns 1
2028 : if a task was written to out, 0 otherwise. */
2029 : static int
2030 : lthash_dispatch_sub( fd_sched_t * sched,
2031 : fd_sched_block_t * block,
2032 : ulong bank_idx,
2033 : ulong ready_bitset,
2034 : int threshold,
2035 : ulong cap,
2036 0 : fd_sched_task_t * out ) {
2037 0 : if( FD_UNLIKELY( !block->sub_queued_cnt ) ) return 0;
2038 0 : if( FD_UNLIKELY( fd_ulong_popcnt( ready_bitset )<=threshold ) ) return 0;
2039 :
2040 0 : int exec_tile_idx = fd_ulong_find_msb( ready_bitset );
2041 0 : ulong lane = block->staging_lane;
2042 0 : fd_sched_lthash_batch_t * batch = sched->lthash_inflight+exec_tile_idx;
2043 0 : lthash_batch_init( block, batch, bank_idx, FD_SCHED_TT_LTHASH_SUB );
2044 0 : while( block->sub_queued_cnt && batch->cnt<cap ) {
2045 0 : uint entry_idx = block->sub_q_head;
2046 0 : FD_TEST( entry_idx!=UINT_MAX );
2047 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ lane ].entry_pool+entry_idx;
2048 0 : FD_TEST( e->sub_state==FD_SCHED_LTHASH_SUB_QUEUED && e->bank_idx==(uint)bank_idx );
2049 :
2050 0 : block->sub_q_head = e->q_next;
2051 0 : if( FD_UNLIKELY( block->sub_q_head==UINT_MAX ) ) block->sub_q_tail = UINT_MAX;
2052 0 : e->q_next = UINT_MAX;
2053 0 : e->sub_state = (uchar)FD_SCHED_LTHASH_SUB_DISPATCHED;
2054 0 : block->sub_queued_cnt--;
2055 0 : block->sub_in_flight_cnt++;
2056 0 : lthash_batch_append( sched, batch, e, 0UL );
2057 0 : }
2058 0 : lthash_dispatch( sched, block, batch, exec_tile_idx, out );
2059 0 : return 1;
2060 0 : }
2061 :
2062 : /* The block is assumed to be active, and nothing else of it could go
2063 : out this poll, either because nothing is queued or because every
2064 : queued transaction is blocked by something in-flight. The block is
2065 : also assumed to satisfy block_may_speculate(). The dispatcher hands
2066 : us an account whose writers have all completed, so the guess is READY
2067 : at once and goes straight to a tile. The one exception is a
2068 : serializing transaction still outstanding, which every pseudo
2069 : transaction created after it waits behind. Speculation is paused for
2070 : a moment until that serializing transaction completes.
2071 :
2072 : Returns 1 with a task in out, or 0 when a pseudo transaction was
2073 : created but waits on a serializing transaction, so the caller returns
2074 : without a task, or -1 when speculation does not apply and the caller
2075 : proceeds. */
2076 : static inline int
2077 : lthash_speculate( fd_sched_t * sched,
2078 : fd_sched_block_t * block,
2079 : ulong bank_idx,
2080 : ulong ready_bitset,
2081 0 : fd_sched_task_t * out ) {
2082 : /* Any unsurfaced pseudo transaction is a speculation, since with
2083 : fec_eos clear there has been no drain. */
2084 0 : uint spec_pending = block->add_created_cnt-block->add_surfaced_cnt;
2085 0 : if( spec_pending ) {
2086 : /* We speculate one addition at a time. */
2087 0 : FD_TEST( spec_pending==1U );
2088 0 : return -1;
2089 0 : }
2090 :
2091 0 : if( fd_ulong_popcnt( ready_bitset )<=idle_reserve( sched, block ) ) return -1;
2092 :
2093 0 : ulong ptxn_idx = fd_rdisp_add_ready_pseudo_txn( sched->rdisp, bank_idx );
2094 0 : if( FD_UNLIKELY( !ptxn_idx ) ) FD_LOG_CRIT(( "invariant violation: block %lu:%lu owes %u additions and has a ready account but fd_rdisp_add_ready_pseudo_txn returned none", block->slot, bank_idx, block->add_owed_cnt ));
2095 0 : sched->metrics->lthash_spec_cnt++;
2096 0 : block->add_owed_cnt--;
2097 0 : lthash_add_created( sched, block, ptxn_idx, 1 );
2098 :
2099 : /* We expect to get either the only pseudo transaction, the one we
2100 : just instructed rdisp to create, or nothing because it is blocked
2101 : by a serializing transaction. There should otherwise be no task
2102 : queued, especially no other pseudo transaction queued. */
2103 0 : ulong idx = fd_rdisp_get_next_ready( sched->rdisp, bank_idx );
2104 0 : if( FD_LIKELY( idx==ptxn_idx ) ) {
2105 0 : fd_sched_lthash_entry_t * e = lthash_surface( sched, block, bank_idx, idx );
2106 0 : FD_TEST( e );
2107 0 : lthash_dispatch_one_add( sched, block, bank_idx, e, idx, fd_ulong_find_msb( ready_bitset ), out );
2108 0 : return 1;
2109 0 : }
2110 0 : if( FD_UNLIKELY( idx ) ) FD_LOG_CRIT(( "invariant violation: fd_rdisp_get_next_ready returned 0x%lx instead of pseudo transaction 0x%lx of block %lu:%lu", idx, ptxn_idx, block->slot, bank_idx ));
2111 :
2112 : /* Pseudo transaction pending behind a serializing transaction. It'll
2113 : be eligible for surfacing once that transaction completes. */
2114 0 : return 0;
2115 0 : }
2116 :
2117 :
2118 : /* Public functions. */
2119 :
2120 : ulong
2121 12064 : fd_sched_align( void ) {
2122 12064 : return fd_ulong_max( alignof(fd_sched_t),
2123 12064 : fd_ulong_max( fd_rdisp_align(),
2124 12064 : fd_ulong_max( alignof(fd_sched_block_t), 64UL ))); /* Minimally cache line aligned. */
2125 12064 : }
2126 :
2127 : static inline ulong
2128 4028 : sched_lthash_batch_sz( void ) {
2129 : /* One batch per exec tile, plus one staging area for the addition
2130 : batch being assembled or held. */
2131 4028 : return (FD_SCHED_MAX_EXEC_TILE_CNT+1UL)*sizeof(fd_sched_lthash_batch_t);
2132 4028 : }
2133 :
2134 : ulong
2135 : fd_sched_footprint( ulong depth,
2136 : ulong block_cnt_max,
2137 : ulong max_shreds_per_block,
2138 1006 : ulong max_txn_per_slot ) {
2139 1006 : if( FD_UNLIKELY( depth<FD_SCHED_MIN_DEPTH || depth>FD_SCHED_MAX_DEPTH ) ) return 0UL; /* bad depth */
2140 1006 : if( FD_UNLIKELY( !block_cnt_max ) ) return 0UL; /* bad block_cnt_max */
2141 1006 : if( FD_UNLIKELY( depth>UINT_MAX-1UL ) ) return 0UL; /* mblk_pool use uint as pointers */
2142 1006 : if( FD_UNLIKELY( !max_shreds_per_block || max_shreds_per_block>UINT_MAX ) ) return 0UL; /* shred_cnt is uint */
2143 1006 : if( FD_UNLIKELY( !max_txn_per_slot || max_txn_per_slot >UINT_MAX ) ) return 0UL; /* txn_parsed_cnt is uint */
2144 :
2145 1006 : ulong rdisp_footprint = fd_rdisp_footprint( depth, block_cnt_max, FD_SCHED_LTHASH_ACCT_EXTRA );
2146 1006 : if( FD_UNLIKELY( !rdisp_footprint ) ) return 0UL;
2147 :
2148 1006 : ulong l = FD_LAYOUT_INIT;
2149 1006 : l = FD_LAYOUT_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
2150 1006 : l = FD_LAYOUT_APPEND( l, fd_rdisp_align(), rdisp_footprint ); /* dispatcher */
2151 1006 : l = FD_LAYOUT_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) ); /* block pool */
2152 1006 : l = FD_LAYOUT_APPEND( l, ref_q_align(), ref_q_footprint( block_cnt_max ) );
2153 1006 : l = FD_LAYOUT_APPEND( l, alignof(fd_txn_p_t), depth*sizeof(fd_txn_p_t) ); /* txn_pool */
2154 1006 : l = FD_LAYOUT_APPEND( l, alignof(fd_sched_txn_info_t), depth*sizeof(fd_sched_txn_info_t) ); /* txn_info_pool */
2155 1006 : l = FD_LAYOUT_APPEND( l, alignof(fd_sched_mblk_t), depth*sizeof(fd_sched_mblk_t) ); /* mblk_pool */
2156 5030 : for( ulong i=0UL; i<FD_SCHED_MAX_STAGING_LANES; i++ ) {
2157 4024 : l = FD_LAYOUT_APPEND( l, alignof(fd_sched_lthash_entry_t), FD_RDISP_MAX_WRITERS_PER_BLOCK*sizeof(fd_sched_lthash_entry_t) );
2158 4024 : l = FD_LAYOUT_APPEND( l, lthash_map_align(), lthash_map_footprint( lthash_chain_cnt() ) );
2159 4024 : }
2160 1006 : l = FD_LAYOUT_APPEND( l, alignof(fd_lthash_value_t), FD_SCHED_LTHASH_SPEC_MAX*sizeof(fd_lthash_value_t) ); /* lthash_spec_pool */
2161 1006 : l = FD_LAYOUT_APPEND( l, alignof(uint), FD_SCHED_LTHASH_SPEC_MAX*sizeof(uint) ); /* lthash_spec_free */
2162 1006 : l = FD_LAYOUT_APPEND( l, alignof(ulong), sched_lthash_batch_sz() ); /* lthash_inflight, lthash_add_batch */
2163 1006 : return FD_LAYOUT_FINI( l, fd_sched_align() );
2164 1006 : }
2165 :
2166 : void *
2167 : fd_sched_new( void * mem,
2168 : fd_rng_t * rng,
2169 : ulong depth,
2170 : ulong block_cnt_max,
2171 : ulong max_shreds_per_block,
2172 : ulong max_txn_per_slot,
2173 1007 : ulong exec_cnt ) {
2174 :
2175 1007 : if( FD_UNLIKELY( !mem ) ) {
2176 0 : FD_LOG_WARNING(( "NULL mem" ));
2177 0 : return NULL;
2178 0 : }
2179 :
2180 1007 : if( FD_UNLIKELY( !rng ) ) {
2181 0 : FD_LOG_WARNING(( "NULL rng" ));
2182 0 : return NULL;
2183 0 : }
2184 :
2185 1007 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, fd_sched_align() ) ) ) {
2186 0 : FD_LOG_WARNING(( "misaligned mem (%p)", mem ));
2187 0 : return NULL;
2188 0 : }
2189 :
2190 1007 : if( FD_UNLIKELY( depth<FD_SCHED_MIN_DEPTH || depth>FD_SCHED_MAX_DEPTH ) ) {
2191 0 : FD_LOG_WARNING(( "bad depth (%lu)", depth ));
2192 0 : return NULL;
2193 0 : }
2194 :
2195 1007 : if( FD_UNLIKELY( !block_cnt_max ) ) {
2196 0 : FD_LOG_WARNING(( "bad block_cnt_max (%lu)", block_cnt_max ));
2197 0 : return NULL;
2198 0 : }
2199 :
2200 1007 : if( FD_UNLIKELY( depth>UINT_MAX-1UL ) ) {
2201 0 : FD_LOG_WARNING(( "bad depth (%lu)", depth ));
2202 0 : return NULL;
2203 0 : }
2204 :
2205 1007 : if( FD_UNLIKELY( !max_shreds_per_block || max_shreds_per_block>UINT_MAX ) ) {
2206 0 : FD_LOG_WARNING(( "bad max_shreds_per_block (%lu)", max_shreds_per_block ));
2207 0 : return NULL;
2208 0 : }
2209 :
2210 1007 : if( FD_UNLIKELY( !max_txn_per_slot || max_txn_per_slot>UINT_MAX ) ) {
2211 0 : FD_LOG_WARNING(( "bad max_txn_per_slot (%lu)", max_txn_per_slot ));
2212 0 : return NULL;
2213 0 : }
2214 :
2215 1007 : if( FD_UNLIKELY( !exec_cnt || exec_cnt>FD_SCHED_MAX_EXEC_TILE_CNT ) ) {
2216 0 : FD_LOG_WARNING(( "bad exec_cnt (%lu)", exec_cnt ));
2217 0 : return NULL;
2218 0 : }
2219 :
2220 1007 : FD_SCRATCH_ALLOC_INIT( l, mem );
2221 1007 : fd_sched_t * sched = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
2222 1007 : void * _rdisp = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(), fd_rdisp_footprint( depth, block_cnt_max, FD_SCHED_LTHASH_ACCT_EXTRA ) );
2223 1007 : void * _bpool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) );
2224 1007 : void * _ref_q = FD_SCRATCH_ALLOC_APPEND( l, ref_q_align(), ref_q_footprint( block_cnt_max ) );
2225 1007 : fd_txn_p_t * _txn_pool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txn_p_t), depth*sizeof(fd_txn_p_t) );
2226 1007 : fd_sched_txn_info_t * _txn_info_pool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_txn_info_t), depth*sizeof(fd_sched_txn_info_t) );
2227 1007 : fd_sched_mblk_t * _mblk_pool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_mblk_t), depth*sizeof(fd_sched_mblk_t) );
2228 5034 : for( ulong i=0UL; i<FD_SCHED_MAX_STAGING_LANES; i++ ) {
2229 4027 : /* */ FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_lthash_entry_t), FD_RDISP_MAX_WRITERS_PER_BLOCK*sizeof(fd_sched_lthash_entry_t) );
2230 4027 : void * _lane_map = FD_SCRATCH_ALLOC_APPEND( l, lthash_map_align(), lthash_map_footprint( lthash_chain_cnt() ) );
2231 4027 : FD_TEST( lthash_map_new( _lane_map, lthash_chain_cnt(), fd_rng_ulong( rng ) ) );
2232 4027 : sched->lthash_lane[ i ].entry_cnt = 0U;
2233 4027 : sched->lthash_lane[ i ].bank_idx = ULONG_MAX;
2234 4027 : }
2235 2014 : /* */ FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_lthash_value_t), FD_SCHED_LTHASH_SPEC_MAX*sizeof(fd_lthash_value_t) );
2236 1007 : uint * _spec_free = FD_SCRATCH_ALLOC_APPEND( l, alignof(uint), FD_SCHED_LTHASH_SPEC_MAX*sizeof(uint) );
2237 1007 : void * _lthash_batch = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), sched_lthash_batch_sz() );
2238 1007 : FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
2239 :
2240 1007 : sched->txn_pool = _txn_pool;
2241 1007 : sched->txn_info_pool = _txn_info_pool;
2242 1007 : sched->mblk_pool = _mblk_pool;
2243 :
2244 1007 : FD_TEST( fd_rdisp_new( _rdisp, depth, block_cnt_max, FD_SCHED_LTHASH_ACCT_EXTRA, fd_rng_ulong( rng ) ) );
2245 :
2246 : /* Acquire is LIFO, so this hands out speculation entries 0, 1, 2, ... */
2247 8258543 : for( ulong i=0UL; i<FD_SCHED_LTHASH_SPEC_MAX; i++ ) _spec_free[ i ] = (uint)(FD_SCHED_LTHASH_SPEC_MAX-1UL-i);
2248 1007 : sched->lthash_spec_free_cnt = FD_SCHED_LTHASH_SPEC_MAX;
2249 1007 : fd_memset( sched->lthash_delta, 0, sizeof(sched->lthash_delta) );
2250 1007 : fd_memset( _lthash_batch, 0, sched_lthash_batch_sz() );
2251 :
2252 1007 : fd_sched_block_t * bpool = (fd_sched_block_t *)_bpool;
2253 130031 : for( ulong i=0; i<block_cnt_max; i++ ) {
2254 129024 : bpool[ i ].in_sched = 0;
2255 129024 : lthash_block_init( bpool+i );
2256 129024 : mblk_slist_new( bpool[ i ].mblks_unhashed );
2257 129024 : mblk_slist_new( bpool[ i ].mblks_mixin_in_progress );
2258 129024 : }
2259 :
2260 1007 : FD_TEST( fd_chkdup_new( sched->chkdup, rng ) );
2261 :
2262 1007 : fd_memset( sched->metrics, 0, sizeof(fd_sched_metrics_t) );
2263 1007 : sched->txn_in_flight_last_tick = fd_tickcount(); /* boot counts as idle */
2264 1007 : sched->next_ready_last_tick = LONG_MAX;
2265 1007 : sched->next_ready_last_bank_idx = ULONG_MAX;
2266 1007 : sched->state_debounce_ticks = (long)( (double)FD_SCHED_SLOT_STATE_DEBOUNCE_NS*fd_tempo_tick_per_ns( NULL ) );
2267 1007 : sched->state_bank_idx = ULONG_MAX;
2268 1007 : sched->state_cnt = 0UL;
2269 1007 : sched->state_drop_cnt = 0UL;
2270 :
2271 1007 : sched->canary = FD_SCHED_MAGIC;
2272 1007 : sched->depth = depth;
2273 1007 : sched->lthash_draining_cnt = 0U;
2274 1007 : sched->block_cnt_max = block_cnt_max;
2275 1007 : sched->max_shreds_per_block = max_shreds_per_block;
2276 1007 : sched->max_txn_per_slot = max_txn_per_slot;
2277 1007 : sched->max_mblk_per_slot = fd_ulong_min( FD_SCHED_MAX_MBLK_PER_SLOT*max_shreds_per_block/FD_SHRED_BLK_MAX, UINT_MAX ); /* mblk_cnt is uint */
2278 1007 : sched->exec_cnt = exec_cnt;
2279 1007 : sched->bypass_poh_verify = 0;
2280 1007 : sched->bypass_alut_resolution = 0;
2281 1007 : sched->sigverify_group = FD_ED25519_VERIFIER_GROUP;
2282 1007 : sched->lthash_txn_writers_max = FD_SCHED_LTHASH_TXN_WRITERS_MAX;
2283 1007 : sched->lthash_oob_reserved = 0UL;
2284 1007 : sched->lthash_oob_block_max = FD_SCHED_LTHASH_OOB_BLOCK_MAX;
2285 1007 : sched->root_idx = ULONG_MAX;
2286 1007 : sched->active_bank_idx = ULONG_MAX;
2287 1007 : sched->last_active_bank_idx = ULONG_MAX;
2288 1007 : sched->staged_bitset = 0UL;
2289 5039 : for( ulong l=0UL; l<FD_SCHED_MAX_STAGING_LANES; l++ ) sched->staged_head_bank_idx[ l ] = ULONG_MAX;
2290 1007 : sched->staged_popcnt_wmk = 0UL;
2291 :
2292 1007 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
2293 1007 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
2294 1007 : sched->poh_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
2295 1007 : sched->footer_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
2296 1007 : sched->lthash_ready_bitset[ 0 ] = fd_ulong_mask_lsb( (int)exec_cnt );
2297 :
2298 1007 : sched->txn_pool_free_cnt = depth-1UL; /* -1 because index 0 is unusable as a sentinel reserved by the dispatcher */
2299 :
2300 4128767 : for( ulong i=0UL; i<depth-1UL; i++ ) sched->mblk_pool[ i ].next = (uint)(i+1UL);
2301 1007 : sched->mblk_pool[ depth-1UL ].next = UINT_MAX;
2302 1007 : sched->mblk_pool_free_head = 0U;
2303 1007 : sched->mblk_pool_free_cnt = depth;
2304 :
2305 1007 : txn_bitset_new( sched->exec_done_set );
2306 1007 : txn_bitset_new( sched->sigverify_done_set );
2307 1007 : txn_bitset_new( sched->poh_mixin_done_set );
2308 :
2309 1007 : sched->block_pool_popcnt = 0UL;
2310 :
2311 1007 : ref_q_new( _ref_q, block_cnt_max );
2312 :
2313 1007 : return sched;
2314 1007 : }
2315 :
2316 : fd_sched_t *
2317 1008 : fd_sched_join( void * mem ) {
2318 :
2319 1008 : if( FD_UNLIKELY( !mem ) ) {
2320 0 : FD_LOG_WARNING(( "NULL mem" ));
2321 0 : return NULL;
2322 0 : }
2323 :
2324 1008 : fd_sched_t * sched = (fd_sched_t *)mem;
2325 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2326 1008 : ulong depth = sched->depth;
2327 1008 : ulong block_cnt_max = sched->block_cnt_max;
2328 :
2329 1008 : FD_SCRATCH_ALLOC_INIT( l, mem );
2330 1008 : /* */ FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), sizeof(fd_sched_t) );
2331 1008 : void * _rdisp = FD_SCRATCH_ALLOC_APPEND( l, fd_rdisp_align(), fd_rdisp_footprint( depth, block_cnt_max, FD_SCHED_LTHASH_ACCT_EXTRA ) );
2332 1008 : void * _bpool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_block_t), block_cnt_max*sizeof(fd_sched_block_t) );
2333 1008 : void * _ref_q = FD_SCRATCH_ALLOC_APPEND( l, ref_q_align(), ref_q_footprint( block_cnt_max ) );
2334 1008 : /* */ FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txn_p_t), depth*sizeof(fd_txn_p_t) );
2335 1008 : /* */ FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_txn_info_t), depth*sizeof(fd_sched_txn_info_t) );
2336 1008 : /* */ FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_mblk_t), depth*sizeof(fd_sched_mblk_t) );
2337 5040 : for( ulong i=0UL; i<FD_SCHED_MAX_STAGING_LANES; i++ ) {
2338 4032 : void * _pool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sched_lthash_entry_t), FD_RDISP_MAX_WRITERS_PER_BLOCK*sizeof(fd_sched_lthash_entry_t) );
2339 4032 : void * _map = FD_SCRATCH_ALLOC_APPEND( l, lthash_map_align(), lthash_map_footprint( lthash_chain_cnt() ) );
2340 0 : sched->lthash_lane[ i ].entry_pool = (fd_sched_lthash_entry_t *)_pool;
2341 4032 : sched->lthash_lane[ i ].map = lthash_map_join( _map );
2342 4032 : FD_TEST( sched->lthash_lane[ i ].map );
2343 4032 : }
2344 1008 : sched->lthash_spec_pool = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_lthash_value_t), FD_SCHED_LTHASH_SPEC_MAX*sizeof(fd_lthash_value_t) );
2345 1008 : sched->lthash_spec_free = FD_SCRATCH_ALLOC_APPEND( l, alignof(uint), FD_SCHED_LTHASH_SPEC_MAX*sizeof(uint) );
2346 1008 : void * _batch = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), sched_lthash_batch_sz() );
2347 1008 : FD_SCRATCH_ALLOC_FINI( l, fd_sched_align() );
2348 :
2349 1008 : sched->rdisp = fd_rdisp_join( _rdisp );
2350 1008 : sched->ref_q = ref_q_join( _ref_q );
2351 1008 : sched->block_pool = _bpool;
2352 1008 : sched->lthash_inflight = (fd_sched_lthash_batch_t *)_batch;
2353 1008 : sched->lthash_add_batch = sched->lthash_inflight+FD_SCHED_MAX_EXEC_TILE_CNT;
2354 :
2355 130032 : for( ulong i=0; i<block_cnt_max; i++ ) {
2356 129024 : mblk_slist_join( sched->block_pool[ i ].mblks_unhashed );
2357 129024 : mblk_slist_join( sched->block_pool[ i ].mblks_mixin_in_progress );
2358 129024 : }
2359 :
2360 1008 : txn_bitset_join( sched->exec_done_set );
2361 1008 : txn_bitset_join( sched->sigverify_done_set );
2362 1008 : txn_bitset_join( sched->poh_mixin_done_set );
2363 :
2364 1008 : return sched;
2365 1008 : }
2366 :
2367 : int
2368 4557 : fd_sched_fec_can_ingest( fd_sched_t * sched, fd_sched_fec_t * fec ) {
2369 4557 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2370 4557 : FD_TEST( fec->bank_idx<sched->block_cnt_max );
2371 4557 : FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
2372 :
2373 4557 : if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
2374 0 : sched->print_buf_sz = 0UL;
2375 0 : print_metrics( sched );
2376 0 : print_sched( sched );
2377 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2378 0 : FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %u, slot %lu, parent slot %lu", fec->fec->data_sz, fec->slot, fec->parent_slot ));
2379 0 : }
2380 :
2381 4557 : ulong fec_buf_sz = 0UL;
2382 4557 : fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
2383 4557 : if( FD_LIKELY( !fec->is_first_in_block ) ) {
2384 3556 : fec_buf_sz += block->residual_sz;
2385 3556 : } else {
2386 : /* No residual data as this is a fresh new block. */
2387 1001 : }
2388 : /* Addition is safe and won't overflow because we checked the FEC set
2389 : size above. */
2390 4557 : fec_buf_sz += fec->fec->data_sz;
2391 : /* Assuming every transaction is min size, do we have enough free
2392 : entries in the txn pool? For a more precise txn count, we would
2393 : have to do some parsing. */
2394 4557 : return sched->txn_pool_free_cnt>=fec_buf_sz/FD_TXN_MIN_SERIALIZED_SZ && sched->mblk_pool_free_cnt>=fec_buf_sz/sizeof(fd_microblock_hdr_t);
2395 4557 : }
2396 :
2397 : ulong
2398 0 : fd_sched_can_ingest_cnt( fd_sched_t * sched ) {
2399 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2400 : /* Worst case, we need one byte from the incoming data to extract a
2401 : transaction out of the residual data, and the rest of the incoming
2402 : data contributes toward min sized transactions. */
2403 0 : return fd_ulong_min( sched->txn_pool_free_cnt/FD_SCHED_MAX_TXN_PER_FEC, sched->mblk_pool_free_cnt/FD_SCHED_MAX_MBLK_PER_FEC );
2404 0 : }
2405 :
2406 : int
2407 : fd_sched_fec_waits_for_lane( fd_sched_t * sched,
2408 : ulong parent_bank_idx,
2409 0 : int is_first_in_block ) {
2410 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2411 0 : if( FD_LIKELY( !fec_waits_for_lane( sched, parent_bank_idx, is_first_in_block ) ) ) return 0;
2412 0 : sched->metrics->lane_wait_cnt++;
2413 0 : return 1;
2414 0 : }
2415 :
2416 : int
2417 0 : fd_sched_is_drained( fd_sched_t * sched ) {
2418 0 : int nothing_inflight = sched->exec_cnt==(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ]&sched->sigverify_ready_bitset[ 0 ]&sched->poh_ready_bitset[ 0 ]&sched->footer_ready_bitset[ 0 ]&sched->lthash_ready_bitset[ 0 ] );
2419 0 : int nothing_queued = sched->active_bank_idx==ULONG_MAX;
2420 0 : return nothing_inflight && nothing_queued;
2421 0 : }
2422 :
2423 : ulong
2424 : fd_sched_footer_dispatch( fd_sched_t * sched,
2425 : ulong bank_idx,
2426 0 : ulong cnt ) {
2427 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2428 0 : FD_TEST( bank_idx==sched->active_bank_idx );
2429 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
2430 0 : FD_TEST( block->block_end_signaled && !block->block_end_done );
2431 0 : ulong idle = sched->txn_exec_ready_bitset[ 0 ]&sched->sigverify_ready_bitset[ 0 ]&sched->poh_ready_bitset[ 0 ]&sched->footer_ready_bitset[ 0 ]&sched->lthash_ready_bitset[ 0 ];
2432 0 : ulong claimed = 0UL;
2433 0 : for( ; idle && cnt; cnt--, idle=fd_ulong_pop_lsb( idle ) ) {
2434 0 : int t = fd_ulong_find_lsb( idle );
2435 0 : claimed = fd_ulong_set_bit( claimed, t );
2436 0 : sched->tile_to_bank_idx[ t ] = bank_idx;
2437 0 : block->footer_in_flight_cnt++;
2438 0 : }
2439 0 : sched->footer_ready_bitset[ 0 ] &= ~claimed;
2440 0 : return claimed;
2441 0 : }
2442 :
2443 : FD_WARN_UNUSED int
2444 : fd_sched_fec_ingest( fd_sched_t * sched,
2445 4557 : fd_sched_fec_t * fec ) {
2446 4557 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2447 4557 : FD_TEST( fec->bank_idx<sched->block_cnt_max );
2448 4557 : FD_TEST( fec->parent_bank_idx<sched->block_cnt_max );
2449 4557 : FD_TEST( ref_q_empty( sched->ref_q ) );
2450 :
2451 4557 : fd_sched_block_t * block = block_pool_ele( sched, fec->bank_idx );
2452 :
2453 4557 : if( FD_UNLIKELY( fec->fec->data_sz>FD_SCHED_MAX_PAYLOAD_PER_FEC ) ) {
2454 0 : sched->print_buf_sz = 0UL;
2455 0 : print_all( sched, block );
2456 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2457 0 : FD_LOG_CRIT(( "invalid FEC set: fec->data_sz %u, slot %lu, parent slot %lu", fec->fec->data_sz, fec->slot, fec->parent_slot ));
2458 0 : }
2459 :
2460 4557 : sched->metrics->fec_cnt++;
2461 :
2462 4557 : if( FD_UNLIKELY( fec->is_first_in_block ) ) {
2463 : /* This is a new block. */
2464 1001 : add_block( sched, fec->bank_idx, fec->parent_bank_idx );
2465 1001 : block->slot = fec->slot;
2466 1001 : block->parent_slot = fec->parent_slot;
2467 1001 : block->starts_at_update_parent = fec->starts_at_update_parent;
2468 :
2469 1001 : if( FD_UNLIKELY( block->dying ) ) {
2470 : /* The child of a dead block is also dead. We added it to our
2471 : fork tree just so we could track an entire lineage of dead
2472 : children and propagate the dead property to the entire lineage,
2473 : in case there were frags for more than one dead children
2474 : in-flight at the time the parent was abandoned. That being
2475 : said, we shouldn't need to add the dead child to the
2476 : dispatcher. */
2477 0 : sched->metrics->block_added_dead_ood_cnt++;
2478 :
2479 : /* Release the refcnt right away since we don't intend to replay
2480 : it at all. We do this so its bank does not linger and stall
2481 : root advancement until the next root notify. */
2482 0 : subtree_release_refcnt( sched, block );
2483 :
2484 : /* Ignore the FEC set for a dead block. */
2485 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
2486 0 : return 0;
2487 0 : }
2488 :
2489 : /* A block whose parent is staged or done takes a free lane of its
2490 : own. A child of an unstaged stageable parent will be unstaged
2491 : too. This is not only lazy, sparing a promotion of the parent
2492 : right now, but an invariant that keeps a lane shortage from
2493 : deadlocking replay, since a staged block then never has an
2494 : unstaged parent that still has to run. See the lane eviction
2495 : comments in try_activate_block(). */
2496 1001 : fd_sched_block_t * parent_block = block_pool_ele( sched, fec->parent_bank_idx );
2497 1001 : int needs_lane = parent_block->staged || !block_is_stageable( parent_block );
2498 1001 : if( FD_UNLIKELY( needs_lane && sched->staged_bitset==fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) {
2499 : /* Every lane is taken. A demotable block is empty and waiting
2500 : for FEC sets and loses nothing by waiting unstaged, so we give
2501 : its lane to the new block. This is how the replay tile's wait
2502 : for a lane ends when no lane voluntarily frees up. A caller
2503 : that chooses to ingest while there are no demotable lanes will
2504 : get the new block unstaged. */
2505 0 : int lane_idx = pick_idle_lane( sched );
2506 0 : if( FD_LIKELY( lane_idx>=0 ) ) {
2507 0 : demote_lane( sched, lane_idx );
2508 0 : sched->metrics->lane_demoted_cnt++;
2509 0 : }
2510 0 : }
2511 1001 : if( FD_LIKELY( needs_lane && sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) {
2512 : /* A lane is free. */
2513 1001 : int lane_idx = pick_free_lane( sched );
2514 1001 : sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
2515 1001 : sched->staged_head_bank_idx[ lane_idx ] = fec->bank_idx;
2516 1001 : sched->staged_popcnt_wmk = fd_ulong_max( sched->staged_popcnt_wmk, (ulong)fd_ulong_popcnt( sched->staged_bitset ) );
2517 1001 : block->in_rdisp = 1;
2518 1001 : block->staged = 1;
2519 1001 : block->staging_lane = (ulong)lane_idx;
2520 1001 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, block->staging_lane );
2521 1001 : lthash_claim_lane_map( sched, block );
2522 1001 : sched->metrics->block_added_cnt++;
2523 1001 : sched->metrics->block_added_staged_cnt++;
2524 1001 : FD_LOG_DEBUG(( "block %lu:%lu entered lane %lu: add", block->slot, fec->bank_idx, block->staging_lane ));
2525 1001 : } else {
2526 : /* The parent is unstaged, or no lane is free. The block will be
2527 : unstaged until it is promoted. */
2528 0 : block->in_rdisp = 1;
2529 0 : block->staged = 0;
2530 0 : fd_rdisp_add_block( sched->rdisp, fec->bank_idx, FD_RDISP_UNSTAGED );
2531 0 : sched->metrics->block_added_cnt++;
2532 0 : sched->metrics->block_added_unstaged_cnt++;
2533 0 : FD_LOG_DEBUG(( "block %lu:%lu entered lane unstaged: add", block->slot, fec->bank_idx ));
2534 0 : }
2535 1001 : }
2536 :
2537 4557 : block->txn_pool_max_popcnt = fd_ulong_max( block->txn_pool_max_popcnt, sched->depth - sched->txn_pool_free_cnt - 1UL );
2538 4557 : block->mblk_pool_max_popcnt = fd_ulong_max( block->mblk_pool_max_popcnt, sched->depth - sched->mblk_pool_free_cnt );
2539 4557 : block->block_pool_max_popcnt = fd_ulong_max( block->block_pool_max_popcnt, sched->block_pool_popcnt );
2540 :
2541 4557 : if( FD_UNLIKELY( block->dying ) ) {
2542 : /* Ignore the FEC set for a dead block. */
2543 0 : sched->metrics->bytes_dropped_cnt += fec->fec->data_sz;
2544 0 : return 1;
2545 0 : }
2546 :
2547 4557 : if( FD_UNLIKELY( !block->in_rdisp ) ) {
2548 : /* Invariant: block must be in the dispatcher at this point. */
2549 0 : sched->print_buf_sz = 0UL;
2550 0 : print_all( sched, block );
2551 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2552 0 : FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, slot %lu, parent slot %lu",
2553 0 : block->slot, block->parent_slot ));
2554 0 : }
2555 :
2556 4557 : if( FD_UNLIKELY( block->fec_eos ) ) {
2557 : /* This means something is wrong upstream. We're getting more FEC
2558 : sets for a block that has already ended, or so we were told. */
2559 0 : sched->print_buf_sz = 0UL;
2560 0 : print_all( sched, block );
2561 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2562 0 : FD_LOG_CRIT(( "invariant violation: block->fec_eos set but getting more FEC sets, slot %lu, parent slot %lu", fec->slot, fec->parent_slot ));
2563 0 : }
2564 4557 : if( FD_UNLIKELY( block->fec_eob ) ) {
2565 : /* fec_eob is only ever set from is_last_in_batch, and the parser is
2566 : expected to finish a batch in full upon getting the FEC set that
2567 : ends the batch. An incomplete parse is caught eagerly below,
2568 : which marks the block dying and is caught above. Getting here
2569 : means the eager check below regressed. */
2570 0 : sched->print_buf_sz = 0UL;
2571 0 : print_all( sched, block );
2572 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2573 0 : FD_LOG_CRIT(( "invariant violation: block->fec_eob set but getting more FEC sets, slot %lu, parent slot %lu", fec->slot, fec->parent_slot ));
2574 0 : }
2575 4557 : if( FD_UNLIKELY( block->child_idx!=ULONG_MAX ) ) {
2576 : /* This means something is wrong upstream. FEC sets are not being
2577 : delivered in replay order. We got a child block FEC set before
2578 : this block was completely delivered. */
2579 0 : sched->print_buf_sz = 0UL;
2580 0 : print_all( sched, block );
2581 0 : fd_sched_block_t * child_block = block_pool_ele( sched, block->child_idx );
2582 0 : print_block_debug( sched, child_block );
2583 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2584 0 : FD_LOG_CRIT(( "invariant violation: block->child_idx %lu, slot %lu, parent slot %lu", block->child_idx, fec->slot, fec->parent_slot ));
2585 0 : }
2586 :
2587 : /* The parse buffer takes the residual from the previous FEC set
2588 : within the same batch, followed by the new FEC set. Both are
2589 : bounded, so this always fits. */
2590 4557 : FD_TEST( block->residual_sz<=FD_SCHED_MAX_RESIDUAL_SZ );
2591 4557 : fd_memcpy( sched->fec_buf, block->residual, block->residual_sz );
2592 4557 : fd_memcpy( sched->fec_buf+block->residual_sz, fec->data, fec->fec->data_sz );
2593 4557 : sched->fec_buf_sz = block->residual_sz+(uint)fec->fec->data_sz;
2594 4557 : sched->fec_buf_soff = 0U;
2595 4557 : sched->metrics->bytes_ingested_cnt += fec->fec->data_sz;
2596 :
2597 4557 : block->fec_eob = fec->is_last_in_batch;
2598 4557 : block->fec_eos = fec->is_last_in_block;
2599 :
2600 4557 : ulong tracked_shred_cnt = fd_ulong_min( fec->shred_cnt, FD_FEC_SHRED_CNT );
2601 4557 : uint tracked_data_sz = 0U;
2602 150381 : for( ulong i=0UL; i<tracked_shred_cnt; i++ ) tracked_data_sz += fec->fec->shred_sz[ i ];
2603 4557 : if( FD_LIKELY( fec->shred_cnt<=FD_FEC_SHRED_CNT ) ) FD_TEST( tracked_data_sz==fec->fec->data_sz );
2604 0 : else FD_TEST( tracked_data_sz<=fec->fec->data_sz );
2605 :
2606 4557 : uint prev_shred_off = 0U;
2607 150381 : for( ulong i=0; i<fec->shred_cnt; i++ ) {
2608 145824 : FD_TEST( block->shred_cnt<sched->max_shreds_per_block );
2609 145824 : uint shred_off;
2610 145824 : if( FD_LIKELY( i<FD_FEC_SHRED_CNT ) ) {
2611 145824 : shred_off = prev_shred_off + fec->fec->shred_sz[ i ];
2612 145824 : } else if( FD_UNLIKELY( i!=fec->shred_cnt-1UL ) ) {
2613 : /* We don't track shred boundaries after 32 shreds, assume they're
2614 : sized uniformly */
2615 0 : ulong num_overflow_shreds = fec->shred_cnt-FD_FEC_SHRED_CNT;
2616 0 : ulong overflow_idx = i-FD_FEC_SHRED_CNT;
2617 0 : ulong overflow_data_sz = (ulong)fec->fec->data_sz-tracked_data_sz;
2618 0 : shred_off = tracked_data_sz + (uint)(overflow_data_sz / num_overflow_shreds * (overflow_idx + 1UL));
2619 0 : } else {
2620 0 : shred_off = (uint)fec->fec->data_sz;
2621 0 : }
2622 145824 : FD_TEST( shred_off>=prev_shred_off && shred_off-prev_shred_off<=USHORT_MAX );
2623 145824 : if( FD_LIKELY( block->shred_cnt-block->shred_scan_idx<FD_SCHED_SHRED_SZ_WINDOW ) ) {
2624 145824 : block->shred_sz[ block->shred_cnt&(uint)(FD_SCHED_SHRED_SZ_WINDOW-1UL) ] = (ushort)(shred_off-prev_shred_off);
2625 145824 : } else {
2626 0 : block->shred_sz_overflow = 1;
2627 0 : }
2628 145824 : block->shred_cnt++;
2629 145824 : prev_shred_off = shred_off;
2630 145824 : }
2631 :
2632 4557 : block->fec_completed_ns = fec->completed_ns;
2633 :
2634 4557 : int err = fd_sched_parse( sched, block, fec->alut_ctx );
2635 :
2636 4557 : if( FD_UNLIKELY( err!=FD_SCHED_DEAD_REASON_NONE ) ) {
2637 0 : handle_bad_block( sched, block, err );
2638 0 : sched->metrics->bytes_dropped_cnt += sched->fec_buf_sz-sched->fec_buf_soff;
2639 0 : return 0;
2640 0 : }
2641 :
2642 4557 : if( FD_UNLIKELY( (fec->is_last_in_batch||fec->is_last_in_block) && (block->txns_rem||block->mblks_rem||block->fec_eob) ) ) {
2643 : /* A malformed block that fails to parse out exactly as many
2644 : transactions and microblocks as it should.
2645 :
2646 : Upon getting a last-in-batch FEC, everything in the ongoing batch
2647 : should completely parse, and the eob flag should be reset. There
2648 : should be no go around for a last-in-batch FEC. */
2649 2 : FD_LOG_INFO(( "bad block: SHORT_BLOCK, bytes_rem %u, txns_rem %lu, mblks_rem %lu, fec_eob %d, slot %lu, parent slot %lu", sched->fec_buf_sz-sched->fec_buf_soff, block->txns_rem, block->mblks_rem, block->fec_eob, block->slot, block->parent_slot ));
2650 2 : handle_bad_block( sched, block, FD_SCHED_DEAD_REASON_SHORT_BLOCK );
2651 2 : return 0;
2652 2 : }
2653 :
2654 4555 : if( FD_UNLIKELY( block->fec_eos && !block->last_mblk_is_tick ) ) {
2655 : /* The last microblock should be a tick.
2656 :
2657 : Note that this early parse-time detection could cause us to throw
2658 : a slightly different error from Agave, in the case that there are
2659 : too few ticks, since the tick count check precedes the trailing
2660 : entry check in Agave. That being said, ultimately a
2661 : TRAILING_ENTRY renders a block invalid, regardless of anything
2662 : else. */
2663 0 : FD_LOG_INFO(( "bad block: TRAILING_ENTRY, slot %lu, parent slot %lu, mblk_cnt %u", block->slot, block->parent_slot, block->mblk_cnt ));
2664 0 : handle_bad_block( sched, block, FD_SCHED_DEAD_REASON_TRAILING_ENTRY );
2665 0 : return 0;
2666 0 : }
2667 :
2668 4555 : if( FD_UNLIKELY( block->fec_eos ) ) {
2669 992 : int dead_reason = on_final( block );
2670 992 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE ) ) {
2671 0 : handle_bad_block( sched, block, dead_reason );
2672 0 : return 0;
2673 0 : }
2674 992 : }
2675 :
2676 : /* Park the residual until the next FEC set. The parser stops only on
2677 : a parseable unit it has not fully received. A parseable unit
2678 : includes a transaction, a block marker, or a microblock or batch
2679 : header. The parser drops the trailing bytes of a finished batch.
2680 : So a residual larger than any of those parseable units is malformed
2681 : content. Instead of crashing, we consider the block invalid. */
2682 4555 : uint residual_sz = sched->fec_buf_sz-sched->fec_buf_soff;
2683 4555 : if( FD_UNLIKELY( residual_sz>FD_SCHED_MAX_RESIDUAL_SZ ) ) {
2684 0 : FD_LOG_INFO(( "bad block: UNPARSEABLE_CONTENT, residual_sz %u, slot %lu, parent slot %lu", residual_sz, fec->slot, fec->parent_slot ));
2685 0 : handle_bad_block( sched, block, FD_SCHED_DEAD_REASON_UNPARSEABLE_CONTENT );
2686 0 : sched->metrics->bytes_dropped_cnt += residual_sz;
2687 0 : return 0;
2688 0 : }
2689 4555 : fd_memcpy( block->residual, sched->fec_buf+sched->fec_buf_soff, residual_sz );
2690 4555 : block->fec_buf_boff += sched->fec_buf_soff;
2691 4555 : block->residual_sz = residual_sz;
2692 : /* Pass the shreds the next transaction cannot start in, so that few
2693 : shred sizes stay outstanding between FEC sets. */
2694 4555 : shred_split( block, block->fec_buf_boff );
2695 :
2696 : /* We just received a FEC set, which may have made all transactions in
2697 : a partially parsed microblock available. If this were a malformed
2698 : block that ends in a non-tick microblock, there's not going to be a
2699 : hashing task from the missing ending tick to drain the mixin queue.
2700 : So we try to drain the mixin queue right here. Another option is
2701 : to drain it at dispatch time, when we are about to dispatch the end
2702 : of block signal, right before the check for whether block should
2703 : end. */
2704 4555 : int mixin_res;
2705 4555 : while( (mixin_res=maybe_mixin( sched, block )) ) {
2706 0 : if( FD_UNLIKELY( mixin_res==-1 ) ) {
2707 0 : handle_bad_block( sched, block, FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH_INGEST );
2708 0 : return 0;
2709 0 : }
2710 0 : FD_TEST( mixin_res==1||mixin_res==2 );
2711 0 : }
2712 :
2713 : /* Every account the block intends to write has now been observed.
2714 : Each gets a pseudo transaction for its LtHash addition, created by
2715 : polls that find nothing to dispatch. */
2716 4555 : if( FD_UNLIKELY( fec->is_last_in_block ) ) lthash_drain_later( sched, block );
2717 :
2718 4555 : slot_state_eval( sched, block );
2719 :
2720 : /* Check if we need to set the active block. */
2721 4555 : check_or_set_active_block( sched );
2722 :
2723 4555 : return 1;
2724 4555 : }
2725 :
2726 : /* A cert holds a tile for about a millisecond. So footer cert verify
2727 : tasks follow the LtHash batch policy. They go out only when no
2728 : transaction is ready, leave the hash batch reserve idle, and are
2729 : offered from the footer's parse until BLOCK_END. Like a hash batch
2730 : it takes the highest idle tile. Returns 1 if a task was written to
2731 : out, 0 otherwise. */
2732 : static int
2733 : cert_dispatch( fd_sched_t * sched,
2734 : fd_sched_block_t * block,
2735 : ulong bank_idx,
2736 : ulong ready_bitset,
2737 : int reserve,
2738 0 : fd_sched_task_t * out ) {
2739 0 : if( FD_LIKELY( !block->footer_seen || block->cert_next>=block->cert_cnt || block->block_end_signaled ) ) return 0;
2740 0 : if( FD_UNLIKELY( (ulong)fd_ulong_popcnt( ready_bitset )<=(ulong)reserve ) ) return 0;
2741 0 : int t = fd_ulong_find_msb( ready_bitset );
2742 0 : sched->footer_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->footer_ready_bitset[ 0 ], t );
2743 0 : sched->tile_to_bank_idx[ t ] = bank_idx;
2744 0 : block->footer_in_flight_cnt++;
2745 0 : out->task_type = FD_SCHED_TT_CERT_VERIFY;
2746 0 : out->cert_verify->bank_idx = bank_idx;
2747 0 : out->cert_verify->cert_idx = (ulong)block->cert_next++;
2748 0 : out->cert_verify->exec_idx = (ulong)t;
2749 0 : return 1;
2750 0 : }
2751 :
2752 : /* If may_drain is set, allow one drain chunk once nothing else can be
2753 : dispatched. The re-entry the chunk earns passes 0, so a poll drains
2754 : at most once. */
2755 : static ulong
2756 0 : task_next_ready( fd_sched_t * sched, fd_sched_task_t * out, int may_drain ) {
2757 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
2758 0 : FD_TEST( ref_q_empty( sched->ref_q ) );
2759 :
2760 0 : ulong exec_ready_bitset0 = sched->txn_exec_ready_bitset[ 0 ];
2761 0 : ulong exec_fully_ready_bitset = sched->sigverify_ready_bitset[ 0 ] & sched->poh_ready_bitset[ 0 ] & sched->footer_ready_bitset[ 0 ] & sched->lthash_ready_bitset[ 0 ] & exec_ready_bitset0;
2762 0 : if( FD_UNLIKELY( !exec_fully_ready_bitset ) ) {
2763 : /* Early exit if no exec tiles available. Also let's make some
2764 : progress on a pending drain. */
2765 0 : lthash_drain_chunk( sched );
2766 0 : return 0UL;
2767 0 : }
2768 :
2769 0 : if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
2770 : /* No need to try activating a block. If we're in this state,
2771 : there's truly nothing to execute. We will activate something
2772 : when we ingest a FEC set with transactions. */
2773 0 : return 0UL;
2774 0 : }
2775 :
2776 0 : out->task_type = FD_SCHED_TT_NULL;
2777 :
2778 : /* We could in theory reevaluate staging lane allocation here and do
2779 : promotion/demotion as needed. It's a policy decision to minimize
2780 : fork churn for now and just execute down the same active fork. */
2781 :
2782 0 : ulong bank_idx = sched->active_bank_idx;
2783 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
2784 0 : if( FD_UNLIKELY( block_should_deactivate( sched, block ) ) ) {
2785 0 : sched->print_buf_sz = 0UL;
2786 0 : print_all( sched, block );
2787 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2788 0 : FD_LOG_CRIT(( "invariant violation: active block %lu:%lu is not activatable nor has anything in-flight", block->slot, sched->active_bank_idx ));
2789 0 : }
2790 :
2791 0 : block->txn_pool_max_popcnt = fd_ulong_max( block->txn_pool_max_popcnt, sched->depth - sched->txn_pool_free_cnt - 1UL );
2792 0 : block->mblk_pool_max_popcnt = fd_ulong_max( block->mblk_pool_max_popcnt, sched->depth - sched->mblk_pool_free_cnt );
2793 0 : block->block_pool_max_popcnt = fd_ulong_max( block->block_pool_max_popcnt, sched->block_pool_popcnt );
2794 :
2795 0 : if( FD_UNLIKELY( !block->block_start_signaled ) ) {
2796 0 : if( !block->lthash_mode_picked ) lthash_pick_mode( sched, block );
2797 0 : out->task_type = FD_SCHED_TT_BLOCK_START;
2798 0 : out->block_start->bank_idx = bank_idx;
2799 0 : out->block_start->parent_bank_idx = block->parent_idx;
2800 0 : out->block_start->slot = block->slot;
2801 0 : out->block_start->lthash_inband = block->lthash_inband;
2802 0 : block->block_start_signaled = 1;
2803 0 : slot_state_open( sched, block );
2804 0 : sched->next_ready_last_tick = fd_tickcount();
2805 0 : sched->next_ready_last_bank_idx = bank_idx;
2806 0 : return 1UL;
2807 0 : }
2808 :
2809 : /* Transactions, PoH and sigverify take the lowest idle tile. LtHash
2810 : batches take the highest. */
2811 0 : ulong exec_tile_idx0 = fd_ulong_if( !!exec_fully_ready_bitset, (ulong)fd_ulong_find_lsb( exec_fully_ready_bitset ), ULONG_MAX );
2812 0 : ulong exec_tile_idx1 = fd_ulong_if( !!exec_fully_ready_bitset, (ulong)fd_ulong_find_msb( exec_fully_ready_bitset ), ULONG_MAX );
2813 0 : ulong idle_cnt = (ulong)fd_ulong_popcnt( exec_fully_ready_bitset );
2814 :
2815 : /* Every task but a transaction leaves reserve tiles idle, a hash
2816 : batch lthash_reserve. A hash batch carries at most lthash_cap
2817 : accounts. */
2818 0 : int reserve = idle_reserve( sched, block );
2819 0 : int lthash_reserve = lthash_idle_reserve( sched );
2820 0 : ulong lthash_cap = fd_ulong_if( block->txn_exec_in_flight_cnt>0U, FD_SCHED_LTHASH_BATCH_BUSY_MAX, FD_SCHED_LTHASH_BATCH_MAX );
2821 :
2822 0 : ulong exec_queued_cnt = block->txn_parsed_cnt-block->txn_exec_in_flight_cnt-block->txn_exec_done_cnt;
2823 0 : int add_unsurfaced = block->add_created_cnt>block->add_surfaced_cnt; /* Addition pseudo transactions are queued in rdisp along with real transactions. */
2824 0 : int add_held = sched->lthash_add_batch->cnt>0UL;
2825 0 : ulong txn_idx = 0UL;
2826 0 : if( FD_LIKELY( (exec_queued_cnt>0UL || add_unsurfaced || add_held) && idle_cnt ) ) { /* Optimize for no fork switching. */
2827 : /* Transaction execution has the highest priority. Current mainnet
2828 : block times are very much dominated by critical path transaction
2829 : execution. To achieve the fastest block replay speed, we can't
2830 : afford to make any mistake in critical path dispatching. Any
2831 : deviation from perfect critical path dispatching is basically
2832 : irrecoverable. As such, we try to keep all the exec tiles busy
2833 : with transaction execution, but we allow at most one transaction
2834 : to be in-flight per exec tile. This is to ensure that whenever a
2835 : critical path transaction completes, we have at least one exec
2836 : tile, e.g. the one that just completed said transaction, readily
2837 : available to continue executing down the critical path. */
2838 0 : ulong appended_cnt = 0UL; /* Additions this poll batched. */
2839 0 : txn_idx = fd_rdisp_get_next_ready( sched->rdisp, bank_idx );
2840 0 : while( FD_UNLIKELY( txn_idx & FD_RDISP_LTHASH_PSEUDO_TXN ) ) {
2841 : /* A pseudo transaction surfaced. By rdisp's scoring policy we
2842 : know that this implies no real transaction is ready. */
2843 0 : block->state_conflict = (uchar)( exec_queued_cnt>0UL );
2844 0 : fd_sched_lthash_entry_t * e = lthash_surface( sched, block, bank_idx, txn_idx );
2845 0 : if( FD_LIKELY( e ) ) {
2846 : /* ... and the addition is not stale. */
2847 0 : fd_sched_lthash_batch_t * batch = sched->lthash_add_batch;
2848 0 : if( FD_UNLIKELY( e->spec_idx!=UINT_MAX ) ) {
2849 : /* This is a speculative addition that waited behind a
2850 : serializing transaction. The speculation goes out under
2851 : the same idleness policy as lthash_speculate(), and could
2852 : be given up rather than forced onto the last idle tile.
2853 : Once the block is complete it is given up outright. Giving
2854 : up creates an addition sequenced behind every real
2855 : transaction, as desired. If we did a speculation here,
2856 : there's a small chance it might be undone. */
2857 0 : if( FD_LIKELY( !block->fec_eos && idle_cnt>(ulong)reserve ) ) {
2858 0 : lthash_dispatch_one_add( sched, block, bank_idx, e, txn_idx, (int)exec_tile_idx1, out );
2859 0 : sched->metrics->lthash_add_batch_held_cnt += (ulong)(batch->cnt>0UL);
2860 0 : sched->next_ready_last_tick = fd_tickcount();
2861 0 : sched->next_ready_last_bank_idx = bank_idx;
2862 0 : return 1UL;
2863 0 : }
2864 0 : lthash_spec_give_up( sched, block, bank_idx, e, txn_idx );
2865 0 : } else {
2866 0 : if( FD_UNLIKELY( !batch->cnt ) ) lthash_batch_init( block, batch, bank_idx, FD_SCHED_TT_LTHASH_ADD );
2867 0 : lthash_batch_append( sched, batch, e, txn_idx );
2868 0 : appended_cnt++;
2869 0 : if( FD_UNLIKELY( batch->cnt==FD_SCHED_LTHASH_BATCH_MAX ) ) {
2870 : /* A full batch goes out even if we're down to the last idle
2871 : tile, since the next poll could give us an addition and
2872 : we'll have no space to batch it. */
2873 0 : lthash_dispatch_held_batch( sched, block, bank_idx, (int)exec_tile_idx1, out );
2874 0 : sched->next_ready_last_tick = fd_tickcount();
2875 0 : sched->next_ready_last_bank_idx = bank_idx;
2876 0 : return 1UL;
2877 0 : }
2878 0 : if( FD_UNLIKELY( batch->cnt>=lthash_cap ) ) {
2879 : /* The batch is big enough to go out. The addition just
2880 : appended implies no transaction is ready, otherwise rdisp
2881 : would have returned the transaction instead of the
2882 : addition. So we're not leaving critical path work on the
2883 : table. */
2884 0 : txn_idx = 0UL;
2885 0 : break;
2886 0 : }
2887 0 : }
2888 0 : }
2889 0 : txn_idx = fd_rdisp_get_next_ready( sched->rdisp, bank_idx );
2890 0 : }
2891 0 : block->state_conflict = (uchar)( !txn_idx && exec_queued_cnt>0UL );
2892 0 : if( FD_UNLIKELY( sched->lthash_add_batch->cnt ) ) {
2893 0 : if( FD_LIKELY( !txn_idx ) ) {
2894 : /* Nothing is ready behind the batch, or the batch is at its
2895 : cap. */
2896 0 : if( FD_LIKELY( idle_cnt>(ulong)lthash_reserve ) ) {
2897 0 : lthash_dispatch_held_batch( sched, block, bank_idx, (int)exec_tile_idx1, out );
2898 0 : sched->next_ready_last_tick = fd_tickcount();
2899 0 : sched->next_ready_last_bank_idx = bank_idx;
2900 0 : return 1UL;
2901 0 : }
2902 : /* Too few idle tiles for a hash batch. The batch stays held
2903 : and goes out from a later call, once a poll finds nothing
2904 : ready and enough tiles are idle. */
2905 0 : sched->metrics->lthash_add_batch_wait_idle_cnt++;
2906 0 : } else if( appended_cnt ) {
2907 : /* A real transaction surfaced after we've batched some
2908 : additions in this poll. */
2909 0 : sched->metrics->lthash_add_batch_held_cnt++;
2910 0 : }
2911 0 : }
2912 0 : if( FD_UNLIKELY( txn_idx==0UL && exec_queued_cnt>0UL ) ) {
2913 : /* There are transactions queued but none ready for execution.
2914 : This implies that there must be in-flight transactions on whose
2915 : completion the queued transactions depend. A queued
2916 : transaction may also be waiting on a dispatched speculative
2917 : addition. */
2918 0 : if( FD_UNLIKELY( !block->txn_exec_in_flight_cnt && block->add_surfaced_cnt==block->add_done_cnt ) ) {
2919 0 : sched->print_buf_sz = 0UL;
2920 0 : print_all( sched, block );
2921 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
2922 0 : FD_LOG_CRIT(( "invariant violation: no ready transaction found but block->txn_exec_in_flight_cnt==0" ));
2923 0 : }
2924 :
2925 : /* Next up are PoH tasks. Same dispatching policy as sigverify
2926 : tasks. */
2927 0 : ulong poh_ready_bitset = exec_fully_ready_bitset;
2928 0 : ulong poh_hashing_queued_cnt = block->mblk_cnt-block->poh_mblk_in_flight_cnt-block->poh_hashing_done_cnt;
2929 0 : int poh_threshold = reserve;
2930 0 : if( FD_LIKELY( poh_hashing_queued_cnt>0UL && fd_ulong_popcnt( poh_ready_bitset )>poh_threshold ) ) {
2931 0 : dispatch_poh( sched, block, bank_idx, fd_ulong_find_lsb( poh_ready_bitset ), out );
2932 0 : sched->next_ready_last_tick = fd_tickcount();
2933 0 : sched->next_ready_last_bank_idx = bank_idx;
2934 0 : return 1UL;
2935 0 : }
2936 :
2937 : /* Dispatch more sigverify tasks to completely idle tiles over the
2938 : reserve, at most one in flight per tile. */
2939 0 : ulong sigverify_ready_bitset = exec_fully_ready_bitset;
2940 0 : ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
2941 0 : if( FD_LIKELY( sigverify_queued_cnt>0UL && fd_ulong_popcnt( sigverify_ready_bitset )>reserve ) ) {
2942 0 : if( FD_LIKELY( dispatch_sigverify( sched, block, bank_idx, fd_ulong_find_lsb( sigverify_ready_bitset ), out ) ) ) {
2943 0 : sched->next_ready_last_tick = fd_tickcount();
2944 0 : sched->next_ready_last_bank_idx = bank_idx;
2945 0 : return 1UL;
2946 0 : }
2947 0 : }
2948 :
2949 : /* Then footer certs and LtHash subtractions, under the hash batch
2950 : reserve. */
2951 0 : if( FD_UNLIKELY( cert_dispatch( sched, block, bank_idx, exec_fully_ready_bitset, lthash_reserve, out ) ) ) {
2952 0 : sched->next_ready_last_tick = fd_tickcount();
2953 0 : sched->next_ready_last_bank_idx = bank_idx;
2954 0 : return 1UL;
2955 0 : }
2956 0 : if( FD_UNLIKELY( lthash_dispatch_sub( sched, block, bank_idx, exec_fully_ready_bitset, lthash_reserve, lthash_cap, out ) ) ) {
2957 0 : sched->next_ready_last_tick = fd_tickcount();
2958 0 : sched->next_ready_last_bank_idx = bank_idx;
2959 0 : return 1UL;
2960 0 : }
2961 : /* Nothing the block must do could go out, so let's make some
2962 : progress on a pending drain. A pseudo transaction the drain
2963 : created may be ready, so poll once more. */
2964 0 : if( may_drain && lthash_drain_chunk( sched ) ) return task_next_ready( sched, out, 0 );
2965 :
2966 : /* Now try to dispatch a speculative addition, the same way we do
2967 : in the branch where the transaction queue is empty. Every
2968 : account we select as a speculation candidate has seen its last
2969 : parsed writer finish, so the speculation doesn't block any
2970 : queued transactions. While having lthash additions in flight
2971 : could in theory slightly delay transaction dispatch when the
2972 : blocker transaction returns, the delay should be minimal. FECs
2973 : also tend to end on a long serial transaction path, so there's
2974 : typically not a wide DAG phase being starved by lthash
2975 : additions. More often, not allowing lthash speculations to go
2976 : out in this branch causes them to be clustered at the end of a
2977 : mid-slot FEC, after the last transaction of the FEC has been
2978 : dispatched. This has a good chance to lead to lthash
2979 : speculations finishing well after the last transaction of the
2980 : FEC has returned, which is not ideal. */
2981 0 : if( FD_LIKELY( block_may_speculate( sched, block, 1UL ) ) ) {
2982 0 : int spec = lthash_speculate( sched, block, bank_idx, exec_fully_ready_bitset, out );
2983 0 : if( FD_UNLIKELY( spec>=0 ) ) {
2984 0 : if( spec ) {
2985 0 : sched->next_ready_last_tick = fd_tickcount();
2986 0 : sched->next_ready_last_bank_idx = bank_idx;
2987 0 : }
2988 0 : return (ulong)spec;
2989 0 : }
2990 0 : }
2991 0 : return 0UL;
2992 0 : }
2993 0 : }
2994 0 : if( FD_LIKELY( txn_idx ) ) {
2995 0 : out->task_type = FD_SCHED_TT_TXN_EXEC;
2996 0 : out->txn_exec->txn_idx = txn_idx;
2997 0 : out->txn_exec->bank_idx = bank_idx;
2998 0 : out->txn_exec->slot = block->slot;
2999 0 : out->txn_exec->exec_idx = exec_tile_idx0;
3000 0 : FD_TEST( out->txn_exec->exec_idx!=ULONG_MAX );
3001 :
3002 0 : long now = fd_tickcount();
3003 0 : ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
3004 0 : ulong txn_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( exec_ready_bitset0 );
3005 0 : sched->metrics->txn_none_in_flight_tickcount += fd_ulong_if( txn_exec_busy_cnt==0UL, delta, 0UL );
3006 0 : sched->metrics->txn_weighted_in_flight_tickcount += fd_ulong_if( txn_exec_busy_cnt!=0UL, delta, 0UL );
3007 0 : sched->metrics->txn_weighted_in_flight_cnt += delta*txn_exec_busy_cnt;
3008 0 : sched->txn_in_flight_last_tick = now;
3009 :
3010 0 : sched->txn_info_pool[ out->txn_exec->txn_idx ].tick_exec_disp = now;
3011 0 : sched->txn_info_pool[ out->txn_exec->txn_idx ].exec_tile_idx = exec_tile_idx0;
3012 :
3013 0 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_clear_bit( exec_ready_bitset0, (int)exec_tile_idx0);
3014 0 : sched->tile_to_bank_idx[ exec_tile_idx0 ] = bank_idx;
3015 :
3016 0 : block->txn_exec_in_flight_cnt++;
3017 0 : sched->metrics->txn_max_in_flight_cnt = fd_uint_max( sched->metrics->txn_max_in_flight_cnt, block->txn_exec_in_flight_cnt );
3018 :
3019 0 : exec_busy_check( sched, block );
3020 0 : sched->next_ready_last_tick = now;
3021 0 : sched->next_ready_last_bank_idx = bank_idx;
3022 0 : return 1UL;
3023 0 : }
3024 :
3025 : /* At this point txn_queued_cnt==0. Any unsurfaced addition waits on
3026 : an in-flight writer or a serializing transaction. */
3027 :
3028 : /* Next up are PoH tasks. Same dispatching policy as sigverify. */
3029 0 : ulong poh_ready_bitset = exec_fully_ready_bitset;
3030 0 : ulong poh_hashing_queued_cnt = block->mblk_cnt-block->poh_mblk_in_flight_cnt-block->poh_hashing_done_cnt;
3031 0 : int poh_threshold = reserve;
3032 0 : if( FD_LIKELY( poh_hashing_queued_cnt>0UL && fd_ulong_popcnt( poh_ready_bitset )>poh_threshold ) ) {
3033 0 : dispatch_poh( sched, block, bank_idx, fd_ulong_find_lsb( poh_ready_bitset ), out );
3034 0 : sched->next_ready_last_tick = fd_tickcount();
3035 0 : sched->next_ready_last_bank_idx = bank_idx;
3036 0 : return 1UL;
3037 0 : }
3038 :
3039 : /* Try to dispatch a sigverify task to a qualified idle tile. */
3040 0 : ulong sigverify_ready_bitset = exec_fully_ready_bitset;
3041 0 : ulong sigverify_queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
3042 0 : if( FD_LIKELY( sigverify_queued_cnt>0UL && fd_ulong_popcnt( sigverify_ready_bitset )>reserve ) ) {
3043 0 : if( FD_LIKELY( dispatch_sigverify( sched, block, bank_idx, fd_ulong_find_lsb( sigverify_ready_bitset ), out ) ) ) {
3044 0 : sched->next_ready_last_tick = fd_tickcount();
3045 0 : sched->next_ready_last_bank_idx = bank_idx;
3046 0 : return 1UL;
3047 0 : }
3048 0 : }
3049 :
3050 : /* Then footer certs and LtHash subtractions, under the hash batch
3051 : reserve. */
3052 0 : if( FD_UNLIKELY( cert_dispatch( sched, block, bank_idx, exec_fully_ready_bitset, lthash_reserve, out ) ) ) {
3053 0 : sched->next_ready_last_tick = fd_tickcount();
3054 0 : sched->next_ready_last_bank_idx = bank_idx;
3055 0 : return 1UL;
3056 0 : }
3057 0 : if( FD_UNLIKELY( lthash_dispatch_sub( sched, block, bank_idx, exec_fully_ready_bitset, lthash_reserve, lthash_cap, out ) ) ) {
3058 0 : sched->next_ready_last_tick = fd_tickcount();
3059 0 : sched->next_ready_last_bank_idx = bank_idx;
3060 0 : return 1UL;
3061 0 : }
3062 : /* Same as above, make some progress on a pending drain and poll once
3063 : more for what the chunk created. */
3064 0 : if( may_drain && lthash_drain_chunk( sched ) ) return task_next_ready( sched, out, 0 );
3065 :
3066 : /* Absolutely nothing else left to dispatch. If the block is still
3067 : receiving FEC sets and owes additions, we will spend the idle tiles
3068 : on speculative additions. Speculation is the lowest priority work
3069 : there is, though. So if nothing of the block is in flight and
3070 : another lane head is ready with must-do work, yield. */
3071 0 : int may_speculate = block_may_speculate( sched, block, 1UL );
3072 0 : if( FD_LIKELY( may_speculate ) ) {
3073 0 : if( FD_UNLIKELY( !block_is_in_flight( block ) && other_lane_has_real_work( sched, block ) ) ) {
3074 0 : FD_LOG_DEBUG(( "reset active_bank_idx %lu: speculation yields", sched->active_bank_idx ));
3075 0 : sched->last_active_bank_idx = sched->active_bank_idx;
3076 0 : sched->active_bank_idx = ULONG_MAX;
3077 0 : sched->metrics->lthash_spec_yield_cnt++;
3078 0 : try_activate_block( sched );
3079 0 : return 0UL;
3080 0 : }
3081 0 : int spec = lthash_speculate( sched, block, bank_idx, exec_fully_ready_bitset, out );
3082 0 : if( FD_UNLIKELY( spec>=0 ) ) {
3083 0 : if( spec ) {
3084 0 : sched->next_ready_last_tick = fd_tickcount();
3085 0 : sched->next_ready_last_bank_idx = bank_idx;
3086 0 : }
3087 0 : return (ulong)spec;
3088 0 : }
3089 0 : }
3090 :
3091 : /* A complete block may still have additions to hand to the
3092 : dispatcher, which we do a chunk at a time. So there could be
3093 : moments when every task we handed out has come back, nothing is
3094 : running, and yet the block cannot end, because we still owe
3095 : additions. Another poll will surface them. Return empty handed,
3096 : since we know the block cannot end yet, and since the liveness
3097 : check at the end of this function would otherwise mistake a block
3098 : with unsurfaced additions for being stuck and abort. */
3099 0 : if( FD_UNLIKELY( block->lthash_draining || block->add_created_cnt>block->add_surfaced_cnt ) ) return 0UL;
3100 :
3101 0 : if( FD_UNLIKELY( block_should_signal_end( block ) ) ) {
3102 0 : FD_TEST( block->block_start_signaled );
3103 0 : if( FD_UNLIKELY( block->add_owed_cnt ) ) {
3104 0 : sched->print_buf_sz = 0UL;
3105 0 : print_all( sched, block );
3106 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
3107 0 : FD_LOG_CRIT(( "invariant violation: block %lu:%lu ends owing %u additions", block->slot, bank_idx, block->add_owed_cnt ));
3108 0 : }
3109 0 : int tick_reason = verify_ticks_final( block );
3110 0 : if( FD_UNLIKELY( tick_reason!=FD_SCHED_DEAD_REASON_NONE ) ) {
3111 : /* Tick verification can't be done at parse time (except for
3112 : TRAILING_ENTRY), because we may not know the expected number of
3113 : hashes yet. It can't be driven by transaction dispatch or
3114 : completion, because the block may be empty. Similarly, it can't
3115 : be driven by PoH hashing, because a bad block may simply not
3116 : have any microblocks. */
3117 0 : handle_bad_block( sched, block, tick_reason );
3118 0 : out->task_type = FD_SCHED_TT_MARK_DEAD;
3119 0 : out->mark_dead->bank_idx = bank_idx;
3120 0 : sched->next_ready_last_tick = fd_tickcount();
3121 0 : sched->next_ready_last_bank_idx = bank_idx;
3122 0 : return 1UL;
3123 0 : }
3124 0 : out->task_type = FD_SCHED_TT_BLOCK_END;
3125 0 : out->block_end->bank_idx = bank_idx;
3126 0 : block->block_end_signaled = 1;
3127 0 : FD_TEST( block->refcnt );
3128 0 : block->refcnt = 0;
3129 0 : FD_TEST( ref_q_avail( sched->ref_q ) );
3130 0 : ref_q_push_tail( sched->ref_q, bank_idx );
3131 0 : sched->next_ready_last_tick = fd_tickcount();
3132 0 : sched->next_ready_last_bank_idx = bank_idx;
3133 0 : return 1UL;
3134 0 : }
3135 :
3136 : /* Nothing queued for the active block. If we haven't received all
3137 : the FEC sets for it, then return and wait for more FEC sets, while
3138 : there are in-flight transactions. This is a policy decision to
3139 : minimize fork churn and allow for executing down the current fork
3140 : as much as we can. If we have received all the FEC sets for it,
3141 : then we'd still like to return and wait for the in-flight
3142 : transactions to retire, before switching to a different block.
3143 :
3144 : Either way, there should be in-flight transactions. We deactivate
3145 : the active block the moment we exhausted transactions from it.
3146 :
3147 : We don't assert block_is_in_flight() here because there might be
3148 : in-flight tasks from dying blocks that are preventing us from
3149 : dispatching anything momentarily. So we assert the global exec
3150 : tile busy count. This doesn't lose too much assertion coverage
3151 : since dying blocks are few and far between. */
3152 0 : ulong total_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( exec_fully_ready_bitset );
3153 0 : if( FD_UNLIKELY( !total_exec_busy_cnt ) ) {
3154 0 : sched->print_buf_sz = 0UL;
3155 0 : print_all( sched, block );
3156 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
3157 0 : FD_LOG_CRIT(( "invariant violation: expected in-flight tasks but none found" ));
3158 0 : }
3159 :
3160 0 : return 0UL;
3161 0 : }
3162 :
3163 : ulong
3164 0 : fd_sched_task_next_ready( fd_sched_t * sched, fd_sched_task_t * out ) {
3165 0 : ulong bank_idx = sched->active_bank_idx;
3166 0 : ulong ret = task_next_ready( sched, out, 1 );
3167 0 : if( FD_LIKELY( bank_idx!=ULONG_MAX ) ) slot_state_eval( sched, block_pool_ele( sched, bank_idx ) );
3168 0 : return ret;
3169 0 : }
3170 :
3171 : int
3172 0 : fd_sched_task_done( fd_sched_t * sched, ulong task_type, ulong txn_idx, ulong exec_idx, void * data ) {
3173 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3174 :
3175 0 : ulong bank_idx = ULONG_MAX;
3176 0 : switch( task_type ) {
3177 0 : case FD_SCHED_TT_BLOCK_START:
3178 0 : case FD_SCHED_TT_BLOCK_END: {
3179 0 : (void)txn_idx;
3180 0 : (void)data;
3181 0 : bank_idx = sched->active_bank_idx;
3182 0 : break;
3183 0 : }
3184 0 : case FD_SCHED_TT_TXN_EXEC: {
3185 0 : (void)data;
3186 0 : FD_TEST( txn_idx < sched->depth );
3187 0 : bank_idx = sched->tile_to_bank_idx[ exec_idx ];
3188 0 : break;
3189 0 : }
3190 0 : case FD_SCHED_TT_TXN_SIGVERIFY:
3191 0 : case FD_SCHED_TT_POH_HASH:
3192 0 : case FD_SCHED_TT_CERT_VERIFY:
3193 0 : case FD_SCHED_TT_REWARDS: {
3194 0 : (void)txn_idx;
3195 0 : bank_idx = sched->tile_to_bank_idx[ exec_idx ];
3196 0 : break;
3197 0 : }
3198 0 : case FD_SCHED_TT_LTHASH_SUB:
3199 0 : case FD_SCHED_TT_LTHASH_ADD: {
3200 : /* Like any other tile task, an LtHash task keeps its block in the
3201 : scheduler and the dispatcher until it lands. block_is_in_flight
3202 : counts it, which gates abandon, demotion and pruning, and
3203 : BLOCK_END waits on it, so the block checks below hold for a
3204 : result of either kind. */
3205 0 : FD_TEST( exec_idx<sched->exec_cnt );
3206 0 : FD_TEST( data );
3207 0 : bank_idx = sched->tile_to_bank_idx[ exec_idx ];
3208 0 : break;
3209 0 : }
3210 0 : default: FD_LOG_CRIT(( "unsupported task_type %lu", task_type ));
3211 0 : }
3212 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3213 :
3214 0 : if( FD_UNLIKELY( !block->in_sched ) ) {
3215 0 : FD_LOG_CRIT(( "invariant violation: block->in_sched==0, block %lu:%lu, parent slot %lu",
3216 0 : block->slot, bank_idx, block->parent_slot ));
3217 0 : }
3218 0 : if( FD_UNLIKELY( !block->staged ) ) {
3219 : /* Invariant: only staged blocks can have in-flight transactions. */
3220 0 : FD_LOG_CRIT(( "invariant violation: block->staged==0, block %lu:%lu, parent slot %lu",
3221 0 : block->slot, bank_idx, block->parent_slot ));
3222 0 : }
3223 0 : if( FD_UNLIKELY( !block->in_rdisp ) ) {
3224 : /* Invariant: staged blocks must be in the dispatcher. */
3225 0 : FD_LOG_CRIT(( "invariant violation: block->in_rdisp==0, block %lu:%lu, parent slot %lu",
3226 0 : block->slot, bank_idx, block->parent_slot ));
3227 0 : }
3228 :
3229 0 : block->txn_pool_max_popcnt = fd_ulong_max( block->txn_pool_max_popcnt, sched->depth - sched->txn_pool_free_cnt - 1UL );
3230 0 : block->mblk_pool_max_popcnt = fd_ulong_max( block->mblk_pool_max_popcnt, sched->depth - sched->mblk_pool_free_cnt );
3231 0 : block->block_pool_max_popcnt = fd_ulong_max( block->block_pool_max_popcnt, sched->block_pool_popcnt );
3232 :
3233 0 : int exec_tile_idx = (int)exec_idx;
3234 :
3235 0 : switch( task_type ) {
3236 0 : case FD_SCHED_TT_BLOCK_START: {
3237 0 : FD_TEST( !block->block_start_done );
3238 0 : block->block_start_done = 1;
3239 0 : if( FD_UNLIKELY( block->starts_at_update_parent && !block->flh_active ) ) {
3240 : /* Without the feature an initial UpdateParent is unexpected, as
3241 : agave rules it. The UpdateParent was parsed at ingest, but
3242 : the feature is only known once BLOCK_START cloned the bank. */
3243 0 : FD_LOG_INFO(( "bad block: SPURIOUS_UPDATE_PARENT, slot %lu, parent slot %lu, started at its UpdateParent without alpenglow_fast_leader_handover", block->slot, block->parent_slot ));
3244 0 : handle_bad_block( sched, block, FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT );
3245 0 : return FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT;
3246 0 : }
3247 0 : break;
3248 0 : }
3249 0 : case FD_SCHED_TT_BLOCK_END: {
3250 : /* It may seem redundant to be invoking task_done() on these
3251 : somewhat fake tasks. But these are necessary to drive state
3252 : transition for empty blocks or slow blocks. */
3253 0 : FD_TEST( !block->block_end_done );
3254 0 : block->block_end_done = 1;
3255 0 : if( FD_LIKELY( block_is_done( block ) ) ) slot_state_close( sched, block ); /* else on the last cert task done */
3256 0 : sched->print_buf_sz = 0UL;
3257 0 : print_block_metrics( sched, block );
3258 0 : FD_LOG_DEBUG(( "block %lu:%lu replayed fully: %s", block->slot, bank_idx, sched->print_buf ));
3259 0 : break;
3260 0 : }
3261 0 : case FD_SCHED_TT_TXN_EXEC: {
3262 0 : long now = fd_tickcount();
3263 0 : ulong delta = (ulong)(now-sched->txn_in_flight_last_tick);
3264 0 : ulong txn_exec_busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ] );
3265 0 : sched->metrics->txn_weighted_in_flight_tickcount += delta;
3266 0 : sched->metrics->txn_weighted_in_flight_cnt += delta*txn_exec_busy_cnt;
3267 0 : sched->txn_in_flight_last_tick = now;
3268 :
3269 0 : sched->txn_info_pool[ txn_idx ].tick_exec_done = now;
3270 :
3271 0 : block->txn_exec_done_cnt++;
3272 0 : block->txn_exec_in_flight_cnt--;
3273 0 : FD_TEST( !fd_ulong_extract_bit( sched->txn_exec_ready_bitset[ 0 ], exec_tile_idx ) );
3274 0 : sched->txn_exec_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->txn_exec_ready_bitset[ 0 ], exec_tile_idx );
3275 0 : sched->metrics->txn_exec_done_cnt++;
3276 0 : txn_bitset_insert( sched->exec_done_set, txn_idx );
3277 0 : sched->txn_info_pool[ txn_idx ].flags |= FD_SCHED_TXN_EXEC_DONE;
3278 0 : int dead_reason = FD_SCHED_DEAD_REASON_NONE;
3279 0 : if( FD_UNLIKELY( (sched->txn_info_pool[ txn_idx ].flags&FD_SCHED_TXN_ALT_UNRESOLVED) && data ) ) {
3280 0 : fd_sched_txn_alts_t const * alts = fd_type_pun_const( data );
3281 0 : if( FD_UNLIKELY( block->lthash_writable_cnt+alts->cnt>sched->lthash_txn_writers_max ) ) {
3282 0 : FD_LOG_INFO(( "bad block: TOO_MANY_WRITABLE_ACCOUNTS, slot %lu, parent slot %lu, txn_idx %lu, lthash_writable_cnt %u, late_reg_cnt %lu", block->slot, block->parent_slot, txn_idx, block->lthash_writable_cnt, alts->cnt ));
3283 0 : dead_reason = FD_SCHED_DEAD_REASON_TOO_MANY_WRITABLE_ACCOUNTS;
3284 0 : } else {
3285 0 : block->lthash_writable_cnt += (uint)alts->cnt;
3286 0 : if( FD_LIKELY( !block->lthash_inband ) ) {
3287 0 : for( ulong i=0UL; i<alts->cnt; i++ ) lthash_register( sched, block, alts->addrs+i );
3288 0 : sched->metrics->lthash_late_reg_cnt += (uint)alts->cnt;
3289 0 : }
3290 0 : }
3291 0 : }
3292 0 : if( txn_bitset_test( sched->sigverify_done_set, txn_idx ) && txn_bitset_test( sched->poh_mixin_done_set, txn_idx ) ) {
3293 : /* Release the txn_idx if all tasks on it are done. This is
3294 : guaranteed to only happen once per transaction because
3295 : whichever one completed first would not release. */
3296 0 : if( FD_UNLIKELY( block->txn_idx_tail==(uint)txn_idx ) ) block->txn_idx_tail = 0U;
3297 0 : fd_rdisp_complete_txn( sched->rdisp, txn_idx, 1 );
3298 0 : sched->txn_pool_free_cnt++;
3299 0 : block->txn_done_cnt++;
3300 0 : sched->metrics->txn_done_cnt++;
3301 0 : } else {
3302 0 : fd_rdisp_complete_txn( sched->rdisp, txn_idx, 0 );
3303 0 : }
3304 0 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE && !block->dying ) ) {
3305 0 : handle_bad_block( sched, block, dead_reason );
3306 0 : return dead_reason;
3307 0 : }
3308 0 : break;
3309 0 : }
3310 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
3311 0 : fd_sched_txn_sigverify_t const * task = sched->sigverify_inflight+exec_tile_idx;
3312 0 : FD_TEST( task->cnt && task->cnt<=FD_SCHED_SIGVERIFY_SIG_PARA );
3313 0 : block->sigverify_task_in_flight_cnt--;
3314 0 : FD_TEST( !fd_ulong_extract_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx ) );
3315 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx );
3316 0 : long now = fd_tickcount();
3317 0 : for( ulong i=0UL; i<task->cnt; i++ ) {
3318 0 : ulong sv_txn_idx = task->txn_idx[ i ];
3319 0 : FD_TEST( sv_txn_idx && sv_txn_idx<sched->depth );
3320 0 : sched->txn_info_pool[ sv_txn_idx ].tick_sigverify_done = now;
3321 0 : block->txn_sigverify_done_cnt++;
3322 0 : block->txn_sigverify_in_flight_cnt--;
3323 0 : sched->metrics->txn_sigverify_done_cnt++;
3324 0 : txn_bitset_insert( sched->sigverify_done_set, sv_txn_idx );
3325 0 : sched->txn_info_pool[ sv_txn_idx ].flags |= FD_SCHED_TXN_SIGVERIFY_DONE;
3326 0 : if( txn_bitset_test( sched->exec_done_set, sv_txn_idx ) && txn_bitset_test( sched->poh_mixin_done_set, sv_txn_idx ) ) {
3327 : /* Release the txn_idx if all tasks on it are done. This is
3328 : guaranteed to only happen once per transaction because
3329 : whichever one completed first would not release. */
3330 0 : if( FD_UNLIKELY( block->txn_idx_tail==(uint)sv_txn_idx ) ) block->txn_idx_tail = 0U;
3331 0 : fd_rdisp_complete_txn( sched->rdisp, sv_txn_idx, 1 );
3332 0 : sched->txn_pool_free_cnt++;
3333 0 : block->txn_done_cnt++;
3334 0 : sched->metrics->txn_done_cnt++;
3335 0 : }
3336 0 : }
3337 0 : break;
3338 0 : }
3339 0 : case FD_SCHED_TT_POH_HASH: {
3340 0 : block->poh_hashing_in_flight_cnt--;
3341 0 : FD_TEST( !fd_ulong_extract_bit( sched->poh_ready_bitset[ 0 ], exec_tile_idx ) );
3342 0 : sched->poh_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->poh_ready_bitset[ 0 ], exec_tile_idx );
3343 0 : fd_execrp_poh_hash_done_msg_t * msg = fd_type_pun( data );
3344 0 : FD_TEST( msg->cnt && msg->cnt<=FD_EXECRP_POH_PARA );
3345 0 : fd_sched_poh_hash_t const * task = sched->poh_inflight+exec_tile_idx;
3346 0 : FD_TEST( msg->cnt==task->cnt );
3347 :
3348 : /* Once the block is found to be bad, the remaining elements are
3349 : freed without retiring. */
3350 0 : int dead_reason = FD_SCHED_DEAD_REASON_NONE;
3351 0 : for( ulong i=0UL; i<msg->cnt; i++ ) {
3352 0 : block->poh_mblk_in_flight_cnt--;
3353 0 : uint mblk_idx = (uint)task->mblk_idx[ i ];
3354 0 : fd_sched_mblk_t * mblk = sched->mblk_pool+mblk_idx;
3355 0 : memcpy( mblk->curr_hash, msg->hash+i, sizeof(fd_hash_t) );
3356 0 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE ) ) {
3357 0 : free_mblk( sched, block, mblk_idx );
3358 0 : continue;
3359 0 : }
3360 0 : dead_reason = poh_retire_mblk( sched, block, mblk_idx );
3361 0 : }
3362 0 : if( FD_LIKELY( dead_reason==FD_SCHED_DEAD_REASON_NONE ) ) dead_reason = verify_ticks_eager( block );
3363 0 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE ) ) {
3364 0 : handle_bad_block( sched, block, dead_reason );
3365 0 : return dead_reason;
3366 0 : }
3367 0 : break;
3368 0 : }
3369 0 : case FD_SCHED_TT_CERT_VERIFY:
3370 0 : case FD_SCHED_TT_REWARDS: {
3371 0 : FD_TEST( block->footer_in_flight_cnt );
3372 0 : block->footer_in_flight_cnt--;
3373 0 : FD_TEST( !fd_ulong_extract_bit( sched->footer_ready_bitset[ 0 ], exec_tile_idx ) );
3374 0 : sched->footer_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->footer_ready_bitset[ 0 ], exec_tile_idx );
3375 0 : if( FD_UNLIKELY( block_is_done( block ) ) ) slot_state_close( sched, block );
3376 0 : break;
3377 0 : }
3378 0 : case FD_SCHED_TT_LTHASH_SUB: {
3379 : /* A subtraction is never invalidated. The parent is frozen, so
3380 : the value does not depend on when it ran. */
3381 0 : fd_sched_lthash_batch_t const * rec = sched->lthash_inflight+exec_tile_idx;
3382 0 : FD_TEST( rec->kind==FD_SCHED_TT_LTHASH_SUB && rec->bank_idx==bank_idx && rec->cnt && rec->cnt<=FD_SCHED_LTHASH_BATCH_MAX );
3383 0 : for( ulong i=0UL; i<rec->cnt; i++ ) {
3384 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ rec->lane ].entry_pool+rec->entry_idx[ i ];
3385 0 : FD_TEST( e->sub_state==FD_SCHED_LTHASH_SUB_DISPATCHED && e->bank_idx==(uint)bank_idx );
3386 0 : e->sub_state = (uchar)FD_SCHED_LTHASH_SUB_DONE;
3387 0 : }
3388 0 : fd_lthash_sub( block_lthash_delta( sched, block ), fd_type_pun_const( data ) );
3389 0 : block->sub_in_flight_cnt -= (uint)rec->cnt;
3390 0 : block->sub_applied_cnt += (uint)rec->cnt;
3391 0 : block->lthash_in_flight_cnt--;
3392 0 : sched->metrics->lthash_sub_applied_cnt += rec->cnt;
3393 0 : FD_TEST( !fd_ulong_extract_bit( sched->lthash_ready_bitset[ 0 ], exec_tile_idx ) );
3394 0 : sched->lthash_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->lthash_ready_bitset[ 0 ], exec_tile_idx );
3395 0 : break;
3396 0 : }
3397 0 : case FD_SCHED_TT_LTHASH_ADD: {
3398 0 : fd_sched_lthash_batch_t const * rec = sched->lthash_inflight+exec_tile_idx;
3399 0 : FD_TEST( rec->kind==FD_SCHED_TT_LTHASH_ADD && rec->bank_idx==bank_idx && rec->cnt && rec->cnt<=FD_SCHED_LTHASH_BATCH_MAX );
3400 : /* Entries live until the lane is reset, so the record's entries
3401 : are still these accounts'. */
3402 0 : ulong stale_cnt = 0UL;
3403 0 : for( ulong i=0UL; i<rec->cnt; i++ ) {
3404 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ rec->lane ].entry_pool+rec->entry_idx[ i ];
3405 0 : FD_TEST( e->bank_idx==(uint)bank_idx );
3406 : /* A later write to the account was observed while the batch
3407 : was on the tile, so its value is not final. The rewrite
3408 : freed its speculation entry, if any. */
3409 0 : stale_cnt += (ulong)(rec->ptxn_idx[ i ]!=e->ptxn_idx);
3410 0 : }
3411 0 : if( FD_UNLIKELY( stale_cnt ) ) {
3412 : /* Stale hash. Drop it. */
3413 0 : if( FD_UNLIKELY( rec->cnt>1UL ) ) {
3414 : /* We're given one sum for a whole batch, so a stale entry
3415 : spoils the batch. We don't need to handle this, because
3416 : only a speculative addition can be stale after it has
3417 : surfaced, and a speculative addition goes out alone. See
3418 : the account state machine. So a stale batch is an
3419 : invariant violation. A stale non-speculative batch of one
3420 : account is silently dropped here, though it ought to be an
3421 : invariant violation too. */
3422 0 : sched->print_buf_sz = 0UL;
3423 0 : print_all( sched, block );
3424 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
3425 0 : FD_LOG_CRIT(( "invariant violation: %lu of %lu batched additions of block %lu:%lu were rewritten on the tile", stale_cnt, rec->cnt, block->slot, bank_idx ));
3426 0 : }
3427 0 : sched->metrics->lthash_add_dispatched_stale_cnt++;
3428 0 : } else {
3429 : /* Good hash. Fold it in. */
3430 0 : fd_lthash_add( block_lthash_delta( sched, block ), fd_type_pun_const( data ) );
3431 0 : for( ulong i=0UL; i<rec->cnt; i++ ) {
3432 0 : fd_sched_lthash_entry_t * e = sched->lthash_lane[ rec->lane ].entry_pool+rec->entry_idx[ i ];
3433 0 : FD_TEST( e->add_state==FD_SCHED_LTHASH_ADD_DISPATCHED );
3434 0 : if( e->spec_idx!=UINT_MAX ) {
3435 : /* A speculative addition is always sent individually. */
3436 0 : FD_TEST( rec->cnt==1UL );
3437 0 : fd_memcpy( sched->lthash_spec_pool+e->spec_idx, data, sizeof(fd_lthash_value_t) );
3438 0 : sched->metrics->lthash_spec_applied_cnt++;
3439 0 : }
3440 0 : e->add_state = (uchar)FD_SCHED_LTHASH_ADD_DONE;
3441 0 : e->ptxn_idx = 0U;
3442 0 : }
3443 0 : block->add_applied_cnt += (uint)rec->cnt;
3444 0 : sched->metrics->lthash_add_applied_cnt += rec->cnt;
3445 0 : }
3446 0 : block->add_done_cnt += (uint)rec->cnt;
3447 0 : for( ulong i=0UL; i<rec->cnt; i++ ) fd_rdisp_complete_txn( sched->rdisp, (ulong)rec->ptxn_idx[ i ], 1 );
3448 0 : block->lthash_in_flight_cnt--;
3449 0 : FD_TEST( !fd_ulong_extract_bit( sched->lthash_ready_bitset[ 0 ], exec_tile_idx ) );
3450 0 : sched->lthash_ready_bitset[ 0 ] = fd_ulong_set_bit( sched->lthash_ready_bitset[ 0 ], exec_tile_idx );
3451 0 : break;
3452 0 : }
3453 0 : }
3454 :
3455 0 : if( FD_UNLIKELY( block->dying && !block_is_in_flight( block ) ) ) {
3456 0 : if( FD_UNLIKELY( sched->active_bank_idx==bank_idx ) ) {
3457 0 : FD_LOG_CRIT(( "invariant violation: active block %lu:%lu shouldn't be dying, parent slot %lu",
3458 0 : block->slot, bank_idx, block->parent_slot ));
3459 0 : }
3460 0 : FD_LOG_DEBUG(( "dying block %lu:%lu drained", block->slot, bank_idx ));
3461 0 : subtree_abandon( sched, block );
3462 0 : try_activate_block( sched );
3463 0 : return 0;
3464 0 : }
3465 :
3466 0 : slot_state_eval( sched, block );
3467 :
3468 0 : if( FD_UNLIKELY( !block->dying && sched->active_bank_idx!=bank_idx ) ) {
3469 : /* Block is not dead. So we should be actively replaying it. */
3470 0 : fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
3471 0 : FD_LOG_CRIT(( "invariant violation: sched->active_bank_idx %lu, slot %lu, parent slot %lu, bank_idx %lu, slot %lu, parent slot %lu",
3472 0 : sched->active_bank_idx, active_block->slot, active_block->parent_slot,
3473 0 : bank_idx, block->slot, block->parent_slot ));
3474 0 : }
3475 :
3476 0 : maybe_switch_block( sched, bank_idx );
3477 :
3478 0 : return 0;
3479 0 : }
3480 :
3481 : void
3482 0 : fd_sched_block_abandon( fd_sched_t * sched, ulong bank_idx, int cause ) {
3483 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3484 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3485 :
3486 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3487 0 : if( FD_UNLIKELY( !block->in_sched ) ) {
3488 0 : FD_LOG_CRIT(( "invariant violation: block->in_sched==0, block %lu:%lu, parent slot %lu",
3489 0 : block->slot, bank_idx, block->parent_slot ));
3490 0 : }
3491 :
3492 0 : if( FD_LIKELY( cause==FD_SCHED_ABANDON_DISCARDED && !block->dying ) ) block->discarded = 1;
3493 :
3494 0 : FD_LOG_INFO(( "abandoning block %lu:%lu", block->slot, bank_idx ));
3495 0 : sched->print_buf_sz = 0UL;
3496 0 : print_all( sched, block );
3497 0 : FD_LOG_DEBUG(( "%s", sched->print_buf ));
3498 :
3499 0 : subtree_abandon( sched, block );
3500 0 : try_activate_block( sched );
3501 0 : }
3502 :
3503 : int
3504 0 : fd_sched_get_dead_reason( fd_sched_t * sched, ulong bank_idx ) {
3505 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3506 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3507 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3508 0 : if( FD_UNLIKELY( !block->in_sched ) ) return FD_SCHED_DEAD_REASON_NONE;
3509 0 : return block->dead_reason;
3510 0 : }
3511 :
3512 : int
3513 0 : fd_sched_block_is_discarded( fd_sched_t * sched, ulong bank_idx ) {
3514 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3515 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3516 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3517 0 : if( FD_UNLIKELY( !block->in_sched ) ) return 0;
3518 0 : return (int)block->discarded;
3519 0 : }
3520 :
3521 : void
3522 0 : fd_sched_cancel( fd_sched_t * sched, ulong bank_idx ) {
3523 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3524 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3525 :
3526 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3527 0 : if( FD_UNLIKELY( !block->in_sched ) ) return;
3528 0 : FD_LOG_INFO(( "canceling subtree at block %lu:%lu", block->slot, bank_idx ));
3529 0 : fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
3530 0 : if( FD_LIKELY( parent ) ) {
3531 : /* Splice the block out of its parent's children list. */
3532 0 : ulong block_idx = block_to_idx( sched, block );
3533 0 : ulong * idx_p = &parent->child_idx;
3534 0 : while( *idx_p!=block_idx ) {
3535 0 : idx_p = &(block_pool_ele( sched, *idx_p )->sibling_idx);
3536 0 : }
3537 0 : *idx_p = block->sibling_idx;
3538 0 : }
3539 0 : subtree_prune( sched, bank_idx, ULONG_MAX );
3540 0 : }
3541 :
3542 : void
3543 1008 : fd_sched_block_add_done( fd_sched_t * sched, ulong bank_idx, ulong parent_bank_idx, ulong slot ) {
3544 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3545 1008 : FD_TEST( bank_idx<sched->block_cnt_max );
3546 :
3547 1008 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3548 1008 : add_block( sched, bank_idx, parent_bank_idx );
3549 1008 : block->slot = slot;
3550 1008 : block->fec_eos = 1;
3551 1008 : block->block_start_signaled = 1;
3552 1008 : block->block_end_signaled = 1;
3553 1008 : block->block_start_done = 1;
3554 1008 : block->block_end_done = 1;
3555 1008 : block->refcnt = 0;
3556 1008 : if( FD_LIKELY( parent_bank_idx!=ULONG_MAX ) ) {
3557 0 : fd_sched_block_t * parent_block = block_pool_ele( sched, parent_bank_idx );
3558 0 : block->parent_slot = parent_block->slot;
3559 0 : }
3560 1008 : if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
3561 : /* Assumes that a NULL parent implies the snapshot slot. */
3562 1008 : block->parent_slot = ULONG_MAX;
3563 1008 : block->rooted = 1;
3564 1008 : sched->root_idx = bank_idx;
3565 1008 : }
3566 1008 : }
3567 :
3568 : void
3569 1008 : fd_sched_advance_root( fd_sched_t * sched, ulong root_idx ) {
3570 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3571 1008 : FD_TEST( root_idx<sched->block_cnt_max );
3572 1008 : FD_TEST( sched->root_idx<sched->block_cnt_max );
3573 1008 : FD_TEST( ref_q_empty( sched->ref_q ) );
3574 :
3575 1008 : fd_sched_block_t * new_root = block_pool_ele( sched, root_idx );
3576 1008 : fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
3577 1008 : if( FD_UNLIKELY( !old_root->rooted ) ) {
3578 0 : FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
3579 0 : old_root->slot, old_root->parent_slot ));
3580 0 : }
3581 :
3582 : /* Early exit if the new root is the same as the old root. */
3583 1008 : if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
3584 1008 : FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
3585 1008 : new_root->slot, new_root->parent_slot ));
3586 1008 : return;
3587 1008 : }
3588 :
3589 0 : subtree_prune( sched, sched->root_idx, root_idx );
3590 :
3591 0 : new_root->parent_idx = ULONG_MAX;
3592 0 : sched->root_idx = root_idx;
3593 0 : }
3594 :
3595 : void
3596 1008 : fd_sched_root_notify( fd_sched_t * sched, ulong root_idx ) {
3597 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3598 1008 : FD_TEST( root_idx<sched->block_cnt_max );
3599 1008 : FD_TEST( sched->root_idx<sched->block_cnt_max );
3600 1008 : FD_TEST( ref_q_empty( sched->ref_q ) );
3601 :
3602 1008 : fd_sched_block_t * block = block_pool_ele( sched, root_idx );
3603 1008 : fd_sched_block_t * old_root = block_pool_ele( sched, sched->root_idx );
3604 1008 : if( FD_UNLIKELY( !old_root->rooted ) ) {
3605 0 : FD_LOG_CRIT(( "invariant violation: old_root is not rooted, slot %lu, parent slot %lu",
3606 0 : old_root->slot, old_root->parent_slot ));
3607 0 : }
3608 :
3609 : /* Early exit if the new root is the same as the old root. */
3610 1008 : if( FD_UNLIKELY( root_idx==sched->root_idx ) ) {
3611 1008 : FD_LOG_INFO(( "new root is the same as the old root, slot %lu, parent slot %lu",
3612 1008 : block->slot, block->parent_slot ));
3613 1008 : return;
3614 1008 : }
3615 :
3616 : /* Mark every node from the new root up through its parents to the
3617 : old root as being rooted. */
3618 0 : fd_sched_block_t * curr = block;
3619 0 : fd_sched_block_t * prev = NULL;
3620 0 : while( curr ) {
3621 0 : if( FD_UNLIKELY( !block_is_done( curr ) ) ) {
3622 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is not done, slot %lu, parent slot %lu",
3623 0 : curr->slot, curr->parent_slot ));
3624 0 : }
3625 0 : if( FD_UNLIKELY( curr->dying ) ) {
3626 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is dying, slot %lu, parent slot %lu",
3627 0 : curr->slot, curr->parent_slot ));
3628 0 : }
3629 0 : if( FD_UNLIKELY( curr->staged ) ) {
3630 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is staged, slot %lu, parent slot %lu",
3631 0 : curr->slot, curr->parent_slot ));
3632 0 : }
3633 0 : if( FD_UNLIKELY( curr->in_rdisp ) ) {
3634 0 : FD_LOG_CRIT(( "invariant violation: rooting a block that is in the dispatcher, slot %lu, parent slot %lu",
3635 0 : curr->slot, curr->parent_slot ));
3636 0 : }
3637 0 : curr->rooted = 1;
3638 0 : prev = curr;
3639 0 : curr = block_pool_ele( sched, curr->parent_idx );
3640 0 : }
3641 :
3642 : /* If we didn't reach the old root, the new root is not a descendant. */
3643 0 : if( FD_UNLIKELY( prev!=old_root ) ) {
3644 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",
3645 0 : block->slot, block->parent_slot, old_root->slot, old_root->parent_slot ));
3646 0 : }
3647 :
3648 0 : ulong old_active_bank_idx = sched->active_bank_idx;
3649 0 : uint old_deactivate_abandon = sched->metrics->deactivate_abandoned_cnt;
3650 :
3651 : /* Now traverse from old root towards new root, and abandon all
3652 : minority forks. */
3653 0 : curr = old_root;
3654 0 : while( curr && curr->rooted && curr!=block ) { /* curr!=block to avoid abandoning good forks. */
3655 0 : fd_sched_block_t * rooted_child_block = NULL;
3656 0 : ulong child_idx = curr->child_idx;
3657 0 : while( child_idx!=ULONG_MAX ) {
3658 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
3659 0 : child_idx = child->sibling_idx;
3660 0 : if( child->rooted ) {
3661 0 : rooted_child_block = child;
3662 0 : } else {
3663 : /* This is a minority fork. Consensus converged elsewhere, so
3664 : the fork is discarded rather than invalid. Mark the subtree
3665 : root as discarded so subtree_abandon() propagates the flag
3666 : down.
3667 :
3668 : Gated on the block not already going down. A block that is
3669 : already dying was abandoned for a reason of its own, either
3670 : sched's own verdict or runtime's verdict on its validity, and
3671 : losing the fork choice race afterwards must not re-label it
3672 : discarded. */
3673 0 : if( FD_LIKELY( !child->dying ) ) child->discarded = 1;
3674 :
3675 0 : ulong abandoned_cnt = sched->metrics->block_abandoned_cnt;
3676 0 : subtree_abandon( sched, child );
3677 0 : abandoned_cnt = sched->metrics->block_abandoned_cnt-abandoned_cnt;
3678 0 : if( FD_UNLIKELY( abandoned_cnt ) ) FD_LOG_DEBUG(( "abandoned %lu blocks on minority fork starting at block %lu:%lu", abandoned_cnt, child->slot, block_to_idx( sched, child ) ));
3679 0 : }
3680 0 : }
3681 0 : curr = rooted_child_block;
3682 0 : }
3683 :
3684 : /* If the active block got abandoned, we need to reset it. */
3685 0 : if( sched->active_bank_idx==ULONG_MAX ) {
3686 : /* Count a minority-fork deactivation once, as MinorityFork only */
3687 0 : sched->metrics->deactivate_abandoned_cnt = old_deactivate_abandon;
3688 0 : sched->metrics->deactivate_pruned_cnt += fd_uint_if( old_active_bank_idx!=ULONG_MAX, 1U, 0U );
3689 0 : try_activate_block( sched );
3690 0 : }
3691 0 : }
3692 :
3693 : ulong
3694 0 : fd_sched_slot_states_drain( fd_sched_t * sched, fd_sched_slot_state_t * out ) {
3695 0 : ulong cnt = sched->state_cnt;
3696 0 : if( FD_UNLIKELY( sched->state_drop_cnt ) ) {
3697 0 : FD_LOG_WARNING(( "dropped %lu slot state intervals", sched->state_drop_cnt ));
3698 0 : sched->state_drop_cnt = 0UL;
3699 0 : }
3700 0 : fd_memcpy( out, sched->state_buf, cnt*sizeof(fd_sched_slot_state_t) );
3701 0 : sched->state_cnt = 0UL;
3702 0 : return cnt;
3703 0 : }
3704 :
3705 : ulong
3706 4557 : fd_sched_pruned_block_next( fd_sched_t * sched ) {
3707 4557 : if( !ref_q_empty( sched->ref_q ) ) {
3708 0 : ulong bank_idx = ref_q_pop_head( sched->ref_q );
3709 0 : return bank_idx;
3710 0 : }
3711 4557 : return ULONG_MAX;
3712 4557 : }
3713 :
3714 : void
3715 0 : fd_sched_set_poh_params( fd_sched_t * sched, ulong bank_idx, ulong tick_height, ulong max_tick_height, fd_hash_t const * start_poh ) {
3716 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3717 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3718 0 : FD_TEST( max_tick_height>tick_height );
3719 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3720 0 : block->tick_height = tick_height;
3721 0 : block->max_tick_height = max_tick_height;
3722 : #if FD_SCHED_SKIP_POH
3723 : /* No-op. */
3724 : (void)start_poh;
3725 : #else
3726 0 : if( FD_LIKELY( block->mblk_cnt ) ) {
3727 : /* Fix up the first mblk's curr_hash. */
3728 0 : FD_TEST( block->mblk_unhashed_cnt );
3729 0 : FD_TEST( !mblk_slist_is_empty( block->mblks_unhashed, sched->mblk_pool ) );
3730 0 : FD_TEST( !block->mblk_freed_cnt );
3731 0 : fd_sched_mblk_t * first_mblk = sched->mblk_pool + mblk_slist_idx_peek_head( block->mblks_unhashed, sched->mblk_pool );
3732 0 : memcpy( first_mblk->curr_hash, start_poh, sizeof(fd_hash_t) );
3733 0 : } else {
3734 0 : memcpy( block->poh_hash, start_poh, sizeof(fd_hash_t) );
3735 0 : }
3736 0 : #endif
3737 0 : }
3738 :
3739 : void
3740 0 : fd_sched_set_flh_active( fd_sched_t * sched, ulong bank_idx, int flh_active ) {
3741 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3742 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3743 0 : block_pool_ele( sched, bank_idx )->flh_active = !!flh_active;
3744 0 : }
3745 :
3746 : void
3747 1008 : fd_sched_set_bypass_poh_verify( fd_sched_t * sched, int bypass_poh_verify ) {
3748 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3749 1008 : sched->bypass_poh_verify = !!bypass_poh_verify;
3750 1008 : }
3751 :
3752 : void
3753 1008 : fd_sched_set_bypass_alut_resolution( fd_sched_t * sched, int bypass_alut_resolution ) {
3754 1008 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3755 1008 : sched->bypass_alut_resolution = !!bypass_alut_resolution;
3756 1008 : }
3757 :
3758 : void
3759 0 : fd_sched_set_sigverify_group( fd_sched_t * sched, ulong sigverify_group ) {
3760 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3761 0 : FD_TEST( sigverify_group>0UL && sigverify_group<=FD_SCHED_SIGVERIFY_SIG_PARA );
3762 0 : sched->sigverify_group = sigverify_group;
3763 0 : }
3764 :
3765 : void
3766 0 : fd_sched_set_lthash_txn_writers_max( fd_sched_t * sched, ulong lthash_txn_writers_max ) {
3767 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3768 0 : FD_TEST( lthash_txn_writers_max<=FD_SCHED_LTHASH_TXN_WRITERS_MAX );
3769 0 : sched->lthash_txn_writers_max = lthash_txn_writers_max;
3770 0 : }
3771 :
3772 : void
3773 0 : fd_sched_set_lthash_oob_block_max( fd_sched_t * sched, ulong lthash_oob_block_max ) {
3774 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3775 0 : FD_TEST( lthash_oob_block_max<=FD_SCHED_LTHASH_OOB_BLOCK_MAX );
3776 0 : sched->lthash_oob_block_max = lthash_oob_block_max;
3777 0 : }
3778 :
3779 : fd_txn_p_t *
3780 0 : fd_sched_get_txn( fd_sched_t * sched, ulong txn_idx ) {
3781 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3782 0 : if( FD_UNLIKELY( txn_idx>=sched->depth ) ) {
3783 0 : return NULL;
3784 0 : }
3785 0 : return sched->txn_pool+txn_idx;
3786 0 : }
3787 :
3788 : fd_sched_txn_info_t *
3789 0 : fd_sched_get_txn_info( fd_sched_t * sched, ulong txn_idx ) {
3790 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3791 0 : if( FD_UNLIKELY( txn_idx>=sched->depth ) ) {
3792 0 : return NULL;
3793 0 : }
3794 0 : return sched->txn_info_pool+txn_idx;
3795 0 : }
3796 :
3797 : fd_hash_t *
3798 0 : fd_sched_get_poh( fd_sched_t * sched, ulong bank_idx ) {
3799 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3800 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3801 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3802 0 : FD_TEST( block->fec_eos );
3803 0 : FD_TEST( block->mblk_cnt );
3804 0 : return block->poh_hash;
3805 0 : }
3806 :
3807 : uint
3808 0 : fd_sched_get_shred_cnt( fd_sched_t * sched, ulong bank_idx ) {
3809 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3810 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3811 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3812 0 : return block->shred_cnt;
3813 0 : }
3814 :
3815 : fd_block_footer_t const *
3816 0 : fd_sched_get_footer( fd_sched_t * sched, ulong bank_idx ) {
3817 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3818 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3819 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3820 0 : return block->footer_seen ? &block->footer : NULL;
3821 0 : }
3822 :
3823 : fd_lthash_value_t const *
3824 0 : fd_sched_lthash_delta( fd_sched_t * sched, ulong bank_idx ) {
3825 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3826 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3827 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3828 : /* The delta accumulates from the block's first LtHash task on and is
3829 : final once BLOCK_END is signaled. Completing BLOCK_END zeroes it. */
3830 0 : FD_TEST( block->in_sched && !block->block_end_done );
3831 0 : return block_lthash_delta( sched, block );
3832 0 : }
3833 :
3834 : void
3835 0 : fd_sched_block_start_writes( fd_sched_t * sched, ulong bank_idx, fd_acct_addr_t const * addrs, ulong cnt ) {
3836 0 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
3837 0 : FD_TEST( bank_idx<sched->block_cnt_max );
3838 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3839 0 : FD_TEST( block->in_sched && block->staged && block->in_rdisp );
3840 0 : FD_TEST( !block->lthash_inband );
3841 : /* We expect this to run during BLOCK_START processing, before any
3842 : real transaction of the block is dispatched. */
3843 0 : FD_TEST( block->block_start_signaled && !block->block_start_done );
3844 0 : FD_TEST( !block->txn_exec_in_flight_cnt && !block->txn_exec_done_cnt );
3845 : /* Exceeding the start-of-block writable account budget is a bug, not
3846 : an invalid block. */
3847 0 : if( FD_UNLIKELY( cnt>FD_SCHED_LTHASH_START_WRITES_MAX ) ) FD_LOG_CRIT(( "start-of-block processing wrote %lu accounts, over the budget of %lu, block %lu:%lu", cnt, FD_SCHED_LTHASH_START_WRITES_MAX, block->slot, bank_idx ));
3848 0 : for( ulong i=0UL; i<cnt; i++ ) lthash_register( sched, block, addrs+i );
3849 0 : sched->metrics->lthash_start_reg_cnt += (uint)cnt;
3850 0 : }
3851 :
3852 : void
3853 : fd_sched_metrics_write( fd_sched_t * sched,
3854 0 : fd_clock_tile_t const * clock ) {
3855 0 : FD_MGAUGE_SET( REPLAY, SCHED_ACTIVE_BANK_INDEX, sched->active_bank_idx );
3856 0 : FD_MGAUGE_SET( REPLAY, SCHED_LAST_DISPATCH_BANK_INDEX, sched->next_ready_last_bank_idx );
3857 0 : FD_MGAUGE_SET( REPLAY, SCHED_LAST_DISPATCH_TIME, sched->next_ready_last_tick==LONG_MAX ? 0UL : (ulong)fd_clock_tile_tickcount_to_wallclock( clock, sched->next_ready_last_tick ) );
3858 0 : FD_MGAUGE_SET( REPLAY, SCHED_STAGING_LANE_USED, (ulong)fd_ulong_popcnt( sched->staged_bitset ) );
3859 0 : FD_MGAUGE_SET( REPLAY, SCHED_STAGING_LANE_PEAK, sched->staged_popcnt_wmk );
3860 0 : ulong staging_lane_head_bank_idx[ FD_METRICS_ENUM_STAGING_LANE_CNT ];
3861 0 : for( ulong lane=0UL; lane<FD_METRICS_ENUM_STAGING_LANE_CNT; lane++ ) {
3862 0 : staging_lane_head_bank_idx[ lane ] = fd_ulong_if( fd_ulong_extract_bit( sched->staged_bitset, (int)lane ), sched->staged_head_bank_idx[ lane ], ULONG_MAX );
3863 0 : }
3864 0 : FD_MGAUGE_ENUM_COPY( REPLAY, SCHED_STAGING_LANE_HEAD_BANK_INDEX, staging_lane_head_bank_idx );
3865 0 : FD_MGAUGE_SET( REPLAY, SCHED_TXN_POOL_USED, sched->depth-sched->txn_pool_free_cnt-1UL );
3866 0 : FD_MGAUGE_SET( REPLAY, SCHED_TXN_POOL_CAPACITY, sched->depth-1UL );
3867 0 : FD_MGAUGE_SET( REPLAY, SCHED_MICROBLOCK_POOL_USED, sched->depth-sched->mblk_pool_free_cnt );
3868 0 : FD_MGAUGE_SET( REPLAY, SCHED_MICROBLOCK_POOL_CAPACITY, sched->depth );
3869 0 : FD_MGAUGE_SET( REPLAY, SCHED_BLOCK_POOL_USED, sched->block_pool_popcnt );
3870 0 : FD_MGAUGE_SET( REPLAY, SCHED_BLOCK_POOL_CAPACITY, sched->block_cnt_max );
3871 :
3872 0 : fd_sched_metrics_t const * m = sched->metrics;
3873 :
3874 0 : ulong sched_block_added[ FD_METRICS_ENUM_SCHED_BLOCK_STAGING_STATE_CNT ];
3875 0 : sched_block_added[ FD_METRICS_ENUM_SCHED_BLOCK_STAGING_STATE_V_STAGED_IDX ] = m->block_added_staged_cnt;
3876 0 : sched_block_added[ FD_METRICS_ENUM_SCHED_BLOCK_STAGING_STATE_V_UNSTAGED_IDX ] = m->block_added_unstaged_cnt;
3877 0 : sched_block_added[ FD_METRICS_ENUM_SCHED_BLOCK_STAGING_STATE_V_DEAD_IDX ] = m->block_added_dead_ood_cnt;
3878 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_BLOCK_ADDED, sched_block_added );
3879 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_REPLAYED, m->block_removed_cnt );
3880 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_ABANDONED, m->block_abandoned_cnt );
3881 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_REJECTED, m->block_bad_cnt );
3882 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_PROMOTED, m->block_promoted_cnt );
3883 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_DEMOTED, m->block_demoted_cnt );
3884 0 : ulong sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_CNT ];
3885 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_NO_CHILD_IDX ] = m->deactivate_no_child_cnt;
3886 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_NO_WORK_IDX ] = m->deactivate_no_txn_cnt;
3887 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_ABANDONED_IDX ] = m->deactivate_abandoned_cnt;
3888 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_MINORITY_FORK_IDX ] = m->deactivate_pruned_cnt;
3889 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_SPEC_YIELD_IDX ] = m->lthash_spec_yield_cnt;
3890 0 : sched_deactivate[ FD_METRICS_ENUM_SCHED_DEACTIVATE_REASON_V_SPEC_POOL_FULL_IDX ] = m->lthash_pool_full_cnt;
3891 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_BLOCK_DEACTIVATED, sched_deactivate );
3892 0 : FD_MCNT_SET( REPLAY, SCHED_LANE_SWITCHED, m->lane_switch_cnt );
3893 0 : FD_MCNT_SET( REPLAY, SCHED_LANE_PROMOTED, m->lane_promoted_cnt );
3894 0 : FD_MCNT_SET( REPLAY, SCHED_LANE_DEMOTED, m->lane_demoted_cnt );
3895 0 : FD_MCNT_SET( REPLAY, SCHED_BLOCK_INBAND_PICKED, m->lthash_inband_block_cnt );
3896 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_PROMOTED, m->txn_promoted_cnt );
3897 0 : FD_MCNT_SET( REPLAY, SCHED_FORK_OBSERVED, m->fork_observed_cnt );
3898 0 : ulong sched_alut[ FD_METRICS_ENUM_SCHED_LUT_RESOLVE_RESULT_CNT ];
3899 0 : sched_alut[ FD_METRICS_ENUM_SCHED_LUT_RESOLVE_RESULT_V_SUCCESS_IDX ] = m->alut_success_cnt;
3900 0 : sched_alut[ FD_METRICS_ENUM_SCHED_LUT_RESOLVE_RESULT_V_FAILED_IDX ] = m->alut_serializing_cnt;
3901 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_TXN_LUT_RESOLVE_ATTEMPTED, sched_alut );
3902 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_PARSED_ABANDONED, m->txn_abandoned_parsed_cnt );
3903 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_EXECUTED_ABANDONED, m->txn_abandoned_exec_done_cnt );
3904 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_DONE_ABANDONED, m->txn_abandoned_done_cnt );
3905 : /* Fold in the open interval, which the next dispatch or completion credits */
3906 0 : ulong open_ticks = (ulong)( fd_tickcount()-sched->txn_in_flight_last_tick );
3907 0 : ulong busy_cnt = sched->exec_cnt-(ulong)fd_ulong_popcnt( sched->txn_exec_ready_bitset[ 0 ] );
3908 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_IN_FLIGHT_INTEGRAL_NANOS, m->txn_weighted_in_flight_cnt+open_ticks*busy_cnt );
3909 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_BUSY_DURATION_NANOS, m->txn_weighted_in_flight_tickcount+fd_ulong_if( !!busy_cnt, open_ticks, 0UL ) );
3910 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_IDLE_DURATION_NANOS, m->txn_none_in_flight_tickcount+fd_ulong_if( !busy_cnt, open_ticks, 0UL ) );
3911 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_PARSED, m->txn_parsed_cnt );
3912 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_EXECUTED, m->txn_exec_done_cnt );
3913 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_SIGNATURE_VERIFIED, m->txn_sigverify_done_cnt );
3914 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_POH_MIXED, m->txn_mixin_done_cnt );
3915 0 : FD_MCNT_SET( REPLAY, SCHED_TXN_DONE, m->txn_done_cnt );
3916 0 : FD_MCNT_SET( REPLAY, SCHED_MICROBLOCK_PARSED, m->mblk_parsed_cnt );
3917 0 : FD_MCNT_SET( REPLAY, SCHED_MICROBLOCK_HASHED, m->mblk_poh_hashed_cnt );
3918 0 : FD_MCNT_SET( REPLAY, SCHED_MICROBLOCK_DONE, m->mblk_poh_done_cnt );
3919 0 : FD_MCNT_SET( REPLAY, SCHED_INGESTED_BYTES, m->bytes_ingested_cnt );
3920 0 : FD_MCNT_SET( REPLAY, SCHED_TRAILING_SKIPPED_BYTES, m->bytes_ingested_unparsed_cnt );
3921 0 : FD_MCNT_SET( REPLAY, SCHED_DROPPED_BYTES, m->bytes_dropped_cnt );
3922 0 : FD_MCNT_SET( REPLAY, SCHED_FEC_INGESTED, m->fec_cnt );
3923 :
3924 0 : ulong lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_CNT ];
3925 0 : lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_V_PARSED_IDX ] = m->lthash_parsed_writable_cnt;
3926 0 : lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_V_BLOCK_START_IDX ] = m->lthash_start_reg_cnt;
3927 0 : lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_V_LATE_LUT_IDX ] = m->lthash_late_reg_cnt;
3928 0 : lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_V_DEMOTED_IDX ] = m->lthash_demote_reg_cnt;
3929 0 : lthash_write[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_TYPE_V_SPEC_GIVEN_UP_IDX ] = m->lthash_spec_given_up_cnt;
3930 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_LTHASH_WRITE_OBSERVED_BY_TYPE, lthash_write );
3931 0 : ulong lthash_handled[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_RESULT_CNT ];
3932 0 : lthash_handled[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_RESULT_V_FIRST_WRITE_IDX ] = m->lthash_first_write_cnt;
3933 0 : lthash_handled[ FD_METRICS_ENUM_SCHED_LTHASH_WRITE_RESULT_V_COALESCED_IDX ] = m->lthash_coalesced_cnt;
3934 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_LTHASH_WRITE_OBSERVED, lthash_handled );
3935 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_CREATED, m->lthash_add_created_cnt );
3936 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_SURFACED, m->lthash_add_surfaced_cnt );
3937 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_STALE_SURFACED, m->lthash_add_surfaced_stale_cnt );
3938 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_DISPATCHED, m->lthash_add_dispatched_cnt );
3939 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_STALE_DROPPED, m->lthash_add_dispatched_stale_cnt );
3940 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_APPLIED, m->lthash_add_applied_cnt );
3941 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ADD_UNDONE, m->lthash_add_undone_cnt );
3942 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SPEC_CREATED, m->lthash_spec_cnt );
3943 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SPEC_APPLIED, m->lthash_spec_applied_cnt );
3944 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SPEC_INVALIDATED, m->lthash_spec_stale_cnt );
3945 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SUB_OWED, m->lthash_entry_cnt );
3946 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SUB_DISPATCHED, m->lthash_sub_dispatched_cnt );
3947 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_SUB_APPLIED, m->lthash_sub_applied_cnt );
3948 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_ACCOUNT_FROZEN, m->lthash_frozen_cnt );
3949 0 : ulong lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_CNT ];
3950 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_ADD_OWED_IDX ] = m->lthash_discarded_add_owed_cnt;
3951 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_ADD_PENDING_IDX ] = m->lthash_discarded_add_pending_cnt;
3952 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_ADD_APPLIED_IDX ] = m->lthash_discarded_add_applied_cnt;
3953 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_SUB_QUEUED_IDX ] = m->lthash_discarded_sub_queued_cnt;
3954 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_SUB_APPLIED_IDX ] = m->lthash_discarded_sub_applied_cnt;
3955 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_ADD_HELD_IDX ] = m->lthash_discarded_add_held_cnt;
3956 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_INBAND_ADD_OWED_IDX ] = m->lthash_inband_add_owed_cnt;
3957 0 : lthash_dropped[ FD_METRICS_ENUM_SCHED_LTHASH_WORK_TYPE_V_INBAND_SUB_QUEUED_IDX ] = m->lthash_inband_sub_queued_cnt;
3958 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_LTHASH_WORK_DROPPED, lthash_dropped );
3959 0 : ulong lthash_task[ FD_METRICS_ENUM_SCHED_LTHASH_TASK_TYPE_CNT ];
3960 0 : lthash_task[ FD_METRICS_ENUM_SCHED_LTHASH_TASK_TYPE_V_SUB_IDX ] = m->lthash_sub_task_cnt;
3961 0 : lthash_task[ FD_METRICS_ENUM_SCHED_LTHASH_TASK_TYPE_V_ADD_IDX ] = m->lthash_add_task_cnt;
3962 0 : FD_MCNT_ENUM_COPY( REPLAY, SCHED_LTHASH_TASK_DISPATCHED, lthash_task );
3963 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_BATCH_HELD, m->lthash_add_batch_held_cnt );
3964 0 : FD_MCNT_SET( REPLAY, SCHED_LTHASH_BATCH_IDLE_WAITED, m->lthash_add_batch_wait_idle_cnt );
3965 0 : }
3966 :
3967 : char *
3968 0 : fd_sched_get_state_cstr( fd_sched_t * sched ) {
3969 0 : lthash_metrics_check( sched );
3970 0 : sched->print_buf_sz = 0UL;
3971 0 : print_metrics( sched );
3972 0 : print_sched( sched );
3973 0 : return sched->print_buf;
3974 0 : }
3975 :
3976 0 : void * fd_sched_leave ( fd_sched_t * sched ) { return sched; }
3977 0 : void * fd_sched_delete( void * mem ) { return mem; }
3978 :
3979 :
3980 : /* Internal helpers. */
3981 :
3982 : static void
3983 : add_block( fd_sched_t * sched,
3984 : ulong bank_idx,
3985 2009 : ulong parent_bank_idx ) {
3986 2009 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
3987 2009 : FD_TEST( !block->in_sched );
3988 2009 : sched->block_pool_popcnt++;
3989 :
3990 2009 : block->txn_parsed_cnt = 0U;
3991 2009 : block->txn_exec_in_flight_cnt = 0U;
3992 2009 : block->txn_exec_done_cnt = 0U;
3993 2009 : block->txn_sigverify_in_flight_cnt = 0U;
3994 2009 : block->txn_sigverify_done_cnt = 0U;
3995 2009 : block->sigverify_task_in_flight_cnt = 0U;
3996 2009 : block->poh_hashing_in_flight_cnt = 0U;
3997 2009 : block->poh_mblk_in_flight_cnt = 0U;
3998 2009 : block->poh_hashing_done_cnt = 0U;
3999 2009 : block->poh_hash_cmp_done_cnt = 0U;
4000 2009 : block->footer_in_flight_cnt = 0U;
4001 2009 : block->txn_done_cnt = 0U;
4002 2009 : block->shred_cnt = 0U;
4003 2009 : block->shred_scan_idx = 0U;
4004 2009 : block->shred_scan_off = 0U;
4005 2009 : block->shred_sz_overflow = 0;
4006 2009 : block->mblk_cnt = 0U;
4007 2009 : block->mblk_freed_cnt = 0U;
4008 2009 : block->mblk_tick_cnt = 0U;
4009 2009 : block->mblk_unhashed_cnt = 0U;
4010 2009 : block->dead_reason = FD_SCHED_DEAD_REASON_NONE;
4011 2009 : block->txn_pool_max_popcnt = sched->depth - sched->txn_pool_free_cnt - 1UL;
4012 2009 : block->mblk_pool_max_popcnt = sched->depth - sched->mblk_pool_free_cnt;
4013 2009 : block->block_pool_max_popcnt = sched->block_pool_popcnt;
4014 2009 : block->txn_idx_tail = 0U;
4015 2009 : block->txn_sigverify_next_idx = 0U;
4016 2009 : block->parse_mblk_idx = UINT_MAX;
4017 :
4018 2009 : mblk_slist_remove_all( block->mblks_unhashed, sched->mblk_pool );
4019 2009 : mblk_slist_remove_all( block->mblks_mixin_in_progress, sched->mblk_pool );
4020 2009 : block->last_mblk_is_tick = 0;
4021 2009 : block->tick_height = ULONG_MAX;
4022 2009 : block->max_tick_height = ULONG_MAX;
4023 :
4024 2009 : block->header_seen = 0;
4025 2009 : block->genesis_cert_seen = 0;
4026 2009 : block->update_parent_seen = 0;
4027 2009 : block->footer_seen = 0;
4028 2009 : block->cert_cnt = 0U;
4029 2009 : block->cert_next = 0U;
4030 2009 : block->alpentick_seen = 0;
4031 2009 : block->starts_at_update_parent = 0;
4032 2009 : block->flh_active = 0;
4033 :
4034 2009 : block->mblks_rem = 0UL;
4035 2009 : block->txns_rem = 0UL;
4036 2009 : block->residual_sz = 0U;
4037 2009 : block->fec_buf_boff = 0U;
4038 2009 : block->fec_eob = 0;
4039 2009 : block->fec_sob = 1;
4040 :
4041 2009 : block->fec_eos = 0;
4042 2009 : block->rooted = 0;
4043 2009 : block->dying = 0;
4044 2009 : block->discarded = 0;
4045 2009 : block->fec_completed_ns = 0L;
4046 2009 : block->refcnt = 1;
4047 2009 : block->in_sched = 1;
4048 2009 : block->in_rdisp = 0;
4049 2009 : block->block_start_signaled = 0;
4050 2009 : block->block_end_signaled = 0;
4051 2009 : block->block_start_done = 0;
4052 2009 : block->block_end_done = 0;
4053 2009 : block->staged = 0;
4054 2009 : block->state_open = 0;
4055 2009 : block->lthash_writable_cnt = 0U;
4056 2009 : block->lthash_mode_picked = 0;
4057 2009 : block->lthash_inband = 0;
4058 2009 : FD_TEST( !block->add_owed_cnt && !block->add_created_cnt && !block->sub_queued_cnt && !block->sub_in_flight_cnt && !block->sub_applied_cnt && !block->lthash_in_flight_cnt && !block->lthash_drained && !block->lthash_draining );
4059 :
4060 2009 : block->luf_depth = 0UL;
4061 :
4062 : /* New leaf node, no child, no sibling. */
4063 2009 : block->child_idx = ULONG_MAX;
4064 2009 : block->sibling_idx = ULONG_MAX;
4065 2009 : block->parent_idx = ULONG_MAX;
4066 :
4067 2009 : if( FD_UNLIKELY( parent_bank_idx==ULONG_MAX ) ) {
4068 1008 : return;
4069 1008 : }
4070 :
4071 : /* node->parent link */
4072 1001 : fd_sched_block_t * parent_block = block_pool_ele( sched, parent_bank_idx );
4073 1001 : block->parent_idx = parent_bank_idx;
4074 :
4075 : /* parent->node and sibling->node links */
4076 1001 : ulong child_idx = bank_idx;
4077 1001 : if( FD_LIKELY( parent_block->child_idx==ULONG_MAX ) ) { /* Optimize for no forking. */
4078 1001 : parent_block->child_idx = child_idx;
4079 1001 : } else {
4080 0 : fd_sched_block_t * curr_block = block_pool_ele( sched, parent_block->child_idx );
4081 0 : while( curr_block->sibling_idx!=ULONG_MAX ) {
4082 0 : curr_block = block_pool_ele( sched, curr_block->sibling_idx );
4083 0 : }
4084 0 : curr_block->sibling_idx = child_idx;
4085 0 : sched->metrics->fork_observed_cnt++;
4086 0 : }
4087 :
4088 1001 : if( FD_UNLIKELY( parent_block->dying ) ) {
4089 0 : record_lineage_death( block, parent_block );
4090 0 : block->dying = 1;
4091 0 : }
4092 1001 : }
4093 :
4094 : /* Alpenglow block structure. agave's BlockComponentProcessor rules
4095 : an Alpenglow block invalid unless its components are laid out as
4096 :
4097 : header | [genesis cert] | entries* | [update parent] | entries* | footer | alpentick
4098 :
4099 : with exactly one header and one footer, and the alpentick as the
4100 : final component. The helpers below drive the same state machine off
4101 : the batches sched parses. */
4102 :
4103 : static int
4104 : on_marker( fd_sched_t * sched,
4105 : fd_sched_block_t * block,
4106 1993 : fd_block_marker_t const * marker ) {
4107 1993 : (void)sched;
4108 1993 : switch( marker->kind ) {
4109 :
4110 1001 : case FD_BLOCK_MARKER_KIND_HEADER:
4111 1001 : if( FD_UNLIKELY( block->header_seen ) ) {
4112 0 : FD_LOG_INFO(( "bad block: MULTIPLE_BLOCK_HEADERS, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4113 0 : return FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_HEADERS;
4114 0 : }
4115 : /* Checking for shred header and parent_slot mismatch occurs at the
4116 : rotor level, so it does not need to be checked in sched. We do
4117 : this because currently replay tile never sees shred headers. */
4118 1001 : block->header_seen = 1;
4119 1001 : return FD_SCHED_DEAD_REASON_NONE;
4120 :
4121 0 : case FD_BLOCK_MARKER_KIND_GENESIS_CERT:
4122 0 : if( FD_UNLIKELY( !block->header_seen ) ) {
4123 0 : FD_LOG_INFO(( "bad block: MISSING_PARENT_MARKER, slot %lu, parent slot %lu, genesis cert before header", block->slot, block->parent_slot ));
4124 0 : return FD_SCHED_DEAD_REASON_MISSING_PARENT_MARKER;
4125 0 : }
4126 0 : if( FD_UNLIKELY( block->mblk_cnt || block->genesis_cert_seen || block->footer_seen ) ) {
4127 0 : FD_LOG_INFO(( "bad block: GENESIS_CERT_OUT_OF_ORDER, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4128 0 : return FD_SCHED_DEAD_REASON_GENESIS_CERT_OUT_OF_ORDER;
4129 0 : }
4130 0 : block->genesis_cert_seen = 1;
4131 0 : return FD_SCHED_DEAD_REASON_NONE;
4132 :
4133 992 : case FD_BLOCK_MARKER_KIND_FOOTER:
4134 992 : if( FD_UNLIKELY( !block->header_seen ) ) {
4135 0 : FD_LOG_INFO(( "bad block: MISSING_PARENT_MARKER, slot %lu, parent slot %lu, footer before header", block->slot, block->parent_slot ));
4136 0 : return FD_SCHED_DEAD_REASON_MISSING_PARENT_MARKER;
4137 0 : }
4138 992 : if( FD_UNLIKELY( block->footer_seen ) ) {
4139 0 : FD_LOG_INFO(( "bad block: MULTIPLE_BLOCK_FOOTERS, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4140 0 : return FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_FOOTERS;
4141 0 : }
4142 992 : block->footer = marker->footer;
4143 992 : block->footer_seen = 1;
4144 992 : fd_block_footer_cert_t const * cert [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
4145 992 : uint tag [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
4146 992 : ulong quorum[ FD_ALPENGLOW_FOOTER_CERT_MAX ];
4147 992 : ulong cert_cnt = fd_alpenglow_footer_certs( &block->footer, cert, tag, quorum );
4148 992 : FD_TEST( cert_cnt<=FD_ALPENGLOW_FOOTER_CERT_MAX );
4149 992 : block->cert_cnt = (uint)cert_cnt & 7U; /* The test is the bound. The mask only quiets the conversion warning. */
4150 992 : return FD_SCHED_DEAD_REASON_NONE;
4151 :
4152 0 : case FD_BLOCK_MARKER_KIND_UPDATE_PARENT:
4153 0 : if( FD_UNLIKELY( block->update_parent_seen ) ) {
4154 0 : FD_LOG_INFO(( "bad block: MULTIPLE_UPDATE_PARENTS, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4155 0 : return FD_SCHED_DEAD_REASON_MULTIPLE_UPDATE_PARENTS;
4156 0 : }
4157 0 : if( FD_LIKELY( !block->header_seen ) ) {
4158 : /* Replay started the block here because rotor saw the
4159 : UpdateParent, so it is the parent marker in place of the
4160 : header and must name the parent replay built the block on.
4161 : Anything before it already died as MISSING_PARENT_MARKER. */
4162 0 : if( FD_UNLIKELY( !block->starts_at_update_parent ) ) {
4163 0 : FD_LOG_INFO(( "bad block: SPURIOUS_UPDATE_PARENT, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4164 0 : return FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT;
4165 0 : }
4166 0 : if( FD_UNLIKELY( marker->update_parent.new_parent_slot!=block->parent_slot ) ) {
4167 0 : FD_LOG_INFO(( "bad block: UPDATE_PARENT_MISMATCH, slot %lu, parent slot %lu, update parent names %lu", block->slot, block->parent_slot, marker->update_parent.new_parent_slot ));
4168 0 : return FD_SCHED_DEAD_REASON_UPDATE_PARENT_MISMATCH;
4169 0 : }
4170 0 : block->header_seen = 1; /* the parent marker the remaining rules gate on */
4171 0 : block->update_parent_seen = 1;
4172 0 : return FD_SCHED_DEAD_REASON_NONE;
4173 0 : }
4174 : /* After a header or footer. If after a header, replay should have
4175 : skipped the prefix instead, so reaching here means it was handed
4176 : a prefix it did not know to skip. If after a footer, its a
4177 : misformed block. */
4178 0 : FD_LOG_INFO(( "bad block: SPURIOUS_UPDATE_PARENT, slot %lu, parent slot %lu, update parent after %s", block->slot, block->parent_slot, block->footer_seen ? "footer" : "header" ));
4179 0 : return FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT;
4180 :
4181 0 : default:
4182 0 : FD_LOG_INFO(( "bad block: slot %lu, parent slot %lu, unknown block marker kind %u", block->slot, block->parent_slot, marker->kind ));
4183 0 : return FD_SCHED_DEAD_REASON_BAD_BLOCK_MARKER;
4184 1993 : }
4185 1993 : }
4186 :
4187 : /* Called when a batch header declares one or more microblocks. */
4188 :
4189 : static int
4190 2562 : on_entry_batch( fd_sched_block_t * block ) {
4191 2562 : if( FD_UNLIKELY( !block->header_seen ) ) {
4192 0 : FD_LOG_INFO(( "bad block: MISSING_PARENT_MARKER, slot %lu, parent slot %lu, entries before header", block->slot, block->parent_slot ));
4193 0 : return FD_SCHED_DEAD_REASON_MISSING_PARENT_MARKER;
4194 0 : }
4195 2562 : if( FD_UNLIKELY( block->alpentick_seen ) ) {
4196 0 : FD_LOG_INFO(( "bad block: ENTRY_AFTER_BLOCK_FOOTER, slot %lu, parent slot %lu, batch after alpentick", block->slot, block->parent_slot ));
4197 0 : return FD_SCHED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER;
4198 0 : }
4199 : /* Only the alpentick may follow the footer: a batch of exactly one
4200 : microblock which must turn out to be a tick (checked when its
4201 : header parses). */
4202 2562 : if( FD_UNLIKELY( block->footer_seen && block->mblks_rem!=1UL ) ) {
4203 0 : FD_LOG_INFO(( "bad block: ENTRY_AFTER_BLOCK_FOOTER, slot %lu, parent slot %lu, batch of %lu microblocks after footer", block->slot, block->parent_slot, block->mblks_rem ));
4204 0 : return FD_SCHED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER;
4205 0 : }
4206 2562 : return FD_SCHED_DEAD_REASON_NONE;
4207 2562 : }
4208 :
4209 : static int
4210 992 : on_final( fd_sched_block_t * block ) {
4211 992 : if( FD_UNLIKELY( !block->footer_seen ) ) {
4212 0 : FD_LOG_INFO(( "bad block: MISSING_BLOCK_FOOTER, slot %lu, parent slot %lu", block->slot, block->parent_slot ));
4213 0 : return FD_SCHED_DEAD_REASON_MISSING_BLOCK_FOOTER;
4214 0 : }
4215 992 : if( FD_UNLIKELY( !block->alpentick_seen ) ) {
4216 0 : FD_LOG_INFO(( "bad block: INVALID_ALPENTICK_POSITION, slot %lu, parent slot %lu, block ended after footer without alpentick", block->slot, block->parent_slot ));
4217 0 : return FD_SCHED_DEAD_REASON_INVALID_ALPENTICK_POSITION;
4218 0 : }
4219 992 : return FD_SCHED_DEAD_REASON_NONE;
4220 992 : }
4221 :
4222 : /* Bounds the tick count as soon as the tick window is known. Returns
4223 : FD_SCHED_DEAD_REASON_NONE (0) on success, else the
4224 : FD_SCHED_DEAD_REASON_* the block should be ruled invalid for. Does
4225 : not modify the block. */
4226 : static int
4227 4555 : verify_ticks_eager( fd_sched_block_t * block ) {
4228 4555 : FD_TEST( block->tick_height!=ULONG_MAX ); /* PoH params initialized. */
4229 :
4230 4555 : if( FD_UNLIKELY( block->mblk_tick_cnt+block->tick_height>block->max_tick_height ) ) {
4231 1486 : FD_LOG_INFO(( "bad block: TOO_MANY_TICKS, slot %lu, parent slot %lu, tick_cnt %u, tick_height %lu, max_tick_height %lu", block->slot, block->parent_slot, block->mblk_tick_cnt, block->tick_height, block->max_tick_height ));
4232 1486 : return FD_SCHED_DEAD_REASON_TOO_MANY_TICKS;
4233 1486 : }
4234 :
4235 3069 : return FD_SCHED_DEAD_REASON_NONE;
4236 4555 : }
4237 :
4238 : /* https://github.com/anza-xyz/agave/blob/v3.0.6/ledger/src/blockstore_processor.rs#L1057
4239 :
4240 : The only check we don't do here is TRAILING_ENTRY, which can be done
4241 : independently when we parse the final FEC set of a block.
4242 :
4243 : Returns FD_SCHED_DEAD_REASON_NONE (0) on success, else the
4244 : FD_SCHED_DEAD_REASON_* the block should be ruled invalid for. Does
4245 : not modify the block. */
4246 : static int
4247 992 : verify_ticks_final( fd_sched_block_t * block ) {
4248 992 : FD_TEST( block->fec_eos );
4249 :
4250 992 : if( FD_UNLIKELY( block->mblk_tick_cnt+block->tick_height<block->max_tick_height ) ) {
4251 0 : FD_LOG_INFO(( "bad block: TOO_FEW_TICKS, slot %lu, parent slot %lu, tick_cnt %u, tick_height %lu, max_tick_height %lu", block->slot, block->parent_slot, block->mblk_tick_cnt, block->tick_height, block->max_tick_height ));
4252 0 : return FD_SCHED_DEAD_REASON_TOO_FEW_TICKS;
4253 0 : }
4254 :
4255 992 : return verify_ticks_eager( block );
4256 992 : }
4257 :
4258 : int
4259 : fd_sched_block_verify_ticks( fd_sched_t * sched,
4260 : ulong bank_idx,
4261 : ulong tick_height,
4262 4555 : ulong max_tick_height ) {
4263 4555 : FD_TEST( sched->canary==FD_SCHED_MAGIC );
4264 4555 : FD_TEST( bank_idx<sched->block_cnt_max );
4265 4555 : FD_TEST( max_tick_height>tick_height );
4266 4555 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
4267 : /* Set the tick window directly; skip fd_sched_set_poh_params
4268 : PoH fixup, unneeded here and unsafe to repeat per FEC set. */
4269 4555 : block->tick_height = tick_height;
4270 4555 : block->max_tick_height = max_tick_height;
4271 : /* fec_eos => slot fully ingested, so the TOO_FEW check is meaningful
4272 : too. */
4273 4555 : return block->fec_eos ? verify_ticks_final( block ) : verify_ticks_eager( block );
4274 4555 : }
4275 :
4276 28260 : #define CHECK( cond ) do { \
4277 28260 : if( FD_UNLIKELY( !(cond) ) ) { \
4278 2 : return FD_SCHED_DEAD_REASON_NONE; \
4279 2 : } \
4280 28260 : } while( 0 )
4281 :
4282 : /* CHECK that it is safe to read at least n more bytes. */
4283 28260 : #define CHECK_LEFT( n ) CHECK( (n)<=(sched->fec_buf_sz-sched->fec_buf_soff) )
4284 :
4285 : /* Consume as much as possible from the buffer. By the end of this
4286 : function, there could be residual data left over in the buffer. That
4287 : residual has to be either a partially received microblock header or a
4288 : transaction that straddles a FEC set boundary. These bytes are kept
4289 : and will be concatenated with the next FEC set. This residual is
4290 : bounded.
4291 :
4292 : Trailing bytes within a batch are dropped immediately. These
4293 : trailing bytes can span arbitrarily many FEC sets. */
4294 : FD_WARN_UNUSED static int
4295 4557 : fd_sched_parse( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
4296 32815 : while( 1 ) {
4297 34357 : while( block->txns_rem>0UL ) {
4298 1542 : int err = fd_sched_parse_txn( sched, block, alut_ctx );
4299 1542 : if( FD_UNLIKELY( -1==err ) ) return FD_SCHED_DEAD_REASON_NONE;
4300 1542 : else if( FD_UNLIKELY( err ) ) return err;
4301 1542 : }
4302 :
4303 32815 : if( block->txns_rem==0UL && block->mblks_rem>0UL ) {
4304 23703 : if( FD_UNLIKELY( block->mblk_cnt>=sched->max_mblk_per_slot ) ) {
4305 : /* Microblock count is enforced as invariant
4306 : mblk_cnt+mblks_rem<=max_mblk_per_slot
4307 : when we read the declared count in a batch header. So we
4308 : shouldn't get here. */
4309 0 : sched->print_buf_sz = 0UL;
4310 0 : print_all( sched, block );
4311 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
4312 0 : FD_LOG_CRIT(( "invariant violation: slot %lu, parent slot %lu, mblks_rem %lu, mblk_cnt %u (%u ticks) >= %lu", block->slot, block->parent_slot, block->mblks_rem, block->mblk_cnt, block->mblk_tick_cnt, sched->max_mblk_per_slot ));
4313 0 : }
4314 :
4315 23703 : CHECK_LEFT( sizeof(fd_microblock_hdr_t) );
4316 23703 : fd_microblock_hdr_t * hdr = (fd_microblock_hdr_t *)fd_type_pun( sched->fec_buf+sched->fec_buf_soff );
4317 23703 : sched->fec_buf_soff += (uint)sizeof(fd_microblock_hdr_t);
4318 :
4319 23703 : if( FD_UNLIKELY( hdr->txn_cnt>fd_ulong_sat_sub( sched->max_txn_per_slot, block->txn_parsed_cnt ) ) ) {
4320 0 : FD_LOG_INFO(( "bad block: TOO_MANY_TXNS, microblock header declared too many transactions, slot %lu, parent slot %lu, txn_parsed_cnt %u, hdr->txn_cnt %lu", block->slot, block->parent_slot, block->txn_parsed_cnt, hdr->txn_cnt ));
4321 0 : return FD_SCHED_DEAD_REASON_TOO_MANY_TXNS;
4322 0 : }
4323 23703 : if( FD_UNLIKELY( hdr->hash_cnt!=1UL ) ) {
4324 0 : FD_LOG_INFO(( "bad block: ALPENGLOW_HASH_CNT, slot %lu, parent slot %lu, mblk idx %u declared hash_cnt %lu", block->slot, block->parent_slot, block->mblk_cnt, hdr->hash_cnt ));
4325 0 : return FD_SCHED_DEAD_REASON_ALPENGLOW_HASH_CNT;
4326 0 : }
4327 23703 : if( FD_UNLIKELY( block->footer_seen ) ) {
4328 992 : if( FD_UNLIKELY( hdr->txn_cnt ) ) {
4329 0 : FD_LOG_INFO(( "bad block: ENTRY_AFTER_BLOCK_FOOTER, slot %lu, parent slot %lu, transaction microblock after footer", block->slot, block->parent_slot ));
4330 0 : return FD_SCHED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER;
4331 0 : }
4332 992 : block->alpentick_seen = 1;
4333 992 : }
4334 :
4335 23703 : block->mblks_rem--;
4336 23703 : block->txns_rem = hdr->txn_cnt;
4337 :
4338 23703 : sched->metrics->mblk_parsed_cnt++;
4339 23703 : if( FD_UNLIKELY( !hdr->txn_cnt ) ) {
4340 : /* This is a tick microblock. */
4341 22304 : block->mblk_tick_cnt++;
4342 22304 : }
4343 :
4344 23703 : FD_TEST( sched->mblk_pool_free_cnt ); /* can_ingest should have guaranteed sufficient free capacity. */
4345 23703 : uint mblk_idx = sched->mblk_pool_free_head;
4346 23703 : sched->mblk_pool_free_head = sched->mblk_pool[ mblk_idx ].next;
4347 23703 : sched->mblk_pool_free_cnt--;
4348 :
4349 23703 : fd_sched_mblk_t * mblk = sched->mblk_pool+mblk_idx;
4350 23703 : mblk->start_txn_idx = block->txn_parsed_cnt;
4351 23703 : mblk->end_txn_idx = mblk->start_txn_idx+hdr->txn_cnt;
4352 23703 : mblk->curr_txn_idx = mblk->start_txn_idx;
4353 23703 : mblk->curr_sig_cnt = 0U;
4354 23703 : mblk->mixin_txn_idx = 0U;
4355 23703 : mblk->is_tick = !hdr->txn_cnt;
4356 23703 : memcpy( mblk->end_hash, hdr->hash, sizeof(fd_hash_t) );
4357 23703 : memcpy( mblk->curr_hash, block->poh_hash, sizeof(fd_hash_t) );
4358 23703 : block->parse_mblk_idx = mblk_idx;
4359 :
4360 : /* Update block tracking. */
4361 23703 : memcpy( block->poh_hash, hdr->hash, sizeof(fd_hash_t) );
4362 23703 : block->last_mblk_is_tick = mblk->is_tick;
4363 23703 : block->mblk_cnt++;
4364 :
4365 : #if FD_SCHED_SKIP_POH
4366 : block->poh_hashing_done_cnt++;
4367 : block->poh_hash_cmp_done_cnt++;
4368 : free_mblk( sched, block, mblk_idx );
4369 : #else
4370 23703 : mblk_slist_idx_push_tail( block->mblks_unhashed, mblk_idx, sched->mblk_pool );
4371 23703 : block->mblk_unhashed_cnt++;
4372 23703 : #endif
4373 23703 : continue;
4374 23703 : }
4375 9112 : if( block->txns_rem==0UL && block->mblks_rem==0UL && block->fec_sob ) {
4376 4557 : CHECK_LEFT( sizeof(ulong) );
4377 4555 : FD_TEST( sched->fec_buf_soff==0U );
4378 4555 : if( FD_UNLIKELY( !FD_LOAD( ulong, sched->fec_buf ) && !block->fec_eob ) ) {
4379 : /* Block marker split across FEC sets */
4380 0 : fd_block_marker_t peek[1];
4381 0 : if( FD_UNLIKELY( FD_BLOCK_MARKER_DE_ERR_SZ==fd_block_marker_de( peek, sched->fec_buf, (ulong)sched->fec_buf_sz ) &&
4382 0 : sched->fec_buf_sz<FD_BLOCK_MARKER_SER_MAX ) ) return FD_SCHED_DEAD_REASON_NONE;
4383 0 : }
4384 4555 : block->mblks_rem = FD_LOAD( ulong, sched->fec_buf );
4385 4555 : sched->fec_buf_soff += (uint)sizeof(ulong);
4386 :
4387 4555 : if( FD_UNLIKELY( block->mblks_rem>fd_ulong_sat_sub( sched->max_mblk_per_slot, block->mblk_cnt ) ) ) {
4388 0 : FD_LOG_INFO(( "bad block: TOO_MANY_MICROBLOCKS, slot %lu, parent slot %lu, mblk_cnt %u (%u ticks) + hdr->mblk_cnt %lu >= %lu", block->slot, block->parent_slot, block->mblk_cnt, block->mblk_tick_cnt, block->mblks_rem, sched->max_mblk_per_slot ));
4389 0 : return FD_SCHED_DEAD_REASON_TOO_MANY_MICROBLOCKS;
4390 0 : }
4391 :
4392 4555 : block->fec_sob = 0;
4393 :
4394 4555 : if( FD_UNLIKELY( !block->mblks_rem ) ) {
4395 : /* A zero-microblock batch is a block marker */
4396 1993 : fd_block_marker_t marker[1];
4397 1993 : int err = fd_block_marker_de( marker, sched->fec_buf, (ulong)sched->fec_buf_sz );
4398 1993 : if( FD_UNLIKELY( err ) ) {
4399 0 : FD_LOG_INFO(( "bad block: slot %lu, unable to parse block marker (err %d)", block->slot, err ));
4400 0 : return FD_SCHED_DEAD_REASON_BAD_BLOCK_MARKER;
4401 0 : }
4402 1993 : int dead_reason = on_marker( sched, block, marker );
4403 1993 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE ) ) return dead_reason;
4404 1993 : sched->fec_buf_soff = (uint)( FD_BLOCK_MARKER_PREAMBLE_SZ+FD_LOAD( ushort, sched->fec_buf+FD_BLOCK_MARKER_PREAMBLE_SZ-sizeof(ushort) ) ); /* the marker is not trailing bytes */
4405 2562 : } else if( FD_UNLIKELY( block->mblks_rem ) ) {
4406 2562 : int dead_reason = on_entry_batch( block );
4407 2562 : if( FD_UNLIKELY( dead_reason!=FD_SCHED_DEAD_REASON_NONE ) ) return dead_reason;
4408 2562 : }
4409 4555 : continue;
4410 4555 : }
4411 4555 : if( block->txns_rem==0UL && block->mblks_rem==0UL ) {
4412 4555 : break;
4413 4555 : }
4414 4555 : }
4415 4555 : if( !block->fec_sob && block->txns_rem==0UL && block->mblks_rem==0UL ) {
4416 : /* All microblocks announced by the current batch header have been
4417 : parsed out. Anything still in the buffer must be trailing bytes.
4418 : Drop them now because trailing bytes can span many FEC sets and
4419 : accumulating them would overflow the parse buffer. Any followup
4420 : FEC sets within the same batch will simply be discarded
4421 : wholesale. */
4422 4555 : sched->metrics->bytes_ingested_unparsed_cnt += sched->fec_buf_sz-sched->fec_buf_soff;
4423 4555 : block->fec_buf_boff += sched->fec_buf_sz;
4424 4555 : sched->fec_buf_soff = 0U;
4425 4555 : sched->fec_buf_sz = 0U;
4426 4555 : }
4427 4555 : if( block->fec_eob ) {
4428 4555 : block->fec_sob = 1;
4429 4555 : block->fec_eob = 0;
4430 4555 : }
4431 4555 : return FD_SCHED_DEAD_REASON_NONE;
4432 4557 : }
4433 :
4434 : static inline fd_acct_addr_t const *
4435 2889 : get_acct( fd_txn_t const * txn, fd_acct_addr_t const * imms, fd_acct_addr_t const * alts, ushort idx ) {
4436 2889 : if( FD_LIKELY( idx<txn->acct_addr_cnt ) ) return imms+idx;
4437 0 : if( FD_UNLIKELY( !alts ) ) return NULL;
4438 0 : return alts+(idx-txn->acct_addr_cnt);
4439 0 : }
4440 :
4441 : FD_WARN_UNUSED static int
4442 1542 : fd_sched_parse_txn( fd_sched_t * sched, fd_sched_block_t * block, fd_sched_alut_ctx_t * alut_ctx ) {
4443 1542 : fd_txn_t * txn = fd_type_pun( sched->txn );
4444 :
4445 1542 : uchar * payload = sched->fec_buf+sched->fec_buf_soff;
4446 1542 : ulong remaining = sched->fec_buf_sz-sched->fec_buf_soff;
4447 1542 : ulong pay_sz = 0UL;
4448 1542 : ulong txn_sz = fd_txn_parse_core( payload,
4449 1542 : remaining,
4450 1542 : txn,
4451 1542 : NULL,
4452 1542 : &pay_sz );
4453 :
4454 : /* Can't parse out a full transaction yet, EAGAIN. */
4455 1542 : if( FD_UNLIKELY( !pay_sz || !txn_sz ) ) return -1;
4456 :
4457 : /* The pool slots this transaction will land in are cold: warm the
4458 : lines written below (the start of the payload copy, the metadata,
4459 : the start of the parsed txn and the whole info record) while the
4460 : ALUT resolution and the dispatcher insert run. A hint only. */
4461 1542 : ulong next_idx = fd_rdisp_peek_free_txn( sched->rdisp );
4462 1542 : if( FD_LIKELY( next_idx && next_idx<sched->depth ) ) {
4463 1542 : fd_txn_p_t * next_p = sched->txn_pool+next_idx;
4464 1542 : __builtin_prefetch( next_p->payload, 1, 3 );
4465 1542 : __builtin_prefetch( next_p->payload+64UL, 1, 3 );
4466 1542 : __builtin_prefetch( &next_p->payload_sz, 1, 3 );
4467 1542 : __builtin_prefetch( TXN( next_p ), 1, 3 );
4468 1542 : ulong info_lo = fd_ulong_align_dn( (ulong)(sched->txn_info_pool+next_idx ), 64UL );
4469 1542 : ulong info_hi = fd_ulong_align_up( (ulong)(sched->txn_info_pool+next_idx+1UL ), 64UL );
4470 7682 : for( ulong line=info_lo; line<info_hi; line+=64UL ) __builtin_prefetch( (void const *)line, 1, 3 );
4471 1542 : }
4472 :
4473 1542 : if( FD_UNLIKELY( block->txn_parsed_cnt>=sched->max_txn_per_slot ) ) {
4474 : /* Transaction count is enforced as invariant
4475 : txn_parsed_cnt+txns_rem<=max_txn_per_slot
4476 : when we read the declared count in a microblock header. So we
4477 : shouldn't get here. */
4478 0 : sched->print_buf_sz = 0UL;
4479 0 : print_all( sched, block );
4480 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
4481 0 : FD_LOG_CRIT(( "invariant violation: slot %lu, parent slot %lu, txn_parsed_cnt %u", block->slot, block->parent_slot, block->txn_parsed_cnt ));
4482 0 : }
4483 :
4484 1542 : ulong imm_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_IMM );
4485 1542 : ulong alt_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_ALT );
4486 :
4487 : /* Try to expand ALUTs. */
4488 1542 : int serializing = 0;
4489 1542 : if( alt_cnt>0UL ) {
4490 0 : if( FD_UNLIKELY( sched->bypass_alut_resolution ) ) {
4491 : /* test/fuzz: no accdb to query, so treat ALUT txns as serializing. */
4492 0 : serializing = 1;
4493 0 : } else {
4494 : /* Copy the slot hashes sysvar out and release the read BEFORE
4495 : resolving the ALTs. fd_runtime_load_txn_address_lookup_tables
4496 : issues its own fd_accdb_read_one per lookup table, and the accdb
4497 : acquire state is a single non-nestable flag — holding this read
4498 : open across that call would trip the IDLE assertion in
4499 : fd_accdb_acquire. The slot_hashes view aliases the record data,
4500 : so it must view the copy, not the released record. */
4501 0 : static uchar slot_hashes_buf[ FD_SYSVAR_SLOT_HASHES_BINCODE_SZ ];
4502 0 : ulong slot_hashes_sz = 0UL;
4503 0 : int have_slot_hashes = 0;
4504 0 : fd_acc_t ro = fd_accdb_read_one( alut_ctx->accdb, alut_ctx->fork_id, fd_sysvar_slot_hashes_id.uc );
4505 0 : if( FD_LIKELY( ro.lamports && ro.data_len<=sizeof(slot_hashes_buf) ) ) {
4506 0 : fd_memcpy( slot_hashes_buf, ro.data, ro.data_len );
4507 0 : slot_hashes_sz = ro.data_len;
4508 0 : have_slot_hashes = 1;
4509 0 : }
4510 0 : fd_accdb_unread_one( alut_ctx->accdb, &ro );
4511 :
4512 0 : fd_slot_hashes_t slot_hashes_view[1];
4513 0 : if( FD_LIKELY( have_slot_hashes &&
4514 0 : fd_sysvar_slot_hashes_view( slot_hashes_view, slot_hashes_buf, slot_hashes_sz ) ) ) {
4515 0 : serializing = !!fd_runtime_load_txn_address_lookup_tables( txn, payload, alut_ctx->accdb, alut_ctx->fork_id, alut_ctx->els, slot_hashes_view, sched->aluts );
4516 0 : sched->metrics->alut_success_cnt += (uint)!serializing;
4517 0 : } else {
4518 0 : serializing = 1;
4519 0 : }
4520 0 : }
4521 0 : }
4522 :
4523 : /* Transactions should not have duplicate accounts.
4524 : https://github.com/anza-xyz/agave/blob/v3.1.11/ledger/src/blockstore_processor.rs#L778-L790 */
4525 1542 : fd_acct_addr_t const * imms = fd_txn_get_acct_addrs( txn, payload );
4526 1542 : fd_acct_addr_t * alts = (!alt_cnt||serializing) ? NULL : sched->aluts;
4527 1542 : alt_cnt = alts ? alt_cnt : 0UL;
4528 1542 : if( FD_UNLIKELY( fd_chkdup_check( sched->chkdup, imms, imm_cnt, alts, alt_cnt ) ) ) {
4529 0 : FD_LOG_INFO(( "bad block: DUPLICATE_ACCOUNT, slot %lu, parent slot %lu, txn_parsed_cnt %u", block->slot, block->parent_slot, block->txn_parsed_cnt ));
4530 0 : return FD_SCHED_DEAD_REASON_DUPLICATE_ACCOUNT;
4531 0 : }
4532 :
4533 : /* The ALT accounts that could not be resolved right now are accounted
4534 : for when the transaction completes and late registers them. */
4535 1542 : ulong writable_cnt = fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_WRITABLE ) - fd_ulong_if( !alts, fd_txn_account_cnt( txn, FD_TXN_ACCT_CAT_WRITABLE_ALT ), 0UL );
4536 1542 : if( FD_UNLIKELY( block->lthash_writable_cnt+writable_cnt>sched->lthash_txn_writers_max ) ) {
4537 0 : FD_LOG_INFO(( "bad block: TOO_MANY_WRITABLE_ACCOUNTS, slot %lu, parent slot %lu, txn_parsed_cnt %u, lthash_writable_cnt %u, writable_cnt %lu", block->slot, block->parent_slot, block->txn_parsed_cnt, block->lthash_writable_cnt, writable_cnt ));
4538 0 : return FD_SCHED_DEAD_REASON_TOO_MANY_WRITABLE_ACCOUNTS;
4539 0 : }
4540 :
4541 1542 : ulong bank_idx = (ulong)(block-sched->block_pool);
4542 1542 : fd_rdisp_neww_t neww[ fd_rdisp_neww_word_cnt ];
4543 1542 : ulong txn_idx = fd_rdisp_add_txn( sched->rdisp, bank_idx, txn, payload, alts, serializing, block->lthash_inband ? NULL : neww );
4544 1542 : FD_TEST( txn_idx && txn_idx<sched->depth );
4545 1542 : block->lthash_writable_cnt += (uint)writable_cnt;
4546 1542 : if( FD_LIKELY( !block->lthash_inband ) ) {
4547 1542 : ulong first_write_cnt = fd_rdisp_neww_cnt( neww );
4548 1542 : sched->metrics->lthash_parsed_writable_cnt += writable_cnt;
4549 1542 : sched->metrics->lthash_first_write_cnt += first_write_cnt;
4550 1542 : sched->metrics->lthash_coalesced_cnt += writable_cnt-first_write_cnt;
4551 1542 : block->add_owed_cnt += (uint)first_write_cnt;
4552 1542 : }
4553 1542 : if( FD_LIKELY( block_owns_lane_map( sched, block ) ) ) {
4554 4431 : for( ulong iter = fd_rdisp_neww_iter_init( neww ); !fd_rdisp_neww_iter_done( iter ); iter=fd_rdisp_neww_iter_next( neww, iter ) ) {
4555 2889 : fd_acct_addr_t const * acct = get_acct( txn, imms, alts, (ushort)iter );
4556 2889 : FD_TEST( acct );
4557 2889 : lthash_entry_new_write_observed( sched, block, acct );
4558 2889 : }
4559 1542 : }
4560 :
4561 : /* This transaction either needs to be consumed by sigverify or PoH.
4562 : If either of the two are caught up, mark it for consumption.
4563 : Otherwise, link it to the tail of the chain. */
4564 :
4565 1542 : ulong sigverify_dispatched_cnt = (ulong)block->txn_sigverify_done_cnt + (ulong)block->txn_sigverify_in_flight_cnt;
4566 1542 : int sigverify_caught_up = sigverify_dispatched_cnt==block->txn_parsed_cnt;
4567 1542 : #if !FD_SCHED_SKIP_POH
4568 1542 : fd_sched_mblk_t * parse_mblk = sched->mblk_pool + block->parse_mblk_idx;
4569 1542 : int poh_caught_up = parse_mblk->curr_txn_idx==block->txn_parsed_cnt;
4570 1542 : #endif
4571 :
4572 1542 : sched->txn_info_pool[ txn_idx ].next_idx = 0U;
4573 : /* Link to the previous transaction. */
4574 1542 : if( FD_LIKELY( block->txn_idx_tail ) ) {
4575 572 : FD_TEST( block->txn_idx_tail<sched->depth );
4576 572 : sched->txn_info_pool[ block->txn_idx_tail ].next_idx = (uint)txn_idx;
4577 572 : }
4578 : /* If either consumer is caught up, update next pointer to the new
4579 : transaction. */
4580 1542 : if( FD_LIKELY( sigverify_caught_up ) ) block->txn_sigverify_next_idx = (uint)txn_idx;
4581 1542 : #if !FD_SCHED_SKIP_POH
4582 1542 : if( FD_LIKELY( poh_caught_up ) ) parse_mblk->mixin_txn_idx = (uint)txn_idx;
4583 1542 : #endif
4584 1542 : block->txn_idx_tail = (uint)txn_idx;
4585 :
4586 1542 : sched->metrics->txn_parsed_cnt++;
4587 1542 : sched->metrics->alut_serializing_cnt += (uint)serializing;
4588 1542 : sched->txn_pool_free_cnt--;
4589 1542 : fd_txn_p_t * txn_p = sched->txn_pool + txn_idx;
4590 1542 : txn_p->payload_sz = (ushort)pay_sz;
4591 :
4592 1542 : txn_p->start_shred_idx = (ushort)fd_uint_min( shred_split( block, block->fec_buf_boff+sched->fec_buf_soff ), USHORT_MAX ); /* monitoring-only ushorts; saturate under bench shred counts */
4593 1542 : txn_p->start_shred_idx = fd_ushort_if( txn_p->start_shred_idx>0U, (ushort)(txn_p->start_shred_idx-1U), txn_p->start_shred_idx );
4594 1542 : txn_p->end_shred_idx = (ushort)fd_uint_min( shred_split( block, block->fec_buf_boff+sched->fec_buf_soff+(uint)pay_sz ), USHORT_MAX );
4595 :
4596 1542 : fd_memcpy( txn_p->payload, payload, pay_sz );
4597 1542 : fd_memcpy( TXN(txn_p), txn, txn_sz );
4598 1542 : txn_bitset_remove( sched->exec_done_set, txn_idx );
4599 1542 : txn_bitset_remove( sched->sigverify_done_set, txn_idx );
4600 1542 : txn_bitset_remove( sched->poh_mixin_done_set, txn_idx );
4601 1542 : sched->txn_info_pool[ txn_idx ].flags = fd_ulong_if( serializing, FD_SCHED_TXN_ALT_UNRESOLVED, 0UL );
4602 1542 : sched->txn_info_pool[ txn_idx ].received_ns = block->fec_completed_ns;
4603 1542 : sched->txn_info_pool[ txn_idx ].txn_err = 0;
4604 :
4605 1542 : sched->txn_info_pool[ txn_idx ].tick_parsed = fd_tickcount();
4606 1542 : sched->txn_info_pool[ txn_idx ].tick_sigverify_disp = LONG_MAX;
4607 1542 : sched->txn_info_pool[ txn_idx ].tick_sigverify_done = LONG_MAX;
4608 1542 : sched->txn_info_pool[ txn_idx ].tick_exec_disp = LONG_MAX;
4609 1542 : sched->txn_info_pool[ txn_idx ].tick_exec_done = LONG_MAX;
4610 1542 : sched->txn_info_pool[ txn_idx ].tick_load_start = LONG_MAX;
4611 1542 : sched->txn_info_pool[ txn_idx ].tick_check_start = LONG_MAX;
4612 1542 : sched->txn_info_pool[ txn_idx ].tick_exec_start = LONG_MAX;
4613 1542 : sched->txn_info_pool[ txn_idx ].tick_commit_start = LONG_MAX;
4614 1542 : sched->txn_info_pool[ txn_idx ].tick_commit_end = LONG_MAX;
4615 :
4616 1542 : sched->txn_info_pool[ txn_idx ].slot = block->slot;
4617 1542 : sched->txn_info_pool[ txn_idx ].bank_seq = ULONG_MAX;
4618 1542 : sched->txn_info_pool[ txn_idx ].index_in_slot = block->txn_parsed_cnt;
4619 1542 : sched->txn_info_pool[ txn_idx ].exec_tile_idx = ULONG_MAX;
4620 1542 : sched->txn_info_pool[ txn_idx ].sigverify_exec_tile_idx = ULONG_MAX;
4621 1542 : sched->txn_info_pool[ txn_idx ].compute_units_consumed = 0U;
4622 1542 : sched->txn_info_pool[ txn_idx ].max_compute_units = ULONG_MAX;
4623 1542 : sched->txn_info_pool[ txn_idx ].transaction_fee = 0UL;
4624 1542 : sched->txn_info_pool[ txn_idx ].priority_fee = 0UL;
4625 1542 : sched->txn_info_pool[ txn_idx ].tips = 0UL;
4626 1542 : sched->fec_buf_soff += (uint)pay_sz;
4627 1542 : block->txn_parsed_cnt++;
4628 : #if FD_SCHED_SKIP_SIGVERIFY
4629 : txn_bitset_insert( sched->sigverify_done_set, txn_idx );
4630 : block->txn_sigverify_done_cnt++;
4631 : #endif
4632 : #if FD_SCHED_SKIP_POH
4633 : txn_bitset_insert( sched->poh_mixin_done_set, txn_idx );
4634 : #endif
4635 1542 : block->txns_rem--;
4636 1542 : return FD_SCHED_DEAD_REASON_NONE;
4637 1542 : }
4638 :
4639 : #undef CHECK
4640 : #undef CHECK_LEFT
4641 :
4642 : /* Build a sigverify task out of the queued transactions in parse order
4643 : and dispatch it to exec_tile_idx. Transactions join the task while
4644 : their signatures fit in a group of sched->sigverify_group; a
4645 : transaction with more signatures than a group goes alone. A task
4646 : short of a group waits for more transactions, unless none can come
4647 : (the block is fully ingested) or nothing of the block is in flight
4648 : to wait on. Returns whether a task was dispatched. */
4649 : static int
4650 0 : dispatch_sigverify( fd_sched_t * sched, fd_sched_block_t * block, ulong bank_idx, int exec_tile_idx, fd_sched_task_t * out ) {
4651 0 : fd_sched_txn_sigverify_t * sigverify = out->txn_sigverify;
4652 0 : ulong queued_cnt = block->txn_parsed_cnt-block->txn_sigverify_in_flight_cnt-block->txn_sigverify_done_cnt;
4653 0 : ulong group = sched->sigverify_group;
4654 0 : ulong sig_cnt = 0UL;
4655 0 : ulong cnt = 0UL;
4656 0 : uint txn_idx = block->txn_sigverify_next_idx;
4657 0 : while( cnt<queued_cnt ) {
4658 0 : FD_TEST( txn_idx && txn_idx<sched->depth );
4659 0 : ulong txn_sig_cnt = (ulong)TXN( sched->txn_pool+txn_idx )->signature_cnt;
4660 0 : if( FD_UNLIKELY( sig_cnt+txn_sig_cnt>group && cnt ) ) break;
4661 0 : sig_cnt += txn_sig_cnt;
4662 0 : sigverify->txn_idx[ cnt++ ] = txn_idx;
4663 0 : txn_idx = sched->txn_info_pool[ txn_idx ].next_idx;
4664 0 : if( FD_LIKELY( sig_cnt>=group ) ) break;
4665 0 : }
4666 0 : if( FD_UNLIKELY( cnt==queued_cnt && sig_cnt<group && !block->fec_eos && block_in_flight_cnt( block ) ) ) return 0;
4667 0 : FD_TEST( cnt && cnt<=FD_SCHED_SIGVERIFY_SIG_PARA );
4668 :
4669 0 : out->task_type = FD_SCHED_TT_TXN_SIGVERIFY;
4670 0 : sigverify->bank_idx = bank_idx;
4671 0 : sigverify->exec_idx = (ulong)exec_tile_idx;
4672 0 : sigverify->cnt = cnt;
4673 0 : long now = fd_tickcount();
4674 0 : for( ulong i=0UL; i<cnt; i++ ) {
4675 0 : fd_sched_txn_info_t * info = sched->txn_info_pool+sigverify->txn_idx[ i ];
4676 0 : info->sigverify_exec_tile_idx = (ulong)exec_tile_idx;
4677 0 : info->tick_sigverify_disp = now;
4678 0 : }
4679 0 : sched->sigverify_inflight[ exec_tile_idx ] = *sigverify;
4680 0 : sched->sigverify_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->sigverify_ready_bitset[ 0 ], exec_tile_idx );
4681 0 : sched->tile_to_bank_idx[ exec_tile_idx ] = bank_idx;
4682 0 : block->sigverify_task_in_flight_cnt++;
4683 0 : block->txn_sigverify_in_flight_cnt += (uint)cnt;
4684 0 : block->txn_sigverify_next_idx = txn_idx;
4685 0 : exec_ready_bitsets_check( sched );
4686 0 : return 1;
4687 0 : }
4688 :
4689 : /* Retires a microblock whose PoH hashing has completed, and eagerly
4690 : does as much mixin as possible. Returns FD_SCHED_DEAD_REASON_NONE on
4691 : success, else the dead reason. */
4692 : static int
4693 0 : poh_retire_mblk( fd_sched_t * sched, fd_sched_block_t * block, uint mblk_idx ) {
4694 0 : fd_sched_mblk_t * mblk = sched->mblk_pool+mblk_idx;
4695 0 : block->poh_hashing_done_cnt++;
4696 0 : sched->metrics->mblk_poh_hashed_cnt++;
4697 0 : if( FD_LIKELY( !mblk->is_tick ) ) {
4698 0 : mblk_slist_idx_push_tail( block->mblks_mixin_in_progress, mblk_idx, sched->mblk_pool );
4699 0 : } else {
4700 0 : block->poh_hash_cmp_done_cnt++;
4701 0 : sched->metrics->mblk_poh_done_cnt++;
4702 0 : free_mblk( sched, block, mblk_idx );
4703 0 : if( FD_UNLIKELY( memcmp( mblk->curr_hash, mblk->end_hash, sizeof(fd_hash_t) ) ) ) {
4704 0 : FD_BASE58_ENCODE_32_BYTES( mblk->curr_hash->hash, our_str );
4705 0 : FD_BASE58_ENCODE_32_BYTES( mblk->end_hash->hash, ref_str );
4706 0 : FD_LOG_INFO(( "bad block: TICK_HASH_MISMATCH, mblk %u, ours %s, claimed %s, slot %lu, parent slot %lu", mblk_idx, our_str, ref_str, block->slot, block->parent_slot ));
4707 0 : return FD_SCHED_DEAD_REASON_TICK_HASH_MISMATCH;
4708 0 : }
4709 0 : }
4710 0 : int mixin_res;
4711 0 : while( (mixin_res=maybe_mixin( sched, block )) ) {
4712 0 : if( FD_UNLIKELY( mixin_res==-1 ) ) return FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH;
4713 0 : FD_TEST( mixin_res==1||mixin_res==2 );
4714 0 : }
4715 0 : return FD_SCHED_DEAD_REASON_NONE;
4716 0 : }
4717 :
4718 : /* Assembles a PoH task for the given exec tile. Assumes there is a
4719 : PoH task available for dispatching. A tick needs one hash and a
4720 : transaction microblock none (its hash is the mixin), so a batch holds
4721 : only one kind. A transaction microblock still dispatches, as a
4722 : degenerate zero-hashcnt task, so that it retires through
4723 : fd_sched_task_done like any other PoH task. */
4724 : static void
4725 0 : dispatch_poh( fd_sched_t * sched, fd_sched_block_t * block, ulong bank_idx, int exec_tile_idx, fd_sched_task_t * out ) {
4726 0 : fd_sched_poh_hash_t * poh = out->poh_hash;
4727 0 : ulong cnt = 0UL;
4728 0 : ulong hashcnt = 0UL;
4729 0 : while( cnt<FD_SCHED_POH_PARA && block->mblk_unhashed_cnt ) {
4730 0 : uint mblk_idx = (uint)mblk_slist_idx_peek_head( block->mblks_unhashed, sched->mblk_pool );
4731 0 : fd_sched_mblk_t * mblk = sched->mblk_pool+mblk_idx;
4732 0 : ulong mblk_hashcnt = (ulong)mblk->is_tick;
4733 0 : if( FD_UNLIKELY( cnt && mblk_hashcnt!=hashcnt ) ) break;
4734 0 : mblk_slist_idx_pop_head( block->mblks_unhashed, sched->mblk_pool );
4735 0 : block->mblk_unhashed_cnt--;
4736 0 : hashcnt = mblk_hashcnt;
4737 0 : poh->mblk_idx[ cnt ] = mblk_idx;
4738 0 : memcpy( poh->hash+cnt, mblk->curr_hash, sizeof(fd_hash_t) );
4739 0 : cnt++;
4740 0 : block->poh_mblk_in_flight_cnt++;
4741 0 : }
4742 0 : FD_TEST( cnt ); /* poh_hashing_queued_cnt>0 implies at least one queued microblock. */
4743 :
4744 0 : out->task_type = FD_SCHED_TT_POH_HASH;
4745 0 : poh->bank_idx = bank_idx;
4746 0 : poh->exec_idx = (ulong)exec_tile_idx;
4747 0 : poh->cnt = cnt;
4748 0 : poh->hashcnt = hashcnt;
4749 0 : sched->poh_inflight[ exec_tile_idx ] = *poh;
4750 :
4751 0 : sched->poh_ready_bitset[ 0 ] = fd_ulong_clear_bit( sched->poh_ready_bitset[ 0 ], exec_tile_idx );
4752 0 : sched->tile_to_bank_idx[ exec_tile_idx ] = bank_idx;
4753 0 : block->poh_hashing_in_flight_cnt++;
4754 0 : exec_ready_bitsets_check( sched );
4755 0 : }
4756 :
4757 : /* Does up to one transaction mixin. Returns 1 if one mixin was done, 2
4758 : if that mixin also completed a microblock, 0 if no transaction mixin
4759 : was available, -1 if there is a PoH verify error. */
4760 : FD_WARN_UNUSED static int
4761 4555 : maybe_mixin( fd_sched_t * sched, fd_sched_block_t * block ) {
4762 4555 : if( FD_UNLIKELY( mblk_slist_is_empty( block->mblks_mixin_in_progress, sched->mblk_pool ) ) ) return 0;
4763 0 : FD_TEST( block->poh_hashing_done_cnt-block->poh_hash_cmp_done_cnt>0 );
4764 :
4765 : /* The microblock we would like to do mixin on is at the head of the
4766 : queue. It may have had some mixin, it may have never had any
4767 : mixin. In the case of the former, we should continue to mixin the
4768 : same head microblock until it's done, lest the per-block bmtree
4769 : gets clobbered when we start a new one. */
4770 0 : ulong mblk_idx = mblk_slist_idx_pop_head( block->mblks_mixin_in_progress, sched->mblk_pool );
4771 0 : fd_sched_mblk_t * mblk = sched->mblk_pool+mblk_idx;
4772 :
4773 0 : if( FD_UNLIKELY( mblk->end_txn_idx>block->txn_parsed_cnt ) ) {
4774 : /* A partially parsed microblock is by definition at the end of the
4775 : FEC stream. If such a microblock is in progress, there should be
4776 : no other microblock in this block so far that hasn't been
4777 : dispatched, because microblocks are dispatched in parse order. */
4778 0 : if( FD_UNLIKELY( block->mblk_unhashed_cnt ) ) {
4779 0 : sched->print_buf_sz = 0UL;
4780 0 : print_all( sched, block );
4781 0 : FD_LOG_CRIT(( "invariant violation end_txn_idx %lu: %s", mblk->end_txn_idx, sched->print_buf ));
4782 0 : }
4783 :
4784 : /* If we've decided to start mixin on a partially parsed microblock,
4785 : there better be nothing else in-progress. Otherwise, they might
4786 : clobber the per-block bmtree for mixin. */
4787 0 : if( FD_UNLIKELY( mblk->curr_txn_idx!=mblk->start_txn_idx && (block->poh_mblk_in_flight_cnt||!mblk_slist_is_empty( block->mblks_mixin_in_progress, sched->mblk_pool )) ) ) {
4788 0 : sched->print_buf_sz = 0UL;
4789 0 : print_all( sched, block );
4790 0 : FD_LOG_CRIT(( "invariant violation end_txn_idx %lu start_txn_idx %lu curr_txn_idx %lu: %s", mblk->end_txn_idx, mblk->start_txn_idx, mblk->curr_txn_idx, sched->print_buf ));
4791 0 : }
4792 0 : }
4793 :
4794 : /* Very rarely, we've finished hashing, but not all transactions in
4795 : the microblock have been parsed out. This can happen if we haven't
4796 : received all the FEC sets for this microblock. We can't yet fully
4797 : mixin the microblock. So we'll stick it back into the end of the
4798 : queue, and try to see if there's a fully parsed microblock.
4799 : Unless, there's truly nothing else to mixin. Then we would start
4800 : mixin with the partially parsed microblock. We do this because the
4801 : txn pool is meant to be an OOO scheduling window not tied to
4802 : max_live_slots sizing requirements, so there shouldn't be a way for
4803 : external input to tie up txn pool entries for longer than
4804 : necessary. */
4805 0 : if( FD_UNLIKELY( mblk->curr_txn_idx>=block->txn_parsed_cnt || /* Nothing more to mixin for this microblock. */
4806 0 : (mblk->end_txn_idx>block->txn_parsed_cnt && /* There is something to mixin, but the microblock isn't fully parsed yet ... */
4807 0 : mblk->curr_txn_idx==mblk->start_txn_idx && /* ... and we haven't started mixin on it yet ... */
4808 0 : (block->poh_mblk_in_flight_cnt || /* ... and another microblock is in-progress and might preempt this microblock and clobber the bmtree, so we shouldn't start the partial microblock just yet. Counting microblocks rather than tasks matters: a batched PoH task is retired one microblock at a time, and the ones not yet retired are off every list. */
4809 0 : !mblk_slist_is_empty( block->mblks_mixin_in_progress, sched->mblk_pool ))) ) ) {
4810 0 : mblk_slist_idx_push_tail( block->mblks_mixin_in_progress, mblk_idx, sched->mblk_pool );
4811 :
4812 : /* No other microblock in the mixin queue. */
4813 0 : if( FD_UNLIKELY( block->poh_hashing_done_cnt-block->poh_hash_cmp_done_cnt==1 ) ) return 0;
4814 :
4815 : /* At this point, there's at least one more microblock in the mixin
4816 : queue we could try. It's a predecessor (in parse order) that
4817 : finished hashing later than the partially parsed microblock at
4818 : the head of the mixin queue. */
4819 :
4820 : /* It should never clobber the bmtree for a microblock that has had some mixin done on it. */
4821 0 : if( FD_UNLIKELY( mblk->curr_txn_idx!=mblk->start_txn_idx ) ) {
4822 0 : sched->print_buf_sz = 0UL;
4823 0 : print_all( sched, block );
4824 0 : FD_LOG_CRIT(( "invariant violation curr_txn_idx %lu start_txn_idx %lu: %s", mblk->curr_txn_idx, mblk->start_txn_idx, sched->print_buf ));
4825 0 : }
4826 :
4827 0 : mblk_idx = mblk_slist_idx_pop_head( block->mblks_mixin_in_progress, sched->mblk_pool );
4828 0 : mblk = sched->mblk_pool+mblk_idx;
4829 :
4830 : /* It should be a fresh microblock for mixin. */
4831 0 : FD_TEST( mblk->curr_txn_idx==mblk->start_txn_idx );
4832 : /* Invariant: at any given point in time, there can be at most one
4833 : microblock that hasn't been fully parsed yet, due to the nature
4834 : of sequential parsing. So this microblock has to be fully
4835 : parsed. */
4836 0 : FD_TEST( mblk->end_txn_idx<=block->txn_parsed_cnt );
4837 0 : }
4838 :
4839 0 : FD_TEST( mblk->curr_txn_idx<mblk->end_txn_idx );
4840 :
4841 : /* Now mixin. */
4842 0 : if( FD_LIKELY( mblk->curr_txn_idx==mblk->start_txn_idx ) ) block->bmtree = fd_bmtree_commit_init( block->bmtree_mem, 32UL, 1UL, 0UL ); /* Optimize for single-transaction microblocks, which are the majority. */
4843 :
4844 0 : ulong txn_gidx = mblk->mixin_txn_idx;
4845 0 : uint next_txn_pool_idx = sched->txn_info_pool[ txn_gidx ].next_idx;
4846 0 : fd_txn_p_t * _txn = sched->txn_pool+txn_gidx;
4847 0 : fd_txn_t * txn = TXN(_txn);
4848 0 : for( ulong j=0; j<txn->signature_cnt; j++ ) {
4849 0 : fd_bmtree_node_t node[ 1 ];
4850 0 : fd_bmtree_hash_leaf( node, _txn->payload+txn->signature_off+FD_TXN_SIGNATURE_SZ*j, 64UL, 1UL );
4851 0 : fd_bmtree_commit_append( block->bmtree, node, 1UL );
4852 0 : mblk->curr_sig_cnt++;
4853 0 : }
4854 :
4855 : /* Release the txn_idx. */
4856 0 : txn_bitset_insert( sched->poh_mixin_done_set, txn_gidx );
4857 0 : sched->metrics->txn_mixin_done_cnt++;
4858 0 : if( txn_bitset_test( sched->exec_done_set, txn_gidx ) && txn_bitset_test( sched->sigverify_done_set, txn_gidx ) ) {
4859 0 : if( FD_UNLIKELY( block->txn_idx_tail==(uint)txn_gidx ) ) block->txn_idx_tail = 0U;
4860 0 : fd_rdisp_complete_txn( sched->rdisp, txn_gidx, 1 );
4861 0 : sched->txn_pool_free_cnt++;
4862 0 : block->txn_done_cnt++;
4863 0 : sched->metrics->txn_done_cnt++;
4864 0 : }
4865 :
4866 0 : mblk->curr_txn_idx++;
4867 0 : mblk->mixin_txn_idx = next_txn_pool_idx;
4868 0 : int rv = 2;
4869 0 : if( FD_LIKELY( mblk->curr_txn_idx==mblk->end_txn_idx ) ) {
4870 : /* Ready to compute the final hash for this microblock. */
4871 0 : block->poh_hash_cmp_done_cnt++;
4872 0 : sched->metrics->mblk_poh_done_cnt++;
4873 0 : uchar * root = fd_bmtree_commit_fini( block->bmtree );
4874 0 : uchar mixin_buf[ 64 ];
4875 0 : fd_memcpy( mixin_buf, mblk->curr_hash, 32UL );
4876 0 : fd_memcpy( mixin_buf+32UL, root, 32UL );
4877 0 : fd_sha256_hash( mixin_buf, 64UL, mblk->curr_hash );
4878 0 : free_mblk( sched, block, (uint)mblk_idx );
4879 : /* Bypass PoH verification for fuzzing/testing throughput. */
4880 0 : if( FD_UNLIKELY( !sched->bypass_poh_verify && memcmp( mblk->curr_hash, mblk->end_hash, sizeof(fd_hash_t) ) ) ) {
4881 0 : FD_BASE58_ENCODE_32_BYTES( mblk->curr_hash->hash, our_str );
4882 0 : FD_BASE58_ENCODE_32_BYTES( mblk->end_hash->hash, ref_str );
4883 0 : FD_LOG_INFO(( "bad block: poh hash mismatch on mblk %lu, ours %s, claimed %s, txns [%lu,%lu), %u sigs, slot %lu, parent slot %lu", mblk_idx, our_str, ref_str, mblk->start_txn_idx, mblk->end_txn_idx, mblk->curr_sig_cnt, block->slot, block->parent_slot ));
4884 0 : return -1;
4885 0 : }
4886 0 : } else {
4887 : /* There are more transactions to mixin in this microblock. */
4888 0 : mblk_slist_idx_push_head( block->mblks_mixin_in_progress, mblk_idx, sched->mblk_pool );
4889 0 : rv = 1;
4890 0 : }
4891 :
4892 0 : return rv;
4893 0 : }
4894 :
4895 : /* Does activating bank_idx move execution to another fork? A block
4896 : that descends from the last active block continues its fork, even
4897 : across blocks that were added done and never activated, such as a
4898 : leader block. */
4899 : static int
4900 1001 : is_fork_switch( fd_sched_t * sched, ulong bank_idx ) {
4901 1001 : if( FD_UNLIKELY( sched->last_active_bank_idx==ULONG_MAX ) ) return 0;
4902 0 : for( ulong idx=bank_idx; idx!=ULONG_MAX; idx=block_pool_ele( sched, idx )->parent_idx ) {
4903 0 : if( idx==sched->last_active_bank_idx ) return 0;
4904 0 : }
4905 0 : return 1;
4906 0 : }
4907 :
4908 : static void
4909 1003 : try_activate_block( fd_sched_t * sched ) {
4910 : /* Early return if there's already an active block. */
4911 1003 : if( FD_LIKELY( sched->active_bank_idx!=ULONG_MAX ) ) return;
4912 :
4913 : /* See if there are any allocated staging lanes that we can activate
4914 : for scheduling ... */
4915 1003 : ulong staged_bitset = sched->staged_bitset;
4916 1003 : while( staged_bitset ) {
4917 1001 : int lane_idx = fd_ulong_find_lsb( staged_bitset );
4918 1001 : staged_bitset = fd_ulong_pop_lsb( staged_bitset );
4919 :
4920 1001 : ulong head_idx = sched->staged_head_bank_idx[ lane_idx ];
4921 1001 : fd_sched_block_t * head_block = block_pool_ele( sched, head_idx );
4922 1001 : fd_sched_block_t * parent_block = block_pool_ele( sched, head_block->parent_idx );
4923 : //FIXME: restore these invariant checks when we have immediate demotion of dying blocks
4924 : //Today, dying blocks can remain staged if they have in-flight transactions.
4925 : // if( FD_UNLIKELY( parent_block->dying ) ) {
4926 : // /* Invariant: no child of a dying block should be staged. */
4927 : // 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",
4928 : // head_idx, head_block->slot, head_block->parent_slot, lane_idx, parent_block->slot, parent_block->parent_slot ));
4929 : // }
4930 : // if( FD_UNLIKELY( head_block->dying ) ) {
4931 : // /* Invariant: no dying block should be staged. */
4932 : // FD_LOG_CRIT(( "invariant violation: staged_head_bank_idx %lu, slot %lu, prime %lu on lane %u has head_block->dying set",
4933 : // head_idx, (ulong)head_block->block_id.slot, (ulong)head_block->block_id.prime, lane_idx ));
4934 : // }
4935 1001 : if( block_is_done( parent_block ) && block_is_activatable( head_block ) ) {
4936 : /* ... Yes, on this staging lane the parent block is done. So we
4937 : can activate the staged child. */
4938 1001 : if( FD_UNLIKELY( is_fork_switch( sched, head_idx ) ) ) { /* Forking is rare. */
4939 0 : FD_LOG_DEBUG(( "activating block %lu:%lu: fork switch to lane %d", head_block->slot, head_idx, lane_idx ));
4940 0 : sched->metrics->lane_switch_cnt++;
4941 1001 : } else {
4942 1001 : FD_LOG_DEBUG(( "activating block %lu:%lu on lane %d", head_block->slot, head_idx, lane_idx ));
4943 1001 : }
4944 1001 : active_bank_set( sched, head_idx );
4945 1001 : return;
4946 1001 : }
4947 1001 : }
4948 :
4949 : /* ... No lane head with a done parent has real work. Wake one that
4950 : can speculate on the additions it owes, if the speculation pool has
4951 : room enough to make the switch worthwhile. */
4952 2 : staged_bitset = sched->staged_bitset;
4953 2 : while( staged_bitset ) {
4954 0 : int lane_idx = fd_ulong_find_lsb( staged_bitset );
4955 0 : staged_bitset = fd_ulong_pop_lsb( staged_bitset );
4956 :
4957 0 : ulong head_idx = sched->staged_head_bank_idx[ lane_idx ];
4958 0 : fd_sched_block_t * head_block = block_pool_ele( sched, head_idx );
4959 0 : fd_sched_block_t * parent_block = block_pool_ele( sched, head_block->parent_idx );
4960 0 : if( block_is_done( parent_block ) && block_is_stageable( head_block ) && block_may_speculate( sched, head_block, FD_SCHED_LTHASH_SPEC_THRESHOLD_PER_TILE*sched->exec_cnt ) ) {
4961 0 : FD_LOG_DEBUG(( "activating block %lu:%lu: speculation on lane %d", head_block->slot, head_idx, lane_idx ));
4962 0 : sched->metrics->lane_switch_cnt += (uint)is_fork_switch( sched, head_idx );
4963 0 : active_bank_set( sched, head_idx );
4964 0 : return;
4965 0 : }
4966 0 : }
4967 :
4968 : /* ... No, promote unstaged blocks. */
4969 2 : ulong root_idx = sched->root_idx;
4970 2 : if( FD_UNLIKELY( root_idx==ULONG_MAX ) ) {
4971 0 : FD_LOG_CRIT(( "invariant violation: root_idx==ULONG_MAX indicating fd_sched is uninitialized" ));
4972 0 : }
4973 : /* Find and stage the longest stageable unstaged fork. This is a
4974 : policy decision. */
4975 2 : ulong depth = compute_longest_unstaged_fork( sched, root_idx );
4976 2 : if( FD_LIKELY( depth>0UL ) ) {
4977 0 : if( FD_UNLIKELY( sched->staged_bitset==fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) {
4978 : /* No more staging lanes available. All of them are occupied by
4979 : slow squatters. Only empty blocks can be demoted, and so
4980 : blocks with in-flight transactions, including dying in-flight
4981 : blocks, shouldn't be demoted. We demote all demotable lanes.
4982 : Demotion isn't all that expensive, since demotable blocks have
4983 : no transactions in them. If a demoted block proves to be
4984 : active still, it'll naturally promote back into a staging lane.
4985 :
4986 : In fact, all lanes should be demotable at this point. None of
4987 : the lanes have anything dispatchable, otherwise we would have
4988 : simply activated one of the dispatchable lanes. None of the
4989 : lanes have anything in-flight either, as we allow for a grace
4990 : period while something is in-flight, before we deactivate any
4991 : block. In principle, we could get rid of the grace period and
4992 : deactivate right away. In that case, it's okay if nothing is
4993 : demotable at the moment, as that simply implies that all lanes
4994 : have in-flight tasks. We would get another chance to try to
4995 : demote when the last in-flight task on any lane completes.
4996 :
4997 : A note on why this whole thing does not deadlock:
4998 :
4999 : One might reasonably wonder what happens if all the lanes are
5000 : non-empty, non-dead, but for some reason couldn't be activated
5001 : for dispatching. We would deadlock in this case, as no lane
5002 : dispatches to the point of being demotable, and no unstaged
5003 : block can be promoted. Such is not in fact possible. The only
5004 : way a dispatchable lane can be ineligible for activation is if
5005 : it has a parent block that isn't done yet. So a deadlock
5006 : happens when this parent block, or any of its dispatchable
5007 : ancestors, is unstaged. An important invariant we maintain is
5008 : that a staged block can't have an unstaged stageable parent.
5009 : This invariant, by induction, gives us the guarantee that at
5010 : least one of the lanes can be activated. */
5011 0 : for( int l=0; l<(int)FD_SCHED_MAX_STAGING_LANES; l++ ) {
5012 : /* We would be able to assert that all lanes are demotable,
5013 : except that abandoned blocks are given no grace period for
5014 : deactivation. So there could be lanes transiently occupied
5015 : by dying blocks that are neither demotable (due to in-flight
5016 : tasks) nor activatable (due to being dying). If
5017 : rdisp_demote() supported non-empty blocks, then we could
5018 : probably restore the assertion. */
5019 0 : if( FD_UNLIKELY( !lane_is_demotable( sched, l ) ) ) continue;
5020 0 : demote_lane( sched, l );
5021 0 : sched->metrics->lane_demoted_cnt++;
5022 0 : }
5023 : /* We weren't able to successfully demote anything. This is
5024 : likely because all lanes are occupied by dying blocks with
5025 : in-flight tasks. We would get another chance to try to demote
5026 : when the last in-flight task on any lane completes. */
5027 0 : if( FD_UNLIKELY( sched->staged_bitset==fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) ) ) return;
5028 0 : }
5029 0 : FD_TEST( sched->staged_bitset!=fd_ulong_mask_lsb( FD_SCHED_MAX_STAGING_LANES ) );
5030 0 : int lane_idx = pick_free_lane( sched );
5031 0 : ulong head_bank_idx = promote_from_longest_unstaged_fork( sched, root_idx, lane_idx );
5032 0 : if( FD_UNLIKELY( head_bank_idx==ULONG_MAX ) ) {
5033 : /* We found a promotable fork depth>0. This should not happen. */
5034 0 : FD_LOG_CRIT(( "invariant violation: head_bank_idx==ULONG_MAX" ));
5035 0 : }
5036 : /* We don't bother with promotion unless the block is immediately
5037 : dispatchable. So it's okay to set the active block here. This
5038 : doesn't cause out-of-order block replay because the parent of the
5039 : promoted block must be done. We can prove this by contradiction
5040 : as follows. The promoted block is the shallowest promotable
5041 : block of its fork, so its parent is not promotable. The parent
5042 : is complete, because FEC sets arrive in order along a fork and a
5043 : child of it has arrived. Suppose the parent were not done. It
5044 : is not dying, or the promoted block would be dying too and not
5045 : promotable. It is not unstaged, or it would be promotable, being
5046 : complete, and so dispatchable until it ends. So it would be
5047 : staged and dispatchable, with nothing in flight, since a block
5048 : with tasks in flight is the active block or dying, and no block
5049 : is active here. That makes it activatable, and only a parent of
5050 : its own that is not done kept the first loop above from
5051 : activating it. That parent is staged as well, because a staged
5052 : block never has an unstaged parent that still has to run, and the
5053 : same argument applies to it, and so on up the fork chain until a
5054 : staged block with a done parent is reached, which the first loop
5055 : would have activated. */
5056 0 : fd_sched_block_t * head_block = block_pool_ele( sched, head_bank_idx );
5057 0 : FD_LOG_DEBUG(( "activating block %lu:%lu: unstaged promotion to lane %d", head_block->slot, head_bank_idx, lane_idx ));
5058 0 : sched->metrics->lane_switch_cnt += (uint)is_fork_switch( sched, head_bank_idx );
5059 0 : active_bank_set( sched, head_bank_idx );
5060 0 : return;
5061 0 : }
5062 : /* No unstaged blocks to promote. So we're done. Yay. */
5063 2 : }
5064 :
5065 : static void
5066 4557 : check_or_set_active_block( fd_sched_t * sched ) {
5067 4557 : if( FD_UNLIKELY( sched->active_bank_idx==ULONG_MAX ) ) {
5068 1003 : try_activate_block( sched );
5069 3554 : } else {
5070 3554 : fd_sched_block_t * active_block = block_pool_ele( sched, sched->active_bank_idx );
5071 3554 : if( FD_UNLIKELY( block_should_deactivate( sched, active_block ) ) ) {
5072 0 : sched->print_buf_sz = 0UL;
5073 0 : print_all( sched, active_block );
5074 0 : FD_LOG_NOTICE(( "%s", sched->print_buf ));
5075 0 : FD_LOG_CRIT(( "invariant violation: should have been deactivated" ));
5076 0 : }
5077 3554 : }
5078 4557 : }
5079 :
5080 : /* This function has two main jobs:
5081 : - Mark everything on the fork tree dying.
5082 : - Take blocks out of rdisp if possible. */
5083 : static void
5084 2 : subtree_mark_and_maybe_prune_rdisp( fd_sched_t * sched, fd_sched_block_t * block ) {
5085 2 : if( FD_UNLIKELY( block->rooted ) ) {
5086 0 : FD_LOG_CRIT(( "invariant violation: rooted block should not be abandoned, slot %lu, parent slot %lu",
5087 0 : block->slot, block->parent_slot ));
5088 0 : }
5089 : /* All minority fork nodes pass through this function eventually. So
5090 : this is a good point to check per-node invariants for minority
5091 : forks. */
5092 2 : if( FD_UNLIKELY( block->staged && !block->in_rdisp ) ) {
5093 0 : FD_LOG_CRIT(( "invariant violation: staged block is not in the dispatcher, slot %lu, parent slot %lu",
5094 0 : block->slot, block->parent_slot ));
5095 0 : }
5096 :
5097 2 : fd_sched_block_t * parent = block_pool_ele( sched, block->parent_idx );
5098 2 : if( FD_UNLIKELY( !parent || !parent->in_sched ) ) {
5099 : /* Only the root has no parent. Abandon should never be called on
5100 : the root. So any block we are trying to abandon should have a
5101 : parent. */
5102 0 : FD_LOG_CRIT(( "invariant violation: parent not found slot %lu, parent slot %lu",
5103 0 : block->slot, block->parent_slot ));
5104 0 : }
5105 :
5106 : /* Gated on the parent going down and on this block not already being
5107 : on its way down.
5108 :
5109 : The parent test is because this function also runs on the block the
5110 : public abandon API was called on, whose parent is very much alive.
5111 : Inheriting there would stamp DEAD_ANCESTOR on a block that went
5112 : down on its own, and overwrite the discarded bit the caller just
5113 : set with the live parent's 0.
5114 :
5115 : The block test is because the walk also revisits blocks that
5116 : already went down, and an ancestor abandoned later must not
5117 : re-label them. This cannot be decided based on dead_reason alone
5118 : because a verdict the replay tile made leaves it NONE.
5119 :
5120 : Descendants work fine either way, because dying is set below before
5121 : we recurse, so every child rightfully sees a dying parent. */
5122 2 : if( FD_UNLIKELY( parent->dying && !block->dying ) ) record_lineage_death( block, parent );
5123 :
5124 : /* Setting the flag is non-optional and can happen more than once. */
5125 2 : block->dying = 1;
5126 2 : slot_state_close( sched, block );
5127 2 : lthash_release_held_batch( sched, block );
5128 :
5129 : /* Removal from dispatcher should only happen once. */
5130 2 : if( block->in_rdisp ) {
5131 :
5132 : /* The dispatcher expects blocks to be abandoned in the same order
5133 : that they were added on each lane, which holds trivially with
5134 : every staged block having its own lane. */
5135 2 : if( FD_UNLIKELY( block->staged && sched->active_bank_idx==sched->staged_head_bank_idx[ block->staging_lane ] && sched->active_bank_idx!=ULONG_MAX ) ) {
5136 2 : FD_TEST( block_pool_ele( sched, sched->active_bank_idx )==block );
5137 2 : FD_LOG_DEBUG(( "reset active_bank_idx %lu: abandon", sched->active_bank_idx ));
5138 2 : sched->last_active_bank_idx = sched->active_bank_idx;
5139 2 : active_bank_set( sched, ULONG_MAX );
5140 2 : sched->metrics->deactivate_abandoned_cnt++;
5141 2 : }
5142 :
5143 : /* We inform the dispatcher of an abandon only when there are no
5144 : more in-flight transactions. Otherwise, if the dispatcher
5145 : recycles the same txn_id that was just abandoned, and we receive
5146 : completion of an in-flight transaction whose txn_id was just
5147 : recycled. */
5148 : // FIXME The recycling might be fine now that we no longer use
5149 : // txn_id to index into anything. We might be able to just drop
5150 : // txn_id on abandoned blocks. Though would this leak transaction
5151 : // content if the txn_id is recycled?
5152 : // Note that subtree pruning from sched isn't dependent on the
5153 : // in-flight check being present here, as is_prunable already checks
5154 : // for in-flight==0.
5155 2 : int abandon = !block_is_in_flight( block );
5156 :
5157 2 : if( abandon ) {
5158 2 : block->in_rdisp = 0;
5159 2 : sched->metrics->lthash_discarded_add_owed_cnt += block->add_owed_cnt;
5160 2 : sched->metrics->lthash_discarded_add_pending_cnt += block->add_created_cnt-block->add_surfaced_cnt;
5161 2 : sched->metrics->lthash_discarded_sub_queued_cnt += block->sub_queued_cnt;
5162 2 : sched->metrics->lthash_discarded_sub_applied_cnt += block->sub_applied_cnt;
5163 2 : sched->metrics->lthash_discarded_add_applied_cnt += block->add_applied_cnt-block->add_undone_cnt;
5164 2 : lthash_block_reset( sched, block );
5165 2 : lthash_release_reservation( sched, block );
5166 2 : fd_rdisp_abandon_block( sched->rdisp, (ulong)(block-sched->block_pool) );
5167 2 : block->txn_idx_tail = 0U;
5168 2 : sched->txn_pool_free_cnt += block->txn_parsed_cnt-block->txn_done_cnt; /* in_flight_cnt==0 */
5169 :
5170 2 : sched->metrics->block_abandoned_cnt++;
5171 2 : sched->metrics->txn_abandoned_parsed_cnt += block->txn_parsed_cnt;
5172 2 : sched->metrics->txn_abandoned_exec_done_cnt += block->txn_exec_done_cnt;
5173 2 : sched->metrics->txn_abandoned_done_cnt += block->txn_done_cnt;
5174 :
5175 : //FIXME when demote supports non-empty blocks, we should demote
5176 : //the block from the lane unconditionally and immediately,
5177 : //regardless of whether it's safe to abandon or not. So a block
5178 : //would go immediately from staged to unstaged and eventually to
5179 : //abandoned.
5180 2 : if( FD_LIKELY( block->staged ) ) {
5181 2 : FD_LOG_DEBUG(( "block %lu:%lu exited lane %lu: abandon", block->slot, block_to_idx( sched, block ), block->staging_lane ));
5182 2 : block->staged = 0;
5183 : /* Release the staging lane, which the block had to itself. */
5184 2 : sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
5185 2 : sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
5186 2 : }
5187 2 : }
5188 2 : }
5189 :
5190 : /* Abandon the entire fork chaining off of this block. */
5191 2 : ulong child_idx = block->child_idx;
5192 2 : while( child_idx!=ULONG_MAX ) {
5193 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
5194 0 : subtree_mark_and_maybe_prune_rdisp( sched, child );
5195 0 : child_idx = child->sibling_idx;
5196 0 : }
5197 2 : }
5198 :
5199 : /* This function tells sched that the subtree is no longer worth
5200 : replaying. It can happen as a result of one of the following.
5201 : - Bad block at head of lane.
5202 : - Bad block at tail of lane.
5203 : - Root notify.
5204 :
5205 : It's safe to call this function more than once on the same block. */
5206 : static void
5207 2 : subtree_abandon( fd_sched_t * sched, fd_sched_block_t * block ) {
5208 2 : subtree_mark_and_maybe_prune_rdisp( sched, block );
5209 : /* We do not gate refcnt releases on the entire subtree being
5210 : prunable. In the root notify case there can be in-flight tasks in
5211 : the middle of a subtree, and gating on whole-subtree prunability
5212 : would defer the release of an otherwise prunable block, e.g. an
5213 : inactive sibling of an in-flight block, to a later abandon such as
5214 : a subsequent root notify. That could stall bank reclamation.
5215 :
5216 : The sched fork tree still stays in sync with the banks tree. This
5217 : is to prevent attacks targeting lifetime discrepancy. So while we
5218 : know that the subtree can be pruned from sched at this point, we
5219 : only release refcnts here and stop short of actually pruning, which
5220 : remains solely initiated by banks. */
5221 2 : subtree_release_refcnt( sched, block );
5222 2 : }
5223 :
5224 : /* Release the bank refcnt for every block in the subtree that sched is
5225 : done with. This is evaluated per block. A block's refcnt is
5226 : released as soon as that block individually qualifies, independent of
5227 : whether its siblings or descendants still have in-flight tasks. This
5228 : is safe because the actual pruning is initiated separately by banks,
5229 : and gated on the whole subtree's refcnts reaching zero. Releasing
5230 : refcnts eagerly per block get us there sooner. Blocks that are still
5231 : in-flight will be released when their last task drains. This
5232 : function is idempotent. */
5233 : static void
5234 2 : subtree_release_refcnt( fd_sched_t * sched, fd_sched_block_t * block ) {
5235 2 : FD_TEST( block->in_sched );
5236 2 : if( block->refcnt && block_is_prunable( block ) ) {
5237 2 : FD_TEST( block->dying ); /* The happy path releases the refcnt on full replay. Only bad blocks end up here. */
5238 2 : FD_TEST( !block->block_end_done ); /* Implied by an outstanding refcnt. The happy path releases the refcnt on full replay. */
5239 2 : block->refcnt = 0;
5240 2 : FD_TEST( ref_q_avail( sched->ref_q ) );
5241 2 : ref_q_push_tail( sched->ref_q, block_to_idx( sched, block ) );
5242 2 : if( FD_LIKELY( block->block_start_done ) ) FD_LOG_DEBUG(( "block %lu:%lu replayed partially, releasing refcnt without full replay", block->slot, block_to_idx( sched, block ) ));
5243 2 : else FD_LOG_DEBUG(( "block %lu:%lu replayed nothing, releasing refcnt without any replay", block->slot, block_to_idx( sched, block ) ));
5244 2 : }
5245 :
5246 2 : ulong child_idx = block->child_idx;
5247 2 : while( child_idx!=ULONG_MAX ) {
5248 0 : fd_sched_block_t * child_block = block_pool_ele( sched, child_idx );
5249 0 : subtree_release_refcnt( sched, child_block );
5250 0 : child_idx = child_block->sibling_idx;
5251 0 : }
5252 2 : }
5253 :
5254 : static void
5255 0 : subtree_prune( fd_sched_t * sched, ulong bank_idx, ulong except_idx ) {
5256 0 : fd_sched_block_t * head = block_pool_ele( sched, bank_idx );
5257 0 : head->parent_idx = ULONG_MAX;
5258 0 : fd_sched_block_t * tail = head;
5259 :
5260 0 : while( head ) {
5261 0 : FD_TEST( !head->refcnt );
5262 0 : FD_TEST( head->in_sched );
5263 0 : head->in_sched = 0;
5264 0 : slot_state_close( sched, head );
5265 0 : if( FD_UNLIKELY( block_to_idx( sched, head )==sched->last_active_bank_idx ) ) sched->last_active_bank_idx = ULONG_MAX; /* Don't count a lane switch if the last active block has been pruned. */
5266 :
5267 0 : ulong child_idx = head->child_idx;
5268 0 : while( child_idx!=ULONG_MAX ) {
5269 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
5270 : /* Add children to be visited. We abuse the parent_idx field to
5271 : link up the next block to visit. */
5272 0 : if( child_idx!=except_idx ) {
5273 0 : tail->parent_idx = child_idx;
5274 0 : tail = child;
5275 0 : tail->parent_idx = ULONG_MAX;
5276 0 : }
5277 0 : child_idx = child->sibling_idx;
5278 0 : }
5279 :
5280 : /* Prune the current block. We will never publish halfway into a
5281 : staging lane, because anything that we are publishing away should
5282 : be out of the dispatcher at this point, much less staged.
5283 : - Anything on the rooted fork should have finished replaying
5284 : gracefully and be out of the dispatcher.
5285 : - Anything on minority forks should have been marked dying and
5286 : be taken out of the dispatcher.
5287 :
5288 : There should be no more in-flight tasks either. Prunes are
5289 : initiated by banks, and the refcnt on the bank won't drop to zero
5290 : unless all in-flight tasks have been drained. So the fact that
5291 : we're pruning implies that the bank thinks there's nothing more
5292 : in-flight. */
5293 0 : if( FD_UNLIKELY( block_is_in_flight( head ) ) ) {
5294 0 : FD_LOG_CRIT(( "invariant violation: block has tasks in flight (%u exec %u sigverify %u poh %u cert %u lthash sub, %u/%u lthash add surfaced/done, %u lthash tasks), slot %lu, parent slot %lu",
5295 0 : head->txn_exec_in_flight_cnt, head->txn_sigverify_in_flight_cnt, head->poh_hashing_in_flight_cnt, head->footer_in_flight_cnt, head->sub_in_flight_cnt, head->add_surfaced_cnt, head->add_done_cnt, head->lthash_in_flight_cnt, head->slot, head->parent_slot ));
5296 0 : }
5297 0 : if( FD_UNLIKELY( head->in_rdisp ) ) {
5298 : /* We should have removed it from the dispatcher when we were
5299 : notified of the new root, or when in-flight transactions were
5300 : drained. */
5301 0 : FD_LOG_CRIT(( "invariant violation: block is in the dispatcher, slot %lu, parent slot %lu", head->slot, head->parent_slot ));
5302 0 : }
5303 :
5304 : /* Return remaining mblk descriptors to the shared pool. */
5305 0 : free_mblk_slist( sched, head, head->mblks_unhashed );
5306 0 : free_mblk_slist( sched, head, head->mblks_mixin_in_progress );
5307 :
5308 0 : if( FD_UNLIKELY( !head->block_end_done ) ) {
5309 0 : sched->print_buf_sz = 0UL;
5310 0 : print_block_metrics( sched, head );
5311 0 : if( FD_LIKELY( head->block_start_done ) ) FD_LOG_DEBUG(( "block %lu:%lu replayed partially, pruning without full replay: %s", head->slot, block_to_idx( sched, head ), sched->print_buf ));
5312 0 : else FD_LOG_DEBUG(( "block %lu:%lu replayed nothing, pruning without any replay: %s", head->slot, block_to_idx( sched, head ), sched->print_buf ));
5313 0 : }
5314 :
5315 0 : sched->block_pool_popcnt--;
5316 :
5317 0 : fd_sched_block_t * next = block_pool_ele( sched, head->parent_idx );
5318 :
5319 : /* We don't have to clear the indices here since no one should be
5320 : accessing them. Defensive programming. */
5321 0 : head->parent_idx = ULONG_MAX;
5322 0 : head->child_idx = ULONG_MAX;
5323 0 : head->sibling_idx = ULONG_MAX;
5324 :
5325 0 : head = next;
5326 0 : }
5327 0 : }
5328 :
5329 : static void
5330 0 : maybe_switch_block( fd_sched_t * sched, ulong bank_idx ) {
5331 : /* This only happens rarely when there are dying in-flight blocks.
5332 : Early exit and don't let dying blocks affect replay. */
5333 0 : if( FD_UNLIKELY( bank_idx!=sched->active_bank_idx ) ) return;
5334 :
5335 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
5336 0 : if( FD_UNLIKELY( block_is_done( block ) ) ) {
5337 : /* A successfully replayed block has exactly one subtraction and one
5338 : addition per writable account, net of undone speculations. This
5339 : is the culmination of all the out-of-band LtHash activity in the
5340 : block, and a cheap way for us to check count invariants. */
5341 0 : FD_TEST( block->sub_applied_cnt==block->add_applied_cnt-block->add_undone_cnt );
5342 0 : sched->metrics->lthash_frozen_cnt += block->sub_applied_cnt;
5343 0 : lthash_block_reset( sched, block );
5344 0 : lthash_release_reservation( sched, block );
5345 0 : fd_rdisp_remove_block( sched->rdisp, bank_idx );
5346 0 : FD_LOG_DEBUG(( "block %lu:%lu exited lane %lu: remove", block->slot, bank_idx, block->staging_lane ));
5347 0 : block->in_rdisp = 0;
5348 0 : block->staged = 0;
5349 0 : sched->metrics->block_removed_cnt++;
5350 0 : FD_LOG_DEBUG(( "reset active_bank_idx %lu: remove", sched->active_bank_idx ));
5351 0 : sched->last_active_bank_idx = sched->active_bank_idx;
5352 0 : active_bank_set( sched, ULONG_MAX );
5353 :
5354 : /* Every staged block has a lane to itself, including this block.
5355 : Release the lane and look for the next block to run, which is
5356 : typically a child on another lane. */
5357 0 : sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, (int)block->staging_lane );
5358 0 : sched->staged_head_bank_idx[ block->staging_lane ] = ULONG_MAX;
5359 0 : try_activate_block( sched );
5360 : /* The common case activates a child of the block that just
5361 : finished. Count the uncommon cases. */
5362 0 : int child_activated = sched->active_bank_idx!=ULONG_MAX && block_pool_ele( sched, sched->active_bank_idx )->parent_idx==bank_idx;
5363 0 : sched->metrics->deactivate_no_child_cnt += (uint)!child_activated;
5364 0 : } else if( block_should_deactivate( sched, block ) ) {
5365 : /* We exhausted the active block, but it's not fully done yet. We
5366 : are just not getting FEC sets for it fast enough. This could
5367 : happen when the network path is congested, or when the leader
5368 : simply went down. Reset the active block. */
5369 0 : sched->last_active_bank_idx = sched->active_bank_idx;
5370 0 : active_bank_set( sched, ULONG_MAX );
5371 : /* Nothing of the block is in flight, so no owed account is waiting
5372 : on a writer. A block that still owes additions could not
5373 : speculate on them because the speculation pool is out of entries,
5374 : or because every owed account is over the hotness threshold and
5375 : the block has nothing it may run. The remaining reason not to
5376 : speculate, fec_eos, never reaches this branch. A block with
5377 : fec_eos is dispatched to finish, and then it leaves through the
5378 : branch above. */
5379 0 : if( block->add_owed_cnt && !sched->lthash_spec_free_cnt ) sched->metrics->lthash_pool_full_cnt++;
5380 0 : else sched->metrics->deactivate_no_txn_cnt++;
5381 0 : try_activate_block( sched );
5382 0 : }
5383 0 : }
5384 :
5385 : /* Returns length of the longest stageable unstaged fork, if there is
5386 : one, and 0 otherwise. */
5387 : static ulong
5388 4 : compute_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx ) {
5389 4 : if( FD_UNLIKELY( bank_idx==ULONG_MAX ) ) {
5390 0 : FD_LOG_CRIT(( "invariant violation: bank_idx==ULONG_MAX" ));
5391 0 : }
5392 :
5393 4 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
5394 :
5395 4 : ulong max_child_depth = 0UL;
5396 4 : ulong child_idx = block->child_idx;
5397 6 : while( child_idx!=ULONG_MAX ) {
5398 2 : ulong child_depth = compute_longest_unstaged_fork( sched, child_idx );
5399 2 : if( child_depth > max_child_depth ) {
5400 0 : max_child_depth = child_depth;
5401 0 : }
5402 2 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
5403 2 : child_idx = child->sibling_idx;
5404 2 : }
5405 :
5406 4 : block->luf_depth = max_child_depth + fd_ulong_if( block_is_promotable( block ), 1UL, 0UL );
5407 4 : return block->luf_depth;
5408 4 : }
5409 :
5410 : /* Promotes one block onto lane_idx, the shallowest promotable block of
5411 : the longest stageable unstaged fork under bank_idx, and returns its
5412 : index, or ULONG_MAX if there is no such block. Assumes
5413 : compute_longest_unstaged_fork() ran on the fork tree since it last
5414 : changed. */
5415 : static ulong
5416 0 : promote_from_longest_unstaged_fork( fd_sched_t * sched, ulong bank_idx, int lane_idx ) {
5417 0 : while( bank_idx!=ULONG_MAX ) {
5418 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
5419 0 : if( FD_LIKELY( block_is_promotable( block ) ) ) {
5420 0 : block->staged = 1;
5421 0 : block->staging_lane = (ulong)lane_idx;
5422 0 : fd_rdisp_promote_block( sched->rdisp, bank_idx, block->staging_lane );
5423 0 : sched->staged_bitset = fd_ulong_set_bit( sched->staged_bitset, lane_idx );
5424 : /* No need to update staged_popcnt_wmk because the fact that there
5425 : are unstaged blocks implies we already maxed out lanes at one
5426 : point. */
5427 0 : sched->staged_head_bank_idx[ lane_idx ] = bank_idx;
5428 0 : sched->metrics->block_promoted_cnt++;
5429 0 : sched->metrics->lane_promoted_cnt++;
5430 : /* We never dispatch from an unstaged block, so every transaction
5431 : it ingested since it was last staged got promoted. */
5432 0 : sched->metrics->txn_promoted_cnt += block->txn_parsed_cnt-block->txn_done_cnt;
5433 0 : FD_LOG_DEBUG(( "block %lu:%lu entered lane %lu: promote", block->slot, bank_idx, block->staging_lane ));
5434 : /* The lane map is claimed first so that the pseudo transactions
5435 : the drain creates land in it. A block that hit fec_eos while
5436 : unstaged has not drained yet, so the polls to come drain it. */
5437 0 : if( FD_LIKELY( !block->lthash_inband ) ) lthash_claim_lane_map( sched, block );
5438 0 : lthash_drain_later( sched, block );
5439 0 : return bank_idx;
5440 0 : }
5441 :
5442 : /* Descend into the longest stageable unstaged fork. */
5443 0 : ulong max_depth = 0UL;
5444 0 : ulong best_child_idx = ULONG_MAX;
5445 0 : for( ulong child_idx=block->child_idx; child_idx!=ULONG_MAX; ) {
5446 0 : fd_sched_block_t * child = block_pool_ele( sched, child_idx );
5447 0 : if( child->luf_depth>max_depth ) {
5448 0 : max_depth = child->luf_depth;
5449 0 : best_child_idx = child_idx;
5450 0 : }
5451 0 : child_idx = child->sibling_idx;
5452 0 : }
5453 0 : bank_idx = best_child_idx;
5454 0 : }
5455 0 : return ULONG_MAX;
5456 0 : }
5457 :
5458 : static int
5459 0 : lane_is_demotable( fd_sched_t * sched, int lane_idx ) {
5460 0 : fd_sched_block_t * block = block_pool_ele( sched, sched->staged_head_bank_idx[ lane_idx ] );
5461 0 : FD_TEST( block->staged );
5462 0 : FD_TEST( block->staging_lane==(ulong)lane_idx );
5463 0 : return block_is_demotable( block );
5464 0 : }
5465 :
5466 : static void
5467 0 : demote_lane( fd_sched_t * sched, int lane_idx ) {
5468 0 : ulong bank_idx = sched->staged_head_bank_idx[ lane_idx ];
5469 0 : fd_sched_block_t * block = block_pool_ele( sched, bank_idx );
5470 0 : FD_TEST( block->staged );
5471 0 : FD_TEST( block->staging_lane==(ulong)lane_idx );
5472 :
5473 0 : lthash_demote( sched, block );
5474 0 : int ret = fd_rdisp_demote_block( sched->rdisp, bank_idx );
5475 0 : if( FD_UNLIKELY( ret!=0 ) ) {
5476 0 : FD_LOG_CRIT(( "fd_rdisp_demote_block failed for block %lu:%lu, lane %d", block->slot, bank_idx, lane_idx ));
5477 0 : }
5478 0 : FD_LOG_DEBUG(( "block %lu:%lu exited lane %lu: demote", block->slot, bank_idx, block->staging_lane ));
5479 0 : block->staged = 0;
5480 :
5481 0 : sched->staged_bitset = fd_ulong_clear_bit( sched->staged_bitset, lane_idx );
5482 0 : sched->staged_head_bank_idx[ lane_idx ] = ULONG_MAX;
5483 0 : sched->metrics->block_demoted_cnt++;
5484 0 : }
|