Drop Qt foreach.
[lxde/libsysstat.git] / memstat.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 **
4 ** SysStat is a Qt-based interface to system statistics
5 **
6 ** Authors:
7 ** Copyright (c) 2009 - 2012 Kuzma Shapran <Kuzma.Shapran@gmail.com>
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Lesser General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2.1 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Lesser General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Lesser General Public
20 ** License along with this library;
21 ** if not, write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 **
24 ** END_COMMON_COPYRIGHT_HEADER */
25
26
27 #include "memstat.h"
28 #include "memstat_p.h"
29
30
31 namespace SysStat {
32
33 MemStatPrivate::MemStatPrivate(MemStat *parent)
34 : BaseStatPrivate(parent)
35 {
36 mSource = defaultSource();
37
38 connect(mTimer, SIGNAL(timeout()), SLOT(timeout()));
39
40 mSources << "memory" << "swap";
41 }
42
43 MemStatPrivate::~MemStatPrivate()
44 {
45 }
46
47 void MemStatPrivate::timeout()
48 {
49 qulonglong memTotal = 0;
50 qulonglong memFree = 0;
51 qulonglong memBuffers = 0;
52 qulonglong memCached = 0;
53 qulonglong swapTotal = 0;
54 qulonglong swapFree = 0;
55
56 const QStringList rows = readAllFile("/proc/meminfo").split(QChar('\n'), QString::SkipEmptyParts);
57 for (const QString &row : rows)
58 {
59 QStringList tokens = row.split(QChar(' '), QString::SkipEmptyParts);
60 if (tokens.size() != 3)
61 continue;
62
63 if (tokens[0] == "MemTotal:")
64 memTotal = tokens[1].toULong();
65 else if(tokens[0] == "MemFree:")
66 memFree = tokens[1].toULong();
67 else if(tokens[0] == "Buffers:")
68 memBuffers = tokens[1].toULong();
69 else if(tokens[0] == "Cached:")
70 memCached = tokens[1].toULong();
71 else if(tokens[0] == "SwapTotal:")
72 swapTotal = tokens[1].toULong();
73 else if(tokens[0] == "SwapFree:")
74 swapFree = tokens[1].toULong();
75 }
76
77 if (mSource == "memory")
78 {
79 if (memTotal)
80 {
81 float memTotal_d = static_cast<float>(memTotal);
82 float applications_d = static_cast<float>(memTotal - memFree - memBuffers - memCached) / memTotal_d;
83 float buffers_d = static_cast<float>(memBuffers) / memTotal_d;
84 float cached_d = static_cast<float>(memCached) / memTotal_d;
85
86 emit memoryUpdate(applications_d, buffers_d, cached_d);
87 }
88 }
89 else if (mSource == "swap")
90 {
91 if (swapTotal)
92 {
93 float swapUsed_d = static_cast<float>(swapTotal - swapFree) / static_cast<float>(swapTotal);
94
95 emit swapUpdate(swapUsed_d);
96 }
97 }
98 }
99
100 QString MemStatPrivate::defaultSource()
101 {
102 return "memory";
103 }
104
105 MemStat::MemStat(QObject *parent)
106 : BaseStat(parent)
107 {
108 impl = new MemStatPrivate(this);
109 baseimpl = impl;
110
111 connect(impl, SIGNAL(memoryUpdate(float,float,float)), this, SIGNAL(memoryUpdate(float,float,float)));
112 connect(impl, SIGNAL(swapUpdate(float)), this, SIGNAL(swapUpdate(float)));
113 }
114
115 MemStat::~MemStat()
116 {
117 }
118
119 }