Skip to content

Commit 04bc6f5

Browse files
committed
wip
1 parent 5135f08 commit 04bc6f5

File tree

5 files changed

+108
-26
lines changed

5 files changed

+108
-26
lines changed

src/ballet/chacha/fd_chacha_rng.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
fd_rng is a better choice in all other cases. */
77

88
#include "fd_chacha.h"
9-
#if !FD_HAS_INT128
109
#include "../../util/bits/fd_uwide.h"
11-
#endif
1210

1311
/* FD_CHACHA_RNG_DEBUG controls debug logging. 0 is off; 1 is on. */
1412

@@ -231,16 +229,9 @@ fd_chacha20_rng_ulong_roll( fd_chacha_rng_t * rng,
231229
(n << (63 - fd_ulong_find_msb( n ) )) - 1UL );
232230

233231
for( int i=0; 1; i++ ) {
234-
ulong v = fd_chacha20_rng_ulong( rng );
235-
#if FD_HAS_INT128
236-
/* Compiles to one mulx instruction */
237-
uint128 res = (uint128)v * (uint128)n;
238-
ulong hi = (ulong)(res>>64);
239-
ulong lo = (ulong) res;
240-
#else
232+
ulong v = fd_chacha20_rng_ulong( rng );
241233
ulong hi, lo;
242234
fd_uwide_mul( &hi, &lo, v, n );
243-
#endif
244235

245236
# if FD_CHACHA_RNG_DEBUG
246237
FD_LOG_DEBUG(( "roll (attempt %d): n=%016lx zone: %016lx v=%016lx lo=%016lx hi=%016lx", i, n, zone, v, lo, hi ));
@@ -260,16 +251,9 @@ fd_chacha8_rng_ulong_roll( fd_chacha_rng_t * rng,
260251
(n << (63 - fd_ulong_find_msb( n ) )) - 1UL );
261252

262253
for( int i=0; 1; i++ ) {
263-
ulong v = fd_chacha8_rng_ulong( rng );
264-
#if FD_HAS_INT128
265-
/* Compiles to one mulx instruction */
266-
uint128 res = (uint128)v * (uint128)n;
267-
ulong hi = (ulong)(res>>64);
268-
ulong lo = (ulong) res;
269-
#else
254+
ulong v = fd_chacha8_rng_ulong( rng );
270255
ulong hi, lo;
271256
fd_uwide_mul( &hi, &lo, v, n );
272-
#endif
273257

274258
# if FD_CHACHA_RNG_DEBUG
275259
FD_LOG_DEBUG(( "roll (attempt %d): n=%016lx zone: %016lx v=%016lx lo=%016lx hi=%016lx", i, n, zone, v, lo, hi ));

src/ballet/wsample/test_wsample.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,11 @@ test_matches_solana( void ) {
214214
ulong weights2[18] = { 78, 70, 38, 27, 21, 82, 42, 21, 77, 77, 17, 4, 50, 96, 83, 33, 16, 72 };
215215

216216
memset( zero_seed, 48, 32UL );
217-
fd_chacha20_rng_init( rng, zero_seed );
218-
219217
partial = fd_wsample_new_init( _shmem, rng, 18UL, 0, FD_WSAMPLE_HINT_FLAT );
220218
for( ulong i=0UL; i<18UL; i++ ) partial = fd_wsample_new_add( partial, weights2[i] );
221219
tree = fd_wsample_join( fd_wsample_new_fini( partial, 0UL ) );
222220
fd_wsample_seed_rng( tree, zero_seed, 0 /* use_chacha8 */ );
223221

224-
225222
FD_TEST( fd_wsample_sample_and_remove( tree ) == 9UL );
226223
FD_TEST( fd_wsample_sample_and_remove( tree ) == 3UL );
227224
FD_TEST( fd_wsample_sample_and_remove( tree ) == 12UL );
@@ -245,6 +242,66 @@ test_matches_solana( void ) {
245242
fd_chacha_rng_delete( fd_chacha_rng_leave( rng ) );
246243
}
247244

245+
static void
246+
test_matches_solana_chacha8( void ) {
247+
/* Adopted from test_repeated_leader_schedule_specific: */
248+
fd_chacha_rng_t _rng[1];
249+
fd_chacha_rng_t * rng = fd_chacha_rng_join( fd_chacha_rng_new( _rng, FD_CHACHA_RNG_MODE_MOD ) );
250+
uchar zero_seed[32] = {0};
251+
252+
void * partial = fd_wsample_new_init( _shmem, rng, 2UL, 0, FD_WSAMPLE_HINT_FLAT );
253+
fd_wsample_t * tree = fd_wsample_join( fd_wsample_new_fini( fd_wsample_new_add( fd_wsample_new_add( partial, 2UL ), 1UL ), 0UL ) );
254+
fd_wsample_seed_rng( tree, zero_seed, 1 /* use_chacha8 */ );
255+
256+
FD_TEST( fd_wsample_sample( tree ) == 1UL );
257+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
258+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
259+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
260+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
261+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
262+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
263+
FD_TEST( fd_wsample_sample( tree ) == 0UL );
264+
265+
fd_wsample_delete( fd_wsample_leave( tree ) );
266+
fd_chacha_rng_delete( fd_chacha_rng_leave( rng ) );
267+
268+
rng = fd_chacha_rng_join( fd_chacha_rng_new( _rng, FD_CHACHA_RNG_MODE_SHIFT ) );
269+
270+
/* Adopted from test_weighted_shuffle_hard_coded, except they handle
271+
the special case for 0 weights inside their WeightedShuffle object,
272+
and the test case initially used i32 as weights, which made their
273+
Chacha20 object generate i32s instead of u64s. */
274+
ulong weights2[18] = { 78, 70, 38, 27, 21, 82, 42, 21, 77, 77, 17, 4, 50, 96, 83, 33, 16, 72 };
275+
276+
memset( zero_seed, 48, 32UL );
277+
partial = fd_wsample_new_init( _shmem, rng, 18UL, 0, FD_WSAMPLE_HINT_FLAT );
278+
for( ulong i=0UL; i<18UL; i++ ) partial = fd_wsample_new_add( partial, weights2[i] );
279+
tree = fd_wsample_join( fd_wsample_new_fini( partial, 0UL ) );
280+
fd_wsample_seed_rng( tree, zero_seed, 1 /* use_chacha8 */ );
281+
282+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 13UL );
283+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 8UL );
284+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 6UL );
285+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 14UL );
286+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 0UL );
287+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 17UL );
288+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 1UL );
289+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 12UL );
290+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 3UL );
291+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 16UL );
292+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 5UL );
293+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 15UL );
294+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 9UL );
295+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 2UL );
296+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 4UL );
297+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 7UL );
298+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 10UL );
299+
FD_TEST( fd_wsample_sample_and_remove( tree ) == 11UL );
300+
301+
fd_wsample_delete( fd_wsample_leave( tree ) );
302+
fd_chacha_rng_delete( fd_chacha_rng_leave( rng ) );
303+
}
304+
248305
static void
249306
test_sharing( void ) {
250307
fd_chacha_rng_t _rng[1];
@@ -472,6 +529,7 @@ main( int argc,
472529
FD_TEST( fd_wsample_footprint( MAX, 1 )<MAX_FOOTPRINT );
473530

474531
test_matches_solana();
532+
test_matches_solana_chacha8();
475533
test_map();
476534
test_sharing();
477535
test_restore_disabled();

src/disco/shred/fd_shred_tile.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ typedef struct {
260260
uchar block_ids[ BLOCK_IDS_TABLE_CNT ][ FD_SHRED_MERKLE_ROOT_SZ ];
261261
} fd_shred_ctx_t;
262262

263+
/* shred features are generally considered active at the epoch *following*
264+
the epoch in which the feature gate is activated.
265+
fd_shred_check_feature_activation() performs the check and it's equivalent
266+
to the rust functions check_feature_activation():
267+
https://github.com/anza-xyz/agave/blob/v3.1.4/turbine/src/cluster_nodes.rs#L771
268+
https://github.com/anza-xyz/agave/blob/v3.1.4/core/src/shred_fetch_stage.rs#L456 */
269+
static inline int
270+
fd_shred_check_feature_activation( ulong shred_slot, ulong activation_slot ) {
271+
//TODO: correct logic based on epochs
272+
return shred_slot >= activation_slot;
273+
}
274+
263275
FD_FN_CONST static inline ulong
264276
scratch_align( void ) {
265277
return 128UL;
@@ -920,7 +932,7 @@ after_frag( fd_shred_ctx_t * ctx,
920932
the shred, but still send it to the blockstore. */
921933
fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, shred->slot );
922934
if( FD_UNLIKELY( !sdest ) ) break;
923-
int use_chacha8 = shred->slot >= ctx->features_activation->switch_to_chacha8_turbine;
935+
int use_chacha8 = fd_shred_check_feature_activation( shred->slot, ctx->features_activation->switch_to_chacha8_turbine );
924936
fd_shred_dest_idx_t * dests = fd_shred_dest_compute_children( sdest, &shred, 1UL, ctx->scratchpad_dests, 1UL, fanout, fanout, max_dest_cnt, use_chacha8 );
925937
if( FD_UNLIKELY( !dests ) ) break;
926938

@@ -1121,7 +1133,7 @@ after_frag( fd_shred_ctx_t * ctx,
11211133
if( FD_UNLIKELY( !k ) ) return;
11221134
fd_shred_dest_t * sdest = fd_stake_ci_get_sdest_for_slot( ctx->stake_ci, new_shreds[ 0 ]->slot );
11231135
if( FD_UNLIKELY( !sdest ) ) return;
1124-
int use_chacha8 = new_shreds[ 0 ]->slot >= ctx->features_activation->switch_to_chacha8_turbine;
1136+
int use_chacha8 = fd_shred_check_feature_activation( new_shreds[ 0 ]->slot, ctx->features_activation->switch_to_chacha8_turbine );
11251137

11261138
ulong out_stride;
11271139
ulong max_dest_cnt[1];

src/disco/shred/test_shred_dest.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,13 @@ test_performance( void ) {
478478
}
479479

480480
dt = -fd_log_wallclock();
481-
#define TEST_CNT 1000000
481+
#define TEST_CNT 100000
482482
for( ulong j=0UL; j<TEST_CNT; j++ ) {
483483
shred[0].idx = (uint)j;
484484
FD_TEST( fd_shred_dest_compute_children( sdest, shred_ptr, 1UL, result, 1UL, 200UL, 200UL, max_dest_cnt, 0 /*use_chacha8*/ ) );
485485
}
486486
dt += fd_log_wallclock();
487-
FD_LOG_NOTICE(( "Compute children (1 shred/batch): %.2f ns/shred", (double)dt / (double)TEST_CNT ));
487+
FD_LOG_NOTICE(( "Compute children (chacha20 1 shred/batch): %.2f ns/shred", (double)dt / (double)TEST_CNT ));
488488

489489
dt = -fd_log_wallclock();
490490
#undef TEST_CNT
@@ -494,7 +494,28 @@ test_performance( void ) {
494494
FD_TEST( fd_shred_dest_compute_children( sdest, shred_ptr, 16UL, result, 16UL, 200UL, 200UL, max_dest_cnt, 0 /*use_chacha8*/ ) );
495495
}
496496
dt += fd_log_wallclock();
497-
FD_LOG_NOTICE(( "Compute children (16 shred/batch): %.2f ns/shred", (double)dt / (double)(16UL*TEST_CNT) ));
497+
FD_LOG_NOTICE(( "Compute children (chacha20 16 shred/batch): %.2f ns/shred", (double)dt / (double)(16UL*TEST_CNT) ));
498+
#undef TEST_CNT
499+
500+
dt = -fd_log_wallclock();
501+
#undef TEST_CNT
502+
#define TEST_CNT 100000
503+
for( ulong j=0UL; j<TEST_CNT; j++ ) {
504+
shred[0].idx = (uint)j;
505+
FD_TEST( fd_shred_dest_compute_children( sdest, shred_ptr, 1UL, result, 1UL, 200UL, 200UL, max_dest_cnt, 1 /*use_chacha8*/ ) );
506+
}
507+
dt += fd_log_wallclock();
508+
FD_LOG_NOTICE(( "Compute children (chacha8 1 shred/batch): %.2f ns/shred", (double)dt / (double)TEST_CNT ));
509+
510+
dt = -fd_log_wallclock();
511+
#undef TEST_CNT
512+
#define TEST_CNT 10000
513+
for( ulong j=0UL; j<TEST_CNT; j++ ) {
514+
for( ulong k=0UL; k<16UL; k++ ) shred[k].idx = (uint)(j*16UL+k);
515+
FD_TEST( fd_shred_dest_compute_children( sdest, shred_ptr, 16UL, result, 16UL, 200UL, 200UL, max_dest_cnt, 1 /*use_chacha8*/ ) );
516+
}
517+
dt += fd_log_wallclock();
518+
FD_LOG_NOTICE(( "Compute children (chacha8 16 shred/batch): %.2f ns/shred", (double)dt / (double)(16UL*TEST_CNT) ));
498519
#undef TEST_CNT
499520
}
500521

src/util/bits/fd_uwide.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ static inline void
8888
fd_uwide_mul( ulong * FD_RESTRICT _zh, ulong * FD_RESTRICT _zl,
8989
ulong x,
9090
ulong y ) {
91+
#if FD_HAS_INT128
92+
/* Compiles to one mulx instruction */
93+
uint128 res = (uint128)x * (uint128)y;
94+
*_zh = (ulong)(res>>64);
95+
*_zl = (ulong) res;
96+
#else
9197
ulong x1 = x>>32; ulong x0 = (ulong)(uint)x; /* both 2^32-1 @ worst case (x==y==2^64-1) */
9298
ulong y1 = y>>32; ulong y0 = (ulong)(uint)y; /* both 2^32-1 @ worst case */
9399

@@ -103,6 +109,7 @@ fd_uwide_mul( ulong * FD_RESTRICT _zh, ulong * FD_RESTRICT _zl,
103109
/* zh 2^64 + zl == 2^128-2^65+1 @ worst case */
104110

105111
*_zh = zh; *_zl = zl;
112+
#endif
106113
}
107114

108115
/* fd_uwide_find_msb returns floor( log2 <xh,xl> ) exactly. Assumes

0 commit comments

Comments
 (0)