diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 96d06b6b9f0e7e..913f0e2d269730 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -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 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{ @@ -1154,6 +1205,7 @@ void RegisterNetRPCCommands(CRPCTable& t) {"network", &getconnectioncount}, {"network", &ping}, {"network", &getpeerinfo}, + {"network", &getpeermemoryinfo}, {"network", &addnode}, {"network", &disconnectnode}, {"network", &getaddednodeinfo},