|
| 1 | +#include "fd_txn_account.h" |
| 2 | +#include "../../funk/fd_funk.h" |
| 3 | + |
| 4 | +#define NUM_ACCOUNTS (ulong)1000000 |
| 5 | + |
| 6 | +fd_pubkey_t random_pubkeys[NUM_ACCOUNTS]; |
| 7 | + |
| 8 | +struct fd_create_account_task_info { |
| 9 | + fd_funk_t * funk; |
| 10 | + fd_funk_txn_t * funk_txn; |
| 11 | +}; |
| 12 | +typedef struct fd_create_account_task_info fd_create_account_task_info_t; |
| 13 | + |
| 14 | +static void |
| 15 | +create_account( fd_funk_t * funk, |
| 16 | + fd_funk_txn_t * funk_txn, |
| 17 | + ulong i ) { |
| 18 | + FD_TXN_ACCOUNT_DECL( rec ); |
| 19 | + int res = fd_txn_account_init_from_funk_mutable( rec, &random_pubkeys[i], funk, funk_txn, 1, 0 ); |
| 20 | + FD_TEST(res == 0); |
| 21 | + fd_txn_account_mutable_fini(rec, funk, funk_txn); |
| 22 | +} |
| 23 | + |
| 24 | +static void |
| 25 | +benchmark_account_creation_single_threaded( fd_funk_t * funk, |
| 26 | + fd_funk_txn_t * funk_txn ) { |
| 27 | + for( ulong i=0; i < NUM_ACCOUNTS; i++) { |
| 28 | + create_account( funk, funk_txn, i ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +static void |
| 33 | +create_account_task( void * tpool, |
| 34 | + ulong t0, ulong t1, |
| 35 | + void *args FD_PARAM_UNUSED, |
| 36 | + void *reduce FD_PARAM_UNUSED, ulong stride FD_PARAM_UNUSED, |
| 37 | + ulong l0 FD_PARAM_UNUSED, ulong l1 FD_PARAM_UNUSED, |
| 38 | + ulong m0 FD_PARAM_UNUSED, ulong m1 FD_PARAM_UNUSED, |
| 39 | + ulong n0 FD_PARAM_UNUSED, ulong n1 FD_PARAM_UNUSED) { |
| 40 | + fd_create_account_task_info_t * task_info = (fd_create_account_task_info_t *)tpool; |
| 41 | + for( ulong i = t0; i < t1; i++ ) { |
| 42 | + create_account( task_info->funk, task_info->funk_txn, i ); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +static void |
| 47 | +benchmark_account_creation( fd_funk_t * funk, |
| 48 | + fd_funk_txn_t * funk_txn, |
| 49 | + uint num_threads, |
| 50 | + fd_tpool_t * tpool ) { |
| 51 | + if( num_threads==1 ) { |
| 52 | + benchmark_account_creation_single_threaded(funk, funk_txn ); |
| 53 | + } |
| 54 | + else { |
| 55 | + fd_create_account_task_info_t task_info = { |
| 56 | + .funk = funk, |
| 57 | + .funk_txn = funk_txn |
| 58 | + }; |
| 59 | + |
| 60 | + ulong cnt_per_worker = NUM_ACCOUNTS / (num_threads); |
| 61 | + for( ulong worker_idx=1UL; worker_idx<num_threads+1; worker_idx++ ) { |
| 62 | + ulong start_idx = (worker_idx-1UL) * cnt_per_worker; |
| 63 | + ulong end_idx = worker_idx!=num_threads+1 ? fd_ulong_sat_sub( start_idx + cnt_per_worker, 1UL ) : fd_ulong_sat_sub( NUM_ACCOUNTS, 1UL ); |
| 64 | + fd_tpool_exec(tpool, worker_idx, create_account_task, &task_info, start_idx, end_idx, NULL, NULL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL ); |
| 65 | + } |
| 66 | + |
| 67 | + for( ulong worker_idx=1UL; worker_idx<num_threads+1; worker_idx++ ) { |
| 68 | + fd_tpool_wait( tpool, worker_idx ); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +int main( int argc, |
| 74 | + char ** argv ) { |
| 75 | + fd_boot( &argc, &argv ); |
| 76 | + |
| 77 | + char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" ); |
| 78 | + ulong page_cnt = fd_env_strip_cmdline_ulong ( &argc, &argv, "--page-cnt", NULL, 50UL ); |
| 79 | + ulong near_cpu = fd_env_strip_cmdline_ulong ( &argc, &argv, "--near-cpu", NULL, fd_log_cpu_id() ); |
| 80 | + uint num_threads = fd_env_strip_cmdline_uint ( &argc, &argv, "--num-threads", NULL, 1 ); |
| 81 | + |
| 82 | + /* setup rng */ |
| 83 | + fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) ); |
| 84 | + |
| 85 | + FD_LOG_NOTICE(( "Creating workspace (--page-cnt %lu, --page-sz %s)", page_cnt, _page_sz )); |
| 86 | + fd_wksp_t * wksp = fd_wksp_new_anonymous( fd_cstr_to_shmem_page_sz( _page_sz ), page_cnt, near_cpu, "wksp", 0UL ); |
| 87 | + FD_TEST( wksp ); |
| 88 | + |
| 89 | + ulong const txn_max = 16UL; |
| 90 | + ulong const rec_max = 2000000UL; |
| 91 | + ulong const funk_seed = 0xeffb398d4552afbcUL; |
| 92 | + ulong const funk_tag = 42UL; |
| 93 | + |
| 94 | + /* init funk */ |
| 95 | + fd_funk_t * funk = fd_funk_join( fd_funk_new( fd_wksp_alloc_laddr( wksp, fd_funk_align(), fd_funk_footprint( txn_max, rec_max ), funk_tag ), funk_tag, funk_seed, txn_max, rec_max ) ); |
| 96 | + FD_TEST( funk ); |
| 97 | + |
| 98 | + /* create funk txn */ |
| 99 | + fd_funk_txn_xid_t xid = { 0 }; |
| 100 | + fd_funk_txn_t * funk_txn = fd_funk_txn_prepare(funk, NULL, &xid, 1); |
| 101 | + |
| 102 | + /* setup tpool */ |
| 103 | + uchar _tpool[ FD_TPOOL_FOOTPRINT(FD_TILE_MAX) ] __attribute__((aligned(FD_TPOOL_ALIGN))); |
| 104 | + ulong worker_cnt = num_threads+1; |
| 105 | + fd_tpool_t * tpool = fd_tpool_init( _tpool, worker_cnt ); |
| 106 | + if( tpool == NULL ) { |
| 107 | + FD_LOG_ERR(( "failed to create thread pool" )); |
| 108 | + } |
| 109 | + |
| 110 | + if( worker_cnt > 2 ) { |
| 111 | + for( ulong i=1UL; i<worker_cnt; i++) { |
| 112 | + if( fd_tpool_worker_push( tpool, i, NULL, 0UL ) == NULL ) { |
| 113 | + FD_LOG_ERR(( "failed to launch worker" )); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /* set up some random pubkeys */ |
| 119 | + for( ulong i=0; i < NUM_ACCOUNTS; i++ ) { |
| 120 | + for( ulong j = 0; j < sizeof(fd_pubkey_t); j++ ) { |
| 121 | + random_pubkeys[i].uc[j] = fd_rng_uchar( rng ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /* create accounts */ |
| 126 | + long dt = -fd_log_wallclock(); |
| 127 | + benchmark_account_creation( funk, funk_txn, num_threads, tpool ); |
| 128 | + dt += fd_log_wallclock(); |
| 129 | + |
| 130 | + FD_LOG_NOTICE(( "Inserted %lu accounts in %.2fs (rate=%.2g per_item=%.0fns)", |
| 131 | + NUM_ACCOUNTS, (double)dt/1e9, |
| 132 | + (double)NUM_ACCOUNTS/( (double)dt/1e9 ), |
| 133 | + (double)dt/(double)NUM_ACCOUNTS )); |
| 134 | + |
| 135 | + /* clean up */ |
| 136 | + fd_rng_delete( fd_rng_leave( rng ) ); |
| 137 | + fd_tpool_fini( tpool ); |
| 138 | + fd_halt(); |
| 139 | + return 0; |
| 140 | +} |
0 commit comments