Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/util/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 35 additions & 1 deletion src/util/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unistd.h>
#include <sys/resource.h>
Expand All @@ -73,6 +73,18 @@ size_t get_current_rss() {
#include <mach/mach.h>
#endif

#if defined(__FreeBSD__)
// for procstat_*
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <libprocstat.h>
// for struct kinfo_proc
#include <sys/user.h>
// for KERN_PROC_PID
#include <sys/sysctl.h>
#endif

namespace lean {
size_t get_peak_rss() {
struct rusage rusage;
Expand All @@ -91,6 +103,28 @@ size_t get_current_rss() {
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), &infoCount) != KERN_SUCCESS)
return static_cast<size_t>(0);
return static_cast<size_t>(info.resident_size);
#elif defined(__FreeBSD__)
// initialize
unsigned int count = 0;

struct procstat *pstat = procstat_open_sysctl();
if (!pstat)
return static_cast<size_t>(0);

struct kinfo_proc *kp = procstat_getprocs(pstat, KERN_PROC_PID, getpid(), &count);
if (!kp) {
procstat_close(pstat);
return static_cast<size_t>(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;
Expand Down