Skip to content

Commit

Permalink
RPC getpeermemoryinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
amitiuttarwar committed Nov 6, 2023
1 parent c9cc0b4 commit 03a7ac9
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,57 @@ static RPCHelpMan ping()
};
}

static RPCHelpMan getpeermemoryinfo()
{
return RPCHelpMan{
"getpeermemoryinfo",
"\n how much memory does each peer use? \n",
{},
RPCResult{
RPCResult::Type::ARR, "", "",
{
{RPCResult::Type::OBJ, "", "",
{
{
{RPCResult::Type::NUM, "id", "Peer id"},
{RPCResult::Type::NUM, "dynamic_cnode_memory", "the result of CNode::DynamicMemoryUsage"},
{RPCResult::Type::NUM, "dynamic_peer_memory", "the result of PeerManagerImpl::GetPeerMemory"},
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"},
}},
}},
},
RPCExamples{
HelpExampleCli("getpeermemoryinfo", "")
+ HelpExampleRpc("getpeermemoryinfo", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = EnsureConnman(node);
PeerManager& peerman = EnsurePeerman(node);

UniValue ret(UniValue::VARR);

// map of node id -> memory usage
std::map<int64_t, size_t> info;
// call DynamicMemoryUsage on all CNode objects
connman.GetNodeMemory(info);
for (auto& [node_id, node_memory] : info) {
UniValue obj(UniValue::VOBJ);
obj.pushKV("id", node_id);
obj.pushKV("dynamic_cnode_memory", node_memory);

auto dynamic_peer_memory = peerman.PeerDynamicMemoryUsage(node_id);
obj.pushKV("dynamic_peer_memory", dynamic_peer_memory);

obj.pushKV("connection_type", connman.NodeToString(node_id));
ret.push_back(obj);
}

return ret;
},
};
}
static RPCHelpMan getpeerinfo()
{
return RPCHelpMan{
Expand Down Expand Up @@ -1154,6 +1205,7 @@ void RegisterNetRPCCommands(CRPCTable& t)
{"network", &getconnectioncount},
{"network", &ping},
{"network", &getpeerinfo},
{"network", &getpeermemoryinfo},
{"network", &addnode},
{"network", &disconnectnode},
{"network", &getaddednodeinfo},
Expand Down

0 comments on commit 03a7ac9

Please sign in to comment.