Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accounts-db: send batches of accounts to the background hasher #810

Merged
merged 1 commit into from
Apr 19, 2024

Conversation

alessandrod
Copy link

Instead of sending accounts individually, send batches of accounts for background hashing.

Before this change we used to send accounts individually, and the background thread used to do:

loop {
    let account = receiver.recv();
    account.hash();
    // go back to sleep in recv()
}

Because most accounts are small and hashing them is very fast, the background thread used to sleep a lot, and required many syscalls from the sender in order to be woken up.

Batching reduces the number of syscalls.

@alessandrod
Copy link
Author

This is what I observed running 4 bench-tps against a local validator node:

Top left is master, bottom right is with the patch:

Screen Shot 2024-04-15 at 6 16 53 pm Screen Shot 2024-04-15 at 6 18 12 pm

Profile before:

Screen Shot 2024-04-15 at 6 19 22 pm

Profile after:

Screen Shot 2024-04-15 at 6 32 23 pm

@apfitzge
Copy link

So in bench-tps, the accounts are very small which might be biasing the results here. Any idea what this might look like wiht larger accounts under normal validator operations?

Also, do you know how often we are sending these batches? does it get any queueing behavior where while we are working on a batch there is another now batch in the channel? If so, wonder the effect of doing a slightly more complicated loop structure on recv side, similar to something we do for new banking stage scheduler:

loop {
    let batch = receiver.recv()?; // blocking receive call
    // chain the initially received batch with non-blocking calls to try_recv
    // can do this manually, or with one-liner below.
    for batch in std::iter::once(batch).chain(receiver.try_iter()) { 
		// ... hashing
	}
}

If we are receiving multiple batches at similar times this may eliminate some additional sleeps?

@alessandrod
Copy link
Author

So in bench-tps, the accounts are very small which might be biasing the results here. Any idea what this might look like wiht larger accounts under normal validator operations?

I haven't been able to profile banking of a mnb validator yet, the staked devboxes get a slot every 8+ hrs so can't be really used for profiling. I'm hoping to get access to a juicier validator sometime this week.

Also, do you know how often we are sending these batches? does it get any queueing behavior where while we are working on a batch there is another now batch in the channel?

Yeah that's the hope: we send one account batch for each executed tx batch, so many of them should be pretty close in time.

If so, wonder the effect of doing a slightly more complicated loop structure on recv side, similar to something we do for new banking stage scheduler:

loop {
    let batch = receiver.recv()?; // blocking receive call
    // chain the initially received batch with non-blocking calls to try_recv
    // can do this manually, or with one-liner below.
    for batch in std::iter::once(batch).chain(receiver.try_iter()) { 
		// ... hashing
	}
}

If we are receiving multiple batches at similar times this may eliminate some additional sleeps?

As discussed on discord yes conceptually this is a good idea, and in fact crossbeam does it internally already to try and save the syscall cost.

@apfitzge
Copy link

As discussed on discord yes conceptually this is a good idea, and in fact crossbeam does it internally already to try and save the syscall cost.

Yeah. Saw the similar pattern and probably jumped the gun with my suggestion. After looking at the code I was referring there was some additional context I was keeping only when we had back-to-back try_recvs instead of re-getting it every recv call. That doesn't exist here, so the suggestion is indeed pointless.

Instead of sending accounts individually, send batches of accounts for
background hashing.

Before this change we used to send accounts individually, and the
background thread used to do:

    loop {
        let account = receiver.recv();
        account.hash();
        // go back to sleep in recv()
    }

Because most accounts are small and hashing them is very fast, the
background thread used to sleep a lot, and required many syscalls from
the sender in order to be woken up.

Batching reduces the number of syscalls.
@alessandrod alessandrod force-pushed the batch-bg-hash-master branch from 16d258d to 2ce3324 Compare April 19, 2024 03:24
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.9%. Comparing base (f0e87f6) to head (2ce3324).
Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #810       +/-   ##
===========================================
+ Coverage        0    81.9%    +81.9%     
===========================================
  Files           0      853      +853     
  Lines           0   231781   +231781     
===========================================
+ Hits            0   189855   +189855     
- Misses          0    41926    +41926     

@jeffwashington jeffwashington self-requested a review April 19, 2024 16:28
Copy link

@jeffwashington jeffwashington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
Yes, this looks amazing.

@alessandrod alessandrod merged commit c2936eb into anza-xyz:master Apr 19, 2024
38 checks passed
Copy link

@brooksprumo brooksprumo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge review: lgtm, thanks!

@cavemanloverboy
Copy link

pls
solana-labs#34605

@alessandrod alessandrod deleted the batch-bg-hash-master branch May 18, 2024 06:18
@alessandrod
Copy link
Author

pls solana-labs#34605

Oops, sorry about that, no clue your PR existed. Personally i can’t really keep track of GitHub notifications, so if your PR goes ignored for more than a few days, I recommend nagging people on discord until you get some kind of acknowledgement.

@cavemanloverboy
Copy link

all good. no worries. got another PR coming this weekend at the request of someone at fndn

@cavemanloverboy
Copy link

glad this got in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants