Skip to content

Commit 947f292

Browse files
committedOct 24, 2024··
Merge bitcoin#31124: util: Remove RandAddSeedPerfmon
9bb92c0 util: Remove RandAddSeedPerfmon (Hodlinator) Pull request description: `RegQueryValueExA(HKEY_PERFORMANCE_DATA, ...)` sometimes hangs *bitcoind.exe* on Windows during startup, at least on CI. We have other sources of entropy to seed randomness with on Windows, so should be alright removing this. Might resurrect if less drastic fix is found. Hopefully sufficient to fix bitcoin#30390. CI debugged with temporary Windows stack trace dumping + Symbols in bitcoin#30956. ACKs for top commit: achow101: ACK 9bb92c0 fanquake: ACK 9bb92c0 hebasto: ACK 9bb92c0, I have reviewed the code and it looks OK. laanwj: Code review ACK 9bb92c0 Tree-SHA512: d3f26b4dd0519ef957f23abaffc6be1fed339eae756aed18042422fc6f0bba4e8fa9a44bf903e54f72747e2d0108146c18fd80576d95fc20690a2daf9c83689d
2 parents 7640cfd + 9bb92c0 commit 947f292

File tree

3 files changed

+3
-45
lines changed

3 files changed

+3
-45
lines changed
 

‎src/random.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
599599
// Add the events hasher into the mix
600600
rng.SeedEvents(hasher);
601601

602-
// Dynamic environment data (performance monitoring, ...)
602+
// Dynamic environment data (clocks, resource usage, ...)
603603
auto old_size = hasher.Size();
604604
RandAddDynamicEnv(hasher);
605605
LogDebug(BCLog::RAND, "Feeding %i bytes of dynamic environment data into RNG\n", hasher.Size() - old_size);
@@ -616,7 +616,7 @@ void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
616616
// Everything that the 'slow' seeder includes.
617617
SeedSlow(hasher, rng);
618618

619-
// Dynamic environment data (performance monitoring, ...)
619+
// Dynamic environment data (clocks, resource usage, ...)
620620
auto old_size = hasher.Size();
621621
RandAddDynamicEnv(hasher);
622622

‎src/random.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
*
5050
* - RandAddPeriodic() seeds everything that fast seeding includes, but additionally:
5151
* - A high-precision timestamp
52-
* - Dynamic environment data (performance monitoring, ...)
52+
* - Dynamic environment data (clocks, resource usage, ...)
5353
* - Strengthen the entropy for 10 ms using repeated SHA512.
5454
* This is run once every minute.
5555
*

‎src/randomenv.cpp

-42
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
#ifdef WIN32
3030
#include <windows.h>
31-
#include <winreg.h>
3231
#else
3332
#include <fcntl.h>
3433
#include <netinet/in.h>
@@ -64,45 +63,6 @@ extern char** environ; // NOLINT(readability-redundant-declaration): Necessary o
6463

6564
namespace {
6665

67-
void RandAddSeedPerfmon(CSHA512& hasher)
68-
{
69-
#ifdef WIN32
70-
// Seed with the entire set of perfmon data
71-
72-
// This can take up to 2 seconds, so only do it every 10 minutes.
73-
// Initialize last_perfmon to 0 seconds, we don't skip the first call.
74-
static std::atomic<SteadyClock::time_point> last_perfmon{SteadyClock::time_point{0s}};
75-
auto last_time = last_perfmon.load();
76-
auto current_time = SteadyClock::now();
77-
if (current_time < last_time + 10min) return;
78-
last_perfmon = current_time;
79-
80-
std::vector<unsigned char> vData(250000, 0);
81-
long ret = 0;
82-
unsigned long nSize = 0;
83-
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
84-
while (true) {
85-
nSize = vData.size();
86-
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
87-
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
88-
break;
89-
vData.resize(std::min((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
90-
}
91-
RegCloseKey(HKEY_PERFORMANCE_DATA);
92-
if (ret == ERROR_SUCCESS) {
93-
hasher.Write(vData.data(), nSize);
94-
memory_cleanse(vData.data(), nSize);
95-
} else {
96-
// Performance data is only a best-effort attempt at improving the
97-
// situation when the OS randomness (and other sources) aren't
98-
// adequate. As a result, failure to read it is isn't considered critical,
99-
// so we don't call RandFailure().
100-
// TODO: Add logging when the logger is made functional before global
101-
// constructors have been invoked.
102-
}
103-
#endif
104-
}
105-
10666
/** Helper to easily feed data into a CSHA512.
10767
*
10868
* Note that this does not serialize the passed object (like stream.h's << operators do).
@@ -227,8 +187,6 @@ void AddAllCPUID(CSHA512& hasher)
227187

228188
void RandAddDynamicEnv(CSHA512& hasher)
229189
{
230-
RandAddSeedPerfmon(hasher);
231-
232190
// Various clocks
233191
#ifdef WIN32
234192
FILETIME ftime;

0 commit comments

Comments
 (0)
Please sign in to comment.