mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
[MOVEONLY] Move perfmon data gathering to new randomenv module
This commit is contained in:
parent
b51bae1a5a
commit
cea3902015
4 changed files with 97 additions and 42 deletions
|
@ -175,6 +175,7 @@ BITCOIN_CORE_H = \
|
||||||
protocol.h \
|
protocol.h \
|
||||||
psbt.h \
|
psbt.h \
|
||||||
random.h \
|
random.h \
|
||||||
|
randomenv.h \
|
||||||
reverse_iterator.h \
|
reverse_iterator.h \
|
||||||
reverselock.h \
|
reverselock.h \
|
||||||
rpc/blockchain.h \
|
rpc/blockchain.h \
|
||||||
|
@ -502,6 +503,7 @@ libbitcoin_util_a_SOURCES = \
|
||||||
interfaces/handler.cpp \
|
interfaces/handler.cpp \
|
||||||
logging.cpp \
|
logging.cpp \
|
||||||
random.cpp \
|
random.cpp \
|
||||||
|
randomenv.cpp \
|
||||||
rpc/request.cpp \
|
rpc/request.cpp \
|
||||||
support/cleanse.cpp \
|
support/cleanse.cpp \
|
||||||
sync.cpp \
|
sync.cpp \
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
#include <randomenv.h>
|
||||||
|
|
||||||
#include <support/allocators/secure.h>
|
#include <support/allocators/secure.h>
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
|
@ -263,44 +265,6 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
|
||||||
memory_cleanse(buffer, sizeof(buffer));
|
memory_cleanse(buffer, sizeof(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RandAddSeedPerfmon(CSHA512& hasher)
|
|
||||||
{
|
|
||||||
#ifdef WIN32
|
|
||||||
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
|
|
||||||
// Seed with the entire set of perfmon data
|
|
||||||
|
|
||||||
// This can take up to 2 seconds, so only do it every 10 minutes
|
|
||||||
static int64_t nLastPerfmon;
|
|
||||||
if (GetTime() < nLastPerfmon + 10 * 60)
|
|
||||||
return;
|
|
||||||
nLastPerfmon = GetTime();
|
|
||||||
|
|
||||||
std::vector<unsigned char> vData(250000, 0);
|
|
||||||
long ret = 0;
|
|
||||||
unsigned long nSize = 0;
|
|
||||||
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
|
|
||||||
while (true) {
|
|
||||||
nSize = vData.size();
|
|
||||||
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
|
|
||||||
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
|
|
||||||
break;
|
|
||||||
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
|
|
||||||
}
|
|
||||||
RegCloseKey(HKEY_PERFORMANCE_DATA);
|
|
||||||
if (ret == ERROR_SUCCESS) {
|
|
||||||
hasher.Write(vData.data(), nSize);
|
|
||||||
memory_cleanse(vData.data(), nSize);
|
|
||||||
} else {
|
|
||||||
// Performance data is only a best-effort attempt at improving the
|
|
||||||
// situation when the OS randomness (and other sources) aren't
|
|
||||||
// adequate. As a result, failure to read it is isn't considered critical,
|
|
||||||
// so we don't call RandFailure().
|
|
||||||
// TODO: Add logging when the logger is made functional before global
|
|
||||||
// constructors have been invoked.
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
|
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
|
||||||
* compatible way to get cryptographic randomness on UNIX-ish platforms.
|
* compatible way to get cryptographic randomness on UNIX-ish platforms.
|
||||||
|
@ -585,8 +549,8 @@ static void SeedSleep(CSHA512& hasher, RNGState& rng)
|
||||||
// High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
|
// High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
|
||||||
SeedTimestamp(hasher);
|
SeedTimestamp(hasher);
|
||||||
|
|
||||||
// Windows performance monitor data (once every 10 minutes)
|
// Dynamic environment data (performance monitoring, ...; once every 10 minutes)
|
||||||
RandAddSeedPerfmon(hasher);
|
RandAddDynamicEnv(hasher);
|
||||||
|
|
||||||
// Strengthen every minute
|
// Strengthen every minute
|
||||||
SeedStrengthen(hasher, rng);
|
SeedStrengthen(hasher, rng);
|
||||||
|
@ -600,8 +564,11 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
|
||||||
// Everything that the 'slow' seeder includes.
|
// Everything that the 'slow' seeder includes.
|
||||||
SeedSlow(hasher);
|
SeedSlow(hasher);
|
||||||
|
|
||||||
// Windows performance monitor data.
|
// Dynamic environment data
|
||||||
RandAddSeedPerfmon(hasher);
|
RandAddDynamicEnv(hasher);
|
||||||
|
|
||||||
|
// Static environment data
|
||||||
|
RandAddStaticEnv(hasher);
|
||||||
|
|
||||||
// Strengthen
|
// Strengthen
|
||||||
SeedStrengthen(hasher, rng);
|
SeedStrengthen(hasher, rng);
|
||||||
|
|
69
src/randomenv.cpp
Normal file
69
src/randomenv.cpp
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <randomenv.h>
|
||||||
|
|
||||||
|
#include <crypto/sha512.h>
|
||||||
|
#include <support/cleanse.h>
|
||||||
|
#include <util/time.h> // for GetTime()
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <compat.h> // for Windows API
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void RandAddSeedPerfmon(CSHA512& hasher)
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
|
||||||
|
// Seed with the entire set of perfmon data
|
||||||
|
|
||||||
|
// This can take up to 2 seconds, so only do it every 10 minutes
|
||||||
|
static int64_t nLastPerfmon;
|
||||||
|
if (GetTime() < nLastPerfmon + 10 * 60)
|
||||||
|
return;
|
||||||
|
nLastPerfmon = GetTime();
|
||||||
|
|
||||||
|
std::vector<unsigned char> vData(250000, 0);
|
||||||
|
long ret = 0;
|
||||||
|
unsigned long nSize = 0;
|
||||||
|
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
|
||||||
|
while (true) {
|
||||||
|
nSize = vData.size();
|
||||||
|
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
|
||||||
|
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
|
||||||
|
break;
|
||||||
|
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
|
||||||
|
}
|
||||||
|
RegCloseKey(HKEY_PERFORMANCE_DATA);
|
||||||
|
if (ret == ERROR_SUCCESS) {
|
||||||
|
hasher.Write(vData.data(), nSize);
|
||||||
|
memory_cleanse(vData.data(), nSize);
|
||||||
|
} else {
|
||||||
|
// Performance data is only a best-effort attempt at improving the
|
||||||
|
// situation when the OS randomness (and other sources) aren't
|
||||||
|
// adequate. As a result, failure to read it is isn't considered critical,
|
||||||
|
// so we don't call RandFailure().
|
||||||
|
// TODO: Add logging when the logger is made functional before global
|
||||||
|
// constructors have been invoked.
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void RandAddDynamicEnv(CSHA512& hasher)
|
||||||
|
{
|
||||||
|
RandAddSeedPerfmon(hasher);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RandAddStaticEnv(CSHA512& hasher)
|
||||||
|
{
|
||||||
|
}
|
17
src/randomenv.h
Normal file
17
src/randomenv.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_RANDOMENV_H
|
||||||
|
#define BITCOIN_RANDOMENV_H
|
||||||
|
|
||||||
|
#include <crypto/sha512.h>
|
||||||
|
|
||||||
|
/** Gather non-cryptographic environment data that changes over time. */
|
||||||
|
void RandAddDynamicEnv(CSHA512& hasher);
|
||||||
|
|
||||||
|
/** Gather non-cryptographic environment data that does not change over time. */
|
||||||
|
void RandAddStaticEnv(CSHA512& hasher);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Reference in a new issue