File tree Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Original file line number Diff line number Diff line change @@ -1005,6 +1005,21 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
1005
1005
[ AC_MSG_RESULT ( [ no] ) ]
1006
1006
)
1007
1007
1008
+ AC_MSG_CHECKING ( for compatible sysinfo call )
1009
+ AC_COMPILE_IFELSE ( [ AC_LANG_PROGRAM ( [ [ #include <sys/sysinfo.h>] ] ,
1010
+ [ [
1011
+ struct sysinfo info;
1012
+ int rv = sysinfo(&info);
1013
+ unsigned long test = info.freeram + info.bufferram + info.mem_unit;
1014
+ ] ] ) ] ,
1015
+ [
1016
+ AC_MSG_RESULT ( yes ) ;
1017
+ AC_DEFINE ( HAVE_LINUX_SYSINFO , 1 , [ Define this symbol if you have a Linux-compatible sysinfo call] )
1018
+ ] ,[
1019
+ AC_MSG_RESULT ( no )
1020
+ ]
1021
+ )
1022
+
1008
1023
dnl Check for posix_fallocate
1009
1024
AC_MSG_CHECKING ( [ for posix_fallocate] )
1010
1025
AC_COMPILE_IFELSE ( [ AC_LANG_PROGRAM ( [ [
Original file line number Diff line number Diff line change 24
24
#include < malloc.h>
25
25
#endif
26
26
27
+ #ifdef HAVE_LINUX_SYSINFO
28
+ #include < sys/sysinfo.h>
29
+ #endif
30
+
27
31
#include < cstdlib>
28
32
#include < locale>
29
33
#include < stdexcept>
@@ -110,3 +114,28 @@ int64_t GetStartupTime()
110
114
{
111
115
return nStartupTime;
112
116
}
117
+
118
+ bool SystemNeedsMemoryReleased ()
119
+ {
120
+ constexpr size_t low_memory_threshold = 10 * 1024 * 1024 /* 10 MB */ ;
121
+ #ifdef WIN32
122
+ MEMORYSTATUSEX mem_status;
123
+ mem_status.dwLength = sizeof (mem_status);
124
+ if (GlobalMemoryStatusEx (&mem_status)) {
125
+ if (mem_status.dwMemoryLoad >= 99 ) return true ;
126
+ if (mem_status.ullAvailPhys < low_memory_threshold) return true ;
127
+ if (mem_status.ullAvailVirtual < low_memory_threshold) return true ;
128
+ }
129
+ #endif
130
+ #ifdef HAVE_LINUX_SYSINFO
131
+ struct sysinfo sys_info;
132
+ if (!sysinfo (&sys_info)) {
133
+ // Explicitly 64-bit in case of 32-bit userspace on 64-bit kernel
134
+ const uint64_t free_ram = uint64_t (sys_info.freeram ) * sys_info.mem_unit ;
135
+ const uint64_t buffer_ram = uint64_t (sys_info.bufferram ) * sys_info.mem_unit ;
136
+ if (free_ram + buffer_ram < low_memory_threshold) return true ;
137
+ }
138
+ #endif
139
+ // NOTE: sysconf(_SC_AVPHYS_PAGES) doesn't account for caches on at least Linux, so not safe to use here
140
+ return false ;
141
+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ std::string ShellEscape(const std::string& arg);
25
25
void runCommand (const std::string& strCommand);
26
26
#endif
27
27
28
+ bool SystemNeedsMemoryReleased ();
29
+
28
30
/* *
29
31
* Return the number of cores available on the current system.
30
32
* @note This does count virtual cores, such as those provided by HyperThreading.
Original file line number Diff line number Diff line change 13
13
#include < chain.h>
14
14
#include < checkqueue.h>
15
15
#include < clientversion.h>
16
+ #include < common/system.h>
16
17
#include < consensus/amount.h>
17
18
#include < consensus/consensus.h>
18
19
#include < consensus/merkle.h>
@@ -2628,8 +2629,15 @@ bool Chainstate::FlushStateToDisk(
2628
2629
}
2629
2630
// 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).
2630
2631
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
2631
- // The cache is over the limit, we have to write now.
2632
- bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
2632
+ bool fCacheCritical = false ;
2633
+ if (mode == FlushStateMode::IF_NEEDED) {
2634
+ if (cache_state >= CoinsCacheSizeState::CRITICAL) {
2635
+ // The cache is over the limit, we have to write now.
2636
+ fCacheCritical = true ;
2637
+ } else if (SystemNeedsMemoryReleased ()) {
2638
+ fCacheCritical = true ;
2639
+ }
2640
+ }
2633
2641
// 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.
2634
2642
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL;
2635
2643
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
You can’t perform that action at this time.
0 commit comments