Skip to content

Commit cad4762

Browse files
committed
Log getMemoryLoad()
1 parent 1f31010 commit cad4762

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

src/interfaces/mining.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class CScript;
2323

2424
namespace interfaces {
2525

26+
//! Tracks memory usage for template-bound transactions.
27+
struct MemoryLoad
28+
{
29+
uint64_t usage{0};
30+
};
31+
2632
//! Block template interface
2733
class BlockTemplate
2834
{
@@ -129,6 +135,12 @@ class Mining
129135
* For signets the challenge verification is skipped when check_pow is false.
130136
*/
131137
virtual bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) = 0;
138+
139+
/**
140+
* Returns the current memory load for template transactions outside the
141+
* mempool.
142+
*/
143+
virtual MemoryLoad getMemoryLoad() = 0;
132144
};
133145

134146
} // namespace interfaces

src/ipc/capnp/mining.capnp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface Mining $Proxy.wrap("interfaces::Mining") {
1919
waitTipChanged @3 (context :Proxy.Context, currentTip: Data, timeout: Float64) -> (result: Common.BlockRef);
2020
createNewBlock @4 (options: BlockCreateOptions) -> (result: BlockTemplate);
2121
checkBlock @5 (block: Data, options: BlockCheckOptions) -> (reason: Text, debug: Text, result: Bool);
22+
getMemoryLoad @6 (context :Proxy.Context) -> (result: MemoryLoad);
2223
}
2324

2425
interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
@@ -36,6 +37,10 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
3637
interruptWait @11() -> ();
3738
}
3839

40+
struct MemoryLoad $Proxy.wrap("interfaces::MemoryLoad") {
41+
usage @0 :UInt64;
42+
}
43+
3944
struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
4045
useMempool @0 :Bool $Proxy.name("use_mempool");
4146
blockReservedWeight @1 :UInt64 $Proxy.name("block_reserved_weight");

src/sv2/template_provider.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <algorithm>
1818
#include <limits>
1919

20+
using interfaces::MemoryLoad;
21+
2022
// Allow a few seconds for clients to submit a block or to request transactions
2123
constexpr size_t STALE_TEMPLATE_GRACE_PERIOD{10};
2224

@@ -110,6 +112,8 @@ bool Sv2TemplateProvider::Start(const Sv2TemplateProviderOptions& options)
110112
}
111113

112114
m_thread_sv2_handler = std::thread(&util::TraceThread, "sv2", [this] { ThreadSv2Handler(); });
115+
m_thread_memory_handler = std::thread(&util::TraceThread, "memory", [this] { ThreadMemoryHandler(); });
116+
113117
return true;
114118
}
115119

@@ -154,6 +158,9 @@ void Sv2TemplateProvider::StopThreads()
154158
if (m_thread_sv2_handler.joinable()) {
155159
m_thread_sv2_handler.join();
156160
}
161+
if (m_thread_memory_handler.joinable()) {
162+
m_thread_memory_handler.join();
163+
}
157164
}
158165

159166
class Timer {
@@ -249,6 +256,26 @@ void Sv2TemplateProvider::ThreadSv2Handler()
249256

250257
}
251258

259+
void Sv2TemplateProvider::ThreadMemoryHandler()
260+
{
261+
size_t seconds{0};
262+
while (!m_flag_interrupt_sv2) {
263+
std::this_thread::sleep_for(1000ms);
264+
if (++seconds % 60 != 0) continue;
265+
if (m_mining.isInitialBlockDownload()) continue;
266+
try {
267+
MemoryLoad memory_load{m_mining.getMemoryLoad()};
268+
const double usage_mib{static_cast<double>(memory_load.usage) / (1024.0 * 1024.0)};
269+
const std::string usage_mib_str{strprintf("%.3f", usage_mib)};
270+
LogTrace(BCLog::SV2, "Template memory footprint %s MiB", usage_mib_str);
271+
} catch (const ipc::Exception& e) {
272+
LogTrace(BCLog::SV2, "getMemoryLoad() not implemented on the node");
273+
// Nothing to do for this thread
274+
break;
275+
}
276+
}
277+
}
278+
252279
void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
253280
{
254281
try {

src/sv2/template_provider.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class Sv2TemplateProvider : public Sv2EventsInterface
8181
*/
8282
std::thread m_thread_sv2_handler;
8383

84+
/**
85+
* Memory management thread
86+
*/
87+
std::thread m_thread_memory_handler;
88+
8489
/**
8590
* Signal for handling interrupts and stopping the template provider event loop.
8691
*/
@@ -129,6 +134,11 @@ class Sv2TemplateProvider : public Sv2EventsInterface
129134
*/
130135
void ThreadSv2Handler() EXCLUSIVE_LOCKS_REQUIRED(!m_tp_mutex);
131136

137+
/**
138+
* Template memory management thread.
139+
*/
140+
void ThreadMemoryHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_tp_mutex);
141+
132142
/**
133143
* Give each client its own thread so they're treated equally
134144
* and so that newly connected clients don't have to wait.

src/test/sv2_mock_mining.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ std::unique_ptr<interfaces::BlockTemplate> MockMining::createNewBlock(const node
123123
}
124124
bool MockMining::checkBlock(const CBlock&, const node::BlockCheckOptions&, std::string&, std::string&) { return true; }
125125

126+
MemoryLoad MockMining::getMemoryLoad()
127+
{
128+
return {
129+
.usage = 0
130+
};
131+
}
132+
126133
uint64_t MockMining::GetTemplateSeq()
127134
{
128135
LOCK(state->m);

src/test/sv2_mock_mining.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <sync.h>
1818
#include <uint256.h>
1919

20+
using interfaces::MemoryLoad;
21+
2022
// Minimal mocks for the Mining IPC interface used by sv2 tests.
2123

2224
struct MockEvent {
@@ -82,6 +84,7 @@ class MockMining : public interfaces::Mining {
8284
std::optional<interfaces::BlockRef> waitTipChanged(uint256, MillisecondsDouble) override;
8385
std::unique_ptr<interfaces::BlockTemplate> createNewBlock(const node::BlockCreateOptions&) override;
8486
bool checkBlock(const CBlock&, const node::BlockCheckOptions&, std::string&, std::string&) override;
87+
MemoryLoad getMemoryLoad() override;
8588

8689
// Accessors for tests (thread-safe)
8790
uint64_t GetTemplateSeq();

0 commit comments

Comments
 (0)