20
20
#endif
21
21
22
22
#include < stdint.h>
23
+ #ifdef HAVE_MALLOC_INFO
24
+ #include < malloc.h>
25
+ #endif
23
26
24
27
#include < boost/assign/list_of.hpp>
25
28
@@ -485,16 +488,39 @@ static UniValue RPCLockedMemoryInfo()
485
488
return obj;
486
489
}
487
490
491
+ #ifdef HAVE_MALLOC_INFO
492
+ static std::string RPCMallocInfo ()
493
+ {
494
+ char *ptr = nullptr ;
495
+ size_t size = 0 ;
496
+ FILE *f = open_memstream (&ptr, &size);
497
+ if (f) {
498
+ malloc_info (0 , f);
499
+ fclose (f);
500
+ if (ptr) {
501
+ std::string rv (ptr, size);
502
+ free (ptr);
503
+ return rv;
504
+ }
505
+ }
506
+ return " " ;
507
+ }
508
+ #endif
509
+
488
510
UniValue getmemoryinfo (const JSONRPCRequest& request)
489
511
{
490
512
/* Please, avoid using the word "pool" here in the RPC interface or help,
491
513
* as users will undoubtedly confuse it with the other "memory pool"
492
514
*/
493
- if (request.fHelp || request.params .size () != 0 )
515
+ if (request.fHelp || request.params .size () > 1 )
494
516
throw std::runtime_error (
495
- " getmemoryinfo\n "
517
+ " getmemoryinfo ( \" mode \" ) \n "
496
518
" Returns an object containing information about memory usage.\n "
497
- " \n Result:\n "
519
+ " Arguments:\n "
520
+ " 1. \" mode\" determines what kind of information is returned. This argument is optional, the default mode is \" stats\" .\n "
521
+ " - \" stats\" returns general statistics about memory usage in the daemon.\n "
522
+ " - \" mallocinfo\" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+).\n "
523
+ " \n Result (mode \" stats\" ):\n "
498
524
" {\n "
499
525
" \" locked\" : { (json object) Information about locked memory manager\n "
500
526
" \" used\" : xxxxx, (numeric) Number of bytes used\n "
@@ -505,13 +531,27 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
505
531
" \" chunks_free\" : xxxxx, (numeric) Number unused chunks\n "
506
532
" }\n "
507
533
" }\n "
534
+ " \n Result (mode \" mallocinfo\" ):\n "
535
+ " \" <malloc version=\" 1\" >...\"\n "
508
536
" \n Examples:\n "
509
537
+ HelpExampleCli (" getmemoryinfo" , " " )
510
538
+ HelpExampleRpc (" getmemoryinfo" , " " )
511
539
);
512
- UniValue obj (UniValue::VOBJ);
513
- obj.push_back (Pair (" locked" , RPCLockedMemoryInfo ()));
514
- return obj;
540
+
541
+ std::string mode = (request.params .size () < 1 || request.params [0 ].isNull ()) ? " stats" : request.params [0 ].get_str ();
542
+ if (mode == " stats" ) {
543
+ UniValue obj (UniValue::VOBJ);
544
+ obj.push_back (Pair (" locked" , RPCLockedMemoryInfo ()));
545
+ return obj;
546
+ } else if (mode == " mallocinfo" ) {
547
+ #ifdef HAVE_MALLOC_INFO
548
+ return RPCMallocInfo ();
549
+ #else
550
+ throw JSONRPCError (RPC_INVALID_PARAMETER, " mallocinfo is only available when compiled with glibc 2.10+" );
551
+ #endif
552
+ } else {
553
+ throw JSONRPCError (RPC_INVALID_PARAMETER, " unknown mode " + mode);
554
+ }
515
555
}
516
556
517
557
UniValue echo (const JSONRPCRequest& request)
@@ -531,7 +571,7 @@ static const CRPCCommand commands[] =
531
571
{ // category name actor (function) okSafeMode
532
572
// --------------------- ------------------------ ----------------------- ----------
533
573
{ " control" , " getinfo" , &getinfo, true , {} }, /* uses wallet if enabled */
534
- { " control" , " getmemoryinfo" , &getmemoryinfo, true , {} },
574
+ { " control" , " getmemoryinfo" , &getmemoryinfo, true , {" mode " } },
535
575
{ " util" , " validateaddress" , &validateaddress, true , {" address" } }, /* uses wallet if enabled */
536
576
{ " util" , " createmultisig" , &createmultisig, true , {" nrequired" ," keys" } },
537
577
{ " util" , " verifymessage" , &verifymessage, true , {" address" ," signature" ," message" } },
0 commit comments