diff options
author | Jose Alonso Cardenas Marquez <acm@FreeBSD.org> | 2024-06-12 19:08:35 -0500 |
---|---|---|
committer | Jose Alonso Cardenas Marquez <acm@FreeBSD.org> | 2024-06-12 19:08:35 -0500 |
commit | 539c968785234fe925d0795081280c9689207d0b (patch) | |
tree | 2be24f7781c4bdd151f99c90d1e93f074c40e7e1 /sysutils/libsysstat-qt6/files/patch-memstat.cpp | |
parent | devel/libqt6xdg: New port: Qt6 implementation of freedesktop.org xdg specs (diff) |
sysutils/libsysstat-qt6: New port: Library used to query system info and statistics ported to qt6
Qt-based library to query system information like CPU, memory usage and
network traffic.
PR: 278905
Approved by: jsm
Diffstat (limited to '')
-rw-r--r-- | sysutils/libsysstat-qt6/files/patch-memstat.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/sysutils/libsysstat-qt6/files/patch-memstat.cpp b/sysutils/libsysstat-qt6/files/patch-memstat.cpp new file mode 100644 index 000000000000..78e493983d06 --- /dev/null +++ b/sysutils/libsysstat-qt6/files/patch-memstat.cpp @@ -0,0 +1,123 @@ +--- memstat.cpp.orig 2021-11-05 10:06:40 UTC ++++ memstat.cpp +@@ -26,10 +26,58 @@ + + #include "memstat.h" + #include "memstat_p.h" ++#if defined(HAVE_KVM_H) && defined(HAVE_SYSCTL_H) ++extern "C" ++{ ++ #include <paths.h> ++ #include <unistd.h> ++ #include <fcntl.h> + ++ #include <kvm.h> ++ #include <sys/types.h> ++ #include <sys/sysctl.h> ++} ++#endif + + namespace SysStat { ++#ifdef HAVE_SYSCTL_H ++int SwapDevices() ++{ ++ int buf; ++ size_t len = sizeof(int); + ++ if (sysctlbyname("vm.nswapdev", &buf, &len, NULL, 0) < 0) ++ return 0; ++ else ++ return buf; ++} ++ ++qulonglong MemGetByBytes(QString property) ++{ ++ qulonglong buf=0; ++ size_t len = sizeof(qulonglong); ++ ++ std::string s = property.toStdString(); ++ const char *name = s.c_str(); ++ ++ if (sysctlbyname(name, &buf, &len, NULL, 0) < 0) ++ return 0; ++ else ++ return buf; ++} ++ ++qulonglong MemGetByPages(QString name) ++{ ++ qulonglong res = 0; ++ ++ ++ res = MemGetByBytes(name); ++ if (res > 0) ++ res = res * getpagesize(); ++ ++ return res; ++} ++#endif + MemStatPrivate::MemStatPrivate(MemStat *parent) + : BaseStatPrivate(parent) + { +@@ -49,8 +97,39 @@ void MemStatPrivate::timeout() + qulonglong memBuffers = 0; + qulonglong memCached = 0; + qulonglong swapTotal = 0; +- qulonglong swapFree = 0; ++#ifdef HAVE_SYSCTL_H ++ memTotal = MemGetByBytes(QLatin1String("hw.physmem")); ++ memFree = MemGetByPages(QLatin1String("vm.stats.vm.v_free_count")); ++ memBuffers = MemGetByBytes(QLatin1String("vfs.bufspace")); ++ memCached = MemGetByPages(QLatin1String("vm.stats.vm.v_inactive_count")); + ++#endif ++#ifdef HAVE_KVM_H ++ qulonglong swapUsed = 0; ++ kvm_t *kd; ++ struct kvm_swap kswap[16]; /* size taken from pstat/pstat.c */ ++ ++ kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); ++ if (kd == NULL) ++ kvm_close(kd); ++ ++ if (kvm_getswapinfo(kd, kswap, (sizeof(kswap) / sizeof(kswap[0])), SWIF_DEV_PREFIX) > 0) ++ { ++ int swapd = SwapDevices(); ++ /* TODO: loop over swap devives */ ++ if (swapd >= 1) ++ { ++ swapTotal = static_cast<qulonglong>(kswap[0].ksw_total * getpagesize()); ++ swapUsed = static_cast<qulonglong>(kswap[0].ksw_used * getpagesize()); ++ } ++ ++ kvm_close(kd); ++ } ++ else ++ kvm_close(kd); ++#endif ++#ifndef HAVE_SYSCTL_H ++ qulonglong swapFree = 0; + const QStringList rows = readAllFile("/proc/meminfo").split(QLatin1Char('\n'), Qt::SkipEmptyParts); + for (const QString &row : rows) + { +@@ -72,6 +151,7 @@ void MemStatPrivate::timeout() + swapFree = tokens[1].toULong(); + } + ++#endif + if (mSource == QLatin1String("memory")) + { + if (memTotal) +@@ -88,8 +168,11 @@ void MemStatPrivate::timeout() + { + if (swapTotal) + { ++#ifndef HAVE_KVM_H + float swapUsed_d = static_cast<float>(swapTotal - swapFree) / static_cast<float>(swapTotal); +- ++#else ++ float swapUsed_d = static_cast<float>(swapUsed) / static_cast<float>(swapTotal); ++#endif + emit swapUpdate(swapUsed_d); + } + } |