1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
--- memstat.cpp.orig 2019-01-24 21:43:32 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)
{
@@ -52,7 +100,37 @@ void MemStatPrivate::timeout()
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
const QStringList rows = readAllFile("/proc/meminfo").split(QLatin1Char('\n'), QString::SkipEmptyParts);
for (const QString &row : rows)
{
@@ -73,7 +151,7 @@ void MemStatPrivate::timeout()
else if(tokens[0] == QLatin1String("SwapFree:"))
swapFree = tokens[1].toULong();
}
-
+#endif
if (mSource == QLatin1String("memory"))
{
if (memTotal)
@@ -90,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);
}
}
|