Skip to content

Commit

Permalink
hackery
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtowns authored and amitiuttarwar committed Nov 2, 2023
1 parent af2ad9b commit a25c685
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/common/bloom.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_COMMON_BLOOM_H
#define BITCOIN_COMMON_BLOOM_H

#include <memusage.h>
#include <serialize.h>
#include <span.h>

Expand Down Expand Up @@ -52,6 +53,8 @@ class CBloomFilter
unsigned int Hash(unsigned int nHashNum, Span<const unsigned char> vDataToHash) const;

public:
size_t DynamicUsage() const { return memusage::DynamicUsage(vData); }

/**
* Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements
* Note that if the given parameters will result in a filter outside the bounds of the protocol limits,
Expand Down Expand Up @@ -115,6 +118,8 @@ class CRollingBloomFilter

void reset();

size_t DynamicUsage() const { return memusage::DynamicUsage(data); }

private:
int nEntriesPerGeneration;
int nEntriesThisGeneration;
Expand Down
20 changes: 4 additions & 16 deletions src/memusage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <indirectmap.h>
#include <prevector.h>
#include <support/allocators/pool.h>
#include <net.h>
#include <support/allocators/zeroafterfree.h>
#include <streams.h>

Expand All @@ -23,6 +22,8 @@
#include <unordered_set>
#include <deque>

class CNetMessage;

namespace memusage
{

Expand Down Expand Up @@ -182,27 +183,14 @@ static inline size_t DynamicUsage(const std::list<X>& l)

// only handles dynamic memory usage of a CNetMessage object
// static usage should be counted elsewhere
static inline size_t RecursiveDynamicUsage(CNetMessage& msg)
{
// mallocusage of the m_type string
// and DynamicUsage(vector) calls MallocUsage(vector)
return MallocUsage(msg.m_type.capacity()) + DynamicUsage(msg.m_recv.vch);
}
size_t RecursiveDynamicUsage(const CNetMessage& msg);

static inline size_t DynamicUsage(const std::string s)
{
return MallocUsage(s.capacity());
}

static inline size_t RecursiveDynamicUsage(std::list<CNetMessage>& msg_list)
{
size_t val = 0;
val = DynamicUsage(msg_list); // MallocUsage of the list
for (auto& msg : msg_list) {
val += RecursiveDynamicUsage(msg);
}
return val;
}
size_t RecursiveDynamicUsage(const std::list<CNetMessage>& msg_list);

template<typename X>
struct unordered_node : private X
Expand Down
19 changes: 19 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4103,3 +4103,22 @@ std::function<void(const CAddress& addr,
Span<const unsigned char> data,
bool is_incoming)>
CaptureMessage = CaptureMessageToFile;

namespace memusage {
size_t RecursiveDynamicUsage(const CNetMessage& msg)
{
// mallocusage of the m_type string
// and DynamicUsage(vector) calls MallocUsage(vector)
return MallocUsage(msg.m_type.capacity()) + DynamicUsage(msg.m_recv.vch);
}

size_t RecursiveDynamicUsage(const std::list<CNetMessage>& msg_list)
{
size_t val = 0;
val = DynamicUsage(msg_list); // MallocUsage of the list
for (auto& msg : msg_list) {
val += RecursiveDynamicUsage(msg);
}
return val;
}
}
49 changes: 34 additions & 15 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ struct Peer {
/** Transaction relay data. May be a nullptr. */
std::unique_ptr<TxRelay> m_tx_relay GUARDED_BY(m_tx_relay_mutex);

size_t ConstantMemoryUsage()
EXCLUSIVE_LOCKS_REQUIRED(!m_misbehavior_mutex, !m_block_inv_mutex, !m_addr_send_times_mutex, !m_getdata_requests_mutex, !m_headers_sync_mutex);

size_t ConstantMemoryUsage() const;
// EXCLUSIVE_LOCKS_REQUIRED(!m_misbehavior_mutex, !m_block_inv_mutex, !m_addr_send_times_mutex, !m_getdata_requests_mutex, !m_headers_sync_mutex);
std::atomic<size_t> inaccessible_dyn_memusage{0};
};

using PeerRef = std::shared_ptr<Peer>;
Expand Down Expand Up @@ -1667,7 +1667,7 @@ PeerRef PeerManagerImpl::RemovePeer(NodeId id)
}

// sanity check. should be comparable to sizeof(peer)
size_t Peer::ConstantMemoryUsage()
size_t Peer::ConstantMemoryUsage() const
{
size_t val = 0;

Expand All @@ -1677,14 +1677,12 @@ size_t Peer::ConstantMemoryUsage()

val += sizeof(m_misbehavior_mutex);
{
LOCK(m_misbehavior_mutex);
val += sizeof(m_misbehavior_score);
val += sizeof(m_should_discourage);
}

val += sizeof(m_block_inv_mutex);
{
LOCK(m_block_inv_mutex);
val += sizeof(m_blocks_for_inv_relay);
val += sizeof(m_blocks_for_headers_relay);
// should this be in the dynamic function? uint256 base_block has an
Expand All @@ -1699,7 +1697,6 @@ size_t Peer::ConstantMemoryUsage()
val += sizeof(m_wtxid_relay);

{
LOCK(NetEventsInterface::g_msgproc_mutex);
val += sizeof(m_fee_filter_sent);
val += sizeof(m_next_send_feefilter);
val += sizeof(m_addrs_to_send);
Expand All @@ -1723,7 +1720,6 @@ size_t Peer::ConstantMemoryUsage()

val += sizeof(m_addr_send_times_mutex);
{
LOCK(m_addr_send_times_mutex);
val += sizeof(m_next_addr_send);
val += sizeof(m_next_local_addr_send);
}
Expand All @@ -1733,10 +1729,10 @@ size_t Peer::ConstantMemoryUsage()
val += sizeof(m_addr_processed);

val += sizeof(m_getdata_requests_mutex);
WITH_LOCK(m_getdata_requests_mutex, val += sizeof(m_getdata_requests));
val += sizeof(m_getdata_requests);

val += sizeof(m_headers_sync_mutex);
WITH_LOCK(m_headers_sync_mutex, val += sizeof(m_headers_sync));
val += sizeof(m_headers_sync);

val += sizeof(m_sent_sendheaders);

Expand All @@ -1746,7 +1742,9 @@ size_t Peer::ConstantMemoryUsage()
size_t PeerManagerImpl::PeerDynamicMemoryUsage(NodeId nodeid)
{
PeerRef peer = GetPeerRef(nodeid);
if (!peer) return 0;
size_t dy = 0;
if (0) dy += peer->ConstantMemoryUsage();

{
LOCK(peer->m_block_inv_mutex);
Expand All @@ -1756,15 +1754,32 @@ size_t PeerManagerImpl::PeerDynamicMemoryUsage(NodeId nodeid)

WITH_LOCK(peer->m_tx_relay_mutex, dy += memusage::DynamicUsage(peer->m_tx_relay.get()));

{
LOCK(NetEventsInterface::g_msgproc_mutex);
dy += memusage::DynamicUsage(peer->m_addrs_to_send);
dy += memusage::DynamicUsage(peer->m_addr_known);
}
dy += peer->inaccessible_dyn_memusage;

WITH_LOCK(peer->m_getdata_requests_mutex, dy += memusage::DynamicUsage(peer->m_getdata_requests));
WITH_LOCK(peer->m_headers_sync_mutex, dy += memusage::DynamicUsage(peer->m_headers_sync));

LOCK(peer->m_tx_relay_mutex);
if (peer->m_tx_relay) {
size_t zzzz{0}, xxxx{0}, yyyy{0};
zzzz += memusage::DynamicUsage(peer->m_tx_relay);
{
LOCK(peer->m_tx_relay->m_bloom_filter_mutex);
zzzz += memusage::DynamicUsage(peer->m_tx_relay->m_bloom_filter);
if (peer->m_tx_relay->m_bloom_filter) {
xxxx += peer->m_tx_relay->m_bloom_filter->DynamicUsage();
}
}
{
LOCK(peer->m_tx_relay->m_tx_inventory_mutex);
yyyy += peer->m_tx_relay->m_tx_inventory_known_filter.DynamicUsage();
zzzz += memusage::DynamicUsage(peer->m_tx_relay->m_tx_inventory_to_send);
}
LogPrintf("ABCD: peer memusage %d m_tx_relay %d %d %d\n", dy, zzzz, xxxx, yyyy);
dy += zzzz + xxxx + yyyy;
} else {
LogPrintf("ABCD: peer memusage %d no m_tx_relay??\n", dy);
}
return dy;
}

Expand Down Expand Up @@ -5126,6 +5141,10 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
PeerRef peer = GetPeerRef(pfrom->GetId());
if (peer == nullptr) return false;

peer->inaccessible_dyn_memusage = (
memusage::DynamicUsage(peer->m_addrs_to_send)
+ memusage::DynamicUsage(peer->m_addr_known));

{
LOCK(peer->m_getdata_requests_mutex);
if (!peer->m_getdata_requests.empty()) {
Expand Down

0 comments on commit a25c685

Please sign in to comment.