Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Feb 23, 2022

There is no need for any lock to be held, because net processing is single threaded. So holding the validation lock cs_main for sending a feefilter is confusing and might even degrade blockchain-related RPC performance minimally.

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

Approach ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 24, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24474 (Additional thread safety annotations for CNode/Peer by ajtowns)
  • #22778 (net processing: Reduce resource usage for inbound block-relay-only connections by jnewbery)
  • #21527 (net_processing: lock clean up by ajtowns)
  • #21160 (net/net processing: Move tx inventory into net_processing by jnewbery)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK faa329f

Message processing is single threaded.

I think all the variables accessed directly or indirectly by MaybeSendFeefilter() are:

  1. not accessed by another thread; or
  2. accessed by another thread after acquiring the appropriate lock (e.g. CChainState::m_chain is accessed by CChainState::IsInitialBlockDownload() but it acquires cs_main before that); or
  3. accessed by another thread without protection but only modified by "init" (::minRelayTxFee), that's a bit scary but is ok wrt this PR

@ajtowns
Copy link
Contributor

ajtowns commented Mar 2, 2022

ACK faa329f ; code review only

I don't think this breaks anything, or even makes anything more confusing. Not convinced by the "might even degrade performance" though -- this patch might equally degrade p2p performance if someone else gains cs_main after it's released, then also gains mempool.cs which then blocks the currentFilter = .. GetMinFee .. call which also wants mempool.cs.

@vasild
Copy link
Contributor

vasild commented Mar 2, 2022

My understanding of the PR description is that holding cs_main for longer "might degrade performance" (which is the case in master without this PR). Not that this PR "might degrade performance".

This PR just holds cs_man for a shorter time compared to master.

@ajtowns
Copy link
Contributor

ajtowns commented Mar 2, 2022

Yes -- holding cs_main for longer (as in current master) might degrade performance of some other thread trying to gain cs_main; but not holding it (as in this PR) might degrade performance if some other thread gains cs_main then a lock we care about (mempool.cs).

@maflcko
Copy link
Member Author

maflcko commented Mar 2, 2022

Sure, the net processing thread might be forced to sleep for an unknown amount of time before calling MaybeSendFeefilter. (Unrelated, this may already happen on current master, as the CPU scheduler is free to put any thread to sleep just "for fun" if it wants to.)
I don't think the changes here may decrease average performance (average throughput / the rate at which MaybeSendFeeFilter is called), though? If there are multiple thread that are competing for a lock in a loop with a short time releasing the lock, at any time at most one thread can hold the lock. Assuming a round robin scheduler to hand out the lock to the threads asking for it, I couldn't come up with an example where the average performance is consistently worse with the patch here, but maybe I am missing something?

@maflcko maflcko added this to the 24.0 milestone Mar 2, 2022
@maflcko
Copy link
Member Author

maflcko commented Mar 2, 2022

Oh, sorry. Quite obviously, assuming a round robin scheduler for giving out the locks to threads, the average performance might decrease if there is more than one thread competing with the message processing thread. (Or alternatively if go further down the route to move more stuff out of cs_main, a single competing thread should be enough).

@vasild
Copy link
Contributor

vasild commented Mar 2, 2022

Ok, but moving stuff out of cs_main is the way to achieve more concurrency and thus better performance of the entire program.

@ajtowns
Copy link
Contributor

ajtowns commented Mar 2, 2022

Sorry, didn't mean to make a thing of that -- I think the key word for the performance degradation in both cases is "minimally" :)

@maflcko maflcko merged commit 384866e into bitcoin:master Mar 7, 2022
@maflcko maflcko deleted the 2202-cs_main-🔹 branch March 7, 2022 09:01
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 7, 2022
faa329f refactor: Release cs_main before MaybeSendFeefilter (MarcoFalke)

Pull request description:

  There is no need for any lock to be held, because net processing is single threaded. So holding the validation lock cs_main for sending a feefilter is confusing and might even degrade blockchain-related RPC performance minimally.

ACKs for top commit:
  ajtowns:
    ACK faa329f ; code review only
  vasild:
    ACK faa329f

Tree-SHA512: 3e7f9faff1631cc64c86fc1a354ada67617ad1e7a046625cc741f4711854eb41ca8aad5a51ef0d94ff65947b68dba8345c9f786b20ee0a8b7a2e8741cfced21f
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 10, 2022
Summary:
[[bitcoin/bitcoin#21992 | core#21992]]
> Remove -feefilter option
>
> Feefilter option is debug only and it isn't used in any tests, it's wasteful
> to check this option for every peer on every iteration of the message handler
> loop. refs [[bitcoin/bitcoin#21545 | core#21545]]

> Factor feefilter logic out
>
> Break SendMessages() function into smaller units to improve readability.
> Adds lock assert, as `round()` isn't thread safe.
> closes #21545

----

[[bitcoin/bitcoin#24427 | core#24427]]
> refactor: Release cs_main before MaybeSendFeefilter

----

This is a backport of [[bitcoin/bitcoin#21992 | core#21992]] and [[bitcoin/bitcoin#24427 | core#24427]]
Depends on D12182

Test Plan:
With Debug clang and tsan
`ninja all check check-functional`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12183
@bitcoin bitcoin locked and limited conversation to collaborators Mar 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants