Skip to content

Commit da3504a

Browse files
make use of mallinfo2 conditional on either being compiled with gllibc>=2.33 or being able to dynload it, otherwise fall back to mallinfo
Signed-off-by: Alexander Balabin <abalabin@bamfunds.com>
1 parent 71de659 commit da3504a

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

  • rust/perspective-server/cpp/perspective/src/cpp

rust/perspective-server/cpp/perspective/src/cpp/server.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#if defined(PSP_ENABLE_WASM) || defined(__linux__)
4242
#include <malloc.h>
43+
#include <dlfcn.h>
4344
#endif
4445

4546
#if !defined(WIN32) && !defined(PSP_ENABLE_WASM)
@@ -820,7 +821,7 @@ ProtoServer::handle_request(
820821
) {
821822
const auto start = std::chrono::high_resolution_clock::now();
822823
proto::Request req_env;
823-
req_env.ParseFromString(data);
824+
req_env.ParseFromArray(data.data(), data.size());
824825
std::vector<ProtoServerResp<std::string>> serialized_responses;
825826
std::vector<proto::Response> responses;
826827

@@ -2999,9 +3000,41 @@ ProtoServer::_handle_request(std::uint32_t client_id, Request&& req) {
29993000
const auto res = mallinfo();
30003001
sys_info->set_used_size(*reinterpret_cast<const unsigned int*>(&res.uordblks));
30013002
#elif defined(__linux__) && !defined(PSP_ENABLE_WASM)
3002-
auto res = mallinfo2();
3003-
sys_info->set_heap_size(res.usmblks);
3003+
#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 33)
3004+
const auto res = mallinfo2();
3005+
sys_info->set_heap_size(res.arena);
30043006
sys_info->set_used_size(res.uordblks);
3007+
#else
3008+
static const auto mallinfo2 = [](){
3009+
struct mallinfo2 {
3010+
size_t arena; /* Non-mmapped space allocated (bytes) */
3011+
size_t ordblks; /* Number of free chunks */
3012+
size_t smblks; /* Number of free fastbin blocks */
3013+
size_t hblks; /* Number of mmapped regions */
3014+
size_t hblkhd; /* Space allocated in mmapped regions
3015+
(bytes) */
3016+
size_t usmblks; /* See below */
3017+
size_t fsmblks; /* Space in freed fastbin blocks (bytes) */
3018+
size_t uordblks; /* Total allocated space (bytes) */
3019+
size_t fordblks; /* Total free space (bytes) */
3020+
size_t keepcost; /* Top-most, releasable space (bytes) */
3021+
};
3022+
#ifdef _GNU_SOURCE
3023+
return (mallinfo2(*)())dlsym(RTLD_DEFAULT, "mallinfo2");
3024+
#else
3025+
return nullptr;
3026+
#endif
3027+
}();
3028+
if (mallinfo2) {
3029+
const auto res = mallinfo2();
3030+
sys_info->set_heap_size(res.arena);
3031+
sys_info->set_used_size(res.uordblks);
3032+
} else { // Fallback to mallinfo if mallinfo2 is not available
3033+
const auto res = mallinfo();
3034+
sys_info->set_heap_size(*reinterpret_cast<const unsigned int*>(&res.arena));
3035+
sys_info->set_used_size(*reinterpret_cast<const unsigned int*>(&res.uordblks));
3036+
}
3037+
#endif
30053038
#elif defined(__APPLE__) && !defined(PSP_ENABLE_WASM)
30063039
rusage out;
30073040
getrusage(RUSAGE_SELF, &out);

0 commit comments

Comments
 (0)