Line data Source code
1 : #ifndef HEADER_fd_src_ballet_pack_fd_pack_pacing_h 2 : #define HEADER_fd_src_ballet_pack_fd_pack_pacing_h 3 : 4 : /* One of the keys to packing well is properly pacing CU consumption. 5 : Without pacing, pack will end up filling the block with non-ideal 6 : transactions. Since at the current limits, the banks can execute a 7 : block worth of CUs in a fraction of the block time, without pacing, 8 : any lucrative transactions that arrive at towards the end of a block 9 : will have to be delayed until the next block (or another leader's 10 : block if it's the last in our rotation). */ 11 : 12 : 13 : struct fd_pack_pacing_private { 14 : /* Start and end time of block in ticks */ 15 : long t_start; 16 : long t_end; 17 : /* Number of CUs in the block */ 18 : float max_cus; 19 : 20 : float ticks_per_cu; 21 : float remaining_cus; 22 : }; 23 : 24 : typedef struct fd_pack_pacing_private fd_pack_pacing_t; 25 : 26 : 27 : /* fd_pack_pacing_init begins pacing for a slot which starts at now and 28 : ends at t_end (both measured in fd_tickcount() space) and will 29 : contain cus CUs. cus in (0, 2^32). t_end - t_start should be about 30 : 400ms or less, but must be in (0, 2^32) as well. */ 31 : static inline void 32 : fd_pack_pacing_init( fd_pack_pacing_t * pacer, 33 : long t_start, 34 : long t_end, 35 : float ticks_per_ns, 36 0 : ulong max_cus ) { 37 : 38 0 : pacer->t_start = t_start; 39 0 : pacer->t_end = t_end; 40 : 41 : /* Time per CU depends on the hardware, the transaction mix, what 42 : fraction of the transactions land, etc. It's hard to just come up 43 : with a value, but a small sample says 9 ns/CU is in the right 44 : ballpark. */ 45 0 : pacer->ticks_per_cu = 9.0f * ticks_per_ns; 46 : 47 : /* Originally, we had all the lines ending 5% before t_end, but the 48 : better thing to do is to adjust max_cus up so that the 1 bank line 49 : ends 5% before t_end. */ 50 0 : pacer->max_cus = (float)max_cus + 0.05f * (float)(t_end-t_start)/pacer->ticks_per_cu; 51 0 : pacer->remaining_cus = pacer->max_cus; 52 0 : } 53 : 54 : /* fd_pack_pacing_update_consumed_cus notes that the instantaneous value 55 : of consumed CUs may have updated. pacer must be a local join. 56 : consumed_cus should be below the value of max_cus but it's treated as 57 : max_cus if it's larger. Now should be the time (in fd_tickcount 58 : space) at which the measurement was taken. */ 59 : static inline void 60 : fd_pack_pacing_update_consumed_cus( fd_pack_pacing_t * pacer, 61 : ulong consumed_cus, 62 0 : long now ) { 63 : /* Keep this function separate so in the future we can learn the 64 : ticks_per_cu rate. */ 65 0 : (void)now; 66 : /* It's possible (but unlikely) that consumed_cus can be greater than 67 : max_cus, so clamp the value at 0 */ 68 0 : pacer->remaining_cus = fmaxf( pacer->max_cus-(float)consumed_cus, 0.0f ); 69 0 : } 70 : 71 : 72 : /* fd_pack_pacing_enabled_bank_cnt computes how many banks should be 73 : active at time `now` (in fd_tickcount space) given the most recent 74 : value specified for consumed CUs. The returned value may be 0, which 75 : indicates that no banks should be active at the moment. It may also 76 : be higher than the number of available banks, which should be 77 : interpreted as all banks being enabled. */ 78 : FD_FN_PURE static inline ulong 79 : fd_pack_pacing_enabled_bank_cnt( fd_pack_pacing_t const * pacer, 80 0 : long now ) { 81 : /* We want to use as few banks as possible to fill the block in 400 82 : milliseconds. That way we pass up the best transaction because it 83 : conflicts with something actively running as infrequently as 84 : possible. To do that, we draw lines through in the time-CU plane 85 : that pass through approximately (400 milliseconds, 48M CUs) with 86 : slope k*(single bank speed), where k varies between 1 and the 87 : number of bank tiles configured. This splits the plane into 88 : several regions, and the region we are in tells us how many bank 89 : tiles to use. 90 : 91 : 92 : 48M - / /| 93 : | / / / 94 : | / // | 95 : U | / / / / 96 : s | 0 banks active / / / | 97 : e | / / / / 98 : d | / e / / | 99 : | / k v / / / 100 : C | / n i / / | 101 : U | / a t / / / 102 : s | / B c / / | 103 : | / 1 a / 2 Banks / / 104 : | / / active / ... | 105 : 0 |-------------------------------------------------------- 106 : 0 ms 400ms 107 : */ 108 : /* We want to be pretty careful with the math here. We want to make 109 : sure we never divide by 0, so clamp the denominator at 1. The 110 : numerator is non-negative. Ticks_per_cu is between 1 and 100, so 111 : it'll always fit in a ulong. */ 112 0 : return (ulong)(pacer->remaining_cus/ 113 0 : (float)(fd_long_max( 1L, pacer->t_end - now )) * pacer->ticks_per_cu ); 114 0 : } 115 : 116 : #endif /* HEADER_fd_src_ballet_pack_fd_pack_pacing_h */