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

[Qt] Reduce a significant cs_main lock freeze #10231

Merged
merged 5 commits into from Apr 20, 2017

Conversation

jonasschnelli
Copy link
Contributor

@jonasschnelli jonasschnelli commented Apr 19, 2017

I can't remember why we added this in the first place: but in current master, we request a cs_main lock (not a try lock) every block tip update in order to get the best headers height / time.
This results in significant freezes in the GUI during IBD/catch-up.

This PR adds two atomic caches for the best headers height and time.

If we do a 0.14.1rc3, we should consider adding this.

@TheBlueMatt
Copy link
Contributor

Can the GUI instead cache the current best tip? This may help with @ryanofsky's process split as well.

@jonasschnelli
Copy link
Contributor Author

@TheBlueMatt: Yes. @theuni recommended this on IRC and I'm currently changing it.

@jonasschnelli
Copy link
Contributor Author

Moved the cache from the core validation logic to the GUI.

if (!pindexBestHeader)
return 0;
return pindexBestHeader->nHeight;
if (cachedBestHeaderHeight == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is this initialized? -- these don't have static duration AFAICT.

Also is zero really the best value to use? -- it's in range, so it'll keep relocking again while there is no data there.

@gmaxwell
Copy link
Contributor

What does the GUI use the header time/height for as opposed to the tip?

@jonasschnelli
Copy link
Contributor Author

Thanks @gmaxwell. Added the missing initialisation and switched to -1 as indicator wether the cache is set or not.

What does the GUI use the header time/height for as opposed to the tip?

The only use case is to calculate the remaining blocks to download/validate (modal sync progress overlay).

Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

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

utACK

@@ -72,20 +74,28 @@ int ClientModel::getNumBlocks() const
return chainActive.Height();
}

int ClientModel::getHeaderTipHeight() const
int ClientModel::getHeaderTipHeight()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this should remain const, and declare the caches mutable?

// otherwise we need to wait for a tip update
LOCK(cs_main);
if (pindexBestHeader) {
cachedBestHeaderHeight = pindexBestHeader->nHeight;
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to populate both caches at once perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

@TheBlueMatt
Copy link
Contributor

utACK

Copy link
Contributor

@gmaxwell gmaxwell left a comment

Choose a reason for hiding this comment

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

utACK

@jonasschnelli
Copy link
Contributor Author

Followed @luke-jr's advice and re-set the method to const and declared the cache atomic's mutable.
Also, both caches are now getting set regardless of the called method. Yes, you could argue to have an explicit function to set the cache to avoid code duplication. But for two lines it's not worth in my opinion.

@gmaxwell, @TheBlueMatt: thanks for a re-ack. :)

@laanwj
Copy link
Member

laanwj commented Apr 20, 2017

Less blocking of the gui on cs_main is always a good thing, thanks!
utACK 4082fb0

@laanwj laanwj merged commit 4082fb0 into bitcoin:master Apr 20, 2017
laanwj added a commit that referenced this pull request Apr 20, 2017
4082fb0 Add missing <atomic> header in clientmodel.h (Jonas Schnelli)
928d4a9 Set both time/height header caches at the same time (Jonas Schnelli)
610a917 Declare headers height/time cache mutable, re-set the methods const (Jonas Schnelli)
cf92bce Update the remaining blocks left in modaloverlay at init. (Jonas Schnelli)
7148f5e Reduce cs_main locks during modal overlay by adding an atomic cache (Jonas Schnelli)

Tree-SHA512: a92ca22f90b8b2a5e8eb94fdce531ef44542e21a8dbbb0693f7723d7018592cb68de687a2a0aac91d31cbf019793f8e922550656d2b130ed3d854d60630341db
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 21, 2017
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 21, 2017
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 21, 2017
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 21, 2017
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Apr 21, 2017
@maflcko maflcko added this to the 0.14.2 milestone Apr 23, 2017
@jonasschnelli jonasschnelli mentioned this pull request May 31, 2017
codablock pushed a commit to codablock/dash that referenced this pull request Oct 31, 2017
4082fb0 Add missing <atomic> header in clientmodel.h (Jonas Schnelli)
928d4a9 Set both time/height header caches at the same time (Jonas Schnelli)
610a917 Declare headers height/time cache mutable, re-set the methods const (Jonas Schnelli)
cf92bce Update the remaining blocks left in modaloverlay at init. (Jonas Schnelli)
7148f5e Reduce cs_main locks during modal overlay by adding an atomic cache (Jonas Schnelli)

Tree-SHA512: a92ca22f90b8b2a5e8eb94fdce531ef44542e21a8dbbb0693f7723d7018592cb68de687a2a0aac91d31cbf019793f8e922550656d2b130ed3d854d60630341db
UdjinM6 pushed a commit to dashpay/dash that referenced this pull request Oct 31, 2017
…1704)

4082fb0 Add missing <atomic> header in clientmodel.h (Jonas Schnelli)
928d4a9 Set both time/height header caches at the same time (Jonas Schnelli)
610a917 Declare headers height/time cache mutable, re-set the methods const (Jonas Schnelli)
cf92bce Update the remaining blocks left in modaloverlay at init. (Jonas Schnelli)
7148f5e Reduce cs_main locks during modal overlay by adding an atomic cache (Jonas Schnelli)

Tree-SHA512: a92ca22f90b8b2a5e8eb94fdce531ef44542e21a8dbbb0693f7723d7018592cb68de687a2a0aac91d31cbf019793f8e922550656d2b130ed3d854d60630341db
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants