-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
79 lines (55 loc) · 1.5 KB
/
Copy pathmemory.cpp
File metadata and controls
79 lines (55 loc) · 1.5 KB
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
#include "memory.h"
Memory::Memory(QObject *parent) : QObject{parent},
m_timer(new QTimer(this)),
getOK(0)
{
m_timer.setInterval(1000);
connect(&m_timer, &QTimer::timeout, this, [=](){
Memory::getMemory();
if(getOK) {
setMemory(m_tempMem);
}
});
m_timer.start();
}
QVariantMap Memory::memory() const
{
return m_memory;
}
void Memory::setMemory(const QVariantMap &newMemory)
{
if (m_memory != newMemory) {
m_memory = newMemory;
emit memoryChanged(m_memory);
}
}
QString Memory::dataFromCpp(QString key)
{
return QString::number(m_memory.value(key).toDouble(), 'f', 2);
}
void Memory::getMemory()
{
QFile memFile("/proc/meminfo");
if (!memFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open memFile";
getOK = 0;
return;
}
QTextStream in(&memFile);
static QRegularExpression re("\\s+");
int count = 3;
while (count--) {
QString line = in.readLine();
QStringList parts = line.split(re, Qt::SkipEmptyParts);
if (parts.size() >= 2) {
QString key = parts[0].remove(":");
QString value = parts[1];
if (key == "MemTotal" || key == "MemFree") {
m_tempMem[key] = value.toDouble() / (1024 * 1024);
}
}
}
getOK = 1;
memFile.close();
m_tempMem["MemUsage"] = 100 - (100 * m_tempMem["MemFree"].toDouble() / m_tempMem["MemTotal"].toDouble());
}