Skip to content

Commit 5eecaa0

Browse files
committed
FreeBSD patches
1 parent 283f6ed commit 5eecaa0

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ else()
298298
set(EXTRA_LIBS ${EXTRA_LIBS} ${GMP_LIBRARIES})
299299
endif()
300300

301+
# system-specific libraries
302+
if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
303+
set(EXTRA_UTIL_LIBS ${EXTRA_UTIL_LIBS} "-lprocstat")
304+
endif()
305+
301306
# DL
302307
if (EMSCRIPTEN)
303308
# no dlopen

src/util/debug.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ void invoke_debugger() {
118118
case 'g': {
119119
std::cerr << "INVOKING GDB...\n";
120120
std::ostringstream buffer;
121+
#if defined(__FreeBSD__)
122+
buffer << "gdb -nw /proc/" << getpid() << "/file " << getpid();
123+
#else
121124
buffer << "gdb -nw /proc/" << getpid() << "/exe " << getpid();
125+
#endif
122126
if (system(buffer.str().c_str()) == 0) {
123127
std::cerr << "continuing the execution...\n";
124128
} else {

src/util/memory.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ size_t get_current_rss() {
6464
--------------------------------------------------- */
6565
#else
6666
/* ----------------------------------------------------
67-
Linux/OSX version for get_peak_rss and get_current_rss
67+
Linux/FreeBSD/OSX version for get_peak_rss and get_current_rss
6868
--------------------------------------------------- */
6969
#include <unistd.h>
7070
#include <sys/resource.h>
@@ -73,6 +73,18 @@ size_t get_current_rss() {
7373
#include <mach/mach.h>
7474
#endif
7575

76+
#if defined(__FreeBSD__)
77+
// for procstat_*
78+
#include <sys/param.h>
79+
#include <sys/queue.h>
80+
#include <sys/socket.h>
81+
#include <libprocstat.h>
82+
// for struct kinfo_proc
83+
#include <sys/user.h>
84+
// for KERN_PROC_PID
85+
#include <sys/sysctl.h>
86+
#endif
87+
7688
namespace lean {
7789
size_t get_peak_rss() {
7890
struct rusage rusage;
@@ -91,6 +103,28 @@ size_t get_current_rss() {
91103
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), &infoCount) != KERN_SUCCESS)
92104
return static_cast<size_t>(0);
93105
return static_cast<size_t>(info.resident_size);
106+
#elif defined(__FreeBSD__)
107+
// initialize
108+
unsigned int count = 0;
109+
110+
struct procstat *pstat = procstat_open_sysctl();
111+
if (!pstat)
112+
return static_cast<size_t>(0);
113+
114+
struct kinfo_proc *kp = procstat_getprocs(pstat, KERN_PROC_PID, getpid(), &count);
115+
if (!kp) {
116+
procstat_close(pstat);
117+
return static_cast<size_t>(0);
118+
}
119+
120+
// compute
121+
size_t rss_size = kp->ki_rssize*getpagesize();
122+
123+
// cleanup
124+
procstat_freeprocs(pstat, kp);
125+
procstat_close(pstat);
126+
127+
return rss_size;
94128
#else
95129
long rss = 0;
96130
FILE * fp = nullptr;

0 commit comments

Comments
 (0)