Skip to content

kernel: Only setup kernel context globals once - #30537

Merged
fanquake merged 1 commit into
bitcoin:masterfrom
sedited:staticInitKernel
Jul 31, 2024
Merged

fanquake merged 1 commit into
bitcoin:masterfrom
sedited:staticInitKernel

Conversation

@sedited

@sedited sedited commented Jul 27, 2024

Copy link
Copy Markdown
Contributor

The globals setup by the function calls when creating a new kernel context only need to be setup once. Calling them multiple times may be wasteful and has no apparent benefit.

Besides kernel users potentially creating multiple contexts, this change may also be useful for tests creating multiple setups.

@DrahtBot

DrahtBot commented Jul 27, 2024 •

Copy link
Copy Markdown
Contributor

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK stickies-v, tdb3, maflcko
Stale ACK danielabrozzoni

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29641 (scripted-diff: Use LogInfo/LogDebug over LogPrintf/LogPrint by maflcko)

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.

@tdb3 tdb3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approach ACK

Comment thread src/kernel/context.cpp Resolved
@sedited
sedited force-pushed the staticInitKernel branch from de78532 to 50c14e5 Compare July 27, 2024 19:59
@sedited

sedited commented Jul 27, 2024

Copy link
Copy Markdown
Contributor Author

Updated de78532 -> 50c14e5 (staticInitKernel_0 -> staticInitKernel_1, compare)

  • Addressed @tdb3's comment, using LogInfo instead of the deprecated LogPrintf.

@tdb3 tdb3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 50c14e5
Nice optimization.
Ran unit/functional tests locally (passed).

@danielabrozzoni danielabrozzoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tACK 50c14e5

@stickies-v stickies-v left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 50c14e5 . No behaviour change for bitcoind and bitcoin-qt, only affects the tests (and downstream).

Comment thread src/kernel/context.cpp Resolved
Comment thread src/kernel/context.cpp Resolved
The globals setup by the function calls when creating a new kernel
context only need to be setup once. Calling them multiple times may be
wasteful and has no apparent benefit.

Besides kernel users potentially creating multiple contexts, this change
may also be useful for tests creating multiple setups.

Co-authored-by: stickies-v <[email protected]>
@sedited
sedited force-pushed the staticInitKernel branch from 50c14e5 to 93fb0e7 Compare July 30, 2024 16:10
@sedited

sedited commented Jul 30, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions @stickies-v,

50c14e5 -> 93fb0e7 (staticInitKernel_1 -> staticInitKernel_2, compare)

  • Addressed @stickies-v's comment, using std::call_once instead of static boolean flag. This makes the behavior consistent on the off chance that a context is created concurrently.
  • Addressed @stickies-v's comment, renaming initialized to globals_initialized.

@stickies-v

Copy link
Copy Markdown
Contributor

re-ACK 93fb0e7

@DrahtBot
DrahtBot requested review from danielabrozzoni and tdb3 July 30, 2024 21:56

@tdb3 tdb3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

re ACK 93fb0e7

@maflcko

maflcko commented Jul 31, 2024

Copy link
Copy Markdown
Member

Concept ACK. The context constructor isn't thread-safe. This can be tested with tsan and:

diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index abe3d6a505..05d00eaf97 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -62,6 +62,7 @@
 
 #include <algorithm>
 #include <functional>
+#include <future>
 #include <stdexcept>
 
 using kernel::BlockTreeDB;
@@ -195,6 +196,13 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
     AppInitParameterInteraction(*m_node.args);
     LogInstance().StartLogging();
     m_node.warnings = std::make_unique<node::Warnings>();
+    {
+        auto fun{[] { kernel::Context{}; }};
+        auto futs{std::array{
+            std::async(std::launch::async, fun),
+            std::async(std::launch::async, fun),
+        }};
+    }
     m_node.kernel = std::make_unique<kernel::Context>();
     m_node.ecc_context = std::make_unique<ECC_Context>();
     SetupEnvironment();

Which should print a data-race. So it seems fine to guard the code and only execute it once.

@maflcko

maflcko commented Jul 31, 2024

Copy link
Copy Markdown
Member

ACK 93fb0e7 👝

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK 93fb0e7897000072ea790a91816aea876718ad27 👝
qDwtQZZCJDX/9k96k2fh3KtTVl9a3ZmElpWNyBB9S0OOk47oYfmKnOTgbM0NiJD/TKvFbXiAMTKqGYgiLK/LAQ==

@fanquake
fanquake merged commit c6b4718 into bitcoin:master Jul 31, 2024

@hebasto hebasto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Post-merge ACK 93fb0e7.

@bitcoin bitcoin locked and limited conversation to collaborators Jul 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done or Closed or Rethinking

Development

Successfully merging this pull request may close these issues.

9 participants