diff --git a/CMake/AddDependencies.cmake b/CMake/AddDependencies.cmake index 584fcb011..9655d217b 100644 --- a/CMake/AddDependencies.cmake +++ b/CMake/AddDependencies.cmake @@ -85,4 +85,7 @@ function(link_dependencies TARGET) # Prettify jsoncpp_static name in VS solution explorer: set_property(TARGET jsoncpp_static PROPERTY PROJECT_LABEL "jsoncpp") + if(${CMAKE_SYSTEM_NAME} MATCHES FreeBSD) + target_link_libraries(${TARGET} PRIVATE kvm) + endif() endfunction() diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 175218c50..e8c1bbb47 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -49,6 +49,7 @@ mBornand MeMuXin mgueydan MikeHunsinger +Morritz (TJ) morsmordere (Anzhelika Iugai) mtilden nesco diff --git a/src/Root.cpp b/src/Root.cpp index 62d50c28c..f100a242a 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -20,6 +20,11 @@ #endif #elif defined(__APPLE__) #include + #elif defined(__FreeBSD__) + #include + #include + #include + #include #endif #endif @@ -885,6 +890,37 @@ int cRoot::GetPhysicalRAMUsage(void) return static_cast(t_info.resident_size / 1024); } return -1; + #elif defined (__FreeBSD__) + /* + struct rusage self_usage; + int status = getrusage(RUSAGE_SELF, &self_usage); + if (!status) + { + return static_cast(self_usage.ru_maxrss); + } + return -1; + */ + // Good to watch: https://www.youtube.com/watch?v=Os5cK0H8EOA - getrusage. + // Unfortunately, it only gives peak memory usage a.k.a max resident set size + // So it is better to use FreeBSD kvm function to get the size of resident pages. + + static kvm_t* kd = NULL; + + if (kd == NULL) + { + kd = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "kvm_open"); // returns a descriptor used to access kernel virtual memory + } + if (kd != NULL) + { + int pc = 0; // number of processes found + struct kinfo_proc* kp; + kp = kvm_getprocs(kd, KERN_PROC_PID, getpid(), &pc); + if ((kp != NULL) && (pc >= 1)) + { + return static_cast(kp->ki_rssize * getpagesize() / 1024); + } + } + return -1; #else LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__); return -1;