Skip to content

Commit abc17a7

Browse files
fix
1 parent e47b2b8 commit abc17a7

File tree

2 files changed

+79
-108
lines changed

2 files changed

+79
-108
lines changed

src/flamenco/runtime/fd_hashes.c

+78-107
Original file line numberDiff line numberDiff line change
@@ -859,62 +859,43 @@ typedef struct accounts_hash accounts_hash_t;
859859
static ulong
860860
fd_accounts_sorted_subrange_count( fd_funkier_t * funk,
861861
uint range_idx,
862-
uint range_cnt,
863-
fd_spad_t * runtime_spad ) {
862+
uint range_cnt ) {
864863

865864
fd_wksp_t * wksp = fd_funkier_wksp( funk );
866865
ulong num_pairs = 0UL;
867866
ulong range_len = ULONG_MAX/range_cnt;
868867
ulong range_min = range_len*range_idx;
869868
ulong range_max = (range_idx+1U<range_cnt) ? (range_min+range_len-1U) : ULONG_MAX;
870869

871-
FD_SPAD_FRAME_BEGIN( runtime_spad ) {
870+
fd_funkier_all_iter_t iter[1];
871+
for( fd_funkier_all_iter_new( funk, iter );
872+
!fd_funkier_all_iter_done( iter );
873+
fd_funkier_all_iter_next( iter ) ) {
874+
fd_funkier_rec_t const * rec = fd_funkier_all_iter_ele_const( iter );
872875

873-
/* Thread-safe iteration as described in the documentation of fd_map_para.c */
874-
fd_funkier_rec_map_t rec_map = fd_funkier_rec_map( funk, wksp );
875-
ulong lock_cnt = fd_funkier_rec_map_chain_cnt( &rec_map );
876-
ulong * lock_seq = fd_spad_alloc( runtime_spad, alignof(ulong), sizeof(ulong) );
877-
for( ulong lock_idx=0UL; lock_idx<lock_cnt; lock_idx++ ) lock_seq[ lock_idx ] = lock_idx;
878-
879-
fd_funkier_rec_map_iter_lock( &rec_map, lock_seq, lock_cnt, FD_MAP_FLAG_BLOCKING );
880-
881-
for( ulong lock_idx=0UL; lock_idx<lock_cnt; lock_idx++ ) {
882-
/* Process chains in the order they were locked */
883-
ulong chain_idx = lock_seq[ lock_idx ];
884-
885-
for( fd_funkier_rec_map_iter_t iter = fd_funkier_rec_map_iter( &rec_map, chain_idx );
886-
!fd_funkier_rec_map_iter_done( iter );
887-
iter = fd_funkier_rec_map_iter_next( iter ) ) {
888-
fd_funkier_rec_t const * rec = fd_funkier_rec_map_iter_ele_const( iter );
889-
890-
if ( !fd_funkier_key_is_acc( rec->pair.key ) || /* not a solana record */
891-
(rec->flags & FD_FUNKIER_REC_FLAG_ERASE) || /* this is a tombstone */
892-
(rec->pair.xid->ul[0] | rec->pair.xid->ul[1]) != 0 /* not root xid */ ) {
893-
continue;
894-
}
895-
896-
ulong n = __builtin_bswap64( rec->pair.key->ul[0] );
897-
if( n<range_min || n>range_max ) {
898-
continue;
899-
}
900-
901-
fd_account_meta_t * metadata = (fd_account_meta_t *)fd_funkier_val_const( rec, wksp );
902-
int is_empty = (metadata->info.lamports == 0);
903-
if( is_empty ) {
904-
continue;
905-
}
876+
if ( !fd_funkier_key_is_acc( rec->pair.key ) || /* not a solana record */
877+
(rec->flags & FD_FUNKIER_REC_FLAG_ERASE) || /* this is a tombstone */
878+
(rec->pair.xid->ul[0] | rec->pair.xid->ul[1]) != 0 /* not root xid */ ) {
879+
continue;
880+
}
906881

907-
if( (metadata->info.executable & ~1) != 0 ) {
908-
continue;
909-
}
882+
ulong n = __builtin_bswap64( rec->pair.key->ul[0] );
883+
if( n<range_min || n>range_max ) {
884+
continue;
885+
}
910886

911-
num_pairs++;
912-
}
887+
fd_account_meta_t * metadata = (fd_account_meta_t *)fd_funkier_val_const( rec, wksp );
888+
int is_empty = (metadata->info.lamports == 0);
889+
if( is_empty ) {
890+
continue;
891+
}
913892

914-
/* Unlock incrementally */
915-
fd_funkier_rec_map_iter_unlock( &rec_map, lock_seq + lock_idx, 1UL );
893+
if( (metadata->info.executable & ~1) != 0 ) {
894+
continue;
916895
}
917-
} FD_SPAD_FRAME_END;
896+
897+
num_pairs++;
898+
}
918899

919900
return num_pairs;
920901
}
@@ -927,8 +908,7 @@ fd_accounts_sorted_subrange_gather( fd_funkier_t * funk,
927908
fd_lthash_value_t * lthash_values_out,
928909
ulong n0,
929910
fd_pubkey_hash_pair_t * pairs,
930-
fd_features_t *features,
931-
fd_spad_t * runtime_spad ) {
911+
fd_features_t *features ) {
932912

933913
fd_wksp_t * wksp = fd_funkier_wksp( funk );
934914
ulong num_pairs = 0UL;
@@ -938,69 +918,51 @@ fd_accounts_sorted_subrange_gather( fd_funkier_t * funk,
938918

939919
fd_lthash_value_t accum = {0};
940920

941-
FD_SPAD_FRAME_BEGIN( runtime_spad ) {
942-
943-
/* Thread-safe iteration as described in the documentation of fd_map_para.c */
944-
fd_funkier_rec_map_t rec_map = fd_funkier_rec_map( funk, wksp );
945-
ulong lock_cnt = fd_funkier_rec_map_chain_cnt( &rec_map );
946-
ulong * lock_seq = fd_spad_alloc( runtime_spad, alignof(ulong), sizeof(ulong) );
947-
for( ulong lock_idx=0UL; lock_idx<lock_cnt; lock_idx++ ) lock_seq[ lock_idx ] = lock_idx;
948-
949-
fd_funkier_rec_map_iter_lock( &rec_map, lock_seq, lock_cnt, FD_MAP_FLAG_BLOCKING );
950-
951-
for( ulong lock_idx=0UL; lock_idx<lock_cnt; lock_idx++ ) {
952-
/* Process chains in the order they were locked */
953-
ulong chain_idx = lock_seq[ lock_idx ];
954-
955-
for( fd_funkier_rec_map_iter_t iter = fd_funkier_rec_map_iter( &rec_map, chain_idx );
956-
!fd_funkier_rec_map_iter_done( iter );
957-
iter = fd_funkier_rec_map_iter_next( iter ) ) {
958-
fd_funkier_rec_t const * rec = fd_funkier_rec_map_iter_ele_const( iter );
959-
if ( !fd_funkier_key_is_acc( rec->pair.key ) || /* not a solana record */
960-
(rec->flags & FD_FUNKIER_REC_FLAG_ERASE) || /* this is a tombstone */
961-
(rec->pair.xid->ul[0] | rec->pair.xid->ul[1]) != 0 /* not root xid */ ) {
962-
continue;
963-
}
964-
965-
ulong n = __builtin_bswap64( rec->pair.key->ul[0] );
966-
if( n<range_min || n>range_max ) {
967-
continue;
968-
}
969-
fd_account_meta_t * metadata = (fd_account_meta_t *)fd_funkier_val_const( rec, wksp );
970-
int is_empty = (metadata->info.lamports == 0);
971-
if( is_empty ) {
972-
continue;
973-
}
974-
975-
/* FIXME: remove magic number */
976-
uchar hash[32];
977-
fd_lthash_value_t new_lthash_value = {0};
921+
fd_funkier_all_iter_t iter[1];
922+
for( fd_funkier_all_iter_new( funk, iter );
923+
!fd_funkier_all_iter_done( iter );
924+
fd_funkier_all_iter_next( iter ) ) {
925+
fd_funkier_rec_t const * rec = fd_funkier_all_iter_ele_const( iter );
926+
if ( !fd_funkier_key_is_acc( rec->pair.key ) || /* not a solana record */
927+
(rec->flags & FD_FUNKIER_REC_FLAG_ERASE) || /* this is a tombstone */
928+
(rec->pair.xid->ul[0] | rec->pair.xid->ul[1]) != 0 /* not root xid */ ) {
929+
continue;
930+
}
978931

979-
fd_hash_account_current( (uchar *)hash, &new_lthash_value, metadata, rec->pair.key->uc, fd_account_meta_get_data( metadata ), FD_HASH_BOTH_HASHES, features );
980-
fd_lthash_add( &accum, &new_lthash_value );
932+
ulong n = __builtin_bswap64( rec->pair.key->ul[0] );
933+
if( n<range_min || n>range_max ) {
934+
continue;
935+
}
936+
fd_account_meta_t * metadata = (fd_account_meta_t *)fd_funkier_val_const( rec, wksp );
937+
int is_empty = (metadata->info.lamports == 0);
938+
if( is_empty ) {
939+
continue;
940+
}
981941

982-
fd_hash_t * h = (fd_hash_t *)metadata->hash;
983-
if( FD_LIKELY( (h->ul[0] | h->ul[1] | h->ul[2] | h->ul[3]) != 0 ) ) {
984-
if( FD_UNLIKELY( fd_acc_exists( metadata ) && memcmp( metadata->hash, &hash, 32 ) != 0 ) ) {
985-
FD_LOG_WARNING(( "snapshot hash (%s) doesn't match calculated hash (%s)", FD_BASE58_ENC_32_ALLOCA( metadata->hash ), FD_BASE58_ENC_32_ALLOCA( &hash ) ));
986-
}
987-
} else {
988-
fd_memcpy( metadata->hash, &hash, sizeof(fd_hash_t) );
989-
}
942+
/* FIXME: remove magic number */
943+
uchar hash[32];
944+
fd_lthash_value_t new_lthash_value = {0};
990945

991-
if( (metadata->info.executable & ~1) != 0 ) {
992-
continue;
993-
}
946+
fd_hash_account_current( (uchar *)hash, &new_lthash_value, metadata, rec->pair.key->uc, fd_account_meta_get_data( metadata ), FD_HASH_BOTH_HASHES, features );
947+
fd_lthash_add( &accum, &new_lthash_value );
994948

995-
fd_pubkey_hash_pair_t * pair = &pairs[num_pairs++];
996-
pair->rec = rec;
997-
pair->hash = (const fd_hash_t *)metadata->hash;
949+
fd_hash_t * h = (fd_hash_t *)metadata->hash;
950+
if( FD_LIKELY( (h->ul[0] | h->ul[1] | h->ul[2] | h->ul[3]) != 0 ) ) {
951+
if( FD_UNLIKELY( fd_acc_exists( metadata ) && memcmp( metadata->hash, &hash, 32 ) != 0 ) ) {
952+
FD_LOG_WARNING(( "snapshot hash (%s) doesn't match calculated hash (%s)", FD_BASE58_ENC_32_ALLOCA( metadata->hash ), FD_BASE58_ENC_32_ALLOCA( &hash ) ));
998953
}
954+
} else {
955+
fd_memcpy( metadata->hash, &hash, sizeof(fd_hash_t) );
956+
}
999957

1000-
/* Unlock incrementally */
1001-
fd_funkier_rec_map_iter_unlock( &rec_map, lock_seq + lock_idx, 1UL );
958+
if( (metadata->info.executable & ~1) != 0 ) {
959+
continue;
1002960
}
1003-
} FD_SPAD_FRAME_END;
961+
962+
fd_pubkey_hash_pair_t * pair = &pairs[num_pairs++];
963+
pair->rec = rec;
964+
pair->hash = (const fd_hash_t *)metadata->hash;
965+
}
1004966

1005967
sort_pubkey_hash_pair_inplace( pairs, num_pairs );
1006968

@@ -1015,7 +977,6 @@ struct fd_subrange_task_info {
1015977
ulong num_lists;
1016978
fd_pubkey_hash_pair_list_t * lists;
1017979
fd_lthash_value_t * lthash_values;
1018-
fd_spad_t * runtime_spad;
1019980
};
1020981
typedef struct fd_subrange_task_info fd_subrange_task_info_t;
1021982

@@ -1028,7 +989,7 @@ fd_accounts_sorted_subrange_count_task( void *tpool,
1028989
ulong m0, ulong m1 FD_PARAM_UNUSED,
1029990
ulong n0 FD_PARAM_UNUSED, ulong n1 FD_PARAM_UNUSED) {
1030991
fd_subrange_task_info_t * task_info = (fd_subrange_task_info_t *)tpool;
1031-
ulong num_pairs = fd_accounts_sorted_subrange_count( task_info->funk, (uint)m0, (uint)task_info->num_lists, task_info->runtime_spad );
992+
ulong num_pairs = fd_accounts_sorted_subrange_count( task_info->funk, (uint)m0, (uint)task_info->num_lists );
1032993
task_info->lists[m0].pairs_len = num_pairs;
1033994
}
1034995

@@ -1043,7 +1004,7 @@ fd_accounts_sorted_subrange_gather_task( void *tpool,
10431004
fd_subrange_task_info_t * task_info = (fd_subrange_task_info_t *)tpool;
10441005
fd_pubkey_hash_pair_list_t * list = task_info->lists + m0;
10451006
fd_accounts_sorted_subrange_gather( task_info->funk, (uint)m0, (uint)task_info->num_lists,
1046-
&list->pairs_len, task_info->lthash_values, n0, list->pairs, task_info->features, task_info->runtime_spad );
1007+
&list->pairs_len, task_info->lthash_values, n0, list->pairs, task_info->features );
10471008
}
10481009

10491010
int
@@ -1057,6 +1018,14 @@ fd_accounts_hash( fd_funkier_t * funk,
10571018

10581019
FD_LOG_NOTICE(("accounts_hash start"));
10591020

1021+
/* FIXME: this is not the correct lock to use, although in reality this is fine as we never modify
1022+
accounts at the same time as hashing them. Once the hashing has been moved into tiles, we need to
1023+
change it so that we partition by hash chains, and acquire the lock on the individual hash chains. */
1024+
fd_wksp_t * wksp = fd_funkier_wksp( funk );
1025+
fd_funkier_rec_pool_t rec_pool = fd_funkier_rec_pool( funk, wksp );
1026+
fd_funkier_rec_pool_lock( &rec_pool, 1 );
1027+
fd_funkier_txn_start_read( funk );
1028+
10601029
if( tpool == NULL || fd_tpool_worker_cnt( tpool ) <= 1U ) {
10611030
ulong num_pairs = 0UL;
10621031
fd_lthash_value_t * lthash_values = fd_spad_alloc( runtime_spad, FD_LTHASH_VALUE_ALIGN, FD_LTHASH_VALUE_FOOTPRINT );
@@ -1066,7 +1035,7 @@ fd_accounts_hash( fd_funkier_t * funk,
10661035
FD_PUBKEY_HASH_PAIR_ALIGN,
10671036
funk->rec_max * sizeof(fd_pubkey_hash_pair_t) );
10681037

1069-
fd_accounts_sorted_subrange_gather( funk, 0, 1, &num_pairs, lthash_values, 0, pairs, features, runtime_spad );
1038+
fd_accounts_sorted_subrange_gather( funk, 0, 1, &num_pairs, lthash_values, 0, pairs, features );
10701039
if( FD_UNLIKELY( !pairs ) ) {
10711040
FD_LOG_ERR(( "failed to allocate memory for account hash" ));
10721041
}
@@ -1097,7 +1066,6 @@ fd_accounts_hash( fd_funkier_t * funk,
10971066
.num_lists = num_lists,
10981067
.lists = lists,
10991068
.lthash_values = lthash_values,
1100-
.runtime_spad = runtime_spad,
11011069
};
11021070

11031071
fd_tpool_exec_all_rrobin( tpool, 0UL, num_lists, fd_accounts_sorted_subrange_count_task, &task_info,
@@ -1126,6 +1094,9 @@ fd_accounts_hash( fd_funkier_t * funk,
11261094
} else
11271095
FD_LOG_NOTICE(( "accounts_hash %s", FD_BASE58_ENC_32_ALLOCA( accounts_hash->hash ) ));
11281096

1097+
fd_funkier_rec_pool_unlock( &rec_pool );
1098+
fd_funkier_txn_end_read( funk );
1099+
11291100
return 0;
11301101
}
11311102

src/funkier/fd_funkier.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fd_funkier_delete( void * shfunk ) {
207207
/* Thread-safe iteration as described in the documentation of fd_map_para.c */
208208
fd_funkier_rec_map_t rec_map = fd_funkier_rec_map( funk, wksp );
209209
ulong lock_cnt = fd_funkier_rec_map_chain_cnt( &rec_map );
210-
ulong * lock_seq = (ulong *)fd_alloc_malloc( alloc, alignof(ulong), 1UL );
210+
ulong * lock_seq = (ulong *)fd_alloc_malloc( alloc, alignof(ulong), sizeof(ulong) * lock_cnt );
211211
for( ulong lock_idx=0UL; lock_idx<lock_cnt; lock_idx++ ) {
212212
lock_seq[ lock_idx ] = lock_idx;
213213
}

0 commit comments

Comments
 (0)