Skip to content

Commit

Permalink
Merge bitcoin#10038: Add mallocinfo mode to getmemoryinfo RPC
Browse files Browse the repository at this point in the history
e141aa4 Add mallocinfo mode to `getmemoryinfo` RPC (Wladimir J. van der Laan)

Tree-SHA512: e778631765c29b3b5fb94eb66e5f50a8f108a234891bdcc4883f1e6e2fdd223f7660fad987eb2d7cbda5b800482d78adc1a309a3f6f83a84c556af43ebee2ed7
  • Loading branch information
laanwj authored and PastaPastaPasta committed Mar 13, 2019
1 parent 25dead5 commit c94272a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
8 changes: 8 additions & 0 deletions configure.ac
Expand Up @@ -600,6 +600,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
[ AC_MSG_RESULT(no)]
)

dnl Check for malloc_info (for memory statistics information in getmemoryinfo)
AC_MSG_CHECKING(for getmemoryinfo)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
[[ int f = malloc_info(0, NULL); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_MALLOC_INFO, 1,[Define this symbol if you have malloc_info]) ],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING([for visibility attribute])
AC_LINK_IFELSE([AC_LANG_SOURCE([
int foo_def( void ) __attribute__((visibility("default")));
Expand Down
54 changes: 47 additions & 7 deletions src/rpc/misc.cpp
Expand Up @@ -25,6 +25,9 @@
#include "spork.h"

#include <stdint.h>
#ifdef HAVE_MALLOC_INFO
#include <malloc.h>
#endif

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string.hpp>
Expand Down Expand Up @@ -1107,16 +1110,39 @@ static UniValue RPCLockedMemoryInfo()
return obj;
}

#ifdef HAVE_MALLOC_INFO
static std::string RPCMallocInfo()
{
char *ptr = nullptr;
size_t size = 0;
FILE *f = open_memstream(&ptr, &size);
if (f) {
malloc_info(0, f);
fclose(f);
if (ptr) {
std::string rv(ptr, size);
free(ptr);
return rv;
}
}
return "";
}
#endif

UniValue getmemoryinfo(const JSONRPCRequest& request)
{
/* Please, avoid using the word "pool" here in the RPC interface or help,
* as users will undoubtedly confuse it with the other "memory pool"
*/
if (request.fHelp || request.params.size() != 0)
if (request.fHelp || request.params.size() > 1)
throw std::runtime_error(
"getmemoryinfo\n"
"getmemoryinfo (\"mode\")\n"
"Returns an object containing information about memory usage.\n"
"\nResult:\n"
"Arguments:\n"
"1. \"mode\" determines what kind of information is returned. This argument is optional, the default mode is \"stats\".\n"
" - \"stats\" returns general statistics about memory usage in the daemon.\n"
" - \"mallocinfo\" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+).\n"
"\nResult (mode \"stats\"):\n"
"{\n"
" \"locked\": { (json object) Information about locked memory manager\n"
" \"used\": xxxxx, (numeric) Number of bytes used\n"
Expand All @@ -1127,13 +1153,27 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
" \"chunks_free\": xxxxx, (numeric) Number unused chunks\n"
" }\n"
"}\n"
"\nResult (mode \"mallocinfo\"):\n"
"\"<malloc version=\"1\">...\"\n"
"\nExamples:\n"
+ HelpExampleCli("getmemoryinfo", "")
+ HelpExampleRpc("getmemoryinfo", "")
);
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("locked", RPCLockedMemoryInfo()));
return obj;

std::string mode = (request.params.size() < 1 || request.params[0].isNull()) ? "stats" : request.params[0].get_str();
if (mode == "stats") {
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("locked", RPCLockedMemoryInfo()));
return obj;
} else if (mode == "mallocinfo") {
#ifdef HAVE_MALLOC_INFO
return RPCMallocInfo();
#else
throw JSONRPCError(RPC_INVALID_PARAMETER, "mallocinfo is only available when compiled with glibc 2.10+");
#endif
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode);
}
}

UniValue echo(const JSONRPCRequest& request)
Expand All @@ -1154,7 +1194,7 @@ static const CRPCCommand commands[] =
// --------------------- ------------------------ ----------------------- ----------
{ "control", "debug", &debug, true, {} },
{ "control", "getinfo", &getinfo, true, {} }, /* uses wallet if enabled */
{ "control", "getmemoryinfo", &getmemoryinfo, true, {} },
{ "control", "getmemoryinfo", &getmemoryinfo, true, {"mode"} },
{ "util", "validateaddress", &validateaddress, true, {"address"} }, /* uses wallet if enabled */
{ "util", "createmultisig", &createmultisig, true, {"nrequired","keys"} },
{ "util", "verifymessage", &verifymessage, true, {"address","signature","message"} },
Expand Down

0 comments on commit c94272a

Please sign in to comment.