summaryrefslogtreecommitdiff
path: root/src/monitor-lib
diff options
context:
space:
mode:
authorBernd Weimer <bernd.weimer@pelagicore.com>2020-01-21 08:26:19 +0100
committerBernd Weimer <bernd.weimer@pelagicore.com>2020-01-21 16:18:23 +0100
commit29c8cec06482fab8816eae2efcff738f498be371 (patch)
treec8611fe00d419bce126bc8a2f67db3112ec6a8b3 /src/monitor-lib
parentb05e55717ecb0affa00ac337aecf7066aa78a2a5 (diff)
downloadqtapplicationmanager-29c8cec06482fab8816eae2efcff738f498be371.tar.gz
Revert back to using a mutex for process readings
Get rid of wanring "load() is deprecated". Besides, one mutex should perform better than ten atomic ints. On my machine it's roughly twice as fast. Change-Id: I35254604b7739f44011b3799c5400a5d0140949a Reviewed-by: Robert Griebl <robert.griebl@qt.io>
Diffstat (limited to 'src/monitor-lib')
-rw-r--r--src/monitor-lib/processreader.cpp96
-rw-r--r--src/monitor-lib/processreader.h47
2 files changed, 63 insertions, 80 deletions
diff --git a/src/monitor-lib/processreader.cpp b/src/monitor-lib/processreader.cpp
index e8b760a9..bbd966f4 100644
--- a/src/monitor-lib/processreader.cpp
+++ b/src/monitor-lib/processreader.cpp
@@ -40,6 +40,7 @@
**
****************************************************************************/
+#include <QMutexLocker>
#include "processreader.h"
#include "logging.h"
@@ -63,32 +64,27 @@ void ProcessReader::enableMemoryReporting(bool enabled)
{
m_memoryReportingEnabled = enabled;
if (!m_memoryReportingEnabled)
- zeroMemory();
+ memory = Memory();
}
void ProcessReader::update()
{
- cpuLoad.store(static_cast<quint32>(cpuLoadFactor * readCpuLoad()));
-
- if (m_memoryReportingEnabled && !readMemory())
- zeroMemory();
+ qreal load = readCpuLoad();
+
+ if (m_memoryReportingEnabled) {
+ Memory mem;
+ bool memRead = readMemory(mem);
+ QMutexLocker locker(&mutex);
+ memory = memRead ? mem : Memory();
+ cpuLoad = load;
+ } else {
+ QMutexLocker locker(&mutex);
+ cpuLoad = load;
+ }
emit updated();
}
-void ProcessReader::zeroMemory()
-{
- totalVm.store(0);
- totalRss.store(0);
- totalPss.store(0);
- textVm.store(0);
- textRss.store(0);
- textPss.store(0);
- heapVm.store(0);
- heapRss.store(0);
- heapPss.store(0);
-}
-
#if defined(Q_OS_LINUX)
void ProcessReader::openCpuLoad()
@@ -134,10 +130,10 @@ qreal ProcessReader::readCpuLoad()
}
-bool ProcessReader::readMemory()
+bool ProcessReader::readMemory(Memory &mem)
{
- QByteArray smapsFile = "/proc/" + QByteArray::number(m_pid) + "/smaps";
- return readSmaps(smapsFile);
+ const QByteArray smapsFile = "/proc/" + QByteArray::number(m_pid) + "/smaps";
+ return readSmaps(smapsFile, mem);
}
static uint parseValue(const char *pl) {
@@ -146,18 +142,8 @@ static uint parseValue(const char *pl) {
return static_cast<uint>(strtoul(pl, nullptr, 10));
}
-bool ProcessReader::readSmaps(const QByteArray &smapsFile)
+bool ProcessReader::readSmaps(const QByteArray &smapsFile, Memory &mem)
{
- quint32 _totalVm = 0;
- quint32 _totalRss = 0;
- quint32 _totalPss = 0;
- quint32 _textVm = 0;
- quint32 _textRss = 0;
- quint32 _textPss = 0;
- quint32 _heapVm = 0;
- quint32 _heapRss = 0;
- quint32 _heapPss = 0;
-
struct ScopedFile {
~ScopedFile() { if (file) fclose(file); }
FILE *file = nullptr;
@@ -288,22 +274,22 @@ bool ProcessReader::readSmaps(const QByteArray &smapsFile)
if (foundTags < allTags)
break;
- _totalVm += vm;
- _totalRss += rss;
- _totalPss += pss;
+ mem.totalVm += vm;
+ mem.totalRss += rss;
+ mem.totalPss += pss;
static const char permRXP[] = { 'r', '-', 'x', 'p' };
static const char permRWP[] = { 'r', 'w', '-', 'p' };
if (!memcmp(permissions, permRXP, sizeof(permissions))) {
- _textVm += vm;
- _textRss += rss;
- _textPss += pss;
+ mem.textVm += vm;
+ mem.textRss += rss;
+ mem.textPss += pss;
} else if (!memcmp(permissions, permRWP, sizeof(permissions))
&& !isMainStack && (vm != 8192 || hasInode || !wasPrivateOnly) // try to exclude stack
&& !hasInode) {
- _heapVm += vm;
- _heapRss += rss;
- _heapPss += pss;
+ mem.heapVm += vm;
+ mem.heapRss += rss;
+ mem.heapPss += pss;
}
static const char permP[] = { '-', '-', '-', 'p' };
@@ -315,22 +301,15 @@ bool ProcessReader::readSmaps(const QByteArray &smapsFile)
}
}
- if (ok) {
- // publish the readings
- totalVm.store(_totalVm);
- totalRss.store(_totalRss);
- totalPss.store(_totalPss);
- textVm.store(_textVm);
- textRss.store(_textRss);
- textPss.store(_textPss);
- heapVm.store(_heapVm);
- heapRss.store(_heapRss);
- heapPss.store(_heapPss);
- }
-
return ok;
}
+bool ProcessReader::testReadSmaps(const QByteArray &smapsFile)
+{
+ memory = Memory();
+ return readSmaps(smapsFile, memory);
+}
+
#elif defined(Q_OS_MACOS)
void ProcessReader::openCpuLoad()
@@ -342,7 +321,7 @@ qreal ProcessReader::readCpuLoad()
return 0.0;
}
-bool ProcessReader::readMemory()
+bool ProcessReader::readMemory(Memory &mem)
{
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
@@ -352,8 +331,8 @@ bool ProcessReader::readMemory()
return false;
}
- totalRss.store(t_info.resident_size);
- totalVm.store(t_info.virtual_size);
+ mem.totalRss = t_info.resident_size;
+ mem.totalVm = t_info.virtual_size;
return true;
}
@@ -369,8 +348,9 @@ qreal ProcessReader::readCpuLoad()
return 0.0;
}
-bool ProcessReader::readMemory()
+bool ProcessReader::readMemory(Memory &mem)
{
+ Q_UNUSED(mem)
return false;
}
diff --git a/src/monitor-lib/processreader.h b/src/monitor-lib/processreader.h
index 9d63790f..d1eace4a 100644
--- a/src/monitor-lib/processreader.h
+++ b/src/monitor-lib/processreader.h
@@ -42,7 +42,7 @@
#pragma once
-#include <QAtomicInteger>
+#include <QMutex>
#include <QElapsedTimer>
#include <QObject>
@@ -57,6 +57,27 @@ QT_BEGIN_NAMESPACE_AM
class ProcessReader : public QObject {
Q_OBJECT
+
+public:
+ QMutex mutex;
+ qreal cpuLoad;
+ struct Memory {
+ quint32 totalVm = 0;
+ quint32 totalRss = 0;
+ quint32 totalPss = 0;
+ quint32 textVm = 0;
+ quint32 textRss = 0;
+ quint32 textPss = 0;
+ quint32 heapVm = 0;
+ quint32 heapRss = 0;
+ quint32 heapPss = 0;
+ } memory;
+
+#if defined(Q_OS_LINUX)
+ // solely for testing purposes
+ bool testReadSmaps(const QByteArray &smapsFile);
+#endif
+
public slots:
void update();
void setProcessId(qint64 pid);
@@ -65,32 +86,14 @@ public slots:
signals:
void updated();
-public:
- QAtomicInteger<quint32> cpuLoad;
-
- QAtomicInteger<quint32> totalVm;
- QAtomicInteger<quint32> totalRss;
- QAtomicInteger<quint32> totalPss;
- QAtomicInteger<quint32> textVm;
- QAtomicInteger<quint32> textRss;
- QAtomicInteger<quint32> textPss;
- QAtomicInteger<quint32> heapVm;
- QAtomicInteger<quint32> heapRss;
- QAtomicInteger<quint32> heapPss;
-
-#if defined(Q_OS_LINUX)
- // it's public solely for testing purposes
- bool readSmaps(const QByteArray &smapsFile);
-#endif
- static constexpr qreal cpuLoadFactor = 1000000.0;
-
private:
void openCpuLoad();
qreal readCpuLoad();
- bool readMemory();
- void zeroMemory();
+ bool readMemory(Memory &mem);
#if defined(Q_OS_LINUX)
+ bool readSmaps(const QByteArray &smapsFile, Memory &mem);
+
QScopedPointer<SysFsReader> m_statReader;
#endif
QElapsedTimer m_elapsedTime;