bdca252114bbe53a5b2faa2dde2700ed35f86ba1
[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 foreach (const QString &row, readAllFile("/proc/meminfo").split(QChar('\n'), QString::SkipEmptyParts))
57 {
58 QStringList tokens = row.split(QChar(' '), QString::SkipEmptyParts);
59 if (tokens.size() != 3)
60 continue;
61
62 if (tokens[0] == "MemTotal:")
63 memTotal = tokens[1].toULong();
64 else if(tokens[0] == "MemFree:")
65 memFree = tokens[1].toULong();
66 else if(tokens[0] == "Buffers:")
67 memBuffers = tokens[1].toULong();
68 else if(tokens[0] == "Cached:")
69 memCached = tokens[1].toULong();
70 else if(tokens[0] == "SwapTotal:")
71 swapTotal = tokens[1].toULong();
72 else if(tokens[0] == "SwapFree:")
73 swapFree = tokens[1].toULong();
74 }
75
76 if (mSource == "memory")
77 {
78 if (memTotal)
79 {
80 float memTotal_d = static_cast<float>(memTotal);
81 float applications_d = static_cast<float>(memTotal - memFree - memBuffers - memCached) / memTotal_d;
82 float buffers_d = static_cast<float>(memBuffers) / memTotal_d;
83 float cached_d = static_cast<float>(memCached) / memTotal_d;
84
85 emit memoryUpdate(applications_d, buffers_d, cached_d);
86 }
87 }
88 else if (mSource == "swap")
89 {
90 if (swapTotal)
91 {
92 float swapUsed_d = static_cast<float>(swapTotal - swapFree) / static_cast<float>(swapTotal);
93
94 emit swapUpdate(swapUsed_d);
95 }
96 }
97 }
98
99 QString MemStatPrivate::defaultSource()
100 {
101 return "memory";
102 }
103
104 MemStat::MemStat(QObject *parent)
105 : BaseStat(parent)
106 {
107 impl = new MemStatPrivate(this);
108 baseimpl = impl;
109
110 connect(impl, SIGNAL(memoryUpdate(float,float,float)), this, SIGNAL(memoryUpdate(float,float,float)));
111 connect(impl, SIGNAL(swapUpdate(float)), this, SIGNAL(swapUpdate(float)));
112 }
113
114 MemStat::~MemStat()
115 {
116 }
117
118 }