From 5eecaa0b9c860858372a22ab242566671907c913 Mon Sep 17 00:00:00 2001 From: Yuri Victorovich Date: Wed, 31 Aug 2022 19:17:21 -0700 Subject: [PATCH] FreeBSD patches --- src/CMakeLists.txt | 5 +++++ src/util/debug.cpp | 4 ++++ src/util/memory.cpp | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7ad2624131..14f0995d92 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -298,6 +298,11 @@ else() set(EXTRA_LIBS ${EXTRA_LIBS} ${GMP_LIBRARIES}) endif() +# system-specific libraries +if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + set(EXTRA_UTIL_LIBS ${EXTRA_UTIL_LIBS} "-lprocstat") +endif() + # DL if (EMSCRIPTEN) # no dlopen diff --git a/src/util/debug.cpp b/src/util/debug.cpp index 7cdd684698..f4b5f87ad2 100644 --- a/src/util/debug.cpp +++ b/src/util/debug.cpp @@ -118,7 +118,11 @@ void invoke_debugger() { case 'g': { std::cerr << "INVOKING GDB...\n"; std::ostringstream buffer; +#if defined(__FreeBSD__) + buffer << "gdb -nw /proc/" << getpid() << "/file " << getpid(); +#else buffer << "gdb -nw /proc/" << getpid() << "/exe " << getpid(); +#endif if (system(buffer.str().c_str()) == 0) { std::cerr << "continuing the execution...\n"; } else { diff --git a/src/util/memory.cpp b/src/util/memory.cpp index 3b748c5302..1fd4d61fcb 100644 --- a/src/util/memory.cpp +++ b/src/util/memory.cpp @@ -64,7 +64,7 @@ size_t get_current_rss() { --------------------------------------------------- */ #else /* ---------------------------------------------------- - Linux/OSX version for get_peak_rss and get_current_rss + Linux/FreeBSD/OSX version for get_peak_rss and get_current_rss --------------------------------------------------- */ #include #include @@ -73,6 +73,18 @@ size_t get_current_rss() { #include #endif +#if defined(__FreeBSD__) +// for procstat_* +#include +#include +#include +#include +// for struct kinfo_proc +#include +// for KERN_PROC_PID +#include +#endif + namespace lean { size_t get_peak_rss() { struct rusage rusage; @@ -91,6 +103,28 @@ size_t get_current_rss() { if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast(&info), &infoCount) != KERN_SUCCESS) return static_cast(0); return static_cast(info.resident_size); +#elif defined(__FreeBSD__) + // initialize + unsigned int count = 0; + + struct procstat *pstat = procstat_open_sysctl(); + if (!pstat) + return static_cast(0); + + struct kinfo_proc *kp = procstat_getprocs(pstat, KERN_PROC_PID, getpid(), &count); + if (!kp) { + procstat_close(pstat); + return static_cast(0); + } + + // compute + size_t rss_size = kp->ki_rssize*getpagesize(); + + // cleanup + procstat_freeprocs(pstat, kp); + procstat_close(pstat); + + return rss_size; #else long rss = 0; FILE * fp = nullptr;