Merge 19873 via mempressure

This commit is contained in:
Luke Dashjr 2025-03-05 03:27:08 +00:00
commit 2fc6668792
7 changed files with 84 additions and 2 deletions

View File

@ -940,6 +940,21 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
[ AC_MSG_RESULT([no])]
)
AC_MSG_CHECKING(for compatible sysinfo call)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/sysinfo.h>]],
[[
struct sysinfo info;
int rv = sysinfo(&info);
unsigned long test = info.freeram + info.bufferram + info.mem_unit;
]])],
[
AC_MSG_RESULT(yes);
AC_DEFINE(HAVE_LINUX_SYSINFO, 1, [Define this symbol if you have a Linux-compatible sysinfo call])
],[
AC_MSG_RESULT(no)
]
)
dnl Check for posix_fallocate
AC_MSG_CHECKING([for posix_fallocate])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[

View File

@ -22,6 +22,10 @@
#include <malloc.h>
#endif
#ifdef HAVE_LINUX_SYSINFO
#include <sys/sysinfo.h>
#endif
#include <cstdlib>
#include <locale>
#include <stdexcept>
@ -110,3 +114,39 @@ int64_t GetStartupTime()
{
return nStartupTime;
}
size_t g_low_memory_threshold = 10 * 1024 * 1024 /* 10 MB */;
bool SystemNeedsMemoryReleased()
{
if (g_low_memory_threshold <= 0) {
// Intentionally bypass other metrics when disabled entirely
return false;
}
#ifdef WIN32
MEMORYSTATUSEX mem_status;
mem_status.dwLength = sizeof(mem_status);
if (GlobalMemoryStatusEx(&mem_status)) {
if (mem_status.dwMemoryLoad >= 99 ||
mem_status.ullAvailPhys < g_low_memory_threshold ||
mem_status.ullAvailVirtual < g_low_memory_threshold) {
LogPrintf("%s: YES: %s%% memory load; %s available physical memory; %s available virtual memory\n", __func__, int(mem_status.dwMemoryLoad), size_t(mem_status.ullAvailPhys), size_t(mem_status.ullAvailVirtual));
return true;
}
}
#endif
#ifdef HAVE_LINUX_SYSINFO
struct sysinfo sys_info;
if (!sysinfo(&sys_info)) {
// Explicitly 64-bit in case of 32-bit userspace on 64-bit kernel
const uint64_t free_ram = uint64_t(sys_info.freeram) * sys_info.mem_unit;
const uint64_t buffer_ram = uint64_t(sys_info.bufferram) * sys_info.mem_unit;
if (free_ram + buffer_ram < g_low_memory_threshold) {
LogPrintf("%s: YES: %s free RAM + %s buffer RAM\n", __func__, free_ram, buffer_ram);
return true;
}
}
#endif
// NOTE: sysconf(_SC_AVPHYS_PAGES) doesn't account for caches on at least Linux, so not safe to use here
return false;
}

View File

@ -23,6 +23,10 @@ std::string ShellEscape(const std::string& arg);
void runCommand(const std::string& strCommand);
#endif
extern size_t g_low_memory_threshold;
bool SystemNeedsMemoryReleased();
/**
* Return the number of cores available on the current system.
* @note This does count virtual cores, such as those provided by HyperThreading.

View File

@ -500,6 +500,7 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-allowignoredconf", strprintf("For backwards compatibility, treat an unused %s file in the datadir as a warning, not an error.", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-lowmem=<n>", strprintf("If system available memory falls below <n> MiB, flush caches (0 to disable, default: %s)", g_low_memory_threshold / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-maxmempool=<n>", strprintf("Keep the transaction memory pool below <n> megabytes (default: %u)", DEFAULT_MAX_MEMPOOL_SIZE_MB), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-maxorphantx=<n>", strprintf("Keep at most <n> unconnectable transactions in memory (default: %u)", DEFAULT_MAX_ORPHAN_TRANSACTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-mempoolexpiry=<n>", strprintf("Do not keep transactions in the mempool longer than <n> hours (default: %u)", DEFAULT_MEMPOOL_EXPIRY_HOURS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@ -1554,6 +1555,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bool do_reindex{args.GetBoolArg("-reindex", false)};
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
if (gArgs.IsArgSet("-lowmem")) {
g_low_memory_threshold = gArgs.GetIntArg("-lowmem", 0 /* not used */) * 1024 * 1024;
}
if (g_low_memory_threshold > 0) {
LogPrintf("* Flushing caches if available system memory drops below %s MiB\n", g_low_memory_threshold / 1024 / 1024);
}
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
bilingual_str mempool_error;
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);

View File

@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
#include <chainparams.h>
#include <common/system.h>
#include <consensus/validation.h>
#include <random.h>
#include <rpc/blockchain.h>
@ -25,6 +26,8 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, ChainTestingSetup)
//!
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
{
g_low_memory_threshold = 0; // disable to get deterministic flushing
ChainstateManager& manager = *Assert(m_node.chainman);
CTxMemPool& mempool = *Assert(m_node.mempool);
Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));

View File

@ -11,6 +11,7 @@
#include <chain.h>
#include <checkqueue.h>
#include <clientversion.h>
#include <common/system.h>
#include <consensus/amount.h>
#include <consensus/consensus.h>
#include <consensus/merkle.h>
@ -2839,8 +2840,15 @@ bool Chainstate::FlushStateToDisk(
}
// The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
// The cache is over the limit, we have to write now.
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
bool fCacheCritical = false;
if (mode == FlushStateMode::IF_NEEDED) {
if (cache_state >= CoinsCacheSizeState::CRITICAL) {
// The cache is over the limit, we have to write now.
fCacheCritical = true;
} else if (SystemNeedsMemoryReleased()) {
fCacheCritical = true;
}
}
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL;
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.

View File

@ -10,6 +10,7 @@
#include <vector>
#include <addresstype.h>
#include <common/system.h>
#include <interfaces/chain.h>
#include <key_io.h>
#include <node/blockstorage.h>
@ -819,6 +820,9 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
//! rescanning where new transactions in new blocks could be lost.
BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
{
// FIXME: this test fails for some reason if there's a flush
g_low_memory_threshold = 0;
m_args.ForceSetArg("-unsafesqlitesync", "1");
// Create new wallet with known key and unload it.
WalletContext context;