Skip to content

Commit 20d2e7b

Browse files
committed
FreeBSD patches
1 parent 283f6ed commit 20d2e7b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-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: 37 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,20 @@ size_t get_current_rss() {
7373
#include <mach/mach.h>
7474
#endif
7575

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

0 commit comments

Comments
 (0)