1
0

Fix: GetPhysicalRamUsage on FreeBSD (UNIX) - webadmin display (#5213)

* Fix: GetPhysicalRamUsage on FreeBSD (UNIX) - webadmin display

* fixed cast type

* Fix: GetPhysicalRamUsage on FreeBSD - webadmin display / style and failing build fixes

* added myself to contributors

Co-authored-by: 12xx12 <44411062+12xx12@users.noreply.github.com>
This commit is contained in:
Morritz 2021-04-30 15:23:31 +02:00 committed by GitHub
parent cbfc740ad0
commit a4eba7639e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View File

@ -85,4 +85,7 @@ function(link_dependencies TARGET)
# Prettify jsoncpp_static name in VS solution explorer: # Prettify jsoncpp_static name in VS solution explorer:
set_property(TARGET jsoncpp_static PROPERTY PROJECT_LABEL "jsoncpp") set_property(TARGET jsoncpp_static PROPERTY PROJECT_LABEL "jsoncpp")
if(${CMAKE_SYSTEM_NAME} MATCHES FreeBSD)
target_link_libraries(${TARGET} PRIVATE kvm)
endif()
endfunction() endfunction()

View File

@ -49,6 +49,7 @@ mBornand
MeMuXin MeMuXin
mgueydan mgueydan
MikeHunsinger MikeHunsinger
Morritz (TJ)
morsmordere (Anzhelika Iugai) morsmordere (Anzhelika Iugai)
mtilden mtilden
nesco nesco

View File

@ -20,6 +20,11 @@
#endif #endif
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include <mach/mach.h> #include <mach/mach.h>
#elif defined(__FreeBSD__)
#include <kvm.h>
#include <fcntl.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#endif #endif
#endif #endif
@ -885,6 +890,37 @@ int cRoot::GetPhysicalRAMUsage(void)
return static_cast<int>(t_info.resident_size / 1024); return static_cast<int>(t_info.resident_size / 1024);
} }
return -1; return -1;
#elif defined (__FreeBSD__)
/*
struct rusage self_usage;
int status = getrusage(RUSAGE_SELF, &self_usage);
if (!status)
{
return static_cast<int>(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<int>(kp->ki_rssize * getpagesize() / 1024);
}
}
return -1;
#else #else
LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__); LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__);
return -1; return -1;